Home > data-analysis > manip > cevent > cevent_shuffling.m

cevent_shuffling

PURPOSE ^

This function shuffles the cevents within certain ranges while reserving

SYNOPSIS ^

function cevents_shuffled = cevent_shuffling(cevents, ranges, is_trial)

DESCRIPTION ^

 This function shuffles the cevents within certain ranges while reserving
 their duration

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function cevents_shuffled = cevent_shuffling(cevents, ranges, is_trial)
0002 % This function shuffles the cevents within certain ranges while reserving
0003 % their duration
0004 
0005 ROI_EMPTY = 0;
0006 
0007 if isempty(cevents)
0008     cevents_shuffled = zeros(0, 3);
0009     return;
0010 end
0011 
0012 if nargin < 3
0013     is_trial = false;
0014 end
0015 
0016 %%%%%%%%%%%
0017 % check if there is overlaps within the cevents
0018 cevents = cevent_sort_by_time(unique(cevents, 'rows'), 1);
0019 cevent_gaps = cevents(2:end, 1) - cevents(1:end-1, 2);
0020 if sum(cevent_gaps < -0.0001) >0
0021     [cevents [nan; cevent_gaps]]
0022     error('There are overlapping cevents in the data. This function won''t perform correctly.');
0023 end
0024 
0025 % only get cevents within ranges
0026 cevents = extract_ranges(cevents, 'cevent', ranges);
0027 num_ranges = size(ranges, 1);
0028 cevents_shuffled = cell(1, num_ranges);
0029 
0030 for ridx = 1:size(ranges, 1)
0031     cevents_one = cevents{ridx};
0032     range_one = ranges(ridx, :);
0033     
0034     if isempty(cevents_one)
0035         continue
0036     end
0037     
0038     % get the gap events
0039     events_empty = event_NOT(cevents_one(:,1:2), range_one);
0040     num_events = size(events_empty, 1);
0041     cevents_empty = [events_empty repmat(ROI_EMPTY, num_events, 1)];
0042     
0043     % turn the time series into cevents with ROI and cevents with empty ROI
0044     % 0 with correct time ordering
0045     cevents_all = cevent_sort_by_time([cevents_one; cevents_empty], 1);
0046     num_cevents = size(cevents_all, 1);
0047     
0048     % randomize the ordering
0049     shuffle_indices = randperm(num_cevents);
0050     
0051     % constructing the randomized cevents based on the new ordering
0052     start_time = range_one(1, 1);
0053     cevents_new = [];
0054     for cidx = 1:num_cevents
0055         index_one = shuffle_indices(cidx);
0056         cevent_one = cevents_all(index_one, :);
0057         end_time = start_time + cevent_one(1,2) - cevent_one(1, 1);
0058         cevents_new = [cevents_new; [start_time end_time cevent_one(1,3)]];
0059         
0060         start_time = end_time;
0061     end
0062     cevents_new = cevents_new(cevents_new(:, 3) > ROI_EMPTY, :);
0063     cevents_shuffled{1, ridx} = cevents_new;
0064 end
0065 
0066 if ~is_trial
0067     cevents_shuffled = vertcat(cevents_shuffled{:});
0068 end
0069 
0070

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