66
77from copy import deepcopy
88from functools import partial
9+ from inspect import signature
910
1011import numpy as np
1112from scipy import ndimage
1213
1314from .._fiff .pick import _FNIRS_CH_TYPES_SPLIT , _picks_to_idx , channel_type , pick_types
1415from ..defaults import _handle_default
1516from ..utils import Bunch , _check_option , _clean_names , _is_numeric , _to_rgb , fill_doc
16- from .ui_events import ChannelsSelect , publish , subscribe
17+ from .ui_events import (
18+ ChannelsSelect ,
19+ TimeChange ,
20+ link ,
21+ publish ,
22+ subscribe ,
23+ )
1724from .utils import (
1825 DraggableColorbar ,
1926 SelectFromCollection ,
@@ -57,7 +64,7 @@ def iter_topography(
5764 on_pick : callable | None
5865 The callback function to be invoked on clicking one
5966 of the axes. Is supposed to instantiate the following
60- API: ``function(axis, channel_index)``.
67+ API: ``function(axis, channel_index, orig_fig )``.
6168 fig : matplotlib.figure.Figure | None
6269 The figure object to be considered. If None, a new
6370 figure will be created.
@@ -413,14 +420,18 @@ def _plot_topo_onpick(event, show_func):
413420 return
414421 ch_idx = orig_ax ._mne_ch_idx
415422 face_color = orig_ax ._mne_ax_face_color
416- fig , ax = plt .subplots (1 )
423+ subfig , ax = plt .subplots (1 )
417424
418425 plt .title (orig_ax ._mne_ch_name )
419426 ax .set_facecolor (face_color )
420427
421428 # allow custom function to override parameters
422- show_func (ax , ch_idx )
423- plt_show (fig = fig )
429+ if "orig_fig" in signature (show_func ).parameters :
430+ show_func (ax , ch_idx , orig_fig = fig )
431+ plt_show (fig = subfig )
432+ else :
433+ show_func (ax , ch_idx )
434+ plt_show (fig = fig )
424435
425436 except Exception as err :
426437 # matplotlib silently ignores exceptions in event handlers,
@@ -568,6 +579,7 @@ def _plot_timeseries(
568579 hline = None ,
569580 hvline_color = "w" ,
570581 labels = None ,
582+ orig_fig = None ,
571583):
572584 """Show time series on topo split across multiple axes."""
573585 import matplotlib .pyplot as plt
@@ -627,7 +639,7 @@ def _cursor_vline(event):
627639 return
628640 if ax ._cursorline is not None :
629641 ax ._cursorline .remove ()
630- ax ._cursorline = ax .axvline (event .xdata , color = ax ._cursorcolor )
642+ ax ._cursorline = ax .axvline (event .xdata , color = ax ._cursorcolor , alpha = 0.2 )
631643 ax .figure .canvas .draw ()
632644
633645 def _rm_cursor (event ):
@@ -637,14 +649,35 @@ def _rm_cursor(event):
637649 ax ._cursorline = None
638650 ax .figure .canvas .draw ()
639651
652+ def _on_click (event ):
653+ if event .inaxes == ax :
654+ publish (ax .figure , TimeChange (time = event .xdata ))
655+
656+ def _on_time_change_sub (event ):
657+ _update_selectline (event .time )
658+
659+ def _update_selectline (time ):
660+ if ax ._selectline is not None :
661+ ax ._selectline .remove ()
662+ ax ._selectline = ax .axvline (time , color = ax ._selectcolor , alpha = 1 )
663+ ax .figure .canvas .draw ()
664+
640665 ax ._cursorline = None
641666 # choose cursor color based on perceived brightness of background
642667 facecol = _to_rgb (ax .get_facecolor ())
643668 face_brightness = np .dot (facecol , [299 , 587 , 114 ])
644669 ax ._cursorcolor = "white" if face_brightness < 150 else "black"
645670
671+ ax ._selectline = None
672+ ax ._selectcolor = "white" if face_brightness < 150 else "black"
673+
646674 plt .connect ("motion_notify_event" , _cursor_vline )
647675 plt .connect ("axes_leave_event" , _rm_cursor )
676+ plt .connect ("button_press_event" , _on_click )
677+
678+ subscribe (ax .figure , "time_change" , _on_time_change_sub )
679+
680+ link (orig_fig , ax .figure , recursive = True )
648681
649682 ymin , ymax = ax .get_ylim ()
650683 # don't pass vline or hline here (this fxn doesn't do hvline_color):
@@ -1139,6 +1172,14 @@ def _plot_evoked_topo(
11391172 select = select ,
11401173 )
11411174
1175+ setattr (fig , "_current_time" , None )
1176+
1177+ def on_time_change (event , fig ):
1178+ """Respond to a time change UI event."""
1179+ fig ._current_time = event .time
1180+
1181+ subscribe (fig , "time_change" , partial (on_time_change , fig = fig ))
1182+
11421183 add_background_image (fig , fig_background )
11431184
11441185 if legend is not False :
0 commit comments