Skip to content

Commit 0efaf96

Browse files
authored
ENH: support auto and fsaverage for add_trans in report (#13232)
1 parent 6696cec commit 0efaf96

File tree

7 files changed

+58
-30
lines changed

7 files changed

+58
-30
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:meth:`mne.Report.add_trans` now accepts ``"auto"`` and ``"fsaverage"`` as arguments for the ``trans`` parameter, by `Stefan Appelhoff`_.

mne/forward/_field_interpolation.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from ..evoked import Evoked, EvokedArray
2222
from ..fixes import _safe_svd
2323
from ..surface import get_head_surf, get_meg_helmet_surf
24-
from ..transforms import _find_trans, _get_trans, transform_surface_to
24+
from ..transforms import _find_trans, transform_surface_to
2525
from ..utils import _check_fname, _check_option, _pl, _reg_pinv, logger, verbose
2626
from ._lead_dots import (
2727
_do_cross_dots,
@@ -514,10 +514,12 @@ def make_field_map(
514514
name="subjects_dir",
515515
need_dir=True,
516516
)
517-
if isinstance(trans, str) and trans == "auto":
518-
# let's try to do this in MRI coordinates so they're easy to plot
519-
trans = _find_trans(subject, subjects_dir)
520-
trans, trans_type = _get_trans(trans, fro="head", to="mri")
517+
518+
trans, trans_type = _find_trans(
519+
trans=trans,
520+
subject=subject,
521+
subjects_dir=subjects_dir,
522+
)
521523

522524
if "eeg" in types and trans_type == "identity":
523525
logger.info("No trans file available. EEG data ignored.")

mne/report/report.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from ..proj import read_proj
4444
from ..source_estimate import SourceEstimate, read_source_estimate
4545
from ..surface import dig_mri_distances
46-
from ..transforms import Transform, read_trans
46+
from ..transforms import Transform, _find_trans, read_trans
4747
from ..utils import (
4848
_check_ch_locs,
4949
_check_fname,
@@ -1606,8 +1606,11 @@ def add_trans(
16061606
16071607
Parameters
16081608
----------
1609-
trans : path-like | instance of Transform
1610-
The ``head -> MRI`` transformation to render.
1609+
%(trans)s "auto" will load trans from the FreeSurfer directory
1610+
specified by ``subject`` and ``subjects_dir`` parameters.
1611+
1612+
.. versionchanged:: 1.10
1613+
Support for 'fsaverage' argument.
16111614
info : path-like | instance of Info
16121615
The `~mne.Info` corresponding to ``trans``.
16131616
title : str
@@ -4224,8 +4227,8 @@ def _add_trans(
42244227
replace,
42254228
):
42264229
"""Render trans (only PNG)."""
4227-
if not isinstance(trans, Transform):
4228-
trans = read_trans(trans)
4230+
trans, _ = _find_trans(trans=trans, subject=subject, subjects_dir=subjects_dir)
4231+
42294232
if not isinstance(info, Info):
42304233
info = read_info(info)
42314234

mne/report/tests/test_report.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,11 @@ def test_manual_report_3d(tmp_path, renderer):
11681168
# use of sparse rather than dense head, and also possibly an arg to specify
11691169
# which views to actually show. Both of these could probably be useful to
11701170
# end-users, too.
1171+
bad_add_kwargs = add_kwargs.copy()
1172+
bad_add_kwargs.update(dict(trans="auto", subjects_dir=subjects_dir))
1173+
with pytest.raises(RuntimeError, match="Could not find"):
1174+
r.add_trans(title="my coreg", **bad_add_kwargs)
1175+
add_kwargs.update(trans="fsaverage") # this is wrong but tests fsaverage code path
11711176
r.add_trans(title="my coreg", **add_kwargs)
11721177
r.add_bem(subject="sample", subjects_dir=subjects_dir, title="my bem", decim=100)
11731178
r.add_inverse_operator(

mne/tests/test_transforms.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,16 @@ def test_get_trans():
103103
def test_io_trans(tmp_path):
104104
"""Test reading and writing of trans files."""
105105
os.mkdir(tmp_path / "sample")
106-
pytest.raises(RuntimeError, _find_trans, "sample", subjects_dir=tmp_path)
106+
pytest.raises(
107+
RuntimeError, _find_trans, trans="auto", subject="sample", subjects_dir=tmp_path
108+
)
107109
trans0 = read_trans(fname)
108110
fname1 = tmp_path / "sample" / "test-trans.fif"
109111
trans0.save(fname1)
110-
assert fname1 == _find_trans("sample", subjects_dir=tmp_path)
112+
trans1, got_fname = _find_trans(
113+
trans="auto", subject="sample", subjects_dir=tmp_path
114+
)
115+
assert fname1 == got_fname
111116
trans1 = read_trans(fname1)
112117

113118
# check all properties

mne/transforms.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,30 @@ def _print_coord_trans(
227227
)
228228

229229

230-
def _find_trans(subject, subjects_dir=None):
231-
if subject is None:
232-
if "SUBJECT" in os.environ:
233-
subject = os.environ["SUBJECT"]
234-
else:
235-
raise ValueError("SUBJECT environment variable not set")
236-
237-
trans_fnames = glob.glob(str(subjects_dir / subject / "*-trans.fif"))
238-
if len(trans_fnames) < 1:
239-
raise RuntimeError(f"Could not find the transformation for {subject}")
240-
elif len(trans_fnames) > 1:
241-
raise RuntimeError(f"Found multiple transformations for {subject}")
242-
return Path(trans_fnames[0])
230+
def _find_trans(*, trans, subject, subjects_dir=None):
231+
if isinstance(trans, str) and trans == "auto":
232+
subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
233+
# let's try to do this in MRI coordinates so they're easy to plot
234+
if subject is None:
235+
if "SUBJECT" in os.environ:
236+
subject = os.environ["SUBJECT"]
237+
else:
238+
raise ValueError(
239+
"subject is None and SUBJECT environment variable not set, cannot "
240+
"use trans='auto'"
241+
)
242+
glob_str = str(subjects_dir / subject / "*-trans.fif")
243+
trans_fnames = glob.glob(glob_str)
244+
if len(trans_fnames) < 1:
245+
raise RuntimeError(
246+
f"Could not find the transformation for {subject} in: {glob_str}"
247+
)
248+
elif len(trans_fnames) > 1:
249+
raise RuntimeError(
250+
f"Found multiple transformations for {subject} in: {glob_str}"
251+
)
252+
trans = Path(trans_fnames[0])
253+
return _get_trans(trans, fro="head", to="mri")
243254

244255

245256
def apply_trans(trans, pts, move=True):

mne/viz/_3d.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,12 @@ def plot_alignment(
720720
f'subject ("{subject}") did not match the '
721721
f'subject name in src ("{src_subject}")'
722722
)
723-
# configure transforms
724-
if isinstance(trans, str) and trans == "auto":
725-
subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
726-
trans = _find_trans(subject, subjects_dir)
727-
trans, trans_type = _get_trans(trans, fro="head", to="mri")
723+
724+
trans, trans_type = _find_trans(
725+
trans=trans,
726+
subject=subject,
727+
subjects_dir=subjects_dir,
728+
)
728729

729730
picks = pick_types(
730731
info,

0 commit comments

Comments
 (0)