Skip to content

FIX Pratibha2: a constant channel z-scores to 0 at any finite value - #1869

Merged
bruAristimunha merged 4 commits into
braindecode:masterfrom
AxelNoun:fix/bd-t11-8-flat-channel-zscore
Dec 1, 2026
Merged

bruAristimunha merged 4 commits into
braindecode:masterfrom
AxelNoun:fix/bd-t11-8-flat-channel-zscore

Conversation

@AxelNoun

@AxelNoun AxelNoun commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

With normalize_input=True, Pratibha2.encode z-scores each channel as (x - mean) / std.clamp_min(std_floor). On a constant channel whose float32 mean is not exactly its value, x - mean is rounding noise, which that division scales up (400 samples, random weights):

channel 0 flat at 0.0 uV: max |z| of channel 0 = 0
channel 0 flat at 0.37 uV: max |z| of channel 0 = 2.98e+04
channel 0 flat at 123.456 uV: max |z| of channel 0 = 1.53e+07
output at 0.37 uV equals output at 0 uV: False

The output then depends on the level of a channel with no signal, and differs between volts and microvolts (by 0.20 at 0.37 uV), against the docstring.

Changes

  • Pratibha2.encode: a channel whose samples are all equal gives x - x[..., :1] (0 at any finite value, NaN for inf as before) instead of the z-score; other channels are unchanged.
  • One docstring sentence, one test, one changelog entry.

Testing

Gates of AGENTS.md section 1 and the files of scripts/ci_plan.py --base upstream/master --head HEAD, on master (92cb203) and on this branch, Windows, Python 3.12, torch 2.15 CPU. Counts are passed / failed / skipped.

Command master branch
pytest -rfEs test/unit_tests/models/ --select-models=Pratibha2 -k "Pratibha2 or completeness or summary_csv or categorization" -n 2 82 / 0 / 5, 1 xfailed 83 / 0 / 5, 1 xfailed
pytest -rfEs test/unit_tests/models/test_model_contract.py --select-models=Pratibha2 -k Pratibha2 14 / 0 / 1 14 / 0 / 1
pytest -rfEs test/unit_tests/models/test_pretrained_compat.py --select-models=Pratibha2 -k "Pratibha2 or covers_every or no_strategy" 15 / 0 / 0 15 / 0 / 0
pytest -rfEs test/unit_tests/models/test_foundation_models.py -k pratibha2 8 / 0 / 2 9 / 0 / 2
pytest -rfEs on the 23 test files of the CI plan, --select-models=Pratibha2 -n 2 1693 / 5 / 38, 1 xfailed 1694 / 5 / 38, 1 xfailed
pytest -rfEs --run-network test/unit_tests/models/test_foundation_models.py::test_pratibha2_pretrained_matches_the_release 1 / 0 / 0 1 / 0 / 0
  • Skips in the gates: --run-network not given (2), torch.compile skipped on Windows, forward takes no ch_mask, pydantic or numpydantic not installed. The plan run skips the same 38 tests on both sides (network, CUDA or MPS, Windows bfloat16, sphinx not installed, models without an embedding parameter or with **kwargs).

  • test_config.py --select-models=Pratibha2, run again with numpydantic 2.0.1 installed: 19 passed on master and here.

  • torch 2.6.0 (the floor), test_integration.py --select-models=Pratibha2 -k "Pratibha2 and (exported or torch_script)": 2 passed on master and here; the new test passes.

  • The 5 failures are the same on both sides: test_huggingface.py::test_local_weights_*symlinks*, WinError 1314 (no symlink privilege on this Windows account).

  • The plan has select_models: "" because a test file changed, so CI will run these files for every model; locally only Pratibha2 was selected.

  • Mutation check: restoring the old line fails test_pratibha2_constant_channel_value_does_not_matter; returning zeros instead of x - x[..., :1] (an inf channel then becomes 0) fails it too; .all to .any fails test_pratibha2_is_montage_agnostic.

  • Parity, master against the branch, same seed, OMP_NUM_THREADS=1, float32 and float64, default config, normalize_input=False, on_missing="drop", 400 and 650 samples, batch 1 to 3: 2456 of 2472 tensors are torch.equal (outputs, return_features, encode, input and parameter gradients, state_dict keys and shapes, from_config). The 16 others are the input gradients of windows with a channel constant at 0 or 10.

  • Released weights (pinned revision): they load on both sides, and the features of non-constant windows and of the pin input are identical.

  • pre-commit run --files on the three files: passed.

  • Not run: torch.compile (skipped on Windows).

  • New or changed behavior is covered by relevant regression tests (or N/A with reason)

  • Style checks recorded, e.g. pre-commit run --files <changed files>

  • docs/whats_new.rst updated (at most 3 lines), or the no changelog label, which a maintainer adds, for changes to code that is not released yet (the changelog CI check requires one of the two)

Notes for reviewers

  • Affected: Pratibha2 (since 2.1), normalize_input=True, windows with an exactly constant channel. Other windows are bitwise unchanged (see Testing). A constant channel at 0 or 10 keeps its outputs; its input gradient goes from about 1e10 to a bounded value.
  • The release's example, (x - mean) / (std + 1e-6), does not zero such a channel either in float32 (max |z| 0.03 at 0.37 uV, 15.3 at 123.456 uV), so neither master nor this branch matches it there (released weights: features differ by 0.84 to 1.59).
  • Subtracting the first sample before the z-score also works but changes noisy channels by up to 2.4e-7. No parameter added; std_floor is unchanged.

@bruAristimunha
bruAristimunha merged commit 4a6e25f into braindecode:master Dec 1, 2026
@AxelNoun
AxelNoun deleted the fix/bd-t11-8-flat-channel-zscore branch December 1, 2026 12:09
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.

2 participants