Home > data-utility > cevent_pattern_query > cevent_query_patterns.m

cevent_query_patterns

PURPOSE ^

[results_final chunked_data] = cevent_query_miner(query,data,relation)

SYNOPSIS ^

function [results_final chunked_data] = cevent_query_patterns(query,data,relation)

DESCRIPTION ^

[results_final chunked_data] = cevent_query_miner(query,data,relation)
 This function is used for extract cevent patterns specified by the user
 input. 
 
 For the details about the input and the output, please see
 txu_cevent_query_test.m script.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [results_final chunked_data] = cevent_query_patterns(query,data,relation)
0002 
0003 %[results_final chunked_data] = cevent_query_miner(query,data,relation)
0004 % This function is used for extract cevent patterns specified by the user
0005 % input.
0006 %
0007 % For the details about the input and the output, please see
0008 % txu_cevent_query_test.m script.
0009 %
0010 
0011 
0012 % check required fields
0013 if isfield(query, 'sub_list')
0014     sub_list = query.sub_list;
0015 else
0016     error('Missing parameter: SUB_LIST!');
0017 end
0018 
0019 if ~isfield(query, 'grouping')
0020     query.grouping = 'subject';
0021 end
0022 
0023 % extract all the data
0024 for dataidx = 1:length(data)
0025     data_one_args = data(dataidx);
0026     data_one_args.grouping = 'subject';
0027     
0028     % user can either input the variable name or data directly
0029     if isfield(data_one_args, 'var_name') && ~isempty(data_one_args.var_name)
0030         chunks_one = get_variable_by_grouping('sub', sub_list, ...
0031             data_one_args.var_name, data_one_args.grouping, data_one_args);
0032     elseif isfield(data_one_args, 'chunks') && ~isempty(data_one_args.chunks)
0033         chunks_one = data_one_args.chunks;
0034     end
0035     
0036     % if there is a duration constraint on the data
0037     if isfield(data_one_args, 'dur_range') && ~isempty(data_one_args.dur_range)
0038         for cidx = 1:length(chunks_one)
0039             chunk_one = chunks_one{cidx};
0040             if size(chunk_one, 1) > 0
0041                 duration_one = chunk_one(:,2) - chunk_one(:,1);
0042                 x_valid_dur = duration_one > data_one_args.dur_range(1,1) & ...
0043                     duration_one < data_one_args.dur_range(1,2);
0044 
0045                 chunk_one = chunk_one(x_valid_dur, :);
0046             end
0047             chunks_one{cidx} = chunk_one;
0048         end
0049     end
0050     
0051     data(dataidx).chunks = chunks_one;
0052 end
0053 
0054 entire_data_list = 1:length(data);
0055 processed_data_list = [];
0056 length_all_chunks = length(data(dataidx).chunks);
0057 
0058 % extract relations and apply the relations on to the data
0059 for relationidx = 1:length(relation)
0060     relation_one = relation(relationidx);
0061     results_separate_one = cell(length_all_chunks, length(entire_data_list));
0062     
0063     relation_one_base_id = relation_one.data_list(1,1);
0064     relation_one_search_id = relation_one.data_list(1,2);
0065     is_base_processed = 1;
0066     is_search_processed = 1;
0067     
0068     % check if previous results include either the search data or the
0069     % search data, so that the new search can only be limited to the
0070     % previous existing results
0071     if ~ismember(relation_one_base_id, processed_data_list)
0072         processed_data_list = [processed_data_list; relation_one_base_id];
0073         is_base_processed = 0;
0074     end    
0075     if ~ismember(relation_one_search_id, processed_data_list)
0076         processed_data_list = [processed_data_list; relation_one_search_id];
0077         is_search_processed = 0;
0078     end    
0079     
0080     if is_base_processed || is_search_processed
0081         prev_result_chunks = results_separates{relationidx-1};
0082     end
0083     
0084     if length(relation_one.data_list) ~= 2
0085         error(['Currently, RELATION only refers to relationship between' ...
0086             'two chunks of data']);
0087     else
0088         if ~is_base_processed
0089             chunks_base = data(relation_one_base_id).chunks;
0090         else
0091             chunks_base = prev_result_chunks(:, relation_one_base_id)';
0092         end
0093             
0094         if ~is_search_processed
0095             chunks_search = data(relation_one_search_id).chunks;
0096         else
0097             chunks_search = prev_result_chunks(:, relation_one_search_id)';
0098         end
0099     end
0100     
0101     if isfield(relation_one, 'whence_list') && ~isempty(relation_one.whence_list)
0102         whence_base = relation_one.whence_list{1};
0103         whence_search = relation_one.whence_list{2};
0104         interval = relation_one.interval;
0105     end
0106     
0107     for chunkidx = 1:length(chunks_base)
0108         chunk_one_base = chunks_base{chunkidx};
0109         chunk_one_search = chunks_search{chunkidx};
0110         chunk_one_search = event_eliminate_repeat(chunk_one_search);
0111         
0112         if ~isfield(relation_one, 'mapping_arg') || isempty(relation_one.mapping_arg)
0113             mapping_arg = 'many2many';
0114         else
0115             mapping_arg = relation_one.mapping_arg;
0116         end
0117         
0118         % by default the relation will be set as 'within'
0119         if ~isfield(relation_one, 'type') || isempty(relation_one.type) ...
0120                 || strcmp(relation_one.type, 'within')
0121             if isfield(relation_one, 'roi_list') && ~isempty(relation_one.roi_list)
0122                 [mask_suc_base events_suc_base events_suc_search time_hist] = ...
0123                     cevent_get_cevents_within_interval(chunk_one_base, chunk_one_search, ...
0124                     whence_base, whence_search, interval, relation_one.roi_list, mapping_arg);
0125             else
0126                 [mask_suc_base events_suc_base events_suc_search time_hist] = ...
0127                     cevent_get_cevents_within_interval(chunk_one_base, chunk_one_search, ...
0128                     whence_base, whence_search, interval, mapping_arg);
0129             end
0130            
0131         elseif strcmp(relation_one.type, 'following')
0132             if isfield(relation_one, 'roi_list') && ~isempty(relation_one.roi_list)
0133                 [mask_suc_base events_suc_base events_suc_search time_hist] = ...
0134                     cevent_get_following_cevents(chunk_one_base, chunk_one_search, ...
0135                     whence_base, whence_search, interval, relation_one.roi_list, mapping_arg);
0136             else
0137                 [mask_suc_base events_suc_base events_suc_search time_hist] = ...
0138                     cevent_get_following_cevents(chunk_one_base, chunk_one_search, ...
0139                     whence_base, whence_search, interval, mapping_arg);
0140             end
0141             
0142         % **the output for leading is special, the base and search are
0143         % backwards
0144         elseif strcmp(relation_one.type, 'leading')
0145             if isfield(relation_one, 'roi_list') && ~isempty(relation_one.roi_list)
0146                 [mask_suc_base events_suc_search events_suc_base time_hist] = ...
0147                     cevent_get_leading_cevents(chunk_one_search, chunk_one_base, ...
0148                     whence_search, whence_base, interval, relation_one.roi_list, mapping_arg);
0149             else
0150                 [mask_suc_base events_suc_search events_suc_base time_hist] = ...
0151                     cevent_get_leading_cevents(chunk_one_search, chunk_one_base, ...
0152                     whence_search, whence_base, interval, mapping_arg);
0153             end
0154             
0155         elseif strcmp(relation_one.type, 'overlap')
0156             if ~isfield(relation_one, 'overlap_arg') || isempty(relation_one.overlap_arg)
0157                 overlap_arg = 'all';
0158             elseif isfield(relation_one, 'overlap_arg')
0159                 overlap_arg = relation_one.overlap_arg;
0160             end
0161             
0162             if isfield(relation_one, 'roi_list') && ~isempty(relation_one.roi_list)
0163                 [mask_suc_base events_suc_base events_suc_search] = ...
0164                     cevent_get_overlap_cevents(chunk_one_base, chunk_one_search, ...
0165                     relation_one.roi_list, overlap_arg);
0166             else
0167                 [mask_suc_base events_suc_base events_suc_search] = ...
0168                     cevent_get_overlap_cevents(chunk_one_base, chunk_one_search, ...
0169                     overlap_arg);
0170             end
0171         end
0172         
0173         if ~strcmp(mapping_arg, 'many2many') && ~strcmp(relation_one.type, 'overlap')
0174             events_suc_search = mat2cell(events_suc_search, ones(1,size(events_suc_search,1)),[3]);
0175             
0176         end        
0177         
0178         if ~isempty(events_suc_search)
0179             cell_search_length_list = cellfun(@(data_one) ...
0180                 size(data_one, 1), ...
0181                 events_suc_search, ...
0182                 'UniformOutput', false);
0183             cell_search_length_list = vertcat(cell_search_length_list{:});
0184             
0185             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0186             %%%%%%%%%% The algorithm here is first shrink and expand %%%%%%%%%%
0187             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0188             if ~is_base_processed && ~is_search_processed % which means relationidx == 1
0189                 events_suc_search = cell2mat(events_suc_search);
0190                 events_suc_base = cevent_repmat(events_suc_base, cell_search_length_list);
0191                 results_separate_one{chunkidx, relation_one_base_id} = events_suc_base;
0192                 results_separate_one{chunkidx, relation_one_search_id} = ...
0193                     events_suc_search;
0194 
0195             % if base chunk is already been processed
0196             elseif is_base_processed && ~is_search_processed
0197                 results_separate_one{chunkidx, relation_one_base_id} = ...
0198                     cevent_repmat(events_suc_base, cell_search_length_list);
0199                 results_separate_one{chunkidx, relation_one_search_id} = ...
0200                     cell2mat(events_suc_search);
0201 
0202                 unfiltered_data_list = setdiff(processed_data_list, ...
0203                     relation_one.data_list);
0204 
0205                 % shrink and expand all in one
0206                 for tmpudli = 1:length(unfiltered_data_list)
0207                     tmp_chunk_one = prev_result_chunks{chunkidx, unfiltered_data_list(tmpudli)};
0208                     tmp_chunk_one = cevent_repmat(...
0209                         tmp_chunk_one(mask_suc_base>0,:), cell_search_length_list);
0210                     results_separate_one{chunkidx, unfiltered_data_list(tmpudli)} = ...
0211                         tmp_chunk_one;
0212                 end
0213 
0214             % if the search chunk is already been processed
0215             elseif ~is_base_processed && is_search_processed            
0216                 search_orgn_chunk = chunks_search{chunkidx};
0217                 events_suc_search = cell2mat(events_suc_search);
0218                 events_suc_base = cevent_repmat(events_suc_base, cell_search_length_list);
0219 
0220                 % first build the mask_suc_search
0221                 mask_suc_search = false(size(search_orgn_chunk, 1), 1);
0222                 for tmpmssi = 1:size(mask_suc_search, 1)
0223                     mask_suc_search(tmpmssi, 1) = ...
0224                         ismember(round(search_orgn_chunk(tmpmssi, 1)*10000)/10000, ...
0225                         round(events_suc_search(:,1)*10000)/10000);
0226                 end            
0227 
0228                 % then build the backward trace and expand
0229                 events_suc_search_filtered = ...
0230                     prev_result_chunks{chunkidx,relation_one_search_id}(mask_suc_search>0,:);
0231                 cell_events_suc_base = {};
0232 
0233                 for tmpessfi = 1:size(events_suc_search_filtered, 1)
0234                     event_suc_search_filtered_one = events_suc_search_filtered(tmpessfi,:);
0235 
0236                     tmp_mask_one = ...
0237                         (event_suc_search_filtered_one(1,1) == events_suc_search(:,1) ...
0238                         & (event_suc_search_filtered_one(1,2) == events_suc_search(:,2)));
0239                     cell_events_suc_base{tmpessfi,1} = events_suc_base(tmp_mask_one,:);
0240                 end
0241 
0242                 cell_base_length_list = cellfun(@(data_one) ...
0243                     size(data_one, 1), ...
0244                     cell_events_suc_base, ...
0245                     'UniformOutput', false);
0246                 cell_base_length_list = vertcat(cell_base_length_list{:});
0247 
0248                 unfiltered_data_list = setdiff(processed_data_list, ...
0249                     relation_one_base_id);
0250                 results_separate_one{chunkidx, relation_one_base_id} = ...
0251                         cell2mat(cell_events_suc_base);
0252 
0253                 % shrink and expand
0254                 for tmpudli = 1:length(unfiltered_data_list)
0255                     tmp_chunk_one = prev_result_chunks{chunkidx, unfiltered_data_list(tmpudli)};
0256                     tmp_chunk_one = cevent_repmat(...
0257                         tmp_chunk_one(mask_suc_search>0,:), cell_base_length_list);
0258                     results_separate_one{chunkidx, unfiltered_data_list(tmpudli)} = ...
0259                         tmp_chunk_one;
0260                 end
0261 
0262 
0263             % if both base and search chunks are processed
0264             elseif is_base_processed && is_search_processed
0265                 events_search_exist = ...
0266                     prev_result_chunks{chunkidx,relation_one_search_id};
0267                 events_search_exist = events_search_exist(mask_suc_base>0,:);
0268                 pattern_valid_mask = false(size(events_search_exist,1),1);
0269                 
0270                 for tmpssei = 1:size(events_search_exist,1)
0271                     tmp_event_one = events_search_exist(tmpssei,:);
0272                     pattern_valid_mask(tmpssei,1) = ...
0273                         ismember(tmp_event_one(1,1), events_suc_search{tmpssei,1}(:,1)) ...
0274                         && ismember(tmp_event_one(1,2), events_suc_search{tmpssei,1}(:,2));
0275                 end
0276                                 
0277                 % shrink
0278                 for tmppdli = 1:length(processed_data_list)
0279                     tmp_chunk_one = prev_result_chunks{chunkidx, processed_data_list(tmppdli)};
0280                     tmp_chunk_one = tmp_chunk_one(mask_suc_base>0,:);
0281                     tmp_chunk_one = tmp_chunk_one(pattern_valid_mask,:);
0282                     results_separate_one{chunkidx, processed_data_list(tmppdli)} = ...
0283                         tmp_chunk_one;
0284                 end
0285             end
0286         else
0287             % shrink and expand
0288             for tmpudli = 1:length(processed_data_list)
0289                 results_separate_one{chunkidx, processed_data_list(tmpudli)} = ...
0290                     zeros(0,3);
0291             end
0292         end
0293     end
0294     
0295     results_separates{relationidx} = results_separate_one;
0296 end
0297 
0298 for rsoi = 1:size(results_separate_one,1)
0299     results_final{rsoi,1} = cevent_pattern_eliminate_repeat(...
0300         cell2mat(results_separate_one(rsoi,:)));
0301 end
0302 % results_final = cell2mat(results_separate_one);
0303 % results_final = cevent_pattern_eliminate_repeat(results_final);
0304 
0305 if isfield(query, 'chunking_var_name')
0306     chunking_var_name = query.chunking_var_name;
0307     chunking_ref_column = query.chunking_ref_column;
0308     chunked_data = {};
0309     
0310     % check the validity of the ref column number
0311     LENGTH_CEVENT = 3;
0312     time_column_idx_list = sort...
0313         ([1:3:length(data)*LENGTH_CEVENT 2:3:length(data)*LENGTH_CEVENT]);
0314     if sum(~ismember(chunking_ref_column, time_column_idx_list)) > 0
0315         error('Invalid chunking_ref_column value!');
0316     end
0317          
0318         
0319     for rfi = 1:size(results_final,1)
0320         results_final_one = results_final{rfi, 1};
0321         sub_id = sub_list(rfi);
0322         for cvni = 1:length(chunking_var_name)
0323             chunking_var_name_one = chunking_var_name{cvni};
0324             if ~isempty(results_final_one)
0325                 tmp_ranges = [results_final_one(:,chunking_ref_column(1,1)) ...
0326                     results_final_one(:,chunking_ref_column(1,2))];
0327                 tmp_ranges = cevent_relative_intervals(...
0328                     tmp_ranges, query.chunking_whence, query.chunking_interval);
0329 
0330             % sum((tmp_ranges(:,2)-tmp_ranges(:,1))<0)
0331                 tmp_var_data = get_variable(sub_id, chunking_var_name_one);
0332                 tmp_var_data = extract_ranges(...
0333                     tmp_var_data, get_data_type(chunking_var_name_one), tmp_ranges);                
0334                 chunked_data{rfi, cvni} = tmp_var_data;
0335             else
0336                 chunked_data{rfi, cvni} = {};
0337             end
0338         end
0339     end
0340 end
0341

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