Skip to content

Phase 4: Export ICLabel to ONNX and add the native onnxruntime backend #377

Closed

Description

@neuromechanist

Phase 4 of #324.
Base: epic/324-pyodide-browser.
Depends on: nothing. Can start immediately, in parallel with phases 1 to 3.
Blocks: phases 5 and 6.

Why

ICLabel is torch-only today, and torch has no WebAssembly build.
An ONNX artifact removes the heaviest optional dependency from the classification path and makes ICLabel deployable anywhere, with or without Python.
This phase does the export and the native runtime; the browser runtime is phase 5 and quantization is phase 6.

Grounding, verified against the current tree

src/eegprep/plugins/ICLabel/iclabel.py:59 imports torch lazily and :86 loads netICL.mat (10.8 MB on disk) through ICLabelNet, rebuilding the network from MATLAB parameter arrays on every call.
iclabel_net.py constructs the graph by assigning torch.nn.Parameter values pulled out of that .mat.
So the export needs to happen once, offline, and the result becomes the shipped artifact.

Feature extraction is already pure numpy in ICL_feature_extractor, and iclabel.py:73-80 does the 4-way augmentation and the :98-102 averaging outside the network.
Per #324, all of that stays float and unchanged.

Two tests assert the packaged artifact by name and must be updated together with the swap: tests/test_public_api_examples.py:73 and :144.
pyproject.toml [tool.setuptools.package-data] matches plugins/**/*.mat and will need an *.onnx pattern.
src/eegprep/resources/help/pop_iclabel.md:25 names netICL.mat in user-facing help text.
tools/iclabel/iclabel_net_load_py_measures.py:22 also resolves the .mat path.

Scope

  • An offline export script under tools/iclabel/ that produces iclabel.onnx from the torch network, with a pinned opset recorded in the script and in the artifact's provenance notes.
    The script is developer tooling and is not shipped.
  • An onnxruntime inference backend, behind an [iclabel] extra.
  • torch becomes export-only: move it out of the runtime path in iclabel.py, and keep the [torch] extra for the export script and for regenerating the artifact.
  • Ship iclabel.onnx in the wheel in place of netICL.mat, updating package data, the two packaging tests, the help Markdown, and the tools script.
  • Cross-check the export three ways: against the existing torch output on sample_data, against the MATLAB fixtures the current parity tests use, and against mne-icalabel's published export.

Decision already recorded on #324

int8 ships as the default artifact everywhere, native and browser, not browser-only.
That is phase 6's job, but it affects this phase in one way: keep the float32 export reproducible and available, because phase 6 needs it as the parity teacher and reviewers will need it to argue from.
Do not delete the float32 export path once int8 lands.

Non-goals

No quantization; phase 6.
No browser runtime or async entry point; phase 5.
No change to feature extraction, input normalization, or softmax, all of which stay float per the epic's out-of-scope list.
No numerical change anywhere else.

Acceptance criteria

  • tools/iclabel/ export script produces iclabel.onnx deterministically, with the opset pinned and recorded.
  • [iclabel] extra provides onnxruntime; all includes it.
  • iclabel(EEG, engine=None) runs through onnxruntime with no torch installed.
  • torch appears nowhere in the runtime classification path.
  • iclabel.onnx replaces netICL.mat in the wheel; pyproject.toml package data, tests/test_public_api_examples.py:73 and :144, src/eegprep/resources/help/pop_iclabel.md, and tools/iclabel/iclabel_net_load_py_measures.py all updated consistently.
  • ONNX output matches the torch output on sample_data to a stated tolerance, with the tolerance justified rather than chosen to make the test pass.
  • Existing MATLAB parity tests in tests/test_iclabel.py still pass where MATLAB is available, with unchanged tolerances and preserved skip behavior.
  • Documented cross-check against mne-icalabel's export, with any disagreement explained rather than averaged away.
  • The lite and beta networks still raise the existing clear NotImplementedError from iclabel.py:53; do not quietly change that behavior.
  • docs/source/ updated: which extra is needed, what changed about the shipped artifact, and how to regenerate it.
  • Pre-commit, ruff, ruff format, ty clean.

/goal prompt

/goal Replace eegprep's torch-only ICLabel runtime with a shipped ONNX artifact and an onnxruntime backend, keeping numerical agreement with the current network and with MATLAB.

Source of truth: src/eegprep/plugins/ICLabel/iclabel.py, iclabel_net.py, the bundled netICL.mat, the MATLAB fixtures exercised by tests/test_iclabel.py, and mne-icalabel's published ONNX export. Read AGENTS.md first.

Constraints: feature extraction, input normalization, and softmax stay float and unchanged; torch becomes export-only and must not appear in the runtime path; the export script lives in tools/ and is not shipped; pin the opset; keep the float32 export reproducible because phase 6 needs it as the parity teacher.

Finish line: iclabel(EEG, engine=None) classifies correctly with torch uninstalled; iclabel.onnx is the packaged artifact and every reference to netICL.mat in packaging, tests, help text, and tools is updated; ONNX-versus-torch agreement is demonstrated on sample_data at a justified tolerance; MATLAB parity tests pass unchanged; the mne-icalabel cross-check is documented; docs updated; pre-commit, ruff, ruff format, and ty clean.

Never relax an existing tolerance to make a test pass. If the exports disagree, investigate and explain rather than widening the bound. Report any review finding you chose not to fix, with the reason.

Activity

  1. added
    phase
    on Sep 17, 2026
  2. neuromechanist commented on Sep 17, 2026

    @neuromechanist
    MemberAuthor

    🤖 Done. PR #381 merged into epic/324-pyodide-browser. Closing manually because Closes #377 only fires on merges into the default branch, not into an epic branch; the work reaches develop when epic #324 merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    phase

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions