Skip to content

Commit dda454d

Browse files
ENH: Add plot_kwargs to report.add_trans (#13234)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 46e7071 commit dda454d

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a new parameter ``plot_kwargs`` to :meth:`mne.Report.add_trans`, to control low level properties of the visualization, by `Stefan Appelhoff`_.

mne/defaults.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
nasion_color=(0.0, 1.0, 0.0),
266266
rpa_color=(0.0, 0.0, 1.0),
267267
),
268+
report_coreg=dict(dig=True, meg=("helmet", "sensors"), show_axes=True),
268269
noise_std=dict(grad=5e-13, mag=20e-15, eeg=0.2e-6),
269270
eloreta_options=dict(eps=1e-6, max_iter=20, force_equal=False),
270271
depth_mne=dict(

mne/report/report.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,10 @@ def _iterate_trans_views(function, alpha, **kwargs):
518518

519519
try:
520520
try:
521-
return _itv(function, fig, surfaces={"head-dense": alpha}, **kwargs)
521+
return _itv(function, fig, **kwargs)
522522
except OSError:
523-
return _itv(function, fig, surfaces={"head": alpha}, **kwargs)
523+
kwargs["surfaces"] = {"head": alpha}
524+
return _itv(function, fig, **kwargs)
524525
finally:
525526
backend._close_3d_figure(fig)
526527

@@ -1597,9 +1598,10 @@ def add_trans(
15971598
subject=None,
15981599
subjects_dir=None,
15991600
alpha=None,
1601+
coord_frame="mri",
1602+
plot_kwargs=None,
16001603
tags=("coregistration",),
16011604
section=None,
1602-
coord_frame="mri",
16031605
replace=False,
16041606
):
16051607
"""Add a coregistration visualization to the report.
@@ -1625,13 +1627,23 @@ def add_trans(
16251627
alpha : float | None
16261628
The level of opacity to apply to the head surface. If a float, must
16271629
be between 0 and 1 (inclusive), where 1 means fully opaque. If
1628-
``None``, will use the MNE-Python default value.
1630+
``None``, will use the MNE-Python default value. See also ``plot_kwargs``.
1631+
coord_frame : 'auto' | 'head' | 'meg' | 'mri'
1632+
Coordinate frame used for plotting. See :func:`mne.viz.plot_alignment`
1633+
and ``plot_kwargs``.
1634+
plot_kwargs : dict | None
1635+
Plotting arguments to be passed to :func:`mne.viz.plot_alignment`.
1636+
If ``alpha`` is not ``None``, it will override a potential
1637+
``plot_kwargs["alpha"]``. The ``coord_frame`` key word argument always
1638+
overrides a potential ``plot_kwargs["coord_frame"]``. If ``None``,
1639+
this defaults to
1640+
``dict(dig=True, meg=("helmet", "sensors"), show_axes=True)``.
1641+
1642+
.. versionadded:: 1.10
16291643
%(tags_report)s
16301644
%(section_report)s
16311645
16321646
.. versionadded:: 1.9
1633-
coord_frame : 'auto' | 'head' | 'meg' | 'mri'
1634-
Coordinate frame used for plotting. See :func:`mne.viz.plot_alignment`.
16351647
%(replace_report)s
16361648
16371649
Notes
@@ -1645,6 +1657,7 @@ def add_trans(
16451657
subject=subject,
16461658
subjects_dir=subjects_dir,
16471659
alpha=alpha,
1660+
plot_kwargs=plot_kwargs,
16481661
title=title,
16491662
section=section,
16501663
tags=tags,
@@ -4220,6 +4233,7 @@ def _add_trans(
42204233
subject,
42214234
subjects_dir,
42224235
alpha,
4236+
plot_kwargs,
42234237
title,
42244238
section,
42254239
tags,
@@ -4232,22 +4246,40 @@ def _add_trans(
42324246
if not isinstance(info, Info):
42334247
info = read_info(info)
42344248

4235-
kwargs = dict(
4236-
info=info,
4237-
trans=trans,
4238-
subject=subject,
4239-
subjects_dir=subjects_dir,
4240-
dig=True,
4241-
meg=["helmet", "sensors"],
4242-
show_axes=True,
4243-
coord_frame=coord_frame,
4249+
plot_kwargs = _handle_default("report_coreg", plot_kwargs)
4250+
4251+
plot_kwargs.update(
4252+
dict(
4253+
info=info,
4254+
trans=trans,
4255+
subject=subject,
4256+
subjects_dir=subjects_dir,
4257+
)
42444258
)
4259+
4260+
# This potentially overwrites information
4261+
plot_kwargs["coord_frame"] = coord_frame
4262+
4263+
if alpha is not None:
4264+
# if not available, fall back to plot_alignment default: 'auto'
4265+
surfaces = plot_kwargs.get("surfaces", "auto")
4266+
if isinstance(surfaces, dict):
4267+
surfaces = list(surfaces.keys())
4268+
elif isinstance(surfaces, list):
4269+
pass
4270+
elif isinstance(surfaces, str) and surfaces != "auto":
4271+
surfaces = [surfaces]
4272+
else:
4273+
surfaces = ["head-dense"] # "auto"
4274+
4275+
surfaces = {surf: alpha for surf in surfaces}
4276+
42454277
img, caption = _iterate_trans_views(
42464278
function=plot_alignment,
42474279
alpha=alpha,
42484280
max_width=self.img_max_width,
42494281
max_res=self.img_max_res,
4250-
**kwargs,
4282+
**plot_kwargs,
42514283
)
42524284
self._add_image(
42534285
img=img,

mne/report/tests/test_report.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,11 @@ def test_manual_report_3d(tmp_path, renderer):
11551155
with info._unlock():
11561156
dig, info["dig"] = info["dig"], []
11571157
add_kwargs = dict(
1158-
trans=trans_fname, info=info, subject="sample", subjects_dir=subjects_dir
1158+
trans=trans_fname,
1159+
info=info,
1160+
subject="sample",
1161+
subjects_dir=subjects_dir,
1162+
alpha=0.75,
11591163
)
11601164
with (
11611165
_record_warnings(),
@@ -1173,6 +1177,7 @@ def test_manual_report_3d(tmp_path, renderer):
11731177
with pytest.raises(RuntimeError, match="Could not find"):
11741178
r.add_trans(title="my coreg", **bad_add_kwargs)
11751179
add_kwargs.update(trans="fsaverage") # this is wrong but tests fsaverage code path
1180+
add_kwargs.update(plot_kwargs=dict(dig="fiducials")) # test additional plot kwargs
11761181
r.add_trans(title="my coreg", **add_kwargs)
11771182
r.add_bem(subject="sample", subjects_dir=subjects_dir, title="my bem", decim=100)
11781183
r.add_inverse_operator(

0 commit comments

Comments
 (0)