Skip to content

Commit 248fe7e

Browse files
authored
MRG: Remove unused argument and refactor (mne-tools#5140)
* FIX: Remove unused argument and refactor * FIX: Move _plot_corrmap
1 parent 1dc1502 commit 248fe7e

5 files changed

Lines changed: 108 additions & 145 deletions

File tree

doc/whats_new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ API
189189

190190
- Channels with unknown locations are now assigned position ``[np.nan, np.nan, np.nan]`` instead of ``[0., 0., 0.]``, by `Eric Larson`_
191191

192+
- Removed unused ``image_mask`` argument from :func:`mne.viz.plot_topomap` by `Eric Larson`_
193+
192194
- Unknown measurement dates are now stored as ``info['meas_date'] = None`` rather than using the current date. ``None`` is also now used when anonymizing data and when determining the machine ID for writing files, by `Mainak Jas`_ and `Eric Larson`_
193195

194196
- :meth:`mne.Evoked.plot` will now append the number of epochs averaged for the evoked data in the first plot title, by `Eric Larson`_

mne/preprocessing/ica.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@
3838
from ..viz import (plot_ica_components, plot_ica_scores,
3939
plot_ica_sources, plot_ica_overlay)
4040
from ..viz.ica import plot_ica_properties
41-
from ..viz.utils import (_prepare_trellis, tight_layout, plt_show,
42-
_setup_vmin_vmax)
43-
from ..viz.topomap import (_prepare_topo_plot, _check_outlines,
44-
plot_topomap, _hide_frame)
41+
from ..viz.topomap import _plot_corrmap
4542

4643
from ..channels.channels import _contains_ch_type, ContainsMixin
4744
from ..io.write import start_file, end_file, write_id
@@ -2291,61 +2288,6 @@ def _find_max_corrs(all_maps, target, threshold):
22912288
return newtarget, median_corr_with_target, sim_i_o, max_corrs
22922289

22932290

2294-
def _plot_corrmap(data, subjs, indices, ch_type, ica, label, show, outlines,
2295-
layout, cmap, contours, template=False):
2296-
"""Customize ica.plot_components for corrmap."""
2297-
if not template:
2298-
title = 'Detected components'
2299-
if label is not None:
2300-
title += ' of type ' + label
2301-
else:
2302-
title = "Supplied template"
2303-
2304-
picks = list(range(len(data)))
2305-
2306-
p = 20
2307-
if len(picks) > p: # plot components by sets of 20
2308-
n_components = len(picks)
2309-
figs = [_plot_corrmap(data[k:k + p], subjs[k:k + p],
2310-
indices[k:k + p], ch_type, ica, label, show,
2311-
outlines=outlines, layout=layout, cmap=cmap,
2312-
contours=contours)
2313-
for k in range(0, n_components, p)]
2314-
return figs
2315-
elif np.isscalar(picks):
2316-
picks = [picks]
2317-
2318-
data_picks, pos, merge_grads, names, _ = _prepare_topo_plot(
2319-
ica, ch_type, layout)
2320-
pos, outlines = _check_outlines(pos, outlines)
2321-
2322-
data = np.atleast_2d(data)
2323-
data = data[:, data_picks]
2324-
2325-
# prepare data for iteration
2326-
fig, axes = _prepare_trellis(len(picks), max_col=5)
2327-
fig.suptitle(title)
2328-
2329-
if merge_grads:
2330-
from ..channels.layout import _merge_grad_data
2331-
for ii, data_, ax, subject, idx in zip(picks, data, axes, subjs, indices):
2332-
if template:
2333-
ttl = 'Subj. {0}, {1}'.format(subject, ica._ica_names[idx])
2334-
ax.set_title(ttl, fontsize=12)
2335-
data_ = _merge_grad_data(data_) if merge_grads else data_
2336-
vmin_, vmax_ = _setup_vmin_vmax(data_, None, None)
2337-
plot_topomap(data_.flatten(), pos, vmin=vmin_, vmax=vmax_,
2338-
res=64, axes=ax, cmap=cmap, outlines=outlines,
2339-
image_mask=None, contours=contours, show=False,
2340-
image_interp='bilinear')[0]
2341-
_hide_frame(ax)
2342-
tight_layout(fig=fig)
2343-
fig.subplots_adjust(top=0.8)
2344-
fig.canvas.draw()
2345-
plt_show(show)
2346-
return fig
2347-
2348-
23492291
@verbose
23502292
def corrmap(icas, template, threshold="auto", label=None, ch_type="eeg",
23512293
plot=True, show=True, verbose=None, outlines='head', layout=None,

mne/viz/topomap.py

Lines changed: 101 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def _plot_sensors(pos_x, pos_y, sensors, ax):
476476

477477
def plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True,
478478
res=64, axes=None, names=None, show_names=False, mask=None,
479-
mask_params=None, outlines='head', image_mask=None,
479+
mask_params=None, outlines='head',
480480
contours=6, image_interp='bilinear', show=True,
481481
head_pos=None, onselect=None):
482482
"""Plot a topographic map as image.
@@ -540,9 +540,6 @@ def plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True,
540540
masking options, either directly or as a function that returns patches
541541
(required for multi-axes plots). If None, nothing will be drawn.
542542
Defaults to 'head'.
543-
image_mask : ndarray of bool, shape (res, res) | None
544-
The image mask to cover the interpolated surface. If None, it will be
545-
computed from the outline.
546543
contours : int | array of float
547544
The number of contour lines to draw. If 0, no contours will be drawn.
548545
If an array, the values represent the levels for the contours. The
@@ -572,13 +569,13 @@ def plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True,
572569
"""
573570
return _plot_topomap(data, pos, vmin, vmax, cmap, sensors, res, axes,
574571
names, show_names, mask, mask_params, outlines,
575-
image_mask, contours, image_interp, show,
572+
contours, image_interp, show,
576573
head_pos, onselect)[:2]
577574

578575

579576
def _plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True,
580577
res=64, axes=None, names=None, show_names=False, mask=None,
581-
mask_params=None, outlines='head', image_mask=None,
578+
mask_params=None, outlines='head',
582579
contours=6, image_interp='bilinear', show=True,
583580
head_pos=None, onselect=None):
584581
import matplotlib.pyplot as plt
@@ -648,20 +645,17 @@ def _plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True,
648645
cmap = 'Reds' if norm else 'RdBu_r'
649646

650647
pos, outlines = _check_outlines(pos, outlines, head_pos)
648+
assert isinstance(outlines, dict)
651649

652650
ax = axes if axes else plt.gca()
653651
pos_x, pos_y = _prepare_topomap(pos, ax)
654-
if outlines is None:
655-
xmin, xmax = pos_x.min(), pos_x.max()
656-
ymin, ymax = pos_y.min(), pos_y.max()
657-
else:
658-
xlim = np.inf, -np.inf,
659-
ylim = np.inf, -np.inf,
660-
mask_ = np.c_[outlines['mask_pos']]
661-
xmin, xmax = (np.min(np.r_[xlim[0], mask_[:, 0]]),
662-
np.max(np.r_[xlim[1], mask_[:, 0]]))
663-
ymin, ymax = (np.min(np.r_[ylim[0], mask_[:, 1]]),
664-
np.max(np.r_[ylim[1], mask_[:, 1]]))
652+
xlim = np.inf, -np.inf,
653+
ylim = np.inf, -np.inf,
654+
mask_ = np.c_[outlines['mask_pos']]
655+
xmin, xmax = (np.min(np.r_[xlim[0], mask_[:, 0]]),
656+
np.max(np.r_[xlim[1], mask_[:, 0]]))
657+
ymin, ymax = (np.min(np.r_[ylim[0], mask_[:, 1]]),
658+
np.max(np.r_[ylim[1], mask_[:, 1]]))
665659

666660
# interpolate data
667661
xi = np.linspace(xmin, xmax, res)
@@ -670,27 +664,30 @@ def _plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True,
670664
interp = _GridData(np.array((pos_x, pos_y)).T).set_values(data)
671665
Zi = interp.set_locations(Xi, Yi)()
672666

673-
if outlines is None:
674-
_is_default_outlines = False
675-
elif isinstance(outlines, dict):
676-
_is_default_outlines = any(k.startswith('head') for k in outlines)
667+
_use_default_outlines = any(k.startswith('head') for k in outlines)
677668

678-
if _is_default_outlines and image_mask is None:
669+
if _use_default_outlines:
679670
# prepare masking
680-
image_mask, pos = _make_image_mask(outlines, pos, res)
671+
pos = _autoshrink(outlines, pos, res)
681672

682673
mask_params = _handle_default('mask_params', mask_params)
683674

684675
# plot outline
685-
linewidth = mask_params['markeredgewidth']
686-
patch = None
676+
patch_ = None
687677
if 'patch' in outlines:
688-
patch = outlines['patch']
689-
patch_ = patch() if callable(patch) else patch
678+
patch_ = outlines['patch']
679+
patch_ = patch_() if callable(patch_) else patch_
690680
patch_.set_clip_on(False)
691681
ax.add_patch(patch_)
692682
ax.set_transform(ax.transAxes)
693683
ax.set_clip_path(patch_)
684+
if _use_default_outlines:
685+
from matplotlib import patches
686+
patch_ = patches.Ellipse((0, 0),
687+
2 * outlines['clip_radius'][0],
688+
2 * outlines['clip_radius'][1],
689+
clip_on=True,
690+
transform=ax.transData)
694691

695692
# plot map and countour
696693
im = ax.imshow(Zi, cmap=cmap, vmin=vmin, vmax=vmax, origin='lower',
@@ -700,6 +697,7 @@ def _plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True,
700697
# This tackles an incomprehensible matplotlib bug if no contours are
701698
# drawn. To avoid rescalings, we will always draw contours.
702699
# But if no contours are desired we only draw one and make it invisible .
700+
linewidth = mask_params['markeredgewidth']
703701
no_contours = False
704702
if isinstance(contours, (np.ndarray, list)):
705703
pass # contours precomputed
@@ -714,14 +712,7 @@ def _plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True,
714712
for col in cont.collections:
715713
col.set_visible(False)
716714

717-
if _is_default_outlines:
718-
from matplotlib import patches
719-
patch_ = patches.Ellipse((0, 0),
720-
2 * outlines['clip_radius'][0],
721-
2 * outlines['clip_radius'][1],
722-
clip_on=True,
723-
transform=ax.transData)
724-
if _is_default_outlines or patch is not None:
715+
if patch_ is not None:
725716
im.set_clip_path(patch_)
726717
if cont is not None:
727718
for col in cont.collections:
@@ -766,15 +757,10 @@ def _show_names(x):
766757
return im, cont, interp
767758

768759

769-
def _make_image_mask(outlines, pos, res):
760+
def _autoshrink(outlines, pos, res):
770761
"""Make an image mask."""
771-
mask_ = np.c_[outlines['mask_pos']]
772-
xmin, xmax = (np.min(np.r_[np.inf, mask_[:, 0]]),
773-
np.max(np.r_[-np.inf, mask_[:, 0]]))
774-
ymin, ymax = (np.min(np.r_[np.inf, mask_[:, 1]]),
775-
np.max(np.r_[-np.inf, mask_[:, 1]]))
776-
777-
if outlines.get('autoshrink', False) is not False:
762+
if outlines.get('autoshrink', False):
763+
mask_ = np.c_[outlines['mask_pos']]
778764
inside = _inside_contour(pos, mask_)
779765
outside = np.invert(inside)
780766
outlier_points = pos[outside]
@@ -783,17 +769,7 @@ def _make_image_mask(outlines, pos, res):
783769
inside = _inside_contour(pos, mask_)
784770
outside = np.invert(inside)
785771
outlier_points = pos[outside]
786-
787-
image_mask = np.zeros((res, res), dtype=bool)
788-
xi_mask = np.linspace(xmin, xmax, res)
789-
yi_mask = np.linspace(ymin, ymax, res)
790-
Xi_mask, Yi_mask = np.meshgrid(xi_mask, yi_mask)
791-
792-
pos_ = np.c_[Xi_mask.flatten(), Yi_mask.flatten()]
793-
inds = _inside_contour(pos_, mask_)
794-
image_mask[inds.reshape(image_mask.shape)] = True
795-
796-
return image_mask, pos
772+
return pos
797773

798774

799775
def _inside_contour(pos, contour):
@@ -835,10 +811,9 @@ def _plot_ica_topomap(ica, idx=0, ch_type=None, res=64, layout=None,
835811
data_picks, pos, merge_grads, names, _ = _prepare_topo_plot(
836812
ica, ch_type, layout)
837813
pos, outlines = _check_outlines(pos, outlines, head_pos)
838-
if outlines not in (None, 'head'):
839-
image_mask, pos = _make_image_mask(outlines, pos, res)
840-
else:
841-
image_mask = None
814+
assert outlines is not None
815+
if outlines != 'head':
816+
pos = _autoshrink(outlines, pos, res)
842817

843818
data = data[data_picks]
844819

@@ -847,11 +822,10 @@ def _plot_ica_topomap(ica, idx=0, ch_type=None, res=64, layout=None,
847822
data = _merge_grad_data(data)
848823
axes.set_title(ica._ica_names[idx], fontsize=12)
849824
vmin_, vmax_ = _setup_vmin_vmax(data, vmin, vmax)
850-
im = plot_topomap(data.ravel(), pos, vmin=vmin_, vmax=vmax_,
851-
res=res, axes=axes, cmap=cmap, outlines=outlines,
852-
image_mask=image_mask, contours=contours,
853-
sensors=sensors, image_interp=image_interp,
854-
show=show)[0]
825+
im = plot_topomap(
826+
data.ravel(), pos, vmin=vmin_, vmax=vmax_, res=res, axes=axes,
827+
cmap=cmap, outlines=outlines, contours=contours, sensors=sensors,
828+
image_interp=image_interp, show=show)[0]
855829
if colorbar:
856830
cbar, cax = _add_colorbar(axes, im, cmap, pad=.05, title="AU",
857831
format='%3.2f')
@@ -998,10 +972,8 @@ def plot_ica_components(ica, picks=None, ch_type=None, res=64,
998972
data_picks, pos, merge_grads, names, _ = _prepare_topo_plot(ica, ch_type,
999973
layout)
1000974
pos, outlines = _check_outlines(pos, outlines, head_pos)
1001-
if outlines not in (None, 'head'):
1002-
image_mask, pos = _make_image_mask(outlines, pos, res)
1003-
else:
1004-
image_mask = None
975+
if outlines == 'head':
976+
pos = _autoshrink(outlines, pos, res)
1005977

1006978
data = np.atleast_2d(data)
1007979
data = data[:, data_picks]
@@ -1020,11 +992,10 @@ def plot_ica_components(ica, picks=None, ch_type=None, res=64,
1020992
titles.append(ax.set_title(ica._ica_names[ii], fontsize=12, **kwargs))
1021993
data_ = _merge_grad_data(data_) if merge_grads else data_
1022994
vmin_, vmax_ = _setup_vmin_vmax(data_, vmin, vmax)
1023-
im = plot_topomap(data_.flatten(), pos, vmin=vmin_, vmax=vmax_,
1024-
res=res, axes=ax, cmap=cmap[0], outlines=outlines,
1025-
image_mask=image_mask, contours=contours,
1026-
image_interp=image_interp, show=False,
1027-
sensors=sensors)[0]
995+
im = plot_topomap(
996+
data_.flatten(), pos, vmin=vmin_, vmax=vmax_, res=res, axes=ax,
997+
cmap=cmap[0], outlines=outlines, contours=contours,
998+
image_interp=image_interp, show=False, sensors=sensors)[0]
1028999
im.axes.set_label(ica._ica_names[ii])
10291000
if colorbar:
10301001
cbar, cax = _add_colorbar(ax, im, cmap, title="AU",
@@ -1580,10 +1551,8 @@ def plot_evoked_topomap(evoked, times="auto", ch_type=None, layout=None,
15801551
mask_ = mask[np.ix_(picks, time_idx)]
15811552

15821553
pos, outlines = _check_outlines(pos, outlines, head_pos)
1583-
if outlines is not None:
1584-
image_mask, pos = _make_image_mask(outlines, pos, res)
1585-
else:
1586-
image_mask = None
1554+
assert outlines is not None
1555+
pos = _autoshrink(outlines, pos, res)
15871556

15881557
vlims = [_setup_vmin_vmax(data[:, i], vmin, vmax, norm=merge_grads)
15891558
for i in range(len(times))]
@@ -1596,7 +1565,7 @@ def plot_evoked_topomap(evoked, times="auto", ch_type=None, layout=None,
15961565

15971566
kwargs = dict(vmin=vmin, vmax=vmax, sensors=sensors, res=res, names=names,
15981567
show_names=show_names, cmap=cmap[0], mask_params=mask_params,
1599-
outlines=outlines, image_mask=image_mask, contours=contours,
1568+
outlines=outlines, contours=contours,
16001569
image_interp=image_interp, show=False)
16011570
for idx, time in enumerate(times):
16021571
tp, cn, interp = _plot_topomap(
@@ -2135,7 +2104,7 @@ def _init_anim(ax, ax_line, ax_cbar, params, merge_grads):
21352104
zi_min = np.min(params['Zis'])
21362105
zi_max = np.max(params['Zis'])
21372106
cont_lims = np.linspace(zi_min, zi_max, 7, endpoint=False)[1:]
2138-
_, pos = _make_image_mask(outlines, pos, res)
2107+
pos = _autoshrink(outlines, pos, res)
21392108
params.update({'vmin': vmin, 'vmax': vmax, 'Xi': Xi, 'Yi': Yi, 'Zi': Zi,
21402109
'extent': (xmin, xmax, ymin, ymax), 'cmap': cmap,
21412110
'cont_lims': cont_lims})
@@ -2354,3 +2323,57 @@ def _set_contour_locator(vmin, vmax, contours):
23542323
locator = ticker.MaxNLocator(nbins=contours + 1)
23552324
contours = locator.tick_values(vmin, vmax)
23562325
return locator, contours
2326+
2327+
2328+
def _plot_corrmap(data, subjs, indices, ch_type, ica, label, show, outlines,
2329+
layout, cmap, contours, template=False):
2330+
"""Customize ica.plot_components for corrmap."""
2331+
if not template:
2332+
title = 'Detected components'
2333+
if label is not None:
2334+
title += ' of type ' + label
2335+
else:
2336+
title = "Supplied template"
2337+
2338+
picks = list(range(len(data)))
2339+
2340+
p = 20
2341+
if len(picks) > p: # plot components by sets of 20
2342+
n_components = len(picks)
2343+
figs = [_plot_corrmap(data[k:k + p], subjs[k:k + p],
2344+
indices[k:k + p], ch_type, ica, label, show,
2345+
outlines=outlines, layout=layout, cmap=cmap,
2346+
contours=contours)
2347+
for k in range(0, n_components, p)]
2348+
return figs
2349+
elif np.isscalar(picks):
2350+
picks = [picks]
2351+
2352+
data_picks, pos, merge_grads, names, _ = _prepare_topo_plot(
2353+
ica, ch_type, layout)
2354+
pos, outlines = _check_outlines(pos, outlines)
2355+
2356+
data = np.atleast_2d(data)
2357+
data = data[:, data_picks]
2358+
2359+
# prepare data for iteration
2360+
fig, axes = _prepare_trellis(len(picks), max_col=5)
2361+
fig.suptitle(title)
2362+
2363+
if merge_grads:
2364+
from ..channels.layout import _merge_grad_data
2365+
for ii, data_, ax, subject, idx in zip(picks, data, axes, subjs, indices):
2366+
if template:
2367+
ttl = 'Subj. {0}, {1}'.format(subject, ica._ica_names[idx])
2368+
ax.set_title(ttl, fontsize=12)
2369+
data_ = _merge_grad_data(data_) if merge_grads else data_
2370+
vmin_, vmax_ = _setup_vmin_vmax(data_, None, None)
2371+
plot_topomap(data_.flatten(), pos, vmin=vmin_, vmax=vmax_,
2372+
res=64, axes=ax, cmap=cmap, outlines=outlines,
2373+
contours=contours, show=False, image_interp='bilinear')[0]
2374+
_hide_frame(ax)
2375+
tight_layout(fig=fig)
2376+
fig.subplots_adjust(top=0.8)
2377+
fig.canvas.draw()
2378+
plt_show(show)
2379+
return fig

0 commit comments

Comments
 (0)