yt.utilities.lib.cykdtree.kdtree module

class yt.utilities.lib.cykdtree.kdtree.PyKDTree

Bases: object

Construct a KDTree for a set of points.

Parameters:
  • pts (np.ndarray of double) – (n,m) array of n coordinates in a m-dimensional domain.

  • left_edge (np.ndarray of double) – (m,) domain minimum in each dimension.

  • right_edge (np.ndarray of double) – (m,) domain maximum in each dimension.

  • periodic (bool or np.ndarray of bool, optional) – Truth of the domain periodicity overall (if bool), or in each dimension (if np.ndarray). Defaults to False.

  • leafsize (int, optional) – The maximum number of points that should be in a leaf. Defaults to 10000.

  • nleaves (int, optional) – The number of leaves that should be in the resulting tree. If greater than 0, leafsize is adjusted to produce a tree with 2**(ceil(log2(nleaves))) leaves. The leafsize keyword argument is ignored if nleaves is greater zero. Defaults to 0.

  • data_version (int, optional) – An optional user-provided integer that can be used to uniquely identify the data used to generate the KDTree. This is useful if you save the kdtree to disk and restore it later and need to verify that the underlying data is the same.

  • use_sliding_midpoint (bool, optional) – If True, the sliding midpoint rule is used to perform splits. Otherwise, the median is used. Defaults to False.

Raises:

ValueError – If leafsize < 2. This currently segfaults.

npts

Number of points in the tree.

Type:

uint64

ndim

Number of dimensions points occupy.

Type:

uint32

data_version

User-provided version number (defaults to 0)

Type:

int64

num_leaves

Number of leaves in the tree.

Type:

uint32

leafsize

Maximum number of points a leaf can have.

Type:

uint32

leaves

Tree leaves.

Type:

list of cykdtree.PyNode

idx

Indices sorting the points by leaf.

Type:

np.ndarray of uint64

left_edge

(m,) domain minimum in each dimension.

Type:

np.ndarray of double

right_edge

(m,) domain maximum in each dimension.

Type:

np.ndarray of double

domain_width

(m,) domain width in each dimension.

Type:

np.ndarray of double

periodic

Truth of domain periodicity in each dimension.

Type:

np.ndarray of bool

assert_equal(solf, strict_idx=True)

Compare this tree to another tree.

Parameters:
  • solf (PyKDTree) – Another KDTree to compare with this one.

  • strict_idx (bool, optional) – If True, the index vectors are compared for equality element by element. If False, corresponding leaves must contain the same indices, but they can be in any order. Defaults to True.

Raises:

AssertionError – If there are mismatches between any of the two trees’ parameters.

consolidate_edges()

Return arrays of the left and right edges for all leaves in the tree on each process.

Returns:

The left (first

array) and right (second array) edges of each leaf (1st array dimension), in each dimension (2nd array dimension).

Return type:

tuple(np.ndarray of double, np.ndarray of double)

data_version
domain_width
classmethod from_file(filename, data_version=None)

Create a PyKDTree from a binary file created by PyKDTree.save()

Note that loading a file created on another machine may create a corrupted PyKDTree instance.

Parameters:
  • filename (string) – Name of the file to load the kdtree from

  • data_version (int) – A unique integer corresponding to the data being loaded. If the loaded data_version does not match the data_version supplied here then an OSError is raised. Optional.

Returns:

A KDTree restored from the file

Return type:

cykdtree.PyKDTree

get(pos)

Return the leaf containing a given position.

Parameters:

pos (np.ndarray of double) – Coordinates.

Returns:

Leaf containing pos.

Return type:

cykdtree.PyNode

Raises:

ValueError – If pos is not contained within the KDTree.

get_neighbor_ids(pos)

Return the IDs of leaves containing & neighboring a given position.

Parameters:

pos (np.ndarray of double) – Coordinates.

Returns:

Leaves containing/neighboring pos.

Return type:

np.ndarray of uint32

Raises:

ValueError – If pos is not contained within the KDTree.

idx
leaf_idx(leafid)

Get array of indices for points in a leaf.

Parameters:

leafid (np.uint32_t) – Unique index of the leaf in question.

Returns:

Indices of points belonging to leaf.

Return type:

np.ndarray of np.uint64_t

leafsize
leaves
left_edge
ndim
npts
num_leaves
periodic
right_edge
save(filename)

Saves the PyKDTree to disk as raw binary data.

Note that this file may not necessarily be portable.

Parameters:

filename (string) – Name of the file to serialize the kdtree to

class yt.utilities.lib.cykdtree.kdtree.PyNode

Bases: object

A container for leaf info.

npts

Number of points in this node.

Type:

np.uint64_t

ndim

Number of dimensions in domain.

Type:

np.uint32_t

num_leaves

Number of leaves in the tree containing this node.

Type:

np.uint32_t

start_idx

Index where indices for this node begin.

Type:

np.uint64_t

stop_idx

One passed the end of indices for this node.

Type:

np.uint64_t

left_edge

Minimum bounds of this node in each dimension.

Type:

np.ndarray of float64

right_edge

Maximum bounds of this node in each dimension.

Type:

np.ndarray of float64

periodic_left

Periodicity of minimum bounds.

Type:

np.ndarray of bool

periodic_right

Periodicity of maximum bounds.

Type:

np.ndarray of bool

domain_width

Width of the total domain in each dimension.

Type:

np.ndarray of float64

left_neighbors

Indices of neighbor leaves at the minimum bounds in each dimension.

Type:

list of lists

right_neighbors

Indices of neighbor leaves at the maximum bounds in each dimension.

Type:

list of lists

assert_equal(solf)

Assert that node properties are equal.

domain_width
id
left_edge
left_neighbors
ndim
neighbors

Indices of all neighboring leaves including this leaf.

Type:

list of int

npts
num_leaves
periodic_left
periodic_right
right_edge
right_neighbors
slice

Slice of kdtree indices contained by this node.

Type:

slice

start_idx
stop_idx