Skip to content

Commit

Permalink
finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgayoso committed Dec 28, 2023
1 parent 578e5c1 commit 125ef84
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/scib_metrics/nearest_neighbors/_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
2 changes: 1 addition & 1 deletion tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 125ef84

Please sign in to comment.