


BML_ANNOT_CONSOLIDATE returns a consolidated annotation table
Use as
annot = bml_annot_consolidate(cfg, annot1);
The first argument cfg is a configuration structure, which can contain
the following field:
cfg.criterion - function handle: consolidation criteria. This function should
accept a table of candidate annotations to consolidate and
return a true or false.
cfg.description - string: description of the output annot table
cfg.additive - cellstr with names of variables to be treated as additive
defaults to empty
Returns a annotation table with the folloing variables:
cons_duration - sum of the consolidated durations
id_starts - first original id of the consolidated row
id_ends - last original id of the consolidated row
cons_n - number of consolidated rows
EXAMPLES
========
%detecting stretches of constant depth in neuroomega
cfg=[];
cfg.criterion = @(x) (length(unique(x.depth))==1) && (abs((max(x.ends)-min(x.starts))-sum(x.duration))<10e-3);
neuro_cons_depth = bml_annot_consolidate(cfg,info_neuroomega);
%grouping annotations in fours
cfg=[];
cfg.criterion = @(x) height(x)<=4
grouped_annot = bml_annot_consolidate(cfg,annot);


0001 function annot = bml_annot_consolidate(cfg, x) 0002 0003 % BML_ANNOT_CONSOLIDATE returns a consolidated annotation table 0004 % 0005 % Use as 0006 % annot = bml_annot_consolidate(cfg, annot1); 0007 % 0008 % The first argument cfg is a configuration structure, which can contain 0009 % the following field: 0010 % cfg.criterion - function handle: consolidation criteria. This function should 0011 % accept a table of candidate annotations to consolidate and 0012 % return a true or false. 0013 % cfg.description - string: description of the output annot table 0014 % cfg.additive - cellstr with names of variables to be treated as additive 0015 % defaults to empty 0016 % 0017 % Returns a annotation table with the folloing variables: 0018 % cons_duration - sum of the consolidated durations 0019 % id_starts - first original id of the consolidated row 0020 % id_ends - last original id of the consolidated row 0021 % cons_n - number of consolidated rows 0022 % 0023 % EXAMPLES 0024 % ======== 0025 % 0026 % %detecting stretches of constant depth in neuroomega 0027 % cfg=[]; 0028 % cfg.criterion = @(x) (length(unique(x.depth))==1) && (abs((max(x.ends)-min(x.starts))-sum(x.duration))<10e-3); 0029 % neuro_cons_depth = bml_annot_consolidate(cfg,info_neuroomega); 0030 % 0031 % %grouping annotations in fours 0032 % cfg=[]; 0033 % cfg.criterion = @(x) height(x)<=4 0034 % grouped_annot = bml_annot_consolidate(cfg,annot); 0035 0036 x = bml_annot_table(x,[],inputname(2)); 0037 0038 description = bml_getopt(cfg,'description', ['cons_' x.Properties.Description]); 0039 criterion = bml_getopt(cfg,'criterion',[]); 0040 additive = bml_getopt(cfg,'additive',{}); 0041 0042 if ~isa(criterion, 'function_handle') 0043 error('''criterion'' should be a function handle'); 0044 end 0045 0046 i=1; j=1; 0047 tmp=collapse_table_rows(x(1,:),additive); 0048 annot = cell2table(cell(0,width(tmp))); 0049 annot.Properties.VariableNames = tmp.Properties.VariableNames; 0050 0051 if height(x)<=1 0052 annot = collapse_table_rows(x,additive); 0053 annot.id=[]; 0054 annot = bml_annot_table(annot,description); 0055 return 0056 end 0057 0058 while i<=height(x) 0059 if j==1 0060 curr_s=collapse_table_rows(x(i,:),additive); 0061 end 0062 0063 merge_s = x(i:(i+j),:); 0064 0065 if criterion(merge_s) 0066 curr_s = collapse_table_rows(merge_s,additive); 0067 j = j + 1; 0068 if i + j > height(x) 0069 annot = [annot; curr_s]; 0070 i = height(x)+1; 0071 end 0072 else 0073 annot = [annot;curr_s]; 0074 i = i + j; 0075 j = 1; 0076 if i == height(x) %adding last register 0077 curr_s = collapse_table_rows(x(i,:),additive); 0078 annot = [annot; curr_s]; 0079 i = height(x)+1; 0080 end 0081 end 0082 end 0083 0084 annot.id=[]; 0085 annot = bml_annot_table(annot,description); 0086 0087 0088 0089 function collapsed = collapse_table_rows(merge_s,additive) 0090 % 0091 % Private function. Collapses a table to a record 0092 0093 collapsed = table(... 0094 min(merge_s.starts),... 0095 max(merge_s.ends),... 0096 sum(merge_s.duration),... 0097 min(merge_s.id),... 0098 max(merge_s.id),... 0099 length(merge_s.id),... 0100 'VariableNames',{'starts','ends','cons_duration','id_starts','id_ends','cons_n'}); 0101 0102 vars = setdiff(merge_s.Properties.VariableNames,[collapsed.Properties.VariableNames,additive]); 0103 row=[]; 0104 for i=1:length(vars) 0105 uval = unique(merge_s.(vars{i})); 0106 if length(uval)==1 0107 row.(vars{i}) = uval; 0108 elseif iscell(uval) 0109 row.(vars{i}) = {[]}; 0110 elseif isa(uval,'datetime') 0111 row.(vars{i}) = NaT; 0112 else 0113 row.(vars{i}) = nan; 0114 end 0115 end 0116 0117 %dealing with additive vars 0118 for i=1:length(additive) 0119 uval = sum(merge_s.(additive{i})); 0120 row.(vars{i}) = uval; 0121 end 0122 0123 collapsed = [collapsed, struct2table(row)]; 0124 0125 0126 0127 0128