instant-distance/instant-distance-py/test/test.py

41 lines
1.1 KiB
Python
Raw Normal View History

2021-03-17 16:18:24 +00:00
import instant_distance, random
2021-05-18 21:08:19 +00:00
def test_hsnw():
2021-03-17 16:18:24 +00:00
points = [[random.random() for _ in range(300)] for _ in range(1024)]
config = instant_distance.Config()
(hnsw, ids) = instant_distance.Hnsw.build(points, config)
p = [random.random() for _ in range(300)]
search = instant_distance.Search()
hnsw.search(p, search)
2021-03-24 14:52:20 +00:00
for candidate in search:
print(candidate)
2021-03-17 16:18:24 +00:00
2021-05-18 21:08:19 +00:00
def test_hsnw_map():
the_chosen_one = 123
embeddings = [[random.random() for _ in range(300)] for _ in range(1024)]
with open("/usr/share/dict/words", "r") as f: # *nix only
values = f.read().splitlines()[1024:]
config = instant_distance.Config()
hnsw_map = instant_distance.HnswMap.build(embeddings, values, config)
search = instant_distance.Search()
hnsw_map.search(embeddings[the_chosen_one], search)
first = next(search)
2021-05-18 21:08:19 +00:00
approx_nearest = first.value
2021-05-18 21:08:19 +00:00
actual_word = values[the_chosen_one]
print("approx word:\t", approx_nearest)
print("actual word:\t", actual_word)
assert approx_nearest == actual_word
if __name__ == "__main__":
test_hsnw()
test_hsnw_map()