235 lines
9.8 KiB
Matlab
235 lines
9.8 KiB
Matlab
function[objs] = rebuild(objs, params);
|
|
%% PDSession.rebuild
|
|
%
|
|
% based on patterns parse the datafile and create trials
|
|
%
|
|
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');
|
|
|
|
for o = 1:length(objs)
|
|
|
|
obj = objs(o);
|
|
|
|
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 = [];
|
|
|
|
%% fill in all relative pattern timings :
|
|
|
|
%% TODO : add previous relative items inhere
|
|
|
|
if iscell(obj.trial_start_pattern),
|
|
ind = length(dependencies);
|
|
eval(['dependencies.' obj.trial_start_pattern{2} ' = {''trial_start'' ' num2str(obj.trial_start_pattern{1}) '};']);
|
|
end
|
|
|
|
if iscell(obj.trial_end_pattern),
|
|
ind = length(dependencies);
|
|
eval(['dependencies.' obj.trial_end_pattern{2} '{' num2str(ind) '} = {''trial_end'' ' num2str(obj.trial_end_pattern{1}) '};']);
|
|
end
|
|
|
|
if iscell(obj.baseline_onset_pattern),
|
|
%display('Dependency found for baseline onset pattern');
|
|
eval(['ind = size(dependencies.' obj.baseline_onset_pattern{2} ', 2) +1;']);
|
|
eval(['dependencies.' obj.baseline_onset_pattern{2} '{' num2str(ind) '} = {''baseline_onset'' ' num2str(obj.baseline_onset_pattern{1}) '};']);
|
|
end
|
|
|
|
if iscell(obj.baseline_offset_pattern),
|
|
%display('Dependency found for baseline offset pattern');
|
|
eval(['ind = size(dependencies.' obj.baseline_offset_pattern{2} ', 2) +1;']);
|
|
eval(['dependencies.' obj.baseline_offset_pattern{2} '{' num2str(ind) '} = {''baseline_offset'' ' num2str(obj.baseline_offset_pattern{1}) '};']);
|
|
end
|
|
|
|
if iscell(obj.stimulus_onset_pattern),
|
|
eval(['ind = size(dependencies.' obj.stimulus_onset_pattern{2} ', 2) +1;']);
|
|
eval(['dependencies.' obj.stimulus_onset_pattern{2} '{' num2str(ind) '} = {''stimulus_onset'' ' num2str(obj.stimulus_onset_pattern{1}) '};']);
|
|
end
|
|
|
|
if iscell(obj.stimulus_offset_pattern),
|
|
eval(['ind = size(dependencies.' obj.stimulus_offset_pattern{2} ', 2) +1;']);
|
|
eval(['dependencies.' obj.stimulus_offset_pattern{2} '{' num2str(ind) '} = {''stimulus_offset'' ' num2str(obj.stimulus_offset_pattern{1}) '};']);
|
|
end
|
|
|
|
|
|
%% go through all events
|
|
msg_indices = strmatch('MSG',obj.events_);
|
|
|
|
|
|
%% these checks can be performed outside of the for-loop
|
|
itsp = ischar(obj.trial_start_pattern);
|
|
itep = ischar(obj.trial_end_pattern);
|
|
ibon = ischar(obj.baseline_onset_pattern);
|
|
ibof = ischar(obj.baseline_offset_pattern);
|
|
ison = ischar(obj.stimulus_onset_pattern);
|
|
isof = ischar(obj.stimulus_offset_pattern);
|
|
|
|
dep_ts = ~isempty(dependencies.trial_start);
|
|
dep_te = ~isempty(dependencies.trial_end);
|
|
dep_bon = ~isempty(dependencies.baseline_onset);
|
|
dep_bof = ~isempty(dependencies.baseline_offset);
|
|
dep_son = ~isempty(dependencies.stimulus_onset);
|
|
dep_sof = ~isempty(dependencies.stimulus_offset);
|
|
|
|
ii=1;
|
|
|
|
for e_i = 1:length(msg_indices) %1:length(obj.events_)
|
|
|
|
e = msg_indices(e_i);
|
|
%% a new trial marker has been found
|
|
if (itsp && ~isempty(regexp(obj.events_{e}, obj.trial_start_pattern)))
|
|
|
|
|
|
%% if trial_start has been set, a new trial starts
|
|
if ~isempty(new_trial.trial_start)
|
|
|
|
%% reset trial_end to new trial_start
|
|
if (strcmp(obj.trial_start_pattern, obj.trial_end_pattern) == 1)
|
|
time = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
new_trial.trial_end = time(1);
|
|
end
|
|
|
|
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.(obj.eye).uncorrected.raw = obj.samples(trial_samples, 4);
|
|
obj.trials(i) = new_trial;
|
|
new_trial = PDTrial;
|
|
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
|
|
%% no previous trial was defined
|
|
time = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
new_trial.trial_start = time(1); %% disregard optional numbers in the message
|
|
if dep_ts
|
|
for db = 1:length(dependencies.trial_start)
|
|
dependency = dependencies.trial_start{db};
|
|
eval(['new_trial.' dependency{1} ' = ' num2str(new_trial.trial_start) ' + ' num2str(dependency{2}) ';']);
|
|
end
|
|
end
|
|
end
|
|
|
|
%% a new baseline marker (onset) has been found
|
|
if (ibon && ~isempty(regexp(obj.events_{e}, obj.baseline_onset_pattern)))
|
|
new_trial.baseline_onset = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
if dep_bon
|
|
for db = 1:length(dependencies.baseline_onset)
|
|
dependency = dependencies.baseline_onset{db};
|
|
eval(['new_trial.' dependency{1} ' = ' num2str(new_trial.baseline_onset) ' + ' num2str(dependency{2}) ';']);
|
|
end
|
|
end
|
|
end
|
|
|
|
%% a new baseline marker (offset) has been found
|
|
if (ibof && ~isempty(regexp(obj.events_{e}, obj.baseline_offset_pattern)))
|
|
new_trial.baseline_offset = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
if dep_bof
|
|
for db = 1:length(dependencies.baseline_offset)
|
|
dependency = dependencies.baseline_offset{db};
|
|
eval(['new_trial.' dependency{1} ' = ' num2str(new_trial.baseline_offset) ' + ' num2str(dependency{2}) ';']);
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
%% a new stimulus onset marker has been found
|
|
if (ison && ~isempty(regexp(obj.events_{e}, obj.stimulus_onset_pattern)))
|
|
|
|
new_trial.stimulus_onset = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
|
|
if dep_son
|
|
for db = 1:length(dependencies.stimulus_onset)
|
|
dependency = dependencies.stimulus_onset{db};
|
|
eval(['new_trial.' dependency{1} ' = ' num2str(new_trial.stimulus_onset) ' + ' num2str(dependency{2}) ';']);
|
|
end
|
|
end
|
|
end
|
|
|
|
%% a new stimulus offset marker has been found
|
|
if (isof && ~isempty(regexp(obj.events_{e}, obj.stimulus_offset_pattern)))
|
|
new_trial.stimulus_offset = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
|
|
if dep_sof
|
|
for db = 1:length(dependencies.stimulus_offset)
|
|
dependency = dependencies.stiulus_offset{db};
|
|
eval(['new_trial.' dependency{1} ' = ' num2str(new_trial.stimulus_offset) ' + ' num2str(dependency{2}) ';']);
|
|
end
|
|
|
|
end
|
|
end
|
|
|
|
%% a new trial end marker has been found
|
|
if ~isempty(new_trial.trial_end)
|
|
|
|
else
|
|
%% if trial_start == trial_end matching, skip the current line
|
|
if (strcmp(obj.trial_start_pattern, obj.trial_end_pattern) == 1)
|
|
continue;
|
|
end
|
|
|
|
if (itep && ~isempty(regexp(obj.events_{e}, obj.trial_end_pattern)))
|
|
end_time = sscanf(obj.events_{e},'%*s %d %*s %*s');
|
|
new_trial.trial_end = end_time(1); % disregard numbers in message
|
|
|
|
if dep_te
|
|
for db = 1:length(dependencies.trial_end)
|
|
dependency = dependencies.trial_end{db};
|
|
eval(['new_trial.' dependency{1} ' = ' num2str(new_trial.trial_end) ' + ' num2str(dependency{2}) ';']);
|
|
end
|
|
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
|
|
if (~isempty(new_trial.trial_start) & ~isempty(new_trial.trial_end))
|
|
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.(obj.eye).uncorrected.raw = obj.samples(trial_samples, 4);
|
|
obj.trials(i) = new_trial;
|
|
end
|
|
|
|
|
|
close(progressbar);
|
|
|
|
obj = label(obj, params);
|
|
|
|
% 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)));
|
|
|
|
objs(o) = obj;
|
|
end
|