Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions seapig/scores/knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class KNNScore(EmbeddingScore, FAISSIndexMixin, ABC):
----------
k : int, default 1
Number of nearest neighbors used to compute the distance score.
stat : {'max', 'mean', 'median', 'min'}, default 'max'
stat : {'max', 'mean', 'median', 'min'}, default 'min'
Statistic applied to aggregate distances across the k neighbors.
pca : `TensorPCA` or None, default None
Optional `TensorPCA` object for dimensionality reduction prior to scoring.
Expand All @@ -57,7 +57,7 @@ class KNNScore(EmbeddingScore, FAISSIndexMixin, ABC):
def __init__(
self,
k: int = 1,
stat: str = "max",
stat: str = "min",
pca: TensorPCA | None = None,
save_index: bool | Path = False,
) -> None:
Expand Down Expand Up @@ -110,7 +110,7 @@ def _filter_outliers(self, q: float | bool = False) -> None:
if self.index is None:
self._setup_index() # temporary index
scores, _ = self._distance(self.ref_embeddings, offset=1)
scores = self._stat(-scores)
scores = self._stat(scores)
threshold = torch.quantile(scores.float(), q=q)
index = scores >= threshold
self.ref_embeddings = self.ref_embeddings[index, :]
Expand Down Expand Up @@ -210,7 +210,7 @@ class EuclideanScore(KNNScore):
----------
k : int, default 1
Number of nearest neighbors to use.
stat : {'max', 'mean', 'median', 'min'}, default 'max'
stat : {'max', 'mean', 'median', 'min'}, default 'min'
Statistic to aggregate distances across the k neighbors.
pca : `TensorPCA` or None, default None
Optional `TensorPCA` object for dimensionality reduction prior to scoring.
Expand Down Expand Up @@ -241,7 +241,7 @@ class EuclideanScore(KNNScore):
def __init__(
self,
k: int = 1,
stat: str = "max",
stat: str = "min",
pca: TensorPCA | None = None,
save_index: bool | Path = False,
) -> None:
Expand Down Expand Up @@ -287,7 +287,7 @@ class CosineScore(KNNScore):
----------
k : int, default 1
Number of nearest neighbors to use.
stat : {'max', 'mean', 'median', 'min'}, default 'max'
stat : {'max', 'mean', 'median', 'min'}, default 'min'
Statistic to aggregate distances across the k neighbors.
pca : `TensorPCA` or None, default None
Optional `TensorPCA` object for dimensionality reduction prior to scoring.
Expand All @@ -307,7 +307,7 @@ class CosineScore(KNNScore):
def __init__(
self,
k: int = 1,
stat: str = "max",
stat: str = "min",
pca: TensorPCA | None = None,
save_index: bool | Path = False,
) -> None:
Expand Down Expand Up @@ -366,7 +366,7 @@ class MahalanobisScore(KNNScore):
----------
k : int, default 1
Number of nearest neighbors to use.
stat : {'max', 'mean', 'median', 'min'}, default 'max'
stat : {'max', 'mean', 'median', 'min'}, default 'min'
Statistic to aggregate distances across the k neighbors.
pca : `TensorPCA` or None, default None
Optional `TensorPCA` object for dimensionality reduction prior to scoring.
Expand All @@ -387,7 +387,7 @@ class MahalanobisScore(KNNScore):
def __init__(
self,
k: int = 1,
stat: str = "max",
stat: str = "min",
pca: TensorPCA | None = None,
save_index: bool | Path = False,
) -> None:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,18 @@ def test_knnstat_all_branches(
result = score._stat(dists)
expected_val = expected(dists)
torch.testing.assert_close(result[0], expected_val)


def test_k_returns_diff():
ref = torch.rand(5000, 256)
query = torch.rand(100, 256)

s1 = EuclideanScore(k=1)
s2 = EuclideanScore(k=10)
s1.fit(ref)
s2.fit(ref)

s_1 = s1.score(query)
s_2 = s2.score(query)

assert not torch.allclose(s_1, s_2)
Loading