# Copyright Anne M. Archibald 2008 # Released under the scipy license from numpy.testing import assert_equal, assert_array_equal, assert_almost_equal, \ assert_, run_module_suite import numpy as np from scipy.spatial import KDTree, Rectangle, distance_matrix, cKDTree from scipy.spatial import minkowski_distance as distance class ConsistencyTests: def test_nearest(self): x = self.x d, i = self.kdtree.query(x, 1) assert_almost_equal(d**2,np.sum((x-self.data[i])**2)) eps = 1e-8 assert_(np.all(np.sum((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((x-self.data[i])**2)) eps = 1e-8 assert_equal(np.sum(np.sum((self.data-x[np.newaxis,:])**2,axis=1)=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.T = KDTree(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_far(test_random_ball): def setUp(self): test_random_ball.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_linf(test_random_ball): def setUp(self): test_random_ball.setUp(self) self.p = np.inf def test_random_ball_vectorized(): n = 20 m = 5 T = KDTree(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)) class two_trees_consistency: def test_all_in_ball(self): r = self.T1.query_ball_tree(self.T2, self.d, p=self.p, eps=self.eps) for i, l in enumerate(r): for j in l: assert_(distance(self.data1[i],self.data2[j],self.p)<=self.d*(1.+self.eps)) def test_found_all(self): r = self.T1.query_ball_tree(self.T2, self.d, p=self.p, eps=self.eps) for i, l in enumerate(r): c = np.ones(self.T2.n,dtype=np.bool) c[l] = False assert_(np.all(distance(self.data2[c],self.data1[i],self.p)>=self.d/(1.+self.eps))) class test_two_random_trees(two_trees_consistency): def setUp(self): n = 50 m = 4 self.data1 = np.random.randn(n,m) self.T1 = KDTree(self.data1,leafsize=2) self.data2 = np.random.randn(n,m) self.T2 = KDTree(self.data2,leafsize=2) self.p = 2. self.eps = 0 self.d = 0.2 class test_two_random_trees_far(test_two_random_trees): def setUp(self): test_two_random_trees.setUp(self) self.d = 2 class test_two_random_trees_linf(test_two_random_trees): def setUp(self): test_two_random_trees.setUp(self) self.p = np.inf class test_rectangle: def setUp(self): self.rect = Rectangle([0,0],[1,1]) def test_min_inside(self): assert_almost_equal(self.rect.min_distance_point([0.5,0.5]),0) def test_min_one_side(self): assert_almost_equal(self.rect.min_distance_point([0.5,1.5]),0.5) def test_min_two_sides(self): assert_almost_equal(self.rect.min_distance_point([2,2]),np.sqrt(2)) def test_max_inside(self): assert_almost_equal(self.rect.max_distance_point([0.5,0.5]),1/np.sqrt(2)) def test_max_one_side(self): assert_almost_equal(self.rect.max_distance_point([0.5,1.5]),np.hypot(0.5,1.5)) def test_max_two_sides(self): assert_almost_equal(self.rect.max_distance_point([2,2]),2*np.sqrt(2)) def test_split(self): less, greater = self.rect.split(0,0.1) assert_array_equal(less.maxes,[0.1,1]) assert_array_equal(less.mins,[0,0]) assert_array_equal(greater.maxes,[1,1]) assert_array_equal(greater.mins,[0.1,0]) def test_distance_l2(): assert_almost_equal(distance([0,0],[1,1],2),np.sqrt(2)) def test_distance_l1(): assert_almost_equal(distance([0,0],[1,1],1),2) def test_distance_linf(): assert_almost_equal(distance([0,0],[1,1],np.inf),1) def test_distance_vectorization(): x = np.random.randn(10,1,3) y = np.random.randn(1,7,3) assert_equal(distance(x,y).shape,(10,7)) class test_count_neighbors: def setUp(self): n = 50 m = 2 self.T1 = KDTree(np.random.randn(n,m),leafsize=2) self.T2 = KDTree(np.random.randn(n,m),leafsize=2) def test_one_radius(self): r = 0.2 assert_equal(self.T1.count_neighbors(self.T2, r), np.sum([len(l) for l in self.T1.query_ball_tree(self.T2,r)])) def test_large_radius(self): r = 1000 assert_equal(self.T1.count_neighbors(self.T2, r), np.sum([len(l) for l in self.T1.query_ball_tree(self.T2,r)])) def test_multiple_radius(self): rs = np.exp(np.linspace(np.log(0.01),np.log(10),3)) results = self.T1.count_neighbors(self.T2, rs) assert_(np.all(np.diff(results)>=0)) for r,result in zip(rs, results): assert_equal(self.T1.count_neighbors(self.T2, r), result) class test_sparse_distance_matrix: def setUp(self): n = 50 m = 4 self.T1 = KDTree(np.random.randn(n,m),leafsize=2) self.T2 = KDTree(np.random.randn(n,m),leafsize=2) self.r = 0.3 def test_consistency_with_neighbors(self): M = self.T1.sparse_distance_matrix(self.T2, self.r) r = self.T1.query_ball_tree(self.T2, self.r) for i,l in enumerate(r): for j in l: assert_equal(M[i,j],distance(self.T1.data[i],self.T2.data[j])) for ((i,j),d) in M.items(): assert_(j in r[i]) def test_zero_distance(self): M = self.T1.sparse_distance_matrix(self.T1, self.r) # raises an exception for bug 870 def test_distance_matrix(): m = 10 n = 11 k = 4 xs = np.random.randn(m,k) ys = np.random.randn(n,k) ds = distance_matrix(xs,ys) assert_equal(ds.shape, (m,n)) for i in range(m): for j in range(n): assert_almost_equal(distance(xs[i],ys[j]),ds[i,j]) def test_distance_matrix_looping(): m = 10 n = 11 k = 4 xs = np.random.randn(m,k) ys = np.random.randn(n,k) ds = distance_matrix(xs,ys) dsl = distance_matrix(xs,ys,threshold=1) assert_equal(ds,dsl) def check_onetree_query(T,d): r = T.query_ball_tree(T, d) s = set() for i, l in enumerate(r): for j in l: if i