From 660a591240d80bd0e80986e4e13a210f19ec81e9 Mon Sep 17 00:00:00 2001 From: mscheltienne Date: Mon, 13 Jun 2022 17:44:10 +0200 Subject: [PATCH 1/8] add support for instance with more than 1 channel type --- mne/viz/tests/test_topomap.py | 10 +++++++++- mne/viz/topomap.py | 12 ++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/mne/viz/tests/test_topomap.py b/mne/viz/tests/test_topomap.py index 2946513d4a8..3124b82e43e 100644 --- a/mne/viz/tests/test_topomap.py +++ b/mne/viz/tests/test_topomap.py @@ -22,11 +22,12 @@ from mne.io.proj import make_eeg_average_ref_proj, Projection from mne.io import read_raw_fif, read_info, RawArray from mne.io.constants import FIFF -from mne.io.pick import pick_info, channel_indices_by_type +from mne.io.pick import pick_info, channel_indices_by_type, _picks_to_idx from mne.io.compensator import get_current_comp from mne.channels import (read_layout, make_dig_montage, make_standard_montage, find_ch_adjacency) from mne.datasets import testing +from mne.preprocessing import compute_bridged_electrodes from mne.time_frequency.tfr import AverageTFR from mne.viz import plot_evoked_topomap, plot_projs_topomap, topomap @@ -730,6 +731,13 @@ def test_plot_bridged_electrodes(): with pytest.raises(RuntimeError, match='Expected'): plot_bridged_electrodes(info, bridged_idx, np.zeros((5, 6, 7))) + # test with multiple channel types + raw = read_raw_fif(raw_fname, preload=True) + picks = _picks_to_idx(raw.info, "eeg") + raw._data[picks[0]] = raw._data[picks[1]] # artificially bridge electrodes + bridged_idx, ed_matrix = compute_bridged_electrodes(raw) + plot_bridged_electrodes(raw.info, bridged_idx, ed_matrix) + def test_plot_ch_adjacency(): """Test plotting of adjacency matrix.""" diff --git a/mne/viz/topomap.py b/mne/viz/topomap.py index e8233e68d27..ab45f9e4358 100644 --- a/mne/viz/topomap.py +++ b/mne/viz/topomap.py @@ -2744,9 +2744,10 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, topomap_args = dict() else: topomap_args = topomap_args.copy() # don't change original + picks = pick_types(info, eeg=True) topomap_args.setdefault('image_interp', 'nearest') topomap_args.setdefault('cmap', 'summer_r') - topomap_args.setdefault('names', info.ch_names) + topomap_args.setdefault('names', pick_info(info, picks).ch_names) topomap_args.setdefault('show_names', True) topomap_args.setdefault('contours', False) if 'axes' not in topomap_args: @@ -2759,7 +2760,6 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, 'colorbar' in topomap_args else True # use sphere to find positions sphere = topomap_args['sphere'] if 'sphere' in topomap_args else None - picks = pick_types(info, eeg=True) if ed_matrix.shape[1:] != (picks.size, picks.size): raise RuntimeError( f'Expected {(ed_matrix.shape[0], picks.size, picks.size)} ' @@ -2770,13 +2770,13 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, for epo_idx in range(ed_matrix.shape[0]): ed_matrix[epo_idx][tril_idx] = ed_matrix[epo_idx].T[tril_idx] elec_dists = np.median(np.nanmin(ed_matrix, axis=1), axis=0) - pos = _find_topomap_coords(info, picks, sphere=sphere) - im, cn = plot_topomap(elec_dists, info, **topomap_args) + im, cn = plot_topomap(elec_dists, pick_info(info, picks), **topomap_args) fig = im.figure if fig is None else fig # add bridged connections for idx0, idx1 in bridged_idx: - im.axes.plot([pos[idx0][0], pos[idx1][0]], - [pos[idx0][1], pos[idx1][1]], color='r') + pos = _find_topomap_coords(info, [idx0, idx1], sphere=sphere) + im.axes.plot([pos[0, 0], pos[1, 0]], + [pos[0, 1], pos[1, 1]], color='r') if title is not None: im.axes.set_title(title) if colorbar: From e80da7cd6f3bc729bca1b3799f6bc1fb82e9e71b Mon Sep 17 00:00:00 2001 From: mscheltienne Date: Mon, 13 Jun 2022 17:46:00 +0200 Subject: [PATCH 2/8] add entry to changelog --- doc/changes/latest.inc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/changes/latest.inc b/doc/changes/latest.inc index 6a598463670..31f6c0ec48e 100644 --- a/doc/changes/latest.inc +++ b/doc/changes/latest.inc @@ -150,6 +150,8 @@ Bugs - In :class:`mne.Report`, some figures would have an undesired border added to the edges; this has now been resolved (:gh:`10730` by `Richard Höchenberger`_) +- Automatically selects EEG channels when plotting bridged electrodes with :func:`mne.viz.plot_bridged_electrodes` (:gh:`` by `Mathieu Scheltienne`_) + API and behavior changes ~~~~~~~~~~~~~~~~~~~~~~~~ - When creating BEM surfaces via :func:`mne.bem.make_watershed_bem` and :func:`mne.bem.make_flash_bem`, the ``copy`` parameter now defaults to ``True``. This means that instead of creating symbolic links inside the FreeSurfer subject's ``bem`` folder, we now create "actual" files. This should avoid troubles when sharing files across different operating systems and file systems (:gh:`10531` by `Richard Höchenberger`_) From 31e1fdf966600b7fe58e6842bab0853caab8a2b4 Mon Sep 17 00:00:00 2001 From: mscheltienne Date: Mon, 13 Jun 2022 17:49:37 +0200 Subject: [PATCH 3/8] add notes [skip ci] --- mne/viz/topomap.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mne/viz/topomap.py b/mne/viz/topomap.py index ab45f9e4358..9442c8cca2c 100644 --- a/mne/viz/topomap.py +++ b/mne/viz/topomap.py @@ -2726,9 +2726,10 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, %(info_not_none)s bridged_idx : list of tuple The indices of channels marked as bridged with each bridged - pair stored as a tuple. + pair stored as a tuple. See notes for additional information. ed_matrix : ndarray of float, shape (n_channels, n_channels) The electrical distance matrix for each pair of EEG electrodes. + See notes for additional information. title : str A title to add to the plot. topomap_args : dict | None @@ -2738,6 +2739,11 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, ------- fig : instance of matplotlib.figure.Figure The topoplot figure handle. + + Notes + ----- + See also :func:`mne.preprocessing.compute_bridged_electrodes` to determine + ``bridged_idx`` and ``ed_matrix``. """ import matplotlib.pyplot as plt if topomap_args is None: From a31e981da96d8b574c7166a3c73a43fbcf913ffd Mon Sep 17 00:00:00 2001 From: mscheltienne Date: Mon, 13 Jun 2022 17:55:31 +0200 Subject: [PATCH 4/8] update gh id --- doc/changes/latest.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/latest.inc b/doc/changes/latest.inc index 31f6c0ec48e..2328eb47894 100644 --- a/doc/changes/latest.inc +++ b/doc/changes/latest.inc @@ -150,7 +150,7 @@ Bugs - In :class:`mne.Report`, some figures would have an undesired border added to the edges; this has now been resolved (:gh:`10730` by `Richard Höchenberger`_) -- Automatically selects EEG channels when plotting bridged electrodes with :func:`mne.viz.plot_bridged_electrodes` (:gh:`` by `Mathieu Scheltienne`_) +- Automatically selects EEG channels when plotting bridged electrodes with :func:`mne.viz.plot_bridged_electrodes` (:gh:`10753` by `Mathieu Scheltienne`_) API and behavior changes ~~~~~~~~~~~~~~~~~~~~~~~~ From 0cbd7503bfff11a2d185f16344af3abe2862f038 Mon Sep 17 00:00:00 2001 From: mscheltienne Date: Tue, 14 Jun 2022 10:10:51 +0200 Subject: [PATCH 5/8] fix doc [skip azp] [skip actions] --- doc/changes/latest.inc | 2 +- mne/viz/topomap.py | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/doc/changes/latest.inc b/doc/changes/latest.inc index 2328eb47894..228fa75e6ef 100644 --- a/doc/changes/latest.inc +++ b/doc/changes/latest.inc @@ -150,7 +150,7 @@ Bugs - In :class:`mne.Report`, some figures would have an undesired border added to the edges; this has now been resolved (:gh:`10730` by `Richard Höchenberger`_) -- Automatically selects EEG channels when plotting bridged electrodes with :func:`mne.viz.plot_bridged_electrodes` (:gh:`10753` by `Mathieu Scheltienne`_) +- Fix selection of EEG channels when plotting bridged electrodes with :func:`mne.viz.plot_bridged_electrodes` (:gh:`10753` by `Mathieu Scheltienne`_) API and behavior changes ~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/mne/viz/topomap.py b/mne/viz/topomap.py index 9442c8cca2c..c8d86b9e6af 100644 --- a/mne/viz/topomap.py +++ b/mne/viz/topomap.py @@ -2726,10 +2726,13 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, %(info_not_none)s bridged_idx : list of tuple The indices of channels marked as bridged with each bridged - pair stored as a tuple. See notes for additional information. + pair stored as a tuple. See also + :func:`mne.preprocessing.compute_bridged_electrodes` to determine + ``bridged_idx``. ed_matrix : ndarray of float, shape (n_channels, n_channels) The electrical distance matrix for each pair of EEG electrodes. - See notes for additional information. + See also :func:`mne.preprocessing.compute_bridged_electrodes` to + determine ``ed_matrix``. title : str A title to add to the plot. topomap_args : dict | None @@ -2739,11 +2742,6 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, ------- fig : instance of matplotlib.figure.Figure The topoplot figure handle. - - Notes - ----- - See also :func:`mne.preprocessing.compute_bridged_electrodes` to determine - ``bridged_idx`` and ``ed_matrix``. """ import matplotlib.pyplot as plt if topomap_args is None: From 96c6e6763f7f14eed2cbe8919d8cde5a20918fee Mon Sep 17 00:00:00 2001 From: mscheltienne Date: Tue, 14 Jun 2022 10:58:16 +0200 Subject: [PATCH 6/8] fix sphere issue --- doc/changes/latest.inc | 2 +- mne/viz/topomap.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/changes/latest.inc b/doc/changes/latest.inc index 228fa75e6ef..1636efbcc11 100644 --- a/doc/changes/latest.inc +++ b/doc/changes/latest.inc @@ -150,7 +150,7 @@ Bugs - In :class:`mne.Report`, some figures would have an undesired border added to the edges; this has now been resolved (:gh:`10730` by `Richard Höchenberger`_) -- Fix selection of EEG channels when plotting bridged electrodes with :func:`mne.viz.plot_bridged_electrodes` (:gh:`10753` by `Mathieu Scheltienne`_) +- Fix selection of EEG channels and selected sphere when plotting bridged electrodes with :func:`mne.viz.plot_bridged_electrodes` (:gh:`10753` by `Mathieu Scheltienne`_) API and behavior changes ~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/mne/viz/topomap.py b/mne/viz/topomap.py index c8d86b9e6af..3599f4f73d0 100644 --- a/mne/viz/topomap.py +++ b/mne/viz/topomap.py @@ -2754,6 +2754,8 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, topomap_args.setdefault('names', pick_info(info, picks).ch_names) topomap_args.setdefault('show_names', True) topomap_args.setdefault('contours', False) + if 'sphere' not in topomap_args: + topomap_args['sphere'] = _check_sphere(None) if 'axes' not in topomap_args: fig, ax = plt.subplots() topomap_args['axes'] = ax From ba2dd3cbb9c94540555073ccfdd24c7e0d85fd79 Mon Sep 17 00:00:00 2001 From: mscheltienne Date: Tue, 14 Jun 2022 11:10:56 +0200 Subject: [PATCH 7/8] better --- mne/viz/topomap.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mne/viz/topomap.py b/mne/viz/topomap.py index 3599f4f73d0..e46ff93a388 100644 --- a/mne/viz/topomap.py +++ b/mne/viz/topomap.py @@ -2754,8 +2754,8 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, topomap_args.setdefault('names', pick_info(info, picks).ch_names) topomap_args.setdefault('show_names', True) topomap_args.setdefault('contours', False) - if 'sphere' not in topomap_args: - topomap_args['sphere'] = _check_sphere(None) + sphere = topomap_args['sphere'] if 'sphere' in topomap_args \ + else _check_sphere(None) if 'axes' not in topomap_args: fig, ax = plt.subplots() topomap_args['axes'] = ax @@ -2764,8 +2764,6 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, # handle colorbar here instead of in plot_topomap colorbar = topomap_args.pop('colorbar') if \ 'colorbar' in topomap_args else True - # use sphere to find positions - sphere = topomap_args['sphere'] if 'sphere' in topomap_args else None if ed_matrix.shape[1:] != (picks.size, picks.size): raise RuntimeError( f'Expected {(ed_matrix.shape[0], picks.size, picks.size)} ' From d555f75aa357655498b8d71e35e88768c2d6aad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20H=C3=B6chenberger?= Date: Tue, 14 Jun 2022 12:23:04 +0200 Subject: [PATCH 8/8] Make docstring more concise & add See Also [skip azp][skip actions] --- mne/viz/topomap.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/mne/viz/topomap.py b/mne/viz/topomap.py index e46ff93a388..6db57b5c7e1 100644 --- a/mne/viz/topomap.py +++ b/mne/viz/topomap.py @@ -2726,13 +2726,13 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, %(info_not_none)s bridged_idx : list of tuple The indices of channels marked as bridged with each bridged - pair stored as a tuple. See also - :func:`mne.preprocessing.compute_bridged_electrodes` to determine - ``bridged_idx``. - ed_matrix : ndarray of float, shape (n_channels, n_channels) + pair stored as a tuple. + Can be generated via + :func:`mne.preprocessing.compute_bridged_electrodes`. + ed_matrix : array of float, shape (n_channels, n_channels) The electrical distance matrix for each pair of EEG electrodes. - See also :func:`mne.preprocessing.compute_bridged_electrodes` to - determine ``ed_matrix``. + Can be generated via + :func:`mne.preprocessing.compute_bridged_electrodes`. title : str A title to add to the plot. topomap_args : dict | None @@ -2742,6 +2742,10 @@ def plot_bridged_electrodes(info, bridged_idx, ed_matrix, title=None, ------- fig : instance of matplotlib.figure.Figure The topoplot figure handle. + + See Also + -------- + mne.preprocessing.compute_bridged_electrodes """ import matplotlib.pyplot as plt if topomap_args is None: