Home > data-analysis > extraction > get_chunks.m

get_chunks

PURPOSE ^

This function will extract the data in var_name during the provided time

SYNOPSIS ^

function [fchunks, fextra, fstats, args] = get_chunks(var_name, subexpID, args)

DESCRIPTION ^

 This function will extract the data in var_name during the provided time
 window. By default, the time window is simply all of the trials for that
 subject.
 e.g. Type into the command line the following,

 >>[chunks, extra, stats] = get_chunks('cevent_inhand_child', 4301)

 the output will be,
 
           chunks = 
 
               [22x3 double]
               [26x3 double]
               [13x3 double]
               [13x3 double]
 
 
           extra = 
 
               individual_ranges: [4x3 double]
               mask_has_variable: 1
               sub_list: [4x1 double]
 
 
           stats = 
 
               [1x1 struct]

 
 
 chunks is a cell array of size 4 x 1, each containing inhand data for one
 trial

 extra is a structure that gives information about how data was extracted.
 For instance, extra.individual_ranges gives the exact time windows of the
 4 trials.

 stats is a structure returned from the _cal_stats.m functions that shows
 basic statistics of the extracted data

 One can define a different time window instead of using the default
 trials. The most common way is to specify a cevent variable.

 >>args.cevent_name = 'cevent_speech_naming_local-id';
 >>args.cevent_values = 1:3;
 >>[chunks, extra, stats] = get_chunks('cevent_inhand_child', 4301, args)

 the output will be,
 
           chunks = 
 
               [1x3 double]
               [2x3 double]
               [2x3 double]
                   ...
                   ...
               [0x3 double]
               [0x3 double]
               [0x3 double]
 
 
           extra = 
 
               individual_ranges: [47x3 double]
               mask_has_variable: 1
               sub_list: [47x1 double]
 
 
           stats = 
 
               [1x1 struct]

 Now chunks is a cell array of size 47 x 1, each containing inhand data during
 one of the naming instances.

 Below is a list of additional arg paramaters that can be specified. Most
 of the paramters modify the time window in some manner before extracting data.

 For instance, using
   args.cevent_min_dur = 2
 will first eliminate all of the naming instances shorter than 2 seconds,
 and then do the extraction using the remaining instances.
 
 chunks now outputs a cell array of size 18 x 1



% LIST OF ARGS FOR GET_VARIABLE_BY_GROUPING (10/12/2015)
{

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [fchunks, fextra, fstats, args] = get_chunks(var_name, subexpID, args)
0002 % This function will extract the data in var_name during the provided time
0003 % window. By default, the time window is simply all of the trials for that
0004 % subject.
0005 % e.g. Type into the command line the following,
0006 %
0007 % >>[chunks, extra, stats] = get_chunks('cevent_inhand_child', 4301)
0008 %
0009 % the output will be,
0010 %
0011 %           chunks =
0012 %
0013 %               [22x3 double]
0014 %               [26x3 double]
0015 %               [13x3 double]
0016 %               [13x3 double]
0017 %
0018 %
0019 %           extra =
0020 %
0021 %               individual_ranges: [4x3 double]
0022 %               mask_has_variable: 1
0023 %               sub_list: [4x1 double]
0024 %
0025 %
0026 %           stats =
0027 %
0028 %               [1x1 struct]
0029 %
0030 %
0031 %
0032 % chunks is a cell array of size 4 x 1, each containing inhand data for one
0033 % trial
0034 %
0035 % extra is a structure that gives information about how data was extracted.
0036 % For instance, extra.individual_ranges gives the exact time windows of the
0037 % 4 trials.
0038 %
0039 % stats is a structure returned from the _cal_stats.m functions that shows
0040 % basic statistics of the extracted data
0041 %
0042 % One can define a different time window instead of using the default
0043 % trials. The most common way is to specify a cevent variable.
0044 %
0045 % >>args.cevent_name = 'cevent_speech_naming_local-id';
0046 % >>args.cevent_values = 1:3;
0047 % >>[chunks, extra, stats] = get_chunks('cevent_inhand_child', 4301, args)
0048 %
0049 % the output will be,
0050 %
0051 %           chunks =
0052 %
0053 %               [1x3 double]
0054 %               [2x3 double]
0055 %               [2x3 double]
0056 %                   ...
0057 %                   ...
0058 %               [0x3 double]
0059 %               [0x3 double]
0060 %               [0x3 double]
0061 %
0062 %
0063 %           extra =
0064 %
0065 %               individual_ranges: [47x3 double]
0066 %               mask_has_variable: 1
0067 %               sub_list: [47x1 double]
0068 %
0069 %
0070 %           stats =
0071 %
0072 %               [1x1 struct]
0073 %
0074 % Now chunks is a cell array of size 47 x 1, each containing inhand data during
0075 % one of the naming instances.
0076 %
0077 % Below is a list of additional arg paramaters that can be specified. Most
0078 % of the paramters modify the time window in some manner before extracting data.
0079 %
0080 % For instance, using
0081 %   args.cevent_min_dur = 2
0082 % will first eliminate all of the naming instances shorter than 2 seconds,
0083 % and then do the extraction using the remaining instances.
0084 %
0085 % chunks now outputs a cell array of size 18 x 1
0086 %
0087 %
0088 %
0089 %% LIST OF ARGS FOR GET_VARIABLE_BY_GROUPING (10/12/2015)
0090 %{
0091 
0092 args.cevent_name
0093         -- e.g. 'cevent_speech_naming_local-id'
0094 
0095 args.cevent_values
0096         -- e.g. [1 2 3]
0097 
0098 args.cevent_ranges
0099         -- e.g. {[30 35 1;
0100                   35 40 2;
0101                   37 42 2;]};
0102 
0103 args.whence
0104         -- options 'start' or 'end'
0105 
0106 args.interval
0107         -- e.g. [-5 0] or [-2 3]
0108 
0109 args.cevent_min_dur
0110         -- e.g. 2
0111 
0112 args.cevent_max_dur
0113         -- e.g. 5
0114 
0115 args.load_from_directory
0116         -- e.g. 'extra_p/sbf'
0117 
0118 args.trial_indices
0119 args.trial_var_name
0120 args.trial_values
0121 args.event_min_dur
0122 args.event_max_dur
0123 args.within_ranges 
0124 
0125 args.event_name (See args.cevent_name)
0126 args.event_ranges (See args.cevent_ranges)
0127 args.data_quality_thresh 
0128 args.convert_cstream2cevent
0129 args.cont_cstream_max_gap 
0130 args.convert_event2cevent
0131 args.remove_empty_chunks 
0132 args.merge_thresh 
0133 args.min_dur_thresh
0134 args.max_dur_thresh 
0135 args.is_reassign_categories
0136 
0137 
0138 %}
0139 
0140 %%
0141 
0142 
0143 if strfind(var_name, 'demo')
0144     switch var_name
0145         case 'demo0'
0146             var_name = 'cont_vision_size_obj#_child';
0147             subexpID = [4301 4302];
0148         case 'demo1'
0149             var_name = 'cont_vision_size_obj#_child';
0150             subexpID = [4301 4302];
0151         case 'demo2'
0152             var_name = 'cevent_inhand_child';
0153             subexpID = [4301 4302];
0154         case 'demo3'
0155             var_name = 'cevent_inhand_child';
0156             subexpID = [4301 4302];
0157         case 'demo4'
0158             var_name = 'cont_vision_size_obj#_child';
0159             subexpID = [4301 4302];
0160             args.cevent_name = 'cevent_inhand_child';
0161             args.cevent_values = 1:3;
0162             args.label_matrix = [1 2 2; 2 1 2; 2 2 1];
0163         case 'demo5'
0164             var_name = 'cevent_inhand_child';
0165             subexpID = 4301;
0166             args.cevent_name = 'cevent_speech_naming_local-id';
0167             args.cevent_values = 1:3;
0168             args.label_matrix = [1 2 2 3; 2 1 2 3; 2 2 1 3];
0169         case 'demo6'
0170             var_name = 'cevent_eye_roi_with-parent_child';
0171             subexpID = [72];
0172             args.load_from_directory = 'extra_p/csuarez';
0173         otherwise
0174             error('%s was not a valid case option', var_name);
0175     end
0176 end
0177 if ~exist('args', 'var') || isempty(args)
0178 %     args = [];
0179     args = struct;
0180 end
0181 
0182 if isfield(args, 'label_matrix')
0183     lmflag = 1;
0184     width = numel(unique(args.label_matrix));
0185 else
0186     width = 1;
0187     lmflag = 0;
0188 end
0189 
0190 if ~isempty(strfind(var_name, 'obj#'))
0191     objflag = 1;
0192 else
0193     objflag = 0;
0194 end
0195 
0196 if isfield(args, 'cevent_name') || isfield(args, 'cevent_ranges')
0197     grouping = 'trialcevent';
0198 elseif isfield(args, 'event_name') || isfield(args, 'event_ranges')
0199     grouping = 'trialevent';
0200 else
0201     grouping = 'trial';
0202 end
0203 
0204 fstats = cell(1,width);
0205 fchunks = cell(1,width);
0206 fextra = [];
0207 
0208 subs = cIDs(subexpID);
0209 
0210 dt = get_data_type(var_name);
0211 
0212 fprintf('\n args for %s:\n', var_name);
0213 
0214 disp(args);
0215 
0216 all_data_chunks = cell(numel(subs), 1);
0217 data_extras = cell(numel(subs), 1);
0218 % allnumobj = get_num_obj(subs);
0219 for s = 1:numel(subs)
0220     expid = sub2exp(subs(s));
0221     if ismember(expid, [18 23])
0222         numobj = 5;
0223     elseif ismember(expid, 12)
0224         numobj = 27;
0225     else
0226         numobj = 3;
0227     end
0228     
0229     if ~isempty(strfind(var_name, 'obj#'))
0230         allvars = arrayfun(@(a) strrep(var_name, 'obj#', sprintf('obj%d', a)), 1:numobj, 'un', 0);
0231         numallvars = numel(allvars);
0232     elseif ~isempty(strfind(var_name, '#'))
0233         allvars = arrayfun(@(a) strrep(var_name, '#', sprintf('obj%d', a)), 1:numobj, 'un', 0);
0234         allvars = cat(2, allvars, {strrep(var_name, '#', 'head')});
0235         numallvars = numel(allvars);
0236     else
0237         allvars = {var_name};
0238         numallvars = 1;
0239     end
0240     
0241     chunks = cell(numel(allvars),1);
0242     for v = 1:numel(allvars)
0243         [chunks{v,1}, extras] = get_variable_by_grouping('sub', subs(s), allvars{v}, grouping, args);
0244         if ~iscell(chunks{v,1})
0245             return
0246         end
0247     end
0248     if strcmp(dt, 'cstream')
0249         chunks = cellfun(@(a) cellfun(@(b) cstream2cevent(b), a, 'un', 0), chunks, 'un', 0);
0250         dt = 'cevent';
0251     end
0252     chunks = horzcat(chunks{:});
0253     if ~isempty(chunks)
0254         if strcmp(grouping, 'trial') && size(extras.individual_ranges, 2) < 3
0255             extras.individual_ranges(:,3) = 1:size(extras.individual_ranges, 1);
0256         end
0257         if lmflag
0258             if numallvars == 1
0259                 tmp = cell(1,size(args.label_matrix, 2));
0260                 for o = 1:size(args.label_matrix, 2)
0261                     tmp{1,o} = cellfun(@(a) a(a(:,end) == o, :), chunks, 'un', 0);
0262                 end
0263                 chunks = horzcat(tmp{:});
0264             end
0265             
0266             if isfield(args, 'cevent_name') || isfield(args, 'cevent_ranges')
0267                 if size(extras.individual_ranges, 2) > 2
0268                     label_matrix = args.label_matrix;
0269                     targets = extras.individual_ranges;
0270                     %                 label_matrix = label_matrix(args.cevent_values, :);
0271                     [chunks, targets] = get_chunks_target(chunks, targets, label_matrix, dt);
0272                     extras.individual_ranges = targets;
0273                     extras.individual_range_dur = targets(:,2)-targets(:,1);
0274                     extras.sub_list = repmat(subs(s), size(targets, 1), 1);
0275                 else
0276                     error('cevent_name or cevent_ranges data has ben converted to an event due to a specific parameter, the label_matrix cannot be specified in this case');
0277                 end
0278             else
0279                 error('label matrix should be only be indicated when specifying cevent_name or cevent_ranges');
0280             end
0281         elseif objflag
0282             label_matrix = ones(size(chunks, 1), numallvars);
0283             targets = ones(size(chunks, 1), 1);
0284             [chunks, ~] = get_chunks_target(chunks, targets, label_matrix, dt);
0285             extras.sub_list = repmat(subs(s), size(targets, 1), 1);
0286         end
0287         all_data_chunks{s,:} = chunks;
0288     end
0289     
0290     data_extras{s,1} = extras;
0291 end
0292 log = cellfun(@isempty, all_data_chunks);
0293 all_data_chunks(log) = [];
0294 data_extras(log) = [];
0295 
0296 if ~isempty(all_data_chunks)
0297     for w = 1:width
0298         data_chunks = cellfun(@(a) a(:,w), all_data_chunks, 'un', 0);
0299         
0300         tmp_subs = cellfun(@(a) a.sub_list, data_extras, 'un', 0);
0301         fextra.sub_list = vertcat(tmp_subs{:});
0302         individual_ranges = cellfun(@(a) a.individual_ranges, data_extras, 'un', 0);
0303         fextra.individual_ranges = vertcat(individual_ranges{:});
0304         individual_range_dur = cellfun(@(a) a.individual_range_dur, data_extras, 'un', 0);
0305         args.individual_range_dur = vertcat(individual_range_dur{:});
0306         fextra.mask_has_variable = cellfun(@(a) a.mask_has_variable, data_extras);
0307         trials = cellfun(@(a) a.trials, data_extras, 'un', 0);
0308         fextra.trials = vertcat(trials{:});
0309         chunks = vertcat(data_chunks{:});
0310         
0311         if sum(cellfun(@isempty, chunks)) ~= numel(chunks)
0312             switch dt
0313                 case 'event'
0314                     stats = event_cal_stats(chunks, args);
0315                 case 'cevent'
0316                     stats = cevent_cal_stats(chunks, args);
0317                 case 'cont'
0318                     stats = cont_cal_stats(chunks, args);
0319                 case 'cstream'
0320                     cstream_stats = cstream_cal_stats(chunks, args);
0321                     stats = cstream_cal_stats(chunks, args, [], 1);
0322                     stats = stats.cevent_stats;
0323                     stats.cstream_stats = cstream_stats;
0324             end
0325             fstats{1,w} = stats;
0326         end
0327         fchunks{1,w} = chunks;
0328         fextra = orderfields(fextra);
0329     end
0330     fchunks = horzcat(fchunks{:});
0331 end
0332 end

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