8000 BUG: Fix bug with example (#13210) · mne-tools/mne-python@855cb53 · GitHub
[go: up one dir, main page]

Skip to content

Commit 855cb53

Browse files
larsonerdrammock
andauthored
BUG: Fix bug with example (#13210)
Co-authored-by: Daniel McCloy <dan@mccloy.info>
1 parent 8ea69e1 commit 855cb53

File tree

6 files changed

+52
-23
lines changed

6 files changed

+52
-23
lines changed

examples/datasets/opm_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,6 @@
149149
subjects_dir=subjects_dir,
150150
initial_time=dip_opm.times[idx],
151151
clim=dict(kind="percent", lims=[99, 99.9, 99.99]),
152-
size=(400, 300),
152+
size=(800, 600),
153153
background="w",
154154
)

mne/viz/_brain/_brain.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,8 @@ def setup_time_viewer(self, time_viewer=True, show_traces=True):
531531
self.label_extract_mode = None
532532
all_keys = ("lh", "rh", "vol")
533533
self.act_data_smooth = {key: (None, None) for key in all_keys}
534-
self.color_list = _get_color_list()
535534
# remove grey for better contrast on the brain
536-
self.color_list.remove("#7f7f7f")
535+
self.color_list = _get_color_list(remove=("#7f7f7f",))
537536
self.color_cycle = _ReuseCycle(self.color_list)
538537
self.mpl_canvas = None
539538
self.help_canvas = None

mne/viz/_figure.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ def _setup_annotation_colors(self):
167167
"""Set up colors for annotations; init some annotation vars."""
168168
segment_colors = getattr(self.mne, "annotation_segment_colors", dict())
169169
labels = self._get_annotation_labels()
170-
colors, red = _get_color_list(annotations=True)
170+
red = "#ff0000"
171+
colors = _get_color_list(remove=("#fa8174", "#d62728", "#ff0000"))
171172
color_cycle = cycle(colors)
172173
for key, color in segment_colors.items():
173174
if color != red and key in labels:

mne/viz/tests/test_raw.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import matplotlib.pyplot as plt
1111
import numpy as np
1212
import pytest
13-
from matplotlib import backend_bases
13+
from matplotlib import backend_bases, rc_context
1414
from numpy.testing import assert_allclose, assert_array_equal
1515

1616
import mne
@@ -23,6 +23,7 @@
2323
_assert_no_instances,
2424
_dt_to_stamp,
2525
_record_warnings,
26+
catch_logging,
2627
check_version,
2728
get_config,
2829
set_config,
@@ -49,6 +50,8 @@ def _get_button_xy(buttons, idx):
4950

5051
def _annotation_helper(raw, browse_backend, events=False):
5152
"""Test interactive annotations."""
53+
from cycler import cycler # dep of matplotlib, should be okay but nest just in case
54+
5255
ismpl = browse_backend.name == "matplotlib"
5356
# Some of our checks here require modern mpl to work properly
5457
n_anns = len(raw.annotations)
@@ -60,7 +63,27 @@ def _annotation_helper(raw, browse_backend, events=False):
6063
else:
6164
events = None
6265
n_events = 0
63-
fig = raw.plot(events=events)
66+
# matplotlib 2.0 default cycler
67+
default_cycler = cycler(
68+
"color",
69+
[
70+
"#1f77b4",
71+
"#ff7f0e",
72+
"#2ca02c",
73+
"#d62728",
74+
"#9467bd",
75+
"#8c564b",
76+
"#e377c2",
77+
"#7f7f7f",
78+
"#bcbd22",
79+
"#17becf",
80+
],
81+
) # noqa: E501
82+
with catch_logging("debug") as log, rc_context({"axes.prop_cycle": default_cycler}):
83+
fig = raw.plot(events=events)
84+
log = log.getvalue()
85+
assert "Removing from color cycle: #d62728" in log
86+
assert log.count("Removing from color cycle") == 1
6487
if ismpl:
6588
assert browse_backend._get_n_figs() == 1
6689

mne/viz/tests/test_utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import matplotlib.pyplot as plt
88
import numpy as np
99
import pytest
10+
from cycler import cycler
11+
from matplotlib import rc_context
1012
from numpy.testing import assert_allclose
1113

1214
from mne import read_evokeds
@@ -46,10 +48,14 @@ def test_setup_vmin_vmax_warns():
4648

4749
def test_get_color_list():
4850
"""Test getting a colormap from rcParams."""
49-
colors = _get_color_list()
50-
assert isinstance(colors, list)
51-
colors_no_red = _get_color_list(annotations=True)
52-
assert "#ff0000" not in colors_no_red
51+
with rc_context({"axes.prop_cycle": cycler(color=["#ff0000", "#00ff00"])}):
52+
colors = _get_color_list()
53+
assert isinstance(colors, list)
54+
assert len(colors) == 2
55+
assert "#ff0000" in colors
56+
colors_no_red = _get_color_list(remove=("#ff0000",))
57+
assert "#ff0000" not in colors_no_red
58+
assert len(colors_no_red) == 1
5359

5460

5561
def test_mne_analyze_colormap():

mne/viz/utils.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,33 +1773,33 @@ def disconnect(self):
17731773
self.canvas.draw_idle()
17741774

17751775

1776-
def _get_color_list(annotations=False):
1776+
def _get_color_list(*, remove=None):
17771777
"""Get the current color list from matplotlib rcParams.
17781778
17791779
Parameters
17801780
----------
1781-
annotations : boolean
1782-
Has no influence on the function if false. If true, check if color
1783-
"red" (#ff0000) is in the cycle and remove it.
1781+
remove : tuple of str | None
1782+
Has no influence on the function if None. Can be a list of colors to
1783+
remove from the list if within 1/255 of the color.
17841784
17851785
Returns
17861786
-------
17871787
colors : list
17881788
"""
17891789
from matplotlib import rcParams
1790+
from matplotlib.colors import to_rgba_array
17901791

17911792
color_cycle = rcParams.get("axes.prop_cycle")
17921793
colors = color_cycle.by_key()["color"]
17931794

1794-
# If we want annotations, red is reserved ... remove if present. This
1795-
# checks for the reddish color in MPL dark background style, normal style,
1796-
# and MPL "red", and defaults to the last of those if none are present
1797-
for red in ("#fa8174", "#d62728", "#ff0000"):
1798-
if annotations and red in colors:
1799-
colors.remove(red)
1800-
break
1801-
1802-
return (colors, red) if annotations else colors
1795+
colors_cast = to_rgba_array(colors)[:, :3]
1796+
atol = 1.5 / 255.0
1797+
for rem in to_rgba_array(remove or [])[:, :3]:
1798+
matches = np.where(np.isclose(colors_cast, rem, atol=atol).all(-1))[0][::-1]
1799+
for idx in matches:
1800+
logger.debug(f"Removing from color cycle: {colors[idx]}")
1801+
colors.pop(idx)
1802+
return colors
18031803

18041804

18051805
def _merge_annotations(start, stop, description, annotations, current=()):

0 commit comments

Comments
 (0)
0