Skip to content

Commit c960cfc

Browse files
authored
MNT deprecate passing kwargs by position (#721)
1 parent 3c6d232 commit c960cfc

29 files changed

+195
-20
lines changed

doc/whats_new/v0.7.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Maintenance
1919
- Remove `FutureWarning` issued by `scikit-learn` 0.23.
2020
:pr:`710` by :user:`Guillaume Lemaitre <glemaitre>`.
2121

22+
- Impose keywords only argument as in `scikit-learn`.
23+
:pr:`721` by :user:`Guillaume Lemaitre <glemaitre>`.
24+
2225
Changed models
2326
..............
2427

@@ -67,3 +70,7 @@ Deprecation
6770
:class:`imblearn.under_sampling.ClusterCentroids` since it was used by
6871
:class:`sklearn.cluster.KMeans` which deprecated it.
6972
:pr:`710` by :user:`Guillaume Lemaitre <glemaitre>`.
73+
74+
- Deprecation of passing keyword argument by position similarly to
75+
`scikit-learn`.
76+
:pr:`721` by :user:`Guillaume lemaitre <glemaitre>`.

imblearn/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from .utils import check_sampling_strategy, check_target_type
1616
from .utils._validation import ArraysTransformer
17+
from .utils._validation import _deprecate_positional_args
1718

1819

1920
class SamplerMixin(BaseEstimator, metaclass=ABCMeta):
@@ -213,7 +214,8 @@ class FunctionSampler(BaseSampler):
213214

214215
_sampling_type = "bypass"
215216

216-
def __init__(self, func=None, accept_sparse=True, kw_args=None,
217+
@_deprecate_positional_args
218+
def __init__(self, *, func=None, accept_sparse=True, kw_args=None,
217219
validate=True):
218220
super().__init__()
219221
self.func = func

imblearn/combine/_smote_enn.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ..utils import Substitution
1616
from ..utils._docstring import _n_jobs_docstring
1717
from ..utils._docstring import _random_state_docstring
18+
from ..utils._validation import _deprecate_positional_args
1819

1920

2021
@Substitution(
@@ -85,8 +86,10 @@ class SMOTEENN(BaseSampler):
8586

8687
_sampling_type = "over-sampling"
8788

89+
@_deprecate_positional_args
8890
def __init__(
8991
self,
92+
*,
9093
sampling_strategy="auto",
9194
random_state=None,
9295
smote=None,

imblearn/combine/_smote_tomek.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ..utils import Substitution
1717
from ..utils._docstring import _n_jobs_docstring
1818
from ..utils._docstring import _random_state_docstring
19+
from ..utils._validation import _deprecate_positional_args
1920

2021

2122
@Substitution(
@@ -85,8 +86,10 @@ class SMOTETomek(BaseSampler):
8586

8687
_sampling_type = "over-sampling"
8788

89+
@_deprecate_positional_args
8890
def __init__(
8991
self,
92+
*,
9093
sampling_strategy="auto",
9194
random_state=None,
9295
smote=None,

imblearn/datasets/_imbalance.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
from ..under_sampling import RandomUnderSampler
1111
from ..utils import check_sampling_strategy
12+
from ..utils._validation import _deprecate_positional_args
1213

1314

15+
@_deprecate_positional_args
1416
def make_imbalance(
15-
X, y, sampling_strategy=None, random_state=None, verbose=False, **kwargs
17+
X, y, *, sampling_strategy=None, random_state=None, verbose=False, **kwargs
1618
):
1719
"""Turns a dataset into an imbalanced dataset with a specific sampling
1820
strategy.

imblearn/datasets/_zenodo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
from sklearn.utils import Bunch
5858
from sklearn.utils import check_random_state
5959

60+
from ..utils._validation import _deprecate_positional_args
61+
6062
URL = (
6163
"https://zenodo.org/record/61452/files/"
6264
"benchmark-imbalanced-learn.tar.gz"
@@ -101,7 +103,9 @@
101103
MAP_ID_NAME[v + 1] = k
102104

103105

106+
@_deprecate_positional_args
104107
def fetch_datasets(
108+
*,
105109
data_home=None,
106110
filter_data=None,
107111
download_if_missing=True,

imblearn/datasets/tests/test_imbalance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ def test_make_imbalance_error(iris, sampling_strategy, err_msg):
3232
# cover in the common tests so we will repeat it here
3333
X, y = iris
3434
with pytest.raises(ValueError, match=err_msg):
35-
make_imbalance(X, y, sampling_strategy)
35+
make_imbalance(X, y, sampling_strategy=sampling_strategy)
3636

3737

3838
def test_make_imbalance_error_single_class(iris):
3939
X, y = iris
4040
y = np.zeros_like(y)
4141
with pytest.raises(ValueError, match="needs to have more than 1 class."):
42-
make_imbalance(X, y, {0: 10})
42+
make_imbalance(X, y, sampling_strategy={0: 10})
4343

4444

4545
@pytest.mark.parametrize(

imblearn/ensemble/_bagging.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ..utils import Substitution, check_target_type, check_sampling_strategy
1919
from ..utils._docstring import _n_jobs_docstring
2020
from ..utils._docstring import _random_state_docstring
21+
from ..utils._validation import _deprecate_positional_args
2122

2223

2324
@Substitution(
@@ -175,10 +176,12 @@ class BalancedBaggingClassifier(BaggingClassifier):
175176
[ 2 225]]
176177
"""
177178

179+
@_deprecate_positional_args
178180
def __init__(
179181
self,
180182
base_estimator=None,
181183
n_estimators=10,
184+
*,
182185
max_samples=1.0,
183186
max_features=1.0,
184187
bootstrap=True,

imblearn/ensemble/_easy_ensemble.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ..utils import Substitution, check_target_type, check_sampling_strategy
1818
from ..utils._docstring import _n_jobs_docstring
1919
from ..utils._docstring import _random_state_docstring
20+
from ..utils._validation import _deprecate_positional_args
2021
from ..pipeline import Pipeline
2122

2223
MAX_INT = np.iinfo(np.int32).max
@@ -125,10 +126,12 @@ class EasyEnsembleClassifier(BaggingClassifier):
125126
[ 2 225]]
126127
"""
127128

129+
@_deprecate_positional_args
128130
def __init__(
129131
self,
130132
n_estimators=10,
131133
base_estimator=None,
134+
*,
132135
warm_start=False,
133136
sampling_strategy="auto",
134137
replacement=False,

imblearn/ensemble/_forest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from ..utils._docstring import _n_jobs_docstring
3535
from ..utils._docstring import _random_state_docstring
3636
from ..utils._validation import check_sampling_strategy
37+
from ..utils._validation import _deprecate_positional_args
3738

3839
MAX_INT = np.iinfo(np.int32).max
3940

@@ -297,9 +298,11 @@ class labels (multi-output problem).
297298
[1]
298299
"""
299300

301+
@_deprecate_positional_args
300302
def __init__(
301303
self,
302304
n_estimators=100,
305+
*,
303306
criterion="gini",
304307
max_depth=None,
305308
min_samples_split=2,

imblearn/ensemble/_weight_boosting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ..pipeline import make_pipeline
1313
from ..utils import Substitution, check_target_type
1414
from ..utils._docstring import _random_state_docstring
15+
from ..utils._validation import _deprecate_positional_args
1516

1617

1718
@Substitution(
@@ -120,9 +121,11 @@ class RUSBoostClassifier(AdaBoostClassifier):
120121
array([...])
121122
"""
122123

124+
@_deprecate_positional_args
123125
def __init__(
124126
self,
125127
base_estimator=None,
128+
*,
126129
n_estimators=50,
127130
learning_rate=1.0,
128131
algorithm="SAMME.R",

imblearn/keras/_generator.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ def import_from_tensforflow():
4141

4242
ParentClass, HAS_KERAS = import_keras()
4343

44-
from scipy.sparse import issparse
44+
from scipy.sparse import issparse # noqa
4545

46-
from sklearn.base import clone
47-
from sklearn.utils import _safe_indexing
48-
from sklearn.utils import check_random_state
46+
from sklearn.base import clone # noqa
47+
from sklearn.utils import _safe_indexing # noqa
48+
from sklearn.utils import check_random_state # noqa
4949

50-
from ..under_sampling import RandomUnderSampler
51-
from ..utils import Substitution
52-
from ..utils._docstring import _random_state_docstring
53-
from ..tensorflow import balanced_batch_generator as tf_bbg
50+
from ..under_sampling import RandomUnderSampler # noqa
51+
from ..utils import Substitution # noqa
52+
from ..utils._docstring import _random_state_docstring # noqa
53+
from ..tensorflow import balanced_batch_generator as tf_bbg # noqa
54+
from ..utils._validation import _deprecate_positional_args # noqa
5455

5556

5657
class BalancedBatchGenerator(*ParentClass):
@@ -130,10 +131,12 @@ class BalancedBatchGenerator(*ParentClass):
130131
# flag for keras sequence duck-typing
131132
use_sequence_api = True
132133

134+
@_deprecate_positional_args
133135
def __init__(
134136
self,
135137
X,
136138
y,
139+
*,
137140
sample_weight=None,
138141
sampler=None,
139142
batch_size=32,
@@ -199,9 +202,11 @@ def __getitem__(self, index):
199202

200203

201204
@Substitution(random_state=_random_state_docstring)
205+
@_deprecate_positional_args
202206
def balanced_batch_generator(
203207
X,
204208
y,
209+
*,
205210
sample_weight=None,
206211
sampler=None,
207212
batch_size=32,

imblearn/metrics/_classification.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@
3030
except ImportError:
3131
from sklearn.externals.funcsigs import signature
3232

33+
from ..utils._validation import _deprecate_positional_args
3334

35+
36+
@_deprecate_positional_args
3437
def sensitivity_specificity_support(
3538
y_true,
3639
y_pred,
40+
*,
3741
labels=None,
3842
pos_label=1,
3943
average=None,
@@ -279,9 +283,11 @@ def sensitivity_specificity_support(
279283
return sensitivity, specificity, true_sum
280284

281285

286+
@_deprecate_positional_args
282287
def sensitivity_score(
283288
y_true,
284289
y_pred,
290+
*,
285291
labels=None,
286292
pos_label=1,
287293
average="binary",
@@ -382,9 +388,11 @@ def sensitivity_score(
382388
return s
383389

384390

391+
@_deprecate_positional_args
385392
def specificity_score(
386393
y_true,
387394
y_pred,
395+
*,
388396
labels=None,
389397
pos_label=1,
390398
average="binary",
@@ -485,9 +493,11 @@ def specificity_score(
485493
return s
486494

487495

496+
@_deprecate_positional_args
488497
def geometric_mean_score(
489498
y_true,
490499
y_pred,
500+
*,
491501
labels=None,
492502
pos_label=1,
493503
average="multiclass",
@@ -675,7 +685,8 @@ class is unrecognized by the classifier, G-mean resolves to zero. To
675685
return gmean
676686

677687

678-
def make_index_balanced_accuracy(alpha=0.1, squared=True):
688+
@_deprecate_positional_args
689+
def make_index_balanced_accuracy(*, alpha=0.1, squared=True):
679690
"""Balance any scoring function using the index balanced accuracy
680691
681692
This factory function wraps scoring function to express it as the
@@ -785,9 +796,11 @@ def compute_score(*args, **kwargs):
785796
return decorate
786797

787798

799+
@_deprecate_positional_args
788800
def classification_report_imbalanced(
789801
y_true,
790802
y_pred,
803+
*,
791804
labels=None,
792805
target_names=None,
793806
sample_weight=None,

imblearn/over_sampling/_adasyn.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ..utils import Substitution
1616
from ..utils._docstring import _n_jobs_docstring
1717
from ..utils._docstring import _random_state_docstring
18+
from ..utils._validation import _deprecate_positional_args
1819

1920

2021
@Substitution(
@@ -81,8 +82,10 @@ class ADASYN(BaseOverSampler):
8182
Resampled dataset shape Counter({{0: 904, 1: 900}})
8283
"""
8384

85+
@_deprecate_positional_args
8486
def __init__(
8587
self,
88+
*,
8689
sampling_strategy="auto",
8790
random_state=None,
8891
n_neighbors=5,

imblearn/over_sampling/_random_over_sampler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ..utils import check_target_type
1515
from ..utils import Substitution
1616
from ..utils._docstring import _random_state_docstring
17+
from ..utils._validation import _deprecate_positional_args
1718

1819
@Substitution(
1920
sampling_strategy=BaseOverSampler._sampling_strategy_docstring,
@@ -68,7 +69,8 @@ class RandomOverSampler(BaseOverSampler):
6869
Resampled dataset shape Counter({{0: 900, 1: 900}})
6970
"""
7071

71-
def __init__(self, sampling_strategy="auto", random_state=None):
72+
@_deprecate_positional_args
73+
def __init__(self, *, sampling_strategy="auto", random_state=None):
7274
super().__init__(sampling_strategy=sampling_strategy)
7375
self.random_state = random_state
7476

0 commit comments

Comments
 (0)