示例#1
0
def time_tree_construction(Ntime, LStime, ndim=2):
    pts, le, re, ls = make_points(Ntime, ndim, leafsize=LStime)
    t0 = time.time()
    cykdtree.PyKDTree(pts, le, re, leafsize=LStime)
    t1 = time.time()
    print("{} {}D points, leafsize {}: took {} s".format(
        Ntime, ndim, LStime, t1 - t0))
示例#2
0
def test_neighbors(periodic=False):
    pts, le, re, ls, left_neighbors, right_neighbors = make_points_neighbors(
        periodic=periodic)
    tree = cykdtree.PyKDTree(pts, le, re, leafsize=ls, periodic=periodic)
    for leaf in tree.leaves:
        out_str = str(leaf.id)
        try:
            for d in range(tree.ndim):
                out_str += '\nleft:  {} {} {}'.format(
                    d, leaf.left_neighbors[d], left_neighbors[d][leaf.id])
                assert (len(left_neighbors[d][leaf.id]) == len(
                    leaf.left_neighbors[d]))
                for i in range(len(leaf.left_neighbors[d])):
                    assert (left_neighbors[d][leaf.id][i] ==
                            leaf.left_neighbors[d][i])
                out_str += '\nright: {} {} {}'.format(
                    d, leaf.right_neighbors[d], right_neighbors[d][leaf.id])
                assert (len(right_neighbors[d][leaf.id]) == len(
                    leaf.right_neighbors[d]))
                for i in range(len(leaf.right_neighbors[d])):
                    assert (right_neighbors[d][leaf.id][i] ==
                            leaf.right_neighbors[d][i])
        except:
            for leaf in tree.leaves:
                print(leaf.id, leaf.left_edge, leaf.right_edge)
            print(out_str)
            raise
示例#3
0
def time_neighbor_search(Ntime, LStime, ndim=2):
    pts, le, re, ls = make_points(Ntime, ndim, leafsize=LStime)
    tree = cykdtree.PyKDTree(pts, le, re, leafsize=LStime)
    t0 = time.time()
    tree.get_neighbor_ids(0.5*np.ones(tree.ndim, 'double'))
    t1 = time.time()
    print("{} {}D points, leafsize {}: took {} s".format(Ntime, ndim, LStime, t1-t0))
示例#4
0
def test_get_neighbor_ids(npts=100, ndim=2, periodic=False):
    pts, le, re, ls = make_points(npts, ndim)
    tree = cykdtree.PyKDTree(pts, le, re, leafsize=ls, periodic=periodic)
    pos_list = [le, (le + re) / 2.]
    if periodic:
        pos_list.append(re)
    for pos in pos_list:
        tree.get_neighbor_ids(pos)
示例#5
0
def test_save_load():
    for periodic in (True, False):
        for ndim in range(1, 5):
            pts, le, re, ls = make_points(100, ndim)
            tree = cykdtree.PyKDTree(pts, le, re, leafsize=ls,
                                     periodic=periodic, data_version=ndim+12)
            with tempfile.NamedTemporaryFile() as tf:
                tree.save(tf.name)
                restore_tree = cykdtree.PyKDTree.from_file(tf.name)
                tree.assert_equal(restore_tree)
示例#6
0
def test_PyKDTree(npts=100,
                  ndim=2,
                  periodic=False,
                  use_sliding_midpoint=False):
    pts, le, re, ls = make_points(npts, ndim)
    cykdtree.PyKDTree(pts,
                      le,
                      re,
                      leafsize=ls,
                      periodic=periodic,
                      use_sliding_midpoint=use_sliding_midpoint)
示例#7
0
def test_consolidate_edges(periodic=False, ndim=2):
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()
    pts, le, re, ls = make_points(20, ndim, leafsize=3)
    Tpara = cykdtree.PyParallelKDTree(pts, le, re, leafsize=ls,
                                      periodic=periodic)
    LEpara, REpara = Tpara.consolidate_edges()
    if rank == 0:
        Tseri = cykdtree.PyKDTree(pts, le, re, leafsize=ls,
                                  periodic=periodic)
        LEseri, REseri = Tseri.consolidate_edges()
    else:
        LEseri, REseri = None, None
    LEseri, REseri = comm.bcast((LEseri, REseri), root=0)
    np.testing.assert_allclose(LEpara, LEseri)
    np.testing.assert_allclose(REpara, REseri)
示例#8
0
def test_consolidate(periodic=False, ndim=2):
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()
    pts, le, re, ls = make_points(20, ndim, leafsize=3)
    Tpara0 = cykdtree.PyParallelKDTree(pts, le, re, leafsize=ls,
                                       periodic=periodic)
    Tpara = Tpara0.consolidate()
    if rank == 0:
        if False:
            from cykdtree.plot import plot2D_serial
            plot2D_serial(Tpara, label_boxes=True,
                          plotfile='test_consolidate.png')
        Tseri = cykdtree.PyKDTree(pts, le, re, leafsize=ls,
                                  periodic=periodic)
        Tpara.assert_equal(Tseri, strict_idx=False)
    else:
        assert(Tpara is None)
示例#9
0
def tree(method, pts, left_edge, right_edge, periodic, *args, **kwargs):
    r"""Get tree for a given domain decomposition schema.

    Args:
        method (str): Domain decomposition method. Supported options are:
            'kdtree': KDTree based on median position along the dimension
                with the greatest domain width. See
                :meth:`cgal4py.domain_decomp.kdtree` for details on
                accepted keyword arguments.
        pts (np.ndarray of float64): (n, m) array of n coordinates in a
            m-dimensional domain.
        left_edge (np.ndarray of float64): (m,) domain minimum in each
            dimension.
        right_edge (np.ndarray of float64): (m,) domain maximum in each
            dimension.
        *args: Variable argument list. Passed to the selected domain
            decomposition method.
        **kwargs: Variable keyword argument list. Passed to the selected
            domain decomposition method.

    Returns:
        object: Tree of type specified by `method`.

    Raises:
        ValueError: If `method` is not one of the supported domain
            decomposition methods listed above.

    """
    # Get leaves
    if method.lower() == 'kdtree':
        tree = kdtree.PyKDTree(pts, left_edge, right_edge, *args, **kwargs)
    else:
        raise ValueError("'{}' is not a supported ".format(method) +
                         "domain decomposition.")
    # Return tree
    return tree
示例#10
0
def test_search_errors(npts=100, ndim=2):
    pts, le, re, ls = make_points(npts, ndim)
    tree = cykdtree.PyKDTree(pts, le, re, leafsize=ls)
    assert_raises(ValueError, tree.get, re)