Skip to content

Commit 4c48d11

Browse files
FIX: correct mode parameter in get_point_spread and get_cross_talk (#13754)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 65b6e62 commit 4c48d11

4 files changed

Lines changed: 27 additions & 12 deletions

File tree

doc/changes/dev/13754.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``mode`` parameter in :func:`~mne.minimum_norm.get_point_spread` and :func:`~mne.minimum_norm.get_cross_talk` to correctly map public mode names (``'max'``, ``'svd'``) to internal names, expose previously hidden modes ``'sum'`` and ``'maxval'`` in the public API, and raise :class:`ValueError` for invalid mode values, by :newcontrib:`Famous077`.

mne/minimum_norm/resolution_matrix.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,16 @@ def _get_psf_ctf(
155155
# summarise PSFs/CTFs across vertices if requested
156156
pca_var = None # variances computed only if return_pca_vars=True
157157
if mode is not None:
158+
# mapping public mode names to internal names
159+
_mode_map = {
160+
"max": "maxnorm",
161+
"svd": "pca",
162+
"mean": "mean",
163+
"sum": "sum",
164+
"maxval": "maxval",
165+
}
158166
funcs, pca_var = _summarise_psf_ctf(
159-
funcs, mode, n_comp, return_pca_vars, nn
167+
funcs, _mode_map[mode], n_comp, return_pca_vars, nn
160168
)
161169

162170
if not vector: # if one value per vertex requested
@@ -193,11 +201,14 @@ def _get_psf_ctf(
193201

194202
def _check_get_psf_ctf_params(mode, n_comp, return_pca_vars):
195203
"""Check input parameters of _get_psf_ctf() for consistency."""
196-
if mode in [None, "sum", "mean"] and n_comp > 1:
204+
valid_modes = (None, "mean", "max", "svd", "sum", "maxval")
205+
if mode not in valid_modes:
206+
raise ValueError(f"mode must be one of {valid_modes}, got {mode!r} instead.")
207+
if mode in [None, "mean", "sum"] and n_comp > 1:
197208
msg = f"n_comp must be 1 for mode={mode}."
198209
raise ValueError(msg)
199-
if mode != "pca" and return_pca_vars:
200-
msg = "SVD variances can only be returned if mode=pca."
210+
if mode != "svd" and return_pca_vars:
211+
msg = "SVD variances can only be returned if mode='svd'."
201212
raise ValueError(msg)
202213

203214

mne/minimum_norm/tests/test_resolution_matrix.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ def test_resolution_matrix_free(src_type, fwd_volume_small):
6868
)
6969
assert_array_almost_equal(rm_mne_free, rm_mne_free.T)
7070
# check various summary and normalisation options
71-
for mode in [None, "sum", "mean", "maxval", "maxnorm", "pca"]:
71+
for mode in [None, "mean", "max", "svd", "sum", "maxval"]:
7272
n_comps = [1, 3]
73-
if mode in [None, "sum", "mean"]:
73+
if mode in [None, "mean", "sum"]:
7474
n_comps = [1]
7575
for n_comp in n_comps:
7676
for norm in [None, "max", "norm", True]:
@@ -114,7 +114,7 @@ def test_resolution_matrix_free(src_type, fwd_volume_small):
114114
# There is an ambiguity in the sign flip from the PCA here.
115115
# Ideally we would use the normals to fix it, but it's not
116116
# trivial.
117-
if mode == "pca" and n_comp == 3:
117+
if mode == "svd" and n_comp == 3:
118118
stc_psf_free = abs(stc_psf_free)
119119
stc_ctf_free = abs(stc_psf_free)
120120
assert_array_almost_equal(
@@ -184,9 +184,9 @@ def test_resolution_matrix_fixed():
184184
# Some arbitrary vertex numbers
185185
idx = [1, 100, 400]
186186
# check various summary and normalisation options
187-
for mode in [None, "sum", "mean", "maxval", "maxnorm", "pca"]:
187+
for mode in [None, "mean", "max", "svd", "sum", "maxval"]:
188188
n_comps = [1, 3]
189-
if mode in [None, "sum", "mean"]:
189+
if mode in [None, "mean", "sum"]:
190190
n_comps = [1]
191191
for n_comp in n_comps:
192192
for norm in [None, "max", "norm", True]:
@@ -217,7 +217,7 @@ def test_resolution_matrix_fixed():
217217
rm_mne,
218218
forward_fxd["src"],
219219
idx,
220-
mode=mode,
220+
mode="svd",
221221
n_comp=n_comp,
222222
norm="norm",
223223
return_pca_vars=True,
@@ -226,7 +226,7 @@ def test_resolution_matrix_fixed():
226226
rm_mne,
227227
forward_fxd["src"],
228228
idx,
229-
mode=mode,
229+
mode="svd",
230230
n_comp=n_comp,
231231
norm="norm",
232232
return_pca_vars=True,

mne/utils/docs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,7 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):
27232723
"""
27242724

27252725
docdict["mode_pctf"] = """
2726-
mode : None | 'mean' | 'max' | 'svd'
2726+
mode : None | 'mean' | 'max' | 'svd' | 'maxval' | 'sum'
27272727
Compute summary of PSFs/CTFs across all indices specified in 'idx'.
27282728
Can be:
27292729
@@ -2734,6 +2734,9 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):
27342734
n_comp largest PSFs/CTFs.
27352735
* 'svd' : SVD components across PSFs/CTFs across vertices. Returns the
27362736
n_comp first SVD components.
2737+
* 'maxval' : PSFs/CTFs with maximum absolute value across vertices.
2738+
Returns the n_comp largest PSFs/CTFs.
2739+
* 'sum' : Sum of PSFs/CTFs across vertices.
27372740
"""
27382741

27392742
docdict["mode_tfr_plot"] = """

0 commit comments

Comments
 (0)