Skip to content

Commit

Permalink
Merge pull request #56 from Dobatymo/new-release
Browse files Browse the repository at this point in the history
v0.1.1
  • Loading branch information
Dobatymo committed May 22, 2024
2 parents f1f0da8 + 61ecd27 commit 0b886dd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
12 changes: 4 additions & 8 deletions pynear/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import List
from typing import Tuple

import numpy as np

from _pynear import BKTreeBinaryIndex64
from _pynear import BKTreeBinaryIndex128
from _pynear import BKTreeBinaryIndex256
Expand All @@ -21,16 +23,13 @@
from _pynear import dist_hamming_512
from _pynear import dist_l1
from _pynear import dist_l2
import numpy as np

from ._version import __version__


def dist_hamming(a: List, b: List):
if len(a) != len(b):
raise ValueError(
f"invalid data dimension: a and b dimensions must agree."
)
raise ValueError("invalid data dimension: a and b dimensions must agree.")
dim = len(a)
if dim == 64:
return dist_hamming_512(a, b)
Expand All @@ -41,10 +40,7 @@ def dist_hamming(a: List, b: List):
elif dim == 8:
return dist_hamming_64(a, b)
else:
raise ValueError(
f"invalid data dimension: hamming distance only supports 64, 32, 16 or 8 bytes of data"
)

raise ValueError("invalid data dimension: hamming distance only supports 64, 32, 16 or 8 bytes of data")


class VPTreeBinaryIndex:
Expand Down
2 changes: 1 addition & 1 deletion pynear/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
_version = {
"major": 0,
"minor": 1,
"revis": 0,
"revis": 1,
}
__version__ = ".".join([str(a) for a in _version.values()])
2 changes: 2 additions & 0 deletions pynear/benchmark/index_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def build_index(self, data: np.ndarray):
def _search_implementation(self, query, k: int):
self._index.searchKNN(query, k)


class PyNearBKTreeAdapter(IndexAdapter):
def __init__(self):
self._index = pynear.BKTreeBinaryIndex()
Expand All @@ -83,6 +84,7 @@ def build_index(self, data: np.ndarray):
def _search_implementation(self, query, k: int):
self._index.find_threshold(query, self._dimensions)


class FaissIndexFlatL2Adapter(IndexAdapter):
def __init__(self):
self._index = None
Expand Down
7 changes: 5 additions & 2 deletions pynear/tests/test_vptree.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

np.set_printoptions(threshold=sys.maxsize)


def hamming_distance_pairwise(a: np.ndarray, b: np.ndarray) -> np.ndarray:
result = []
for ai in a:
Expand All @@ -37,6 +38,7 @@ def hamming_distance_pairwise(a: np.ndarray, b: np.ndarray) -> np.ndarray:

return np.array(result).astype(np.uint8)


def euclidean_distance_pairwise(a: np.ndarray, b: np.ndarray) -> np.ndarray:
result = []
for ai in a:
Expand All @@ -60,6 +62,7 @@ def manhattan_distance_pairwise(a: np.ndarray, b: np.ndarray) -> np.ndarray:

return np.array(result)


def chebyshev_distance_pairwise(a: np.ndarray, b: np.ndarray) -> np.ndarray:
result = []
for ai in a:
Expand Down Expand Up @@ -211,9 +214,9 @@ def test_k_equals_dataset(vptree_cls, exaustive_metric):

vptree_indices = np.array(vptree_indices, dtype=np.uint64)[:, ::-1]
vptree_distances = np.array(vptree_distances, dtype=np.float64)[:, ::-1]
dist_diff = vptree_distances - exaustive_distances
dist_diff = vptree_distances - exaustive_distances
ind_diff = vptree_indices - exaustive_indices
print(">>>>>>>>>>>>", dist_diff[dist_diff > 1e-7] )
print(">>>>>>>>>>>>", dist_diff[dist_diff > 1e-7])
print(">>>>>>>>>>>>", np.argwhere(ind_diff != 0))

np.testing.assert_allclose(exaustive_distances, vptree_distances, rtol=1e-06)
Expand Down

0 comments on commit 0b886dd

Please sign in to comment.