Skip to content

TST Changes assert to pytest style in tests/test_random_projection.py #19846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 44 additions & 48 deletions sklearn/tests/test_random_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
from sklearn.random_projection import SparseRandomProjection
from sklearn.random_projection import GaussianRandomProjection

from sklearn.utils._testing import assert_raises
from sklearn.utils._testing import assert_raise_message
from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_almost_equal
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils._testing import assert_warns
from sklearn.exceptions import DataDimensionalityWarning

all_sparse_random_matrix: List[Any] = [_sparse_random_matrix]
Expand Down Expand Up @@ -59,19 +56,21 @@ def densify(matrix):
###############################################################################
# test on JL lemma
###############################################################################
def test_invalid_jl_domain():
assert_raises(ValueError, johnson_lindenstrauss_min_dim, 100, eps=1.1)
assert_raises(ValueError, johnson_lindenstrauss_min_dim, 100, eps=0.0)
assert_raises(ValueError, johnson_lindenstrauss_min_dim, 100, eps=-0.1)
assert_raises(ValueError, johnson_lindenstrauss_min_dim, 0, eps=0.5)

@pytest.mark.parametrize("n_samples, eps", [
(100, 1.1),
(100, 0.0),
(100, -0.1),
(0, 0.5)
])
def test_invalid_jl_domain(n_samples, eps):
with pytest.raises(ValueError):
johnson_lindenstrauss_min_dim(n_samples, eps=eps)

def test_input_size_jl_min_dim():
assert_raises(ValueError, johnson_lindenstrauss_min_dim,
3 * [100], eps=2 * [0.9])

assert_raises(ValueError, johnson_lindenstrauss_min_dim, 3 * [100],
eps=2 * [0.9])
def test_input_size_jl_min_dim():
with pytest.raises(ValueError):
johnson_lindenstrauss_min_dim(3 * [100], eps=2 * [0.9])

johnson_lindenstrauss_min_dim(np.random.randint(1, 10, size=(10, 10)),
eps=np.full((10, 10), 0.5))
Expand All @@ -81,18 +80,17 @@ def test_input_size_jl_min_dim():
# tests random matrix generation
###############################################################################
def check_input_size_random_matrix(random_matrix):
assert_raises(ValueError, random_matrix, 0, 0)
assert_raises(ValueError, random_matrix, -1, 1)
assert_raises(ValueError, random_matrix, 1, -1)
assert_raises(ValueError, random_matrix, 1, 0)
assert_raises(ValueError, random_matrix, -1, 0)
inputs = [(0, 0), (-1, 1), (1, -1), (1, 0), (-1, 0)]
for n_components, n_features in inputs:
with pytest.raises(ValueError):
random_matrix(n_components, n_features)


def check_size_generated(random_matrix):
assert random_matrix(1, 5).shape == (1, 5)
assert random_matrix(5, 1).shape == (5, 1)
assert random_matrix(5, 5).shape == (5, 5)
assert random_matrix(1, 1).shape == (1, 1)
inputs = [(1, 5), (5, 1), (5, 5), (1, 1)]
for n_components, n_features in inputs:
assert random_matrix(n_components, n_features).shape == (
n_components, n_features)


def check_zero_mean_and_unit_norm(random_matrix):
Expand All @@ -109,8 +107,8 @@ def check_input_with_sparse_random_matrix(random_matrix):
n_components, n_features = 5, 10

for density in [-1., 0.0, 1.1]:
assert_raises(ValueError,
random_matrix, n_components, n_features, density=density)
with pytest.raises(ValueError):
random_matrix(n_components, n_features, density=density)


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

A = _sparse_random_matrix(n_components,
n_features,
density=density,
random_state=0)
n_features,
density=density,
random_state=0)
A = densify(A)

# Check possible values
Expand Down Expand Up @@ -196,31 +194,27 @@ def test_sparse_random_matrix():
###############################################################################
# tests on random projection transformer
###############################################################################
def test_sparse_random_projection_transformer_invalid_density():
for RandomProjection in all_SparseRandomProjection:
assert_raises(ValueError,
RandomProjection(density=1.1).fit, data)

assert_raises(ValueError,
RandomProjection(density=0).fit, data)

assert_raises(ValueError,
RandomProjection(density=-0.1).fit, data)
@pytest.mark.parametrize("density", [1.1, 0, -0.1])
def test_sparse_random_projection_transformer_invalid_density(density):
for RandomProjection in all_SparseRandomProjection:
with pytest.raises(ValueError):
RandomProjection(density=density).fit(data)


def test_random_projection_transformer_invalid_input():
@pytest.mark.parametrize("n_components, fit_data", [
('auto', [[0, 1, 2]]), (-10, data)]
)
def test_random_projection_transformer_invalid_input(n_components, fit_data):
for RandomProjection in all_RandomProjection:
assert_raises(ValueError,
RandomProjection(n_components='auto').fit, [[0, 1, 2]])

assert_raises(ValueError,
RandomProjection(n_components=-10).fit, data)
with pytest.raises(ValueError):
RandomProjection(n_components=n_components).fit(fit_data)


def test_try_to_transform_before_fit():
for RandomProjection in all_RandomProjection:
assert_raises(ValueError,
RandomProjection(n_components='auto').transform, data)
with pytest.raises(ValueError):
RandomProjection(n_components='auto').transform(data)


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


def test_random_projection_embedding_quality():
Expand Down Expand Up @@ -318,7 +313,8 @@ def test_correct_RandomProjection_dimensions_embedding():
assert_array_equal(projected_1, projected_3)

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

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

for RandomProjection in all_RandomProjection:
assert_warns(DataDimensionalityWarning,
RandomProjection(n_components=n_features + 1).fit, data)
with pytest.warns(DataDimensionalityWarning):
RandomProjection(n_components=n_features + 1).fit(data)


def test_works_with_sparse_data():
Expand Down