0001 function annot = bml_event2annot(cfg, event)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
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
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
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
0058
0059
0060
0061
0062
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);