Correctness and validation
Analysis software is only useful if its numbers can be trusted. This chapter documents what is verified, how, and — as importantly — what is not.
Phasic’s test suite currently runs 587 tests with every feature enabled, covering the pure
signal layers, the readers and writers, model inference, and a format conformance suite. A
plain cargo test runs 481 of them; the rest belong to model inference, which is behind the
onnx feature and needs cargo test --features onnx.
Tests are expected to pass on a fresh checkout with no setup; those needing the ONNX Runtime library skip themselves with a printed reason rather than failing.
Four kinds of verification
Different claims need different evidence, and the suite is built in layers accordingly.
1. Unit tests on the pure layers
Signal mathematics lives in layers with no interface or file dependencies — filters, resampling, spectral estimation, montages, interpolation, epoching, ICA and ASR — and is tested in isolation. This includes degenerate inputs: empty and single-epoch averages, zero-variance channels, chunk-boundary continuity for streaming operations, and the behaviour of each entry point on input that cannot produce a meaningful answer.
2. Format conformance against an external oracle
Testing a reader against fixtures you wrote yourself proves only that it is self-consistent: it pins the layout you believe the specification describes, which is the one thing it cannot check.
The XDF reader is therefore also tested against the reference files published by the format’s own maintainers — the same files the ecosystem’s Python implementation tests against — whose documentation states every expected value. That makes it a genuine oracle rather than a smoke test.
This caught two real bugs on its first run, both now pinned as regression tests: a marker stranded at a negative onset, and 175 markers collapsed into a single instant by a clock reset.
3. Round-trips
Writers are tested by reading back what they wrote. A recording written to BDF and read again must return the same physical values in microvolts — a stronger claim than checking that the header fields look plausible.
4. Model parity against upstream
This is the most involved, because a model ported from Python to a Rust inference pipeline can differ in several independent ways, and a single end-to-end comparison cannot say which.
Each model in the zoo ships validation fixtures and a script that answers three separate questions:
Is the port faithful? Run the upstream PyTorch implementation and Phasic’s pipeline over the same recording and compare. Any gap is a bug in the port, not a property of the model.
Where does a gap come from? Rather than comparing two pipelines, the CHB-MIT validation runs three, so that a nonzero result is interpretable:
| Pipeline | Composition | Isolates |
|---|---|---|
| A | PyTorch + reference preprocessing | — |
| C | ONNX graph + reference preprocessing | A vs C — graph fidelity, expected near 1e-6 |
| B | ONNX graph + Phasic preprocessing | C vs B — the preprocessing difference alone |
| A vs B — what a user actually gets |
Is the model any good? Measured against ground truth, with a metric appropriate to the task rather than a default one.
Choosing the right metric
The metric matters more than it might appear, and the two shipped models need different ones for reasons worth stating.
For sleep staging, class agreement is appropriate — but the target is not 100%. Human inter-scorer agreement on sleep staging is only around 82%, so agreement in the 70–80% range is the expected result. Agreement near 100% would indicate that labels had leaked into the evaluation; agreement near chance (20%) would indicate a broken pipeline. Both are failures.
For seizure detection, class agreement is meaningless. The head was trained on 1.1% positives and is badly uncalibrated — it labels effectively every window as non-seizure, so two pipelines agree on the class 100% of the time regardless of how far apart their probabilities are. The per-window probabilities are the only honest comparison, and quality is reported as AUROC, a ranking metric, because accuracy at roughly 1% prevalence is satisfied by always answering “no”.
What is not verified
- Windows and macOS builds. Everything is developed and tested on Linux. Neither other platform has been built, let alone tested
- Continuous integration. There is none yet, so every result here comes from a developer’s machine rather than a clean automated environment
- The CHB-MIT preprocessing choice is an open question. Upstream resamples with an FFT method per ten-second window; Phasic’s manifest declares a polyphase resample over the whole recording. The validation isolates this difference rather than hiding it, but which is correct for this model is not yet settled
- Interface behaviour. The test suite covers signal processing and file handling. The interface itself is verified by use, not automatically
Reproducing the validation
The validation scripts live in scripts/ and declare their own dependencies inline, so they
run without setting up an environment:
uv run scripts/validate_sleepfm.py
uv run scripts/validate_chbmit.py
uv run scripts/verify_xdf.py
They need the upstream model code and PyTorch, which is why they are scripts rather than part of the Rust test suite: the application itself never depends on PyTorch or Python.