Repository navigation
Description
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:
- 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.contextaround figure creation is the matplotlib-idiomatic form and keeps moabb's own output identical. Noteapply_moabb_style(ax, ...)is per-axes and cannot substitute:axes.gridandaxes.spines.*are read at axes creation. - Make the top level lazy. A PEP 562
__getattr__plus__all__inmoabb/__init__.py(orlazy_loader.attach, as MNE and scikit-image use) sobenchmark/analysisare 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
bruAristimunha commentedon Aug 26, 2026
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:
All three are cut by deferring the plotting imports — into the functions that use them, and behind a module-level
__getattr__(PEP 562) inmoabb/__init__.pyandmoabb/analysis/__init__.py.Verified:
moabb,moabb.datasets,moabb.datasets.utils,moabb.analysis,moabb.paradigmsandmoabb.evaluationsall leave rcParams untouched. Every public name still resolves with docstrings and signatures intact, sodir()and Sphinx autodoc are unaffected, andapi.rstnever referenced the deferred names directly. An explicitimport moabb.analysis.plottingstill 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 moabbdrops from ~2.6 s to ~0.2 s.moabb/tests/test_import_side_effects.pyasserts this across four entry modules in a fresh interpreter each; verified to fail on the pre-change tree with the exact before/after rcParams.bruAristimunha commentedon Aug 26, 2026
Fixed in #1161 (
eb8e59d).