Repository navigation
Export headless spectrum summaries - #603
Conversation
Move the Welch estimator out of the plotting module so it can be imported without Matplotlib, and add line-noise excess and normal-scaled MAD helpers. Keep spectopo's numeric output identical. 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.
…trum-summaries-ab6b Co-authored-by: Suraj Ranganath <[email protected]> # Conflicts: # docs/source/changelog.rst
|
Claude finished @suraj-ranganath's task in 1m 21s —— View job Code review
BlockingNone. Important
Nits
Test gapsNone. The new tests cover:
EEGLAB parity notesNone. The body of Other things I checked
I could not run pytest here because the command needed approval that this job doesn't have. CI "Tests" was still running when I wrote this; the docs build passed. |
| # Digests of spectopo's spectra and frequency grids for _reference_signal. | ||
| _FROZEN = { | ||
| "default": ( | ||
| "43acecdc43b1b1c89306565ad8f55a9f91405e511460ced27cd7d64fa1f12d3b", | ||
| "a2c297b161f5e61a7e16c45d19feb53ec8f2ae0103d1a97d7c91756ccfccba51", | ||
| (2, 65), | ||
| {}, | ||
| ), | ||
| "percent": ( | ||
| "d14b6db8886cd0e1aa4be4626ef3de5fdcefce24bf856fce7207774a31f51c41", | ||
| "a2c297b161f5e61a7e16c45d19feb53ec8f2ae0103d1a97d7c91756ccfccba51", | ||
| (2, 65), | ||
| {"percent": 50}, | ||
| ), | ||
| "overlap": ( | ||
| "402138c32b745057b68a9e4e434c51536dd9a4694d146084ed3ff5eb30778393", | ||
| "9287e14778ee197dd339ad57e60da907f264a23d5f074d4d284919838f48ded3", | ||
| (2, 33), | ||
| {"overlap": 32, "winsize": 64}, | ||
| ), | ||
| "blackman": ( | ||
| "b311d13d3bf6fe5ab321433c06b58c07a009b9f2c6daf327aa33d709346d1353", | ||
| "9287e14778ee197dd339ad57e60da907f264a23d5f074d4d284919838f48ded3", | ||
| (2, 33), | ||
| {"wintype": "blackmanharris"}, | ||
| ), | ||
| "nfft": ( | ||
| "feb6c9d9049241199b388d5710517beb00098087508f92ebd87c472afd5cc928", | ||
| "fd9a0ed0205cc3c1ac8223db9278715bb7349d63bf093fb76a18d502ac105b3b", | ||
| (2, 129), | ||
| {"nfft": 256, "winsize": 64}, | ||
| ), | ||
| "epochs": ( | ||
| "6014d735b83bf8114f9b44ac4a3abfd9db26c191603908cd7c74d1972018fa64", | ||
| "9287e14778ee197dd339ad57e60da907f264a23d5f074d4d284919838f48ded3", | ||
| (2, 33), | ||
| {}, | ||
| ), | ||
| "concat": ( | ||
| "35185bca7611e8a9a383bb44b75c84a86ba911676b16296cb11aa132d485cb85", | ||
| "9287e14778ee197dd339ad57e60da907f264a23d5f074d4d284919838f48ded3", | ||
| (2, 33), | ||
| {}, | ||
| ), | ||
| } | ||
|
|
||
|
|
||
| def _digest(values: np.ndarray) -> str: | ||
| return hashlib.sha256(np.ascontiguousarray(values).tobytes()).hexdigest() | ||
|
|
||
|
|
||
| def test_spectopo_numeric_output_matches_the_moved_estimator(): | ||
| data, pnts, srate = _reference_signal() | ||
| epochs = data.reshape(2, 64, 4) | ||
| inputs = {"epochs": (epochs, 64), "concat": (data, 64)} | ||
| for name, (spectra_hash, frequency_hash, shape, kwargs) in _FROZEN.items(): | ||
| array, frames = inputs.get(name, (data, pnts)) | ||
| spectra, freqs, first, second, specstd, figure = spectopo(array, frames, srate, plot="off", **kwargs) | ||
| direct, direct_freqs, direct_std = compute_spectra(array, frames, srate, **kwargs) | ||
| assert spectra.shape == shape | ||
| assert _digest(spectra) == spectra_hash | ||
| assert _digest(freqs) == frequency_hash | ||
| assert np.array_equal(spectra, direct) | ||
| assert np.array_equal(freqs, direct_freqs) | ||
| assert spectopo_compute_spectra is compute_spectra | ||
| assert specstd is None and direct_std is None | ||
| assert first is None and second is None and figure is None |
There was a problem hiding this comment.
Important (test brittleness): These SHA-256 digests hash the raw float bytes of spectopo output. Any last-bit change from a NumPy or SciPy upgrade (for example to pocketfft or get_window), or from platform FMA/SIMD differences, will make this test fail even when the spectra are numerically identical. AGENTS.md asks for tests of behavior, and tests/test_spectopo_parity.py already checks the numbers against MATLAB.
The regression this test is meant to catch, spectopo drifting from the moved estimator, is already caught by np.array_equal(spectra, direct) together with spectopo_compute_spectra is compute_spectra, both in this same loop. The current hash values also only show that the move kept results byte-identical in one environment, which the reviewer can check once from the diff.
Suggested fix: remove the _FROZEN digests and _digest, and keep the loop over the kwargs cases with the array_equal and identity assertions. If a value-level freeze is still wanted, store a few rounded spectrum values and compare them with np.testing.assert_allclose(..., rtol=1e-12) instead of hashing bytes.
spectopo and compute_spectra share one estimator, so the cases that were frozen as SHA-256 digests now require exact array equality. Hypothesis integer draws include literals from modules the sigprocfunc package used to import on load. The fuzz harness imports those modules before drawing so the golden cases stay on that set. Co-authored-by: Suraj Ranganath <[email protected]>
|
Claude finished @suraj-ranganath's task in 1m 38s —— View job Code review
BlockingNone. Important
Nits
Test gapsNone beyond the import-order case above. A test for it: after EEGLAB parity notesNone. The body of I could not run |
| def __getattr__(name: str) -> Any: | ||
| try: | ||
| module_name, attr_name = _LAZY_EXPORTS[name] | ||
| except KeyError as exc: | ||
| raise AttributeError(f"module {__name__!r} has no attribute {name!r}") from exc | ||
| value = getattr(import_module(module_name), attr_name) | ||
| globals()[name] = value | ||
| return value |
There was a problem hiding this comment.
Important (regression): the lazy re-exports can return the submodule instead of the function. Each of the 18 names has the same name as its submodule (readbdf/readbdf, headplot/headplot, …). When anything imports the submodule directly, Python's import system runs setattr(package, "readbdf", <module>), and from then on module __getattr__ is never consulted. EEGPrep does that itself: pop_biosig.py:16-17 imports sigprocfunc.openbdf/readbdf, pop_chanedit.py:26 imports forcelocs, pop_writeeeg.py:17 imports writegdf, and pop_headplot.py/eegmovie.py import headplot. The root lazy export eegprep.cbar does it too.
Failure scenario:
import eegprep.functions.popfunc.pop_biosig
from eegprep.functions.sigprocfunc import readbdf
readbdf(...) # TypeError: 'module' object is not callableBefore this PR, from .readbdf import readbdf rebound the package attribute to the function after the submodule loaded, so this always returned the function. test_numerical_imports_do_not_load_matplotlib misses it because it imports cbar in a fresh process before anything else has loaded the submodule.
Suggested fix: nothing in src/ or tests/ uses these package-level function aliases; the public names come from eegprep.<name>. Under AGENTS.md ("No backward compatibility by default"), the simplest fix is to make sigprocfunc/__init__.py contain only its docstring, add a changelog note, and change the subprocess test to import from eegprep.functions.sigprocfunc.cbar import cbar. If the aliases must stay, __getattr__ alone cannot keep them working; the helpers would have to be bound eagerly without importing Matplotlib.
There was a problem hiding this comment.
Agreed. This is a real regression from the eager aliases, so I am holding the merge until __init__.py is reduced to its docstring (plus a changelog note) or the functions are bound in a way that survives a direct submodule import.
…ment. Hypothesis 6.169 has no supported way to disable or pin local-constant collection, and clearing it changes the golden draws. The sigproc modules stay imported before the draw, under a name that says why. Co-authored-by: Suraj Ranganath <[email protected]>
|
Claude finished @suraj-ranganath's task in 40s —— View job Code review
BlockingNone. Important
Nits
Test gapsNone beyond the import-order case above. With the suggested fix that case cannot happen, so it needs no test. EEGLAB parity notesNone. The body of I could not run the Python import check in this job because |
|
The pre-import of sigprocfunc modules in tests/fuzz/harness.py is accepted. Hypothesis 6.169 has no opt-out for local-constant collection, and re-recording the golden draws is Suraj's call. The lazy re-export point is valid, and I am holding the merge on this head. After a direct submodule import such as The tautological package-root export test is a nit. |
Importing a same-named submodule binds the module on the package and hides a lazy __getattr__ export. Lookup returns the exported function either way. Co-authored-by: Suraj Ranganath <[email protected]>
|
Claude finished @suraj-ranganath's task in 1m 7s —— View job Code review
BlockingNone. ImportantNone.
Nits
Test gapsNone. EEGLAB parity notesNone. The body of I couldn't run anything in this job because |
…trum-summaries-ab6b Drop the sigprocfunc pre-import. The draw generator from #640 no longer reads Hypothesis local constants, and the golden draws still match. Co-authored-by: Suraj Ranganath <[email protected]>
Move the Welch estimator out of the plotting module and export it as compute_spectra, without a second implementation. line_noise_excess_db and channel_robust_amplitude report neighboring-bin excess and a normal-scaled MAD amplitude. spectopo's numeric output is unchanged.
MATLAB is not available in this environment, so MATLAB-backed tests, including tests/test_spectopo_parity.py and the EEGLAB reference suite, were not run.
Fixes #536