Home > database > archived_scripts > ensemble_apply_crit.m

ensemble_apply_crit

PURPOSE ^

[data_st] = ensemble_apply_crit(data_st,filt);

SYNOPSIS ^

function [data_st] = ensemble_apply_crit(data_st,filt)

DESCRIPTION ^

 [data_st] = ensemble_apply_crit(data_st,filt);

 Filters data in the data structure (data_st) according to the exclusion and
 inclusion parameters specified in filt.exclude and filt.include.  Each of
 those structures contains a set of fields whose names are matched against the
 list of variable names in data_st.vars in order to find the column of data in
 data_st.data to filter.

 Example: filt.exclude.subject_id = {'tmp_*','01zin79271','08tgs78071'}
 would cause any rows that have subject IDs beginning with tmp_ to be removed,
 along with the specific subject IDs given by the 2nd and 3rd elements in the
 cell array of strings.

 Filtering based on inclusion criteria:  If multiple fields are specified, *all*
 of the conditions have to be true (logical AND) for the data to be retained.

 Filtering based on exclusion criteria: Any match on any specified field is
 the basis for exclusion (logical OR).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data_st] = ensemble_apply_crit(data_st,filt)
0002 % [data_st] = ensemble_apply_crit(data_st,filt);
0003 %
0004 % Filters data in the data structure (data_st) according to the exclusion and
0005 % inclusion parameters specified in filt.exclude and filt.include.  Each of
0006 % those structures contains a set of fields whose names are matched against the
0007 % list of variable names in data_st.vars in order to find the column of data in
0008 % data_st.data to filter.
0009 %
0010 % Example: filt.exclude.subject_id = {'tmp_*','01zin79271','08tgs78071'}
0011 % would cause any rows that have subject IDs beginning with tmp_ to be removed,
0012 % along with the specific subject IDs given by the 2nd and 3rd elements in the
0013 % cell array of strings.
0014 %
0015 % Filtering based on inclusion criteria:  If multiple fields are specified, *all*
0016 % of the conditions have to be true (logical AND) for the data to be retained.
0017 %
0018 % Filtering based on exclusion criteria: Any match on any specified field is
0019 % the basis for exclusion (logical OR).
0020 
0021 % 01/25/07 Petr Janata
0022 
0023 crit_types_to_proc = fieldnames(filt);
0024 ntypes = length(crit_types_to_proc);
0025 
0026 nvars = length(data_st.vars);
0027 
0028 for itype = 1:ntypes
0029   type_str = crit_types_to_proc{itype};
0030   
0031   % Get a list of the fields to construct masks for
0032   flds = fieldnames(filt.(type_str));
0033   nflds = length(flds);
0034 
0035   % Loop over all of the fields associated with this criterion type
0036   curr_mask = [];
0037   for ifld = 1:nflds
0038     fld_str = flds{ifld};
0039 
0040     % Find the field string in the list of variable names
0041     data_col = strmatch(fld_str,data_st.vars);
0042     if isempty(data_col)
0043       fprintf('Did not find criterion field (%s) in list of variables\n');
0044       continue
0045     end
0046     
0047     % Check to see if fld_str is a structure containing limits
0048     if isstruct(filt.(type_str).(fld_str))
0049       limit_flds = fieldnames(filt.(type_str).(fld_str));
0050       nlim = length(limit_flds);
0051       
0052       tmp = true(size(data_st.data{data_col}));
0053       for ilim = 1:nlim
0054     limit_str = limit_flds{ilim};
0055     crit_val = filt.(type_str).(fld_str).(limit_str);
0056     
0057     % Make sure there is only one criterion value
0058     if length(crit_val) > 1
0059       fprintf('ensemble_apply_crit: Too many criterion values\n');
0060       continue
0061     end
0062     
0063     % Make sure some criteria are specified for this field
0064     if isempty(crit_val)
0065       continue
0066     end
0067     
0068     switch limit_str
0069       case 'start'
0070         tmp2 = data_st.data{data_col} > crit_val;
0071       case 'start_inc'
0072         tmp2 = data_st.data{data_col} >= crit_val;
0073       case 'stop'
0074         tmp2 = data_st.data{data_col} < crit_val;
0075       case 'stop_inc'
0076         tmp2 = data_st.data{data_col} <= crit_val;
0077     end % switch limit_str
0078 
0079     tmp = tmp & tmp2;  % conjoin masks
0080       end % for ilim
0081     else
0082       crit_vals = filt.(type_str).(fld_str);
0083       
0084       % Make sure some criteria are specified for this field
0085       if isempty(crit_vals)
0086     continue
0087       end
0088       
0089       tmp = ismember(data_st.data{data_col}, crit_vals);
0090       % Check to see if any of the criterion values have wildcards, in which case
0091       % we need to switch to regexp
0092       if iscellstr(crit_vals) | isstr(crit_vals)
0093     is_wild = ~cellfun('isempty',regexp(crit_vals,'[*]'));
0094     wild_idxs = find(is_wild);
0095     for iwild = 1:length(wild_idxs)
0096       tmp = tmp|~cellfun('isempty',regexp(data_st.data{data_col}, ...
0097           crit_vals{wild_idxs(iwild)})); 
0098     end
0099       end
0100     end % if isstruct(filt.(type_str).(fld_str))
0101     curr_mask(:,end+1) = tmp;
0102   end % for ifld
0103   
0104   % If it is an inclusion mask, collapse the mask across columns. We need all
0105   % of the conditions to be true
0106   if strcmp(type_str,'include')
0107     mask_vect = all(curr_mask,2);
0108     mask_vect = ~mask_vect; % toggle the mask to be an exclusion mask
0109   else
0110     mask_vect = any(curr_mask,2);
0111   end
0112   
0113   % Perform row extraction
0114   for ivar = 1:nvars
0115     data_st.data{ivar}(mask_vect,:) = [];
0116   end
0117   
0118 end % for itype=

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003