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
|
from __future__ import division, print_function, absolute_import
import sys
from numpy.testing import *
from scipy.spatial import cKDTree, KDTree
import numpy as np
class TestBuild(TestCase):
def bench_build(self):
print()
print(' Constructing kd-tree')
print('=====================================')
print(' dim | # points | KDTree | cKDTree ')
for (m, n, repeat) in [(3,10000,3), (8,10000,3), (16,10000,3)]:
print('%4s | %7s ' % (m, n), end=' ')
sys.stdout.flush()
data = np.concatenate((np.random.randn(n//2,m),
np.random.randn(n-n//2,m)+np.ones(m)))
print('| %6.3fs ' % (measure('T1 = KDTree(data)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('| %6.3fs' % (measure('T2 = cKDTree(data)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('')
class TestQuery(TestCase):
def bench_query(self):
print()
print(' Querying kd-tree')
print('===============================================================')
print(' dim | # points | # queries | KDTree | cKDTree | flat cKDTree')
for (m, n, r, repeat) in [(3,10000,1000,3),
(8,10000,1000,3),
(16,10000,1000,3)]:
print('%4s | %8s | %8s ' % (m, n, r), end=' ')
sys.stdout.flush()
data = np.concatenate((np.random.randn(n//2,m),
np.random.randn(n-n//2,m)+np.ones(m)))
queries = np.concatenate((np.random.randn(r//2,m),
np.random.randn(r-r//2,m)+np.ones(m)))
T1 = KDTree(data)
T2 = cKDTree(data)
T3 = cKDTree(data,leafsize=n)
print('| %6.3fs ' % (measure('T1.query(queries)', 1) / 1), end=' ')
sys.stdout.flush()
print('| %6.3fs' % (measure('T2.query(queries)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('| %6.3fs' % (measure('T3.query(queries)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('')
class TestQueryBallPoint(TestCase):
def bench_query_ball_point(self):
print()
print(' Query ball point kd-tree')
print('===============================================================')
print(' dim | # points | # queries | probe radius | KDTree | cKDTree | flat cKDTree')
for (m, n, r, repeat) in [(3,10000,1000,3)]: # ,
# (8,10000,1000,3),
# (16,10000,1000,3)]:
for probe_radius in (0.2, 0.5):
print('%4s | %8s | %9s | %11.1f ' % (m, n, r, probe_radius), end=' ')
sys.stdout.flush()
data = np.concatenate((np.random.randn(n//2,m),
np.random.randn(n-n//2,m)+np.ones(m)))
queries = np.concatenate((np.random.randn(r//2,m),
np.random.randn(r-r//2,m)+np.ones(m)))
T1 = KDTree(data)
T2 = cKDTree(data)
T3 = cKDTree(data,leafsize=n)
print('| %6.3fs ' % (measure('T1.query_ball_point(queries, probe_radius)', 1) / 1), end=' ')
sys.stdout.flush()
print('| %6.3fs' % (measure('T2.query_ball_point(queries, probe_radius)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('| %6.3fs' % (measure('T3.query_ball_point(queries, probe_radius)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('')
class TestQueryPairs(TestCase):
def bench_query_pairs(self):
print()
print(' Query pairs kd-tree')
print('==================================================================')
print(' dim | # points | probe radius | KDTree | cKDTree | flat cKDTree')
for (m, n, repeat) in [(3,1000,30),
(8,1000,30),
(16,1000,30)]:
for probe_radius in (0.2, 0.5):
print('%4s | %8s | %11.1f ' % (m, n, probe_radius), end=' ')
sys.stdout.flush()
data = np.concatenate((np.random.randn(n//2,m),
np.random.randn(n-n//2,m)+np.ones(m)))
T1 = KDTree(data)
T2 = cKDTree(data)
T3 = cKDTree(data,leafsize=n)
print('| %6.3fs ' % (measure('T1.query_pairs(probe_radius)', 1) / 1), end=' ')
sys.stdout.flush()
print('| %6.3fs' % (measure('T2.query_pairs(probe_radius)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('| %6.3fs' % (measure('T3.query_pairs(probe_radius)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('')
class TestSparseDistanceMatrix(TestCase):
def bench_sparse_distance_matrix(self):
print()
print(' Sparse distance matrix kd-tree')
print('====================================================================')
print(' dim | # points T1 | # points T2 | probe radius | KDTree | cKDTree')
for (m, n1, n2, repeat) in [(3,1000,1000,30),
(8,1000,1000,30),
(16,1000,1000,30)]:
data1 = np.concatenate((np.random.randn(n1//2,m),
np.random.randn(n1-n1//2,m)+np.ones(m)))
data2 = np.concatenate((np.random.randn(n2//2,m),
np.random.randn(n2-n2//2,m)+np.ones(m)))
T1 = KDTree(data1)
T2 = KDTree(data2)
cT1 = cKDTree(data1)
cT2 = cKDTree(data2)
for probe_radius in (0.2, 0.5):
print('%4s | %11s | %11s | %11.1f ' % (m, n1, n2, probe_radius), end=' ')
sys.stdout.flush()
print('| %6.3fs ' % (measure('T1.sparse_distance_matrix(T2, probe_radius)', 1) / 1), end=' ')
sys.stdout.flush()
print('| %6.3fs ' % (measure('cT1.sparse_distance_matrix(cT2, probe_radius)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('')
class TestCountNeighbors(TestCase):
def bench_count_neighbors(self):
print()
print(' Count neighbors kd-tree')
print('====================================================================')
print(' dim | # points T1 | # points T2 | probe radius | KDTree | cKDTree')
for (m, n1, n2, repeat) in [(3,1000,1000,30),
(8,1000,1000,30),
(16,1000,1000,30)]:
data1 = np.concatenate((np.random.randn(n1//2,m),
np.random.randn(n1-n1//2,m)+np.ones(m)))
data2 = np.concatenate((np.random.randn(n2//2,m),
np.random.randn(n2-n2//2,m)+np.ones(m)))
T1 = KDTree(data1)
T2 = KDTree(data2)
cT1 = cKDTree(data1)
cT2 = cKDTree(data2)
for probe_radius in (0.2, 0.5):
print('%4s | %11s | %11s | %11.1f ' % (m, n1, n2, probe_radius), end=' ')
sys.stdout.flush()
print('| %6.3fs ' % (measure('T1.count_neighbors(T2, probe_radius)', 1) / 1), end=' ')
sys.stdout.flush()
print('| %6.3fs ' % (measure('cT1.count_neighbors(cT2, probe_radius)', repeat) / repeat), end=' ')
sys.stdout.flush()
print('')
if __name__ == "__main__":
run_module_suite()
|