Skip to content

pop_select keeps the last sample at 300, 1024, 2048 and 30000 Hz from eeg_point2lat rounding #1007

In review

Description

@arnodelorme

pop_select still keeps the last sample of a continuous recording when a point range ends one sample before it, at sampling rates such as 300, 1024, 2048 and 30000 Hz. This is the second cause noted in sccn/eeglab issue 1001 (pop_select keeps the last sample at 250, 500 and 1000 Hz), after pull request 1002 (eeg_lat2point: round-off tolerance, so pop_select no longer keeps the last sample at 250, 500 and 1000 Hz).

  • What happens. On 10 samples at 300 Hz (0 to 9), pop_select(EEG, 'point', [3 9]) returns samples 3 to 10 (8 samples, values 2 to 9) instead of samples 3 to 9 (7 samples, values 2 to 8). On 10 samples at 1024 Hz, pop_select(EEG, 'nopoint', [10 10]) also keeps sample 10 instead of removing it.
  • Cause. pop_select.m converts point ranges to seconds with eeg_point2lat, which rounds times to 1e-9 s (round(newlat*1E9)/1E9), shifts the rejection edges by 1/EEG.srate, and converts them back to points with eeg_lat2point. At 300 Hz, sample 9 (8/300 s) rounds up to 0.026666667 s, so adding 1/300 s gives 0.030000000333333336 s, which maps to point 10.0000001, about 1e-7 sample past EEG.xmax. eeg_lat2point flags that excess (flag = 1), and pop_select (lines 592 to 594) drops the [10 10] rejection region.
  • How often. Across 9,243 short point ranges (every [a b] with a < b on 10 and on 37 samples at 13 sampling rates), 156 keep the last sample for this reason, all at 300, 1024, 2048 and 30000 Hz and all ending at n - 1. The Python port EEGPrep copied this path and had the same 156 cases.
  • Fix. Keep 'point', 'nopoint' and 'rmpoint' in sample indices on continuous data (EEG.trials == 1) in pop_select.m, and build the rejection regions directly in points for eeg_eegrej without the trip through eeg_point2lat and eeg_lat2point. Time ranges in seconds, epoched data, eeg_point2lat.m and eeg_lat2point.m are not changed. A matching pull request updates EEGPrep at the same time.
Script to reproduce and 17 digit trace (MATLAB R2025b, develop 579a27d)
EEG = pop_importdata('data', 0:9, 'srate', 300, 'dataformat', 'array');
t = [eeg_point2lat(3, 1, EEG.srate, [EEG.xmin EEG.xmax]), ...
     eeg_point2lat(9, 1, EEG.srate, [EEG.xmin EEG.xmax])];
notime = [0, t(1) - 1/EEG.srate; t(2) + 1/EEG.srate, EEG.xmax];
raw_pts = (notime - EEG.xmin) * EEG.srate + 1;
[pts, flag] = eeg_lat2point(notime(:)', ones(1, 4), EEG.srate, [EEG.xmin EEG.xmax]);
OUT = pop_select(EEG, 'point', [3 9]);
fprintf('EEG.xmax     = %.17g\n', EEG.xmax);
fprintf('t            = [%.17g %.17g]\n', t(1), t(2));
fprintf('notime row 2 = [%.17g %.17g]\n', notime(2,1), notime(2,2));
fprintf('raw_pts row2 = [%.17g %.17g], flag = %d\n', raw_pts(2,1), raw_pts(2,2), flag);
fprintf('kept values  = %s\n', mat2str(OUT.data));
EEG.xmax     = 0.029999999999999999
t            = [0.0066666670000000003 0.026666667000000002]
notime row 2 = [0.030000000333333336 0.029999999999999999]
raw_pts row2 = [10.000000100000001 10], flag = 1
kept values  = [2 3 4 5 6 7 8 9]
Why the fix belongs in pop_select.m on continuous point ranges

eeg_eegrej already takes 1 based sample regions. Converting integer points to seconds rounded to 1e-9 s in eeg_point2lat and then back to points in eeg_lat2point introduces up to 0.5e-9 * srate samples of error (1e-7 sample at 300 Hz, 1.5e-5 sample at 30000 Hz), which is much larger than floating point eps and triggers the out of range check at EEG.xmax. Building the rejection regions directly in points when the user passed 'point', 'nopoint' or 'rmpoint' on continuous data avoids that conversion error completely, leaves eeg_point2lat.m and eeg_lat2point.m untouched for all other callers, and leaves 'time', 'notime', 'rmtime' and epoched data on their existing paths.

Adjacent keep ranges that touch ('point', [1 3; 4 8]), tracked in sccn/eegprep issue 864 (pop_select: two keep ranges that touch lose the two samples where they meet), keep their current result (0, 1, 4, 5, 6, 7 on 0:9) and are left for their own separate change.

— orchestrator

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions