Home > data-analysis > extraction > get_variable_by_regrouping.m

get_variable_by_regrouping

PURPOSE ^

GET_VARIABLE_BY_REGROUPING Get only the valid portion of a variable's data

SYNOPSIS ^

function results = get_variable_by_regrouping(input)

DESCRIPTION ^

GET_VARIABLE_BY_REGROUPING Get only the valid portion of a variable's data
according to different grouping method and parameters. 
 
 For very detailed example and explanation of how to set the parameters,
 please go to:
 http://einstein.psych.indiana.edu/visual/multi-lib/data-access/demo_get_variable_by_regrouping.m
 
 when the input variable contains more than 1 variable name (of same type)
 the output chunks will be containing processed data of regrouped chunks
 
 
 written by Linger Xu, txu@indiana.edu, Apr. 2012

 last update: June 20th, 2014

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function results = get_variable_by_regrouping(input)
0002 %GET_VARIABLE_BY_REGROUPING Get only the valid portion of a variable's data
0003 %according to different grouping method and parameters.
0004 %
0005 % For very detailed example and explanation of how to set the parameters,
0006 % please go to:
0007 % http://einstein.psych.indiana.edu/visual/multi-lib/data-access/demo_get_variable_by_regrouping.m
0008 %
0009 % when the input variable contains more than 1 variable name (of same type)
0010 % the output chunks will be containing processed data of regrouped chunks
0011 %
0012 %
0013 % written by Linger Xu, txu@indiana.edu, Apr. 2012
0014 %
0015 % last update: June 20th, 2014
0016 
0017 
0018 ROI_OFFSET = 100;
0019 
0020 % get sub_list according to user input
0021 if isfield(input, 'exp_list')
0022     sub_list = [];
0023     for expidx = 1:length(input.exp_list)
0024         sub_list = [sub_list; list_subjects(input.exp_list(expidx))];
0025     end
0026 elseif isfield(input, 'sub_list')
0027     sub_list = input.sub_list;
0028 else
0029     error('Either exp_list or sub_list should be specified.');
0030 end
0031 
0032 if isempty(sub_list)
0033     error('The subect list is empty.');
0034 end
0035 
0036 % if the variable to be extracted are continue variables or event
0037 % variables, the name input should be a cell - a list of variable names
0038 if ~iscell(input.var_name)
0039     extracted_var_type = get_data_type(input.var_name);
0040 %     if ~(strcmp(tmp_var_type, 'cstream') || strcmp(tmp_var_type, 'cevent'))
0041 %         error('Invalid input: VAR_NAME');
0042 %     end
0043 else
0044     extracted_var_type = get_data_type(input.var_name{1});
0045 %     if ~(strcmp(tmp_var_type, 'cont') || strcmp(tmp_var_type, 'event'))
0046 %         error('Invalid input: VAR_NAME');
0047 %     end
0048 end
0049 
0050 % create local vars specified by input
0051 var_name = input.var_name;
0052 groupid_matrix = input.groupid_matrix;
0053 groupid_matrix_list = unique(groupid_matrix);
0054 % cevent_category = input.cevent_category;
0055 if ~isfield(input, 'var_category')
0056     error(['Under all situations, the field ' ...
0057         'VAR_CATEGORY must be specified.']);
0058 else
0059     var_category = input.var_category;
0060 end
0061 
0062 %%
0063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0064 if iscell(input.var_name)
0065     if isfield(input, 'grouping')
0066         grouping = input.grouping;
0067         if ~strcmp(grouping, 'subcevent_cat')
0068             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0069 %                 error(['Currently, only when the grouping is subcevent_cat, ' ...
0070 %                     'this function works and thoroughly tested. It will be updated.']);
0071         end
0072     else
0073         grouping = 'subcevent_cat';
0074         input.grouping = grouping;
0075     end
0076     
0077     if strcmp(grouping, 'subcevent_cat') || strcmp(grouping, 'subevent_cat')
0078         for lmlidx = 1:length(groupid_matrix_list)
0079             results(lmlidx).regroup_label_id = groupid_matrix_list(lmlidx);
0080             results(lmlidx).chunks_count = 0;
0081             results(lmlidx).chunks = cell(length(sub_list), 1);
0082             results(lmlidx).sub_list = sub_list;
0083             results(lmlidx).dur_list = zeros(length(sub_list), 1);
0084         end
0085     elseif strcmp(grouping, 'trialcevent_cat') || strcmp(grouping, 'trialevent_cat')
0086         for lmlidx = 1:length(groupid_matrix_list)
0087             results(lmlidx).regroup_label_id = groupid_matrix_list(lmlidx);
0088             results(lmlidx).chunks_count = 0;
0089             results(lmlidx).chunks = [];
0090             results(lmlidx).sub_list = [];
0091             results(lmlidx).dur_list = [];
0092         end
0093     elseif strcmp(grouping, 'cevent') || strcmp(grouping, 'event') ...
0094             || strcmp(grouping, 'trialcevent') || strcmp(grouping, 'trialevent')
0095         for lmlidx = 1:length(groupid_matrix_list)
0096             results(lmlidx).regroup_label_id = groupid_matrix_list(lmlidx);
0097             results(lmlidx).chunks_count = 0;
0098             results(lmlidx).chunks = [];
0099             results(lmlidx).sub_list = [];
0100             results(lmlidx).dur_list = [];
0101         end
0102     end
0103     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0104     if isfield(input, 'cevent_name')
0105         if ~isfield(input, 'cevent_category')
0106             error(['When data are regrouped by cevents, the field ' ...
0107                 'CEVENT_CATEGORY must be specified.']);
0108         end
0109         
0110         extracted_var_len = 2;
0111         if strcmp(extracted_var_type, 'cevent')
0112             extracted_var_len = 3;
0113         end
0114         regroup_op_idx = [extracted_var_len extracted_var_len*2];
0115         
0116         if isfield(input, 'regroup_op')
0117             regroup_op = input.regroup_op; % it can be 'OR', 'AND', 'CAT', or 'AVG'
0118             
0119             if (~strcmp(regroup_op, 'CAT')) && (strcmp(extracted_var_type, 'event') || strcmp(extracted_var_type, 'cevent'))
0120                 error('For event/cevent variable, regrouping operation can only be CAT - concatenation');
0121             end
0122         else
0123             if strcmp(extracted_var_type, 'cstream')
0124                 regroup_op = 'OR';
0125             elseif strcmp(extracted_var_type, 'cont')
0126                 regroup_op = 'AVG';
0127             elseif strcmp(extracted_var_type, 'event')
0128                 regroup_op = 'CAT';
0129             elseif strcmp(extracted_var_type, 'cevent')
0130                 regroup_op = 'CAT';
0131             end
0132         end
0133         
0134         cevent_category = input.cevent_category;
0135         
0136         cevent_name = input.cevent_name;
0137 
0138         for ceventidx = 1 : length(cevent_category)
0139             input.cevent_values = cevent_category(ceventidx);
0140             chunks_list = cell(length(var_name), 1);
0141             chunk_sub_list = cell(length(var_name), 1);
0142             chunk_dur_list = cell(length(var_name), 1);
0143             
0144             for contidx = 1:length(var_name)
0145                 [chunks_one, extra_one] = get_variable_by_grouping('sub', sub_list, ...
0146                     var_name{contidx}, grouping, input);
0147                 if strcmp(extracted_var_type, 'cstream')
0148                     chunks_one_new = cellfun( ...
0149                         @(chunk_one) ...
0150                         cstream_reassign_categories(chunk_one, {},(groupid_matrix(contidx, ceventidx))+ROI_OFFSET), ...
0151                         chunks_one, ...
0152                         'UniformOutput', 0);
0153                     chunks_one = chunks_one_new;
0154                     
0155                     chunks_one_cat = vertcat(chunks_one_new{:});
0156                     roi_checking = unique(chunks_one_cat(:,2));
0157                     if length(roi_checking) > 2
0158                         error('cstream roi are not reassigned!');
0159                     end
0160                 elseif strcmp(extracted_var_type, 'cevent')
0161                     chunks_one_new = cellfun( ...
0162                         @(chunk_one) ...
0163                         cevent_reassign_categories(chunk_one, {},(groupid_matrix(contidx, ceventidx))+ROI_OFFSET), ...
0164                         chunks_one, ...
0165                         'UniformOutput', 0);
0166                     chunks_one = chunks_one_new;
0167 
0168                     chunks_one_cat = vertcat(chunks_one_new{:});
0169                     roi_checking = unique(chunks_one_cat(:,3));
0170                     if length(roi_checking) > 2
0171                         error('cevent roi are not reassigned!');
0172                     end
0173                 end
0174                 chunks_list{contidx} = chunks_one;
0175                 chunk_sub_list{contidx} = extra_one.sub_list;
0176                 chunk_dur_list{contidx} = extra_one.individual_range_dur;
0177             end
0178 
0179             label_column = groupid_matrix(:, ceventidx);
0180             label_column_list = unique(label_column);
0181             for lidx = 1:length(label_column_list)
0182                 label_one = label_column_list(lidx);
0183 %                 label_one_idx = find(groupid_matrix_list == label_one);
0184                 data_label_idx = find(label_column == label_one);
0185                 
0186                 if length(data_label_idx) > 1
0187                     chunk_tmp = cell(1,length(data_label_idx));
0188                     chunk_dur_tmp = cell(1,length(data_label_idx));
0189                     chunk_sub_tmp = cell(1,length(data_label_idx));
0190                     for dlidx = 1:length(data_label_idx)
0191                         chunk_tmp{dlidx} = chunks_list{data_label_idx(dlidx)};
0192                         chunk_dur_tmp{dlidx} = chunk_dur_list{data_label_idx(dlidx)};
0193                         chunk_sub_tmp{dlidx} = chunk_sub_list{data_label_idx(dlidx)};
0194                     end
0195                     chunk_tmp = horzcat(chunk_tmp{:});
0196                     chunk_tmp_new = cell(size(chunk_tmp, 1), 1); 
0197                     
0198                     switch regroup_op
0199                         case 'CAT'
0200                             for tmpci = 1:size(chunk_tmp, 1)
0201                                 chunk_tmp_new{tmpci,1} = vertcat(chunk_tmp{tmpci, :});
0202                             end
0203                         case 'OR'
0204                             for tmpci = 1:size(chunk_tmp, 1)
0205                                 tmp_one = horzcat(chunk_tmp{tmpci, :});
0206                                 if ~isempty(tmp_one)
0207                                     chunk_tmp_new{tmpci,1} = [tmp_one(:,1) nanmax(tmp_one(:,regroup_op_idx), [], 2)];
0208                                 end
0209                             end
0210                         case 'AVG'
0211                             for tmpci = 1:size(chunk_tmp, 1)
0212                                 tmp_one = horzcat(chunk_tmp{tmpci, :});
0213                                 if ~isempty(tmp_one)
0214                                     chunk_tmp_new{tmpci,1} = [tmp_one(:,1) nanmean(tmp_one(:,regroup_op_idx), 2)];
0215                                 end
0216                             end
0217                         case 'SUM'
0218                             for tmpci = 1:size(chunk_tmp, 1)
0219                                 tmp_one = horzcat(chunk_tmp{tmpci, :});
0220                                 if ~isempty(tmp_one)
0221                                     chunk_tmp_new{tmpci,1} = [tmp_one(:,1) nansum(tmp_one(:,regroup_op_idx), 2)];
0222                                 end
0223                             end
0224                         case 'AND'
0225                             for tmpci = 1:size(chunk_tmp, 1)
0226                                 tmp_one = horzcat(chunk_tmp{tmpci, :});
0227                                 if ~isempty(tmp_one)
0228                                     chunk_tmp_new{tmpci,1} = [tmp_one(:,1) nanmin(tmp_one(:,regroup_op_idx), [], 2)];
0229                                 end
0230                             end
0231                         otherwise
0232                             error('Invalid regroup operation!');
0233                     end
0234                         
0235                     chunk_dur_tmp = chunk_dur_tmp{1};
0236                     chunk_sub_tmp = chunk_sub_tmp{1};
0237                     chunk_tmp = chunk_tmp_new;
0238                 else
0239                     chunk_tmp = chunks_list{data_label_idx};
0240                     chunk_dur_tmp = chunk_dur_list{data_label_idx};
0241                     chunk_sub_tmp = chunk_sub_list{data_label_idx};
0242                 end
0243                 
0244 
0245                 if strcmp(grouping, 'subcevent_cat')
0246                     chunks_new = results(label_one).chunks;
0247                     chunks_dur_new = results(label_one).dur_list;
0248 
0249                     for sidx = 1:size(chunk_tmp, 1)
0250                         chunks_new{sidx, 1} = ...
0251                             vertcat(chunks_new{sidx, 1}, chunk_tmp{sidx});
0252                         chunks_dur_new(sidx, 1) = ...
0253                             chunk_dur_tmp(sidx) + chunks_dur_new(sidx);
0254                     end
0255                     results(label_one).chunks = chunks_new;
0256                     results(label_one).dur_list = chunks_dur_new;
0257                 elseif strcmp(grouping, 'trialcevent_cat')
0258                     chunks_new = results(label_one).chunks;
0259                     chunks_dur_new = results(label_one).dur_list;
0260 
0261                     for sidx = 1:size(chunk_tmp, 1)
0262                         chunks_new{sidx, 1} = ...
0263                             vertcat(chunks_new{sidx, 1}, chunk_tmp{sidx});
0264                         chunks_dur_new(sidx, 1) = ...
0265                             chunk_dur_tmp(sidx) + chunks_dur_new(sidx);
0266                     end
0267                     results(label_one).chunks = chunks_new;
0268                     results(label_one).dur_list = chunks_dur_new;
0269                 elseif strcmp(grouping, 'cevent')
0270                     chunk_count_one = length(chunk_tmp);
0271                     results(label_one).chunks = ...
0272                         [results(label_one).chunks; chunk_tmp];
0273                     results(label_one).sub_list = ...
0274                         [results(label_one).sub_list; chunk_sub_tmp];
0275                     results(label_one).dur_list = ...
0276                         [results(label_one).dur_list; chunk_dur_tmp];
0277                 end
0278             end
0279         end
0280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0281     elseif isfield(input, 'event_name')
0282         event_name = input.event_name;
0283 
0284         if isfield(input, 'grouping')
0285             grouping = input.grouping;
0286             if ~strcmp(grouping, 'subevent_cat')
0287                 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0288                 error(['Currently, only when the grouping is subcevent_cat, ' ...
0289                     'this function works and thoroughly tested. It will be updated.']);
0290             end
0291         else
0292             grouping = 'subevent_cat';
0293             input.grouping = grouping;
0294         end
0295     
0296         if strcmp(grouping, 'subevent_cat')
0297             for lmlidx = 1:length(groupid_matrix_list)
0298                 results(lmlidx).sub_list = sub_list;
0299             end
0300         end
0301         
0302         for contidx = 1:length(var_name)
0303             label_row = groupid_matrix(contidx,:);
0304             label_row_list = unique(label_row);
0305             
0306             for lidx = 1:length(label_row_list)
0307                 label_one = label_row_list(lidx);
0308 %                 label_one_idx = find(groupid_matrix_list == label_one);
0309                 data_label_idx = find(label_row == label_one);
0310 
0311                 for dlidx = 1:length(data_label_idx)                    
0312                     input_one = input;
0313                     input_one.var_name = input.var_name{contidx};
0314                     input_one.event_name = input.event_name{data_label_idx(dlidx)};
0315                     [chunks_one extra_one] = get_variable_by_grouping('sub', sub_list, ...
0316                         var_name{contidx}, grouping, input_one);
0317                     
0318                     if strcmp(extracted_var_type, 'cstream')
0319                         chunks_one_new = cellfun( ...
0320                             @(chunk_one) ...
0321                             cstream_reassign_categories(chunk_one, {},(groupid_matrix(contidx, ceventidx))+ROI_OFFSET), ...
0322                             chunks_one, ...
0323                             'UniformOutput', 0);
0324                         chunks_one = chunks_one_new;
0325 
0326                         chunks_one_cat = vertcat(chunks_one_new{:});
0327                         roi_checking = unique(chunks_one_cat(:,2));
0328                         if length(roi_checking) > 2
0329                             error('cstream roi are not reassigned!');
0330                         end
0331                     elseif strcmp(extracted_var_type, 'cevent')
0332                         chunks_one_new = cellfun( ...
0333                             @(chunk_one) ...
0334                             cevent_reassign_categories(chunk_one, {},(groupid_matrix(contidx, ceventidx))+ROI_OFFSET), ...
0335                             chunks_one, ...
0336                             'UniformOutput', 0);
0337                         chunks_one = chunks_one_new;
0338 
0339                         chunks_one_cat = vertcat(chunks_one_new{:});
0340                         roi_checking = unique(chunks_one_cat(:,3));
0341                         if length(roi_checking) > 2
0342                             error('cevent roi are not reassigned!');
0343                         end
0344                     end
0345 
0346                     if strcmp(grouping, 'subevent_cat')
0347                         tmp_length = results(label_one).chunks_count + 1;
0348                         results(label_one).chunks(:,tmp_length) = chunks_one;
0349                         results(label_one).chunks_count = tmp_length;
0350                         results(label_one).dur_list = ...
0351                             results(label_one).dur_list + extra_one.individual_range_dur;
0352                     elseif strcmp(grouping, 'event')
0353                         tmp_length = results(label_one).chunks_count + length(chunks_one);
0354                         results(label_one).chunks...
0355                             ((results(label_one).chunks_count+1):tmp_length,:) ...
0356                             = chunks_one;
0357                         results(label_one).chunks_count = tmp_length;
0358                     end
0359                 end
0360             end
0361         end
0362 
0363         if strcmp(grouping, 'subevent_cat')
0364             for lmlidx = 1:length(groupid_matrix_list)
0365                 tmp_chunks = results(lmlidx).chunks;
0366                 tmp_chunks_new = {};
0367 
0368                 for sidx = 1:size(tmp_chunks, 1)
0369                     tmp_chunks_new{sidx,1} = vertcat(tmp_chunks{sidx,:});
0370                 end
0371 
0372                 results(lmlidx).chunks = tmp_chunks_new;
0373             end
0374         end
0375     end    
0376 
0377 %     for lmlidx = 1:length(groupid_matrix_list)
0378 %         results(lmlidx) = rmfield(results(lmlidx), 'chunks_count');
0379 %     end
0380 
0381     % reassign the roi values back to roi values
0382     if strcmp(extracted_var_type, 'cstream')
0383         for lmlidx = 1:length(groupid_matrix_list)
0384             chunks_new = results(lmlidx).chunks;
0385             for cnidx = 1:length(chunks_new)
0386                 chunk_one_new = chunks_new{cnidx};
0387 
0388                 label_one = groupid_matrix_list(lmlidx);
0389                 chunk_one_new = ...
0390                     cstream_reassign_categories(chunk_one_new, {label_one+ROI_OFFSET}, {label_one});
0391                 
0392                 [Y I] = sort(chunk_one_new(:,1));
0393                 chunk_one_new = chunk_one_new(I,:);
0394                 results(lmlidx).chunks{cnidx} = chunk_one_new;
0395             end
0396 
0397         end
0398     elseif strcmp(extracted_var_type, 'cevent')
0399         for lmlidx = 1:length(groupid_matrix_list)
0400             chunks_new = results(lmlidx).chunks;
0401             for cnidx = 1:length(chunks_new)
0402                 chunk_one_new = chunks_new{cnidx};
0403 
0404                 label_one = groupid_matrix_list(lmlidx);
0405                 chunk_one_new = ...
0406                     cevent_reassign_categories(chunk_one_new, {label_one+ROI_OFFSET}, {label_one});
0407 
0408                 [Y I] = sort(chunk_one_new(:,1));
0409                 chunk_one_new = chunk_one_new(I,:);
0410                 results(lmlidx).chunks{cnidx} = chunk_one_new;
0411             end
0412         end
0413 %     else
0414 %         error('Invalid input: VAR_NAME. Please see example page.');
0415     end
0416 
0417 else
0418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0419 %% if not cell start here
0420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0421     results.regroup_id_list = groupid_matrix_list;
0422     
0423     if ~isfield(input, 'var_category')
0424         error(['When data are in the format of cstream or cevents, ' ...
0425                 'the field VAR_CATEGORY must be specified.']);
0426     end
0427     var_category = input.var_category;
0428     
0429     if isfield(input, 'cevent_name')
0430         if isfield(input, 'grouping')
0431             grouping = input.grouping;
0432             if ~strcmp(grouping, 'subcevent_cat')
0433                 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0434 %                 error(['Currently, only when the grouping is subcevent_cat, ' ...
0435 %                     'this function works and thoroughly tested. It will be updated.']);
0436             end
0437         else
0438             grouping = 'subcevent_cat';
0439             input.grouping = grouping;
0440         end
0441         
0442         if strcmp(grouping, 'subcevent_cat')
0443             results.chunks = cell(length(sub_list), 1);
0444             results.sub_list = sub_list;
0445             results.dur_list = zeros(length(sub_list), 1);
0446         elseif strcmp(grouping, 'cevent')
0447             results.chunks = {};
0448             results.sub_list = [];
0449             results.dur_list = [];
0450             results.cevent_list = [];
0451         end
0452         
0453         if ~isfield(input, 'cevent_category')
0454             error(['When data are regrouped by cevents, the field ' ...
0455                 'CEVENT_CATEGORY must be specified.']);
0456         end
0457         
0458         cevent_category = input.cevent_category;
0459         cevent_name = input.cevent_name;
0460 
0461         for ceventidx = 1 : length(cevent_category)
0462             input.cevent_values = cevent_category(ceventidx);
0463 
0464             [chunks_one, extra] = get_variable_by_grouping('sub', sub_list, ...
0465                 var_name, grouping, input);
0466             if strcmp(grouping, 'cevent') && isempty(chunks_one)
0467                 continue
0468             end
0469             
0470             label_column = groupid_matrix(:, ceventidx);
0471             label_column_list = unique(label_column);
0472             
0473             for lidx = 1:length(label_column_list)
0474                 label_one = label_column_list(lidx);
0475 %                 label_one_idx = find(groupid_matrix_list == label_one);
0476                 target_categories = var_category(label_column == label_one);
0477                 
0478                 if strcmp(extracted_var_type, 'cstream')
0479                     chunks_one_new = cellfun( ...
0480                         @(chunk_one) ...
0481                         cstream_reassign_categories(chunk_one, {target_categories}, {label_one+ROI_OFFSET}), ...
0482                         chunks_one, ...
0483                         'UniformOutput', 0);
0484                 elseif strcmp(extracted_var_type, 'cevent')
0485                     chunks_one_new = cellfun( ...
0486                         @(chunk_one) ...
0487                         cevent_reassign_categories(chunk_one, {target_categories}, {label_one+ROI_OFFSET}), ...
0488                         chunks_one, ...
0489                         'UniformOutput', 0);
0490                 else
0491                     error('Invalid input: VAR_NAME. Please see example page.');
0492                 end
0493                 
0494                 chunks_one = chunks_one_new; 
0495             end
0496             chunks_dur_list = extra.individual_range_dur;
0497             cevent_list = extra.individual_cevent;
0498             
0499             if strcmp(grouping, 'subcevent_cat')
0500                 chunks_new = results.chunks;
0501                 chunks_dur_new = results.dur_list;
0502                 
0503                 for sidx = 1:size(chunks_one, 1)
0504                     chunks_new{sidx, 1} = ...
0505                         vertcat(chunks_new{sidx, 1}, chunks_one{sidx});
0506                     chunks_dur_new(sidx, 1) = ...
0507                         chunks_dur_new(sidx, 1)+chunks_dur_list(sidx, 1);
0508                 end
0509                 results.chunks = chunks_new;
0510                 results.dur_list = chunks_dur_new;
0511             elseif strcmp(grouping, 'cevent')
0512                 results.chunks = [results.chunks; chunks_one];
0513                 results.sub_list = [results.sub_list; extra.sub_list];
0514                 results.dur_list = [results.dur_list; extra.individual_range_dur];
0515                 results.cevent_list = [results.cevent_list; cevent_list];
0516             end
0517         end
0518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0519     elseif isfield(input, 'event_name') && false
0520         event_name = input.event_name;
0521 
0522         if isfield(input, 'grouping')
0523             grouping = input.grouping;
0524             if ~strcmp(grouping, 'subevent_cat')
0525                 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0526                 error(['Currently, only when the grouping is subevent_cat, ' ...
0527                     'this function works and thoroughly tested. It will be updated.']);
0528             end
0529         else
0530             grouping = 'subevent_cat';
0531             input.grouping = grouping;
0532         end
0533         
0534         if strcmp(grouping, 'subcevent_cat')
0535             results.chunks = cell(length(sub_list), 1);
0536             results.sub_list = sub_list;
0537             results.dur_list = zeros(length(sub_list), 1);
0538         end
0539         
0540         for eventidx = 1 : length(event_name)
0541             input_one = input;
0542             input_one.event_name = event_name{eventidx};
0543 
0544             chunks_one = get_variable_by_grouping('sub', sub_list, ...
0545                 var_name, grouping, input_one);
0546             
0547             label_column = groupid_matrix(:, ceventidx);
0548             label_column_list = unique(label_column);
0549             
0550             for lidx = 1:length(label_column_list)
0551                 label_one = label_column_list(lidx);
0552 %                 label_one_idx = find(groupid_matrix_list == label_one);
0553                 target_categories = var_category(label_column == label_one);
0554                 
0555                 if strcmp(extracted_var_type, 'cstream')
0556                     chunks_one_new = cellfun( ...
0557                         @(chunk_one) ...
0558                         cstream_reassign_categories(chunk_one, {target_categories}, {label_one}), ...
0559                         chunks_one, ...
0560                         'UniformOutput', 0);
0561                 elseif strcmp(extracted_var_type, 'cevent')
0562                     chunks_one_new = cellfun( ...
0563                         @(chunk_one) ...
0564                         cevent_reassign_categories(chunk_one, {target_categories}, {label_one}), ...
0565                         chunks_one, ...
0566                         'UniformOutput', 0);
0567                 else
0568                     error('Invalid input: VAR_NAME. Please see example page.');
0569                 end
0570                 
0571                 chunks_one = chunks_one_new;                
0572             end
0573             
0574             if strcmp(grouping, 'subcevent_cat')
0575                 chunks_new = results.chunks;
0576                 for sidx = 1:size(chunks_one_new, 1)
0577                     chunks_new{sidx, 1} = ...
0578                         vertcat(chunks_new{sidx, 1}, chunks_one_new{sidx});
0579                 end
0580                 results.chunks = chunks_new;
0581             elseif strcmp(grouping, 'cevent')
0582                 results.chunks = ...
0583                     vertcat(results.chunks, chunks_one_new{:});
0584             end
0585         end
0586         
0587         for contidx = 1:length(var_name)
0588 
0589             label_row = groupid_matrix(contidx,:);
0590             label_row_list = unique(label_row);
0591             for lidx = 1:length(label_row_list)
0592                 label_one = label_row_list(lidx);
0593 %                 label_one_idx = find(groupid_matrix_list == label_one);
0594                 data_label_idx = find(label_row == label_one);
0595 
0596                 for dlidx = 1:length(data_label_idx)                    
0597                     input_one = input;
0598                     input_one.var_name = input.var_name{contidx};
0599                     input_one.event_name = input.event_name{data_label_idx(dlidx)};
0600                     chunks_one = get_variable_by_grouping('sub', sub_list, ...
0601                         var_name{contidx}, grouping, input_one);
0602 
0603                     if strcmp(grouping, 'subevent_cat')
0604                         tmp_length = results(label_one).chunks_count + 1;
0605                         results(label_one).results(:,tmp_length) = chunks_one;
0606                         results(label_one).chunks_count = tmp_length;
0607                     elseif strcmp(grouping, 'event')
0608                         tmp_length = results(label_one).chunks_count + length(chunks_one);
0609                         results(label_one).chunks...
0610                             ((results(label_one).chunks_count+1):tmp_length,:) ...
0611                             = chunks_one;
0612                         results(label_one).chunks_count = tmp_length;
0613                     end
0614                 end
0615             end
0616         end
0617 
0618         if strcmp(grouping, 'subevent_cat')
0619             for lmlidx = 1:length(groupid_matrix_list)
0620                 tmp_chunks = results(lmlidx).chunks;
0621                 tmp_chunks_new = {};
0622 
0623                 for sidx = 1:size(tmp_chunks, 1)
0624                     tmp_chunks_new{sidx,1} = vertcat(tmp_chunks{sidx,:});
0625                 end
0626 
0627                 results(lmlidx).chunks = tmp_chunks_new;
0628             end
0629         end
0630     end
0631     
0632     % reassign the roi values back to roi values
0633     if strcmp(extracted_var_type, 'cstream')
0634         chunks_new = results.chunks;
0635         for cnidx = 1:length(chunks_new)
0636             chunk_one_new = chunks_new{cnidx};
0637             for lmlidx = 1:length(groupid_matrix_list)
0638                 label_one = groupid_matrix_list(lmlidx);
0639                 chunk_one_new = ...
0640                     cstream_reassign_categories(chunk_one_new, {label_one+ROI_OFFSET}, {label_one});
0641             end
0642 
0643             [Y I] = sort(chunk_one_new(:,1));
0644             chunk_one_new = chunk_one_new(I,:);
0645             results.chunks{cnidx} = chunk_one_new;
0646         end
0647     elseif strcmp(extracted_var_type, 'cevent')
0648         chunks_new = results.chunks;
0649         for cnidx = 1:length(chunks_new)
0650             chunk_one_new = chunks_new{cnidx};
0651             for lmlidx = 1:length(groupid_matrix_list)
0652                 label_one = groupid_matrix_list(lmlidx);
0653                 chunk_one_new = ...
0654                     cevent_reassign_categories(chunk_one_new, {label_one+ROI_OFFSET}, {label_one});
0655             end
0656             [Y I] = sort(chunk_one_new(:,1));
0657             chunk_one_new = chunk_one_new(I,:);
0658             results.chunks{cnidx} = chunk_one_new;
0659         end
0660     else
0661         error('Invalid input: VAR_NAME. Please see example page.');
0662     end
0663 end
0664 
0665 % results_output = results;

Generated on Tue 23-May-2017 03:00:58 by m2html © 2005