Skip to content

MOABBDataset adds interval[0] twice without a stim channel and drops BAD annotations #1845

Backlog

Description

@AxelNoun

Found while reading braindecode/datasets/moabb.py. On master at 83f756a, moabb 1.7.2, MNE 1.13.2, synthetic datasets with triggers at samples 1000 and 3000 (100 Hz):

Script
import tempfile

import mne
import numpy as np
from moabb.datasets.base import BaseDataset

from braindecode.datasets import MOABBDataset

mne.set_log_level("ERROR")


class Fake(BaseDataset):
    def __init__(self, interval, stim, bad=False):
        super().__init__([1], 1, {"left": 1, "right": 2}, "Fake", list(interval), "imagery")
        self.stim, self.bad = stim, bad

    def _get_single_subject_data(self, subject):
        data = np.zeros((2, 6000))
        data[1, [1000, 3000]] = [1, 2]
        info = mne.create_info(["C3", "STI"], 100.0, ["eeg", "stim"])
        raw = mne.io.RawArray(data, info)
        onset, desc = ([], []) if self.stim else ([10.0, 30.0], ["left", "right"])
        if not self.stim:
            raw.drop_channels(["STI"])
        if self.bad:
            onset, desc = onset + [20.0], desc + ["BAD_artifact"]
        raw.set_annotations(mne.Annotations(onset, 0.0, desc))
        return {"0": {"0": raw}}

    def data_path(self, *args, **kwargs):
        return None


def trials(raw):
    events, _ = mne.events_from_annotations(raw, regexp=None)
    return list(zip((events[:, 0] - raw.first_samp).tolist(), raw.annotations.description))


cache = dict(use=True, save_raw=True, path=tempfile.mkdtemp())
for name, kw, load in [
    ("stim", dict(interval=(2, 6), stim=True), None),
    ("no stim", dict(interval=(2, 6), stim=False), None),
    ("stim, raw cache", dict(interval=(2, 6), stim=True), {"cache_config": cache}),
    ("BAD segment", dict(interval=(0, 2), stim=True, bad=True), None),
]:
    moabb_raw = Fake(**kw).get_data([1])[1]["0"]["0"]
    raw = MOABBDataset(Fake(**kw), [1], dataset_load_kwargs=load).datasets[0].raw
    print(f"{name:16} moabb {trials(moabb_raw)}\n{'':16} braindecode {trials(raw)}")
stim             moabb [(1200, 'left'), (3200, 'right')]
                 braindecode [(1200, 'left'), (3200, 'right')]
no stim          moabb [(1200, 'left'), (3200, 'right')]
                 braindecode [(1400, 'left'), (3400, 'right')]
stim, raw cache  moabb [(1200, 'left'), (3200, 'right')]
                 braindecode [(1400, 'left'), (3400, 'right')]
BAD segment      moabb [(1000, 'left'), (2000, 'BAD_artifact'), (3000, 'right')]
                 braindecode [(1000, 'left'), (3000, 'right')]

Since NeuroTechX/moabb#607, SetRawAnnotations already puts each trial at event + interval[0]. Without a stim channel, _annotations_from_moabb_stim_channel starts from these shifted annotations (the branch added in #694) and adds interval[0] again. In this test, raws read through MOABB's raw cache (save_raw=True) had no stim channel, so they took this path too. The function also drops the BAD segments that moabb keeps since 1.6.

Expected: the trials and BAD segments of dataset.get_data(), with or without the cache.

From reading moabb 1.7.2 (not run on real data), datasets without a stim channel and with interval[0] != 0 include Huebner2017/2018, Kojima2024A, Nieto2022, ErpCore2021, BNCI2016_002, BNCI2019_001 and BNCI2020_001. With the raw cache, datasets with a stim channel, such as BNCI2014_001, may be affected as well.

Options, tried on synthetic data:

  • A. Keep MOABB's annotations unless process_pipeline is given. Fixes the four cases above; a custom pipeline that contains SetRawAnnotations is still shifted, and MOABB's bnci_artifact marker becomes an overlapping trial (create_windows_from_events raises).
  • B. Skip interval[0] without a stim channel and re-attach BAD segments. Wrong after a custom pipeline without SetRawAnnotations.
  • C. Load with an empty pipeline and compute once. No cache is written, and BAD segments are still dropped.

With A or B, the inferred mapping counts BAD_artifact: targets become 1, 2 instead of 0, 1.

Which option do you prefer, and should BAD segments be kept? I can open the PR.

Activity

  1. AxelNoun commented on Nov 30, 2026

    @AxelNoun
    ContributorAuthor

    I opened #1849 for the double shift only; it leaves the BAD segments as they are until you decide whether to keep them.

    On the real BNCI2014_001 (subject 4), with MOABB's raw cache (save_raw=True), master puts every trial 500 samples (2 s) after dataset.get_data(); the branch matches it.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions