Skip to content

Commit

Permalink
Migrate assert_raises_regex to pytest.raises
Browse files Browse the repository at this point in the history
  • Loading branch information
massich committed Aug 18, 2017
1 parent 8cbcc77 commit c995c8b
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 151 deletions.
10 changes: 5 additions & 5 deletions imblearn/combine/tests/test_smote_enn.py
Expand Up @@ -7,7 +7,7 @@

import numpy as np
from sklearn.utils.testing import assert_allclose, assert_array_equal
from sklearn.utils.testing import assert_raises_regex
from pytest import raises

from imblearn.combine import SMOTEENN
from imblearn.under_sampling import EditedNearestNeighbours
Expand Down Expand Up @@ -113,8 +113,8 @@ def test_error_wrong_object():
smote = 'rnd'
enn = 'rnd'
smt = SMOTEENN(smote=smote, random_state=RND_SEED)
assert_raises_regex(ValueError, "smote needs to be a SMOTE",
smt.fit_sample, X, Y)
with raises(ValueError, match="smote needs to be a SMOTE"):
smt.fit_sample(X, Y)
smt = SMOTEENN(enn=enn, random_state=RND_SEED)
assert_raises_regex(ValueError, "enn needs to be an ",
smt.fit_sample, X, Y)
with raises(ValueError, match="enn needs to be an "):
smt.fit_sample(X, Y)
10 changes: 5 additions & 5 deletions imblearn/combine/tests/test_smote_tomek.py
Expand Up @@ -7,7 +7,7 @@

import numpy as np
from sklearn.utils.testing import assert_allclose, assert_array_equal
from sklearn.utils.testing import assert_raises_regex
from pytest import raises

from imblearn.combine import SMOTETomek
from imblearn.over_sampling import SMOTE
Expand Down Expand Up @@ -156,8 +156,8 @@ def test_error_wrong_object():
smote = 'rnd'
tomek = 'rnd'
smt = SMOTETomek(smote=smote, random_state=RND_SEED)
assert_raises_regex(ValueError, "smote needs to be a SMOTE",
smt.fit_sample, X, Y)
with raises(ValueError, match="smote needs to be a SMOTE"):
smt.fit_sample(X, Y)
smt = SMOTETomek(tomek=tomek, random_state=RND_SEED)
assert_raises_regex(ValueError, "tomek needs to be a TomekLinks",
smt.fit_sample, X, Y)
with raises(ValueError, match="tomek needs to be a TomekLinks"):
smt.fit_sample(X, Y)
18 changes: 9 additions & 9 deletions imblearn/datasets/tests/test_imbalance.py
Expand Up @@ -11,8 +11,8 @@
import numpy as np

from sklearn.datasets import load_iris
from sklearn.utils.testing import assert_raises_regex
from sklearn.utils.testing import assert_warns_message
from pytest import raises

from imblearn.datasets import make_imbalance

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


# FIXME: to be removed in 0.4 due to deprecation
Expand Down
19 changes: 10 additions & 9 deletions imblearn/datasets/tests/test_zenodo.py
Expand Up @@ -8,7 +8,8 @@

from imblearn.datasets import fetch_datasets
from sklearn.utils.testing import SkipTest, assert_allclose
from sklearn.utils.testing import assert_raises_regex

from pytest import raises

DATASET_SHAPE = {'ecoli': (336, 7),
'optical_digits': (5620, 64),
Expand Down Expand Up @@ -84,11 +85,11 @@ def test_fetch_filter():


def test_fetch_error():
assert_raises_regex(ValueError, 'is not a dataset available.',
fetch_datasets, filter_data=tuple(['rnd']))
assert_raises_regex(ValueError, 'dataset with the ID=',
fetch_datasets, filter_data=tuple([-1]))
assert_raises_regex(ValueError, 'dataset with the ID=',
fetch_datasets, filter_data=tuple([100]))
assert_raises_regex(ValueError, 'value in the tuple',
fetch_datasets, filter_data=tuple([1.00]))
with raises(ValueError, match='is not a dataset available.'):
fetch_datasets(filter_data=tuple(['rnd']))
with raises(ValueError, match='dataset with the ID='):
fetch_datasets(filter_data=tuple([-1]))
with raises(ValueError, match='dataset with the ID='):
fetch_datasets(filter_data=tuple([100]))
with raises(ValueError, match='value in the tuple'):
fetch_datasets(filter_data=tuple([1.00]))
6 changes: 3 additions & 3 deletions imblearn/ensemble/tests/test_balance_cascade.py
Expand Up @@ -7,7 +7,7 @@

import numpy as np
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_raises_regex

from sklearn.ensemble import RandomForestClassifier

from imblearn.ensemble import BalanceCascade
Expand Down Expand Up @@ -365,5 +365,5 @@ def test_give_classifier_wrong_obj():
classifier = 2
bc = BalanceCascade(ratio=ratio, random_state=RND_SEED,
return_indices=True, estimator=classifier)
assert_raises_regex(ValueError, "Invalid parameter `estimator`",
bc.fit_sample, X, Y)
with raises(ValueError, match="Invalid parameter `estimator`"):
bc.fit_sample(X, Y)
7 changes: 4 additions & 3 deletions imblearn/over_sampling/tests/test_adasyn.py
Expand Up @@ -7,11 +7,12 @@

import numpy as np
from sklearn.utils.testing import assert_allclose, assert_array_equal
from sklearn.utils.testing import assert_raises_regex
from sklearn.neighbors import NearestNeighbors

from imblearn.over_sampling import ADASYN

from pytest import raises

RND_SEED = 0
X = np.array([[0.11622591, -0.0317206], [0.77481731, 0.60935141],
[1.25192108, -0.22367336], [0.53366841, -0.30312976],
Expand Down Expand Up @@ -141,5 +142,5 @@ def test_ada_fit_sample_nn_obj():
def test_ada_wrong_nn_obj():
nn = 'rnd'
ada = ADASYN(random_state=RND_SEED, n_neighbors=nn)
assert_raises_regex(ValueError, "has to be one of",
ada.fit_sample, X, Y)
with raises(ValueError, match="has to be one of"):
ada.fit_sample(X, Y)
23 changes: 12 additions & 11 deletions imblearn/over_sampling/tests/test_smote.py
Expand Up @@ -7,12 +7,13 @@

import numpy as np
from sklearn.utils.testing import assert_allclose, assert_array_equal
from sklearn.utils.testing import assert_raises_regex
from sklearn.neighbors import NearestNeighbors
from sklearn.svm import SVC

from imblearn.over_sampling import SMOTE

from pytest import raises

RND_SEED = 0
X = np.array([[0.11622591, -0.0317206], [0.77481731, 0.60935141],
[1.25192108, -0.22367336], [0.53366841, -0.30312976],
Expand All @@ -31,8 +32,8 @@
def test_smote_wrong_kind():
kind = 'rnd'
smote = SMOTE(kind=kind, random_state=RND_SEED)
assert_raises_regex(ValueError, "Unknown kind for SMOTE",
smote.fit_sample, X, Y)
with raises(ValueError, match="Unknown kind for SMOTE"):
smote.fit_sample(X, Y)


def test_sample_regular():
Expand Down Expand Up @@ -203,19 +204,19 @@ def test_wrong_nn():
nn_k = NearestNeighbors(n_neighbors=6)
smote = SMOTE(
random_state=RND_SEED, kind=kind, k_neighbors=nn_k, m_neighbors=nn_m)
assert_raises_regex(ValueError, "has to be one of",
smote.fit_sample, X, Y)
with raises(ValueError, match="has to be one of"):
smote.fit_sample(X, Y)
nn_k = 'rnd'
nn_m = NearestNeighbors(n_neighbors=10)
smote = SMOTE(
random_state=RND_SEED, kind=kind, k_neighbors=nn_k, m_neighbors=nn_m)
assert_raises_regex(ValueError, "has to be one of",
smote.fit_sample, X, Y)
with raises(ValueError, match="has to be one of"):
smote.fit_sample(X, Y)
kind = 'regular'
nn_k = 'rnd'
smote = SMOTE(random_state=RND_SEED, kind=kind, k_neighbors=nn_k)
assert_raises_regex(ValueError, "has to be one of",
smote.fit_sample, X, Y)
with raises(ValueError, match="has to be one of"):
smote.fit_sample(X, Y)


def test_sample_regular_with_nn_svm():
Expand Down Expand Up @@ -250,5 +251,5 @@ def test_sample_regular_wrong_svm():
smote = SMOTE(
random_state=RND_SEED, kind=kind, k_neighbors=nn_k, svm_estimator=svm)

assert_raises_regex(ValueError, "has to be one of",
smote.fit_sample, X, Y)
with raises(ValueError, match="has to be one of"):
smote.fit_sample(X, Y)
8 changes: 4 additions & 4 deletions imblearn/tests/test_exceptions.py
Expand Up @@ -4,12 +4,12 @@
# License: MIT


from sklearn.utils.testing import assert_raises_regex

from imblearn.exceptions import raise_isinstance_error

from pytest import raises


def test_raise_isinstance_error():
var = 10.0
assert_raises_regex(ValueError, "has to be one of",
raise_isinstance_error, 'var', [int], var)
with raises(ValueError, match="has to be one of"):
raise_isinstance_error('var', [int], var)
27 changes: 13 additions & 14 deletions imblearn/tests/test_pipeline.py
Expand Up @@ -11,7 +11,6 @@
import time

import numpy as np
from sklearn.utils.testing import assert_raises_regex
from sklearn.utils.testing import assert_raise_message
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_array_almost_equal
Expand Down Expand Up @@ -181,10 +180,9 @@ def test_pipeline_init():
Pipeline()
# Check that we can't instantiate pipelines with objects without fit
# method
assert_raises_regex(TypeError,
'Last step of Pipeline should implement fit. '
'.*NoFit.*',
Pipeline, [('clf', NoFit())])
error_regex = 'Last step of Pipeline should implement fit. .*NoFit.*'
with raises(TypeError, match=error_regex):
Pipeline([('clf', NoFit())])
# Smoke test with only an estimator
clf = NoTrans()
pipe = Pipeline([('svc', clf)])
Expand All @@ -206,9 +204,9 @@ def test_pipeline_init():

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

# Check that params are set
pipe.set_params(svc__C=0.1)
Expand Down Expand Up @@ -400,9 +398,9 @@ def test_fit_predict_on_pipeline_without_fit_predict():
scaler = StandardScaler()
pca = PCA(svd_solver='full')
pipe = Pipeline([('scaler', scaler), ('pca', pca)])
assert_raises_regex(AttributeError,
"'PCA' object has no attribute 'fit_predict'",
getattr, pipe, 'fit_predict')
error_regex = "'PCA' object has no attribute 'fit_predict'"
with raises(AttributeError, match=error_regex):
getattr(pipe, 'fit_predict')


def test_fit_predict_with_intermediate_fit_params():
Expand Down Expand Up @@ -620,9 +618,10 @@ def test_pipeline_wrong_memory():
memory = 1
cached_pipe = Pipeline([('transf', DummyTransf()), ('svc', SVC())],
memory=memory)
assert_raises_regex(ValueError, "'memory' should either be a string or a"
" joblib.Memory instance, got 'memory=1' instead.",
cached_pipe.fit, X, y)
error_regex = ("'memory' should either be a string or a joblib.Memory"
" instance, got 'memory=1' instead.")
with raises(ValueError, match=error_regex):
cached_pipe.fit(X, y)


def test_pipeline_memory_transformer():
Expand Down
Expand Up @@ -6,7 +6,8 @@
import numpy as np
from sklearn.utils.testing import assert_allclose
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_raises_regex
from pytest import raises

from sklearn.cluster import KMeans

from imblearn.under_sampling import ClusterCentroids
Expand Down Expand Up @@ -79,5 +80,5 @@ def test_fit_sample_wrong_object():
cluster = 'rnd'
cc = ClusterCentroids(
ratio=ratio, random_state=RND_SEED, estimator=cluster)
assert_raises_regex(ValueError, "has to be a KMeans clustering",
cc.fit_sample, X, Y)
with raises(ValueError, match="has to be a KMeans clustering"):
cc.fit_sample(X, Y)
Expand Up @@ -7,7 +7,7 @@

import numpy as np
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_raises_regex
from pytest import raises

from sklearn.neighbors import KNeighborsClassifier

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

import numpy as np
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_raises_regex
from pytest import raises

from sklearn.neighbors import NearestNeighbors

Expand Down Expand Up @@ -102,5 +102,5 @@ def test_enn_not_good_object():
nn = 'rnd'
enn = EditedNearestNeighbours(
n_neighbors=nn, random_state=RND_SEED, kind_sel='mode')
assert_raises_regex(ValueError, "has to be one of",
enn.fit_sample, X, Y)
with raises(ValueError, match="has to be one of"):
enn.fit_sample(X, Y)
Expand Up @@ -8,8 +8,8 @@
import numpy as np
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_raises_regex
from sklearn.ensemble import GradientBoostingClassifier
from pytest import raises

from imblearn.under_sampling import InstanceHardnessThreshold

Expand Down Expand Up @@ -275,5 +275,5 @@ def test_iht_fit_sample_wrong_class_obj():
from sklearn.cluster import KMeans
est = KMeans()
iht = InstanceHardnessThreshold(estimator=est, random_state=RND_SEED)
assert_raises_regex(ValueError, "Invalid parameter `estimator`",
iht.fit_sample, X, Y)
with raises(ValueError, match="Invalid parameter `estimator`"):
iht.fit_sample(X, Y)
Expand Up @@ -7,8 +7,8 @@

import numpy as np
from sklearn.utils.testing import assert_array_equal, assert_warns
from sklearn.utils.testing import assert_raises_regex
from sklearn.neighbors import NearestNeighbors
from pytest import raises

from imblearn.under_sampling import NearMiss

Expand Down Expand Up @@ -42,8 +42,8 @@ def test_nearmiss_deprecation():
def test_nearmiss_wrong_version():
version = 1000
nm = NearMiss(version=version, random_state=RND_SEED)
assert_raises_regex(ValueError, "must be 1, 2 or 3",
nm.fit_sample, X, Y)
with raises(ValueError, match="must be 1, 2 or 3"):
nm.fit_sample(X, Y)


def test_nm_wrong_nn_obj():
Expand All @@ -53,15 +53,15 @@ def test_nm_wrong_nn_obj():
version=VERSION_NEARMISS,
return_indices=True,
n_neighbors=nn)
assert_raises_regex(ValueError, "has to be one of",
nm.fit_sample, X, Y)
with raises(ValueError, match="has to be one of"):
nm.fit_sample(X, Y)
nn3 = 'rnd'
nn = NearestNeighbors(n_neighbors=3)
nm3 = NearMiss(ratio=ratio, random_state=RND_SEED,
version=3, return_indices=True,
n_neighbors=nn, n_neighbors_ver3=nn3)
assert_raises_regex(ValueError, "has to be one of",
nm3.fit_sample, X, Y)
with raises(ValueError, match="has to be one of"):
nm3.fit_sample(X, Y)


def test_nm_fit_sample_auto():
Expand Down

0 comments on commit c995c8b

Please sign in to comment.