From 6bd39692607d0fc2f8a04ffbb7f14f14ac9b5370 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Thu, 15 Oct 2020 14:36:23 -0500 Subject: [PATCH] MRG, BUG, VIZ: Fix overlay times (#8377) * fix bug * fstrings * style * safer? * what's new * touch tutorial to re-render --- doc/changes/latest.inc | 2 ++ .../visualization/plot_roi_erpimage_by_rt.py | 4 +-- mne/viz/epochs.py | 28 +++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/doc/changes/latest.inc b/doc/changes/latest.inc index 7ac34775954..ac8fc6a7b00 100644 --- a/doc/changes/latest.inc +++ b/doc/changes/latest.inc @@ -27,6 +27,8 @@ Bugs ~~~~ - Fix bug with reading split files that have dashes in the filename **by new contributor** |Eduard Ort|_ (:gh:`8339`) +- Fix bug with `~mne.viz.plot_epochs_image` when ``order`` is supplied and multiple conditions are plotted by `Daniel McCloy`_ (:gh:`8377`) + - Fix bug with :func:`mne.viz.plot_source_estimates` when using the PyVista backend where singleton time points were not handled properly by `Eric Larson`_ (:gh:`8285`) - Fix bug when passing ``axes`` to plotting functions, :func:`matplotlib.pyplot.tight_layout` will not be called when the figure was created using a constrained layout by `Eric Larson`_ (:gh:`8344`) diff --git a/examples/visualization/plot_roi_erpimage_by_rt.py b/examples/visualization/plot_roi_erpimage_by_rt.py index 39fca546001..c88e2fd6b05 100644 --- a/examples/visualization/plot_roi_erpimage_by_rt.py +++ b/examples/visualization/plot_roi_erpimage_by_rt.py @@ -54,13 +54,13 @@ # define target events: # 1. find response times: distance between "square" and "rt" events # 2. extract A. "square" events B. followed by a button press within 700 msec -tmax = .7 +tmax = 0.7 sfreq = raw.info["sfreq"] reference_id, target_id = 2, 1 new_events, rts = define_target_events(events, reference_id, target_id, sfreq, tmin=0., tmax=tmax, new_id=2) -epochs = mne.Epochs(raw, events=new_events, tmax=tmax + .1, +epochs = mne.Epochs(raw, events=new_events, tmax=tmax + 0.1, event_id={"square": 2}) ############################################################################### diff --git a/mne/viz/epochs.py b/mne/viz/epochs.py index cce5a80dc4a..9120db09621 100644 --- a/mne/viz/epochs.py +++ b/mne/viz/epochs.py @@ -289,8 +289,8 @@ def plot_epochs_image(epochs, picks=None, sigma=0., vmin=None, this_image = combine_func(this_data * scalings[this_ch_type]) # handle `order`. NB: this can potentially yield different orderings # in each figure! - this_image, overlay_times = _order_epochs(this_image, epochs.times, - order, overlay_times) + this_image, _overlay_times = _order_epochs(this_image, epochs.times, + order, overlay_times) this_norm = np.all(this_image > 0) # apply smoothing if sigma > 0.: @@ -325,7 +325,7 @@ def plot_epochs_image(epochs, picks=None, sigma=0., vmin=None, if isinstance(combine, str) and len(title): _comb = combine.upper() if combine == 'gfp' else combine _comb = 'std. dev.' if _comb == 'std' else _comb - title += ' ({})'.format(_comb) + title += f' ({_comb})' # plot the image this_fig = _plot_epochs_image( @@ -334,7 +334,7 @@ def plot_epochs_image(epochs, picks=None, sigma=0., vmin=None, style_axes=True, norm=this_group_dict['norm'], unit=units[this_ch_type], ax=this_axes_dict, show=False, title=title, combine=combine, combine_given=combine_given, - overlay_times=overlay_times, evoked=evoked, ts_args=ts_args) + overlay_times=_overlay_times, evoked=evoked, ts_args=ts_args) group_by[this_group].update(fig=this_fig) # detect ylims across figures @@ -478,27 +478,25 @@ def _order_epochs(data, times, order=None, overlay_times=None): if overlay_times is not None: if len(overlay_times) != n_epochs: - raise ValueError('size of overlay_times parameter ({}) does not ' - 'match the number of epochs ({}).' - .format(len(overlay_times), n_epochs)) + raise ValueError( + f'size of overlay_times parameter ({len(overlay_times)}) does ' + f'not match the number of epochs ({n_epochs}).') overlay_times = np.array(overlay_times) times_min = np.min(overlay_times) times_max = np.max(overlay_times) - if ((times_min < times[0]) or (times_max > times[-1])): + if (times_min < times[0]) or (times_max > times[-1]): warn('Some values in overlay_times fall outside of the epochs ' - 'time interval (between %s s and %s s)' - % (times[0], times[-1])) + f'time interval (between {times[0]} s and {times[-1]} s)') if callable(order): order = order(times, data) if order is not None: if len(order) != n_epochs: - raise ValueError('If order is a {}, its length ({}) must match ' - 'the length of the data ({}).' - .format(type(order).__name__, len(order), - n_epochs)) - order = np.asarray(order) + raise ValueError(f'If order is a {type(order).__name__}, its ' + f'length ({len(order)}) must match the length of ' + f'the data ({n_epochs}).') + order = np.array(order) data = data[order] if overlay_times is not None: overlay_times = overlay_times[order]