forked from spotify/annoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_test.py
115 lines (98 loc) · 3.56 KB
/
index_test.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
# Copyright (c) 2013 Spotify AB
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import random
from common import TestCase
from annoy import AnnoyIndex
class IndexTest(TestCase):
def test_not_found_tree(self):
i = AnnoyIndex(10)
self.assertRaises(IOError, i.load, 'nonexists.tree')
def test_binary_compatibility(self):
i = AnnoyIndex(10)
i.load('test/test.tree')
# This might change in the future if we change the search algorithm, but in that case let's update the test
self.assertEqual(i.get_nns_by_item(0, 10), [0, 85, 42, 11, 54, 38, 53, 66, 19, 31])
def test_load_unload(self):
# Issue #108
i = AnnoyIndex(10)
for x in range(100000):
i.load('test/test.tree')
i.unload()
def test_construct_load_destruct(self):
for x in range(100000):
i = AnnoyIndex(10)
i.load('test/test.tree')
def test_construct_destruct(self):
for x in range(100000):
i = AnnoyIndex(10)
i.add_item(1000, [random.gauss(0, 1) for z in range(10)])
def test_save_twice(self):
# Issue #100
t = AnnoyIndex(10)
t.save("t.ann")
t.save("t.ann")
def test_load_save(self):
# Issue #61
i = AnnoyIndex(10)
i.load('test/test.tree')
u = i.get_item_vector(99)
i.save('x.tree')
v = i.get_item_vector(99)
self.assertEqual(u, v)
j = AnnoyIndex(10)
j.load('test/test.tree')
w = i.get_item_vector(99)
self.assertEqual(u, w)
def test_save_without_build(self):
# Issue #61
i = AnnoyIndex(10)
i.add_item(1000, [random.gauss(0, 1) for z in range(10)])
i.save('x.tree')
j = AnnoyIndex(10)
j.load('x.tree')
j.build(10)
def test_unbuild_with_loaded_tree(self):
i = AnnoyIndex(10)
i.load('test/test.tree')
i.unbuild()
def test_seed(self):
i = AnnoyIndex(10)
i.load('test/test.tree')
i.set_seed(42)
def test_unknown_distance(self):
self.assertRaises(Exception, AnnoyIndex, 10, 'banana')
def test_metric_kwarg(self):
# Issue 211
i = AnnoyIndex(2, metric='euclidean')
i.add_item(0, [1, 0])
i.add_item(1, [9, 0])
self.assertAlmostEqual(i.get_distance(0, 1), 8)
self.assertEquals(i.f, 2)
def test_metric_f_kwargs(self):
i = AnnoyIndex(f=3, metric='euclidean')
def test_item_vector_after_save(self):
# Issue #279
a = AnnoyIndex(3)
a.verbose(True)
a.add_item(1, [1, 0, 0])
a.add_item(2, [0, 1, 0])
a.add_item(3, [0, 0, 1])
a.build(-1)
self.assertEquals(a.get_n_items(), 4)
self.assertEquals(a.get_item_vector(3), [0, 0, 1])
self.assertEquals(set(a.get_nns_by_item(1, 999)), set([1, 2, 3]))
a.save('something.annoy')
self.assertEquals(a.get_n_items(), 4)
self.assertEquals(a.get_item_vector(3), [0, 0, 1])
self.assertEquals(set(a.get_nns_by_item(1, 999)), set([1, 2, 3]))