Skip to content

Commit ddcefca

Browse files
authored
ENH: Show the current time as a vertical line in plot_evoked_topo (#14032)
1 parent 25ff13e commit ddcefca

4 files changed

Lines changed: 45 additions & 20 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :func:`mne.viz.plot_evoked_topo` show the currently select time with a solid vertical line also in the main figure, by `Marijn van Vliet`_.

examples/visualization/topo_compare_conditions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
Compare evoked responses for different conditions
66
=================================================
77
8-
In this example, an Epochs object for visual and auditory responses is created.
9-
Both conditions are then accessed by their respective names to create a sensor
10-
layout plot of the related evoked responses.
8+
In this example, an Epochs object for visual and auditory responses is created. Both
9+
conditions are then accessed by their respective names to create a sensor layout plot of
10+
the related evoked responses.
1111
1212
"""
1313

mne/viz/tests/test_topo.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010
import numpy as np
1111
import pytest
1212

13-
from mne import (
14-
Epochs,
15-
compute_proj_evoked,
16-
read_cov,
17-
read_events,
18-
)
13+
from mne import Epochs, compute_proj_evoked, read_cov, read_events
1914
from mne.channels import read_layout
2015
from mne.io import read_raw_fif
2116
from mne.time_frequency.tfr import AverageTFRArray
@@ -355,18 +350,24 @@ def test_plot_topo_timechange():
355350
_fake_click(subfig, ax, (0.5, 0.5), kind="press")
356351
init_time = ax._selectline.get_xdata()[0]
357352
assert init_time == 0.0
358-
359-
# test existence of _current_time in main figure
360353
assert hasattr(fig, "_current_time")
361354
assert fig._current_time == 0.0
355+
assert hasattr(fig.axes[0]._mne_axs[0], "time_cursor")
362356

363357
# test time change event from subfig and main fig
364358
ui_events.publish(subfig, ui_events.TimeChange(time=0.1))
365359
assert ax._selectline.get_xdata()[0] == 0.1
366360
assert fig._current_time == 0.1
361+
for subax in fig.axes[0]._mne_axs:
362+
time = 0.1 * subax.x_s + subax.x_t
363+
assert subax.time_cursor.get_xdata() == [time, time]
364+
367365
ui_events.publish(subfig, ui_events.TimeChange(time=0.2))
368366
assert ax._selectline.get_xdata()[0] == 0.2
369367
assert fig._current_time == 0.2
368+
for subax in fig.axes[0]._mne_axs:
369+
time = 0.2 * subax.x_s + subax.x_t
370+
assert subax.time_cursor.get_xdata() == [time, time]
370371

371372
plt.close("all")
372373

mne/viz/topo.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@
1414
from .._fiff.pick import _FNIRS_CH_TYPES_SPLIT, _picks_to_idx, channel_type, pick_types
1515
from ..defaults import _handle_default
1616
from ..utils import Bunch, _check_option, _clean_names, _is_numeric, _to_rgb, fill_doc
17-
from .ui_events import (
18-
ChannelsSelect,
19-
TimeChange,
20-
link,
21-
publish,
22-
subscribe,
23-
)
17+
from .ui_events import ChannelsSelect, TimeChange, link, publish, subscribe
2418
from .utils import (
2519
DraggableColorbar,
2620
SelectFromCollection,
@@ -670,6 +664,8 @@ def _update_selectline(time):
670664

671665
ax._selectline = None
672666
ax._selectcolor = "white" if face_brightness < 150 else "black"
667+
if orig_fig._current_time is not None:
668+
_update_selectline(orig_fig._current_time)
673669

674670
plt.connect("motion_notify_event", _cursor_vline)
675671
plt.connect("axes_leave_event", _rm_cursor)
@@ -1174,11 +1170,38 @@ def _plot_evoked_topo(
11741170

11751171
setattr(fig, "_current_time", None)
11761172

1177-
def on_time_change(event, fig):
1173+
def _on_time_change(event, fig, tmin, tmax):
11781174
"""Respond to a time change UI event."""
11791175
fig._current_time = event.time
11801176

1181-
subscribe(fig, "time_change", partial(on_time_change, fig=fig))
1177+
# Draw a vertical line at the current time.
1178+
axs = fig.axes[0]._mne_axs
1179+
for subax in axs:
1180+
time = subax.x_s * event.time + subax.x_t
1181+
if "time_cursor" not in subax:
1182+
subax["time_cursor"] = subax.ax.axvline(
1183+
time,
1184+
subax.pos[1],
1185+
subax.pos[1] + subax.pos[3],
1186+
color=font_color,
1187+
linewidth=0.5,
1188+
)
1189+
else:
1190+
subax.time_cursor.set_xdata([time, time])
1191+
# Hide the vertical line when the time is out of bounds.
1192+
subax.time_cursor.set_visible(tmin <= event.time <= tmax)
1193+
fig.canvas.draw()
1194+
1195+
subscribe(
1196+
fig,
1197+
"time_change",
1198+
partial(
1199+
_on_time_change,
1200+
fig=fig,
1201+
tmin=np.min([t[0] for t in times]),
1202+
tmax=np.max([t[-1] for t in times]),
1203+
),
1204+
)
11821205

11831206
add_background_image(fig, fig_background)
11841207

0 commit comments

Comments
 (0)