Home > bml > signal > bml_redefinetrial.m

bml_redefinetrial

PURPOSE ^

BML_REDEFINETRIAL creates new epoching from a raw object (not necessarily continuous)

SYNOPSIS ^

function [redefined, redefined_epoch] = bml_redefinetrial(cfg, raw)

DESCRIPTION ^

 BML_REDEFINETRIAL creates new epoching from a raw object (not necessarily continuous)

 Use as
   redefined = bml_redefinetrial(cfg, raw)

 raw - FT_DATAYPR_RAW to be re-epoched with time in global coordinates
 cfg - configuraton structure
 cfg.epoch - ANNOT table with new epoching
 cfg.t0 - reference time for each epoch. If not specified the time is kept in
          global time coordinates. Can be string or char that matches a 
          variable of cfg.epoch, or a numeric vector of the same length than cfg.epoch
 cfg.regularize - if true, resulting times are forced to be equal.
          Defaults to false
 cfg.warn - logical indicating if warnings should be issued. Defaults to true

 returns a raw with new trials. The epoch ANNOT is added as a new field in the 
 raw, changing the id to match the index of the corresponding trials if necessary.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [redefined, redefined_epoch] = bml_redefinetrial(cfg, raw)
0002 
0003 % BML_REDEFINETRIAL creates new epoching from a raw object (not necessarily continuous)
0004 %
0005 % Use as
0006 %   redefined = bml_redefinetrial(cfg, raw)
0007 %
0008 % raw - FT_DATAYPR_RAW to be re-epoched with time in global coordinates
0009 % cfg - configuraton structure
0010 % cfg.epoch - ANNOT table with new epoching
0011 % cfg.t0 - reference time for each epoch. If not specified the time is kept in
0012 %          global time coordinates. Can be string or char that matches a
0013 %          variable of cfg.epoch, or a numeric vector of the same length than cfg.epoch
0014 % cfg.regularize - if true, resulting times are forced to be equal.
0015 %          Defaults to false
0016 % cfg.warn - logical indicating if warnings should be issued. Defaults to true
0017 %
0018 % returns a raw with new trials. The epoch ANNOT is added as a new field in the
0019 % raw, changing the id to match the index of the corresponding trials if necessary.
0020 
0021 epoch      = bml_annot_table(bml_getopt(cfg,'epoch'),'epoch');
0022 t0         = bml_getopt(cfg,'t0');
0023 regularize = bml_getopt(cfg,'regularize',false);
0024 warn       = bml_getopt(cfg,'warn',true);
0025 
0026 if regularize
0027   keyboard %have to implement this
0028 end
0029 
0030 if ~isempty(t0)
0031   if isstring(t0) || ischar(t0)
0032     t0 = cellstr(t0);
0033   end
0034     
0035   if iscellstr(t0)
0036     assert(numel(t0)==1,"single t0 variable required");
0037     if ismember(t0,epoch.Properties.VariableNames)
0038       t0 = epoch.(t0{1});
0039     else
0040       error("t0 doesn't match any variable in epoch");
0041     end
0042   end
0043   
0044   if isnumeric(t0)
0045     assert(length(t0)==height(epoch),"incorrect length for t0");
0046   else
0047     error("t0 should be a numeric vector, or be the name of a numeric variable in epoch");
0048   end
0049 end
0050 
0051 %creating output raw
0052 redefined = [];
0053 redefined.trial = {};
0054 redefined.time = {};
0055 redefined_epoch = table();
0056 redefined.label = raw.label;
0057 if ismember('hdr',fields(raw))
0058   redefined.hdr = raw.hdr;
0059 end
0060 
0061 %extracting trial info from raw
0062 raw_trial = bml_annot_table(bml_raw2annot(raw),'raw');
0063 raw_trial.midpoint = (raw_trial.starts + raw_trial.ends)/2;
0064 
0065 %looping though epochs
0066 for i=1:height(epoch)
0067   
0068   %intersecting epochs with raw's trials
0069   new_row = epoch(i,:);
0070   i_raw_trial = bml_annot_intersect(struct('keep','x'),raw_trial,epoch(i,:));
0071   
0072   %if no intersection, move to next epoch
0073   if isempty(i_raw_trial)
0074     if warn
0075       warning("epoch %i not found in raw",i);
0076     end
0077     continue
0078   end
0079   
0080   %selecting best intersection (largest duration and centered)
0081   if height(i_raw_trial)>1
0082     max_duration=max(i_raw_trial.duration);
0083     i_raw_trial = i_raw_trial(i_raw_trial.duration == max_duration,:);
0084     [~,min_i]=min(abs((i_raw_trial.starts+i_raw_trial.ends)/2 - i_raw_trial.midpoint));
0085     i_raw_trial = i_raw_trial(min_i,:);  
0086     %if warn
0087     %  warning("several trials of raw match to epoch %i. Selecting trial %i",i,i_raw_trial.raw_id(min_i));
0088     %end
0089   end
0090   
0091   %partial epoch
0092   if (epoch.duration(i) > i_raw_trial.duration) && warn
0093     warning('partial epoch %i loaded',i);
0094   end
0095   
0096   new_row.starts = i_raw_trial.starts;
0097   new_row.ends = i_raw_trial.ends;
0098   
0099   %cropping
0100   [s,e]=bml_crop_idx(i_raw_trial,i_raw_trial.starts,i_raw_trial.ends);
0101 
0102     if s < 1; s=1; end
0103     if e > i_raw_trial.nSamples; e = i_raw_trial.nSamples; end
0104 
0105   %creating trial
0106   new_row.id = numel(redefined.trial) + 1;
0107   redefined.trial{new_row.id} = raw.trial{i_raw_trial.raw_id}(:,s:e);
0108   redefined.time{new_row.id} = raw.time{i_raw_trial.raw_id}(:,s:e);
0109   redefined_epoch = [redefined_epoch; new_row];
0110   %cropped.sampleinfo(i,:) = [s,e];
0111   
0112   %changing time reference if t0 is present
0113   if ~isempty(t0)
0114     redefined.time{new_row.id} = redefined.time{new_row.id} - t0(i);
0115   end
0116  
0117 end
0118 
0119 
0120

Generated on Tue 25-Sep-2018 10:08:19 by m2html © 2005