270 lines
12 KiB
Matlab
270 lines
12 KiB
Matlab
function[obj] = rebuildMulti(obj);
|
|
|
|
%% assertions:
|
|
i=1; i1=1;i2=1;i3=1;
|
|
progressbar = waitbar(0, 'Parsing trial data...','Visible', 'off');
|
|
p= get(progressbar, 'Position'); p(2)=p(2)+100;
|
|
set(progressbar,'Position', p, 'Color', [1 1 1]);
|
|
t = get(get(progressbar, 'Children'), 'Title');
|
|
set(t, 'FontSize', 15, 'FontWeight', 'bold', 'FontName', 'Tahoma');
|
|
set(progressbar,'Visible','on');
|
|
|
|
new_trial = PDTrial;
|
|
|
|
obj = obj.check_patterns;
|
|
|
|
dependencies.trial_start= [];
|
|
dependencies.trial_end = [];
|
|
dependencies.baseline_onset = [];
|
|
dependencies.baseline_offset = [];
|
|
dependencies.stimulus_onset = [];
|
|
dependencies.stimulus_offset = [];
|
|
|
|
current_pattern.trial_start_pattern = 1;
|
|
current_pattern.trial_end_pattern = 1;
|
|
current_pattern.stimulus_onset_pattern = 1;
|
|
current_pattern.stimulus_offset_pattern = 1;
|
|
current_pattern.baseline_onset_pattern = 1;
|
|
current_pattern.baseline_offset_pattern = 1;
|
|
|
|
%% fill in all relative pattern timings :
|
|
for i = 1:length(obj.trial_start_pattern)
|
|
if iscell(obj.trial_start_pattern(i).pattern),
|
|
eval(['dependencies(' num2str(i) ').' obj.trial_start_pattern(i).pattern{2} ' = {''trial_start'' ' num2str(obj.trial_start_pattern(i).pattern{1}) '};']);
|
|
end
|
|
end
|
|
|
|
for i = 1:length(obj.trial_end_pattern)
|
|
if iscell(obj.trial_end_pattern(i).pattern),
|
|
eval(['dependencies(' num2str(i) ').' obj.trial_end_pattern(i).pattern{2} ' = {''trial_end'' ' num2str(obj.trial_end_pattern(i).pattern{1}) '};']);
|
|
end
|
|
end
|
|
|
|
for i = 1:length(obj.baseline_onset_pattern)
|
|
if iscell(obj.baseline_onset_pattern(i).pattern),
|
|
eval(['dependencies(' num2str(i) ').' obj.baseline_onset_pattern(i).pattern{2} ' = {''baseline_onset'' ' num2str(obj.baseline_onset_pattern(i).pattern{1}) '};']);
|
|
end
|
|
end
|
|
|
|
for i = 1:length(obj.baseline_offset_pattern)
|
|
if iscell(obj.baseline_offset_pattern(i).pattern),
|
|
eval(['dependencies(' num2str(i) ').' obj.baseline_offset_pattern(i).pattern{2} ' = {''baseline_offset'' ' num2str(obj.baseline_offset_pattern(i).pattern{1}) '};']);
|
|
end
|
|
end
|
|
|
|
for i = 1:length(obj.stimulus_onset_pattern)
|
|
if iscell(obj.stimulus_onset_pattern(i).pattern),
|
|
eval(['dependencies(' num2str(i) ').' obj.stimulus_onset_pattern(i).pattern{2} ' = {''stimulus_onset'' ' num2str(obj.stimulus_onset_pattern(i).pattern{1}) '};']);
|
|
end
|
|
end
|
|
|
|
for i = 1:length(obj.stimulus_offset_pattern)
|
|
if iscell(obj.stimulus_offset_pattern(i).pattern),
|
|
eval(['dependencies(' num2str(i) ').' obj.stimulus_offset_pattern(i).pattern{2} ' = {''stimulus_onset'' ' num2str(obj.stimulus_offset_pattern(i).pattern{1}) '};']);
|
|
end
|
|
end
|
|
%% go through all events
|
|
msg_indices = strmatch('MSG',obj.events_);
|
|
|
|
% tmp = sscanf(obj.events_{e}, '%*s %d %*s');
|
|
% data.msg_events(i,1) = tmp(1);
|
|
% data.msg_events(i,2) = e;
|
|
% i3=i3+1;
|
|
% end
|
|
|
|
%% these checks can be performed outside of the for-loop
|
|
itsp = ischar(obj.trial_start_pattern(current_pattern.trial_start).pattern);
|
|
itep = ischar(obj.trial_end_pattern(current_pattern.trial_end).pattern;
|
|
ibon = ischar(obj.baseline_onset_pattern(current_pattern.baseline_onest).pattern;
|
|
ibof = ischar(obj.baseline_offset_pattern(current_pattern.baseline_offset).pattern;
|
|
ison = ischar(obj.stimulus_onset_pattern(current_pattern.stimulus_onset).pattern);
|
|
isof = ischar(obj.stimulus_offset_pattern(current_pattern.stimulus_offset).pattern);
|
|
|
|
dep_ts = ~isempty(dependencies(current_pattern.trial_start).trial_start);
|
|
dep_te = ~isempty(dependencies(current_pattern.trial_end).trial_end);
|
|
dep_bon = ~isempty(dependencies(current_pattern.baseline_onset).baseline_onset);
|
|
dep_bof = ~isempty(dependencies(current_pattern.baseline_offset).baseline_offset);
|
|
dep_son = ~isempty(dependencies(current_pattern.stimulus_onset).stimulus_onset);
|
|
dep_sof = ~isempty(dependencies(current_pattern.stimulus_offset).stimulus_offset);
|
|
|
|
ii=1;
|
|
previous_trial_e_i = 1;
|
|
%for e_i = 1:length(msg_indices) %1:length(obj.events_)
|
|
while e_i <= length(msg_indices);
|
|
|
|
e = msg_indices(e_i);
|
|
%% a new trial marker has been found
|
|
if (itsp && ~isempty(regexp(obj.events_{e}, obj.trial_start_pattern(current_pattern.trial_start).pattern)))
|
|
rescan_trial = 0;
|
|
if isempty(new_trial.trial_end)
|
|
current_pattern.trial_end = current_pattern.trial_end + 1;
|
|
itsp = ischar(obj.trial_start_pattern(current_pattern.trial_start).pattern);
|
|
dep_ts = ~isempty(dependencies(current_pattern.trial_start).trial_start);
|
|
|
|
|
|
if current_pattern.trial_end <= length(obj.trial_start_pattern)
|
|
rescan_trial = 1;
|
|
end
|
|
end
|
|
|
|
if isempty(new_trial.stimulus_onset)
|
|
current_pattern.stimulus_onset = current_pattern.stimulus_onset + 1;
|
|
ison = ischar(obj.stimulus_onset_pattern(current_pattern.stimulus_onset).pattern);
|
|
dep_son = ~isempty(dependencies(current_pattern.stimulus_onset).stimulus_onset);
|
|
|
|
if current_pattern.stimulus_onset <= length(obj.stimulus_onset_pattern)
|
|
rescan_trial = 1;
|
|
end
|
|
end
|
|
|
|
if isempty(new_trial.stimulus_offset)
|
|
current_pattern.stimulus_offset = current_pattern.stimulus_offset + 1;
|
|
isof = ischar(obj.stimulus_offset_pattern(current_pattern.stimulus_offset).pattern);
|
|
dep_sof = ~isempty(dependencies(current_pattern.stimulus_offset).stimulus_offset);
|
|
|
|
if current_pattern.stimulus_offset <= length(obj.stimulus_offset_pattern)
|
|
rescan_trial = 1;
|
|
end
|
|
|
|
end
|
|
|
|
if isempty(new_trial.baseline_offset)
|
|
current_pattern.baseline_offset = current_pattern.baseline_offset + 1;
|
|
ibof = ischar(obj.baseline_offset_pattern(current_pattern.baseline_offset).pattern;
|
|
dep_bof = ~isempty(dependencies(current_pattern.baseline_offset).baseline_offset
|
|
|
|
if current_pattern.baseline_offset <= length(obj.baseline_offset_pattern)
|
|
rescan_trial = 1;
|
|
end
|
|
end
|
|
|
|
if isempty(new_trial.baseline_onset)
|
|
current_pattern.baseline_onset = current_pattern.baseline_onset + 1;
|
|
ibon = ischar(obj.baseline_onset_pattern(current_pattern.baseline_onest).pattern;
|
|
dep_bof = ~isempty(dependencies(current_pattern.baseline_offset).baseline_offset);
|
|
|
|
if current_pattern.baseline_onset <= length(obj.baseline_onset_pattern)
|
|
rescan_trial = 1;
|
|
end
|
|
end
|
|
|
|
if (rescan_trial)
|
|
e_i = previous_trial_e_i;
|
|
continue;
|
|
end
|
|
|
|
%% if trial_start has been set, a new trial starts
|
|
if ~isempty(new_trial.trial_start)
|
|
trial_samples = intersect(find(obj.samples(:,1) > new_trial.trial_start), ...
|
|
find(obj.samples(:,1) < new_trial.trial_end));
|
|
|
|
new_trial.time = obj.samples(trial_samples, 1);
|
|
new_trial.data.uncorrected.raw = obj.samples(trial_samples, 4);
|
|
obj.trials(i) = new_trial;
|
|
new_trial = PDTrial;
|
|
previous_trial_e_i = e_i;
|
|
i=i+1;
|
|
if (mod(i,10)==0) % updating waitbar is very time consuming
|
|
waitbar(e_i/length(msg_indices), progressbar, sprintf('Session: %s - Parsing data for trial %d...', obj.name, i));
|
|
end
|
|
end
|
|
|
|
new_trial.trial_start = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
if dep_ts
|
|
eval(['new_trial.' dependencies(current_pattern.trial_start).trial_start{1} ' = ' num2str(new_trial.trial_start) ' + ' num2str(dependencies(current_pattern.trial_start).trial_start{2}) ';']);
|
|
end
|
|
end
|
|
|
|
%% a new baseline marker (offset) has been found
|
|
if (ibon && ~isempty(regexp(obj.events_{e}, obj.baseline_onset_pattern(current_pattern.baseline_onset).pattern)))
|
|
new_trial.baseline_onset = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
if dep_bon
|
|
eval(['new_trial.' dependencies(current_pattern.baseline_onset).baseline_onset{1} ' = ' num2str(new_trial.baseline_onset) ' + ' num2str(dependencies(current_pattern.baseline_onset).baseline_onset{2}) ';']);
|
|
|
|
end
|
|
end
|
|
|
|
%% a new baseline marker (offset) has been found
|
|
if (ibof && ~isempty(regexp(obj.events_{e}, obj.baseline_offset_pattern(current_pattern.baseline_offset).pattern)))
|
|
new_trial.baseline_offset = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
if dep_bof
|
|
eval(['new_trial.' dependencies((current_pattern.baseline_onset).baseline_offset{1} ' = ' num2str(new_trial.baseline_offset) ' + ' num2str(dependencies(current_pattern.baseline_onset).baseline_offset{2}) ';']);
|
|
|
|
end
|
|
end
|
|
|
|
|
|
%% a new stimulus onset marker has been found
|
|
if (ison && ~isempty(regexp(obj.events_{e}, obj.stimulus_onset_pattern(current_pattern.stimulus_onset).pattern)))
|
|
new_trial.stimulus_onset = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
|
|
if dep_son
|
|
eval(['new_trial.' dependencies(current_pattern.stimulus_onset).stimulus_onset{1} ' = ' num2str(new_trial.stimulus_onset) ' + ' num2str(dependencies(current_pattern.stimulus_onset).stimulus_onset{2}) ';']);
|
|
|
|
end
|
|
end
|
|
|
|
%% a new stimulus offset marker has been found
|
|
if (isof && ~isempty(regexp(obj.events_{e}, obj.stimulus_offset_pattern(current_pattern.stimulus_offset).pattern)))
|
|
new_trial.stimulus_offset = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
|
|
if dep_bof
|
|
eval(['new_trial.' dependencies(current_pattern.stimulus_offset).stimulus_offset{1} ' = ' num2str(new_trial.stimulus_offset) ' + ' num2str(dependencies(current_pattern.stimulus_offset).stimulus_offset{2}) ';']);
|
|
|
|
end
|
|
end
|
|
|
|
%% a new trial end marker has been found
|
|
if ~isempty(new_trial.trial_end)
|
|
|
|
else
|
|
if (itep && ~isempty(regexp(obj.events_{e}, obj.trial_end_pattern(current_pattern.trial_end).pattern)))
|
|
new_trial.trial_end = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
|
|
if dep_te
|
|
eval(['new_trial.' dependencies(current_pattern.trial_end).trial_end ' = ' num2str(new_trial.time) ' + ' num2str(dependencies(current_pattern.trial_end).trial_end{2}) ';']);
|
|
|
|
end
|
|
|
|
if (isempty(new_trial.trial_start))
|
|
warning('Trial end detected, but no previous Trial start has been found : skipping trial');
|
|
end
|
|
|
|
%% [trialdata_i quality_i qc_i blink_list_i] = preprocess_trial(data.samples(total_trial_ind,:), settings, sprintf('Trial %2d',i));
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
%% add the final trial
|
|
|
|
trial_samples = intersect(find(obj.samples(:,1) > new_trial.trial_start), ...
|
|
find(obj.samples(:,1) < new_trial.trial_end));
|
|
|
|
new_trial.time = obj.samples(trial_samples, 1);
|
|
new_trial.data.uncorrected.raw = obj.samples(trial_samples, 4);
|
|
obj.trials(i) = new_trial;
|
|
|
|
|
|
|
|
|
|
close(progressbar);
|
|
|
|
obj = label(obj);
|
|
|
|
% data.trialdata.indices = trial_indices;
|
|
% data.trialdata.indices_description = {'Fixation6s starting','Stimulus Presentation','Fixation15s starting','Fixation15s ending'};
|
|
% data.baseline.start = baseline_start;
|
|
% data.baseline.end = baseline_end;
|
|
|
|
% baseline = [];
|
|
% for b = 1: length(data.baseline.data)
|
|
% baseline = [baseline data.baseline.data{b}];
|
|
% end
|
|
|
|
% cumulative = cumsum(hist(baseline, 10000));
|
|
%%turning_points = find(diff(cumulative) > 1000);
|
|
%%data.baseline.cumulative = cumulative;
|
|
%%data.baseline.mean = mean(data.baseline.cumulative(1:turning_points(1)));
|
|
|