You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Replace remaining np.linspace call sites with miscfunc.linspace where MATLAB parity matters #568
Problem.#560 added miscfunc.linspace to reproduce MATLAB's linspace bit for bit, because np.linspace rounds differently in the last bit (ULP-level ties) and that shifted which sample was picked. Several call sites still use np.linspace, including the timefreq frequency grids, newtimeffull_times, dftfilt2/dftfilt3, the times computed in eeg_checkset, and rejtrend.
Done when
Every np.linspace call in the library has been reviewed.
Each one that feeds sample selection, rounding, or output that's compared with EEGLAB uses miscfunc.linspace.
Each change is backed by a test or a MATLAB parity check showing it matters, or the call is left as is with a reason.
Inventory every np.linspace call in src/eegprep (excluding vendored eeglab/): 63 calls in 38 files
Measure how often np.linspace differs from miscfunc.linspace
Decide scope. This touches many modules and each change needs a test or MATLAB check, so it is too large for one small PR. Analysis is below for review. No PR has been opened.
A human approves the split, then PRs land one batch at a time
Research
miscfunc.linspace computes start + (i*delta)/(n-1), the same as MATLAB. np.linspace computes start + i*(delta/(n-1)), so the two can differ in the last bit. Measured locally: dataset times grids (xmin*1000 → xmax*1000, random srate/xmin/pnts) differ in 347 of 3000 cases, and float frequency grids (linspace(lo, hi, n)) differ in 2817 of 3000. Grids with exact steps never differ. Examples are dftfilt2winval (step −1), erpimage_engine window, eegplot int edges, and fft_freqs at power-of-two sizes. Duplicate check: no open PR touches this. #553 (Stream 8) touches only DIPFIT and ICLabel files, which are left as is below.
Frequency and cycle grids: timefreq.py:207,219,233,234, dftfilt2.py:62-63, dftfilt3.py:116-117, tf_cycle_calc.py:102
newtimef.py:387full_times (argmin nearest sample)
B. Keep only if a MATLAB check shows a threshold or bin flip. Otherwise leave as is: the difference is one ULP inside a regression or kernel, and no sample is selected from it.
rejtrend.py:30 and _rejection.py:209 (slope regressor), dftfilt.py:19, gauss.py:17, hist2.py:69 (bin edges, where ties could matter), fdr.py:55, _pac_support.py:519
C. Leave as is. These are display, colormap, GUI, or exact-step uses that EEGLAB never compares.
Proposed split (each batch changes core functions, so each needs the EEGLAB MATLAB reference run):
times rebuilds. Swap to miscfunc.linspace. Add one regression test with an (xmin, srate, pnts) triple where the grids differ, asserting EEG['times'] equals MATLAB's linspace values. Pin those values from the MATLAB backend in the same way as test_linspace_matches_matlab_values.
timefreq/newtimef/dftfilt2/dftfilt3/tf_cycle_calc. Swap, then extend the existing test_f91_f126_time_frequency_matches_eeglab to compare freqs bit for bit on a non-power-of-two log grid.
Group B. Run one MATLAB parity sweep. Swap only the sites where an output flips, and record the reason for keeping each of the others in the PR.
Open questions
Should all the times rebuilds call one shared helper (e.g. eeg_checkset's logic) instead of 17 copies of linspace(xmin*1000, xmax*1000, pnts)? That is a larger refactor. My preference is to do the plain swap now and track the refactor separately.
Is bitwise equality of freqs with MATLAB the acceptance bar? The current parity tests use 1e-4, so ULP drift would not fail them.
Activity
claude commentedon Nov 7, 2026
🤖 Plan:
np.linspacecall insrc/eegprep(excluding vendoredeeglab/): 63 calls in 38 filesnp.linspacediffers frommiscfunc.linspaceResearch
miscfunc.linspacecomputesstart + (i*delta)/(n-1), the same as MATLAB.np.linspacecomputesstart + i*(delta/(n-1)), so the two can differ in the last bit. Measured locally: datasettimesgrids (xmin*1000 → xmax*1000, random srate/xmin/pnts) differ in 347 of 3000 cases, and float frequency grids (linspace(lo, hi, n)) differ in 2817 of 3000. Grids with exact steps never differ. Examples aredftfilt2winval(step −1),erpimage_enginewindow,eegplotint edges, andfft_freqsat power-of-two sizes. Duplicate check: no open PR touches this. #553 (Stream 8) touches only DIPFIT and ICLabel files, which are left as is below.Relevant code
src/eegprep/functions/miscfunc/linspace.py- MATLAB-compatible helper (Stream 4: Signal processing #560)src/eegprep/functions/adminfunc/eeg_checkset.py#L600-EEG['times']rebuild that most code relies onsrc/eegprep/functions/timefreqfunc/timefreq.py#L207-L234- frequency grids returned asfreqssrc/eegprep/functions/timefreqfunc/newtimef.py#L387-full_timesused for argmin sample choiceProposed Fix
Triage of every call site:
A. Replace. These feed sample selection, rounding, or output compared with EEGLAB.
timesrebuilds:eeg_checkset.py:600,pop_epoch.py:249,pop_resample.py:210,eeg_eegrej.py:334,clean_windows.py:220,pop_editset.py:443,_file_io.py:185,pop_fileio_brainvision_mat.py:150,plot_utils.py:78,eeg_amplitudearea.py:35,pop_erpimage.py:176,pop_spectopo.py:255,pop_export.py:67,envtopo.py:274,_pac_support.py:321,495,vectdata.py:60timefreq.py:207,219,233,234,dftfilt2.py:62-63,dftfilt3.py:116-117,tf_cycle_calc.py:102newtimef.py:387full_times(argmin nearest sample)B. Keep only if a MATLAB check shows a threshold or bin flip. Otherwise leave as is: the difference is one ULP inside a regression or kernel, and no sample is selected from it.
rejtrend.py:30and_rejection.py:209(slope regressor),dftfilt.py:19,gauss.py:17,hist2.py:69(bin edges, where ties could matter),fdr.py:55,_pac_support.py:519C. Leave as is. These are display, colormap, GUI, or exact-step uses that EEGLAB never compares.
cbar.py,signalstat.py,topoplot.py(all),savefig.py,eegmovie.py,eegplot.py:366,erpimage.py:476,_log_image.py,gradmap.py,plotfresp.py,_prop_browser.py,erpimage_engine.py:1816,dftfilt2.py:33,dipfit/*(FieldTrip grids, not bit-compared)Proposed split (each batch changes core functions, so each needs the EEGLAB MATLAB reference run):
timesrebuilds. Swap tomiscfunc.linspace. Add one regression test with an(xmin, srate, pnts)triple where the grids differ, assertingEEG['times']equals MATLAB'slinspacevalues. Pin those values from the MATLAB backend in the same way astest_linspace_matches_matlab_values.timefreq/newtimef/dftfilt2/dftfilt3/tf_cycle_calc. Swap, then extend the existingtest_f91_f126_time_frequency_matches_eeglabto comparefreqsbit for bit on a non-power-of-two log grid.Open questions
timesrebuilds call one shared helper (e.g.eeg_checkset's logic) instead of 17 copies oflinspace(xmin*1000, xmax*1000, pnts)? That is a larger refactor. My preference is to do the plain swap now and track the refactor separately.freqswith MATLAB the acceptance bar? The current parity tests use1e-4, so ULP drift would not fail them.Match MATLAB spacing on time, frequency, and cycle grids (#602)