Skip to content

fix: interpolate only the requested time range - #980

Merged
arnodelorme merged 2 commits into
developfrom
cursor/fix-pop-interp-time-range-1d9a
Nov 27, 2026
Merged

arnodelorme merged 2 commits into
developfrom
cursor/fix-pop-interp-time-range-1d9a

Conversation

@suraj-ranganath

@suraj-ranganath suraj-ranganath commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

pop_interp was clearing a scripted time range, and a range chosen in the GUI never appeared in the history command. Both now follow the help: a passed range is kept, and a filled dialog field is recorded. Calls that omit the range, including pop_reref and the Tools menu, still interpolate the whole recording.

eeg_interp then restored original samples with floor(tsrate) and stopped at floor(xmaxsrate). On sample_data/eeglab_data.set that left the sample at 3 s unchanged and still replaced the final sample. The window now comes from EEG.times (milliseconds, same limits as xmin/xmax). Samples inside [tmin, tmax] are interpolated, both endpoints included, and every other sample is copied back from the input. The same latency window is applied in each epoch. A new channel supplied as a chanlocs structure has nothing to restore, so that path is unchanged.

On this recording, [1 3] s is samples 129 through 385. Sample 384 (2.992 s) is inside the window; sample 386 and sample 30504 are not. MATLAB is required for the spline itself. The checks below should print 0 for every outside difference, 1 for the endpoint changes, and a positive difference for each 3-argument call.

eeglab nogui;
EEG = pop_loadset('filename', 'eeglab_data.set', 'filepath', 'sample_data/');
orig = EEG.data(5,:);
out = pop_interp(EEG, 5, 'spherical', [1 3]);
t = EEG.times/1000;
outside = t < 1 | t > 3;
i1 = find(t == 1, 1);
i3 = find(t == 3, 1);
fprintf('outside max abs diff (0): %g\n', max(abs(out.data(5,outside) - orig(outside))));
fprintf('1 s sample %d changed (1): %d\n', i1, out.data(5,i1) ~= orig(i1));
fprintf('3 s sample %d changed (1): %d\n', i3, out.data(5,i3) ~= orig(i3));
outall = pop_interp(EEG, 5, 'spherical');
fprintf('3-arg outside max abs diff (>0): %g\n', max(abs(outall.data(5,outside) - orig(outside))));
fprintf('3-arg matches inside the window (0): %g\n', max(abs(outall.data(5,~outside) - out.data(5,~outside))));

EEGe = pop_loadset('filename', 'eeglab_data_epochs_ica.set', 'filepath', 'sample_data/');
orige = EEGe.data(5,:,:);
oute = pop_interp(EEGe, 5, 'spherical', [0 0.5]);
te = EEGe.times/1000;
eout = te < 0 | te > 0.5;
i0 = find(te == 0, 1);
i05 = find(te == 0.5, 1);
fprintf('epoch outside max abs diff (0): %g\n', max(abs(oute.data(5,eout,:) - orige(1,eout,:)), [], 'all'));
fprintf('epoch 0 s changed (1): %d\n', any(oute.data(5,i0,:) ~= orige(1,i0,:)));
fprintf('epoch 0.5 s changed (1): %d\n', any(oute.data(5,i05,:) ~= orige(1,i05,:)));
outalle = pop_interp(EEGe, 5, 'spherical');
fprintf('epoch 3-arg outside max abs diff (>0): %g\n', max(abs(outalle.data(5,eout,:) - orige(1,eout,:)), [], 'all'));
Open in Web Open in Cursor 

@claude

claude Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

@cursor cursor Bot changed the title fix: honour pop_interp time ranges from scripts and the GUI history fix: interpolate only the requested time range Nov 5, 2026
@suraj-ranganath
suraj-ranganath marked this pull request as ready for review November 5, 2026 20:51

Copy link
Copy Markdown
Member Author

@arnodelorme this is the fix for the pop_interp time range issue from our eegprep GUI audit, ready for your review.

pop_interp no longer overwrites a time range passed from a script, and the GUI now writes the range into the history command. While checking it in MATLAB R2025b I found that eeg_interp also left the last sample of a recording interpolated and left out the sample at the exact end of the range. It now takes the window from EEG.times with inclusive endpoints, for continuous and epoched data.

Checked in MATLAB: samples outside the range are bit-identical to the input, values inside match unrestricted interpolation exactly, and calls without a range (pop_reref, three-argument pop_interp, direct eeg_interp, missing-channel restoration) give exactly the same output as before. The STUDY callers don't pass a range, and I read them but didn't run them. The PR has no automated tests yet, so let me know if you want some added.

Suraj

@claude

claude Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

@arnodelorme

Copy link
Copy Markdown
Collaborator

This pull request fixes three issues with time-range channel interpolation in functions/popfunc/pop_interp.m and functions/popfunc/eeg_interp.m:

  • What was checked:
    • In pop_interp.m (lines 61 to 63), t_range was unconditionally reset to '', overwriting any 4th argument passed from a script, and line 150 omitted the time range from the returned history string com when chosen in the GUI. Both are fixed cleanly.
    • In eeg_interp.m (lines 317 to 335), the previous times_2b_ignored = [1:floor(t_start*ORIEEG.srate), floor(t_end*ORIEEG.srate):floor(ORIEEG.xmax*ORIEEG.srate)] only ran when length(size(tmpdata)) == 2 (skipping epoched datasets), left the final sample ORIEEG.pnts interpolated because floor(ORIEEG.xmax*ORIEEG.srate) is ORIEEG.pnts - 1, and excluded the upper boundary sample t_end. Computing outside = sample_ms < t_start*1000 | sample_ms > t_end*1000 from ORIEEG.times (with a linspace fallback) and guarding ~isstruct(bad_elec) restores all samples outside [t_start, t_end] for both continuous and epoched datasets while leaving full-recording and missing-channel calls unchanged.
    • One small GUI detail to note: in pop_interp.m (lines 83 to 116), the GUI dialog only shows the Time range [min max] (s) edit box when isempty(EEG.epoch) (continuous datasets). For epoched datasets, eeg_interp and scripted pop_interp(EEG, chans, method, [tmin tmax]) now interpolate [tmin, tmax] per epoch, while the GUI dialog for epoched data still omits the time-range field (numel(res) is 1, which the new numel(res) > 1 && ~isempty(res{2}) check handles safely).
  • MATLAB R2025b verification: Ran both reproduction blocks in MATLAB R2025b on sample_data/eeglab_data.set ([1 3] s on channel 5) and sample_data/eeglab_data_epochs_ica.set ([0 0.5] s on channel 5). For both continuous and epoched datasets, the maximum absolute difference outside the requested window is 0, both endpoint samples (1 s and 3 s continuous, 0 s and 0.5 s epoched) are interpolated, and values inside the window match the 3-argument full-recording interpolation (inside_diff = 0).
  • Recommendation: Ready to merge.

— orchestrator

@arnodelorme
arnodelorme merged commit fb72d8d into develop Nov 27, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants