Beispiel #1
0
    def test_large_k(self):
        x = random_vectors()
        q = random_vectors(1)
        k = x.shape[0] + 1  # make k larger than # of vectors in x

        index = Index(x)
        self.assertTrue(index.is_using_pyarray)
        index.add_points(x.shape[0])

        with self.assertRaises(ValueError):
            index.knn_search(0, k)

        with self.assertRaises(ValueError):
            index.knn_search_points(q, k)
def test_knn_search():
    """
  KNN_SEARCH
  ----------
  GIVEN INDEX, RETURN INDEXES & DISTANCES in ASCENDING ORDER (including itself)

  Parameters
  ----------
  pid: index of target point
  k: number of points to find (WE MUST SET K LESS THAN OR EQUAL TO THE # OF POINTS)
  cores: number of cores to use
  checks:
  eps:
  sorted:

  Returns
  -------
  ids: ids of points found (numpy 2D array)
  dists: distances from target point (numpy 2D array)
  """
    x = random_vectors()
    index = Index(x)
    index.add_points(x.shape[0])

    # pick random integer
    pt = np.random.randint(x.shape[0])  # id. e.g.) 94
    print(x[[pt]])  # data. e.g.) [[0.64, ...]]

    idx, dist = index.knn_search(pt, 5, cores=1)
    print(idx)  # if pt=10, array([[10, 80, 87,  5, 95]])
    print(dist)  # array([[0, 0.76741797, 0.86952025, 0.90387696, 0.9157505 ]])
Beispiel #3
0
    def test_return_shape_64(self):
        x = random_vectors(dtype=np.float64)

        index = Index(x)
        self.assertIs(x, index.array)
        self.assertTrue(index.is_using_pyarray)

        index.add_points(x.shape[0])

        for i in range(x.shape[0]):
            ids, dists = index.knn_search(i, 5)
            self.assertEqual(ids.shape, (1, 5))
            self.assertEqual(dists.shape, (1, 5))