Skip to content

eeg_lat2point: round-off tolerance, so pop_select no longer keeps the last sample at 250, 500 and 1000 Hz - #1002

Merged
arnodelorme merged 2 commits into
developfrom
agent/issue-1001
Nov 29, 2026
Merged

arnodelorme merged 2 commits into
developfrom
agent/issue-1001

Conversation

@arnodelorme

@arnodelorme arnodelorme commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1001. pop_select kept the last sample of a recording when the requested point range ended one sample before it, at 250, 500 and 1000 Hz. The cause is in eeg_lat2point.m, which flagged the last sample as out of range because of floating point round-off.

Example (10 samples at 250 Hz, MATLAB R2025b)

pop_select(EEG, 'point', [3 9]) Samples returned Limit of the last region in eeg_lat2point flag
develop (f6a4100) 3 to 10, one too many 10.000000000000002 for a maximum of 10 1
this pull request 3 to 9, as requested 10.000000000000002, accepted as the last sample 0
256 Hz, before and after 3 to 9 10 0

Comparison with EEGPrep (the Python port, head of sccn/eegprep pull request 856). The same calls in MATLAB and in Python: every point range [a b] with a < b on 10 and on 37 samples at 100, 128, 250, 256, 300, 500, 512, 600, 1000, 1024, 2048, 4900 and 30000 Hz.

9,243 point ranges develop this pull request
Ranges where EEGLAB and EEGPrep differ 129 0
Ranges where EEGLAB does not return the requested samples 285 156
Ranges changed by this pull request 129, all at 250, 500 and 1000 Hz, all "the last sample is no longer kept"

Histogram of the number of samples kept by EEGLAB minus the number kept by EEGPrep, over the 9,243 ranges:

Difference in kept samples develop this pull request
0 9,114 9,243
+1 129 0
  • The 156 ranges left keep the last sample for another reason, at 300, 1024, 2048 and 30000 Hz: eeg_point2lat rounds times to 1e-9 s, so the limit lands about 1e-7 sample after the end, which is really out of range. EEGPrep does the same in all 156. This belongs to pop_select.m and is not changed here (see the issue).
  • Longer recordings. 280 calls on 30,504 to 122,882 samples at 128, 250, 300, 500, 1000, 2048 and 4096 Hz: this pull request changes 9 of them, the ranges that end at sample 99,999 of 100,000 at 250, 500 and 1000 Hz, which now return the requested samples. EEGPrep still keeps the last sample in those 9, because its tolerance is a fixed 1e-12 point, smaller than one eps above 8,192 samples. With the tolerance of this pull request in EEGPrep (an experiment, not committed) the 280 calls and the 9,243 ranges are all equal.
  • Tests. The 29 tests of sccn/eeglab_tests on pop_select, eeg_lat2point, eeg_point2lat, pop_epoch, eeg_eegrej, eegrej, pop_comperp, pop_selectevent, pop_autorej, eeg_interp and newtimef pass before and after. Two new tests fail on develop and pass here: sccn/eeglab_tests pull request 17.
The change and the choice of the tolerance

newlat is (lat*timeunit - timewin(1))*srate + 1 + (epoch-1)*pnts, and the maximum is max(epoch)*pnts with pnts = (timewin(2)-timewin(1))*srate + 1. Each operation rounds, so a latency that is the last sample can come out a few eps above the maximum.

  • Tolerance: min(16*eps(max([maxpnt abs(timewin)*srate])), 0.5), that is 16 eps of the largest number in the computation (the maximum point, or the time limits in points when the offset xmin is larger than the duration). On 10 samples it is 2.8e-14 point, on one hour at 1000 Hz 7.5e-9 point.
  • Never half a sample. The bound of 0.5 was added after the review (second commit): with time limits so large that one eps exceeds a sample, or that overflow, a point that rounds to a sample after the last one is still out of range. It changes nothing for real data, where the tolerance is below 1e-6 point.
  • Why 16. Over 368,000 combinations of 23 sampling rates (0.5 Hz to 44.1 kHz, also 1000/3 and 512.03), 2 to 10 million samples, xmin from -10 to 1000 s and 1 to 500 epochs, the last sample came out at most 3 eps above the maximum through the path of pop_select and at most 1 eps through the other ones. 16 leaves a factor of 5.
  • What it accepts. A point above the maximum by at most the tolerance is set to the maximum, as before, without flag and without the message. A point above it by more is replaced, flagged and reported as before (or an error with 'outrange', 0). A point one millionth of a sample after the end is still flagged at every length tested.
  • The returned points do not change. With the default 'outrange', 1, newlat is bit for bit what it was: every point above the maximum was set to the maximum before, and still is. Only flag and the message change, and only for round-off. Checked on 8,400 random calls (old file against new file): newlat identical in all, flag identical in the 4,200 calls with a point really out of range, and in the 4,200 calls with points in range only the old code flagged 152 and the new code none.
  • With 'outrange', 0 a last sample one eps above the maximum raised an error before, and is now returned as the maximum. No caller in EEGLAB or its plugins uses this option.
Callers of eeg_lat2point and why they do not change
Caller Uses Effect
pop_select.m line 588 points and flag The only caller that reads flag. The last region is no longer dropped when its limit is the last sample: this is the fix. A limit one full sample after the end (a range that ends at the last sample but is not exactly EEG.xmax) is still flagged and the empty region still dropped.
pop_editeventvals.m lines 332 and 627 newlat Same values. The message no longer prints for round-off.
pop_comperp.m line 363 round(newlat) Same values.
timefreq.m line 657, pac_cont.m line 413 round(newlat) Same values.
plugin neuroscanio, pop_loaddat.m line 113 newlat Same values.
plugins mffmatlabio and Fieldtrip-lite, private copies of pop_editeventvals.m newlat Same values.
Multi range and one sample calls, and how the numbers were made

135 calls (27 calls with two keep ranges, one sample nopoint, rmpoint, notime and rmtime regions and time ranges, at 100, 250, 500, 1000 and 2048 Hz): 30 differ from EEGPrep on develop and 15 with this pull request. The 15 that the fix removes are last sample cases at 250, 500 and 1000 Hz. The 15 left are keep ranges given out of order (EEGLAB returns an empty dataset) and keep ranges that overlap (EEGLAB removes requested samples), which EEGPrep sorts and merges first. They were known and are not touched here.

MATLAB R2025b on macOS (Apple silicon), EEGLAB develop f6a4100 without and with this commit, pop_importdata on one channel holding 0, 1, 2, ... then pop_select(EEG, 'point', [a b]), the kept values written to a text file. The table of develop is identical to the one made the night before on EEGLAB 4207693 (9,403 of 9,403 calls). Python: EEGPrep at commit 8513716e. "Differs" means the kept samples are not the same list.

— orchestrator

🤖 Generated with Claude Code

The last sample came out one eps above the maximum at 250, 500 and 1000 Hz, was flagged as out of range, and pop_select then kept it although the point range ended one sample before it. Points within 16 eps of the maximum are now set to it without the flag. Fixes #1001.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@claude

claude Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

The round-off tolerance never reaches half a sample, so a point that rounds to a sample after the last one is always out of range, also with huge or non-finite time limits (review of pull request 1002).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@arnodelorme

Copy link
Copy Markdown
Collaborator Author

Code review

Independent review by Codex (OpenAI GPT-6.1 Terra), asked by the orchestrator, which wrote the change with Claude Fable. Gemini 4 Carbon through agy was asked first and had returned nothing after nine minutes, so Codex did the review. It read the first commit (8c5ebbb) in a read-only sandbox, without running MATLAB, and found no blocking defect and two important ones, both fixed in the second commit (8cec8fe). Its text follows at the end, unchanged except for the link to the file, which pointed to a folder of this Mac.

What was done with the findings

  • Important, the tolerance can hide a whole sample with huge time limits: fixed. With time limits around 1e12 s, one eps is larger than a sample and the tolerance reached 2 points. It is now min(16*eps(...), 0.5), so a point that rounds to a sample after the last one is always out of range. Codex's example (timewin = [1e12 1e12+1], 1000 Hz, latency 1e12+1.001) is now replaced and flagged, and raises an error with 'outrange', 0. For real data nothing changes: the tolerance is below 1e-6 point.
  • Important, the tolerance can become NaN when timewin*srate overflows: fixed by the same bound, because min(NaN, 0.5) is 0.5 in MATLAB and in Octave. Checked with timewin = [1e306 1e306+1e291]: the point after the end is flagged.
  • Nit, with 'outrange', 0 a last sample one eps above the maximum is now returned and no longer an error: left. It is the purpose of the change, it is said in the description, and no caller in EEGLAB or its plugins uses this option.
  • Nit, matrix inputs: left. On develop a matrix of latencies stopped with an error in the && test, so no working behaviour changes. With this change it returns the points.
  • Test gaps: mostly covered in sccn/eeglab_tests pull request 17, which Codex could not see: pop_select at 100, 250, 256, 500 and 1000 Hz, the last sample in eeg_lat2point at 250, 500 and 1000 Hz, a point really out of range alone and next to a last sample, 'outrange', 0, and the large time limits of the first finding. Column, empty, NaN, Inf and integer inputs were compared by hand between develop and this branch and give the same result, and the existing tests with cell inputs pass on both. In single precision the last sample is no longer flagged either, which is the same fix. These are not in the test files.

After the second commit the 9,243 point ranges, the 280 calls on long recordings and the 135 multi range calls give exactly the tables of the description (the result files are identical to those of the first commit, and the 31 tests pass), and the 8,400 random calls still return the same points as develop.

Codex's review

Blocking

None.

Important

The tolerance can hide a whole sample. At eeg_lat2point.m:114, timewin = [1e12, 1e12+1] and srate = 1000 give tol = 2 points. A latency of 1e12+1+0.001 produces point 1001.9765625, beyond the maximum of 1001, but is silently clipped. This also bypasses 'outrange', 0. Large epoch numbers or long recordings can have the same issue through maxpnt. Bound the tolerance below a meaningful fraction of a sample and handle cases where floating point precision cannot distinguish samples.

The tolerance can become NaN. At line 114, abs(timewin)*srate can overflow even when pnts and newlat are finite. Then eps(Inf) is NaN, so line 115 is false and an out of range point is silently clipped, including with 'outrange', 0. Handle nonfinite scaling explicitly.

Nits

The function is a reasonable place to correct conversion roundoff because the issue affects its range check. For ordinary finite vector inputs with the default option, newlat stays the same: develop already clipped excess points. The flag and message change. With 'outrange', 0, a near-boundary excess now returns a clipped value instead of an error.

The callers found are pop_select, pop_comperp, pop_editeventvals, timefreq, and pac_cont. Only pop_select reads the flag. The others can lose a range message and may see changed results for unusual inputs.

Test gaps

Add tests for the reported pop_select regression at 250, 500, and 1000 Hz, including a truly out of range point and 'outrange', 0. Test mixed near and far excesses, large time origins and epoch numbers, long recordings, noninteger rates, and timeunit = 1e-3.

Test empty, row, and column inputs, matrices, cells, NaN, Inf, and overflow. Matrix behavior can change because line 109 now takes the maximum over all epochs rather than per column. Cell conversion and empty handling occur before the changed block. No new concatenation or epoch_array(:) error is apparent for ordinary numeric inputs, but unusual input classes remain unverified. No MATLAB execution was performed.

Merge recommendation: Needs changes.

— orchestrator

@arnodelorme
arnodelorme merged commit 579a27d into develop Nov 29, 2026
2 checks passed
arnodelorme added a commit to sccn/eegprep that referenced this pull request Nov 29, 2026
Use 16*eps(max(max_valid, max(abs(timewin_sec))*srate)) capped at 0.5, clamp points within the tolerance to max_valid without flagging, and match MATLAB max/min NaN handling, as merged in sccn/eeglab#1002. Fixes #867.
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.

pop_select keeps the last sample at 250, 500 and 1000 Hz (eeg_lat2point flags the last sample as out of range)

1 participant