Skip to content

Commit

Permalink
MRg Fix ERP sensor legend plotting (mne-tools#5634)
Browse files Browse the repository at this point in the history
* fix regression

* add test
  • Loading branch information
jona-sassenhagen authored and jasmainak committed Oct 20, 2018
1 parent 92380dc commit 4c670aa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion mne/channels/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ def _auto_topomap_coords(info, picks, ignore_overlap=False, to_sphere=True):
locs3d = np.array([ch['loc'][:3] for ch in chs])

# If electrode locations are not available, use digization points
if _check_ch_locs(chs):
if not _check_ch_locs(chs):
logging.warning('Did not find any electrode locations (in the info '
'object), will attempt to use digitization points '
'instead. However, if digitization points do not '
Expand Down
5 changes: 3 additions & 2 deletions mne/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,8 +2388,9 @@ def _check_ch_locs(chs):
The channels from info['chs']
"""
locs3d = np.array([ch['loc'][:3] for ch in chs])
return (locs3d == 0).all() or \
(~np.isfinite(locs3d)).all() or np.allclose(locs3d, 0.)
return not ((locs3d == 0).all() or
(~np.isfinite(locs3d)).all() or
np.allclose(locs3d, 0.))


def _clean_names(names, remove_whitespace=False, before_dash=True):
Expand Down
21 changes: 10 additions & 11 deletions mne/viz/evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def _plot_lines(data, info, picks, fig, axes, spatial_colors, unit, units,
if not gfp_only:
chs = [info['chs'][i] for i in idx]
locs3d = np.array([ch['loc'][:3] for ch in chs])
if spatial_colors is True and _check_ch_locs(chs):
if spatial_colors is True and not _check_ch_locs(chs):
warn('Channel locations not available. Disabling spatial '
'colors.')
spatial_colors = selectable = False
Expand Down Expand Up @@ -2122,22 +2122,21 @@ def plot_compare_evokeds(evokeds, picks=None, gfp=False, colors=None,
# and now for 3 "legends" ..
# a head plot showing the sensors that are being plotted
if show_sensors:
if not _check_ch_locs(one_evoked.info['chs']):
pos = _auto_topomap_coords(one_evoked.info, pos_picks,
ignore_overlap=True, to_sphere=True)
else:
_validate_type(show_sensors, (np.int, bool, str, type(None)),
"show_sensors", "numeric, str, None or bool")
if not _check_ch_locs(np.array(one_evoked.info['chs'])[pos_picks]):
warn("Cannot find channel coordinates in the supplied Evokeds. "
"Not showing channel locations.")

if show_sensors is True:
ymin, ymax = np.abs(ax.get_ylim())
show_sensors = "lower right" if ymin > ymax else "upper right"
else:
if show_sensors is True:
ymin, ymax = np.abs(ax.get_ylim())
show_sensors = "lower right" if ymin > ymax else "upper right"

pos = _auto_topomap_coords(one_evoked.info, pos_picks,
ignore_overlap=True, to_sphere=True)
head_pos = {'center': (0, 0), 'scale': (0.5, 0.5)}
pos, outlines = _check_outlines(pos, np.array([1, 1]), head_pos)

_validate_type(show_sensors, (np.int, bool, str),
"show_sensors", "numeric, str or bool")
show_sensors = _check_loc_legal(show_sensors, "show_sensors")
_plot_legend(pos, ["k"] * len(picks), ax, list(), outlines,
show_sensors, size=25)
Expand Down
5 changes: 4 additions & 1 deletion mne/viz/tests/test_evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ def test_plot_evoked():
evoked.plot_white([cov, cov], time_unit='s')

# plot_compare_evokeds: test condition contrast, CI, color assignment
plot_compare_evokeds(evoked.copy().pick_types(meg='mag'))
fig = plot_compare_evokeds(evoked.copy().pick_types(meg='mag'),
show_sensors=True)
assert len(fig.axes) == 2

plot_compare_evokeds(
evoked.copy().pick_types(meg='grad'), picks=[1, 2],
show_sensors="upper right", show_legend="upper left")
Expand Down

0 comments on commit 4c670aa

Please sign in to comment.