Skip to content

Commit c66550e

Browse files
authored
Add title for RawArray (#14181)
1 parent d030bd5 commit c66550e

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

mne/viz/raw.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
_get_stim_channel,
1717
_validate_type,
1818
legacy,
19+
sizeof_fmt,
1920
verbose,
2021
)
2122
from ..utils.spectrum import _split_psd_kwargs
@@ -137,8 +138,9 @@ def plot_raw(
137138
show_options : bool
138139
If True, a dialog for options related to projection is shown.
139140
title : str | None
140-
The title of the window. If None, and either the filename of the
141-
raw object or '<unknown>' will be displayed as title.
141+
The title of the window. If None, the filename of the raw object is
142+
used; for in-memory instances without a filename (e.g.,
143+
`~mne.io.RawArray`), the class name and approximate size are used.
142144
show : bool
143145
Show figure if True.
144146
block : bool
@@ -369,16 +371,18 @@ def plot_raw(
369371
start += first_time
370372
event_id_rev = {v: k for k, v in (event_id or {}).items()}
371373

372-
# generate window title; allow instances without a filename (e.g., ICA)
374+
# generate window title; allow instances without a filename (e.g., RawArray)
373375
if title is None:
374-
title = "<unknown>"
375-
fnames = list(tuple(raw.filenames)) # get a list of a copy of the filenames
376+
# in-memory instances (e.g., RawArray) have filenames of (None,)
377+
fnames = [fname for fname in raw.filenames if fname is not None]
376378
if len(fnames):
377379
title = fnames.pop(0)
378380
extra = f" ... (+ {len(fnames)} more)" if len(fnames) else ""
379381
title = f"{title}{extra}"
380382
if len(title) > 60:
381383
title = _shorten_path_from_middle(title)
384+
else: # give at least a hint about the data being shown
385+
title = f"{type(raw).__name__} (~{sizeof_fmt(raw._size)})"
382386
elif not isinstance(title, str):
383387
raise TypeError(f"title must be None or a string, got a {type(title)}")
384388

mne/viz/tests/test_raw.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import itertools
66
import os
7+
import re
78
from copy import deepcopy
89
from pathlib import Path
910

@@ -747,6 +748,16 @@ def test_plot_raw_traces(raw, events, browser_backend):
747748
raw.plot(order="foo")
748749
with pytest.raises(TypeError, match="title must be None or a string, got"):
749750
raw.plot(title=1)
751+
# in-memory raw has filenames == (None,); title should fall back to class + size
752+
fig = RawArray(raw.get_data(), raw.info).plot()
753+
if browser_backend.name != "matplotlib":
754+
title = fig.windowTitle()
755+
elif check_version("matplotlib", "3.10.3"):
756+
title = fig.canvas.manager.get_window_title()
757+
else: # matplotlib < 3.10.3 hard-codes "image" for non-GUI window titles
758+
title = None
759+
if title is not None:
760+
assert re.match(r"RawArray \(~[\d.]+ (bytes|[KMGT]iB)\)", title), title
750761
raw.plot(show_options=True)
751762
browser_backend._close_all()
752763

0 commit comments

Comments
 (0)