{ } Raw JSON

bundles / scipy 1.18.0 / scipy / spatial / _ckdtree / cKDTree / query_ball_tree

cython_function_or_method

scipy.spatial._ckdtree:cKDTree.query_ball_tree

Signature

def query_ball_tree(self, other, r, p = 2.0, eps = 0.0)

Summary

Find all pairs of points between self and other whose distance is at most r

Parameters

other : cKDTree instance

The tree containing points to search against.

r : float

The maximum distance, has to be positive.

p : float, optional

Which Minkowski norm to use. p has to meet the condition 1 <= p <= infinity. A finite large p may cause a ValueError if overflow can occur.

eps : float, optional

Approximate search. Branches of the tree are not explored if their nearest points are further than r/(1+eps), and branches are added in bulk if their furthest points are nearer than r * (1+eps). eps has to be non-negative.

Returns

results : list of lists

For each element self.data[i] of this tree, results[i] is a list of the indices of its neighbors in other.data.

Examples

You can search all pairs of points between two kd-trees within a distance:
import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import cKDTree
rng = np.random.default_rng()
points1 = rng.random((15, 2))
points2 = rng.random((15, 2))
✓
plt.figure(figsize=(6, 6))
plt.plot(points1[:, 0], points1[:, 1], "xk", markersize=14)
plt.plot(points2[:, 0], points2[:, 1], "og", markersize=14)
✗
[<matplotlib.lines.Line2D object at 0x7fa8e43b9160>]
kd_tree1 = cKDTree(points1)
kd_tree2 = cKDTree(points2)
indexes = kd_tree1.query_ball_tree(kd_tree2, r=0.2)
✓
for i in range(len(indexes)):
    for j in indexes[i]:
        plt.plot([points1[i, 0], points2[j, 0]],
            [points1[i, 1], points2[j, 1]], "-r")
✗
[<matplotlib.lines.Line2D object at 0x7fa8e43baa50>]
[<matplotlib.lines.Line2D object at 0x7fa8e43ba7b0>]
[<matplotlib.lines.Line2D object at 0x7fa8e43ba510>]
[<matplotlib.lines.Line2D object at 0x7fa8e43bb0e0>]
[<matplotlib.lines.Line2D object at 0x7fa8e43ba900>]
[<matplotlib.lines.Line2D object at 0x7fa8e43bb380>]
[<matplotlib.lines.Line2D object at 0x7fa8e43b8050>]
[<matplotlib.lines.Line2D object at 0x7fa8e43ba120>]
[<matplotlib.lines.Line2D object at 0x7fa8e43baf90>]
[<matplotlib.lines.Line2D object at 0x7fa8e43bae40>]
[<matplotlib.lines.Line2D object at 0x7fa8e43b81a0>]
[<matplotlib.lines.Line2D object at 0x7fa8e43b97f0>]
[<matplotlib.lines.Line2D object at 0x7fa8dd8f0830>]
[<matplotlib.lines.Line2D object at 0x7fa8dd8f0d70>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb1e80>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb2120>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb17f0>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb3620>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb3e00>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb2e40>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb3a10>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb3380>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb3770>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb2660>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb1a90>]
[<matplotlib.lines.Line2D object at 0x7fa8dccb1d30>]
[<matplotlib.lines.Line2D object at 0x7fa8f6851400>]
[<matplotlib.lines.Line2D object at 0x7fa8f6852e40>]
[<matplotlib.lines.Line2D object at 0x7fa8f6853380>]
[<matplotlib.lines.Line2D object at 0x7fa8f6851e80>]
[<matplotlib.lines.Line2D object at 0x7fa8f6853770>]
plt.show()
✓
fig-02fa4fbdfe721859.png

Aliases

  • scipy.spatial.cKDTree.query_ball_tree