forked from patvarilly/periodic_kdtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_periodic_kdtree.py
372 lines (310 loc) · 11.9 KB
/
test_periodic_kdtree.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
# test_periodic_kdtree.py
#
# Unit tests for periodic_kdtree.py
#
# Written by Patrick Varilly, 6 Jul 2012
# Released under the scipy license
import numpy as np
from periodic_kdtree import PeriodicKDTree, PeriodicCKDTree
from numpy.testing import assert_equal, assert_array_equal, \
assert_array_almost_equal, assert_almost_equal, \
assert_, run_module_suite
from scipy.spatial import minkowski_distance
class ConsistencyTests:
def distance(self, x, y, p):
return minkowski_distance(np.zeros(x.shape), self.pbcs(x - y), p)
def pbcs(self, x):
return x - np.where(self.bounds > 0,
(np.round(x / self.bounds) * self.bounds), 0.0)
def test_nearest(self):
x = self.x
d, i = self.kdtree.query(x, 1)
assert_almost_equal(d**2,np.sum(self.pbcs(x-self.data[i])**2))
eps = 1e-8
assert_(np.all(np.sum(self.pbcs(self.data-x[np.newaxis,:])**2,axis=1)>d**2-eps))
def test_m_nearest(self):
x = self.x
m = self.m
dd, ii = self.kdtree.query(x, m)
d = np.amax(dd)
i = ii[np.argmax(dd)]
assert_almost_equal(d**2,np.sum(self.pbcs(x-self.data[i])**2))
eps = 1e-8
assert_equal(np.sum(np.sum(self.pbcs(self.data-x[np.newaxis,:])**2,axis=1)<d**2+eps),m)
def test_points_near(self):
x = self.x
d = self.d
dd, ii = self.kdtree.query(x, k=self.kdtree.n, distance_upper_bound=d)
eps = 1e-8
hits = 0
for near_d, near_i in zip(dd,ii):
if near_d==np.inf:
continue
hits += 1
assert_almost_equal(near_d**2,np.sum(self.pbcs(x-self.data[near_i])**2))
assert_(near_d<d+eps, "near_d=%g should be less than %g" % (near_d,d))
assert_equal(np.sum(np.sum(self.pbcs(self.data-x[np.newaxis,:])**2,axis=1)<d**2+eps),hits)
def test_points_near_l1(self):
x = self.x
d = self.d
dd, ii = self.kdtree.query(x, k=self.kdtree.n, p=1, distance_upper_bound=d)
eps = 1e-8
hits = 0
for near_d, near_i in zip(dd,ii):
if near_d==np.inf:
continue
hits += 1
assert_almost_equal(near_d,self.distance(x,self.data[near_i],1))
assert_(near_d<d+eps, "near_d=%g should be less than %g" % (near_d,d))
assert_equal(np.sum(self.distance(self.data,x,1)<d+eps),hits)
def test_points_near_linf(self):
x = self.x
d = self.d
dd, ii = self.kdtree.query(x, k=self.kdtree.n, p=np.inf, distance_upper_bound=d)
eps = 1e-8
hits = 0
for near_d, near_i in zip(dd,ii):
if near_d==np.inf:
continue
hits += 1
assert_almost_equal(near_d,self.distance(x,self.data[near_i],np.inf))
assert_(near_d<d+eps, "near_d=%g should be less than %g" % (near_d,d))
assert_equal(np.sum(self.distance(self.data,x,np.inf)<d+eps),hits)
def test_approx(self):
x = self.x
k = self.k
eps = 0.1
d_real, i_real = self.kdtree.query(x, k)
d, i = self.kdtree.query(x, k, eps=eps)
assert_(np.all(d<=d_real*(1+eps)))
class test_random(ConsistencyTests):
def setUp(self):
self.n = 100
self.m = 4
self.data = np.random.randn(self.n, self.m)
self.bounds = np.ones(self.m)
self.kdtree = PeriodicKDTree(self.bounds, self.data,leafsize=2)
self.x = np.random.randn(self.m)
self.d = 0.2
self.k = 10
class test_random_far(test_random):
def setUp(self):
test_random.setUp(self)
self.x = np.random.randn(self.m)+10
class test_small(ConsistencyTests):
def setUp(self):
self.data = np.array([[0,0,0],
[0,0,1],
[0,1,0],
[0,1,1],
[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1]])
self.bounds = 1.1 * np.ones(3)
self.kdtree = PeriodicKDTree(self.bounds, self.data)
self.n = self.kdtree.n
self.m = self.kdtree.m
self.x = np.random.randn(3)
self.d = 0.5
self.k = 4
def test_nearest(self):
assert_array_equal(
self.kdtree.query((0,0,0.1), 1),
(0.1,0))
def test_nearest_two(self):
assert_array_almost_equal(
self.kdtree.query((0,0,0.1), 2),
([0.1,np.sqrt(0.1**2 + 0.1**2)],[0,2]))
class test_small_nonleaf(test_small):
def setUp(self):
test_small.setUp(self)
self.kdtree = PeriodicKDTree(self.bounds, self.data,leafsize=1)
class test_small_compiled(test_small):
def setUp(self):
test_small.setUp(self)
self.kdtree = PeriodicCKDTree(self.bounds, self.data)
class test_small_nonleaf_compiled(test_small):
def setUp(self):
test_small.setUp(self)
self.kdtree = PeriodicCKDTree(self.bounds, self.data,leafsize=1)
class test_random_compiled(test_random):
def setUp(self):
test_random.setUp(self)
self.kdtree = PeriodicCKDTree(self.bounds, self.data)
class test_random_far_compiled(test_random_far):
def setUp(self):
test_random_far.setUp(self)
self.kdtree = PeriodicCKDTree(self.bounds, self.data)
class test_vectorization:
def setUp(self):
self.data = np.array([[0,0,0],
[0,0,1],
[0,1,0],
[0,1,1],
[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1]])
self.bounds = 1.1 * np.ones(3)
self.kdtree = PeriodicKDTree(self.bounds, self.data)
def test_single_query(self):
d, i = self.kdtree.query(np.array([0,0,0]))
assert_(isinstance(d,float))
assert_(np.issubdtype(i, int))
def test_vectorized_query(self):
d, i = self.kdtree.query(np.zeros((2,4,3)))
assert_equal(np.shape(d),(2,4))
assert_equal(np.shape(i),(2,4))
def test_single_query_multiple_neighbors(self):
s = 23
kk = 27*self.kdtree.n+s
d, i = self.kdtree.query(np.array([0,0,0]),k=kk)
assert_equal(np.shape(d),(kk,))
assert_equal(np.shape(i),(kk,))
assert_(np.all(~np.isfinite(d[-s:])))
assert_(np.all(i[-s:]==self.kdtree.n))
def test_vectorized_query_multiple_neighbors(self):
s = 23
kk = 27*self.kdtree.n+s
d, i = self.kdtree.query(np.zeros((2,4,3)),k=kk)
assert_equal(np.shape(d),(2,4,kk))
assert_equal(np.shape(i),(2,4,kk))
assert_(np.all(~np.isfinite(d[:,:,-s:])))
assert_(np.all(i[:,:,-s:]==self.kdtree.n))
def test_single_query_all_neighbors(self):
d, i = self.kdtree.query([0,0,0],k=None,distance_upper_bound=1.1)
assert_(isinstance(d,list))
assert_(isinstance(i,list))
def test_vectorized_query_all_neighbors(self):
d, i = self.kdtree.query(np.zeros((2,4,3)),k=None,distance_upper_bound=1.1)
assert_equal(np.shape(d),(2,4))
assert_equal(np.shape(i),(2,4))
assert_(isinstance(d[0,0],list))
assert_(isinstance(i[0,0],list))
class test_vectorization_compiled:
def setUp(self):
self.data = np.array([[0,0,0],
[0,0,1],
[0,1,0],
[0,1,1],
[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1]])
self.bounds = 1.1 * np.ones(3)
self.kdtree = PeriodicCKDTree(self.bounds, self.data)
def test_single_query(self):
d, i = self.kdtree.query([0,0,0])
assert_(isinstance(d,float))
assert_(isinstance(i,int))
def test_vectorized_query(self):
d, i = self.kdtree.query(np.zeros((2,4,3)))
assert_equal(np.shape(d),(2,4))
assert_equal(np.shape(i),(2,4))
def test_vectorized_query_noncontiguous_values(self):
qs = np.random.randn(3,1000).T
ds, i_s = self.kdtree.query(qs)
for q, d, i in zip(qs,ds,i_s):
assert_equal(self.kdtree.query(q),(d,i))
def test_single_query_multiple_neighbors(self):
s = 23
kk = 27*self.kdtree.n+s
d, i = self.kdtree.query([0,0,0],k=kk)
assert_equal(np.shape(d),(kk,))
assert_equal(np.shape(i),(kk,))
assert_(np.all(~np.isfinite(d[-s:])))
assert_(np.all(i[-s:]==self.kdtree.n))
def test_vectorized_query_multiple_neighbors(self):
s = 23
kk = 27*self.kdtree.n+s
d, i = self.kdtree.query(np.zeros((2,4,3)),k=kk)
assert_equal(np.shape(d),(2,4,kk))
assert_equal(np.shape(i),(2,4,kk))
assert_(np.all(~np.isfinite(d[:,:,-s:])))
assert_(np.all(i[:,:,-s:]==self.kdtree.n))
class ball_consistency:
def distance(self, x, y, p):
return minkowski_distance(np.zeros(x.shape), self.pbcs(x - y), p)
def pbcs(self, x):
return x - np.where(self.bounds > 0,
(np.round(x / self.bounds) * self.bounds), 0.0)
def test_in_ball(self):
l = self.T.query_ball_point(self.x, self.d, p=self.p, eps=self.eps)
for i in l:
assert_(self.distance(self.data[i],self.x,self.p)<=self.d*(1.+self.eps))
def test_found_all(self):
c = np.ones(self.T.n,dtype=np.bool)
l = self.T.query_ball_point(self.x, self.d, p=self.p, eps=self.eps)
c[l] = False
assert_(np.all(self.distance(self.data[c],self.x,self.p)>=self.d/(1.+self.eps)))
class test_random_ball(ball_consistency):
def setUp(self):
n = 100
m = 4
self.data = np.random.randn(n,m)
self.bounds = np.ones(m)
self.T = PeriodicKDTree(self.bounds, self.data,leafsize=2)
self.x = np.random.randn(m)
self.p = 2.
self.eps = 0
self.d = 0.2
class test_random_ball_compiled(ball_consistency):
def setUp(self):
n = 100
m = 4
self.data = np.random.randn(n,m)
self.bounds = np.ones(m)
self.T = PeriodicCKDTree(self.bounds, self.data,leafsize=2)
self.x = np.random.randn(m)
self.p = 2.
self.eps = 0
self.d = 0.2
class test_random_ball_approx(test_random_ball):
def setUp(self):
test_random_ball.setUp(self)
self.eps = 0.1
class test_random_ball_approx_compiled(test_random_ball_compiled):
def setUp(self):
test_random_ball_compiled.setUp(self)
self.eps = 0.1
class test_random_ball_far(test_random_ball):
def setUp(self):
test_random_ball.setUp(self)
self.d = 2.
class test_random_ball_far_compiled(test_random_ball_compiled):
def setUp(self):
test_random_ball_compiled.setUp(self)
self.d = 2.
class test_random_ball_l1(test_random_ball):
def setUp(self):
test_random_ball.setUp(self)
self.p = 1
class test_random_ball_l1_compiled(test_random_ball_compiled):
def setUp(self):
test_random_ball_compiled.setUp(self)
self.p = 1
class test_random_ball_linf(test_random_ball):
def setUp(self):
test_random_ball.setUp(self)
self.p = np.inf
class test_random_ball_linf_compiled(test_random_ball_compiled):
def setUp(self):
test_random_ball_compiled.setUp(self)
self.p = np.inf
def test_random_ball_vectorized():
n = 20
m = 5
bounds = np.ones(m)
T = PeriodicKDTree(bounds, np.random.randn(n,m))
r = T.query_ball_point(np.random.randn(2,3,m),1)
assert_equal(r.shape,(2,3))
assert_(isinstance(r[0,0],list))
def test_random_ball_vectorized_compiled():
n = 20
m = 5
bounds = np.ones(m)
T = PeriodicCKDTree(bounds, np.random.randn(n,m))
r = T.query_ball_point(np.random.randn(2,3,m),1)
assert_equal(r.shape,(2,3))
assert_(isinstance(r[0,0],list))