### Navigation

- index
- modules |
- next |
- previous |
- MDAnalysis 0.7.7 documentation »
- 8. KDTree module »

# 8.1. KDTree — `MDAnalysis.KDTree.KDTree`

Author: | Thomas Hamelryck, Oliver Beckstein
Year: | 2002, 2008
License: | BSD

The KD tree data structure can be used for all kinds of searches that involve N-dimensional vectors, e.g. neighbor searches (find all points within a radius of a given point) or finding all point pairs in a set that are within a certain radius of each other. See “Computational Geometry: Algorithms and Applications” (Mark de Berg, Marc van Kreveld, Mark Overmars, Otfried Schwarzkopf) [deBerg2000].

class `MDAnalysis.KDTree.KDTree.``KDTree`(dim, bucket_size=10)

  KD tree implementation (C++, SWIG python wrapper)

  The KD tree data structure can be used for all kinds of searches that involve N-dimensional vectors, e.g. neighbor searches (find all points within a radius of a given point) or finding all point pairs in a set that are within a certain radius of each other.

  Reference:

  Computational Geometry: Algorithms and Applications Second Edition Mark de Berg, Marc van Kreveld, Mark Overmars, Otfried Schwarzkopf published by Springer-Verlag 2nd rev. ed. 2000. ISBN: 3-540-65620-0

  The KD tree data structure is described in chapter 5, pg. 99 of [deBerg2000].

  The following article [Bentley1990] made clear to me that the nodes should contain more than one point (this leads to dramatic speed improvements for the “all fixed radius neighbor search”, see below):

  JL Bentley, “Kd trees for semidynamic point sets,” in Sixth Annual ACM Symposium on Computational Geometry, vol. 91. San Francisco, 1990

  This KD implementation also performs a “all fixed radius neighbor search”, i.e. it can find all point pairs in a set that are within a certain radius of each other. As far as I know the algorithm has not been published.

  Set up a KDTree for <dim> dimensions and <bucket_size> points per node.

  kdt = KDTree(<dim>,bucket_size=<n>)

  For “all fixed radius neighbor search” as typically used in MDAnalysis, use a value such as bucket_size=10; for the classical KD-tree use 1.

  `all_get_indices`()

    Return All Fixed Neighbor Search results.

    Return a Nx2 dim Numeric array containing the indices of the point pairs, where N is the number of neighbor pairs.

  `all_get_radii`()

    Return All Fixed Neighbor Search results.

    Return an N-dim array containing the distances of all the point pairs, where N is the number of neighbor pairs.

  `all_search`(radius)

    All fixed neighbor search.

    Search all point pairs that are within radius.

    o radius - float (>0)

  `get_indices`()

    Return the list of indices.

    Return the list of indices after a neighbor search. The indices refer to the original coords numpy array. The coordinates with these indices were within radius of center.

    For an index pair, the first index<second index.

  `get_radii`()

    Return radii.

    Return the list of distances from center after a neighbor search.

  `list_search`(centers, radius)

    Search all points within radius of any center (radii NOT available).

  `search`(center, radius)

    Search all points within radius of center.

    o center - one dimensional numpy array. E.g. if the points have dimensionality D, the center array should have length D. o radius - float>0

    center is always cast to numpy.float32

  `set_coords`(coords)

    Add the coordinates of the points.

    o coords - two dimensional numpy array. E.g. if the points have dimensionality D and there are N points, the coords array should be NxD dimensional.

    The coords array is always cast to a numpy.float32 array.

### Navigation

- index
- modules |
- next |
- previous |
- MDAnalysis 0.7.7 documentation »
- 8. KDTree module »