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 606c467

Browse filesBrowse files
LSturtewthomasjpfan
authored andcommitted
TST Changes assert to pytest style in test_random_projection.py (scikit-learn#19846)
1 parent 9019311 commit 606c467
Copy full SHA for 606c467

File tree

1 file changed

+44
-48
lines changed
Filter options

1 file changed

+44
-48
lines changed

‎sklearn/tests/test_random_projection.py

Copy file name to clipboardExpand all lines: sklearn/tests/test_random_projection.py
+44-48Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@
1414
from sklearn.random_projection import SparseRandomProjection
1515
from sklearn.random_projection import GaussianRandomProjection
1616

17-
from sklearn.utils._testing import assert_raises
18-
from sklearn.utils._testing import assert_raise_message
1917
from sklearn.utils._testing import assert_array_equal
2018
from sklearn.utils._testing import assert_almost_equal
2119
from sklearn.utils._testing import assert_array_almost_equal
22-
from sklearn.utils._testing import assert_warns
2320
from sklearn.exceptions import DataDimensionalityWarning
2421

2522
all_sparse_random_matrix: List[Any] = [_sparse_random_matrix]
@@ -59,19 +56,21 @@ def densify(matrix):
5956
###############################################################################
6057
# test on JL lemma
6158
###############################################################################
62-
def test_invalid_jl_domain():
63-
assert_raises(ValueError, johnson_lindenstrauss_min_dim, 100, eps=1.1)
64-
assert_raises(ValueError, johnson_lindenstrauss_min_dim, 100, eps=0.0)
65-
assert_raises(ValueError, johnson_lindenstrauss_min_dim, 100, eps=-0.1)
66-
assert_raises(ValueError, johnson_lindenstrauss_min_dim, 0, eps=0.5)
6759

60+
@pytest.mark.parametrize("n_samples, eps", [
61+
(100, 1.1),
62+
(100, 0.0),
63+
(100, -0.1),
64+
(0, 0.5)
65+
])
66+
def test_invalid_jl_domain(n_samples, eps):
67+
with pytest.raises(ValueError):
68+
johnson_lindenstrauss_min_dim(n_samples, eps=eps)
6869

69-
def test_input_size_jl_min_dim():
70-
assert_raises(ValueError, johnson_lindenstrauss_min_dim,
71-
3 * [100], eps=2 * [0.9])
7270

73-
assert_raises(ValueError, johnson_lindenstrauss_min_dim, 3 * [100],
74-
eps=2 * [0.9])
71+
def test_input_size_jl_min_dim():
72+
with pytest.raises(ValueError):
73+
johnson_lindenstrauss_min_dim(3 * [100], eps=2 * [0.9])
7574

7675
johnson_lindenstrauss_min_dim(np.random.randint(1, 10, size=(10, 10)),
7776
eps=np.full((10, 10), 0.5))
@@ -81,18 +80,17 @@ def test_input_size_jl_min_dim():
8180
# tests random matrix generation
8281
###############################################################################
8382
def check_input_size_random_matrix(random_matrix):
84-
assert_raises(ValueError, random_matrix, 0, 0)
85-
assert_raises(ValueError, random_matrix, -1, 1)
86-
assert_raises(ValueError, random_matrix, 1, -1)
87-
assert_raises(ValueError, random_matrix, 1, 0)
88-
assert_raises(ValueError, random_matrix, -1, 0)
83+
inputs = [(0, 0), (-1, 1), (1, -1), (1, 0), (-1, 0)]
84+
for n_components, n_features in inputs:
85+
with pytest.raises(ValueError):
86+
random_matrix(n_components, n_features)
8987

9088

9189
def check_size_generated(random_matrix):
92-
assert random_matrix(1, 5).shape == (1, 5)
93-
assert random_matrix(5, 1).shape == (5, 1)
94-
assert random_matrix(5, 5).shape == (5, 5)
95-
assert random_matrix(1, 1).shape == (1, 1)
90+
inputs = [(1, 5), (5, 1), (5, 5), (1, 1)]
91+
for n_components, n_features in inputs:
92+
assert random_matrix(n_components, n_features).shape == (
93+
n_components, n_features)
9694

9795

9896
def check_zero_mean_and_unit_norm(random_matrix):
@@ -109,8 +107,8 @@ def check_input_with_sparse_random_matrix(random_matrix):
109107
n_components, n_features = 5, 10
110108

111109
for density in [-1., 0.0, 1.1]:
112-
assert_raises(ValueError,
113-
random_matrix, n_components, n_features, density=density)
110+
with pytest.raises(ValueError):
111+
random_matrix(n_components, n_features, density=density)
114112

115113

116114
@pytest.mark.parametrize("random_matrix", all_random_matrix)
@@ -153,9 +151,9 @@ def test_sparse_random_matrix():
153151
s = 1 / density
154152

155153
A = _sparse_random_matrix(n_components,
156-
n_features,
157-
density=density,
158-
random_state=0)
154+
n_features,
155+
density=density,
156+
random_state=0)
159157
A = densify(A)
160158

161159
# Check possible values
@@ -196,31 +194,27 @@ def test_sparse_random_matrix():
196194
###############################################################################
197195
# tests on random projection transformer
198196
###############################################################################
199-
def test_sparse_random_projection_transformer_invalid_density():
200-
for RandomProjection in all_SparseRandomProjection:
201-
assert_raises(ValueError,
202-
RandomProjection(density=1.1).fit, data)
203197

204-
assert_raises(ValueError,
205-
RandomProjection(density=0).fit, data)
206-
207-
assert_raises(ValueError,
208-
RandomProjection(density=-0.1).fit, data)
198+
@pytest.mark.parametrize("density", [1.1, 0, -0.1])
199+
def test_sparse_random_projection_transformer_invalid_density(density):
200+
for RandomProjection in all_SparseRandomProjection:
201+
with pytest.raises(ValueError):
202+
RandomProjection(density=density).fit(data)
209203

210204

211-
def test_random_projection_transformer_invalid_input():
205+
@pytest.mark.parametrize("n_components, fit_data", [
206+
('auto', [[0, 1, 2]]), (-10, data)]
207+
)
208+
def test_random_projection_transformer_invalid_input(n_components, fit_data):
212209
for RandomProjection in all_RandomProjection:
213-
assert_raises(ValueError,
214-
RandomProjection(n_components='auto').fit, [[0, 1, 2]])
215-
216-
assert_raises(ValueError,
217-
RandomProjection(n_components=-10).fit, data)
210+
with pytest.raises(ValueError):
211+
RandomProjection(n_components=n_components).fit(fit_data)
218212

219213

220214
def test_try_to_transform_before_fit():
221215
for RandomProjection in all_RandomProjection:
222-
assert_raises(ValueError,
223-
RandomProjection(n_components='auto').transform, data)
216+
with pytest.raises(ValueError):
217+
RandomProjection(n_components='auto').transform(data)
224218

225219

226220
def test_too_many_samples_to_find_a_safe_embedding():
@@ -232,7 +226,8 @@ def test_too_many_samples_to_find_a_safe_embedding():
232226
'eps=0.100000 and n_samples=1000 lead to a target dimension'
233227
' of 5920 which is larger than the original space with'
234228
' n_features=100')
235-
assert_raise_message(ValueError, expected_msg, rp.fit, data)
229+
with pytest.raises(ValueError, match=expected_msg):
230+
rp.fit(data)
236231

237232

238233
def test_random_projection_embedding_quality():
@@ -318,7 +313,8 @@ def test_correct_RandomProjection_dimensions_embedding():
318313
assert_array_equal(projected_1, projected_3)
319314

320315
# Try to transform with an input X of size different from fitted.
321-
assert_raises(ValueError, rp.transform, data[:, 1:5])
316+
with pytest.raises(ValueError):
317+
rp.transform(data[:, 1:5])
322318

323319
# it is also possible to fix the number of components and the density
324320
# level
@@ -337,8 +333,8 @@ def test_warning_n_components_greater_than_n_features():
337333
data, _ = make_sparse_random_data(5, n_features, int(n_features / 4))
338334

339335
for RandomProjection in all_RandomProjection:
340-
assert_warns(DataDimensionalityWarning,
341-
RandomProjection(n_components=n_features + 1).fit, data)
336+
with pytest.warns(DataDimensionalityWarning):
337+
RandomProjection(n_components=n_features + 1).fit(data)
342338

343339

344340
def test_works_with_sparse_data():

0 commit comments

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