Home > data-analysis > extraction > temporal_files > temporal_profile_generate_by_cevent.m

temporal_profile_generate_by_cevent

PURPOSE ^

This function generates temporal profile of a group of continue variables

SYNOPSIS ^

function [profile_data] = temporal_profile_generate_by_cevent(input)

DESCRIPTION ^

 This function generates temporal profile of a group of continue variables
 or one cstream profile chunked by one cevent variable.

 For detailed user guide one this function, please go to demo script at:
 
 
 Last update by Linger, txu@indiana.edu on 07/21/2016

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [profile_data] = temporal_profile_generate_by_cevent(input)
0002 % This function generates temporal profile of a group of continue variables
0003 % or one cstream profile chunked by one cevent variable.
0004 %
0005 % For detailed user guide one this function, please go to demo script at:
0006 %
0007 %
0008 % Last update by Linger, txu@indiana.edu on 07/21/2016
0009 
0010 
0011 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0012 sub_list = input.sub_list;
0013 % expsub_ids = input.sub_list;
0014 % if size(expsub_ids, 1) < size(expsub_ids, 2)
0015 %     expsub_ids = expsub_ids';
0016 % end
0017 %
0018 % mask_expid = expsub_ids < 1000;
0019 % mask_subid = expsub_ids > 1000;
0020 %
0021 % sub_list = expsub_ids(mask_subid);
0022 %
0023 % if sum(mask_expid) > 0
0024 %     exp_list = expsub_ids(mask_expid);
0025 %     sub_list = [sub_list; list_subjects(exp_list)];
0026 %     exp_list = unique(floor(sub_list / 100));
0027 % end
0028 
0029 if ~(isfield(input, 'whence') && isfield(input, 'interval'))
0030     error(['Error. This functions only works ''whence'' and ''interval'' are both specified. ' ...
0031         'For example, one wants to generate gaze profile 10 seconds before to the onset of ' ...
0032         'naming instances. In this case, whence is ''start'', interval is [-10 0].']);
0033 else
0034     whence = input.whence;
0035     interval = input.interval;
0036 end
0037 
0038 if ~isfield(input, 'cevent_category')
0039     error(['When data are regrouped by cevents, the field ' ...
0040         'CEVENT_CATEGORY must be specified.']);
0041 else
0042     cevent_category = input.cevent_category;
0043 end
0044 if ~isfield(input, 'var_category')
0045     error(['Under all situations, the field ' ...
0046         'VAR_CATEGORY must be specified.']);
0047 else
0048     var_category = input.var_category;
0049 end
0050 
0051 % if strcmp(whence, 'start')
0052 %     ref_column = 1;
0053 %     str_align = 'onset';
0054 % elseif strcmp(whence, 'end')
0055 %     ref_column = 2;
0056 %     str_align = 'offset';
0057 % end
0058 
0059 cevent_name = input.cevent_name;
0060 var_name = input.var_name;
0061 
0062 x_has_var_cevent = arrayfun( ...
0063     @(sub_id) ...
0064     has_variable(sub_id, cevent_name), ...
0065     sub_list, ...
0066     'UniformOutput', 0);
0067 x_has_var_cevent = vertcat(x_has_var_cevent{:});
0068 
0069 if sum(~x_has_var_cevent) > 0
0070     missvar_sub_list = num2str(sub_list(~x_has_var_cevent)');
0071     fprintf('Cevent variable %s does not exist for subject(s) %s\n', cevent_name, missvar_sub_list);
0072 end
0073 mask_has_variable = x_has_var_cevent;
0074 
0075 groupid_matrix = input.groupid_matrix;
0076 groupid_list = unique(groupid_matrix);
0077 num_groupids = length(groupid_list);
0078 
0079 if iscell(input.var_name)
0080     example_var_name = var_name{1};
0081     is_var_cell = true;
0082     num_vars = length(var_name);
0083     
0084     if size(groupid_matrix, 1) ~= num_vars
0085         error(['In ''groupid_matrix'', each row corresponding to a cstream ROI value or the order of cont type ' ...
0086             'variable, and each column corresponding to a cevent value. So, if you input a cell list of ' ...
0087             'cont variables in the grouping variable list, the number and order of the variables ' ...
0088             'should match with the rows in groupid_matrix.']);
0089     end
0090     str_var_type = get_data_type(example_var_name);
0091     
0092     if ~strcmp(str_var_type, 'cont')
0093         error('Only a list of continue variables or one cstream variable are accepted as input.')
0094     end
0095     
0096     for vidx = 1:num_vars
0097         x_var_one = arrayfun( ...
0098             @(sub_id) ...
0099             has_variable(sub_id, var_name{vidx}), ...
0100             sub_list, ...
0101             'UniformOutput', 0);
0102         x_var_one = vertcat(x_var_one{:});
0103 
0104         if sum(~x_var_one) > 0
0105             missvar_sub_list = num2str(sub_list(~x_var_one)');
0106             fprintf('Continue variable %s does not exist for subject(s) %s\n', var_name{vidx}, missvar_sub_list);
0107         end
0108         mask_has_variable = mask_has_variable & x_var_one;
0109     end
0110 else
0111     example_var_name = var_name;
0112     is_var_cell = false; % meaning the input variable only has one cstream
0113     num_vars = 1;
0114     str_var_type = get_data_type(example_var_name);
0115     
0116     if ~strcmp(str_var_type, 'cstream')
0117         error('Only a list of continue variables or one cstream variable are accepted as input.')
0118     end
0119 
0120     x_var_one = arrayfun( ...
0121         @(sub_id) ...
0122         has_variable(sub_id, var_name), ...
0123         sub_list, ...
0124         'UniformOutput', 0);
0125     x_var_one = vertcat(x_var_one{:});
0126 
0127     if sum(~x_var_one) > 0
0128         missvar_sub_list = num2str(sub_list(~x_var_one)');
0129         fprintf('Cstream variable %s does not exist for subject(s) %s\n', var_name, missvar_sub_list);
0130     end
0131     mask_has_variable = mask_has_variable & x_var_one;
0132 end
0133 
0134 if size(groupid_matrix, 1) ~= length(var_category)
0135     error(['In ''groupid_matrix'', each row corresponding to a cstream ROI value or the order of cont type ' ...
0136         'variables, and each column corresponding to a cevent value. So, the number of values in ' ...
0137         '''var_category'' should match with the number of rows in groupid_matrix.']);
0138 end
0139 
0140 if size(groupid_matrix, 2) ~= length(cevent_category)
0141     error(['In ''groupid_matrix'', each row corresponding to a cstream value/variable, and ' ...
0142         'each column corresponding to a cevent value. So, the number of values in ' ...
0143         '''cevent_category'' should match with the number of columns in groupid_matrix.']);
0144 end
0145 
0146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0147 % for eidx = 1:length(exp_list)
0148 %     exp_id = exp_list(eidx);
0149 
0150 % mask_sub_list = sub_list > exp_id*100 & sub_list < (exp_id+1)*100;
0151 % sub_list = sub_list(mask_sub_list);
0152 sub_list = sub_list(mask_has_variable);
0153 
0154 result_chunks = cell(length(sub_list), num_groupids);
0155 result_sub_list = cell(length(sub_list), 1);
0156 result_ranges = cell(length(sub_list), 1);
0157 result_cevent = cell(length(sub_list), 1);
0158 result_cevent_index = cell(length(sub_list), 1);
0159 result_cevent_trialid = cell(length(sub_list), 1);
0160 result_probs_mean = cell(length(sub_list), 1);
0161 
0162 for sidx = 1:length(sub_list)
0163     sub_id = sub_list(sidx);
0164     if is_var_cell
0165         var_data = cell(1, num_vars);
0166         for vidx = 1:num_vars
0167             var_data{vidx} = get_variable(sub_id, var_name{vidx});
0168         end
0169     else
0170         var_data = get_variable(sub_id, var_name);
0171     end
0172 
0173     if isfield(input, 'sample_rate')
0174         sample_rate = input.sample_rate;
0175     else
0176         sub_timing = get_timing(sub_id);
0177         sample_rate = 1/sub_timing.camRate;
0178     end
0179     time_base = interval(1):sample_rate:(interval(2)-+0.0001);
0180     length_profile = length(time_base);
0181     duration_profile = interval(2) - interval(1);
0182     time_base_ts = 0:sample_rate:(duration_profile-0.0001);
0183 
0184     if isfield(input, 'trial_var_name')
0185         trials_one = get_variable(sub_id, input.trial_var_name);
0186         if isfield(input, 'trial_values')
0187             trials_one = cevent_category_equals(...
0188                 trials_one, input.trial_values);
0189         end
0190     else
0191         trials_one = get_trial_times(sub_id);
0192 
0193         if isfield(input, 'trial_indices')
0194             if iscell(input.trial_indices)
0195                 trials_one = trials_one(input.trial_indices{sidx}, :);
0196             else
0197                 trials_one = trials_one(input.trial_indices, :);
0198             end
0199         end
0200     end
0201 
0202     cevent_data = get_variable(sub_id, cevent_name);
0203 
0204     chunks_cevents = cell(size(trials_one, 1), 1);
0205     chunks_trialid = cell(size(trials_one, 1), 1);
0206 
0207     for tidx = 1:size(trials_one, 1)
0208         range = trials_one(tidx, :); % range(1) is start, range(2) is end
0209         chunk_one = get_event_in_scope(cevent_data, range);
0210         len_chunks = size(chunk_one, 1);
0211 
0212         chunks_cevents{tidx} = chunk_one;
0213         chunks_trialid{tidx} = repmat(tidx, len_chunks, 1);
0214     end
0215 
0216     cevent_data = vertcat(chunks_cevents{:});
0217     cevent_trialid = vertcat(chunks_trialid{:});
0218 
0219     if isfield(input, 'cevent_min_dur')
0220         cevent_dur = cevent_data(:,2) - cevent_data(:,1); 
0221         x_dur_mask = cevent_dur >= input.cevent_min_dur;
0222         cevent_data = cevent_data(x_dur_mask, :);
0223         cevent_trialid = cevent_trialid(x_dur_mask, :);
0224     end
0225 
0226     if isfield(input, 'cevent_max_dur')
0227         cevent_dur = cevent_data(:,2) - cevent_data(:,1); 
0228         x_dur_mask = cevent_dur <= input.cevent_max_dur;
0229         cevent_data = cevent_data(x_dur_mask, :);
0230         cevent_trialid = cevent_trialid(x_dur_mask, :);
0231     end
0232 
0233     if isempty(cevent_data)
0234         fprintf('Subject %d has zero instances of %s that met criteria.\n', sub_id, cevent_name);
0235         continue
0236     end
0237 
0238     % After retrieving cevent data, start getting cont/cstream
0239     % variables
0240     num_cevents = size(cevent_data, 1);
0241     result_sub_list{sidx, 1} = repmat(sub_id, num_cevents, 1);
0242     result_cevent{sidx, 1} = cevent_data;
0243     result_cevent_index{sidx, 1} = (1:num_cevents)'; 
0244     result_cevent_trialid{sidx, 1} = cevent_trialid;
0245     probs_mean_sub = nan(num_cevents, num_groupids);
0246 
0247     temporal_ranges = cevent_relative_intervals(...
0248         cevent_data, input.whence, input.interval);
0249 
0250     if isfield(input, 'within_ranges') && ~input.within_ranges
0251         temporal_ranges = get_cevent_opposite(sub_id, cevent_data, trials_one);
0252     end
0253 
0254     result_ranges{sidx, 1} = temporal_ranges;
0255 
0256     chunks_profile_sub = cell(1, num_groupids);
0257     for coidx = 1:num_groupids
0258         chunks_profile_sub{coidx} = nan(num_cevents, length_profile);
0259     end
0260 
0261     if is_var_cell % when user input a list of continue variables
0262         % chunks_var_origin stores the variable extracted from the dataset
0263         chunks_var_origin = cell(num_cevents, num_vars);
0264         mat_var_profile = nan(num_cevents, length_profile, num_vars);
0265         cont_sum_sub = zeros(num_cevents, length_profile, num_groupids);
0266         cont_count_sub = zeros(num_cevents, length_profile, num_groupids);
0267         
0268         % fetch and format continue variable data
0269         for vidx = 1:num_vars
0270             chunks_var_one  = extract_ranges(var_data{vidx}, ...
0271                 str_var_type, {temporal_ranges});
0272             mat_profile_one = nan(num_cevents, length_profile);
0273             
0274             for cnidx = 1:num_cevents
0275                 range_one = temporal_ranges(cnidx, :);
0276 
0277                 chunks_one_new = chunks_var_one{cnidx};
0278                 chunks_one_new(:, 1) = chunks_one_new(:, 1) - range_one(1);
0279                 length_one = size(chunks_one_new, 1);
0280 
0281                 if length_one < length_profile
0282                     chunk_ts = timeseries(chunks_one_new(:, 2:end), chunks_one_new(:, 1));
0283                     chunk_ts = resample(chunk_ts, time_base_ts, 'zoh');
0284                     chunks_one_new = horzcat(get(chunk_ts, 'Time'), get(chunk_ts, 'Data'));
0285                 end
0286 
0287                 chunks_one_new(isnan(chunks_one_new(:,2)),2) = 0;
0288                 mat_profile_one(cnidx, :) = chunks_one_new(:, 2)';
0289             end
0290             
0291             mat_var_profile(:, :, vidx) = mat_profile_one;
0292         end
0293         
0294         for ceventidx = 1 : length(cevent_category)
0295             cevent_values = cevent_category(ceventidx);
0296             label_column = groupid_matrix(:, ceventidx);
0297             label_column_list = unique(label_column);
0298 
0299             mask_cvalues = ismember(cevent_data(:, 3), cevent_values);
0300 %             chunks_var_by_cvalue = chunks_var_origin(mask_cvalues);
0301 %             chunks_ranges = temporal_ranges(mask_cvalues);
0302 %             num_cevents_value = sum(mask_cvalues);
0303             
0304             for lidx = 1:length(label_column_list)
0305                 label_one = label_column_list(lidx);
0306                 target_categories = var_category(label_column == label_one);
0307                 
0308                 tmp_profile = cont_sum_sub(mask_cvalues, :, label_one);
0309                 tmp_count = cont_count_sub(mask_cvalues, :, label_one);
0310                 cont_sum_sub(mask_cvalues, :, label_one) = ...
0311                     cont_sum_sub(mask_cvalues, :, label_one) + ...
0312                     sum(mat_var_profile(mask_cvalues, :, target_categories), 3);
0313                 cont_count_sub(mask_cvalues, :, label_one) = ...
0314                     cont_count_sub(mask_cvalues, :, label_one) + ...
0315                     sum(mat_var_profile(mask_cvalues, :, target_categories)>0, 3);
0316             end
0317         end % end of going through all cevents
0318         
0319         for gidx = 1:num_groupids
0320             label_one = groupid_list(gidx);
0321             tmp_count = cont_count_sub(:, :, gidx);
0322             probs_mean_sub(:, gidx) = sum(cont_sum_sub(:, :, gidx), 2) ./ sum(tmp_count, 2);
0323             tmp_count(tmp_count < 1) = 1;
0324             chunks_profile_sub{gidx} = cont_sum_sub(:, :, gidx) ./ tmp_count;
0325         end
0326     else
0327         % chunks_var_origin stores the variable extracted from the dataset
0328         chunks_var_origin = extract_ranges(var_data, ...
0329             str_var_type, {temporal_ranges});
0330         % chunks_var_mat stores the variables that were reassigned
0331         chunks_var_mat = nan(num_cevents, length_profile);
0332         chunks_check_mat = nan(num_cevents, length_profile);
0333 
0334         for ceventidx = 1 : length(cevent_category)
0335             cevent_values = cevent_category(ceventidx);
0336             label_column = groupid_matrix(:, ceventidx);
0337             label_column_list = unique(label_column);
0338 
0339             mask_cvalues = ismember(cevent_data(:, 3), cevent_values);
0340             chunks_var_by_cvalue = chunks_var_origin(mask_cvalues);
0341             chunks_ranges = temporal_ranges(mask_cvalues);
0342             num_cevents_value = sum(mask_cvalues);
0343 
0344             mat_var_profile = nan(num_cevents_value, length_profile);
0345 
0346             for cnidx = 1:size(chunks_ranges, 1)
0347                 range_one = chunks_ranges(cnidx, :);
0348 
0349                 chunks_one_new = chunks_var_by_cvalue{cnidx};
0350                 chunks_one_new(:, 1) = chunks_one_new(:, 1) - range_one(1);
0351                 length_one = size(chunks_one_new, 1);
0352 
0353                 if length_one < length_profile
0354                     chunk_ts = timeseries(chunks_one_new(:, 2:end), chunks_one_new(:, 1));
0355                     chunk_ts = resample(chunk_ts, time_base_ts, 'zoh');
0356                     chunks_one_new = horzcat(get(chunk_ts, 'Time'), get(chunk_ts, 'Data'));
0357                 end
0358 
0359                 mat_var_profile(cnidx, :) = chunks_one_new(:, 2)';
0360             end
0361 
0362             mat_origin_profile = mat_var_profile;
0363             for lidx = 1:length(label_column_list)
0364                 label_one = label_column_list(lidx);
0365                 target_categories = var_category(label_column == label_one);
0366 
0367                 mask_reassign = ismember(mat_origin_profile, target_categories);
0368                 mat_var_profile(mask_reassign) = label_one;
0369             end
0370 
0371             chunks_check_mat(mask_cvalues, :) = mat_origin_profile;
0372             chunks_var_mat(mask_cvalues, :) = mat_var_profile;
0373         end % end of going through cevent categorical values
0374 
0375         for gidx = 1 : num_groupids
0376             label_target = groupid_list(gidx);
0377             label_other = setdiff(groupid_list, label_target);
0378 
0379             % Each cell contains the matrix that holds data for one group label
0380             tmp_chunk = chunks_profile_sub{gidx};
0381 
0382             mask_group = ismember(chunks_var_mat, label_target);
0383             tmp_chunk(mask_group) = 1;
0384             mask_other = ismember(chunks_var_mat, label_other);
0385             tmp_chunk(mask_other) = 0;
0386             mask_zeros = chunks_var_mat < 1;
0387             tmp_chunk(mask_zeros) = 0;
0388             chunks_profile_sub{gidx} = tmp_chunk;
0389 
0390             num_valid_data = sum(~isnan(tmp_chunk), 2);
0391             num_matches = sum(mask_group, 2);
0392             probs_mean_sub(:, gidx) = num_matches ./ num_valid_data;
0393         end
0394     end
0395     
0396     result_chunks(sidx, :) = chunks_profile_sub;
0397     result_probs_mean{sidx, :} = probs_mean_sub;
0398 end % end of sidx
0399  
0400 % result_chunks = vertcat(result_chunks{:});
0401 result_probs_mean = vertcat(result_probs_mean{:});
0402 
0403 % subID    expID    onset    offset    category    trialsID    instanceID
0404 profile_data.sub_list = vertcat(result_sub_list{:});
0405 profile_data.exp_list = floor(profile_data.sub_list / 100);
0406 profile_data.cevents = vertcat(result_cevent{:});
0407 profile_data.cevent_trialid = vertcat(result_cevent_trialid{:});
0408 profile_data.cevent_instanceid = vertcat(result_cevent_index{:});
0409 profile_data.probs_mean_per_instance = result_probs_mean;
0410 
0411 if isfield(input, 'groupid_label')
0412     profile_data.groupid_label = input.groupid_label;
0413 else
0414     profile_data.groupid_label = {'target', 'non-target', 'other'};
0415     profile_data.groupid_label = profile_data.groupid_label(groupid_list);
0416 end
0417 profile_data.group_list = groupid_list';
0418 profile_data_mat = cell(1, num_groupids);
0419 for gidx = 1:num_groupids
0420     profile_data_mat{gidx} = vertcat(result_chunks{:, gidx});
0421 end
0422 profile_data.profile_data_mat = profile_data_mat;
0423 profile_data.sample_rate = sample_rate;
0424 profile_data.time_base = time_base;
0425 profile_data.cevent_name = cevent_name;
0426 profile_data.var_name = var_name;

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005