Skip to content

Commit

Permalink
Fix vmax slider for EvokedField figures (mne-tools#12354)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmvanvliet authored Jan 16, 2024
1 parent 2040898 commit 6af181a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
1 change: 1 addition & 0 deletions doc/changes/devel/12354.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug in :meth:`mne.viz.EvokedField.set_vmax` that prevented setting the color limits of the MEG magnetic field density, by `Marijn van Vliet`_
34 changes: 16 additions & 18 deletions mne/viz/evoked_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ def _configure_dock(self):
if self._show_density:
r._dock_add_label(value="max value", align=True, layout=layout)

@_auto_weakref
def _callback(vmax, kind, scaling):
self.set_vmax(vmax / scaling, kind=kind)

for surf_map in self._surf_maps:
if surf_map["map_kind"] == "meg":
scaling = DEFAULTS["scalings"]["grad"]
Expand All @@ -388,18 +392,14 @@ def _configure_dock(self):
rng = [0, np.max(np.abs(surf_map["data"])) * scaling]
hlayout = r._dock_add_layout(vertical=False)

@_auto_weakref
def _callback(vmax, type, scaling):
self.set_vmax(vmax / scaling, type=type)

self._widgets[
f"vmax_slider_{surf_map['map_kind']}"
] = r._dock_add_slider(
name=surf_map["map_kind"].upper(),
value=surf_map["map_vmax"] * scaling,
rng=rng,
callback=partial(
_callback, type=surf_map["map_kind"], scaling=scaling
_callback, kind=surf_map["map_kind"], scaling=scaling
),
double=True,
layout=hlayout,
Expand All @@ -411,7 +411,7 @@ def _callback(vmax, type, scaling):
value=surf_map["map_vmax"] * scaling,
rng=rng,
callback=partial(
_callback, type=surf_map["map_kind"], scaling=scaling
_callback, kind=surf_map["map_kind"], scaling=scaling
),
layout=hlayout,
)
Expand Down Expand Up @@ -473,17 +473,15 @@ def _on_colormap_range(self, event):
if self._show_density:
surf_map["mesh"].update_overlay(name="field", rng=[vmin, vmax])
# Update the GUI widgets
# TODO: type is undefined here and only avoids a flake warning because it's
# a builtin!
if type == "meg": # noqa: E721
if kind == "meg":
scaling = DEFAULTS["scalings"]["grad"]
else:
scaling = DEFAULTS["scalings"]["eeg"]
with disable_ui_events(self):
widget = self._widgets.get(f"vmax_slider_{type}", None)
widget = self._widgets.get(f"vmax_slider_{kind}", None)
if widget is not None:
widget.set_value(vmax * scaling)
widget = self._widgets.get(f"vmax_spin_{type}", None)
widget = self._widgets.get(f"vmax_spin_{kind}", None)
if widget is not None:
widget.set_value(vmax * scaling)

Expand Down Expand Up @@ -543,28 +541,28 @@ def set_contours(self, n_contours):
),
)

def set_vmax(self, vmax, type="meg"):
def set_vmax(self, vmax, kind="meg"):
"""Change the color range of the density maps.
Parameters
----------
vmax : float
The new maximum value of the color range.
type : 'meg' | 'eeg'
kind : 'meg' | 'eeg'
Which field map to apply the new color range to.
"""
_check_option("type", type, ["eeg", "meg"])
_check_option("type", kind, ["eeg", "meg"])
for surf_map in self._surf_maps:
if surf_map["map_kind"] == type:
if surf_map["map_kind"] == kind:
publish(
self,
ColormapRange(
kind=f"field_strength_{type}",
kind=f"field_strength_{kind}",
fmin=-vmax,
fmax=vmax,
),
)
break
break
else:
raise ValueError(f"No {type.upper()} field map currently shown.")

Expand All @@ -573,4 +571,4 @@ def _rescale(self):
for surf_map in self._surf_maps:
current_data = surf_map["data_interp"](self._current_time)
vmax = float(np.max(current_data))
self.set_vmax(vmax, type=surf_map["map_kind"])
self.set_vmax(vmax, kind=surf_map["map_kind"])
11 changes: 10 additions & 1 deletion mne/viz/tests/test_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from mne._fiff.constants import FIFF
from mne.bem import read_bem_solution, read_bem_surfaces
from mne.datasets import testing
from mne.defaults import DEFAULTS
from mne.io import read_info, read_raw_bti, read_raw_ctf, read_raw_kit, read_raw_nirx
from mne.minimum_norm import apply_inverse
from mne.source_estimate import _BaseVolSourceEstimate
Expand Down Expand Up @@ -196,8 +197,16 @@ def test_plot_evoked_field(renderer):
assert isinstance(fig, EvokedField)
fig._rescale()
fig.set_time(0.05)
assert fig._current_time == 0.05
fig.set_contours(10)
fig.set_vmax(2)
assert fig._n_contours == 10
assert fig._widgets["contours"].get_value() == 10
fig.set_vmax(2e-12, kind="meg")
assert fig._surf_maps[1]["contours"][-1] == 2e-12
assert (
fig._widgets["vmax_slider_meg"].get_value()
== DEFAULTS["scalings"]["grad"] * 2e-12
)

fig = evoked.plot_field(maps, time_viewer=False)
assert isinstance(fig, Figure3D)
Expand Down

0 comments on commit 6af181a

Please sign in to comment.