/
index.py
1401 lines (1188 loc) 路 46.7 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
from inspect import signature
# The purpose of this file is to provide Pythonic wrapper on top
# the native precompiled CPython module. It improves compatibility
# Python tooling, linters, and static analyzers. It also embeds JIT
# into the primary `Index` class, connecting USearch with Numba.
import os
import math
from dataclasses import dataclass
from typing import (
Any,
Optional,
Union,
NamedTuple,
List,
Iterable,
Tuple,
Dict,
Callable,
)
import numpy as np
from tqdm import tqdm
# Precompiled symbols that won't be exposed directly:
from usearch.compiled import (
Index as _CompiledIndex,
Indexes as _CompiledIndexes,
IndexStats as _CompiledIndexStats,
index_dense_metadata_from_path as _index_dense_metadata_from_path,
index_dense_metadata_from_buffer as _index_dense_metadata_from_buffer,
exact_search as _exact_search,
hardware_acceleration as _hardware_acceleration,
)
# Precompiled symbols that will be exposed
from usearch.compiled import (
MetricKind,
ScalarKind,
MetricSignature,
DEFAULT_CONNECTIVITY,
DEFAULT_EXPANSION_ADD,
DEFAULT_EXPANSION_SEARCH,
USES_OPENMP,
USES_SIMSIMD,
USES_FP16LIB,
)
MetricKindBitwise = (
MetricKind.Hamming,
MetricKind.Tanimoto,
MetricKind.Sorensen,
)
class CompiledMetric(NamedTuple):
pointer: int
kind: MetricKind
signature: MetricSignature
Key = np.uint64
KeyOrKeysLike = Union[Key, Iterable[Key], int, Iterable[int], np.ndarray, memoryview]
VectorOrVectorsLike = Union[np.ndarray, Iterable[np.ndarray], memoryview]
DTypeLike = Union[str, ScalarKind]
MetricLike = Union[str, MetricKind, CompiledMetric]
NoneType = type(None)
PathOrBuffer = Union[str, os.PathLike, bytes]
ProgressCallback = Callable[[int, int], bool]
def _match_signature(
func: Callable[[Any], Any], arg_types: List[type], ret_type: type
) -> bool:
assert callable(func), "Not callable"
sig = signature(func)
param_types = [param.annotation for param in sig.parameters.values()]
return param_types == arg_types and sig.return_annotation == ret_type
def _normalize_dtype(
dtype,
ndim: int = 0,
metric: MetricKind = MetricKind.Cos,
) -> ScalarKind:
if dtype is None or dtype == "":
if metric in MetricKindBitwise:
return ScalarKind.B1
if _hardware_acceleration(dtype=ScalarKind.F16, ndim=ndim, metric_kind=metric):
return ScalarKind.F16
return ScalarKind.F32
if isinstance(dtype, ScalarKind):
return dtype
if isinstance(dtype, str):
dtype = dtype.lower()
_normalize = {
"f64": ScalarKind.F64,
"f32": ScalarKind.F32,
"f16": ScalarKind.F16,
"i8": ScalarKind.I8,
"b1": ScalarKind.B1,
"b1x8": ScalarKind.B1,
"float64": ScalarKind.F64,
"float32": ScalarKind.F32,
"float16": ScalarKind.F16,
"int8": ScalarKind.I8,
np.float64: ScalarKind.F64,
np.float32: ScalarKind.F32,
np.float16: ScalarKind.F16,
np.int8: ScalarKind.I8,
np.uint8: ScalarKind.B1,
}
return _normalize[dtype]
def _to_numpy_dtype(dtype: ScalarKind):
_normalize = {
ScalarKind.F64: np.float64,
ScalarKind.F32: np.float32,
ScalarKind.F16: np.float16,
ScalarKind.I8: np.int8,
ScalarKind.B1: np.uint8,
}
if dtype in _normalize.values():
return dtype
return _normalize[dtype]
def _normalize_metric(metric) -> MetricKind:
if metric is None:
return MetricKind.Cos
if isinstance(metric, str):
_normalize = {
"cos": MetricKind.Cos,
"cosine": MetricKind.Cos,
"ip": MetricKind.IP,
"dot": MetricKind.IP,
"inner_product": MetricKind.IP,
"l2sq": MetricKind.L2sq,
"l2_sq": MetricKind.L2sq,
"haversine": MetricKind.Haversine,
"divergence": MetricKind.Divergence,
"pearson": MetricKind.Pearson,
"hamming": MetricKind.Hamming,
"tanimoto": MetricKind.Tanimoto,
"sorensen": MetricKind.Sorensen,
}
return _normalize[metric.lower()]
return metric
def _search_in_compiled(
compiled_callable: Callable,
vectors: np.ndarray,
*,
log: Union[str, bool],
progress: Optional[ProgressCallback],
**kwargs,
) -> 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"
assert not progress or _match_signature(
progress, [int, int], bool
), "Invalid callback"
if vectors.ndim == 1:
vectors = vectors.reshape(1, len(vectors))
count_vectors = vectors.shape[0]
def distill_batch(
batch_matches: BatchMatches,
) -> Union[BatchMatches, Matches]:
return batch_matches[0] if count_vectors == 1 else batch_matches
# Create progress bar if needed
if log:
name = log if isinstance(log, str) else "Search"
progress_bar = tqdm(
desc=name,
total=count_vectors,
unit="vector",
)
def update_progress_bar(processed: int, total: int) -> bool:
progress_bar.update(processed - progress_bar.n)
return progress if progress else True
tuple_ = compiled_callable(vectors, progress=update_progress_bar, **kwargs)
progress_bar.close()
else:
tuple_ = compiled_callable(vectors, **kwargs)
return distill_batch(BatchMatches(*tuple_))
def _add_to_compiled(
compiled,
*,
keys,
vectors,
copy: bool,
threads: int,
log: Union[str, bool],
progress: Optional[ProgressCallback],
) -> Union[int, np.ndarray]:
#
assert isinstance(vectors, np.ndarray), "Expects a NumPy array"
assert not progress or _match_signature(
progress, [int, int], bool
), "Invalid callback"
assert vectors.ndim == 1 or vectors.ndim == 2, "Expects a matrix or vector"
if vectors.ndim == 1:
vectors = vectors.reshape(1, len(vectors))
# Validate or generate the keys
count_vectors = vectors.shape[0]
generate_labels = keys is None
if generate_labels:
start_id = len(compiled)
keys = np.arange(start_id, start_id + count_vectors, dtype=Key)
else:
if not isinstance(keys, Iterable):
assert count_vectors == 1, "Each vector must have a key"
keys = [keys]
keys = np.array(keys).astype(Key)
assert len(keys) == count_vectors
# Create progress bar if needed
if log:
name = log if isinstance(log, str) else "Add"
pbar = tqdm(
desc=name,
total=count_vectors,
unit="vector",
)
def update_progress_bar(processed: int, total: int) -> bool:
pbar.update(processed - pbar.n)
return progress(processed, total) if progress else True
compiled.add_many(
keys,
vectors,
copy=copy,
threads=threads,
progress=update_progress_bar,
)
pbar.close()
else:
compiled.add_many(keys, vectors, copy=copy, threads=threads, progress=progress)
return keys
@dataclass
class Match:
"""This class contains information about retrieved vector."""
key: int
distance: float
def to_tuple(self) -> tuple:
return self.key, self.distance
@dataclass
class Matches:
"""This class contains information about multiple retrieved vectors for single query,
i.e it is a set of `Match` instances."""
keys: np.ndarray
distances: np.ndarray
visited_members: int = 0
computed_distances: int = 0
def __len__(self) -> int:
return len(self.keys)
def __getitem__(self, index: int) -> Match:
if isinstance(index, int) and index < len(self):
return Match(
key=self.keys[index],
distance=self.distances[index],
)
else:
raise IndexError(f"`index` must be an integer under {len(self)}")
def to_list(self) -> List[tuple]:
"""
Convert matches to the list of tuples which contain matches' indices and distances to them.
"""
return [(int(l), float(d)) for l, d in zip(self.keys, self.distances)]
def __repr__(self) -> str:
return f"usearch.Matches({len(self)})"
@dataclass
class BatchMatches:
"""This class contains information about multiple retrieved vectors for multiple queries,
i.e it is a set of `Matches` instances."""
keys: np.ndarray
distances: np.ndarray
counts: np.ndarray
visited_members: int = 0
computed_distances: int = 0
def __len__(self) -> int:
return len(self.counts)
def __getitem__(self, index: int) -> Matches:
if isinstance(index, int) and index < len(self):
return Matches(
keys=self.keys[index, : self.counts[index]],
distances=self.distances[index, : self.counts[index]],
visited_members=self.visited_members // len(self),
computed_distances=self.computed_distances // len(self),
)
else:
raise IndexError(f"`index` must be an integer under {len(self)}")
def to_list(self) -> List[List[tuple]]:
"""Convert the result for each query to the list of tuples with information about its matches."""
list_of_matches = [self.__getitem__(row) for row in range(self.__len__())]
return [match.to_tuple() for matches in list_of_matches for match in matches]
def mean_recall(self, expected: np.ndarray, count: Optional[int] = None) -> float:
"""Measures recall [0, 1] as of `Matches` that contain the corresponding
`expected` entry anywhere among results."""
return self.count_matches(expected, count=count) / len(expected)
def count_matches(self, expected: np.ndarray, count: Optional[int] = None) -> int:
"""Measures recall [0, len(expected)] as of `Matches` that contain the corresponding
`expected` entry anywhere among results.
"""
assert len(expected) == len(self)
recall = 0
if count is None:
count = self.keys.shape[1]
if count == 1:
recall = np.sum(self.keys[:, 0] == expected)
else:
for i in range(len(self)):
recall += expected[i] in self.keys[i, :count]
return recall
def __repr__(self) -> str:
return f"usearch.BatchMatches({np.sum(self.counts)} across {len(self)} queries)"
@dataclass
class Clustering:
def __init__(
self,
index: Index,
matches: BatchMatches,
queries: Optional[np.ndarray] = None,
) -> None:
if queries is None:
queries = index._compiled.get_keys_in_slice()
self.index = index
self.queries = queries
self.matches = matches
def __repr__(self) -> str:
return f"usearch.Clustering(for {len(self.queries)} queries)"
@property
def centroids_popularity(self) -> Tuple[np.ndarray, np.ndarray]:
return np.unique(self.matches.keys, return_counts=True)
def members_of(self, centroid: Key) -> np.ndarray:
return self.queries[self.matches.keys.flatten() == centroid]
def subcluster(self, centroid: Key, **clustering_kwards) -> Clustering:
sub_keys = self.members_of(centroid)
return self.index.cluster(keys=sub_keys, **clustering_kwards)
def plot_centroids_popularity(self):
from matplotlib import pyplot as plt
_, sizes = self.centroids_popularity
plt.yscale("log")
plt.plot(sorted(sizes), np.arange(len(sizes)))
plt.show()
@property
def network(self):
import networkx as nx
keys, sizes = self.centroids_popularity
g = nx.Graph()
for key, size in zip(keys, sizes):
g.add_node(key, size=size)
for i, i_key in enumerate(keys):
for j_key in keys[:i]:
d = self.index.pairwise_distance(i_key, j_key)
g.add_edge(i_key, j_key, distance=d)
return g
class IndexedKeys:
"""Smart-reference for the range of keys present in a specific `Index`"""
def __init__(self, index: Index) -> None:
self.index = index
def __len__(self) -> int:
return len(self.index)
def __getitem__(
self,
offset_offsets_or_slice: Union[int, np.ndarray, slice],
) -> Union[Key, np.ndarray]:
if isinstance(offset_offsets_or_slice, slice):
start, stop, step = offset_offsets_or_slice.indices(len(self))
if step:
raise
return self.index._compiled.get_keys_in_slice(start, stop - start)
elif isinstance(offset_offsets_or_slice, Iterable):
offsets = np.array(offset_offsets_or_slice)
return self.index._compiled.get_keys_at_offsets(offsets)
else:
offset = int(offset_offsets_or_slice)
return self.index._compiled.get_key_at_offset(offset)
def __array__(self, dtype=None) -> np.ndarray:
if dtype is None:
dtype = Key
return self.index._compiled.get_keys_in_slice().astype(dtype)
class Index:
"""Fast vector-search engine for dense equi-dimensional embeddings.
Vector keys must be integers.
Vectors must have the same number of dimensions within the index.
Supports Inner Product, Cosine Distance, L^n measures like the Euclidean metric,
as well as automatic downcasting to low-precision floating-point and integral
representations.
"""
def __init__(
self,
*,
ndim: int = 0,
metric: MetricLike = MetricKind.Cos,
dtype: Optional[DTypeLike] = None,
connectivity: Optional[int] = None,
expansion_add: Optional[int] = None,
expansion_search: Optional[int] = None,
multi: bool = False,
path: Optional[os.PathLike] = None,
view: bool = False,
enable_key_lookups: bool = True,
) -> None:
"""Construct the index and compiles the functions, if requested (expensive).
:param ndim: Number of vector dimensions
:type ndim: int
Required for some metrics, pre-set for others.
Haversine, for example, only applies to 2-dimensional latitude/longitude
coordinates. Angular (Cos) and Euclidean (L2sq), obviously, apply to
vectors with arbitrary number of dimensions.
:param metric: Distance function
:type metric: MetricLike, defaults to MetricKind.Cos
Kind of the distance function, or the Numba `cfunc` JIT-compiled object.
Possible `MetricKind` values: IP, Cos, L2sq, Haversine, Pearson,
Hamming, Tanimoto, Sorensen.
:param dtype: Scalar type for internal vector storage
:type dtype: Optional[DTypeLike], defaults to None
For continuous metrics can be: f16, f32, f64, or i8.
For bitwise metrics it's implementation-defined, and can't change.
If nothing is provided, the optimal data type is selected based on the metric
kind and hardware support.
Example: you can use the `f16` index with `f32` vectors in Euclidean space,
which will be automatically downcasted. Moreover, if `dtype=None` is passed,
and hardware supports `f16` SIMD-instructions, this choice will be done for you.
You can later double-check the used representation with `index.dtype`.
:param connectivity: Connections per node in HNSW
:type connectivity: Optional[int], defaults to None
Hyper-parameter for the number of Graph connections
per layer of HNSW. The original paper calls it "M".
Optional, but can't be changed after construction.
:param expansion_add: Traversal depth on insertions
:type expansion_add: Optional[int], defaults to None
Hyper-parameter for the search depth when inserting new
vectors. The original paper calls it "efConstruction".
Can be changed afterwards, as the `.expansion_add`.
:param expansion_search: Traversal depth on queries
:type expansion_search: Optional[int], defaults to None
Hyper-parameter for the search depth when querying
nearest neighbors. The original paper calls it "ef".
Can be changed afterwards, as the `.expansion_search`.
:param multi: Allow multiple vectors with the same key
:type multi: bool, defaults to True
:param path: Where to store the index
:type path: Optional[os.PathLike], defaults to None
:param view: Are we simply viewing an immutable index
:type view: bool, defaults to False
"""
if connectivity is None:
connectivity = DEFAULT_CONNECTIVITY
if expansion_add is None:
expansion_add = DEFAULT_EXPANSION_ADD
if expansion_search is None:
expansion_search = DEFAULT_EXPANSION_SEARCH
assert isinstance(connectivity, int), "Expects integer `connectivity`"
assert isinstance(expansion_add, int), "Expects integer `expansion_add`"
assert isinstance(expansion_search, int), "Expects integer `expansion_search`"
metric = _normalize_metric(metric)
if isinstance(metric, MetricKind):
self._metric_kind = metric
self._metric_jit = None
self._metric_pointer = 0
self._metric_signature = MetricSignature.ArrayArraySize
elif isinstance(metric, CompiledMetric):
self._metric_jit = metric
self._metric_kind = metric.kind
self._metric_pointer = metric.pointer
self._metric_signature = metric.signature
else:
raise ValueError(
"The `metric` must be a `CompiledMetric` or a `MetricKind`"
)
# Validate, that the right scalar type is defined
dtype = _normalize_dtype(dtype, ndim, self._metric_kind)
self._compiled = _CompiledIndex(
ndim=ndim,
dtype=dtype,
connectivity=connectivity,
expansion_add=expansion_add,
expansion_search=expansion_search,
multi=multi,
enable_key_lookups=enable_key_lookups,
metric_kind=self._metric_kind,
metric_pointer=self._metric_pointer,
metric_signature=self._metric_signature,
)
self.path = path
if path is not None and os.path.exists(path):
if view:
self.view(path)
else:
self.load(path)
@staticmethod
def metadata(path_or_buffer: PathOrBuffer) -> Optional[dict]:
try:
if isinstance(path_or_buffer, bytearray):
path_or_buffer = bytes(path_or_buffer)
if isinstance(path_or_buffer, bytes):
return _index_dense_metadata_from_buffer(path_or_buffer)
else:
path_or_buffer = os.fspath(path_or_buffer)
if not os.path.exists(path_or_buffer):
return None
return _index_dense_metadata_from_path(path_or_buffer)
except Exception as e:
raise e
@staticmethod
def restore(path_or_buffer: PathOrBuffer, view: bool = False) -> Optional[Index]:
meta = Index.metadata(path_or_buffer)
if not meta:
return None
index = Index(
ndim=meta["dimensions"],
dtype=meta["kind_scalar"],
metric=meta["kind_metric"],
)
if view:
index.view(path_or_buffer)
else:
index.load(path_or_buffer)
return index
def __len__(self) -> int:
return self._compiled.__len__()
def add(
self,
keys: KeyOrKeysLike,
vectors: VectorOrVectorsLike,
*,
copy: bool = True,
threads: int = 0,
log: Union[str, bool] = False,
progress: Optional[ProgressCallback] = None,
) -> Union[int, np.ndarray]:
"""Inserts one or move vectors into the index.
For maximal performance the `keys` and `vectors`
should conform to the Python's "buffer protocol" spec.
To index a single entry:
keys: int, vectors: np.ndarray.
To index many entries:
keys: np.ndarray, vectors: np.ndarray.
When working with extremely large indexes, you may want to
pass `copy=False`, if you can guarantee the lifetime of the
primary vectors store during the process of construction.
:param keys: Unique identifier(s) for passed vectors
:type keys: Optional[KeyOrKeysLike], can be `None`
:param vectors: Vector or a row-major matrix
:type vectors: VectorOrVectorsLike
:param copy: Should the index store a copy of vectors
:type copy: bool, defaults to True
:param threads: Optimal number of cores to use
:type threads: int, defaults to 0
:param log: Whether to print the progress bar
:type log: Union[str, bool], defaults to False
:param progress: Callback to report stats of the progress and control it
:type progress: Optional[ProgressCallback], defaults to None
:return: Inserted key or keys
:type: Union[int, np.ndarray]
"""
return _add_to_compiled(
self._compiled,
keys=keys,
vectors=vectors,
copy=copy,
threads=threads,
log=log,
progress=progress,
)
def search(
self,
vectors: VectorOrVectorsLike,
count: int = 10,
radius: float = math.inf,
*,
threads: int = 0,
exact: bool = False,
log: Union[str, bool] = False,
progress: Optional[ProgressCallback] = None,
) -> Union[Matches, BatchMatches]:
"""
Performs approximate nearest neighbors search for one or more queries.
:param vectors: Query vector or vectors.
:type vectors: VectorOrVectorsLike
:param count: Upper count on the number of matches to find
:type count: int, defaults to 10
:param threads: Optimal number of cores to use
:type threads: int, defaults to 0
:param exact: Perform exhaustive linear-time exact search
:type exact: bool, defaults to False
:param log: Whether to print the progress bar, default to False
:type log: Union[str, bool], optional
:param progress: Callback to report stats of the progress and control it
:type progress: Optional[ProgressCallback], defaults to None
:return: Matches for one or more queries
:rtype: Union[Matches, BatchMatches]
"""
return _search_in_compiled(
self._compiled.search_many,
vectors,
# Batch scheduling:
log=log,
# Search constraints:
count=count,
exact=exact,
threads=threads,
progress=progress,
)
def contains(self, keys: KeyOrKeysLike) -> Union[bool, np.ndarray]:
if isinstance(keys, Iterable):
return self._compiled.contains_many(np.array(keys, dtype=Key))
else:
return self._compiled.contains_one(int(keys))
def __contains__(self, keys: KeyOrKeysLike) -> Union[bool, np.ndarray]:
return self.contains(keys)
def count(self, keys: KeyOrKeysLike) -> Union[int, np.ndarray]:
if isinstance(keys, Iterable):
return self._compiled.count_many(np.array(keys, dtype=Key))
else:
return self._compiled.count_one(int(keys))
def get(
self,
keys: KeyOrKeysLike,
dtype: Optional[DTypeLike] = None,
) -> Union[Optional[np.ndarray], Tuple[Optional[np.ndarray]]]:
"""Looks up one or more keys from the `Index`, retrieving corresponding vectors.
Returns `None`, if one key is requested, and its not present.
Returns a (row) vector, if the key maps into a single vector.
Returns a (row-major) matrix, if the key maps into a multiple vectors.
If multiple keys are requested, composes many such responses into a `tuple`.
:param keys: One or more keys to lookup
:type keys: KeyOrKeysLike
:return: One or more keys lookup results
:rtype: Union[Optional[np.ndarray], Tuple[Optional[np.ndarray]]]
"""
if not dtype:
dtype = self.dtype
else:
dtype = _normalize_dtype(dtype)
view_dtype = _to_numpy_dtype(dtype)
def cast(result):
if result is not None:
return result.view(view_dtype)
return result
is_one = not isinstance(keys, Iterable)
if is_one:
keys = [keys]
if not isinstance(keys, np.ndarray):
keys = np.array(keys, dtype=Key)
else:
keys = keys.astype(Key)
results = self._compiled.get_many(keys, dtype)
results = [cast(result) for result in results]
return results[0] if is_one else results
def __getitem__(
self, keys: KeyOrKeysLike
) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]:
"""Looks up one or more keys from the `Index`, retrieving corresponding vectors.
Returns `None`, if one key is requested, and its not present.
Returns a (row) vector, if the key maps into a single vector.
Returns a (row-major) matrix, if the key maps into a multiple vectors.
If multiple keys are requested, composes many such responses into a `tuple`.
:param keys: One or more keys to lookup
:type keys: KeyOrKeysLike
:return: One or more keys lookup results
:rtype: Union[Optional[np.ndarray], Tuple[Optional[np.ndarray]]]
"""
return self.get(keys)
def remove(
self,
keys: KeyOrKeysLike,
*,
compact: bool = False,
threads: int = 0,
) -> Union[int, np.ndarray]:
"""Removes one or move vectors from the index.
When working with extremely large indexes, you may want to
mark some entries deleted, instead of rebuilding a filtered index.
In other cases, rebuilding - is the recommended approach.
:param keys: Unique identifier for passed vectors, optional
:type keys: KeyOrKeysLike
:param compact: Removes links to removed nodes (expensive), defaults to False
:type compact: bool, optional
:param threads: Optimal number of cores to use, defaults to 0
:type threads: int, optional
:return: Array of integers for the number of removed vectors per key
:type: Union[int, np.ndarray]
"""
if not isinstance(keys, Iterable):
return self._compiled.remove_one(keys, compact=compact, threads=threads)
else:
keys = np.array(keys, dtype=Key)
return self._compiled.remove_many(keys, compact=compact, threads=threads)
def __delitem__(self, keys: KeyOrKeysLike) -> Union[int, np.ndarray]:
raise self.remove(keys)
def rename(
self,
from_: KeyOrKeysLike,
to: KeyOrKeysLike,
) -> Union[int, np.ndarray]:
"""Rename existing member vector or vectors.
May be used in iterative clustering procedures, where one would iteratively
relabel every vector with the name of the cluster an entry belongs to, until
the system converges.
:param from_: One or more keys to be renamed
:type from_: KeyOrKeysLike
:param to: New name or names (of identical length as `from_`)
:type to: KeyOrKeysLike
:return: Number of vectors that were found and renamed
:rtype: int
"""
if isinstance(from_, Iterable):
from_ = np.array(from_, dtype=Key)
if isinstance(to, Iterable):
to = np.array(to, dtype=Key)
return self._compiled.rename_many_to_many(from_, to)
else:
return self._compiled.rename_many_to_one(from_, int(to))
else:
return self._compiled.rename_one_to_one(int(from_), int(to))
@property
def jit(self) -> bool:
"""
:return: True, if the provided `metric` was JIT-ed
:rtype: bool
"""
return self._metric_jit is not None
@property
def hardware_acceleration(self) -> str:
"""Describes the kind of hardware-acceleration support used in
that exact instance of the `Index`, for that metric kind, and
the given number of dimensions.
:return: "auto", if nothing is available, ISA subset name otherwise
:rtype: str
"""
return self._compiled.hardware_acceleration
@property
def size(self) -> int:
return self._compiled.size
@property
def ndim(self) -> int:
return self._compiled.ndim
@property
def serialized_length(self) -> int:
return self._compiled.serialized_length
@property
def metric_kind(self) -> Union[MetricKind, CompiledMetric]:
return self._metric_jit.kind if self._metric_jit else self._metric_kind
@property
def metric(self) -> Union[MetricKind, CompiledMetric]:
return self._metric_jit if self._metric_jit else self._metric_kind
@metric.setter
def metric(self, metric: MetricLike):
metric = _normalize_metric(metric)
if isinstance(metric, MetricKind):
metric_kind = metric
metric_pointer = 0
metric_signature = MetricSignature.ArrayArraySize
elif isinstance(metric, CompiledMetric):
metric_kind = metric.kind
metric_pointer = metric.pointer
metric_signature = metric.signature
else:
raise ValueError(
"The `metric` must be a `CompiledMetric` or a `MetricKind`"
)
return self._compiled.change_metric(
metric_kind=metric_kind,
metric_pointer=metric_pointer,
metric_signature=metric_signature,
)
@property
def dtype(self) -> ScalarKind:
return self._compiled.dtype
@property
def connectivity(self) -> int:
return self._compiled.connectivity
@property
def capacity(self) -> int:
return self._compiled.capacity
@property
def memory_usage(self) -> int:
return self._compiled.memory_usage
@property
def expansion_add(self) -> int:
return self._compiled.expansion_add
@property
def expansion_search(self) -> int:
return self._compiled.expansion_search
@expansion_add.setter
def expansion_add(self, v: int):
self._compiled.expansion_add = v
@expansion_search.setter
def expansion_search(self, v: int):
self._compiled.expansion_search = v
def save(
self,
path_or_buffer: Union[str, os.PathLike, NoneType] = None,
progress: Optional[ProgressCallback] = None,
) -> Optional[bytes]:
assert not progress or _match_signature(
progress, [int, int], bool
), "Invalid callback signature"
path_or_buffer = path_or_buffer if path_or_buffer else self.path
if path_or_buffer is None:
return self._compiled.save_index_to_buffer(progress)
else:
self._compiled.save_index_to_path(os.fspath(path_or_buffer), progress)
def load(
self,
path_or_buffer: Union[str, os.PathLike, bytes, NoneType] = None,
progress: Optional[ProgressCallback] = None,
):
assert not progress or _match_signature(
progress, [int, int], bool
), "Invalid callback signature"
path_or_buffer = path_or_buffer if path_or_buffer else self.path
if path_or_buffer is None:
raise Exception("Define the source")
if isinstance(path_or_buffer, bytearray):
path_or_buffer = bytes(path_or_buffer)
if isinstance(path_or_buffer, bytes):
self._compiled.load_index_from_buffer(path_or_buffer, progress)
else:
path_or_buffer = os.fspath(path_or_buffer)
if os.path.exists(path_or_buffer):
self._compiled.load_index_from_path(path_or_buffer, progress)
else:
raise RuntimeError("Missing file!")
def view(
self,
path_or_buffer: Union[str, os.PathLike, bytes, bytearray, NoneType] = None,
progress: Optional[ProgressCallback] = None,
):
assert not progress or _match_signature(
progress, [int, int], bool
), "Invalid callback signature"
path_or_buffer = path_or_buffer if path_or_buffer else self.path
if path_or_buffer is None:
raise Exception("Define the source")
if isinstance(path_or_buffer, bytearray):
path_or_buffer = bytes(path_or_buffer)
if isinstance(path_or_buffer, bytes):
self._compiled.view_index_from_buffer(path_or_buffer, progress)
else:
self._compiled.view_index_from_path(os.fspath(path_or_buffer), progress)
def clear(self):
"""Erases all the vectors from the index, preserving the space for future insertions."""
self._compiled.clear()
def reset(self):
"""Erases all members from index, closing files, and returning RAM to OS."""
if not hasattr(self, "_compiled"):