Skip to content

Support MNE bad_* annotations in AMICAICA.fit() #251

Closed

Description

@sunyuhongwr

I found one remaining MNE-compatibility issue related to bad annotations. Currently, AMICAICA.fit() obtains the fitting data through:
X = picked.get_data(**range_kw)
but does not appear to account for raw.annotations containing segments whose descriptions start with bad.
In MNE-Python, ICA.fit() uses reject_by_annotation=True by default, so annotated bad segments are excluded from the data used for ICA fitting.
For example:

raw.set_annotations(
    mne.Annotations(
        onset=[100.0],
        duration=[10.0],
        description=["bad_artifact"],
    )
)

ica = mne.preprocessing.ICA(...)
ica.fit(raw)

The 10-second bad segment is not used for the ICA decomposition. With the current AMICAICA.fit(), however, the corresponding samples appear to still be passed to AMICA. This could potentially affect the decomposition because strongly contaminated segments can contribute substantially to the AMICA likelihood optimization.

Why this matters for MNE compatibility

This also creates an inconsistency with the MNE-facing API.
For example, after fitting:

model_probability = ica.get_model_probability(raw)

the returned model probabilities still contain values for the annotated bad segments.
I understand that this is not necessarily wrong for get_model_probability() itself—the method is evaluating the fitted model on the supplied data. The problem is that, from an MNE user's perspective, the model was expected to be fitted while excluding the bad_* segments, just as mne.preprocessing.ICA.fit() does.

Possible implementation

Would it be possible to add something analogous to MNE's:

reject_by_annotation=True

to AMICAICA.fit()?
For example:

amicaica.fit(
    raw,
    reject_by_annotation=True,
)

Internally, the wrapper could construct a mask of valid samples from the bad_* annotations and fit AMICA only on the valid samples.
Importantly, I don't think the bad segments necessarily need to be physically deleted from the Raw object. A cleaner approach may be:

  1. identify samples covered by bad_* annotations;
  2. create a boolean mask of good samples;
  3. extract only the good samples for AMICA fitting;
  4. concatenate the remaining good segments along time;
  5. preserve the original Raw object and annotations.
    This would allow AMICA to be trained only on valid data while preserving the original time axis and annotations for subsequent MNE operations.

One additional consideration

For multi-model AMICA, this may be particularly important because the model probabilities are time-dependent. If bad segments participate in fitting, they can influence the learned model distributions and potentially the resulting model dominance probabilities. I think adding annotation-aware fitting would make AMICAICA substantially more consistent with the semantics users expect from an MNE ICA wrapper, while still preserving the existing AMICA backend behavior.

TEST

I ran a test to investigate whether including bad data segments affects AMICA model fitting. As shown in the plotted results below, there is a clear structural/shape change in the AMICA model probabilities right around the bad segments (red shadow). This indicates that uncleaned artifactual segments might indeed introduce underlying bias or distortion to the model fitting process and thus lead to strange result (figure 2)

Image

.

Image

Activity

  1. neuromechanist commented on Aug 22, 2026

    @neuromechanist
    Member

    Implemented in #252 (thanks @sunyuhongwr for both the report and the PR). AMICAICA.fit now omits bad_*-annotated samples by default (reject_by_annotation=True, recorded in good_sample_mask_), and the scoring surface (get_model_probability, plot_model_probability, mir, pmi) rejects by the passed instance's own annotations with per-sample outputs kept timeline-aligned: full n_times length, NaN over rejected segments, so no manual reconstruction is needed. Available on dev now; it will be in the next release.

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