Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corner cases in ft_redefinetrial (trials selection + variable offset/toilim) #2243

Open
NirOfir opened this issue May 3, 2023 · 1 comment

Comments

@NirOfir
Copy link
Contributor

NirOfir commented May 3, 2023

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.

NirOfir added a commit to NirOfir/fieldtrip that referenced this issue May 4, 2023
@NirOfir
Copy link
Contributor Author

NirOfir commented May 4, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant