Skip to content

Commit 4d5e005

Browse files
committed
Add basic tests
1 parent 47d85b9 commit 4d5e005

File tree

5 files changed

+57
-42
lines changed

5 files changed

+57
-42
lines changed

src/cellmapper/check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def check(self) -> None:
5757

5858

5959
INSTALL_HINTS = types.SimpleNamespace(
60-
rapids="To speed up k-NN search on GPU, you may install RAPIDS following the guide from "
60+
rapids="To speed up k-NN search on GPU, you may install rapids following the guide from "
6161
"https://docs.rapids.ai/install/. Note that you will only need cuML.",
6262
faiss="To speed up k-NN search on GPU, you may install faiss following the guide from "
6363
"https://github.com/facebookresearch/faiss/blob/main/INSTALL.md",

src/cellmapper/knn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def compute_neighbors(
191191
logger.info("Using %s to compute %d neighbors.", method, n_neighbors)
192192

193193
if method == "rapids":
194-
check_deps("rapids_singlecell")
194+
check_deps("rapids")
195195
import cuml as cm
196196
import cupy as cp
197197

tests/conftest.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import anndata as ad
21
import numpy as np
32
import pytest
43

54

65
@pytest.fixture
7-
def adata():
8-
adata = ad.AnnData(X=np.array([[1.2, 2.3], [3.4, 4.5], [5.6, 6.7]]).astype(np.float32))
9-
adata.layers["scaled"] = np.array([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]]).astype(np.float32)
6+
def sample_distances():
7+
# 3 samples, 2 neighbors each
8+
return np.array([[0.0, 1.0], [0.0, 2.0], [0.0, 3.0]], dtype=np.float64)
109

11-
return adata
10+
11+
@pytest.fixture
12+
def sample_indices():
13+
# 3 samples, 2 neighbors each
14+
return np.array([[0, 1], [1, 2], [2, 0]], dtype=np.int64)

tests/test_basic.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,5 @@
1-
import pytest
2-
31
import cellmapper
42

53

64
def test_package_has_version():
75
assert cellmapper.__version__ is not None
8-
9-
10-
@pytest.mark.skip(reason="This decorator should be removed when test passes.")
11-
def test_example():
12-
assert 1 == 0 # This test is designed to fail.
13-
14-
15-
@pytest.mark.skip(reason="This decorator should be removed when test passes.")
16-
@pytest.mark.parametrize(
17-
"transform,layer_key,max_items,expected_len,expected_substring",
18-
[
19-
# Test default parameters
20-
(lambda vals: f"mean={vals.mean():.2f}", None, 100, 1, "mean="),
21-
# Test with layer_key
22-
(lambda vals: f"mean={vals.mean():.2f}", "scaled", 100, 1, "mean=0."),
23-
# Test with max_items limit (won't affect single item)
24-
(lambda vals: f"max={vals.max():.2f}", None, 1, 1, "max=6.70"),
25-
],
26-
)
27-
def test_elaborate_example_adata_only_simple(
28-
adata, # this tests uses the adata object from the fixture in the conftest.py
29-
transform,
30-
layer_key,
31-
max_items,
32-
expected_len,
33-
expected_substring,
34-
):
35-
result = cellmapper.pp.elaborate_example(
36-
items=[adata], transform=transform, layer_key=layer_key, max_items=max_items
37-
)
38-
39-
assert len(result) == expected_len
40-
assert expected_substring in result[0]

tests/test_neighbors_results.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

Comments
 (0)