Skip to content

Teach NEMAR's assistant and notebook a power spectrum and an ERP image - #483

Merged
neuromechanist merged 6 commits into
developfrom
feature/nemar-erp-core-examples
Sep 24, 2026
Merged

neuromechanist merged 6 commits into
developfrom
feature/nemar-erp-core-examples

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

What changed

NEMAR's assistant now covers the two analyses a reader asks for first: a power spectrum of a recording's EEG channels, and an ERP image of its conditions. Its notebook starter ends with the power spectrum for any recording. ERP CORE (nm000132) is the worked example; the full walkthrough lives in the documentation (nemarOrg/docs#43, and the OSC documentation).

The chat (src/assistants/nemar/config.yaml)

  • A prompt section, "Spectra, events and ERP images", under "Running code in the reader's browser". It covers:

    • choosing EEG channels from window.labels;
    • Welch's method in numpy, since the chat runtime has no SciPy;
    • finding the conditions from nemar_get_events' columns_summary, then asking for only those rows with where and columns;
    • average reference, a windowed-sinc 30 Hz low-pass, epochs from -200 to 800 ms with baseline and 100 µV rejection, and the ERP image itself;
    • ERP CORE's N170 as the worked example, with the numbers the data show: faces more negative than scrambled faces from about 150 ms, most near 250 ms in this subject. It tells the model to report the data, not the textbook latency.

    It is generic on purpose: BIDS names conditions in trial_type, and datasets also use value or their own columns (ERP CORE's faces are in event_type). It also says what to do against an MCP server that predates the filters: the answer has no columns_summary, the filters are ignored, and every row comes back.

  • The dataset-page questions offer the power spectrum and an ERP image second and third.

The notebook (src/assistants/nemar/notebook/starter.ipynb)

  • A last section: the power spectrum of the first two minutes of the chosen recording, so nothing more is downloaded, with SciPy.
    • EOG, ECG and EMG channels are left out by name when the group also holds others.
    • The axis goes to 80 Hz for EEG, MEG and iEEG, and to Nyquist otherwise.
    • It links to NEMAR's worked examples.
  • It takes Welch's periodograms one segment at a time. scipy.signal.welch builds a strided view of every segment at once, whose nominal size overflows Pyodide's 32-bit address space on a long multichannel read (ValueError: array is too big, reproduced in Pyodide 0.29.5 at 129 channels by 30,000 samples). The segment-wise average matches welch to 1e-15.

The notebook check (notebook/e2e-check.js)

  • JupyterLab renders only the cells in view. With a longer starter, the read cell's output left the DOM and the check timed out.
  • The read line, the figures and any error now come from the notebook model. An error names its cell, type and message, which is how the Pyodide welch failure above surfaced; the old search of the page's text for "Traceback" could not see it in an off-screen cell.

Tested

  • bun notebook/e2e-check.js --environment production and --environment develop: all checks pass, with one figure each from the read cell and the spectrum cell and no errors.
  • The starter's cells in CPython on nm000132 (N170), nm000103, xx099903 (EMG, 259 channels), xx099904 and xx099906.
  • pytest on tests/test_assistants, tests/test_scripts/test_build_notebook_site.py and tests/test_core/test_config (746 passed); bun frontend/test-widget-dataset-questions.js (64 passed); nbformat validation.

Depends on

The chat's filters need nemarOrg/nemar-cli#1501, and nm000132's events need nemarOrg/nemar-cli#1499 (merged into dev). Both reach mcp.nemar.org with nemar-cli's next release. Until then, production's get_events declines nm000132 and ignores the filters, and the prompt accounts for that.

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://feature-nemar-erp-core-examp-demo.osc.earth
Branch feature/nemar-erp-core-examples
Commit 7ec27a1

This preview will be updated automatically when you push new commits.

neuromechanist added a commit that referenced this pull request Sep 24, 2026
np.convolve is one-dimensional and an even tap count shifts the signal by
half a sample, so the prompt now says to filter the plotted channel (or
loop), with an odd tap count it gives as code. It says to read every
channel of the group for these analyses, and to offer the power spectrum
when a recording has no events that name conditions. EOG and ECG are
spelled out. Review findings on #483.

Tested: pytest tests/test_assistants and tests/test_core/test_config
(690 passed); the recipe as now written is the one the OSC docs' chat
example runs, which reproduces the page's -4.2 uV.
neuromechanist added a commit that referenced this pull request Sep 24, 2026
The spectrum cell stops with a message on a recording with no samples,
which the index contract allows. The notebook and the changelog spell
out ERP, EOG, ECG and EMG. Review findings on #483.

Tested: the cells in CPython on nm000103 and xx099906; the notebook e2e
check in both environments.
neuromechanist added a commit that referenced this pull request Sep 24, 2026
The figure check allowed a plotting cell that silently drew nothing, as
long as another cell drew one. It now requires exactly one figure from
each of the starter's two plotting cells. Review finding on #483.

Tested: bun notebook/e2e-check.js --environment production and develop
(all checks pass, figures per code cell [0,0,1,1]).
@neuromechanist

Copy link
Copy Markdown
Member Author

Review response

One Sonnet review, which also checked the signal processing with scripts. Every finding is fixed.

  • The filter recipe could not run as written. np.convolve is one-dimensional, and 0.4 s of taps is an even count at 250, 500 and 1000 Hz, which shifts the signal by half a sample. The prompt now says to filter the plotted channel after re-referencing, or loop over channels, with an odd tap count it gives as code (int(0.4 * rate) // 2 * 2 + 1). This is the recipe the OSC docs' chat example runs, and it reproduces that page's result (6c2f57a).
  • Channel choice conflicted with the base snippet's short CHANNELS list. The prompt now says to read every channel of the group for these analyses.
  • The ERP-image suggestion shows on recordings with no conditions. The prompt now tells the model to say so and offer the power spectrum instead. The suggestion itself stays third: gating it on events would need a new fact from the page.
  • The figure check could miss a plotting cell that drew nothing. It now requires exactly one figure from each of the starter's two plotting cells (820b764). A spectrum cell that closes its figure without showing it now fails, with figures per cell [0,0,1,0].
  • ERP, EOG, ECG and EMG were not spelled out in the notebook and the changelog. Fixed (2c956a8).
  • The zero-sample read. The index contract allows n_samples: 0, so the spectrum cell now stops with a message naming the recording.

Gates:

  • bun notebook/e2e-check.js in both environments: all checks pass, figures [0,0,1,1].
  • pytest on tests/test_assistants and tests/test_core/test_config: 690 passed.
  • bun frontend/test-widget-dataset-questions.js: 64 passed.

JupyterLab renders only the cells in view, so once a starter is longer
than the viewport the read cell's output leaves the DOM and the check
timed out. The read line, the figure count and any error now come from
the notebook model; the figure check allows one figure per cell rather
than one in all, and an error names its cell, type and message.

Tested: bun notebook/e2e-check.js --environment production and develop
(all checks pass); before the fix, a longer starter timed out on the
read line, and a SciPy error in an off-screen cell was invisible to the
old Traceback search.
A prompt section on Welch's method, filtering and epoching in numpy (the
chat runtime has no SciPy), and on finding a dataset's conditions from
nemar_get_events' columns_summary before asking for them with where and
columns, with ERP CORE's N170 as the worked example. The dataset-page
questions offer the power spectrum and an ERP image first. The widget
test's templates are relabeled as a fixture, not NEMAR's list.

Tested: pytest tests/test_assistants and tests/test_core/test_config;
bun frontend/test-widget-dataset-questions.js (64 passed); the worked
example's numbers come from running it on nm000132.
The last cell reads the first two minutes of the recording and plots each
channel's spectrum and their median, with SciPy, leaving out EOG, ECG and
EMG channels by name. It takes Welch's periodograms one segment at a time:
scipy.signal.welch's strided view of every segment at once overflows the
browser's 32-bit Python on a long multichannel read. It links to NEMAR's
worked examples for more.

Tested: the notebook e2e check in both environments; the cells in
CPython on nm000132, nm000103, xx099903 (EMG, 259 channels at 1000 Hz),
xx099904 and xx099906; the segment-wise average matches
scipy.signal.welch to 1e-15.
np.convolve is one-dimensional and an even tap count shifts the signal by
half a sample, so the prompt now says to filter the plotted channel (or
loop), with an odd tap count it gives as code. It says to read every
channel of the group for these analyses, and to offer the power spectrum
when a recording has no events that name conditions. EOG and ECG are
spelled out. Review findings on #483.

Tested: pytest tests/test_assistants and tests/test_core/test_config
(690 passed); the recipe as now written is the one the OSC docs' chat
example runs, which reproduces the page's -4.2 uV.
The spectrum cell stops with a message on a recording with no samples,
which the index contract allows. The notebook and the changelog spell
out ERP, EOG, ECG and EMG. Review findings on #483.

Tested: the cells in CPython on nm000103 and xx099906; the notebook e2e
check in both environments.
The figure check allowed a plotting cell that silently drew nothing, as
long as another cell drew one. It now requires exactly one figure from
each of the starter's two plotting cells. Review finding on #483.

Tested: bun notebook/e2e-check.js --environment production and develop
(all checks pass, figures per code cell [0,0,1,1]).
@neuromechanist
neuromechanist force-pushed the feature/nemar-erp-core-examples branch from 820b764 to cbd0dca Compare September 24, 2026 18:12
@neuromechanist
neuromechanist merged commit d7fffcd into develop Sep 24, 2026
18 checks passed
@neuromechanist
neuromechanist deleted the feature/nemar-erp-core-examples branch September 24, 2026 18:17
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