Skip to content

Commit

Permalink
MRG Evoked plots should error nicely on NaN input (mne-tools#5398)
Browse files Browse the repository at this point in the history
  • Loading branch information
jona-sassenhagen authored and agramfort committed Aug 6, 2018
1 parent 5ae2ad3 commit 80dfec2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion mne/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2751,7 +2751,7 @@ def _validate_type(item, types=None, item_name=None, type_name=None):


def linkcode_resolve(domain, info):
"""Determine the URL corresponding to Python object.
"""Determine the URL corresponding to a Python object.
Parameters
----------
Expand Down Expand Up @@ -2821,3 +2821,9 @@ def linkcode_resolve(domain, info):
kind = 'maint/%s' % ('.'.join(mne.__version__.split('.')[:2]))
return "http://github.com/mne-tools/mne-python/blob/%s/mne/%s%s" % ( # noqa
kind, fn, linespec)


def _check_if_nan(data, msg=" to be plotted"):
"""Raise if any of the values are NaN."""
if not np.isfinite(data).all():
raise ValueError("Some of the values {} are NaN.".format(msg))
9 changes: 7 additions & 2 deletions mne/viz/evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
_setup_plot_projector, _prepare_joint_axes,
_set_title_multiple_electrodes, _check_time_unit,
_plot_masked_image)
from ..utils import logger, _clean_names, warn, _pl, verbose, _validate_type
from ..utils import (logger, _clean_names, warn, _pl, verbose, _validate_type,
_check_if_nan)

from .topo import _plot_evoked_topo
from .topomap import (_prepare_topo_plot, plot_topomap, _check_outlines,
Expand Down Expand Up @@ -409,6 +410,7 @@ def _plot_lines(data, info, picks, fig, axes, spatial_colors, unit, units,
if len(idx) > 0:
# Set amplitude scaling
D = this_scaling * data[idx, :]
_check_if_nan(D)
gfp_only = (isinstance(gfp, string_types) and gfp == 'only')
if not gfp_only:
chs = [info['chs'][i] for i in idx]
Expand Down Expand Up @@ -577,6 +579,8 @@ def _plot_image(data, ax, this_type, picks, cmap, unit, units, scalings, times,
else:
vmin, vmax = ylim[this_type]

_check_if_nan(data)

im, t_end = _plot_masked_image(
ax, data, times, mask, picks=None, yvals=None, cmap=cmap[0],
vmin=vmin, vmax=vmax, mask_style=mask_style, mask_alpha=mask_alpha,
Expand Down Expand Up @@ -1989,7 +1993,8 @@ def plot_compare_evokeds(evokeds, picks=None, gfp=False, colors=None,
if _ci_fun is not None: # compute CI if requested:
ci_dict[cond] = _ci_fun(data)
# average across conditions:
data_dict[cond] = np.mean(data, axis=0)
data_dict[cond] = data = np.mean(data, axis=0)
_check_if_nan(data)

del evokeds

Expand Down
7 changes: 7 additions & 0 deletions mne/viz/tests/test_evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ def test_plot_evoked():

evoked.plot_image(proj=True, time_unit='ms')

# fail nicely on NaN
evoked_nan = evoked.copy()
evoked_nan.data[:, 0] = np.nan
pytest.raises(ValueError, evoked_nan.plot)
pytest.raises(ValueError, evoked_nan.plot_image)
pytest.raises(ValueError, evoked_nan.plot_joint)

# test mask
evoked.plot_image(picks=[1, 2], mask=evoked.data > 0, time_unit='s')
evoked.plot_image(picks=[1, 2], mask_cmap=None, colorbar=False,
Expand Down

0 comments on commit 80dfec2

Please sign in to comment.