From 125ef849f7803e0fb0dec5fe40abf898d1eefa5d Mon Sep 17 00:00:00 2001 From: adamgayoso Date: Thu, 28 Dec 2023 04:50:34 +0000 Subject: [PATCH] finalize --- src/scib_metrics/nearest_neighbors/_dataclass.py | 7 +++++-- tests/test_metrics.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/scib_metrics/nearest_neighbors/_dataclass.py b/src/scib_metrics/nearest_neighbors/_dataclass.py index 47a61df..f7119db 100644 --- a/src/scib_metrics/nearest_neighbors/_dataclass.py +++ b/src/scib_metrics/nearest_neighbors/_dataclass.py @@ -28,11 +28,13 @@ def __post_init__(self): chex.assert_equal_shape([self.indices, self.distances]) @property - def n_samples(self): + def n_samples(self) -> np.ndarray: + """Number of samples (cells).""" return self.indices.shape[0] @property - def n_neighbors(self): + def n_neighbors(self) -> np.ndarray: + """Number of neighbors.""" return self.indices.shape[1] @cached_property @@ -65,6 +67,7 @@ def knn_graph_connectivities(self) -> coo_matrix: return connectivities[0] def subset_neighbors(self, n: int) -> "NeighborsResults": + """Subset down to `n` neighbors.""" if n > self.n_neighbors: raise ValueError("n must be smaller than the number of neighbors") return self.__class__(indices=self.indices[:, :n], distances=self.distances[:, :n]) diff --git a/tests/test_metrics.py b/tests/test_metrics.py index d5bfa54..c327a06 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -74,7 +74,7 @@ def test_lisi_knn(): X, pd.DataFrame(labels, columns=["labels"]), label_colnames=["labels"], perplexity=10 )[:, 0] # Slight numerical differences arise due to how self edges are handled. With approximate nearest - # neighbors methods, there is no guarantee that the self edge is the first edge. To accomodate this, + # neighbors methods, there is no guarantee that the self edge is the first edge. To accommodate this, # we mask out self edges internally in lisi computation which causes slight numerical differences. np.testing.assert_allclose(lisi_res, harmonypy_lisi_res, rtol=5e-5, atol=5e-5)