Skip to content

Commit

Permalink
BUG: Fix bugs with coreg
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Oct 4, 2024
1 parent ed933b8 commit 5c8203c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/changes/devel/bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug where :ref:`mne coreg` would always show MEG channels even if the "MEG Sensors" checkbox was disabled, by `Eric Larson`_.
13 changes: 10 additions & 3 deletions mne/gui/_coreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,14 @@ def _add_channels(self):
fnirs=self._defaults["sensor_opacity"],
meg=0.25,
)
picks = pick_types(self._info, ref_meg=False, meg=True, eeg=True, fnirs=True)
picks = pick_types(
self._info,
ref_meg=False,
meg=True,
eeg=True,
fnirs=True,
exclude=(),
)
these_actors = _plot_sensors_3d(
self._renderer,
self._info,
Expand All @@ -1295,7 +1302,7 @@ def _add_channels(self):
nearest=self._nearest,
**plot_types,
)
sens_actors = sum(these_actors.values(), list())
sens_actors = sum((these_actors or {}).values(), list())
self._update_actor("sensors", sens_actors)

def _add_head_surface(self):
Expand Down Expand Up @@ -1760,7 +1767,7 @@ def _configure_dock(self):
)
self._widgets["meg"] = self._renderer._dock_add_check_box(
name="Show MEG sensors",
value=self._helmet,
value=self._meg_channels,
callback=self._set_meg_channels,
tooltip="Enable/Disable MEG sensors",
layout=view_options_layout,
Expand Down
10 changes: 9 additions & 1 deletion mne/gui/tests/test_coreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ def test_coreg_gui_pyvista_basic(tmp_path, monkeypatch, renderer_interactive_pyv
assert not coreg._helmet
assert coreg._actors["helmet"] is None
coreg._set_helmet(True)
assert coreg._eeg_channels
coreg._set_eeg_channels(False)
assert not coreg._eeg_channels
assert coreg._helmet
with catch_logging() as log:
coreg._redraw(verbose="debug")
Expand All @@ -251,19 +254,24 @@ def test_coreg_gui_pyvista_basic(tmp_path, monkeypatch, renderer_interactive_pyv
log = log.getvalue()
assert "Drawing helmet" in log
assert not coreg._meg_channels
assert coreg._actors["helmet"] is not None
# TODO: Someday test our file dialogs like:
# coreg._widgets["save_trans"].widget.click()
assert len(coreg._actors["sensors"]) == 0
coreg._set_meg_channels(True)
assert coreg._meg_channels
with catch_logging() as log:
coreg._redraw(verbose="debug")
assert "Drawing meg sensors" in log.getvalue()
assert coreg._actors["helmet"] is not None
assert len(coreg._actors["sensors"]) == 306
assert coreg._orient_glyphs
assert coreg._scale_by_distance
assert coreg._mark_inside
assert_allclose(
coreg._head_opacity, float(config.get("MNE_COREG_HEAD_OPACITY", "0.8"))
)
assert coreg._hpi_coils
assert coreg._eeg_channels
assert coreg._head_shape_points
assert coreg._scale_mode == "None"
assert coreg._icp_fid_match == "matched"
Expand Down
15 changes: 11 additions & 4 deletions mne/viz/_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,9 +1507,16 @@ def _plot_sensors_3d(
elif ch_type in _MEG_CH_TYPES_SPLIT:
ch_type = "meg"
# only plot sensor locations if channels/original in selection
plot_sensors = (ch_type != "fnirs" or "channels" in fnirs) and (
ch_type != "eeg" or "original" in eeg
)
plot_sensors = True
if ch_type == "fnirs":
if not fnirs or "channels" not in fnirs:
plot_sensors = False
elif ch_type == "eeg":
if not eeg or "original" not in eeg:
plot_sensors = False
elif ch_type == "meg":
if not meg or "sensors" not in meg:
plot_sensors = False
# plot sensors
if isinstance(ch_coord, tuple): # is meg, plot coil
ch_coord = dict(rr=ch_coord[0] * unit_scalar, tris=ch_coord[1])
Expand Down Expand Up @@ -1558,7 +1565,7 @@ def _plot_sensors_3d(
assert isinstance(sensor_colors, dict)
assert isinstance(sensor_scales, dict)
for ch_type, sens_loc in locs.items():
logger.debug(f"Drawing {ch_type} sensors")
logger.debug(f"Drawing {ch_type} sensors ({len(sens_loc)})")
assert len(sens_loc) # should be guaranteed above
colors = to_rgba_array(sensor_colors.get(ch_type, defaults[ch_type + "_color"]))
scales = np.atleast_1d(
Expand Down
2 changes: 2 additions & 0 deletions mne/viz/backends/_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,8 @@ def _dock_add_file_button(
):
layout = self._dock_layout if layout is None else layout
weakself = weakref.ref(self)
if initial_directory is not None:
initial_directory = str(initial_directory)

def callback():
self = weakself()
Expand Down

0 comments on commit 5c8203c

Please sign in to comment.