Skip to content

import moabb mutates the caller's global matplotlib rcParams #1165

Closed

Description

@bruAristimunha

What

Importing anything from moabb mutates the caller's global matplotlib rcParams. Measured:

import matplotlib.pyplot as plt
before = (plt.rcParams["font.family"], plt.rcParams["axes.grid"], plt.rcParams["axes.spines.left"])
import moabb.datasets
after  = (plt.rcParams["font.family"], plt.rcParams["axes.grid"], plt.rcParams["axes.spines.left"])

# before: (['sans-serif'], False, True)
# after : (['serif'],      True,  False)

Chain:

moabb/__init__.py:4          from .benchmark import benchmark
moabb/benchmark.py:12        from moabb.analysis import analyze
moabb/analysis/__init__.py:8 from moabb.analysis import plotting as plt
moabb/analysis/plotting.py:39  set_moabb_defaults()      <-- seaborn set_theme() at import time

Because the package __init__ always runs first, even import moabb.datasets.utils restyles the caller — there is no import path that avoids it.

Why it matters

A user who imports moabb only to load a dataset silently gets serif fonts, gridlines and hidden spines on every unrelated figure they draw afterwards, with no opt-out short of saving and restoring rcParams themselves. Downstream projects already carry defensive code to undo it.

It also makes import moabb.datasets pull in seaborn, matplotlib and the whole analysis subtree for a caller that never plots.

Why the obvious fix doesn't work

Moving the plotting imports out of moabb/datasets/utils.py does not help — I tried it and re-measured; the trigger is the __init__ → benchmark → analysis chain, not datasets.

Suggested direction

Two independent halves, either of which fixes the symptom:

  1. Stop mutating globals at import. Remove the module-scope set_moabb_defaults() call and have the plotting entry points apply the theme themselves — rc_context/plt.style.context around figure creation is the matplotlib-idiomatic form and keeps moabb's own output identical. Note apply_moabb_style(ax, ...) is per-axes and cannot substitute: axes.grid and axes.spines.* are read at axes creation.
  2. Make the top level lazy. A PEP 562 __getattr__ plus __all__ in moabb/__init__.py (or lazy_loader.attach, as MNE and scikit-image use) so benchmark/analysis are only imported on first attribute access. This also cuts import time and the eager third-party dependency pull.

(1) is the narrower correctness fix; (2) is the structural one and additionally speeds up import moabb. They compose.

Both change behaviour for anyone relying on import moabb styling their figures, so this belongs in a minor release rather than a patch — which is why it was deliberately left out of #1161.

Activity

  1. bruAristimunha commented on Aug 26, 2026

    @bruAristimunha
    CollaboratorAuthor

    Fixed in #1161 (e878029).

    Tracing it with an import hook rather than by inspection found three chains, and the one that mattered was indirect — which is why fixing either of the others alone appeared to do nothing:

    moabb/datasets/bids_interface.py -> moabb.analysis.results (get_digest)
      -> runs moabb/analysis/__init__.py -> from moabb.analysis import plotting
        -> plotting.py:39 set_moabb_defaults()
    

    All three are cut by deferring the plotting imports — into the functions that use them, and behind a module-level __getattr__ (PEP 562) in moabb/__init__.py and moabb/analysis/__init__.py.

    Verified: moabb, moabb.datasets, moabb.datasets.utils, moabb.analysis, moabb.paradigms and moabb.evaluations all leave rcParams untouched. Every public name still resolves with docstrings and signatures intact, so dir() and Sphinx autodoc are unaffected, and api.rst never referenced the deferred names directly. An explicit import moabb.analysis.plotting still applies the theme, so MOABB's own figures are unchanged — the theme was not removed, it stopped being a side effect of importing a dataset.

    Bonus: import moabb drops from ~2.6 s to ~0.2 s.

    moabb/tests/test_import_side_effects.py asserts this across four entry modules in a fresh interpreter each; verified to fail on the pre-change tree with the exact before/after rcParams.

  2. bruAristimunha commented on Aug 26, 2026

    @bruAristimunha
    CollaboratorAuthor

    Fixed in #1161 (eb8e59d).

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