ET_PDToolkit/PDToolkit/@PDExperiment/calculate_aggregate.m

105 lines
2.6 KiB
Matlab

function[e] = calculate_aggregate(e, index);
%
% Extract aggregated data based on parameters / selection of
% subjects/sessions/trials
%
aggregate = e.aggregates(index);
try
tmp = aggregate.trials;
aggregate.trials = str2num(aggregate.trials);
if (isempty(aggregate.trials))
aggregate.trials = tmp;
end
catch
end
%% rewrite all subjects to list of subjects...
if strcmp(aggregate.subjects, 'All subjects')
e.aggregates(index).subjects = 1:length(e.subjects);
end
if strcmp(aggregate.sessions, 'All sessions')
e.aggregates(index).sessions = 1:length(e.subjects(1).sessions);
end
aggregate = e.aggregates(index);
stimulus_onsets = {};
idx =1;
for sub = 1:length(aggregate.subjects)
subject_id = aggregate.subjects(sub);
for sess = 1:length(aggregate.sessions)
sess_id = aggregate.sessions(sess);
%trial_query = aggregate.trials;
trials = aggregate.trials{subject_id}{sess_id};
%aggregate.e.get_trials_based_on_label(e.subjects(subject_id).sessions(sess_id), trial_query);
for t = 1:length(trials)
trial_id = trials(t);
trial_list(idx) = e.subjects(subject_id).sessions(sess_id).trials(trial_id).data;
stimulus_onsets{idx} = e.subjects(subject_id).sessions(sess_id).trials(trial_id).stimulus_onset - ...
e.subjects(subject_id).sessions(sess_id).trials(trial_id).trial_start;
idx = idx + 1;
end
end
end
if isempty(find(~cellfun(@isempty,stimulus_onsets)));
display('return?');
return;
end
min_onset = min([stimulus_onsets{:}]);
max_onset = min([stimulus_onsets{:}]);
diffs = [stimulus_onsets{:}] - min_onset;
max_diff = max(diffs);
clear trials
for t = 1:length(trial_list)
try
trials{t} = [ nan(1,max_diff - diffs(t)) trial_list(t).baseline_corrected.filtered(diffs(t)+1:end-max_diff)' ];
catch
display('error');
end
end
min_trial_length = min(cellfun(@length, trials));
for t = 1:length(trial_list)
try
trial_mat(t,:) = trials{t}(1:min_trial_length);
catch
end
end
mean_trial = mean(trial_mat);
std_trial = std(trial_mat);
var_trial = var(trial_mat);
l = length(mean_trial);
mean_trial([1:e.settings.FilterSize end-e.settings.FilterSize:end]) = NaN;
std_trial([1:e.settings.FilterSize end-e.settings.FilterSize:end]) = NaN;
var_trial([1:e.settings.FilterSize end-e.settings.FilterSize:end]) = NaN;
aggregate.mean = mean_trial;
aggregate.std = std_trial;
aggregate.var = var_trial;
e.aggregates(index).data = aggregate;