Skip to content

Commit 44fc1cc

Browse files
committed
DRY up
1 parent 28651f1 commit 44fc1cc

3 files changed

Lines changed: 41 additions & 38 deletions

File tree

mne/viz/epochs.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
_handle_precompute,
3030
_make_combine_callable,
3131
_make_event_color_dict,
32+
_normalize_annotation_colors,
3233
_set_title_multiple_electrodes,
3334
_set_window_title,
3435
_setup_cmap,
@@ -869,7 +870,7 @@ def plot_epochs(
869870
annotation_colors : dict | None
870871
A dictionary mapping annotation description strings to colors. Use this to
871872
override the default color assigned to specific annotation types (e.g.,
872-
``dict(bad_segment='orange')``). Colors can be any valid matplotlib color
873+
``dict(bad_segment='orange')``). Colors can be any valid Matplotlib color
873874
specification. Keys that do not match any annotation description in the data
874875
will trigger a warning. If ``None`` (default), automatic colors are used.
875876
@@ -1026,24 +1027,9 @@ def plot_epochs(
10261027

10271028
# handle annotation_colors
10281029
if annotation_colors is not None:
1029-
from matplotlib.colors import to_hex
1030-
1031-
_validate_type(annotation_colors, dict, "annotation_colors")
1032-
normalized = {}
1033-
for k, v in annotation_colors.items():
1034-
try:
1035-
normalized[k] = to_hex(v)
1036-
except ValueError:
1037-
raise ValueError(
1038-
f"annotation_colors[{k!r}] is not a valid matplotlib color: {v!r}"
1039-
) from None
1040-
unknown = set(normalized) - set(epochs.annotations.description)
1041-
if unknown:
1042-
warn(
1043-
"The following annotation_colors keys do not match any annotation "
1044-
f"description in the data: {sorted(unknown)}"
1045-
)
1046-
annotation_colors = normalized
1030+
annotation_colors = _normalize_annotation_colors(
1031+
annotation_colors, epochs.annotations
1032+
)
10471033

10481034
params = dict(
10491035
inst=epochs,

mne/viz/raw.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
_handle_decim,
2828
_handle_precompute,
2929
_make_event_color_dict,
30+
_normalize_annotation_colors,
3031
_shorten_path_from_middle,
3132
)
3233

@@ -115,7 +116,7 @@ def plot_raw(
115116
annotation_colors : dict | None
116117
A dictionary mapping annotation description strings to colors. Use this to
117118
override the default color assigned to specific annotation types (e.g.,
118-
``dict(bad_segment='orange')``). Colors can be any valid matplotlib color
119+
``dict(bad_segment='orange')``). Colors can be any valid Matplotlib color
119120
specification. Keys that do not match any annotation description in the data
120121
will trigger a warning. If ``None`` (default), automatic colors are used.
121122
@@ -353,24 +354,9 @@ def plot_raw(
353354

354355
# handle annotation_colors
355356
if annotation_colors is not None:
356-
from matplotlib.colors import to_hex
357-
358-
_validate_type(annotation_colors, dict, "annotation_colors")
359-
normalized = {}
360-
for k, v in annotation_colors.items():
361-
try:
362-
normalized[k] = to_hex(v)
363-
except ValueError:
364-
raise ValueError(
365-
f"annotation_colors[{k!r}] is not a valid matplotlib color: {v!r}"
366-
) from None
367-
unknown = set(normalized) - set(raw.annotations.description)
368-
if unknown:
369-
warn(
370-
"The following annotation_colors keys do not match any "
371-
f"annotation description in the data: {sorted(unknown)}"
372-
)
373-
annotation_colors = normalized
357+
annotation_colors = _normalize_annotation_colors(
358+
annotation_colors, raw.annotations
359+
)
374360

375361
# handle event colors
376362
event_color_dict = _make_event_color_dict(event_color, events, event_id)

mne/viz/utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,3 +2866,34 @@ def _get_plot_ch_type(inst, ch_type, allow_ref_meg=False):
28662866
f"No plottable channel types found. Allowed types are: {allowed_types}"
28672867
)
28682868
return ch_type
2869+
2870+
2871+
def _normalize_annotation_colors(annotation_colors, annotations):
2872+
"""Normalize annotation_colors and check that keys match annotation descriptions.
2873+
2874+
Parameters
2875+
----------
2876+
annotation_colors : dict[str, color]
2877+
The annotation colors to normalize (``color`` can be any valid Matplotlib color
2878+
specification).
2879+
annotations : mne.Annotations
2880+
The Annotations object to check against.
2881+
"""
2882+
from matplotlib.colors import to_hex
2883+
2884+
_validate_type(annotation_colors, dict, "annotation_colors")
2885+
normalized = {}
2886+
for k, v in annotation_colors.items():
2887+
try:
2888+
normalized[k] = to_hex(v)
2889+
except ValueError:
2890+
raise ValueError(
2891+
f"annotation_colors[{k!r}] is not a valid matplotlib color: {v!r}"
2892+
) from None
2893+
unknown = set(normalized) - set(annotations.description)
2894+
if unknown:
2895+
warn(
2896+
"The following annotation_colors keys do not match any annotation "
2897+
f"description in the data: {sorted(unknown)}"
2898+
)
2899+
return normalized

0 commit comments

Comments
 (0)