Skip to content

Fix MotorImagery.used_events ignoring n_classes when events=None - #1361

Merged
bruAristimunha merged 3 commits into
NeuroTechX:developfrom
SID-6921:fix/motor-imagery-used-events-n-classes
Nov 18, 2026
Merged

bruAristimunha merged 3 commits into
NeuroTechX:developfrom
SID-6921:fix/motor-imagery-used-events-n-classes

Conversation

@SID-6921

Copy link
Copy Markdown
Contributor

MotorImagery.used_events and its sibling FilterBankMotorImagery.used_events both have an events is None branch meant to collect dataset events up to n_classes. FilterBankMotorImagery breaks once len(out) == n_classes; MotorImagery doesn't, so it collects every event in the dataset regardless of n_classes.

from moabb.paradigms import MotorImagery, FilterBankMotorImagery

# FakeDataset(paradigm="imagery") has 3 events (fake1, fake2, fake3) by default.
dataset = FakeDataset(paradigm="imagery")

MotorImagery(n_classes=2, events=None).used_events(dataset)
# {'fake1': 1, 'fake2': 2, 'fake3': 3}  <- all 3, ignoring n_classes=2

FilterBankMotorImagery(n_classes=2, events=None).used_events(dataset)
# {'fake1': 1, 'fake2': 2}  <- correctly stops at 2

Since used_events's result is used directly as the event_id passed to EventsToLabels with no truncation downstream, this means a user who sets n_classes on MotorImagery without an explicit events list silently gets however many classes the dataset actually has, not the number they asked for.

Added the same len(out) == n_classes break to MotorImagery.used_events, guarded so the n_classes is None auto-detect path (which should keep collecting everything, then set n_classes = len(out)) is unchanged.

Added a regression test comparing both siblings' output on the same dataset; it fails on the pre-fix code ({'fake1': 1, 'fake2': 2, 'fake3': 3} instead of 2 entries) and passes after. Ran the full test_paradigms.py (120 passed) and ruff check/ruff format --check on both changed files.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 03:44

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

With events=None, the loop collected every dataset event with no
limit, so a dataset with more events than n_classes silently used
all of them instead of the requested n_classes. FilterBankMotorImagery
already breaks once len(out) == n_classes in the same branch; applied
the same guard here, keeping the auto-detect path (n_classes=None)
unchanged.
@SID-6921
SID-6921 force-pushed the fix/motor-imagery-used-events-n-classes branch from 7e2b4eb to 35f86c0 Compare November 18, 2026 05:15
…n neural signatures

- MotorImagery fills n_classes in from the first dataset when it is None
  (scoring reads it); the new early stop then cut later datasets down to
  that count. Stop only on an n_classes given to the constructor.
- neural_signatures built MotorImagery(n_classes=2), which now kept two
  classes; drop it so the figures keep every class, as before.
- Changelog entry.
@bruAristimunha

Copy link
Copy Markdown
Collaborator

Thanks @SID-6921, the fix is right. I pushed e8d4afc on top to cover two side effects found while reviewing:

  • With n_classes=None, used_events fills n_classes in from the first dataset (scoring reads it), so the new early stop cut later datasets down to that count (a 4-class dataset after a 2-class one came back with 2). Only an n_classes passed to the constructor now limits the events; regression check added to your test.
  • neural_signatures built MotorImagery(n_classes=2), so its figures would have dropped to two classes; it no longer passes n_classes.
  • Changelog entry (check-whats-news).

# Conflicts:
#	docs/source/whats_new.rst
@bruAristimunha
bruAristimunha merged commit 49bbb62 into NeuroTechX:develop Nov 18, 2026
15 checks passed
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.

3 participants