Skip to content

Commit c995c8b

Browse files
author
Joan Massich
committed
Migrate assert_raises_regex to pytest.raises
1 parent 8cbcc77 commit c995c8b

20 files changed

+168
-151
lines changed

imblearn/combine/tests/test_smote_enn.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99
from sklearn.utils.testing import assert_allclose, assert_array_equal
10-
from sklearn.utils.testing import assert_raises_regex
10+
from pytest import raises
1111

1212
from imblearn.combine import SMOTEENN
1313
from imblearn.under_sampling import EditedNearestNeighbours
@@ -113,8 +113,8 @@ def test_error_wrong_object():
113113
smote = 'rnd'
114114
enn = 'rnd'
115115
smt = SMOTEENN(smote=smote, random_state=RND_SEED)
116-
assert_raises_regex(ValueError, "smote needs to be a SMOTE",
117-
smt.fit_sample, X, Y)
116+
with raises(ValueError, match="smote needs to be a SMOTE"):
117+
smt.fit_sample(X, Y)
118118
smt = SMOTEENN(enn=enn, random_state=RND_SEED)
119-
assert_raises_regex(ValueError, "enn needs to be an ",
120-
smt.fit_sample, X, Y)
119+
with raises(ValueError, match="enn needs to be an "):
120+
smt.fit_sample(X, Y)

imblearn/combine/tests/test_smote_tomek.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99
from sklearn.utils.testing import assert_allclose, assert_array_equal
10-
from sklearn.utils.testing import assert_raises_regex
10+
from pytest import raises
1111

1212
from imblearn.combine import SMOTETomek
1313
from imblearn.over_sampling import SMOTE
@@ -156,8 +156,8 @@ def test_error_wrong_object():
156156
smote = 'rnd'
157157
tomek = 'rnd'
158158
smt = SMOTETomek(smote=smote, random_state=RND_SEED)
159-
assert_raises_regex(ValueError, "smote needs to be a SMOTE",
160-
smt.fit_sample, X, Y)
159+
with raises(ValueError, match="smote needs to be a SMOTE"):
160+
smt.fit_sample(X, Y)
161161
smt = SMOTETomek(tomek=tomek, random_state=RND_SEED)
162-
assert_raises_regex(ValueError, "tomek needs to be a TomekLinks",
163-
smt.fit_sample, X, Y)
162+
with raises(ValueError, match="tomek needs to be a TomekLinks"):
163+
smt.fit_sample(X, Y)

imblearn/datasets/tests/test_imbalance.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import numpy as np
1212

1313
from sklearn.datasets import load_iris
14-
from sklearn.utils.testing import assert_raises_regex
1514
from sklearn.utils.testing import assert_warns_message
15+
from pytest import raises
1616

1717
from imblearn.datasets import make_imbalance
1818

@@ -24,18 +24,18 @@ def test_make_imbalance_error():
2424
# we are reusing part of utils.check_ratio, however this is not cover in
2525
# the common tests so we will repeat it here
2626
ratio = {0: -100, 1: 50, 2: 50}
27-
assert_raises_regex(ValueError, "in a class cannot be negative",
28-
make_imbalance, X, Y, ratio)
27+
with raises(ValueError, match="in a class cannot be negative"):
28+
make_imbalance(X, Y, ratio)
2929
ratio = {0: 10, 1: 70}
30-
assert_raises_regex(ValueError, "should be less or equal to the original",
31-
make_imbalance, X, Y, ratio)
30+
with raises(ValueError, match="should be less or equal to the original"):
31+
make_imbalance(X, Y, ratio)
3232
y_ = np.zeros((X.shape[0], ))
3333
ratio = {0: 10}
34-
assert_raises_regex(ValueError, "needs to have more than 1 class.",
35-
make_imbalance, X, y_, ratio)
34+
with raises(ValueError, match="needs to have more than 1 class."):
35+
make_imbalance(X, y_, ratio)
3636
ratio = 'random-string'
37-
assert_raises_regex(ValueError, "has to be a dictionary or a function",
38-
make_imbalance, X, Y, ratio)
37+
with raises(ValueError, match="has to be a dictionary or a function"):
38+
make_imbalance(X, Y, ratio)
3939

4040

4141
# FIXME: to be removed in 0.4 due to deprecation

imblearn/datasets/tests/test_zenodo.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
from imblearn.datasets import fetch_datasets
1010
from sklearn.utils.testing import SkipTest, assert_allclose
11-
from sklearn.utils.testing import assert_raises_regex
11+
12+
from pytest import raises
1213

1314
DATASET_SHAPE = {'ecoli': (336, 7),
1415
'optical_digits': (5620, 64),
@@ -84,11 +85,11 @@ def test_fetch_filter():
8485

8586

8687
def test_fetch_error():
87-
assert_raises_regex(ValueError, 'is not a dataset available.',
88-
fetch_datasets, filter_data=tuple(['rnd']))
89-
assert_raises_regex(ValueError, 'dataset with the ID=',
90-
fetch_datasets, filter_data=tuple([-1]))
91-
assert_raises_regex(ValueError, 'dataset with the ID=',
92-
fetch_datasets, filter_data=tuple([100]))
93-
assert_raises_regex(ValueError, 'value in the tuple',
94-
fetch_datasets, filter_data=tuple([1.00]))
88+
with raises(ValueError, match='is not a dataset available.'):
89+
fetch_datasets(filter_data=tuple(['rnd']))
90+
with raises(ValueError, match='dataset with the ID='):
91+
fetch_datasets(filter_data=tuple([-1]))
92+
with raises(ValueError, match='dataset with the ID='):
93+
fetch_datasets(filter_data=tuple([100]))
94+
with raises(ValueError, match='value in the tuple'):
95+
fetch_datasets(filter_data=tuple([1.00]))

imblearn/ensemble/tests/test_balance_cascade.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99
from sklearn.utils.testing import assert_array_equal
10-
from sklearn.utils.testing import assert_raises_regex
10+
1111
from sklearn.ensemble import RandomForestClassifier
1212

1313
from imblearn.ensemble import BalanceCascade
@@ -365,5 +365,5 @@ def test_give_classifier_wrong_obj():
365365
classifier = 2
366366
bc = BalanceCascade(ratio=ratio, random_state=RND_SEED,
367367
return_indices=True, estimator=classifier)
368-
assert_raises_regex(ValueError, "Invalid parameter `estimator`",
369-
bc.fit_sample, X, Y)
368+
with raises(ValueError, match="Invalid parameter `estimator`"):
369+
bc.fit_sample(X, Y)

imblearn/over_sampling/tests/test_adasyn.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
import numpy as np
99
from sklearn.utils.testing import assert_allclose, assert_array_equal
10-
from sklearn.utils.testing import assert_raises_regex
1110
from sklearn.neighbors import NearestNeighbors
1211

1312
from imblearn.over_sampling import ADASYN
1413

14+
from pytest import raises
15+
1516
RND_SEED = 0
1617
X = np.array([[0.11622591, -0.0317206], [0.77481731, 0.60935141],
1718
[1.25192108, -0.22367336], [0.53366841, -0.30312976],
@@ -141,5 +142,5 @@ def test_ada_fit_sample_nn_obj():
141142
def test_ada_wrong_nn_obj():
142143
nn = 'rnd'
143144
ada = ADASYN(random_state=RND_SEED, n_neighbors=nn)
144-
assert_raises_regex(ValueError, "has to be one of",
145-
ada.fit_sample, X, Y)
145+
with raises(ValueError, match="has to be one of"):
146+
ada.fit_sample(X, Y)

imblearn/over_sampling/tests/test_smote.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
import numpy as np
99
from sklearn.utils.testing import assert_allclose, assert_array_equal
10-
from sklearn.utils.testing import assert_raises_regex
1110
from sklearn.neighbors import NearestNeighbors
1211
from sklearn.svm import SVC
1312

1413
from imblearn.over_sampling import SMOTE
1514

15+
from pytest import raises
16+
1617
RND_SEED = 0
1718
X = np.array([[0.11622591, -0.0317206], [0.77481731, 0.60935141],
1819
[1.25192108, -0.22367336], [0.53366841, -0.30312976],
@@ -31,8 +32,8 @@
3132
def test_smote_wrong_kind():
3233
kind = 'rnd'
3334
smote = SMOTE(kind=kind, random_state=RND_SEED)
34-
assert_raises_regex(ValueError, "Unknown kind for SMOTE",
35-
smote.fit_sample, X, Y)
35+
with raises(ValueError, match="Unknown kind for SMOTE"):
36+
smote.fit_sample(X, Y)
3637

3738

3839
def test_sample_regular():
@@ -203,19 +204,19 @@ def test_wrong_nn():
203204
nn_k = NearestNeighbors(n_neighbors=6)
204205
smote = SMOTE(
205206
random_state=RND_SEED, kind=kind, k_neighbors=nn_k, m_neighbors=nn_m)
206-
assert_raises_regex(ValueError, "has to be one of",
207-
smote.fit_sample, X, Y)
207+
with raises(ValueError, match="has to be one of"):
208+
smote.fit_sample(X, Y)
208209
nn_k = 'rnd'
209210
nn_m = NearestNeighbors(n_neighbors=10)
210211
smote = SMOTE(
211212
random_state=RND_SEED, kind=kind, k_neighbors=nn_k, m_neighbors=nn_m)
212-
assert_raises_regex(ValueError, "has to be one of",
213-
smote.fit_sample, X, Y)
213+
with raises(ValueError, match="has to be one of"):
214+
smote.fit_sample(X, Y)
214215
kind = 'regular'
215216
nn_k = 'rnd'
216217
smote = SMOTE(random_state=RND_SEED, kind=kind, k_neighbors=nn_k)
217-
assert_raises_regex(ValueError, "has to be one of",
218-
smote.fit_sample, X, Y)
218+
with raises(ValueError, match="has to be one of"):
219+
smote.fit_sample(X, Y)
219220

220221

221222
def test_sample_regular_with_nn_svm():
@@ -250,5 +251,5 @@ def test_sample_regular_wrong_svm():
250251
smote = SMOTE(
251252
random_state=RND_SEED, kind=kind, k_neighbors=nn_k, svm_estimator=svm)
252253

253-
assert_raises_regex(ValueError, "has to be one of",
254-
smote.fit_sample, X, Y)
254+
with raises(ValueError, match="has to be one of"):
255+
smote.fit_sample(X, Y)

imblearn/tests/test_exceptions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
# License: MIT
55

66

7-
from sklearn.utils.testing import assert_raises_regex
8-
97
from imblearn.exceptions import raise_isinstance_error
108

9+
from pytest import raises
10+
1111

1212
def test_raise_isinstance_error():
1313
var = 10.0
14-
assert_raises_regex(ValueError, "has to be one of",
15-
raise_isinstance_error, 'var', [int], var)
14+
with raises(ValueError, match="has to be one of"):
15+
raise_isinstance_error('var', [int], var)

imblearn/tests/test_pipeline.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import time
1212

1313
import numpy as np
14-
from sklearn.utils.testing import assert_raises_regex
1514
from sklearn.utils.testing import assert_raise_message
1615
from sklearn.utils.testing import assert_array_equal
1716
from sklearn.utils.testing import assert_array_almost_equal
@@ -181,10 +180,9 @@ def test_pipeline_init():
181180
Pipeline()
182181
# Check that we can't instantiate pipelines with objects without fit
183182
# method
184-
assert_raises_regex(TypeError,
185-
'Last step of Pipeline should implement fit. '
186-
'.*NoFit.*',
187-
Pipeline, [('clf', NoFit())])
183+
error_regex = 'Last step of Pipeline should implement fit. .*NoFit.*'
184+
with raises(TypeError, match=error_regex):
185+
Pipeline([('clf', NoFit())])
188186
# Smoke test with only an estimator
189187
clf = NoTrans()
190188
pipe = Pipeline([('svc', clf)])
@@ -206,9 +204,9 @@ def test_pipeline_init():
206204

207205
# Check that we can't instantiate with non-transformers on the way
208206
# Note that NoTrans implements fit, but not transform
209-
assert_raises_regex(TypeError,
210-
'implement fit and transform or sample',
211-
Pipeline, [('t', NoTrans()), ('svc', clf)])
207+
error_regex = 'implement fit and transform or sample'
208+
with raises(TypeError, match=error_regex):
209+
Pipeline([('t', NoTrans()), ('svc', clf)])
212210

213211
# Check that params are set
214212
pipe.set_params(svc__C=0.1)
@@ -400,9 +398,9 @@ def test_fit_predict_on_pipeline_without_fit_predict():
400398
scaler = StandardScaler()
401399
pca = PCA(svd_solver='full')
402400
pipe = Pipeline([('scaler', scaler), ('pca', pca)])
403-
assert_raises_regex(AttributeError,
404-
"'PCA' object has no attribute 'fit_predict'",
405-
getattr, pipe, 'fit_predict')
401+
error_regex = "'PCA' object has no attribute 'fit_predict'"
402+
with raises(AttributeError, match=error_regex):
403+
getattr(pipe, 'fit_predict')
406404

407405

408406
def test_fit_predict_with_intermediate_fit_params():
@@ -620,9 +618,10 @@ def test_pipeline_wrong_memory():
620618
memory = 1
621619
cached_pipe = Pipeline([('transf', DummyTransf()), ('svc', SVC())],
622620
memory=memory)
623-
assert_raises_regex(ValueError, "'memory' should either be a string or a"
624-
" joblib.Memory instance, got 'memory=1' instead.",
625-
cached_pipe.fit, X, y)
621+
error_regex = ("'memory' should either be a string or a joblib.Memory"
622+
" instance, got 'memory=1' instead.")
623+
with raises(ValueError, match=error_regex):
624+
cached_pipe.fit(X, y)
626625

627626

628627
def test_pipeline_memory_transformer():

imblearn/under_sampling/prototype_generation/tests/test_cluster_centroids.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import numpy as np
77
from sklearn.utils.testing import assert_allclose
88
from sklearn.utils.testing import assert_array_equal
9-
from sklearn.utils.testing import assert_raises_regex
9+
from pytest import raises
10+
1011
from sklearn.cluster import KMeans
1112

1213
from imblearn.under_sampling import ClusterCentroids
@@ -79,5 +80,5 @@ def test_fit_sample_wrong_object():
7980
cluster = 'rnd'
8081
cc = ClusterCentroids(
8182
ratio=ratio, random_state=RND_SEED, estimator=cluster)
82-
assert_raises_regex(ValueError, "has to be a KMeans clustering",
83-
cc.fit_sample, X, Y)
83+
with raises(ValueError, match="has to be a KMeans clustering"):
84+
cc.fit_sample(X, Y)

imblearn/under_sampling/prototype_selection/tests/test_condensed_nearest_neighbour.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99
from sklearn.utils.testing import assert_array_equal
10-
from sklearn.utils.testing import assert_raises_regex
10+
from pytest import raises
1111

1212
from sklearn.neighbors import KNeighborsClassifier
1313

@@ -87,5 +87,5 @@ def test_cnn_fit_sample_with_object():
8787
def test_cnn_fit_sample_with_wrong_object():
8888
knn = 'rnd'
8989
cnn = CondensedNearestNeighbour(random_state=RND_SEED, n_neighbors=knn)
90-
assert_raises_regex(ValueError, "has to be a int or an ",
91-
cnn.fit_sample, X, Y)
90+
with raises(ValueError, match="has to be a int or an "):
91+
cnn.fit_sample(X, Y)

imblearn/under_sampling/prototype_selection/tests/test_edited_nearest_neighbours.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99
from sklearn.utils.testing import assert_array_equal
10-
from sklearn.utils.testing import assert_raises_regex
10+
from pytest import raises
1111

1212
from sklearn.neighbors import NearestNeighbors
1313

@@ -102,5 +102,5 @@ def test_enn_not_good_object():
102102
nn = 'rnd'
103103
enn = EditedNearestNeighbours(
104104
n_neighbors=nn, random_state=RND_SEED, kind_sel='mode')
105-
assert_raises_regex(ValueError, "has to be one of",
106-
enn.fit_sample, X, Y)
105+
with raises(ValueError, match="has to be one of"):
106+
enn.fit_sample(X, Y)

imblearn/under_sampling/prototype_selection/tests/test_instance_hardness_threshold.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import numpy as np
99
from sklearn.utils.testing import assert_array_equal
1010
from sklearn.utils.testing import assert_raises
11-
from sklearn.utils.testing import assert_raises_regex
1211
from sklearn.ensemble import GradientBoostingClassifier
12+
from pytest import raises
1313

1414
from imblearn.under_sampling import InstanceHardnessThreshold
1515

@@ -275,5 +275,5 @@ def test_iht_fit_sample_wrong_class_obj():
275275
from sklearn.cluster import KMeans
276276
est = KMeans()
277277
iht = InstanceHardnessThreshold(estimator=est, random_state=RND_SEED)
278-
assert_raises_regex(ValueError, "Invalid parameter `estimator`",
279-
iht.fit_sample, X, Y)
278+
with raises(ValueError, match="Invalid parameter `estimator`"):
279+
iht.fit_sample(X, Y)

imblearn/under_sampling/prototype_selection/tests/test_nearmiss.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import numpy as np
99
from sklearn.utils.testing import assert_array_equal, assert_warns
10-
from sklearn.utils.testing import assert_raises_regex
1110
from sklearn.neighbors import NearestNeighbors
11+
from pytest import raises
1212

1313
from imblearn.under_sampling import NearMiss
1414

@@ -42,8 +42,8 @@ def test_nearmiss_deprecation():
4242
def test_nearmiss_wrong_version():
4343
version = 1000
4444
nm = NearMiss(version=version, random_state=RND_SEED)
45-
assert_raises_regex(ValueError, "must be 1, 2 or 3",
46-
nm.fit_sample, X, Y)
45+
with raises(ValueError, match="must be 1, 2 or 3"):
46+
nm.fit_sample(X, Y)
4747

4848

4949
def test_nm_wrong_nn_obj():
@@ -53,15 +53,15 @@ def test_nm_wrong_nn_obj():
5353
version=VERSION_NEARMISS,
5454
return_indices=True,
5555
n_neighbors=nn)
56-
assert_raises_regex(ValueError, "has to be one of",
57-
nm.fit_sample, X, Y)
56+
with raises(ValueError, match="has to be one of"):
57+
nm.fit_sample(X, Y)
5858
nn3 = 'rnd'
5959
nn = NearestNeighbors(n_neighbors=3)
6060
nm3 = NearMiss(ratio=ratio, random_state=RND_SEED,
6161
version=3, return_indices=True,
6262
n_neighbors=nn, n_neighbors_ver3=nn3)
63-
assert_raises_regex(ValueError, "has to be one of",
64-
nm3.fit_sample, X, Y)
63+
with raises(ValueError, match="has to be one of"):
64+
nm3.fit_sample(X, Y)
6565

6666

6767
def test_nm_fit_sample_auto():

0 commit comments

Comments
 (0)