Skip to content

Commit

Permalink
MRG, BUG, VIZ: Fix overlay times (mne-tools#8377)
Browse files Browse the repository at this point in the history
* fix bug

* fstrings

* style

* safer?

* what's new

* touch tutorial to re-render
  • Loading branch information
drammock committed Oct 15, 2020
1 parent 49ac85b commit 6bd3969
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 2 additions & 0 deletions doc/changes/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
4 changes: 2 additions & 2 deletions examples/visualization/plot_roi_erpimage_by_rt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

###############################################################################
Expand Down
28 changes: 13 additions & 15 deletions mne/viz/epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.:
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 6bd3969

Please sign in to comment.