|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from scipy.sparse import csr_matrix |
| 4 | + |
| 5 | +from cellmapper.knn import NeighborsResults |
| 6 | + |
| 7 | + |
| 8 | +def test_neighborsresults_init_shape(sample_distances, sample_indices): |
| 9 | + # Should not raise |
| 10 | + nr = NeighborsResults(distances=sample_distances, indices=sample_indices) |
| 11 | + assert nr.n_samples == 3 |
| 12 | + assert nr.n_neighbors == 2 |
| 13 | + assert nr.shape == (3, 3) |
| 14 | + |
| 15 | + |
| 16 | +def test_neighborsresults_invalid_shape(sample_distances): |
| 17 | + # indices shape mismatch should raise |
| 18 | + bad_indices = np.array([[0, 1, 2], [1, 2, 0], [2, 0, 1]]) |
| 19 | + with pytest.raises(ValueError): |
| 20 | + NeighborsResults(distances=sample_distances, indices=bad_indices) |
| 21 | + |
| 22 | + |
| 23 | +def test_knn_graph_distances(sample_distances, sample_indices): |
| 24 | + nr = NeighborsResults(distances=sample_distances, indices=sample_indices) |
| 25 | + mat = nr.knn_graph_distances |
| 26 | + assert isinstance(mat, csr_matrix) |
| 27 | + assert mat.shape == (3, 3) |
| 28 | + # Check that diagonal is zero (self-distance) |
| 29 | + assert np.allclose(mat.diagonal(), 0) |
| 30 | + |
| 31 | + |
| 32 | +def test_knn_graph_connectivities_gaussian(sample_distances, sample_indices): |
| 33 | + nr = NeighborsResults(distances=sample_distances, indices=sample_indices) |
| 34 | + mat = nr.knn_graph_connectivities(kernel="gaussian") |
| 35 | + assert isinstance(mat, csr_matrix) |
| 36 | + assert mat.shape == (3, 3) |
| 37 | + # All values should be in (0, 1] |
| 38 | + assert np.all((mat.data > 0) & (mat.data <= 1)) |
| 39 | + |
| 40 | + |
| 41 | +def test_boolean_adjacency(sample_distances, sample_indices): |
| 42 | + nr = NeighborsResults(distances=sample_distances, indices=sample_indices) |
| 43 | + mat = nr.boolean_adjacency() |
| 44 | + assert isinstance(mat, csr_matrix) |
| 45 | + assert mat.shape == (3, 3) |
| 46 | + # All nonzero values should be 1 |
| 47 | + assert np.all(mat.data == 1) |
0 commit comments