8000 MNT minimally revert strict_mode by glemaitre · Pull Request #18905 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

MNT minimally revert strict_mode #18905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 1 addition & 74 deletions sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,22 @@
from functools import partial

import pytest
import numpy as np

from sklearn.utils import all_estimators
from sklearn.utils._testing import ignore_warnings
from sklearn.exceptions import ConvergenceWarning, SkipTestWarning
from sklearn.exceptions import ConvergenceWarning
from sklearn.utils.estimator_checks import check_estimator

import sklearn
from sklearn.base import BiclusterMixin

from sklearn.decomposition import PCA
from sklearn.decomposition import NMF
from sklearn.linear_model._base import LinearClassifierMixin
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.svm import NuSVC

from sklearn.utils import IS_PYPY
from sklearn.utils._testing import SkipTest
Expand All @@ -45,7 +42,6 @@
parametrize_with_checks,
check_n_features_in_after_fitting,
)
from sklearn.utils.validation import check_non_negative, check_array


def test_all_estimator_no_base_class():
Expand Down Expand Up @@ -217,75 +213,6 @@ def test_class_support_removed():
parametrize_with_checks([LogisticRegression])


class MyNMFWithBadErrorMessage(NMF):
# Same as NMF but raises an uninformative error message if X has negative
# value. This estimator would fail the check suite in strict mode,
# specifically it would fail check_fit_non_negative
# FIXME : should be removed in 0.26
def __init__(self):
super().__init__()
self.init = 'nndsvda'
self.max_iter = 500

def fit(self, X, y=None, **params):
X = check_array(X, accept_sparse=('csr', 'csc'),
dtype=[np.float64, np.float32])
try:
check_non_negative(X, whom='')
except ValueError:
raise ValueError("Some non-informative error msg")

return super().fit(X, y, **params)


def test_strict_mode_check_estimator():
# Tests various conditions for the strict mode of check_estimator()
# Details are in the comments

# LogisticRegression has no _xfail_checks, so when strict_mode is on, there
# should be no skipped tests.
with pytest.warns(None) as catched_warnings:
check_estimator(LogisticRegression(), strict_mode=True)
assert not any(isinstance(w, SkipTestWarning) for w in catched_warnings)
# When strict mode is off, check_n_features should be skipped because it's
# a fully strict check
msg_check_n_features_in = 'check_n_features_in is fully strict '
with pytest.warns(SkipTestWarning, match=msg_check_n_features_in):
check_estimator(LogisticRegression(), strict_mode=False)

# NuSVC has some _xfail_checks. They should be skipped regardless of
# strict_mode
with pytest.warns(SkipTestWarning,
match='fails for the decision_function method'):
check_estimator(NuSVC(), strict_mode=True)
# When strict mode is off, check_n_features_in is skipped along with the
# rest of the xfail_checks
with pytest.warns(SkipTestWarning, match=msg_check_n_features_in):
check_estimator(NuSVC(), strict_mode=False)

# MyNMF will fail check_fit_non_negative() in strict mode because it yields
# a bad error message
with pytest.raises(
AssertionError, match="The error message should contain"
):
check_estimator(MyNMFWithBadErrorMessage(), strict_mode=True)
# However, it should pass the test suite in non-strict mode because when
# strict mode is off, check_fit_non_negative() will not check the exact
# error messsage. (We still assert that the warning from
# check_n_features_in is raised)
with pytest.warns(SkipTestWarning, match=msg_check_n_features_in):
check_estimator(MyNMFWithBadErrorMessage(), strict_mode=False)


@parametrize_with_checks([LogisticRegression(),
NuSVC(),
MyNMFWithBadErrorMessage()],
strict_mode=False)
def test_strict_mode_parametrize_with_checks(estimator, check):
# Ideally we should assert that the strict checks are Xfailed...
check(estimator)


def _generate_search_cv_instances():
for SearchCV, (Estimator, param_grid) in zip(
[GridSearchCV, RandomizedSearchCV],
Expand Down
Loading
0