Skip to content

Replace remaining np.linspace call sites with miscfunc.linspace where MATLAB parity matters #568

Closed

Description

@suraj-ranganath

Follow-up from Stream 4 (#549, PR #560).

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, newtimef full_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.
  • No tolerance is loosened.

Activity

  1. claude commented on Nov 7, 2026

    @claude
    Contributor

    🤖 Plan:

    • 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 dftfilt2 winval (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.

    Relevant code

    Proposed Fix

    Triage of every call site:

    A. Replace. These feed sample selection, rounding, or output compared with EEGLAB.

    • times rebuilds: 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:60
    • 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:387 full_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.

    • 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):

    1. 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.
    2. 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.
    3. 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.
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