def test_nn_xy(self): nnset = NNSet() for x_i in range(10): nnset.add([x_i, x_i], [x_i * 2]) for x_i in range(10): dists, idxs = nnset.nn_y([2 * x_i + 0.1], k=1) nn_y = list(nnset.ys[idxs[0]]) self.assertEqual(nn_y, [2 * x_i])
def test_nn_x(self): nnset = NNSet() for x_i in range(10): nnset.add([x_i, x_i]) for x_i in range(10): dists, idxs = nnset.nn_x([x_i, x_i + 0.5], k=1) nn_x = list(nnset.xs[idxs[0]]) self.assertEqual(nn_x, [x_i, x_i])
def test_nn_xy(self): nnset = NNSet() for x_i in range(10): nnset.add([x_i, x_i], [x_i*2]) for x_i in range(10): dists, idxs = nnset.nn_y([2*x_i+0.1], k=1) nn_y = list(nnset.ys[idxs[0]]) self.assertEqual(nn_y, [2*x_i])
def test_nn_x(self): nnset = NNSet() for x_i in range(10): nnset.add([x_i, x_i]) for x_i in range(10): dists, idxs = nnset.nn_x([x_i, x_i+0.5], k=1) nn_x = list(nnset.xs[idxs[0]]) self.assertEqual(nn_x, [x_i, x_i])
def test_incrementalnn(self): for _ in range(10): implementations = [ BruteForceNNSet(), BatchNNSet(), NNSet(poolsize=7) ] n = random.randint(2, 5) m = random.randint(2, 5) for j in range(100): x, y = [random.random() for _ in range(n) ], [random.random() for _ in range(m)] for imp in implementations: imp.add(x, y) for _ in range(10): k = min(random.randint(1, 10), j + 1) x, y = [random.random() for _ in range(n) ], [random.random() for _ in range(m)] indexes = [] for imp in implementations: dists, idxes = imp.nn_x(x, k=k) indexes.append(idxes) self.assertEqual(*indexes)
K = 1 DYN_FREQ = 1 DYN_QUERIES = 1 END_QUERIES = 50 def testrun(nnset): random.seed(0) for i in range(N): x = [random.random() for _ in range(DIM)] nnset.add(x, [random.random()]) if i % DYN_FREQ == 0: for i in range(DYN_QUERIES): x = [random.random() for _ in range(DIM)] nnset.nn_x(x, k=1) for i in range(END_QUERIES): x = [random.random() for _ in range(DIM)] nnset.nn_x(x, k=1) if __name__ == '__main__': print('Running benchmark... this may take a few minutes.') for nnset in [NNSet(), BatchNNSet(), BruteForceNNSet()]: start = time.time() testrun(nnset) print('{}: {:4.2f}s'.format(nnset.__class__.__name__, time.time() - start))