Repository navigation
Return and reapply the ASR calibration state - #621
Conversation
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.
|
Claude finished @neuromechanist's task in 1m 11s —— View job Code review
BlockingNone. ImportantNone. Nits
Test gapsNone. The tests run real calibration, with no mocks, and cover all three modes, the channel and srate refusals, use of the recorded arguments, and malformed documents. Optional: a round trip with EEGLAB parity notesNone. EEGLAB's NoteI couldn't post the review with |
| if step_size is None: | ||
| step_size = int(math.floor(srate * window_len / 2)) # Samples |
There was a problem hiding this comment.
Nit: clean_asr can save a state that state_from_json refuses. step_size only becomes an int when it defaults. A caller-passed step_size=64.0 is stored as a float, state_to_json writes 64.0, and state_from_json then raises step_size: expected a positive integer. step_size is a sample count, so coerce it after the default:
| if step_size is None: | |
| step_size = int(math.floor(srate * window_len / 2)) # Samples | |
| if step_size is None: | |
| step_size = int(math.floor(srate * window_len / 2)) # Samples | |
| step_size = int(step_size) |
clean_asrcan now return the calibration state it used, and reapplying a saved state to the same EEG givesnumpy.array_equaldata, so a pipeline can store the state beside its record and anyone can reproduce the cleaned data.clean_asr(EEG, ..., return_state=False, state=None): withreturn_state=Trueit returns(EEG, state); with neither argument it returnsEEGexactly as before. Withstate=it skips reference selection and calibration and processes with that state and the processing arguments the state records (cutoff,window_len,step_size,max_dims,useriemannian,maxmem).The state is the calibration as
asr_calibrate(orasr_calibrate_rforuseriemannian=True) returned it, before processing touched it, plussrate,channels(the labels in data order,Nonewhen the EEG has nochanlocs), the processing arguments, andformat: "eegprep.asr-state.v1".New module
plugins/clean_rawdata/asr_state.py:state_to_jsonwrites every array as{"dtype": "float64", "shape": [...], "data": nested lists}with numbers as Python floats (shortest exact text), andstate_from_jsonrefuses anotherformat, a missing or unexpected key, a shape that does not match its data, a non-float64 dtype, a non-finite number, and a non-null processing buffer, each with aValueErrorthat names the key.Reapplying refuses a state whose
srateorchannelsdiffer from the EEG's, a state with another channel count, and a changedcutoff,window_len,step_size,max_dims,ref_*,useriemannianormaxmempassed besidestate=(the state owns them; the card was silent on this, the alternative was to ignore them silently).Tests (
tests/test_asr_state.py, real calibration on 40 s ofsample_data/eeglab_data.set, no mocks): JSON round trip thenarray_equalreproduction for the default path,useriemannian="calib"and full Riemannian ASR; reordered channels and another sampling rate refused; a changed state element changes the data; recorded processing arguments are used; malformed documents refused naming the key; the default call still returns a single EEG. Each test was checked to fail under a one-line mutation of the line it guards. The state of the 32-channel sample is about 48 kB of JSON.Docs: changelog entry and a "Save and Reapply the ASR State" section in the preprocessing pipeline guide; the docs build without gallery execution adds no warning.
Gates run:
uv sync --python 3.12 --all-extras --group dev;ruff check .(all checks passed);ruff format --check .(946 files already formatted);ty check(all checks passed);uv lock --check(resolved 174 packages);./pre-commit.py --changed-from origin/develop(OK);pytest tests/test_asr_state.py tests/test_clean_asr.py(49 passed);tests/test_clean_artifacts.pyandtests/test_package_exports.pypass.Not run, would check: the full
pytest testsand the MATLAB parity tests, because CI on this PR covers them.tests/test_riemannian_asr.py::test_process_r_updates_final_sample_when_length_mod_step_is_onefails on currentdevelop(af91451) in this environment, with and without this change; it callsasr_process_rdirectly and is unrelated.Overlap with open PRs:
docs/source/changelog.rstis also edited by #617, #601, #591, #605, #606 and #386, so the changelog hunk will conflict textually with each (resolve by keeping both entries). #605, #606 and #386 also editdocs/source/user_guide/preprocessing_pipeline.rst. #386 editsclean_asr.py(itsmaxmemdefault and docstring sit next to the newreturn_stateandstateparameters). #610 touches none of these files.Card PX-04 of the NEMAR portable-compute program.
Round 1 (review fixes, head db83cc7):
maxmemfixes the block count on every path, not only full Riemannian ASR. When the recordedmaxmemisNoneor below thechannels * channels * lookahead_samples * 24bytes thatasr_processreserves, it substitutes half of the free memory and the output then differs between runs and machines. The docstring and the guide now say so, andclean_asremits aUserWarning(never an error) naming the overhead and the smallest sufficient value when a state is requested or supplied with such a budget; plain calls are unchanged.A state in memory is now checked by the code that reads a stored document (new
asr_state.check_state: write, then read), sostate=[1], a dictionary with Euclidean keys anduseriemannian="all", or a bareformatkey raisesValueError. A whole-numberstep_sizesuch as32.0is recorded as an integer, and a fractional one raises when a state is requested, so every state that is accepted can be written and read back.An argument passed beside
state=is accepted when it equals its default or the value the state records (useriemannian=False,"off"andNoneare the same; a list of tolerances equals the tuple); anything else raises. All nine refusals are tested, the recordedmaxmemis tested on the Euclidean path, and every validation branch ofasr_state.pyhas a case.return_stateandstateare keyword-only, with overloads so a type checker sees a tuple forreturn_state=True. A labeled state and an unlabeled EEG never mix, in either direction, and identical output after reapplying holds on one platform and numerical library stack.Merge order with epic #386: it removes the free-memory probe but changes the
maxmemdefault toNone, so a v1 state recorded withmaxmem=64on a dense montage would raise on reapply after #386.EEGLAB reference run (AGENTS.md)
AGENTS.md asks for one run of
.github/workflows/eeglab-reference.ymlon the branch of a core-function change.Run: https://github.com/sccn/eegprep/actions/runs/43631767812 (workflow_dispatch on
feature/asr-stateat db83cc7).Result: all seven shards fail at test setup with
Failed: EEGLAB is at 8ac485f654d6bbb1a6acb8dc9ef3f2eaf3d409ba; expected pinned d0963be9d81acc5b66e84f8d3d7c89e3ce127cc4; for example thecoreshard ends2273 passed, 7 skipped, 127 errors, thehybridshard18 passed, 4 skipped, 125 errorsand theparityshard4 passed, 1 skipped, 7 errors; the run took about 11 minutes.Cause: it is on
develop, not in this pull request:developrecords thesrc/eegprep/eeglabsubmodule at 8ac485f6 whiletests/eeglab_tests/__init__.pypins d0963be9, so any branch cut fromdevelopfails the same way (issue #622). This pull request changes neither value.The owner, as the human approver, decided on 2026-11-14 to merge with this explanation recorded.