Repository navigation
Fix inconsistent event targets and preserve window dataset settings - #1166
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Handle event descriptions missing from individual recordings before passing the mapping to MNE.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Fixes inconsistent event targets, preserves window dataset settings on reload, and prevents metadata mutation.
Changes:
- Synchronize event mappings across recordings.
- Restore saved window target settings.
- Copy metadata before adding descriptions.
- Add regression tests and release notes.
File summaries
| File | Reviewed changes |
|---|---|
test/unit_tests/preprocessing/test_windowers.py |
Parallel mapping regression tests |
test/unit_tests/datautil/test_serialization.py |
Target-setting reload tests |
test/unit_tests/datasets/test_dataset.py |
Metadata immutability tests |
docs/whats_new.rst |
Documents fixes |
braindecode/preprocessing/windowers.py |
Global event mapping; moderate issue (2 votes) with missing local descriptions under use_mne_epochs=True |
braindecode/datautil/serialization.py |
Restores saved dataset settings |
braindecode/datasets/base.py |
Avoids metadata mutation |
Review details
- Files reviewed: 7/7 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if infer_mapping: | ||
| mapping = _infer_mapping(concat_ds) |
A mapping may send several event descriptions to the same target, as the sleep staging examples do for stages 3 and 4. mne.Epochs rejects an event_id with repeated values since MNE 1.13, which broke create_windows_from_events whenever the windows were stored as epochs. Descriptions sharing a target are now joined into one /-separated key.
| mapping: dict(str: int) | ||
| Mapping from event description to numerical target value. Must be | ||
| provided when any of ``trial_start_offset_samples``, | ||
| Mapping from event description to numerical target value. If None, the | ||
| event descriptions of all datasets are numbered from 0 in dataset | ||
| order, with descriptions sorted within each dataset. This mapping is | ||
| shared by every dataset regardless of ``n_jobs``. Must be provided | ||
| when any of ``trial_start_offset_samples``, | ||
| ``trial_stop_offset_samples``, or ``window_stride_samples`` is a dict. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1166 +/- ##
==========================================
+ Coverage 86.98% 86.99% +0.01%
==========================================
Files 144 144
Lines 16570 16580 +10
==========================================
+ Hits 14413 14424 +11
+ Misses 2157 2156 -1 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
Review effort: Lite
Findings: 3
Open (5)
Ifwindow_kwargs.jsonever contains both entries (e.g., an intermediate or future format that… · New This release note says shared-target descriptions are joined into a singleevent_idkey, but the… · New Withmapping=None, this global mapping is passed unchanged to every_create_windows_from_events… Minor API clarity:_infer_mappingis now part of the public behavior described in the… · New Update mapping docstring type to allow None
| kwargs[1] | ||
| for kwargs in window_kwargs | ||
| if kwargs[0] in ("WindowsDataset", "EEGWindowsDataset") | ||
| ] | ||
| windows_ds_kwargs = windows_ds_kwargs[0] if len(windows_ds_kwargs) == 1 else {} |
| def _infer_mapping(concat_ds): | ||
| # built once here so parallel workers do not each start counting from 0 | ||
| mapping: dict[str, int] = dict() | ||
| for ds in concat_ds.datasets: | ||
| for event_name in np.unique(ds.raw.annotations.description): | ||
| if event_name not in mapping: | ||
| mapping[event_name] = len(mapping) | ||
| return mapping |
|
Many thanks @tayal-sarthak 🙏🏽 |
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
Only a non-blocking documentation nit remains.
Review effort: Lite
Findings: 2
Open (4)
Ifwindow_kwargs.jsonever contains both entries (e.g., an intermediate or future format that… Withmapping=None, this global mapping is passed unchanged to every_create_windows_from_events… Minor API clarity:_infer_mappingis now part of the public behavior described in the… Update mapping docstring type to allow None
Resolved since last review (1)
…rainTokenizer, BrainOmni, Deep4Net, AvgPool2dWithConv, TimeDistributed, Chomp1d, braindecode#1166), BaRISTA braindecode#1175 entry, MIRepNet (braindecode#1146) under the current section
…rop_last_window removal moved to 3.0 (#1520) * whats_new: Breaking changes in 2.0, corrected PR references; drop_last_window removal moves to 3.0 * whats_new: PR references from #1237 (NeuroRVQ, llms.txt, BrainTokenizer, BrainOmni, Deep4Net, AvgPool2dWithConv, TimeDistributed, Chomp1d, #1166), BaRISTA #1175 entry, MIRepNet (#1146) under the current section * whats_new: cite #1519


Windowed datasets could return different targets after parallel windowing, reloading, or reading metadata. This change fixes three paths that caused those differences.
create_windows_from_eventsnow infers one event mapping across all recordings before starting parallel workers. Withmapping=None, a recording containing onlyrightpreviously assigned it target0when processed in a separate worker, even ifrightwas target1in another recording. The target IDs now agree acrossn_jobssettings.load_concat_datasetnow restorestargets_fromandlast_target_onlyfrom savedEEGWindowsDatasetsettings. Previously, reloading a dataset with channel targets silently fell back to metadata targets. The loader still accepts the olderWindowsDatasetsetting name.BaseConcatDataset.get_metadatanow adds description columns to a copy. Previously, a description key such astargetcould overwrite the source metadata and change later reads of the dataset.Closes #1165