Skip to content

Commit

Permalink
Refactor: Revert Matches
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed Aug 3, 2023
1 parent c37f80b commit 5731e70
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ index = Index(

vector = np.array([0.2, 0.6, 0.4])
index.add(42, vector)
matches: SearchResults = index.search(vector, 10)
matches: Matches = index.search(vector, 10)

assert len(index) == 1
assert len(matches) == 1
Expand Down
2 changes: 1 addition & 1 deletion golang/usearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ typedef struct {
float* Distances;
int Len;
char* Error;
} SearchResults;
} Matches;
2 changes: 1 addition & 1 deletion include/usearch/index_plugins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ enum class scalar_kind_t : std::uint8_t {
f64_k,
f32_k,
f16_k,
i8_k,
f8_k,
// Common Integral:
u64_k,
u32_k,
Expand Down
6 changes: 3 additions & 3 deletions javascript/usearch.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/** Search result object. */
export interface SearchResults {
export interface Matches {
/** The labels of the nearest neighbors found, size n*k. */
labels: BigUint64Array,
/** The disances of the nearest negihbors found, size n*k. */
Expand Down Expand Up @@ -81,7 +81,7 @@ export class Index {
*
* @param {Float32Array} mat Input vectors to search, matrix of size n * d.
* @param {number} k The number of nearest neighbors to search for.
* @return {SearchResults} Output of the search result.
* @return {Matches} Output of the search result.
*/
search(mat: Float32Array, k: number): SearchResults;
search(mat: Float32Array, k: number): Matches;
}
6 changes: 3 additions & 3 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ index = Index(

vector = np.array([0.2, 0.6, 0.4])
index.add(42, vector)
matches: SearchResults = index.search(vector, 10)
matches: Matches = index.search(vector, 10)

assert len(index) == 1
assert len(matches) == 1
Expand Down Expand Up @@ -61,9 +61,9 @@ keys = np.arange(n)
vectors = np.random.uniform(0, 0.3, (n, index.ndim)).astype(np.float32)

index.add(keys, vectors, threads=..., copy=...)
matches: BatchSearchResults = index.search(vectors, 10, threads=...)
matches: BatchMatches = index.search(vectors, 10, threads=...)

first_query_matches: SearchResults = matches[0]
first_query_matches: Matches = matches[0]
assert matches[0].key == 0
assert matches[0].distance <= 0.001

Expand Down
6 changes: 3 additions & 3 deletions python/scripts/index_faiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from faiss import IndexHNSWFlat, IndexIVFPQ, read_index

from usearch.index import SearchResults
from usearch.index import Matches
from usearch.index import (
DEFAULT_CONNECTIVITY,
DEFAULT_EXPANSION_ADD,
Expand Down Expand Up @@ -45,9 +45,9 @@ def add(self, keys, vectors):
# self._faiss.add_with_ids(vectors, keys)
self._faiss.add(vectors)

def search(self, queries, k: int) -> SearchResults:
def search(self, queries, k: int) -> Matches:
distances, keys = self._faiss.search(queries, k)
return SearchResults(keys, distances, np.array([k] * queries.shape[0]))
return Matches(keys, distances, np.array([k] * queries.shape[0]))

def __len__(self) -> int:
return self._faiss.ntotal
Expand Down
24 changes: 12 additions & 12 deletions python/scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
MetricKind,
ScalarKind,
Match,
SearchResults,
BatchSearchResults,
Matches,
BatchMatches,
)
from usearch.index import (
DEFAULT_CONNECTIVITY,
Expand Down Expand Up @@ -117,7 +117,7 @@ def test_index(
if numpy_type != np.byte:
assert np.allclose(index[42], vector, atol=0.1)

matches: SearchResults = index.search(vector, 10)
matches: Matches = index.search(vector, 10)
assert len(matches.keys) == 1, "Number of matches"
assert len(matches.keys) == len(matches.distances), "Symmetric match sub-arrays"
assert len({match.key for match in matches}) == 1, "Iteration over matches"
Expand Down Expand Up @@ -152,27 +152,27 @@ def test_index(
assert len(index) == 0
index.add(42, vector)
assert len(index) == 1
matches: SearchResults = index.search(vector, 10)
matches: Matches = index.search(vector, 10)
assert len(matches) == 1

index_copy = index.copy()
assert len(index_copy) == 1
assert len(index_copy[42]) == ndim
matches_copy: SearchResults = index_copy.search(vector, 10)
matches_copy: Matches = index_copy.search(vector, 10)
assert np.all(matches_copy.keys == matches.keys)

index.load(temporary_usearch_filename)
assert len(index) == 1
assert len(index[42]) == ndim

matches_loaded: SearchResults = index.search(vector, 10)
matches_loaded: Matches = index.search(vector, 10)
assert np.all(matches_loaded.keys == matches.keys)

index = Index.restore(temporary_usearch_filename, view=True)
assert len(index) == 1
assert len(index[42]) == ndim

matches_viewed: SearchResults = index.search(vector, 10)
matches_viewed: Matches = index.search(vector, 10)
assert np.all(matches_viewed.keys == matches.keys)

# Cleanup
Expand Down Expand Up @@ -217,7 +217,7 @@ def test_index_batch(
assert len(index) == batch_size
assert np.allclose(index.get_vectors(keys).astype(numpy_type), vectors, atol=0.1)

matches: BatchSearchResults = index.search(vectors, 10, threads=2)
matches: BatchMatches = index.search(vectors, 10, threads=2)
assert matches.keys.shape[0] == matches.distances.shape[0]
assert len(matches) == batch_size
assert np.all(np.sort(index.keys) == np.sort(keys))
Expand All @@ -238,7 +238,7 @@ def test_index_batch(
assert len(index[0]) == ndim

if batch_size > 1:
matches_loaded: BatchSearchResults = index.search(vectors, 10, threads=2)
matches_loaded: BatchMatches = index.search(vectors, 10, threads=2)
for idx in range(len(matches_loaded)):
assert np.all(matches_loaded[idx].keys == matches[idx].keys)

Expand All @@ -247,7 +247,7 @@ def test_index_batch(
assert len(index[0]) == ndim

if batch_size > 1:
matches_viewed: BatchSearchResults = index.search(vectors, 10, threads=2)
matches_viewed: BatchMatches = index.search(vectors, 10, threads=2)
for idx in range(len(matches_viewed)):
assert np.all(matches_viewed[idx].keys == matches[idx].keys)

Expand Down Expand Up @@ -279,15 +279,15 @@ def test_exact_recall(

# Search one at a time
for i in range(batch_size):
matches: SearchResults = index.search(vectors[i], 10, exact=True)
matches: Matches = index.search(vectors[i], 10, exact=True)
found_labels = matches.keys
assert found_labels[0] == i
assert matches.computed_distances == len(index)
assert matches.visited_members == 0, "Exact search won't traverse the graph"

# Search the whole batch
if batch_size > 1:
matches: BatchSearchResults = index.search(vectors, 10, exact=True)
matches: BatchMatches = index.search(vectors, 10, exact=True)
assert matches.computed_distances == len(index) * len(vectors)
assert matches.visited_members == 0, "Exact search won't traverse the graph"

Expand Down
8 changes: 4 additions & 4 deletions python/usearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from ucall.client import Client

from usearch.index import SearchResults
from usearch.index import Matches


def _vector_to_ascii(vector: np.ndarray) -> Optional[str]:
Expand Down Expand Up @@ -49,7 +49,7 @@ def add(self, keys: Union[np.ndarray, int], vectors: np.ndarray):
else:
return self.add_many(keys, vectors)

def search_one(self, vector: np.ndarray, count: int) -> SearchResults:
def search_one(self, vector: np.ndarray, count: int) -> Matches:
matches: List[dict] = []
vector = vector.flatten()
ascii = _vector_to_ascii(vector)
Expand All @@ -71,7 +71,7 @@ def search_one(self, vector: np.ndarray, count: int) -> SearchResults:

return keys, distances, counts

def search_many(self, vectors: np.ndarray, count: int) -> SearchResults:
def search_many(self, vectors: np.ndarray, count: int) -> Matches:
batch_size: int = vectors.shape[0]
list_of_matches: List[List[dict]] = self.client.search_many(
vectors=vectors, count=count
Expand All @@ -88,7 +88,7 @@ def search_many(self, vectors: np.ndarray, count: int) -> SearchResults:

return keys, distances, counts

def search(self, vectors: np.ndarray, count: int) -> SearchResults:
def search(self, vectors: np.ndarray, count: int) -> Matches:
if vectors.ndim == 1 or (vectors.ndim == 2 and vectors.shape[0] == 1):
return self.search_one(vectors, count)
else:
Expand Down
6 changes: 3 additions & 3 deletions python/usearch/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from usearch.io import load_matrix
from usearch.index import (
Index,
SearchResults,
BatchSearchResults,
Matches,
BatchMatches,
MetricKind,
MetricKindBitwise,
Key,
Expand Down Expand Up @@ -111,7 +111,7 @@ def self_recall(index: Index, sample: float = 1, **kwargs) -> SearchStats:
keys = np.random.choice(keys, int(ceil(len(keys) * sample)))

queries = index.get_vectors(keys, index.dtype)
matches: BatchSearchResults = index.search(queries, **kwargs)
matches: BatchMatches = index.search(queries, **kwargs)
count_matches: float = matches.count_matches(keys)
return SearchStats(
index_size=len(index),
Expand Down
34 changes: 17 additions & 17 deletions python/usearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _search_in_compiled(
exact: bool,
log: Union[str, bool],
batch_size: int,
) -> Union[SearchResults, BatchSearchResults]:
) -> Union[Matches, BatchMatches]:
#
assert isinstance(vectors, np.ndarray), "Expects a NumPy array"
assert vectors.ndim == 1 or vectors.ndim == 2, "Expects a matrix or vector"
Expand All @@ -121,8 +121,8 @@ def _search_in_compiled(
count_vectors = vectors.shape[0]

def distil_batch(
batch_matches: BatchSearchResults,
) -> Union[BatchSearchResults, SearchResults]:
batch_matches: BatchMatches,
) -> Union[BatchMatches, Matches]:
return batch_matches[0] if count_vectors == 1 else batch_matches

if log and batch_size == 0:
Expand All @@ -149,12 +149,12 @@ def distil_batch(
exact=exact,
threads=threads,
)
tasks_matches.append(BatchSearchResults(*tuple_))
tasks_matches.append(BatchMatches(*tuple_))
pbar.update(vectors.shape[0])

pbar.close()
return distil_batch(
BatchSearchResults(
BatchMatches(
keys=np.vstack([m.keys for m in tasks_matches]),
distances=np.vstack([m.distances for m in tasks_matches]),
counts=np.concatenate([m.counts for m in tasks_matches], axis=None),
Expand All @@ -168,7 +168,7 @@ def distil_batch(
exact=exact,
threads=threads,
)
return distil_batch(BatchSearchResults(*tuple_))
return distil_batch(BatchMatches(*tuple_))


def _add_to_compiled(
Expand Down Expand Up @@ -244,7 +244,7 @@ class Match:


@dataclass
class SearchResults:
class Matches:
"""This class contains information about multiple retrieved vectors for single query,
i.e it is a set of `Match` instances."""

Expand Down Expand Up @@ -274,13 +274,13 @@ def to_list(self) -> List[tuple]:
return [(int(l), float(d)) for l, d in zip(self.keys, self.distances)]

def __repr__(self) -> str:
return f"usearch.SearchResults({len(self)})"
return f"usearch.Matches({len(self)})"


@dataclass
class BatchSearchResults:
class BatchMatches:
"""This class contains information about multiple retrieved vectors for multiple queries,
i.e it is a set of `SearchResults` instances."""
i.e it is a set of `Matches` instances."""

keys: np.ndarray
distances: np.ndarray
Expand All @@ -292,9 +292,9 @@ class BatchSearchResults:
def __len__(self) -> int:
return len(self.counts)

def __getitem__(self, index: int) -> SearchResults:
def __getitem__(self, index: int) -> Matches:
if isinstance(index, int) and index < len(self):
return SearchResults(
return Matches(
keys=self.keys[index, : self.counts[index]],
distances=self.distances[index, : self.counts[index]],
visited_members=self.visited_members // len(self),
Expand All @@ -309,12 +309,12 @@ def to_list(self) -> List[List[tuple]]:
return [match.to_list() for matches in list_of_matches for match in matches]

def mean_recall(self, expected: np.ndarray, k: Optional[int] = None) -> float:
"""Measures recall [0, 1] as of `SearchResults` that contain the corresponding
"""Measures recall [0, 1] as of `Matches` that contain the corresponding
`expected` entry anywhere among results."""
return self.count_matches(expected, k=k) / len(expected)

def count_matches(self, expected: np.ndarray, k: Optional[int] = None) -> int:
"""Measures recall [0, len(expected)] as of `SearchResults` that contain the corresponding
"""Measures recall [0, len(expected)] as of `Matches` that contain the corresponding
`expected` entry anywhere among results.
"""
assert len(expected) == len(self)
Expand All @@ -330,7 +330,7 @@ def count_matches(self, expected: np.ndarray, k: Optional[int] = None) -> int:
return recall

def __repr__(self) -> str:
return f"usearch.BatchSearchResults({np.sum(self.counts)} across {len(self)} queries)"
return f"usearch.BatchMatches({np.sum(self.counts)} across {len(self)} queries)"


class CompiledMetric(NamedTuple):
Expand Down Expand Up @@ -534,7 +534,7 @@ def search(
exact: bool = False,
log: Union[str, bool] = False,
batch_size: int = 0,
) -> Union[SearchResults, BatchSearchResults]:
) -> Union[Matches, BatchMatches]:
"""
Performs approximate nearest neighbors search for one or more queries.
Expand All @@ -551,7 +551,7 @@ def search(
:param batch_size: Number of vectors to process at once, defaults to 0
:type batch_size: int, optional
:return: Approximate matches for one or more queries
:rtype: Union[SearchResults, BatchSearchResults]
:rtype: Union[Matches, BatchMatches]
"""

return _search_in_compiled(
Expand Down
6 changes: 3 additions & 3 deletions python/usearch/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import List

from ucall.rich_posix import Server
from usearch.index import Index, SearchResults, Key
from usearch.index import Index, Matches, Key


def _ascii_to_vector(string: str) -> np.ndarray:
Expand Down Expand Up @@ -73,12 +73,12 @@ def add_many(keys: np.ndarray, vectors: np.ndarray):
def search_one(vector: np.ndarray, count: int) -> List[dict]:
print("search", vector, count)
vectors = vector.reshape(vector.shape[0], 1)
results: SearchResults = index.search(vectors, count)
results: Matches = index.search(vectors, count)
return results.to_list()

@server
def search_many(vectors: np.ndarray, count: int) -> List[List[dict]]:
results: SearchResults = index.search(vectors, count)
results: Matches = index.search(vectors, count)
return results.to_list()

@server
Expand Down
Loading

0 comments on commit 5731e70

Please sign in to comment.