Skip to content

Commit e802a19

Browse files
authored
MAINT remove deprecated function (#888)
1 parent 16eda28 commit e802a19

File tree

9 files changed

+12
-176
lines changed

9 files changed

+12
-176
lines changed

doc/developers_utils.rst

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ E.g., renaming an attribute ``labels_`` to ``classes_`` can be done as::
7575
def labels_(self):
7676
return self.classes_
7777

78-
If a parameter has to be deprecated, use ``DeprecationWarning`` appropriately.
78+
If a parameter has to be deprecated, use ``FutureWarning`` appropriately.
7979
In the following example, k is deprecated and renamed to n_clusters::
8080

8181
import warnings
@@ -106,49 +106,6 @@ On the top of all the functionality provided by scikit-learn. imbalanced-learn
106106
provides :func:`deprecate_parameter`: which is used to deprecate a sampler's
107107
parameter (attribute) by another one.
108108

109-
Testing utilities
110-
~~~~~~~~~~~~~~~~~
111-
Currently, imbalanced-learn provide a warning management utility. This feature
112-
is going to be merge in pytest and will be removed when the pytest release will
113-
have it.
114-
115-
If using Python 2.7 or above, you may use this function as a
116-
context manager::
117-
118-
>>> import warnings
119-
>>> from imblearn.utils.testing import warns
120-
>>> with warns(RuntimeWarning):
121-
... warnings.warn("my runtime warning", RuntimeWarning)
122-
123-
>>> with warns(RuntimeWarning):
124-
... pass
125-
Traceback (most recent call last):
126-
...
127-
Failed: DID NOT WARN. No warnings of type ...RuntimeWarning... was emitted...
128-
129-
>>> with warns(RuntimeWarning):
130-
... warnings.warn(UserWarning)
131-
Traceback (most recent call last):
132-
...
133-
Failed: DID NOT WARN. No warnings of type ...RuntimeWarning... was emitted...
134-
135-
In the context manager form you may use the keyword argument ``match`` to assert
136-
that the exception matches a text or regex::
137-
138-
>>> import warnings
139-
>>> from imblearn.utils.testing import warns
140-
>>> with warns(UserWarning, match='must be 0 or None'):
141-
... warnings.warn("value must be 0 or None", UserWarning)
142-
143-
>>> with warns(UserWarning, match=r'must be \d+$'):
144-
... warnings.warn("value must be 42", UserWarning)
145-
146-
>>> with warns(UserWarning, match=r'must be \d+$'):
147-
... warnings.warn("this is not here", UserWarning)
148-
Traceback (most recent call last):
149-
...
150-
AssertionError: 'must be \d+$' pattern not found in ['this is not here']
151-
152109
Making a release
153110
----------------
154111
This section document the different steps that are necessary to make a new

imblearn/metrics/tests/test_classification.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
from imblearn.metrics import classification_report_imbalanced
3232
from imblearn.metrics import macro_averaged_mean_absolute_error
3333

34-
from imblearn.utils.testing import warns
35-
3634
RND_SEED = 42
3735
R_TOL = 1e-2
3836

@@ -182,7 +180,8 @@ def test_sensitivity_specificity_support_errors():
182180

183181
def test_sensitivity_specificity_unused_pos_label():
184182
# but average != 'binary'; even if data is binary
185-
with warns(UserWarning, r"use labels=\[pos_label\] to specify a single"):
183+
msg = r"use labels=\[pos_label\] to specify a single"
184+
with pytest.warns(UserWarning, match=msg):
186185
sensitivity_specificity_support(
187186
[1, 2, 1], [1, 2, 2], pos_label=2, average="macro"
188187
)

imblearn/under_sampling/_prototype_generation/_cluster_centroids.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
# Christos Aridas
77
# License: MIT
88

9-
import warnings
10-
119
import numpy as np
1210
from scipy import sparse
1311

@@ -18,7 +16,6 @@
1816

1917
from ..base import BaseUnderSampler
2018
from ...utils import Substitution
21-
from ...utils._docstring import _n_jobs_docstring
2219
from ...utils._docstring import _random_state_docstring
2320
from ...utils._validation import _deprecate_positional_args
2421

@@ -27,7 +24,6 @@
2724

2825
@Substitution(
2926
sampling_strategy=BaseUnderSampler._sampling_strategy_docstring,
30-
n_jobs=_n_jobs_docstring,
3127
random_state=_random_state_docstring,
3228
)
3329
class ClusterCentroids(BaseUnderSampler):
@@ -65,11 +61,6 @@ class ClusterCentroids(BaseUnderSampler):
6561
6662
.. versionadded:: 0.3.0
6763
68-
{n_jobs}
69-
70-
.. deprecated:: 0.7
71-
`n_jobs` was deprecated in 0.7 and will be removed in 0.9.
72-
7364
Attributes
7465
----------
7566
sampling_strategy_ : dict
@@ -125,21 +116,14 @@ def __init__(
125116
random_state=None,
126117
estimator=None,
127118
voting="auto",
128-
n_jobs="deprecated",
129119
):
130120
super().__init__(sampling_strategy=sampling_strategy)
131121
self.random_state = random_state
132122
self.estimator = estimator
133123
self.voting = voting
134-
self.n_jobs = n_jobs
135124

136125
def _validate_estimator(self):
137126
"""Private function to create the KMeans estimator"""
138-
if self.n_jobs != "deprecated":
139-
warnings.warn(
140-
"'n_jobs' was deprecated in 0.7 and will be removed in 0.9",
141-
FutureWarning,
142-
)
143127
if self.estimator is None:
144128
self.estimator_ = KMeans(random_state=self.random_state)
145129
else:

imblearn/under_sampling/_prototype_generation/tests/test_cluster_centroids.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,6 @@ def test_fit_resample_error(cluster_centroids_params, err_msg):
112112
cc.fit_resample(X, Y)
113113

114114

115-
def test_cluster_centroids_n_jobs():
116-
# check that we deprecate the `n_jobs` parameter.
117-
cc = ClusterCentroids(n_jobs=1)
118-
with pytest.warns(FutureWarning) as record:
119-
cc.fit_resample(X, Y)
120-
assert len(record) == 1
121-
assert "'n_jobs' was deprecated" in record[0].message.args[0]
122-
123-
124115
def test_cluster_centroids_hard_target_class():
125116
# check that the samples selecting by the hard voting corresponds to the
126117
# targeted class

imblearn/utils/deprecation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ def deprecate_parameter(sampler, version_deprecation, param_deprecated, new_para
3333
f"'{param_deprecated}' is deprecated from {version_deprecation} and "
3434
f" will be removed in {version_removed} for the estimator "
3535
f"{sampler.__class__}.",
36-
category=DeprecationWarning,
36+
category=FutureWarning,
3737
)
3838
else:
3939
if getattr(sampler, param_deprecated) is not None:
4040
warnings.warn(
4141
f"'{param_deprecated}' is deprecated from {version_deprecation} and "
4242
f"will be removed in {version_removed} for the estimator "
4343
f"{sampler.__class__}. Use '{new_param}' instead.",
44-
category=DeprecationWarning,
44+
category=FutureWarning,
4545
)
4646
setattr(sampler, new_param, getattr(sampler, param_deprecated))

imblearn/utils/testing.py

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66

77
import inspect
88
import pkgutil
9-
import warnings
10-
from contextlib import contextmanager
119
from importlib import import_module
1210
from operator import itemgetter
1311
from pathlib import Path
14-
from re import compile
1512

1613
from scipy import sparse
17-
from pytest import warns as _warns
1814

1915
from sklearn.base import BaseEstimator
2016
from sklearn.neighbors import KDTree
@@ -116,58 +112,6 @@ def is_abstract(c):
116112
return sorted(set(estimators), key=itemgetter(0))
117113

118114

119-
@contextmanager
120-
def warns(expected_warning, match=None):
121-
r"""Assert that a warning is raised with an optional matching pattern.
122-
123-
.. deprecated:: 0.8
124-
This function is deprecated in 0.8 and will be removed in 0.10.
125-
Use `pytest.warns()` instead.
126-
127-
Assert that a code block/function call warns ``expected_warning``
128-
and raise a failure exception otherwise. It can be used within a context
129-
manager ``with``.
130-
131-
Parameters
132-
----------
133-
expected_warning : Warning
134-
Warning type.
135-
136-
match : regex str or None, optional
137-
The pattern to be matched. By default, no check is done.
138-
139-
Yields
140-
------
141-
Nothing.
142-
143-
Examples
144-
--------
145-
>>> import warnings
146-
>>> from imblearn.utils.testing import warns
147-
>>> with warns(UserWarning, match=r'must be \d+$'):
148-
... warnings.warn("value must be 42", UserWarning)
149-
"""
150-
warnings.warn(
151-
"The warns function is deprecated in 0.8 and will be removed in 0.10. "
152-
"Use pytest.warns() instead."
153-
)
154-
155-
with _warns(expected_warning) as record:
156-
yield
157-
158-
if match is not None:
159-
for each in record:
160-
if compile(match).search(str(each.message)) is not None:
161-
break
162-
else:
163-
msg = "'{}' pattern not found in {}".format(
164-
match, "{}".format([str(r.message) for r in record])
165-
)
166-
assert False, msg
167-
else:
168-
pass
169-
170-
171115
class _CustomNearestNeighbors(BaseEstimator):
172116
"""Basic implementation of nearest neighbors not relying on scikit-learn.
173117

imblearn/utils/tests/test_deprecation.py

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

6+
import pytest
7+
68
from imblearn.utils.deprecation import deprecate_parameter
7-
from imblearn.utils.testing import warns
89

910

1011
class Sampler:
@@ -14,7 +15,7 @@ def __init__(self):
1415

1516

1617
def test_deprecate_parameter():
17-
with warns(DeprecationWarning, match="is deprecated from"):
18+
with pytest.warns(FutureWarning, match="is deprecated from"):
1819
deprecate_parameter(Sampler(), "0.2", "a")
19-
with warns(DeprecationWarning, match="Use 'b' instead."):
20+
with pytest.warns(FutureWarning, match="Use 'b' instead."):
2021
deprecate_parameter(Sampler(), "0.2", "a", "b")

imblearn/utils/tests/test_testing.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from imblearn.base import SamplerMixin
1313
from imblearn.utils.testing import all_estimators, _CustomNearestNeighbors
1414

15-
from imblearn.utils.testing import warns
16-
1715

1816
def test_all_estimators():
1917
# check if the filtering is working with a list or a single string
@@ -31,40 +29,6 @@ def test_all_estimators():
3129
all_estimators(type_filter=type_filter)
3230

3331

34-
@pytest.mark.filterwarnings("ignore:The warns function is deprecated in 0.8")
35-
def test_warns():
36-
import warnings
37-
38-
with warns(UserWarning, match=r"must be \d+$"):
39-
warnings.warn("value must be 42", UserWarning)
40-
41-
with pytest.raises(AssertionError, match="pattern not found"):
42-
with warns(UserWarning, match=r"must be \d+$"):
43-
warnings.warn("this is not here", UserWarning)
44-
45-
with warns(UserWarning, match=r"aaa"):
46-
warnings.warn("cccccccccc", UserWarning)
47-
warnings.warn("bbbbbbbbbb", UserWarning)
48-
warnings.warn("aaaaaaaaaa", UserWarning)
49-
50-
a, b, c = ("aaa", "bbbbbbbbbb", "cccccccccc")
51-
expected_msg = r"'{}' pattern not found in \['{}', '{}'\]".format(a, b, c)
52-
with pytest.raises(AssertionError, match=expected_msg):
53-
with warns(UserWarning, match=r"aaa"):
54-
warnings.warn("bbbbbbbbbb", UserWarning)
55-
warnings.warn("cccccccccc", UserWarning)
56-
57-
58-
# TODO: remove in 0.9
59-
def test_warns_deprecation():
60-
import warnings
61-
62-
with pytest.warns(None) as record:
63-
with warns(UserWarning):
64-
warnings.warn("value must be 42")
65-
assert "The warns function is deprecated" in str(record[0].message)
66-
67-
6832
def test_custom_nearest_neighbors():
6933
"""Check that our custom nearest neighbors can be used for our internal
7034
duck-typing."""

imblearn/utils/tests/test_validation.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from imblearn.utils import check_neighbors_object
1717
from imblearn.utils import check_sampling_strategy
1818
from imblearn.utils import check_target_type
19-
from imblearn.utils.testing import warns, _CustomNearestNeighbors
19+
from imblearn.utils.testing import _CustomNearestNeighbors
2020
from imblearn.utils._validation import ArraysTransformer
2121
from imblearn.utils._validation import _deprecate_positional_args
2222

@@ -262,12 +262,8 @@ def test_check_sampling_strategy(
262262
def test_sampling_strategy_dict_over_sampling():
263263
y = np.array([1] * 50 + [2] * 100 + [3] * 25)
264264
sampling_strategy = {1: 70, 2: 140, 3: 70}
265-
expected_msg = (
266-
r"After over-sampling, the number of samples \(140\) in"
267-
r" class 2 will be larger than the number of samples in"
268-
r" the majority class \(class #2 -> 100\)"
269-
)
270-
with warns(UserWarning, expected_msg):
265+
expected_msg = "After over-sampling, the number of samples "
266+
with pytest.warns(UserWarning, match=expected_msg):
271267
check_sampling_strategy(sampling_strategy, y, "over-sampling")
272268

273269

0 commit comments

Comments
 (0)