You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
ft_redefinetrial can fail (either crash or choose the wrong data) if the user tries to both make a selection of trials and make a trial-by-trial offset.
To Reproduce
% Create dummy data
data = [];
data.trial = mat2cell(randn(3, 4004), 3, [1001 1001 1001 1001]);
data.time = repmat({linspace(0, 1, 1001)}, 1, 4);
data.label = {'A', 'B', 'C'};
% This incorrectly shifts the second trial by 0.3 seconds
cfg = [];
cfg.trials = [false, true, false, false];
cfg.offset = [-500, -300];
data_out = ft_redefinetrial(cfg, data);
data_out.time{1}(1)
% This throws an error because it looks for more offsets than it got
cfg = [];
cfg.trials = [false, true, true, false];
cfg.offset = [-500, -300];
data_out = ft_redefinetrial(cfg, data);
% This throws an error because not enough toilims are provided
cfg = [];
cfg.trials = [false, true, true, true];
cfg.toilim = [0, 0.3; 0.3, 0.5];
data_out = ft_redefinetrial(cfg, data);
The 3rd bug happens because the code uses length(cfg.trials) even when it is logical and sum(cfg.trials) should be used instead in the following lines:
if length(cfg.offset)>1 && length(cfg.offset)~=length(cfg.trials)
cfg.offset =cfg.offset(cfg.trials);
end
if length(cfg.begsample)>1 && length(cfg.begsample)~=length(cfg.trials)
cfg.begsample =cfg.begsample(cfg.trials);
end
if length(cfg.endsample)>1 && length(cfg.endsample)~=length(cfg.trials)
cfg.endsample =cfg.endsample(cfg.trials);
end
Although it's possible to patch the code and keep the current (silent) "correction" of offsets and add a correction for toilims too, I think that the code should be stricter with users instead of guessing what they want, and return a clear error if the number of chosen trials is inconsistent with the number of offsets/toilims. If you think that makes sense I'll be happy to make the PR.
The text was updated successfully, but these errors were encountered:
I made a PR which I think corrects for the original intention (select from offset/toilims if the user provided those for all of the original data along with cfg.trials). I added a check that the number of offset/toilims is consistent, and expanded the help a bit.
The PR returns hopefully informative error messages for cases 1 & 3 in the test code and works as expected for case 2.
Describe the bug
ft_redefinetrial can fail (either crash or choose the wrong data) if the user tries to both make a selection of trials and make a trial-by-trial offset.
To Reproduce
The 3rd bug happens because the code uses length(cfg.trials) even when it is logical and sum(cfg.trials) should be used instead in the following lines:
fieldtrip/ft_redefinetrial.m
Lines 142 to 150 in 3be5222
Although it's possible to patch the code and keep the current (silent) "correction" of offsets and add a correction for toilims too, I think that the code should be stricter with users instead of guessing what they want, and return a clear error if the number of chosen trials is inconsistent with the number of offsets/toilims. If you think that makes sense I'll be happy to make the PR.
The text was updated successfully, but these errors were encountered: