Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/pspm_extract_segments.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
switch method
case 'data'
data_raw = data;
if isnumeric(data), data_raw = {data}; end
case 'file'
for i_sn = 1:numel(datafile)
[lsts, alldata{i_sn}] = pspm_load_channel(datafile{i_sn}, channel);
Expand All @@ -152,6 +153,9 @@
case 'model'
if strcmpi(data.modeltype, 'glm')
data_raw = data.input.data;
timing = data.input.timing;
options.timeunits = data.input.timeunits;
events = data.input.events;
elseif strcmpi(data.modeltype, 'dcm')
data_raw = data.input.scr;
else
Expand Down Expand Up @@ -181,11 +185,6 @@
session_duration(i_sn, 1) = numel(data_raw{i_sn});
end

if strcmpi(method, 'model') && strcmpi(data.modeltype, 'glm')
timing = data.input.timing;
end


if strcmpi(method, 'model') && strcmpi(data.modeltype, 'dcm')
% DCM has no condition information
onsets{1} = cellfun(@(x, y) pspm_time2index(x, sr , y), ...
Expand All @@ -194,6 +193,7 @@
'UniformOutput', false);
names{1} = 'all';
n_cond = 1;
options.timeunits = 'seconds';
else
[lsts, multi] = pspm_get_timing('onsets', timing, options.timeunits);
[msts, onsets] = pspm_multi2index(options.timeunits, multi, sr, session_duration, events);
Expand All @@ -204,6 +204,8 @@
end
end

% onsets has the structure: onsets{i_cond}{i_sn}

%% prepare missing
if isfield(options, 'missing')
missing = options.missing;
Expand Down
7 changes: 6 additions & 1 deletion src/pspm_glm.m
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@
warning('ID:invalid_input', 'Could not load the specified markerchannel');
return
end
events{iFile} = data.data(:) * data.header.sr;
% here, data.header.sr correponds to the time resolution of the
% time stamps which should always be 1, but this will convert any
% potentially deviating time stamp resolution to seconds
events{iFile} = data.data(:) / data.header.sr;
if strcmp(model.timeunits,'markervalues')
model.timing{iFile}.markerinfo = data.markerinfo;
end
Expand Down Expand Up @@ -491,6 +494,7 @@
%% 12 collect data & regressors for output model
glm.input.data = y;
glm.input.sr = sr;
glm.input.events = events;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add the description of glm to Outputs in help text if useful

glm.Y = Y;
glm.M = M; % set to 1 if data is missing, otherwise set to 0
glm.infos.sr = newsr;
Expand Down Expand Up @@ -758,6 +762,7 @@

%% 17 save data
% 17.1 overwrite is determined in load1 --
glm.datetime = datetime;
savedata = struct('glm', glm);
[sts, data_load1, mdltype_load1] = pspm_load1(model.modelfile, 'save', savedata, options);

8 changes: 4 additions & 4 deletions src/pspm_multi2index.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
% [onsets, durations] = pspm_multi2index('markers', multi, sr, session_duration, events)
% ● Arguments
% * multi : multi structure from pspm_get_timing
% sr : sampling rate, or vector of sampling rates with the same number of
% sr : sampling rate, or vector of sampling rates with the same number of
% elements as multi
% * sr_ratio : If data was downsampled wrt onset definition, ratio of new_sr/old_sr; or
% vector of sampling rate ratios. Otherwise, should be 1.
Expand All @@ -23,7 +23,7 @@
sr = repmat(sr, numel(multi), 1);
end
if numel(sr) ~= numel(multi) || numel(session_duration) ~= numel(multi)
warning('ID:invalid_input', 'No event definition provided.'); return;
warning('ID:invalid_input', 'Number of sessions do not match.'); return;
end
if ~isempty(multi)
for iSn = 1:numel(multi)
Expand All @@ -40,8 +40,8 @@
events = varargin{1};
end
% markers are timestamps in seconds
onsets{n}{iSn} = pspm_time2index(multi(iSn).onsets{n}, sr(iSn), session_duration, 0, events{iSn});
durations{n}{iSn} = pspm_time2index(multi(iSn).durations{n}, sr(iSn), session_duration, 1);
onsets{n}{iSn} = pspm_time2index(multi(iSn).onsets{n}, sr(iSn), session_duration(iSn), 0, events{iSn});
durations{n}{iSn} = pspm_time2index(multi(iSn).durations{n}, sr(iSn), session_duration(iSn), 1);
end
end
end
Expand Down
15 changes: 7 additions & 8 deletions test/pspm_extract_segments_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ function test_manual_length(this,nr_trial,nan_ratio)
function test_auto_mode_glm_with_markers(this)
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.RelativeTolerance
rehash
load(['ImportTestData' filesep 'fitted_models' filesep 'glm_scr_cond_marker.mat'], 'glm');
load(['ImportTestData' filesep 'fitted_models' filesep 'glm_orig_data.mat'], 'data');
load(fullfile('ImportTestData', 'fitted_models', 'glm_scr_cond_marker.mat'), 'glm');
load(fullfile('ImportTestData', 'fitted_models', 'glm_orig_data.mat'), 'data');
if ~isfield(glm.input, 'channel') && isfield(glm.input, 'chan')
glm.input.channel = glm.input.chan;
glm.input = rmfield(glm.input,'chan'); % rename the field channel to chan
Expand Down Expand Up @@ -221,13 +220,13 @@ function test_auto_mode_glm_with_markers(this)
this.verifyThat(nanmean(seg{3}.data, 1), IsEqualTo(seg{3}.mean, 'Within', RelativeTolerance(1e-10)));
this.verifyThat(nanstd(seg{3}.data, 0, 1), IsEqualTo(seg{3}.std, 'Within', RelativeTolerance(1e-10)));
% compute statistics from scratch
for i = 1:numel(glm.timing.multi.durations)
for i = 1:numel(glm.timing.multi.onsets)
all_vecs = [];
seg_len = this.options.length * sr;
onset_i = glm.timing.multi.onsets{i};
for j = 1:numel(onset_i)
onset = round(onset_i(j) * sr) + 1;
all_vecs = [all_vecs, input_data(onset : onset + seg_len - 1)];
onsets_i = pspm_time2index(marker(glm.timing.multi.onsets{i}), sr);
for j = 1:numel(onsets_i)
onset = onsets_i(j);
all_vecs = [all_vecs, input_data(onset : onset + seg_len - 1)];
end
all_vecs = all_vecs';
expected_mean = nanmean(all_vecs, 1);
Expand Down