Repository navigation
Match MATLAB spacing on time, frequency, and cycle grids - #602
Conversation
EEG.times, import and epoch time vectors, and returned frequency and cycle grids now use the MATLAB linspace helper, or the upstream colon or logspace when that is the EEGLAB expression. Display, tick, and exact-step grids are unchanged. Refs #568 Co-authored-by: Suraj Ranganath <[email protected]>
There was a problem hiding this comment.
If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.
Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.
There was a problem hiding this comment.
If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.
Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.
Gauss follows -sds:incr:sds. Histogram centers, FDR thresholds, trend regressors, the dftfilt phase and wavelet window, color-bar ticks and labels, DIPFIT scan grids, and the topoplot mesh use MATLAB linspace or colon spacing. Co-authored-by: Suraj Ranganath <[email protected]>
|
Claude finished @suraj-ranganath's task in 2m 4s —— View job Code review
Blocking
Important
Nits
Test gaps
EEGLAB parity notes
I posted inline comments on |
| minimum -= span | ||
| maximum += span | ||
| return np.linspace(minimum, maximum, count) | ||
| return linspace(minimum, maximum, count) |
There was a problem hiding this comment.
Important: regression on constant data. The PR deleted the minimum == maximum branch. Constant input now gives count identical centers, for example a flat channel, or hist2([1, 1], [1, 1]) with the default 100 bins. _bar_widths then returns all-zero widths, so the bars are invisible, and set_xlim(c, c) triggers matplotlib's identical-limits warning. Explicit centers are still required to be strictly increasing (line 59), so auto-generated centers now break that invariant.
Fix: keep the degenerate-range handling and use the MATLAB linspace only on the normal path:
| return linspace(minimum, maximum, count) | |
| if minimum == maximum: | |
| if count == 1: | |
| return np.asarray([minimum]) | |
| span = max(abs(minimum), 1.0) * 0.5 | |
| minimum -= span | |
| maximum += span | |
| return linspace(minimum, maximum, count) |
|
|
||
| import numpy as np | ||
|
|
||
| from eegprep.plugins.clean_rawdata.private.windows import matlab_colon |
There was a problem hiding this comment.
Important: layering. This line and the matching ones in cbar.py, envtopo.py, erpimage.py and newtimef.py are the first imports from plugins/clean_rawdata/private anywhere under functions/. A general MATLAB colon helper doesn't belong in a clean_rawdata private module. Right now these imports avoid a cycle only because plugins/clean_rawdata/__init__.py loads lazily.
Fix: move matlab_colon into functions/miscfunc/, next to linspace.py, and update every caller, clean_windows included. A separate commit is fine.
| count = winlen + 1 | ||
| start = float(np.floor(winlen / 2)) | ||
| stop = float(np.floor(-winlen / 2)) | ||
| offsets = np.empty(count, dtype=float) | ||
| if count == 1: | ||
| offsets[0] = stop | ||
| else: | ||
| offsets[:-1] = start + np.arange(count - 1) * (stop - start) / (count - 1) | ||
| offsets[-1] = stop | ||
| window = (minx + 1) - offsets |
There was a problem hiding this comment.
Important: duplicate helper. These lines rewrite linspace by hand, including the count == 1 → stop case. AGENTS.md lists "Parallel implementations of existing helpers" as a failure pattern.
Fix: import linspace from eegprep.functions.miscfunc.linspace at the top of the module and replace these lines with:
window = (minx + 1) - linspace(np.floor(winlen / 2), np.floor(-winlen / 2), winlen + 1)| dec = int(np.floor(np.log10(scale))) if scale else 0 | ||
| if scale == 0: | ||
| return ticks, labels |
There was a problem hiding this comment.
Nit: guards that can't run. The only caller, _draw_colorbar (line 468), calls this function only when vmax > vmin. So scale = max(|vmin|, |vmax|) is never 0, and AGENTS.md says not to add checks for impossible states.
| dec = int(np.floor(np.log10(scale))) if scale else 0 | |
| if scale == 0: | |
| return ticks, labels | |
| dec = int(np.floor(np.log10(scale))) |
A zero data span is widened before MATLAB linspace, so auto bin centers stay strictly increasing. matlab_colon now lives next to linspace. The ERP-image engine keeps its own linspace because that module cannot import the package. Co-authored-by: Suraj Ranganath <[email protected]>
Co-authored-by: Suraj Ranganath <[email protected]>
|
Claude finished @suraj-ranganath's task in 1m 30s —— View job Code review
Blocking
Important
Nits
Test gaps
EEGLAB parity notes
I could not run tests or MATLAB in this job. |
| @@ -0,0 +1,376 @@ | |||
| """Sun fdlibm ``exp`` and ``pow``. | |||
There was a problem hiding this comment.
Important: this module's premise is unverified, and the PR applies it inconsistently.
The module adds 376 lines of a bit-level Sun fdlibm exp/pow port. It assumes MATLAB's elementwise exp and .^ on the CI platforms return fdlibm results. No MATLAB run backs that up (the PR body says "MATLAB was not run here"). The expected hex values in tests/test_linspace.py for gauss and the log-spaced widths come from this same code, so the tests can't show that it matches MATLAB.
The PR also only uses it in two places. The other MATLAB exp(linspace(...)) grids this PR touches still call np.exp:
fdr.py:57timefreq.py:231(logfreqscale)dftfilt2.py:63,dftfilt3.py:118(log cycles)erpimage_engine._gauss(erpimage_engine.py:2230), the private copy ofgaussthaterpimage_engine.py's docstring says tests pin tomiscfunc.gauss
So if the premise holds, those grids still differ from MATLAB. If it doesn't hold, gauss and tf_cycle_calc were moved away from correct results.
Fix: split the fdlibm change into its own PR. Get it accepted only after the EEGLAB reference workflow shows that gauss/tf_cycle_calc match MATLAB bit-for-bit with this module and don't match with np.exp/np.power. Then apply it to every exp(linspace) site, or document why some sites are left out. Until then, keep np.exp and 10.0 ** linspace(...) here and in gauss.py.
There was a problem hiding this comment.
MATLAB R2025b Update 7 on jamming matches these two sites bit for bit: gauss(7, 0.3)(5) is 0x1.fae7cfd2b9cfep-1 and logspace(log10(3), log10(8), 7)(6) is 0x1.b2c91e6a9e540p+2 (num2hex 401b2c91e6a9e540). The previous numpy.exp / 10.0 ** linspace path differed in the last bit (0x1.fae7cfd2b9cfdp-1 and 0x1.b2c91e6a9e53fp+2). I am leaving ieee754.py in this PR on that evidence. Applying the same exp/pow to the remaining grids, including erpimage_engine._gauss, can be a follow-up; the current pin is allclose rtol=1e-13 and still passes.
|
Jamming reran the previous pr602 suite at 2418d44 (MATLAB R2025b Update 7, matlabengine 25.2.2). All tests passed: tests/test_linspace.py (16), the five timefreq tests, test_reference_gauss, test_reference_envtopo_original_sample, and topoplot/erpimage/fdr (365). MATLAB num2hex and Python agreed bit for bit on every requested value, including:
The 03:04 PT review asked to drop ieee754.py because those hex values had no MATLAB evidence. This run supplies that evidence. Removing the fdlibm exp/pow port would restore the previous mismatch (numpy logspace was 0x1.b2c91e6a9e53fp+2 and numpy gauss was 0x1.fae7cfd2b9cfdp-1). A large timefreq call (1x2048, 40 frequencies, winsize 256) was 1.01x the previous numpy.exp path (median 78.6 ms vs 78.0 ms). Isolated gauss on 1e6 samples is much slower because fdlibm exp walks scalars in Python (about 1.94 s vs 6.4 ms). That cost does not show up in ordinary timefreq window sizes. Other exp(linspace) sites that still use np.exp, including erpimage_engine._gauss, can be a follow-up. The existing pin between _gauss and miscfunc.gauss is allclose rtol=1e-13, and that test still passes. The topoplot head-circle point count is left as it is. The dftfilt3 import spacing is a nit. |
|
CI is red on Python 3.12 macos and windows (ubuntu still running). I am holding the merge. tests/test_fuzz_events.py::test_original_draws_match_the_golden_snapshot and test_original_draws_do_not_depend_on_import_order fail with AssertionError: pop_reref. The Hypothesis draws for pop_reref no longer match tests/fuzz/golden_draws_20261012.json. Jamming MATLAB R2025b Update 7 at 2418d44 was clean and bit-for-bit on the linspace/logspace/gauss/envtopo/timefreq checks, and the requested test suite passed. I am not re-recording the golden draws. This mismatch needs a look before merge. |
Co-authored-by: Suraj Ranganath <[email protected]>
Co-authored-by: Suraj Ranganath <[email protected]>
Bring in the fuzz-draw independence fix so the golden tests can pass without changing the recorded draws. Co-authored-by: Suraj Ranganath <[email protected]>
Apple libm rounds this complex exponential one ULP above MATLAB. Sun fdlibm sin and cos keep the phase and its Hanning taper on the recorded bit on every platform. Co-authored-by: Suraj Ranganath <[email protected]>
Bring in the headless spectrum summaries and the pAMICA thread-count fix. Co-authored-by: Suraj Ranganath <[email protected]>
|
Claude finished @suraj-ranganath's task in 1m 9s —— View job Code review
Blocking
Important
Nits
Test gaps
EEGLAB parity notes
I didn't run tests or MATLAB in this job. The vendored |
| def _symmetric_hanning(n: int) -> np.ndarray: | ||
| if n % 2 == 0: | ||
| half = 0.5 * (1.0 - cos(2.0 * np.pi * np.arange(1, n // 2 + 1) / (n + 1))) | ||
| return np.concatenate([half, half[::-1]]) | ||
| half = 0.5 * (1.0 - cos(2.0 * np.pi * np.arange(1, (n + 1) // 2 + 1) / (n + 1))) | ||
| return np.concatenate([half, half[-2::-1]]) |
There was a problem hiding this comment.
Important: this copies symmetric_hanning, and the two copies now give different results. This is dftfilt3.symmetric_hanning (dftfilt3.py#L96-L105) with np.cos replaced by fdlibm cos. Both stand for the same MATLAB hanning, but they can now differ in the last bit. timefreq (timefreq.py:362), dftfilt2 and dftfilt3 still use the np.cos copy. AGENTS.md lists "Parallel implementations of existing helpers" as a failure pattern.
Fix: keep one helper. Switch dftfilt3.symmetric_hanning to ieee754.cos and import it here. That also moves the timefreq/dftfilt2/dftfilt3 tapers onto fdlibm, so those three need the same MATLAB check as dftfilt. If that check can't happen in this PR, keep np.cos in dftfilt for now and change all four together in the fdlibm follow-up.
Argument reduction and the polynomial kernels run on arrays. Values past the medium cutoff stay on the scalar port, and dftfilt evaluates one phase grid per call. Co-authored-by: Suraj Ranganath <[email protected]>
Time, frequency, and cycle grids now follow the EEGLAB linspace, colon, or logspace expression, including EEG.times. Gauss uses -sds:incr:sds and fdlibm exp. dftfilt evaluates its phase and Hanning taper with fdlibm sin and cos. Log-spaced widths use fdlibm 10.^x. Histogram centers, FDR thresholds, trend regressors, the dftfilt phase and wavelet window, color-bar ticks and labels, DIPFIT scan grids, and the topoplot mesh do too. A constant hist2 widens a zero span before that spacing, so the bars have a width. The plotted head outline keeps 100 points, spaced with MATLAB linspace; EEGLAB's circle uses 201. Colormap samples, eegplot pixel edges, ICLabel scroll ticks, log-image ticks, signalstat histogram centers, the blank scalp grid, and contour levels stay, because EEGLAB does not build them with linspace or a colon. MATLAB was not run here.
Fixes #568