Home > data-analysis > extraction > extract_multi_measures.m

extract_multi_measures

PURPOSE ^

{

SYNOPSIS ^

function [datamatrix, hd] = extract_multi_measures(var_list, subexpID, filename, args)

DESCRIPTION ^

{

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [datamatrix, hd] = extract_multi_measures(var_list, subexpID, filename, args)
0002 %{
0003 Check demo_extract_multi_measures.m for usage of these parameters
0004 
0005 args.label_matrix
0006 args.label_names
0007 args.cevent_measures
0008 args.cont_measures
0009 args.measures
0010 args.persubject
0011 
0012 Any argument that is supported by get_chunks (thus,
0013 get_variable_by_grouping) is also supported here.
0014 
0015 %}
0016 
0017 if ~exist('args', 'var') || isempty(args)
0018     args = struct([]);
0019 end
0020 
0021 if ~iscell(var_list)
0022     var_list = {var_list};
0023 end
0024 
0025 if exist('filename', 'var') && ~isempty(filename)
0026     fid = fopen(filename, 'w');
0027     if ~fid
0028         error('Cannot open %s\n', filename);
0029     end
0030     fclose(fid);
0031 end
0032 
0033 if isfield(args, 'cevent_ranges')
0034     ranges = args.cevent_ranges;
0035 elseif isfield(args, 'event_ranges')
0036     ranges = args.event_ranges;
0037 else
0038     ranges = [];
0039 end
0040 
0041 if ~isfield(args, 'persubject')
0042     persubject = 0;
0043 else
0044     persubject = args.persubject;
0045 end
0046 
0047 subs = cIDs(subexpID);
0048 
0049 numvar = numel(var_list);
0050 
0051 datamatrix = cell(1000,100); %preallocate array with up to 1000 subjects and 100 variables
0052 stats = cell(numel(subs), numvar);
0053 prefixdata = cell(numel(subs), 1);
0054 
0055 if isfield(args, 'label_names')
0056     if ~iscell(args.label_names)
0057         args.label_names = {args.label_names};
0058     end
0059     ln = numel(args.label_names);
0060     label_names = args.label_names;
0061 else
0062     ln = 1;
0063     label_names{1} = '';
0064 end
0065 
0066 if persubject
0067     if isfield(args, 'cevent_name')
0068         h1 = sprintf(',,base:%s,', args.cevent_name);
0069     else
0070         h1 = ',,,';
0071     end
0072     h2 = ',,,';
0073     h3 = ',,,';
0074     h4 = 'subID,expID,instanceID,';
0075 else
0076     if isfield(args, 'cevent_name')
0077         h1 = sprintf(',,base:%s,,,,,', args.cevent_name);
0078     else
0079         h1 = ',,base_ranges,,,,,';
0080     end
0081     h2 = ',,,,,,,';
0082     h3 = ',,,,,,,';
0083     h4 = 'subID,expID,onset,offset,category,trialsID,instanceID,';
0084 end
0085 
0086 numcol = cell(1,numvar);
0087 numcol = cellfun(@(a) num2cell(zeros(1,100)), numcol, 'un', 0);
0088 for s = 1:numel(subs)
0089     fprintf('\nProcessing %d\n', subs(s));
0090     %first passthrough
0091     if ~isempty(ranges)
0092         if isfield(args, 'cevent_ranges')
0093             args.cevent_ranges = ranges(s);
0094         elseif isfield(args, 'event_ranges')
0095             args.event_ranges = ranges(s);
0096         end
0097     end
0098     for v = 1:numvar
0099         [~,extra,thisstat, ~] = get_chunks(var_list{v}, subs(s), args);
0100         stats{s,v} = thisstat;
0101         clear setnumcol;
0102         for w = 1:numel(thisstat)
0103             if ~isempty(thisstat{1,w})
0104                 if isfield(thisstat{1,w}, 'categories')
0105                     if numcol{1,v}{1,w} == 0
0106                         setnumcol = thisstat{1,w}.categories;
0107                     else
0108                         setnumcol = unique(cat(2, numcol{1,v}{1,w}, thisstat{1,w}.categories));
0109                     end
0110                 end
0111             end
0112             if exist('setnumcol', 'var')
0113                 numcol{1,v}{1,w} = setnumcol;
0114             end
0115         end
0116         if isempty(prefixdata{s,1}) && ~isempty(extra)
0117             if persubject
0118                 prefixdata{s,1} = [subs(s) sub2exp(subs(s)) s];
0119             else
0120                 irsize = size(extra.individual_ranges);
0121                 ir = nan(irsize(1), 3);
0122                 ir(:,1:irsize(2)) = extra.individual_ranges;
0123                 % splits up cevent ranges into trials so that we can easily
0124                 % tag the trial number to each cevent
0125                 trials_id = extract_ranges(ir, 'cevent', extra.trials);
0126                 trials_id = cellfun(@(a,b) [a repmat(b, size(a, 1), 1)], trials_id, num2cell(extra.trials(:,end)), 'un', 0);
0127                 ir = vertcat(trials_id{:});
0128                 prefixdata{s,1} = [extra.sub_list sub2exp(extra.sub_list) ir (1:irsize(1))'];
0129             end
0130         end
0131     end
0132 end
0133 
0134 for s = 1:numel(subs)
0135     i = 1;
0136     datamatrix{s,i} = prefixdata{s,1};
0137     numrow = size(prefixdata{s,1}, 1);
0138     i = i + 1;
0139     
0140     for v = 1:numvar
0141         if s == 1
0142             h1 = strcat(h1, var_list{v});
0143         end
0144         datatype = get_data_type(var_list{v});
0145         
0146         if isfield(args, 'measures')
0147             measures = args.measures{v};
0148         else
0149             switch datatype
0150                 case {'cevent', 'event', 'cstream'}
0151                     if isfield(args, 'cevent_measures')
0152                         measures = args.cevent_measures;
0153                     else
0154                         measures = {'individual_prop_by_cat', 'individual_mean_dur_by_cat', 'individual_freq_by_cat'};
0155                     end
0156                 case 'cont'
0157                     if isfield(args, 'cont_measures')
0158                         measures = args.cont_measures;
0159                     else
0160                         measures = {'individual_mean', 'individual_median'};
0161                     end
0162             end
0163         end
0164         
0165         
0166         if ~iscell(measures)
0167             measures = {measures};
0168         end
0169         
0170         % We don't want to get a measure during each trial, then average 4 trials,
0171         % instead concatenate all data across trials then find the measure.
0172         % This can be achieved in _cal_stats by not specifying the
0173         % 'individual_' prefix.
0174         if persubject && ~isfield(args, 'cevent_ranges') && ~isfield(args, 'event_ranges')
0175             measures = cellfun(@(a) strrep(a, 'individual_', ''), measures, 'un', 0);
0176             measures = cellfun(@(a) strrep(a, 'number', 'total_number'), measures, 'un', 0);
0177         end
0178         
0179         if isfield(args, 'label_matrix')
0180             measures = cellfun(@(a) strrep(a, '_by_cat', ''), measures, 'un', 0);
0181         end
0182         
0183         for m = 1:numel(measures)
0184             if ~isempty(strfind(measures{m}, '_by_cat'))
0185                 by_cat = 1;
0186             else
0187                 by_cat = 0;
0188             end
0189             
0190             if s == 1
0191                 h2 = strcat(h2, measures{m});
0192             end
0193             
0194             for w = 1:ln % ln is number of label_names
0195                 if s == 1
0196                     if by_cat
0197                         mc = repmat(',', 1, length(numcol{1,v}{1,w}));
0198                     else
0199                         mc = ',';
0200                     end
0201                     h1 = strcat(h1, mc);
0202                     h2 = strcat(h2, mc);
0203                     h3 = strcat(h3, label_names{w}, mc);
0204                     if by_cat
0205                         h4 = strcat(h4, sprintf('cat-%d,', numcol{1,v}{1,w}));
0206                     else
0207                         h4 = strcat(h4, 'cat-all,');
0208                     end
0209                 end
0210                 
0211                 if by_cat
0212                     prearray = nan(numrow, length(numcol{1,v}{1,w}));
0213                 else
0214                     prearray = nan(numrow, 1);
0215                 end
0216                 
0217                 if isempty(stats{s,v}{w})
0218                     datamatrix{s,i} = prearray;
0219                 else
0220                     thisstat = stats{s,v}{w}.(measures{m});
0221                     thisstat(isnan(thisstat)) = 0;
0222                     if by_cat
0223                         if isfield(stats{s,v}{w}, 'categories')
0224                             log = ismember(numcol{1,v}{1,w},stats{s,v}{w}.categories);
0225                         else
0226                             log = ones(1,length(numcol{1,v}{1,w})) == 1;
0227                         end
0228                     else
0229                         log = 1;
0230                     end
0231 
0232                     if persubject
0233                         prearray(:,log) = nanmean(thisstat, 1);
0234                     else
0235                         prearray(:,log) = thisstat;
0236                     end
0237                     datamatrix{s,i} = prearray;
0238                 end
0239                 i = i + 1;
0240             end
0241         end
0242     end
0243 end
0244 
0245 logmat = cellfun(@isempty, datamatrix);
0246 datamatrix(all(logmat, 2),:) = [];
0247 datamatrix(:,all(logmat,1)) = [];
0248 
0249 datamatrix = cell2mat(datamatrix);
0250 log = all(isnan(datamatrix), 1);
0251 datamatrix(:,log) = [];
0252 hd = {h1, h2, h3, h4};
0253 for h = 1:4
0254     head = hd{h};
0255     head = strrep(head, ',', ' ,');
0256     head = strsplit(head, ',');
0257     head = head(1:end-1);
0258     head(log) = [];
0259     head = strjoin(head, ',');
0260     head(isspace(head)) = '';
0261     hd{h} = head;
0262 end
0263 
0264 if exist('filename', 'var') && ~isempty(filename)
0265     write2csv(datamatrix, filename, hd);
0266 end
0267 
0268 
0269 end

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005