Skip to content

Commit d7ca9ff

Browse files
committed
__getattr__ can silently catch attribute error from filters_ property
1 parent 222ed16 commit d7ca9ff

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

mne/decoding/base.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,16 @@ def fit(self, X, y, **fit_params):
476476

477477
@property
478478
def filters_(self):
479-
check_is_fitted(self)
479+
check_is_fitted(self.model_)
480480
if hasattr(self.model_, "coef_"):
481481
# Standard Linear Model
482482
filters = self.model_.coef_
483483
elif hasattr(self.model_, "estimators_"):
484484
# Linear model with OneVsRestClassifier
485485
filters = np.vstack([est.coef_ for est in self.model_.estimators_])
486-
elif hasattr(self.model_.best_estimator_, "coef_"):
486+
elif hasattr(self.model_, "best_estimator_") and hasattr(
487+
self.model_.best_estimator_, "coef_"
488+
):
487489
# Linear Model with GridSearchCV
488490
filters = self.model_.best_estimator_.coef_
489491
else:
@@ -511,6 +513,11 @@ def model(self):
511513
def model(self, value):
512514
self._orig_model = value
513515

516+
# XXX Remove this after 'model' warning cycle
517+
def __repr__(self):
518+
"""Avoid FutureWarning from filter_ when printing the instance."""
519+
return f"LinearModel(model={self._orig_model})"
520+
514521

515522
def _set_cv(cv, estimator=None, X=None, y=None):
516523
"""Set the default CV depending on whether clf is classifier/regressor."""

mne/decoding/tests/test_base.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,16 @@ def test_linearmodel():
385385
clf = LinearModel(LinearDiscriminantAnalysis())
386386
_ = clf.fit_transform(X, y)
387387

388+
# check that model has to have coef_, RBF-SVM doesn't
389+
clf = LinearModel(svm.SVC(kernel="rbf"))
390+
with pytest.raises(ValueError, match="does not have a `coef_`"):
391+
clf.fit(X, y)
392+
393+
# check that model has to be a predictor
394+
clf = LinearModel(StandardScaler())
395+
with pytest.raises(ValueError, match="classifier or regressor"):
396+
clf.fit(X, y)
397+
388398
# check categorical target fit in standard linear model with GridSearchCV
389399
parameters = {"kernel": ["linear"], "C": [1, 10]}
390400
clf = LinearModel(
@@ -421,11 +431,6 @@ def test_linearmodel():
421431
wrong_y = rng.rand(n, n_features, 99)
422432
clf.fit(X, wrong_y)
423433

424-
# check that model has to be a predictor
425-
clf = LinearModel(StandardScaler())
426-
with pytest.raises(ValueError, match="classifier or regressor"):
427-
clf.fit(X, Y)
428-
429434

430435
def test_cross_val_multiscore():
431436
"""Test cross_val_multiscore for computing scores on decoding over time."""

0 commit comments

Comments
 (0)