forked from flann-lib/flann
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_nn_index.py
More file actions
executable file
·81 lines (54 loc) · 1.83 KB
/
Copy pathtest_nn_index.py
File metadata and controls
executable file
·81 lines (54 loc) · 1.83 KB
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
#!/usr/bin/env python
from pyflann import *
from copy import copy
from numpy import *
from numpy.random import *
import unittest
class Test_PyFLANN_nn(unittest.TestCase):
def setUp(self):
self.nn = FLANN()
class Test_PyFLANN_nn_index(unittest.TestCase):
def testnn_index(self):
dim = 10
N = 100
x = rand(N, dim)
nn = FLANN()
nn.build_index(x)
nnidx, nndist = nn.nn_index(x)
correct = all(nnidx == arange(N, dtype = index_type))
nn.delete_index()
self.assertTrue(correct)
def testnn_index_random_permute(self):
numtests = 500
dim = 10
N = 100
nns = [None]*numtests
x = [rand(N, dim) for i in range(numtests)]
correct = ones(numtests, dtype=bool_)
for i in permutation(numtests):
nns[i] = FLANN()
nns[i].build_index(x[i])
# For kicks
if rand() < 0.5:
nns[i].kmeans(x[i], 5)
if rand() < 0.5:
nns[i].nn(x[i], x[i])
for i in permutation(numtests):
nnidx,nndist = nns[i].nn_index(x[i])
correct[i] = all(nnidx == arange(N, dtype = index_type))
for i in reversed(range(numtests)):
if rand() < 0.5:
nns[i].delete_index()
else:
del nns[i]
self.assertTrue(all(correct))
def testnn_index_bad_index_call_noindex(self):
nn = FLANN()
self.assertRaises(FLANNException, lambda: nn.nn_index(rand(5,5)))
def testnn_index_bad_index_call_delindex(self):
nn = FLANN()
nn.build_index(rand(5,5))
nn.delete_index()
self.assertRaises(FLANNException, lambda: nn.nn_index(rand(5,5)))
if __name__ == '__main__':
unittest.main()