Skip to content

Commit

Permalink
Update warnings (#1346)
Browse files Browse the repository at this point in the history
* Added ignored_warnings file

* Use ignored_warnings file

* Test regressors with 1d, 1d as 2d and 2d targets

* Flake'd

* Fix broken relative imports to ignore_warnings

* Removed print and updated parameter type for tests

* Added warning catches to fit methods in tests

* Added more warning catches

* Flake'd

* Created top-level module to allow relativei imports

* Deleted blank line in __init__

* Remove uneeded ignore warnings from tests

* Fix bad indent

* Fix github merge conflict editor whitespaces and indents
  • Loading branch information
eddiebergman committed Aug 18, 2022
1 parent 45a7df5 commit 14dd5ab
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
2 changes: 1 addition & 1 deletion test/test_pipeline/components/classification/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sklearn.metrics
import numpy as np

from test.test_pipeline.ignored_warnings import ignore_warnings, classifier_warnings
from ...ignored_warnings import ignore_warnings, classifier_warnings


class BaseClassificationComponentTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
get_dataset
import sklearn.metrics

from test.test_pipeline.ignored_warnings import ignore_warnings, feature_preprocessing_warnings
from ...ignored_warnings import ignore_warnings, feature_preprocessing_warnings


class LiblinearComponentTest(PreprocessingTestCase):
Expand Down
12 changes: 9 additions & 3 deletions test/test_pipeline/components/regression/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def test_default_boston(self):

for _ in range(2):

with ignore_warnings(regressor_warnings):
predictions, targets, n_calls = _test_regressor(
dataset="boston",
Regressor=self.module
)

with ignore_warnings(regressor_warnings):
predictions, targets, n_calls = _test_regressor(
dataset="boston",
Expand Down Expand Up @@ -331,7 +337,7 @@ def test_fit_and_predict_with_1d_targets_as_1d(
regressor: Type[RegressorChoice],
X: np.ndarray,
y: np.ndarray
):
) -> None:
"""Test that all pipelines work with 1d target types
Parameters
Expand Down Expand Up @@ -374,7 +380,7 @@ def test_fit_and_predict_with_1d_targets_as_2d(
regressor: Type[RegressorChoice],
X: np.ndarray,
y: np.ndarray
):
) -> None:
"""Test that all pipelines work with 1d target types when they are wrapped as 2d
Parameters
Expand Down Expand Up @@ -423,7 +429,7 @@ def test_fit_and_predict_with_2d_targets(
regressor: Type[RegressorChoice],
X: np.ndarray,
y: np.ndarray
):
) -> None:
"""Test that all pipelines work with 2d target types
Parameters
Expand Down
23 changes: 22 additions & 1 deletion test/test_pipeline/ignored_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Iterator, Tuple

import warnings

from sklearn.exceptions import ConvergenceWarning


Expand Down Expand Up @@ -68,14 +69,34 @@
r" optimization hasn't converged yet\."
)
),
(
ConvergenceWarning, ( # From FastICA
r"FastICA did not converge\."
r" Consider increasing tolerance or the maximum number of iterations\."
)
),
(
UserWarning, ( # From LDA (Linear Discriminant Analysis)
r"Variables are collinear"
)
),
(
UserWarning, (
r"Clustering metrics expects discrete values but received continuous values"
r" for label, and multiclass values for target"
)
)
]

feature_preprocessing_warnings = [
(
ConvergenceWarning, ( # From liblinear
r"Liblinear failed to converge, increase the number of iterations."
)
)
]

ignored_warnings = regressor_warnings + classifier_warnings
ignored_warnings = regressor_warnings + classifier_warnings + feature_preprocessing_warnings


@contextmanager
Expand Down
45 changes: 32 additions & 13 deletions test/test_pipeline/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def test_default_configuration(self):

auto = SimpleClassificationPipeline(random_state=1)

auto = auto.fit(X_train, Y_train)
with ignore_warnings(classifier_warnings):
auto = auto.fit(X_train, Y_train)

predictions = auto.predict(X_test)

acc = sklearn.metrics.accuracy_score(predictions, Y_test)
Expand All @@ -196,7 +198,9 @@ def test_default_configuration_multilabel(self):
default = cs.get_default_configuration()
classifier.set_hyperparameters(default)

classifier = classifier.fit(X_train, Y_train)
with ignore_warnings(classifier_warnings):
classifier = classifier.fit(X_train, Y_train)

predictions = classifier.predict(X_test)

acc = sklearn.metrics.accuracy_score(predictions, Y_test)
Expand All @@ -221,10 +225,12 @@ def test_default_configuration_iterative_fit(self):
random_state=0
)
classifier.fit_transformer(X_train, Y_train)
for i in range(1, 11):
classifier.iterative_fit(X_train, Y_train)
n_estimators = classifier.steps[-1][-1].choice.estimator.n_estimators
self.assertEqual(n_estimators, i)

with ignore_warnings(classifier_warnings):
for i in range(1, 11):
classifier.iterative_fit(X_train, Y_train)
n_estimators = classifier.steps[-1][-1].choice.estimator.n_estimators
self.assertEqual(n_estimators, i)

def test_repr(self):
"""Test that the default pipeline can be converted to its representation and
Expand Down Expand Up @@ -727,7 +733,9 @@ def test_predict_batched(self):

# Multiclass
X_train, Y_train, X_test, Y_test = get_dataset(dataset='digits')
cls.fit(X_train, Y_train)

with ignore_warnings(classifier_warnings):
cls.fit(X_train, Y_train)

X_test_ = X_test.copy()
prediction_ = cls.predict_proba(X_test_)
Expand Down Expand Up @@ -759,7 +767,8 @@ def test_predict_batched_sparse(self):

# Multiclass
X_train, Y_train, X_test, Y_test = get_dataset(dataset='digits', make_sparse=True)
cls.fit(X_train, Y_train)
with ignore_warnings(classifier_warnings):
cls.fit(X_train, Y_train)

X_test_ = X_test.copy()
prediction_ = cls.predict_proba(X_test_)
Expand Down Expand Up @@ -788,7 +797,8 @@ def test_predict_proba_batched(self):
cls = SimpleClassificationPipeline(include={'classifier': ['sgd']})
X_train, Y_train, X_test, Y_test = get_dataset(dataset='digits')

cls.fit(X_train, Y_train)
with ignore_warnings(classifier_warnings):
cls.fit(X_train, Y_train)

X_test_ = X_test.copy()
prediction_ = cls.predict_proba(X_test_)
Expand All @@ -808,7 +818,9 @@ def test_predict_proba_batched(self):
X_train, Y_train, X_test, Y_test = get_dataset(dataset='digits')
Y_train = np.array(list([(list([1 if i != y else 0 for i in range(10)]))
for y in Y_train]))
cls.fit(X_train, Y_train)

with ignore_warnings(classifier_warnings):
cls.fit(X_train, Y_train)

X_test_ = X_test.copy()
prediction_ = cls.predict_proba(X_test_)
Expand Down Expand Up @@ -842,7 +854,9 @@ def test_predict_proba_batched_sparse(self):
X_train, Y_train, X_test, Y_test = get_dataset(dataset='digits', make_sparse=True)
X_test_ = X_test.copy()

cls.fit(X_train, Y_train)
with ignore_warnings(classifier_warnings):
cls.fit(X_train, Y_train)

prediction_ = cls.predict_proba(X_test_)

# The object behind the last step in the pipeline
Expand All @@ -861,10 +875,13 @@ def test_predict_proba_batched_sparse(self):
include={'classifier': ['lda']}
)
X_train, Y_train, X_test, Y_test = get_dataset(dataset='digits', make_sparse=True)

X_test_ = X_test.copy()
Y_train = np.array([[1 if i != y else 0 for i in range(10)] for y in Y_train])

cls.fit(X_train, Y_train)
with ignore_warnings(classifier_warnings):
cls.fit(X_train, Y_train)

prediction_ = cls.predict_proba(X_test_)

# The object behind the last step in the pipeline
Expand All @@ -889,7 +906,9 @@ def test_pipeline_clonability(self):
X_train, Y_train, X_test, Y_test = get_dataset(dataset='iris')

auto = SimpleClassificationPipeline()
auto = auto.fit(X_train, Y_train)

with ignore_warnings(classifier_warnings):
auto = auto.fit(X_train, Y_train)

auto_clone = clone(auto)
auto_clone_params = auto_clone.get_params()
Expand Down

0 comments on commit 14dd5ab

Please sign in to comment.