Repository navigation
eeg_lat2point: round-off tolerance, so pop_select no longer keeps the last sample at 250, 500 and 1000 Hz - #1002
Conversation
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 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]>
Code reviewIndependent 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
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 reviewBlockingNone. ImportantThe tolerance can hide a whole sample. At eeg_lat2point.m:114, The tolerance can become NaN. At line 114, NitsThe 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, The callers found are Test gapsAdd tests for the reported 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 Merge recommendation: Needs changes. — orchestrator |
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.
Fixes #1001.
pop_selectkept 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 ineeg_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])eeg_lat2pointflag10.000000000000002for a maximum of1010.000000000000002, accepted as the last sample10Comparison 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]witha < bon 10 and on 37 samples at 100, 128, 250, 256, 300, 500, 512, 600, 1000, 1024, 2048, 4900 and 30000 Hz.Histogram of the number of samples kept by EEGLAB minus the number kept by EEGPrep, over the 9,243 ranges:
eeg_point2latrounds 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 topop_select.mand is not changed here (see the issue).epsabove 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.pop_select,eeg_lat2point,eeg_point2lat,pop_epoch,eeg_eegrej,eegrej,pop_comperp,pop_selectevent,pop_autorej,eeg_interpandnewtimefpass 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
newlatis(lat*timeunit - timewin(1))*srate + 1 + (epoch-1)*pnts, and the maximum ismax(epoch)*pntswithpnts = (timewin(2)-timewin(1))*srate + 1. Each operation rounds, so a latency that is the last sample can come out a fewepsabove the maximum.min(16*eps(max([maxpnt abs(timewin)*srate])), 0.5), that is 16epsof the largest number in the computation (the maximum point, or the time limits in points when the offsetxminis larger than the duration). On 10 samples it is 2.8e-14 point, on one hour at 1000 Hz 7.5e-9 point.epsexceeds 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.xminfrom -10 to 1000 s and 1 to 500 epochs, the last sample came out at most 3epsabove the maximum through the path ofpop_selectand at most 1epsthrough the other ones. 16 leaves a factor of 5.flagand 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.'outrange', 1,newlatis bit for bit what it was: every point above the maximum was set to the maximum before, and still is. Onlyflagand the message change, and only for round-off. Checked on 8,400 random calls (old file against new file):newlatidentical in all,flagidentical 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.'outrange', 0a last sample oneepsabove 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
pop_select.mline 588pointsandflagflag. 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 exactlyEEG.xmax) is still flagged and the empty region still dropped.pop_editeventvals.mlines 332 and 627newlatpop_comperp.mline 363round(newlat)timefreq.mline 657,pac_cont.mline 413round(newlat)pop_loaddat.mline 113newlatpop_editeventvals.mnewlatMulti range and one sample calls, and how the numbers were made
135 calls (27 calls with two keep ranges, one sample
nopoint,rmpoint,notimeandrmtimeregions 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_importdataon one channel holding 0, 1, 2, ... thenpop_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