Skip to content

Commit 978f42f

Browse files
committed
Draft some tests for the Neighbors class
1 parent 4d5e005 commit 978f42f

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ def sample_distances():
1212
def sample_indices():
1313
# 3 samples, 2 neighbors each
1414
return np.array([[0, 1], [1, 2], [2, 0]], dtype=np.int64)
15+
16+
17+
@pytest.fixture
18+
def small_data():
19+
# 5 points in 2D, easy to check neighbors
20+
x = np.array([[0, 0], [1, 0], [0, 1], [1, 1], [2, 2]], dtype=np.float64)
21+
y = np.array([[0, 0], [1, 0], [0, 1], [1, 1], [2, 2]], dtype=np.float64)
22+
return x, y

tests/test_neighbors.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import numpy as np
2+
3+
from cellmapper.knn import Neighbors
4+
5+
6+
def assert_adjacency_equal(neigh1, neigh2, attrs=("xx", "yy", "xy", "yx")):
7+
for attr in attrs:
8+
# Direct attribute access instead of getattr for Ruff compliance
9+
adj1 = getattr(neigh1, attr).boolean_adjacency().toarray()
10+
adj2 = getattr(neigh2, attr).boolean_adjacency().toarray()
11+
assert np.allclose(adj1, adj2), f"Adjacency matrices ({attr}) differ between methods"
12+
13+
14+
def test_neighbors_sklearn_vs_pynndescent(small_data):
15+
x, y = small_data
16+
n_neighbors = 3
17+
# sklearn
18+
neigh_skl = Neighbors(x, y)
19+
neigh_skl.compute_neighbors(n_neighbors=n_neighbors, method="sklearn")
20+
# pynndescent
21+
neigh_pynn = Neighbors(x, y)
22+
neigh_pynn.compute_neighbors(n_neighbors=n_neighbors, method="pynndescent")
23+
# Compare adjacency matrices
24+
assert_adjacency_equal(neigh_skl, neigh_pynn)
25+
26+
27+
def test_neighbors_repr(small_data):
28+
x, y = small_data
29+
neigh = Neighbors(x, y)
30+
r = repr(neigh)
31+
assert "Neighbors(" in r and "xrep_shape" in r and "yrep_shape" in r
32+
neigh.compute_neighbors(n_neighbors=2, method="sklearn")
33+
r2 = repr(neigh)
34+
assert "xx=True" in r2 and "yy=True" in r2 and "xy=True" in r2 and "yx=True" in r2

tests/test_neighbors_results.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ def test_knn_graph_distances(sample_distances, sample_indices):
2929
assert np.allclose(mat.diagonal(), 0)
3030

3131

32-
def test_knn_graph_connectivities_gaussian(sample_distances, sample_indices):
32+
@pytest.mark.parametrize("kernel", ["gaussian", "scarches", "random", "inverse_distance"])
33+
def test_knn_graph_connectivities_kernels(sample_distances, sample_indices, kernel):
3334
nr = NeighborsResults(distances=sample_distances, indices=sample_indices)
34-
mat = nr.knn_graph_connectivities(kernel="gaussian")
35+
mat = nr.knn_graph_connectivities(kernel=kernel)
3536
assert isinstance(mat, csr_matrix)
3637
assert mat.shape == (3, 3)
37-
# All values should be in (0, 1]
38-
assert np.all((mat.data > 0) & (mat.data <= 1))
38+
# All values should be > 0 for all kernels except 'random', which can be 0
39+
if kernel != "random":
40+
assert np.all(mat.data > 0)
3941

4042

4143
def test_boolean_adjacency(sample_distances, sample_indices):

0 commit comments

Comments
 (0)