Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3217999

Browse filesBrowse files
authored
TST Replace the use of assert_warns messages in cluster/tests/ module (#19437)
1 parent cc13313 commit 3217999
Copy full SHA for 3217999

File tree

Expand file treeCollapse file tree

5 files changed

+38
-28
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+38
-28
lines changed

‎sklearn/cluster/tests/test_affinity_propagation.py

Copy file name to clipboardExpand all lines: sklearn/cluster/tests/test_affinity_propagation.py
+19-14Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
from scipy.sparse import csr_matrix
99

1010
from sklearn.exceptions import ConvergenceWarning
11-
from sklearn.utils._testing import (
12-
assert_array_equal, assert_warns,
13-
assert_warns_message, assert_no_warnings)
11+
from sklearn.utils._testing import assert_array_equal
1412

1513
from sklearn.cluster import AffinityPropagation
1614
from sklearn.cluster._affinity_propagation import (
@@ -72,6 +70,7 @@ def test_affinity_propagation():
7270
with pytest.raises(TypeError):
7371
af_2.fit(csr_matrix((3, 3)))
7472

73+
7574
def test_affinity_propagation_predict():
7675
# Test AffinityPropagation.predict
7776
af = AffinityPropagation(affinity="euclidean", random_state=63)
@@ -104,7 +103,8 @@ def test_affinity_propagation_fit_non_convergence():
104103
# Force non-convergence by allowing only a single iteration
105104
af = AffinityPropagation(preference=-10, max_iter=1, random_state=82)
106105

107-
assert_warns(ConvergenceWarning, af.fit, X)
106+
with pytest.warns(ConvergenceWarning):
107+
af.fit(X)
108108
assert_array_equal(np.empty((0, 2)), af.cluster_centers_)
109109
assert_array_equal(np.array([-1, -1, -1]), af.labels_)
110110

@@ -114,24 +114,28 @@ def test_affinity_propagation_equal_mutual_similarities():
114114
S = -euclidean_distances(X, squared=True)
115115

116116
# setting preference > similarity
117-
cluster_center_indices, labels = assert_warns_message(
118-
UserWarning, "mutually equal", affinity_propagation, S, preference=0)
117+
with pytest.warns(UserWarning, match="mutually equal"):
118+
cluster_center_indices, labels = affinity_propagation(
119+
S, preference=0)
119120

120121
# expect every sample to become an exemplar
121122
assert_array_equal([0, 1], cluster_center_indices)
122123
assert_array_equal([0, 1], labels)
123124

124125
# setting preference < similarity
125-
cluster_center_indices, labels = assert_warns_message(
126-
UserWarning, "mutually equal", affinity_propagation, S, preference=-10)
126+
with pytest.warns(UserWarning, match="mutually equal"):
127+
cluster_center_indices, labels = affinity_propagation(
128+
S, preference=-10)
127129

128130
# expect one cluster, with arbitrary (first) sample as exemplar
129131
assert_array_equal([0], cluster_center_indices)
130132
assert_array_equal([0, 0], labels)
131133

132134
# setting different preferences
133-
cluster_center_indices, labels = assert_no_warnings(
134-
affinity_propagation, S, preference=[-20, -10], random_state=37)
135+
with pytest.warns(None) as record:
136+
cluster_center_indices, labels = affinity_propagation(
137+
S, preference=[-20, -10], random_state=37)
138+
assert not len(record)
135139

136140
# expect one cluster, with highest-preference sample as exemplar
137141
assert_array_equal([1], cluster_center_indices)
@@ -144,14 +148,15 @@ def test_affinity_propagation_predict_non_convergence():
144148
X = np.array([[0, 0], [1, 1], [-2, -2]])
145149

146150
# Force non-convergence by allowing only a single iteration
147-
af = assert_warns(ConvergenceWarning,
148-
AffinityPropagation(preference=-10,
149-
max_iter=1, random_state=75).fit, X)
151+
with pytest.warns(ConvergenceWarning):
152+
af = AffinityPropagation(preference=-10,
153+
max_iter=1, random_state=75).fit(X)
150154

151155
# At prediction time, consider new samples as noise since there are no
152156
# clusters
153157
to_predict = np.array([[2, 2], [3, 3], [4, 4]])
154-
y = assert_warns(ConvergenceWarning, af.predict, to_predict)
158+
with pytest.warns(ConvergenceWarning):
159+
y = af.predict(to_predict)
155160
assert_array_equal(np.array([-1, -1, -1]), y)
156161

157162

‎sklearn/cluster/tests/test_birch.py

Copy file name to clipboardExpand all lines: sklearn/cluster/tests/test_birch.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from sklearn.utils._testing import assert_almost_equal
1818
from sklearn.utils._testing import assert_array_equal
1919
from sklearn.utils._testing import assert_array_almost_equal
20-
from sklearn.utils._testing import assert_warns
2120

2221

2322
def test_n_samples_leaves_roots():
@@ -92,7 +91,8 @@ def test_n_clusters():
9291

9392
# Test that a small number of clusters raises a warning.
9493
brc4 = Birch(threshold=10000.)
95-
assert_warns(ConvergenceWarning, brc4.fit, X)
94+
with pytest.warns(ConvergenceWarning):
95+
brc4.fit(X)
9696

9797

9898
def test_sparse_X():

‎sklearn/cluster/tests/test_feature_agglomeration.py

Copy file name to clipboardExpand all lines: sklearn/cluster/tests/test_feature_agglomeration.py
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"""
44
# Authors: Sergul Aydore 2017
55
import numpy as np
6+
import pytest
67
from sklearn.cluster import FeatureAgglomeration
7-
from sklearn.utils._testing import assert_no_warnings
88
from sklearn.utils._testing import assert_array_almost_equal
99

1010

@@ -16,8 +16,12 @@ def test_feature_agglomeration():
1616
pooling_func=np.mean)
1717
agglo_median = FeatureAgglomeration(n_clusters=n_clusters,
1818
pooling_func=np.median)
19-
assert_no_warnings(agglo_mean.fit, X)
20-
assert_no_warnings(agglo_median.fit, X)
19+
with pytest.warns(None) as record:
20+
agglo_mean.fit(X)
21+
assert not len(record)
22+
with pytest.warns(None) as record:
23+
agglo_median.fit(X)
24+
assert not len(record)
2125
assert np.size(np.unique(agglo_mean.labels_)) == n_clusters
2226
assert np.size(np.unique(agglo_median.labels_)) == n_clusters
2327
assert np.size(agglo_mean.labels_) == X.shape[1]

‎sklearn/cluster/tests/test_hierarchical.py

Copy file name to clipboardExpand all lines: sklearn/cluster/tests/test_hierarchical.py
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from sklearn.cluster._hierarchical_fast import average_merge, max_merge
3434
from sklearn.utils._fast_dict import IntFloatDict
3535
from sklearn.utils._testing import assert_array_equal
36-
from sklearn.utils._testing import assert_warns
3736
from sklearn.datasets import make_moons, make_circles
3837

3938

@@ -94,17 +93,18 @@ def test_unstructured_linkage_tree():
9493
# With specified a number of clusters just for the sake of
9594
# raising a warning and testing the warning code
9695
with ignore_warnings():
97-
children, n_nodes, n_leaves, parent = assert_warns(
98-
UserWarning, ward_tree, this_X.T, n_clusters=10)
96+
with pytest.warns(UserWarning):
97+
children, n_nodes, n_leaves, parent = ward_tree(
98+
this_X.T, n_clusters=10)
9999
n_nodes = 2 * X.shape[1] - 1
100100
assert len(children) + n_leaves == n_nodes
101101

102102
for tree_builder in _TREE_BUILDERS.values():
103103
for this_X in (X, X[0]):
104104
with ignore_warnings():
105-
children, n_nodes, n_leaves, parent = assert_warns(
106-
UserWarning, tree_builder, this_X.T, n_clusters=10)
107-
105+
with pytest.warns(UserWarning):
106+
children, n_nodes, n_leaves, parent = tree_builder(
107+
this_X.T, n_clusters=10)
108108
n_nodes = 2 * X.shape[1] - 1
109109
assert len(children) + n_leaves == n_nodes
110110

@@ -550,7 +550,8 @@ def test_connectivity_fixing_non_lil():
550550
m = np.array([[True, False], [False, True]])
551551
c = grid_to_graph(n_x=2, n_y=2, mask=m)
552552
w = AgglomerativeClustering(connectivity=c, linkage='ward')
553-
assert_warns(UserWarning, w.fit, x)
553+
with pytest.warns(UserWarning):
554+
w.fit(x)
554555

555556

556557
def test_int_float_dict():

‎sklearn/cluster/tests/test_spectral.py

Copy file name to clipboardExpand all lines: sklearn/cluster/tests/test_spectral.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from sklearn.utils import check_random_state
1212
from sklearn.utils._testing import assert_array_equal
13-
from sklearn.utils._testing import assert_warns_message
1413

1514
from sklearn.cluster import SpectralClustering, spectral_clustering
1615
from sklearn.cluster._spectral import discretize
@@ -132,7 +131,8 @@ def test_affinities():
132131
# nearest neighbors affinity
133132
sp = SpectralClustering(n_clusters=2, affinity='nearest_neighbors',
134133
random_state=0)
135-
assert_warns_message(UserWarning, 'not fully connected', sp.fit, X)
134+
with pytest.warns(UserWarning, match='not fully connected'):
135+
sp.fit(X)
136136
assert adjusted_rand_score(y, sp.labels_) == 1
137137

138138
sp = SpectralClustering(n_clusters=2, gamma=2, random_state=0)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.