Repository navigation
Description
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 (
0to9),pop_select(EEG, 'point', [3 9])returns samples 3 to 10 (8 samples, values2to9) instead of samples 3 to 9 (7 samples, values2to8). On 10 samples at 1024 Hz,pop_select(EEG, 'nopoint', [10 10])also keeps sample 10 instead of removing it. - Cause.
pop_select.mconverts point ranges to seconds witheeg_point2lat, which rounds times to1e-9s (round(newlat*1E9)/1E9), shifts the rejection edges by1/EEG.srate, and converts them back to points witheeg_lat2point. At 300 Hz, sample 9 (8/300s) rounds up to0.026666667s, so adding1/300s gives0.030000000333333336s, which maps to point10.0000001, about1e-7sample pastEEG.xmax.eeg_lat2pointflags that excess (flag = 1), andpop_select(lines 592 to 594) drops the[10 10]rejection region. - How often. Across 9,243 short point ranges (every
[a b]witha < bon 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 atn - 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) inpop_select.m, and build the rejection regions directly in points foreeg_eegrejwithout the trip througheeg_point2latandeeg_lat2point. Time ranges in seconds, epoched data,eeg_point2lat.mandeeg_lat2point.mare 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