Skip to content

Commit dd62f2b

Browse files
authored
Update tests for sklearn 1.2, server issue (#1200)
* Relax error checking * Skip unit test due to server issue openml/OpenML#1180 * Account for rename parameter `base_estimator` to `estimator` in sk 1.2 * Update n_init parameter for sklearn 1.2 * Test for more specific exceptions
1 parent 467f6eb commit dd62f2b

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

tests/test_extensions/test_sklearn_extension/test_sklearn_extension.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,15 @@ def test_serialize_model_clustering(self):
338338
)
339339
)
340340
else:
341+
n_init = '"warn"' if LooseVersion(sklearn.__version__) >= "1.2" else "10"
341342
fixture_parameters = OrderedDict(
342343
(
343344
("algorithm", '"lloyd"'),
344345
("copy_x", "true"),
345346
("init", '"k-means++"'),
346347
("max_iter", "300"),
347348
("n_clusters", "8"),
348-
("n_init", "10"),
349+
("n_init", n_init),
349350
("random_state", "null"),
350351
("tol", "0.0001"),
351352
("verbose", "0"),
@@ -358,13 +359,13 @@ def test_serialize_model_clustering(self):
358359
)
359360
structure = serialization.get_structure("name")
360361

361-
self.assertEqual(serialization.name, fixture_name)
362-
self.assertEqual(serialization.class_name, fixture_name)
363-
self.assertEqual(serialization.custom_name, fixture_short_name)
364-
self.assertEqual(serialization.description, fixture_description)
365-
self.assertEqual(serialization.parameters, fixture_parameters)
366-
self.assertEqual(serialization.dependencies, version_fixture)
367-
self.assertDictEqual(structure, fixture_structure)
362+
assert serialization.name == fixture_name
363+
assert serialization.class_name == fixture_name
364+
assert serialization.custom_name == fixture_short_name
365+
assert serialization.description == fixture_description
366+
assert serialization.parameters == fixture_parameters
367+
assert serialization.dependencies == version_fixture
368+
assert structure == fixture_structure
368369

369370
def test_serialize_model_with_subcomponent(self):
370371
model = sklearn.ensemble.AdaBoostClassifier(
@@ -1449,22 +1450,19 @@ def test_deserialize_complex_with_defaults(self):
14491450
pipe_orig = sklearn.pipeline.Pipeline(steps=steps)
14501451

14511452
pipe_adjusted = sklearn.clone(pipe_orig)
1452-
if LooseVersion(sklearn.__version__) < "0.23":
1453-
params = {
1454-
"Imputer__strategy": "median",
1455-
"OneHotEncoder__sparse": False,
1456-
"Estimator__n_estimators": 10,
1457-
"Estimator__base_estimator__n_estimators": 10,
1458-
"Estimator__base_estimator__base_estimator__learning_rate": 0.1,
1459-
}
1460-
else:
1461-
params = {
1462-
"Imputer__strategy": "mean",
1463-
"OneHotEncoder__sparse": True,
1464-
"Estimator__n_estimators": 50,
1465-
"Estimator__base_estimator__n_estimators": 10,
1466-
"Estimator__base_estimator__base_estimator__learning_rate": 0.1,
1467-
}
1453+
impute_strategy = "median" if LooseVersion(sklearn.__version__) < "0.23" else "mean"
1454+
sparse = LooseVersion(sklearn.__version__) >= "0.23"
1455+
estimator_name = (
1456+
"base_estimator" if LooseVersion(sklearn.__version__) < "1.2" else "estimator"
1457+
)
1458+
params = {
1459+
"Imputer__strategy": impute_strategy,
1460+
"OneHotEncoder__sparse": sparse,
1461+
"Estimator__n_estimators": 10,
1462+
f"Estimator__{estimator_name}__n_estimators": 10,
1463+
f"Estimator__{estimator_name}__{estimator_name}__learning_rate": 0.1,
1464+
}
1465+
14681466
pipe_adjusted.set_params(**params)
14691467
flow = self.extension.model_to_flow(pipe_adjusted)
14701468
pipe_deserialized = self.extension.flow_to_model(flow, initialize_with_defaults=True)

tests/test_runs/test_run_functions.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,19 @@ def test_check_erronous_sklearn_flow_fails(self):
410410

411411
# Invalid parameter values
412412
clf = LogisticRegression(C="abc", solver="lbfgs")
413-
with self.assertRaisesRegex(
414-
ValueError,
415-
r"Penalty term must be positive; got \(C=u?'abc'\)", # u? for 2.7/3.4-6 compability
416-
):
413+
# The exact error message depends on scikit-learn version.
414+
# Because the sklearn-extension module is to be separated,
415+
# I will simply relax specifics of the raised Error.
416+
# old: r"Penalty term must be positive; got \(C=u?'abc'\)"
417+
# new: sklearn.utils._param_validation.InvalidParameterError:
418+
# The 'C' parameter of LogisticRegression must be a float in the range (0, inf]. Got 'abc' instead. # noqa: E501
419+
try:
420+
from sklearn.utils._param_validation import InvalidParameterError
421+
422+
exceptions = (ValueError, InvalidParameterError)
423+
except ImportError:
424+
exceptions = (ValueError,)
425+
with self.assertRaises(exceptions):
417426
openml.runs.run_model_on_task(
418427
task=task,
419428
model=clf,
@@ -680,6 +689,7 @@ def get_ct_cf(nominal_indices, numeric_indices):
680689
sentinel=sentinel,
681690
)
682691

692+
@unittest.skip("https://github.com/openml/OpenML/issues/1180")
683693
@unittest.skipIf(
684694
LooseVersion(sklearn.__version__) < "0.20",
685695
reason="columntransformer introduction in 0.20.0",

0 commit comments

Comments
 (0)