Exemplo n.º 1
0
def test_unique():
    n = 1024
    #test k-nearest neighbors
    for dim in range(1, 8):
        data = np.random.rand(n, dim)
        data_unique = np.unique(data, axis=1)

        query_data = np.random.rand(dim)
        mkdtree = cKDTree.cKDTree(dim)
        mkdtree.build(data_unique.copy())
        query = mkdtree.query(query_data, 10)

        kdtree = scipyKDTree(data_unique, leafsize=1)

        dist, index = kdtree.query(query_data, 10)
        result = dict(zip(index, dist))
        assert(query == result)

    #test distance smaller than dist
    for dim in range(1, 8):
        data = np.random.rand(n, dim)
        data_unique = np.unique(data, axis=1)

        dist = np.random.rand() / 4

        query_data = np.random.rand(dim)
        mkdtree = cKDTree.cKDTree(dim)
        mkdtree.build(data_unique.copy())
        query = list(mkdtree.query_distance(query_data, dist).keys())

        kdtree = scipyKDTree(data_unique, leafsize=1)

        result = kdtree.query_ball_point(query_data, dist)
        result.sort()
        assert(query == result)

    for dim in range(1, 8):
        data = np.random.rand(n, dim)
        data_unique = np.unique(data, axis=1)

        query_data = np.random.rand(dim)
        mkdtree = cKDTree.cKDTree(dim)
        mkdtree.build(data_unique.copy())
        query = mkdtree.query_nearest(query_data)[0]

        kdtree = scipyKDTree(data_unique, leafsize=1)

        result = kdtree.query(query_data, 1)[1]

        assert(query == result)
def test_sparse_distance_matrix(coordinates, max_dist, p):
    """
    Test that the `sparse_distance_matrix` method returns the same thing in the
    redistributed KDTree and on the actual scipy one.
    """
    original_tree = scipyKDTree(coordinates)
    redis_tree = redisKDTree(coordinates)

    original_output = original_tree.sparse_distance_matrix(original_tree, max_dist, p)
    redis_output = redis_tree.sparse_distance_matrix(redis_tree, max_dist, p)

    assert dict_close(redis_output, original_output)
Exemplo n.º 3
0
    def __init__(self, tree_type, point_list, nclose=0):
        self.tree_type = tree_type
        self.nclose = nclose
        if tree_type not in ['node', 'element']:
            # verifies you're calling the right
            msg = 'Error!  Invalid tree_type\n'
            msg += "tree_type=%r valid='node','element'" % tree_type
            raise RuntimeError(msg)
        npoints = len(point_list)
        nodes = array((npoints, 3), dtype='float64')
        self.node_ids = array(npoints, dtype='int32')

        i = 0
        for node_id, xyz in sorted(iteritems(point_list)):
            nodes[i, :] = xyz
            self.node_ids[i] = node_id
        self.tree = scipyKDTree(point_list)