Skip to content

Commit a127230

Browse files
author
Joan Massich
committed
migrate assert_warns_message to imblearn.utils.testing.warns
1 parent e842084 commit a127230

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

imblearn/datasets/tests/test_imbalance.py

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

1313
from sklearn.datasets import load_iris
14-
from sklearn.utils.testing import assert_warns_message
14+
from imblearn.utils.testing import warns
15+
1516
from pytest import raises
1617

1718
from imblearn.datasets import make_imbalance
@@ -40,12 +41,12 @@ def test_make_imbalance_error():
4041

4142
# FIXME: to be removed in 0.4 due to deprecation
4243
def test_make_imbalance_float():
43-
X_, y_ = assert_warns_message(DeprecationWarning,
44-
"'min_c_' is deprecated in 0.2",
45-
make_imbalance, X, Y, ratio=0.5, min_c_=1)
46-
X_, y_ = assert_warns_message(DeprecationWarning,
47-
"'ratio' being a float is deprecated",
48-
make_imbalance, X, Y, ratio=0.5, min_c_=1)
44+
with warns(DeprecationWarning, match="deprecated in 0.2"):
45+
X_, y_ = make_imbalance(X, Y, ratio=0.5, min_c_=1)
46+
47+
with warns(DeprecationWarning, match="'ratio' being a float"):
48+
X_, y_ = make_imbalance(X, Y, ratio=0.5, min_c_=1)
49+
4950
assert Counter(y_) == {0: 50, 1: 25, 2: 50}
5051
# resample without using min_c_
5152
X_, y_ = make_imbalance(X_, y_, ratio=0.25, min_c_=None)

imblearn/metrics/tests/test_classification.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from sklearn.utils.validation import check_random_state
1919
from sklearn.utils.testing import assert_allclose, assert_array_equal
2020
from sklearn.utils.testing import assert_no_warnings
21-
from sklearn.utils.testing import assert_warns_message, ignore_warnings
21+
from sklearn.utils.testing import ignore_warnings
2222
from sklearn.metrics import accuracy_score, average_precision_score
2323
from sklearn.metrics import brier_score_loss, cohen_kappa_score
2424
from sklearn.metrics import jaccard_similarity_score, precision_score
@@ -32,6 +32,8 @@
3232
from imblearn.metrics import classification_report_imbalanced
3333

3434
from pytest import approx, raises
35+
from imblearn.utils.testing import warns
36+
3537

3638
RND_SEED = 42
3739
R_TOL = 1e-2
@@ -196,15 +198,10 @@ def test_sensitivity_specificity_support_errors():
196198

197199
def test_sensitivity_specificity_unused_pos_label():
198200
# but average != 'binary'; even if data is binary
199-
assert_warns_message(
200-
UserWarning,
201-
"Note that pos_label (set to 2) is "
202-
"ignored when average != 'binary' (got 'macro'). You "
203-
"may use labels=[pos_label] to specify a single "
204-
"positive class.",
205-
sensitivity_specificity_support, [1, 2, 1], [1, 2, 2],
206-
pos_label=2,
207-
average='macro')
201+
with warns(UserWarning, "use labels=\[pos_label\] to specify a single"):
202+
sensitivity_specificity_support([1, 2, 1], [1, 2, 2],
203+
pos_label=2,
204+
average='macro')
208205

209206

210207
def test_geometric_mean_support_binary():

imblearn/utils/tests/test_deprecation.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
# Authors: Guillaume Lemaitre <g.lemaitre58@gmail.com>
44
# License: MIT
55

6-
from sklearn.utils.testing import assert_warns_message
7-
86
from imblearn.utils.deprecation import deprecate_parameter
7+
from imblearn.utils.testing import warns
98

109

1110
class Sampler(object):
@@ -15,7 +14,7 @@ def __init__(self):
1514

1615

1716
def test_deprecate_parameter():
18-
assert_warns_message(DeprecationWarning, "is deprecated from",
19-
deprecate_parameter, Sampler(), '0.2', 'a')
20-
assert_warns_message(DeprecationWarning, "Use 'b' instead.",
21-
deprecate_parameter, Sampler(), '0.2', 'a', 'b')
17+
with warns(DeprecationWarning, match="is deprecated from"):
18+
deprecate_parameter(Sampler(), '0.2', 'a')
19+
with warns(DeprecationWarning, match="Use 'b' instead."):
20+
deprecate_parameter(Sampler(), '0.2', 'a', 'b')

imblearn/utils/tests/test_validation.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sklearn.neighbors.base import KNeighborsMixin
1111
from sklearn.neighbors import NearestNeighbors
1212

13-
from sklearn.utils.testing import assert_warns_message
13+
from imblearn.utils.testing import warns
1414
from pytest import raises
1515

1616
from imblearn.utils import check_neighbors_object
@@ -126,10 +126,11 @@ def test_ratio_dict_over_sampling():
126126
ratio_ = check_ratio(ratio, y, 'over-sampling')
127127
assert ratio_ == {1: 20, 2: 0, 3: 45}
128128
ratio = {1: 70, 2: 140, 3: 70}
129-
assert_warns_message(UserWarning, "After over-sampling, the number of"
130-
" samples (140) in class 2 will be larger than the"
131-
" number of samples in the majority class (class #2"
132-
" -> 100)", check_ratio, ratio, y, 'over-sampling')
129+
expected_msg = ("After over-sampling, the number of samples \(140\) in"
130+
" class 2 will be larger than the number of samples in the"
131+
" majority class \(class #2 -> 100\)")
132+
with warns(UserWarning, expected_msg):
133+
check_ratio(ratio, y, 'over-sampling')
133134

134135

135136
def test_ratio_dict_under_sampling():

0 commit comments

Comments
 (0)