diff --git a/pynear/__init__.py b/pynear/__init__.py index 4234bfd..4e470f1 100644 --- a/pynear/__init__.py +++ b/pynear/__init__.py @@ -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 @@ -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) @@ -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: diff --git a/pynear/_version.py b/pynear/_version.py index 26dc89a..3cab177 100644 --- a/pynear/_version.py +++ b/pynear/_version.py @@ -3,6 +3,6 @@ _version = { "major": 0, "minor": 1, - "revis": 0, + "revis": 1, } __version__ = ".".join([str(a) for a in _version.values()]) diff --git a/pynear/benchmark/index_adapters.py b/pynear/benchmark/index_adapters.py index e7e35b1..563dd0e 100644 --- a/pynear/benchmark/index_adapters.py +++ b/pynear/benchmark/index_adapters.py @@ -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() @@ -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 diff --git a/pynear/tests/test_vptree.py b/pynear/tests/test_vptree.py index 6c3b195..64716ee 100644 --- a/pynear/tests/test_vptree.py +++ b/pynear/tests/test_vptree.py @@ -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: @@ -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: @@ -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: @@ -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)