Skip to content

Commit 171682d

Browse files
committed
FIX: Better fix
1 parent 0475fd1 commit 171682d

4 files changed

Lines changed: 13 additions & 12 deletions

File tree

mne/cov.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,7 @@ def _unpack_epochs(epochs):
11611161

11621162
epochs = np.hstack(epochs)
11631163
n_samples_tot = epochs.shape[-1]
1164+
_check_n_samples(n_samples_tot, len(picks_meeg), on_few_samples)
11641165

11651166
epochs = epochs.T # sklearn | C-order
11661167
cov_data = _compute_covariance_auto(

mne/preprocessing/tests/test_maxwell.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,13 +1883,13 @@ def test_compute_maxwell_basis(regularize, n, int_order):
18831883
raw = read_raw_fif(raw_small_fname).crop(0, 2)
18841884
assert raw.info["bads"] == []
18851885
raw.del_proj()
1886-
rank = compute_rank(raw, on_few_samples="ignore")["meg"]
1886+
rank = compute_rank(raw)["meg"]
18871887
assert rank == 306
18881888
raw.info["bads"] = ["MEG 2443"]
18891889
kwargs = dict(regularize=regularize, int_order=int_order, verbose=True)
18901890
raw_sss = maxwell_filter(raw, **kwargs)
18911891
want = raw_sss.get_data("meg")
1892-
rank = compute_rank(raw_sss, on_few_samples="ignore")["meg"]
1892+
rank = compute_rank(raw_sss)["meg"]
18931893
assert rank == n
18941894
S, pS, reg_moments, n_use_in = compute_maxwell_basis(raw.info, **kwargs)
18951895
assert n_use_in == n

mne/rank.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,15 @@ def _estimate_rank_meeg_signals(
200200
If return_singular is True, the singular values that were
201201
thresholded to determine the rank are also returned.
202202
"""
203-
from .cov import _check_n_samples
204-
205203
picks_list = _picks_by_type(info)
206204
assert data.ndim == 2, data.shape
207-
_check_n_samples(*data.shape[::-1], on_few_samples=on_few_samples)
205+
n_channels, n_samples = data.shape
206+
if n_samples < n_channels:
207+
msg = (
208+
f"Too few samples ({n_samples=} is less than {n_channels=}), "
209+
"rank estimate may be unreliable"
210+
)
211+
_on_missing(on_few_samples, msg, "on_few_samples")
208212
with _scaled_array(data, picks_list, scalings):
209213
out = estimate_rank(
210214
data,

mne/tests/test_cov.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import itertools as itt
66
import sys
7-
from contextlib import nullcontext
87
from inspect import signature
98
from pathlib import Path
109

@@ -802,11 +801,7 @@ def test_low_rank_methods(rank, raw_epochs_events):
802801
empirical=(-15000, -5000), diagonal_fixed=(-700, -600), oas=(-700, -600)
803802
),
804803
}
805-
if rank is None:
806-
ctx = pytest.warns(RuntimeWarning, match="Too few samples")
807-
else:
808-
ctx = nullcontext()
809-
with ctx:
804+
with pytest.warns(RuntimeWarning, match="Too few samples"):
810805
covs = compute_covariance(
811806
epochs, method=methods, return_estimators=True, rank=rank, verbose=True
812807
)
@@ -858,7 +853,8 @@ def test_low_rank_cov(raw_epochs_events):
858853
epochs_meg, method="oas", rank="full", verbose="error"
859854
)
860855
assert _cov_rank(cov_full, epochs_meg.info) == 306
861-
cov_dict = compute_covariance(epochs_meg, method="oas", rank=dict(meg=306))
856+
with pytest.warns(RuntimeWarning, match="few samples"):
857+
cov_dict = compute_covariance(epochs_meg, method="oas", rank=dict(meg=306))
862858
assert _cov_rank(cov_dict, epochs_meg.info) == 306
863859
assert_allclose(cov_full["data"], cov_dict["data"])
864860
cov_dict = compute_covariance(

0 commit comments

Comments
 (0)