Skip to content

Add the plot tier to eegprep-lean - #409

Merged
neuromechanist merged 5 commits into
developfrom
feat/eegprep-lean-plot
Sep 21, 2026
Merged

neuromechanist merged 5 commits into
developfrom
feat/eegprep-lean-plot

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

Draws what the reader reads, and corrects two things the reader was getting wrong.

The correction that matters

This package previously stated that physical units are not knowable and labeled channels by index. Both were wrong, and the mistake was reading the level-0 array, which carries scale, offset and physical_formula but not the unit they produce. The unit is one document up, on the channel group. nm000103 names its channels E1..E129 and declares them in uV.

The same document says something else worth knowing: level 0 is not always the rate the recording was acquired at. The store resamples level 0 to min(native rate, modality cap), 250 Hz for electroencephalography, so nm000103 was recorded at 500 Hz and arrives here at 250. A reader reporting only rate lets a caller believe they hold the recording as acquired.

So channels.py is new, base tier and standard library only: a session can learn what a recording's channels are called and what they are measured in without installing zarr or numpy. A Window now carries labels, unit and original_rate.

A window reports a unit only when every channel in it agrees on one, because the store contract says to read the unit from the channel rather than the modality, and magnetoencephalography is a Tesla-based unit, not a voltage. Stored counts never carry the unit, because they are not in it.

The plot

plot_window stacks traces with the first channel at the top; to_png renders to bytes. Behind the plot extra.

Nothing imports matplotlib.pyplot. pyplot selects a backend when imported, and in a browser that backend wants the DOM. The Figure API needs none, and bytes are what the browser case wants anyway. A subprocess test asserts pyplot stays unimported, because that failure is invisible natively and fatal in Pyodide.

Traces are demeaned for display. Level-0 channels on nm000103 sit at offsets of -16250, -25999 and -28396, differing by more than the signal spans, so without it every trace is a flat line at its own level. It never touches the window, and demean=False draws values as they are. Demeaning, not detrending.

Default spacing follows the median channel amplitude, not the largest. One high-amplitude channel is ordinary in raw EEG, and scaling to it flattens everything else to an unreadable line.

Tiers stand alone

window.py imports zarr inside read_window, so [plot] installs and works without [zarr]. CI runs four offline tiers, and the base tier caught a real bug while this was written: a module-scope numpy import in a new test file made it fail collection instead of skip.

tier offline
base, nothing installed 52 passed
[zarr] 67 passed
[plot] 84 passed
both 96 passed

Live tier: 7 passed against zarr.nemar.org.

Review findings, all addressed

  • read_window lost its friendly error. Deferring the zarr import moved its failure past the lazy loader in __init__, so it raised a bare ModuleNotFoundError where every other name explains itself. Hint tables moved to extras.py so both paths give the same message.
  • A test agreed with itself. The lazy-name test read the expected extra from the table it was testing, so a remapping mutation passed. It now checks against a literal map, and against the table's key set so a new name cannot be added without being covered.
  • Five uncovered paths, each found by a surviving mutation: the ax= parameter, the shape guard, the scale bar's position (height alone did not pin it; a bar drawn above the top trace is the same height), the axis limits, and the title contents.
  • test.yml gained a both-extras offline tier, which TestExtras's docstring already claimed existed.
  • The install table's last row presented 30.4 MB as the cost of [zarr,preprocess]. ADR 0069 measured cumulatively, so that figure includes matplotlib; the row is now [zarr,plot,preprocess] and says [zarr,preprocess] alone has not been measured.
  • The FALLBACK_SPACING comment said "every channel is flat" when the rule triggers on the median.

The scheduled job that was claimed but missing

test.yml said the network tier was "covered by the scheduled live job". No such job existed, so the contract tests ran nowhere. lean-live.yml adds it, daily and on dispatch, deliberately not on pull requests: those tests fail on an outage at zarr.nemar.org as readily as on a real divergence.

A divergence from ADR 0069

The install table credited the bare install with "read a window of data", which needs the zarr extra. ADR 0069's table puts zarr in the base, and the implementation cannot follow it: zarr installs under Pyodide only with deps=False, so a declared zarr makes micropip.install("eegprep-lean") fail outright. Recorded in nemarOrg/nemar-cli#1468.

Verification

Twenty mutations applied one at a time, each caught. One survives offline and is covered in the live tier only: unit carried onto stored counts, which only read_window composes and nothing offline exercises, because that needs a real sharded zarr store. Worth closing with a local store served over loopback; not in this PR.

The network tier checks conformance to a contract another team serves at
zarr.nemar.org, so it fails on an outage there as readily as on a real
divergence. That is not a reason to turn an unrelated pull request red,
so it runs daily rather than on pull requests, where a failure means the
contract and this reader have diverged.

test.yml already claimed a scheduled job covered this tier. It did not
exist, so until now the contract tests ran nowhere.
plot_window draws a window as stacked traces with the first channel at
the top, and to_png renders the figure to bytes.

Three things are deliberate. Nothing imports matplotlib.pyplot: pyplot
selects a backend when imported and in a browser that backend wants the
DOM, so this uses the Figure API, and a subprocess test asserts pyplot
stays unimported because that failure is invisible natively. The y-axis
carries no unit, because the array declares scale and offset but not
what they produce; amplitude is stated as a scale bar instead. Traces
are demeaned for display, since level-0 channels sit at per-channel
offsets in the thousands that differ by more than the signal spans.

Default spacing follows the median channel amplitude rather than the
largest, so one high-amplitude channel overlaps its neighbors instead of
flattening every other channel to an unreadable line.

window.py now imports zarr inside read_window rather than at module
scope, so the plot tier installs and runs on its own. CI exercises each
extra separately for that reason.

The README install table credited the base install with reading a window
of data, which needs the zarr extra. Corrected, along with why zarr is an
extra rather than a base dependency as ADR 0069 has it: a declared zarr
makes micropip.install("eegprep-lean") fail outright, because zarr
installs under Pyodide only with deps=False.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Code review skipped — your organization has no extra usage available to pay for this review.

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, reopen this pull request to trigger a review.

@claude

claude Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Claude encountered an error after 0s —— View job


I'll analyze this and get back to you.

The lazy loader caught every ImportError and reported it as the extra not
being installed. An ImportError raised from inside store.py or plot.py
looks identical from outside, so a bug in either would tell the reader to
install something they already have while the real error stayed buried in
the cause.

It now checks ImportError.name against the packages the extra actually
installs and re-raises anything else untouched. Covered in test_index.py,
which runs in the base tier, with errors the interpreter really raised
rather than constructed stand-ins.
The unit is on the channel group, not on the level-0 array. Reading only
the array, which carries scale, offset and physical_formula but not the
unit they produce, led this package to state that units were unknowable
and to label channels by index. Both were wrong: nm000103 names its
channels E1..E129 and declares them in uV, one document up.

The same document says level 0 is not always the rate the recording was
acquired at. The store resamples it to min(native rate, modality cap),
250 Hz for electroencephalography, so nm000103 was recorded at 500 Hz and
arrives here at 250. A reader that reported only rate let a caller believe
they held the recording as acquired.

channels.py is base tier, stdlib only, so a session can learn what a
recording's channels are called and what they are measured in without
installing zarr or numpy. A Window now carries labels, unit and
original_rate; a window reports a unit only when every channel in it
agrees on one, because the store contract says to read the unit from the
channel and magnetoencephalography is a Tesla-based unit, not a voltage.
Stored counts never carry the unit, since they are not in it.

Also from review:

- read_window imports zarr inside its own body, which put its failure
  past the lazy loader in __init__ and gave a bare ModuleNotFoundError
  where every other name explains itself. The hint tables moved to
  extras.py so both paths give the same message.
- The extras table is now checked against a literal map rather than
  against itself, after a remapping mutation agreed with its own
  expectation.
- Covered the ax= parameter, the shape guard, the scale bar's position,
  the axis limits and the title, each of which survived a mutation.
- test.yml gains a both-extras offline tier, which TestExtras's docstring
  already claimed existed.
The install table credits the base with reading the channel list, and
nothing showed how. read_group_metadata is what does it, and it needs no
extra.
@neuromechanist
neuromechanist merged commit bc2a63c into develop Sep 21, 2026
6 checks passed
@neuromechanist
neuromechanist deleted the feat/eegprep-lean-plot branch September 21, 2026 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant