Skip to content

Commit 6b9e392

Browse files
authored
TST Fix test_frozen.py not thread safe (scikit-learn#34449)
1 parent 20868a7 commit 6b9e392

1 file changed

Lines changed: 19 additions & 29 deletions

File tree

sklearn/frozen/tests/test_frozen.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,38 @@
2929
from sklearn.utils.validation import check_is_fitted
3030

3131

32-
@pytest.fixture
33-
def regression_dataset():
34-
return make_regression()
35-
36-
37-
@pytest.fixture
38-
def classification_dataset():
39-
return make_classification()
40-
41-
4232
@pytest.mark.parametrize(
43-
"estimator, dataset",
33+
"estimator, make_dataset",
4434
[
45-
(LinearRegression(), "regression_dataset"),
46-
(LogisticRegression(), "classification_dataset"),
47-
(make_pipeline(StandardScaler(), LinearRegression()), "regression_dataset"),
35+
(LinearRegression(), make_regression),
36+
(LogisticRegression(), make_classification),
37+
(make_pipeline(StandardScaler(), LinearRegression()), make_regression),
4838
(
4939
make_pipeline(StandardScaler(), LogisticRegression()),
50-
"classification_dataset",
40+
make_classification,
5141
),
52-
(StandardScaler(), "regression_dataset"),
53-
(KMeans(), "regression_dataset"),
54-
(LocalOutlierFactor(), "regression_dataset"),
42+
(StandardScaler(), make_regression),
43+
(KMeans(), make_regression),
44+
(LocalOutlierFactor(), make_regression),
5545
(
5646
make_column_transformer(
5747
(StandardScaler(), [0]),
5848
(RobustScaler(), [1]),
5949
),
60-
"regression_dataset",
50+
make_regression,
6151
),
6252
],
6353
)
6454
@pytest.mark.parametrize(
6555
"method",
6656
["predict", "predict_proba", "predict_log_proba", "decision_function", "transform"],
6757
)
68-
def test_frozen_methods(estimator, dataset, request, method):
58+
def test_frozen_methods(estimator, make_dataset, method):
6959
"""Test that frozen.fit doesn't do anything, and that all other methods are
7060
exposed by the frozen estimator and return the same values as the estimator.
7161
"""
7262
estimator = clone(estimator)
73-
X, y = request.getfixturevalue(dataset)
63+
X, y = make_dataset()
7464
set_random_state(estimator)
7565
estimator.fit(X, y)
7666
frozen = FrozenEstimator(estimator)
@@ -87,7 +77,7 @@ def test_frozen_methods(estimator, dataset, request, method):
8777

8878

8979
@config_context(enable_metadata_routing=True)
90-
def test_frozen_metadata_routing(regression_dataset):
80+
def test_frozen_metadata_routing():
9181
"""Test that metadata routing works with frozen estimators."""
9282

9383
class ConsumesMetadata(BaseEstimator):
@@ -106,7 +96,7 @@ def predict(self, X, metadata=None):
10696
assert metadata is not None
10797
return np.ones(len(X))
10898

109-
X, y = regression_dataset
99+
X, y = make_regression()
110100
pipeline = make_pipeline(
111101
ConsumesMetadata(on_fit=True, on_predict=True)
112102
.set_fit_request(metadata=True)
@@ -133,7 +123,7 @@ def predict(self, X, metadata=None):
133123
frozen.predict(X, metadata="test")
134124

135125

136-
def test_composite_fit(classification_dataset):
126+
def test_composite_fit():
137127
"""Test that calling fit_transform and fit_predict doesn't call fit."""
138128

139129
class Estimator(BaseEstimator):
@@ -152,7 +142,7 @@ def fit_predict(self, X, y=None):
152142
# only here to test that it doesn't get called
153143
... # pragma: no cover
154144

155-
X, y = classification_dataset
145+
X, y = make_classification()
156146
est = Estimator().fit(X, y)
157147
frozen = FrozenEstimator(est)
158148

@@ -164,18 +154,18 @@ def fit_predict(self, X, y=None):
164154
assert frozen._fit_counter == 1
165155

166156

167-
def test_clone_frozen(regression_dataset):
157+
def test_clone_frozen():
168158
"""Test that cloning a frozen estimator keeps the frozen state."""
169-
X, y = regression_dataset
159+
X, y = make_regression()
170160
estimator = LinearRegression().fit(X, y)
171161
frozen = FrozenEstimator(estimator)
172162
cloned = clone(frozen)
173163
assert cloned.estimator is estimator
174164

175165

176-
def test_check_is_fitted(regression_dataset):
166+
def test_check_is_fitted():
177167
"""Test that check_is_fitted works on frozen estimators."""
178-
X, y = regression_dataset
168+
X, y = make_regression()
179169

180170
estimator = LinearRegression()
181171
frozen = FrozenEstimator(estimator)

0 commit comments

Comments
 (0)