Home > bml > annot > bml_event2annot.m

bml_event2annot

PURPOSE ^

BML_EVENT2ANNOT creates a annot table from a events structure

SYNOPSIS ^

function annot = bml_event2annot(cfg, event)

DESCRIPTION ^

 BML_EVENT2ANNOT creates a annot table from a events structure

 Use as
   annot = bml_event2annot(cfg, event)
   annot = bml_event2annot(cfg.roi, event)

 cfg.roi
 cfg.event_type  - cells of char: event types selected
 cfg.coord       - logical: should file coordinates be appended (defaults false)
 cfg.starts      - float: t0 of the event structure. If empty no time
          correction is done
 cfg.Fs          - sampling frequency in Hz. Defaults to 1. 
 cfg.warn        - logical: should a warnings be issued. Defaults to true.  

 returns a annot table

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function annot = bml_event2annot(cfg, event)
0002 
0003 % BML_EVENT2ANNOT creates a annot table from a events structure
0004 %
0005 % Use as
0006 %   annot = bml_event2annot(cfg, event)
0007 %   annot = bml_event2annot(cfg.roi, event)
0008 %
0009 % cfg.roi
0010 % cfg.event_type  - cells of char: event types selected
0011 % cfg.coord       - logical: should file coordinates be appended (defaults false)
0012 % cfg.starts      - float: t0 of the event structure. If empty no time
0013 %          correction is done
0014 % cfg.Fs          - sampling frequency in Hz. Defaults to 1.
0015 % cfg.warn        - logical: should a warnings be issued. Defaults to true.
0016 %
0017 % returns a annot table
0018 
0019 if istable(cfg)
0020   cfg = struct('roi',cfg);
0021 end
0022 roi         = bml_getopt(cfg,'roi');
0023 event_type  = bml_getopt(cfg,'event_type');
0024 coord       = bml_getopt(cfg,'coord',false);
0025 cfg_starts  = bml_getopt(cfg,'starts');
0026 cfg_Fs      = bml_getopt(cfg,'Fs',1);
0027 cfg_warn    = bml_getopt(cfg,'warn',true);
0028 
0029 %selecting events
0030 if isempty(event_type)
0031   event_type = unique({event.type});
0032 else
0033   present_event_type = ismember(event_type,unique({event.type}));
0034   if ~all(present_event_type) && cfg_warn
0035     warning('selected event type %s not present. Available event types are: %s',...
0036       strjoin(event_type(~present_event_type)),strjoin(unique({event.type})));
0037   end
0038   event_type = event_type(present_event_type);
0039 end
0040 
0041 if ~isempty(roi)
0042   %creating raw
0043   roi = bml_sync_consolidate(bml_roi_confluence(roi));
0044   assert(height(roi)==1,"can't deal with more than one sync chunk after consolidation for now");
0045 
0046   annot=table();
0047   for i=1:length(event_type)
0048     i_event = event(ismember({event.type},event_type(i)));
0049     starts = bml_idx2time(roi,[i_event.sample])';
0050     ends = starts;
0051     type = {i_event.type}';
0052     value = [i_event.value]';  
0053     offset = [i_event.offset]';
0054     duration = [i_event.duration]';  
0055     sample = [i_event.sample]'; 
0056     i_annot = table(starts,ends,type,value,sample);
0057     %if ~isempty(offset)
0058     %  i_annot = [i_annot, offset];
0059     %end
0060     %if ~isempty(duration)
0061     %  i_annot = [i_annot, duration];
0062     %end
0063     annot = [annot; i_annot];
0064   end
0065 
0066   if coord
0067     annot = [annot repmat(roi(1,{'s1','t1','s2','t2','folder','name','nSamples','Fs'}),height(annot),1)];
0068     annot = bml_roi_table(annot);
0069   else
0070     annot = bml_annot_table(annot);
0071   end
0072   
0073 elseif ~isempty(cfg_starts) && ~isempty(cfg_Fs)
0074 
0075   annot=table();
0076   for i=1:length(event_type)
0077     i_event = event(ismember({event.type},event_type(i)));
0078     starts = (cfg_starts+[i_event.sample] ./ cfg_Fs)';
0079     ends = starts;
0080     type = {i_event.type}';
0081     value = [i_event.value]';  
0082     offset = [i_event.offset]';
0083     duration = [i_event.duration]';  
0084     sample = [i_event.sample]'; 
0085     i_annot = table(starts,ends,type,value,sample);
0086     annot = [annot; i_annot];
0087   end
0088 
0089 else
0090   error('Either roi or starts/Fs should be given');
0091   
0092 end
0093 
0094 annot = bml_annot_table(annot);

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