Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take units (m or mm) into account when showing fieldmaps on top of brains #13101

Merged
merged 13 commits into from
Feb 5, 2025
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ stages:
DISPLAY: ':99'
OPENBLAS_NUM_THREADS: '1'
MNE_TEST_ALLOW_SKIP: '^.*(PySide6 causes segfaults).*$'
MNE_BROWSER_PRECOMPUTE: 'false'
steps:
- bash: |
set -e
Expand Down
1 change: 1 addition & 0 deletions doc/changes/devel/13101.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Take units (m or mm) into account when drawing :func:`~mne.viz.plot_evoked_field` on top of :class:`~mne.viz.Brain`, by `Marijn van Vliet`_.
10 changes: 7 additions & 3 deletions mne/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import inspect
import os
import os.path as op
import platform
import re
import shutil
import sys
Expand Down Expand Up @@ -190,6 +191,8 @@ def pytest_configure(config: pytest.Config):
ignore:Starting field name with a underscore.*:
# joblib
ignore:process .* is multi-threaded, use of fork/exec.*:DeprecationWarning
# sklearn
ignore:Python binding for RankQuantileOptions.*:RuntimeWarning
""" # noqa: E501
for warning_line in warning_lines.split("\n"):
warning_line = warning_line.strip()
Expand Down Expand Up @@ -285,9 +288,10 @@ def __init__(self, exception_handler=None, signals=None):
@pytest.fixture(scope="session")
def azure_windows():
"""Determine if running on Azure Windows."""
return os.getenv(
"AZURE_CI_WINDOWS", "false"
).lower() == "true" and sys.platform.startswith("win")
return (
os.getenv("AZURE_CI_WINDOWS", "false").lower() == "true"
and platform.system() == "Windows"
)


@pytest.fixture(scope="function")
Expand Down
6 changes: 5 additions & 1 deletion mne/viz/evoked_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

from copy import deepcopy
from functools import partial

import numpy as np
Expand Down Expand Up @@ -185,6 +186,7 @@ def __init__(
if isinstance(fig, Brain):
self._renderer = fig._renderer
self._in_brain_figure = True
self._units = fig._units
if _get_3d_backend() == "notebook":
raise NotImplementedError(
"Plotting on top of an existing Brain figure "
Expand All @@ -195,6 +197,7 @@ def __init__(
fig, bgcolor=(0.0, 0.0, 0.0), size=(600, 600)
)
self._in_brain_figure = False
self._units = "m"

self.plotter = self._renderer.plotter
self.interaction = interaction
Expand Down Expand Up @@ -277,7 +280,8 @@ def _prepare_surf_map(self, surf_map, color, alpha):

# Make a solid surface
surf = surf_map["surf"]
if self._in_brain_figure:
if self._units == "mm":
surf = deepcopy(surf)
surf["rr"] *= 1000
map_vmax = self._vmax.get(surf_map["kind"])
if map_vmax is None:
Expand Down
18 changes: 15 additions & 3 deletions mne/viz/tests/test_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,21 @@ def test_plot_evoked_field(renderer):
ch_type=t,
)
evoked.plot_field(maps, time=0.1, n_contours=n_contours)
renderer.backend._close_all()

# Test plotting inside an existing Brain figure.
brain = Brain("fsaverage", "lh", "inflated", subjects_dir=subjects_dir)
fig = evoked.plot_field(maps, time=0.1, fig=brain)
# Test plotting inside an existing Brain figure. Check that units are taken into
# account.
for units in ["mm", "m"]:
brain = Brain(
"fsaverage", "lh", "inflated", units=units, subjects_dir=subjects_dir
)
fig = evoked.plot_field(maps, time=0.1, fig=brain)
assert brain._units == fig._units
scale = 1000 if units == "mm" else 1
assert (
fig._surf_maps[0]["surf"]["rr"][0, 0] == scale * maps[0]["surf"]["rr"][0, 0]
)
larsoner marked this conversation as resolved.
Show resolved Hide resolved
renderer.backend._close_all()

# Test some methods
fig = evoked.plot_field(maps, time_viewer=True)
Expand All @@ -214,6 +225,7 @@ def test_plot_evoked_field(renderer):

fig = evoked.plot_field(maps, time_viewer=False)
assert isinstance(fig, Figure3D)
renderer.backend._close_all()


@testing.requires_testing_data
Expand Down