scipy.spatial — Spatial Algorithms¶
Provides spatial distance functions and spatial data-structure predicates from
scipy.spatial as importable clausal predicates.
Import¶
or via the canonical py.* path:
Tiers¶
| Tier | Predicates | Notes |
|---|---|---|
| 1 — pure | CrossDistance, PairwiseDistance, SquareForm, PointDistance |
Array in, array/scalar out |
| 3 — handle | MakeKdTree, KdTreeQuery, KdTreeQueryBall, KdTreeQueryPairs |
KD-tree |
| 3 — handle | MakeConvexHull, ConvexHullAttr |
Convex hull |
| 3 — handle | MakeDelaunay, DelaunayFindSimplex |
Delaunay triangulation |
| 3 — handle | MakeRotation, RotationApply, RotationAs, RotationCompose, RotationInverse |
3-D rotation |
| lifecycle | Free |
Release any handle |
Tier 1 — Distance Functions¶
CrossDistance¶
# skip
CrossDistance(XA, XB, RESULT)
CrossDistance(XA, XB, METRIC, RESULT)
CrossDistance(XA, XB, METRIC, KWARGS, RESULT)
Compute the distance between each pair of rows from XA and XB.
Wraps scipy.spatial.distance.cdist.
XA,XB: arrays of shape(m, d)and(n, d)METRIC: distance metric string (default'minkowski')KWARGS: Python dict of extra metric-specific keyword arguments, orNoneRESULT:(m × n)distance matrix
# skip
CrossDistance(++([[0.0, 0.0]]), ++([[3.0, 4.0]]), 'euclidean', D),
V is ++(float(D[0, 0])) % V = 5.0
PairwiseDistance¶
# skip
PairwiseDistance(X, RESULT)
PairwiseDistance(X, METRIC, RESULT)
PairwiseDistance(X, METRIC, KWARGS, RESULT)
Compute pairwise distances between all rows within a single array X.
Wraps scipy.spatial.distance.pdist.
X: array of shape(n, d)RESULT: condensed distance vector of lengthn*(n-1)/2
SquareForm¶
Convert between a condensed distance vector and a square distance matrix.
Wraps scipy.spatial.distance.squareform.
# skip
PairwiseDistance(PTS, CONDENSED),
SquareForm(CONDENSED, SQUARE) % SQUARE is the full n×n matrix
PointDistance¶
Compute the scalar distance between two individual points using the named metric.
METRIC: e.g.'euclidean','cityblock','cosine','mahalanobis'X,Y: 1-D arrays or listsRESULT: float scalar
Tier 3 — KD-Tree¶
MakeKdTree¶
Build a KD-tree for fast nearest-neighbour queries.
Wraps scipy.spatial.KDTree.
DATA:(n, d)array of pointsLEAFSIZE: leaf-size threshold (default 10)RESULT: integer handle
KdTreeQuery¶
Query the KD-tree for the K nearest neighbours of each point in X.
X: query point(s) —(d,)for a single point,(m, d)for multipleK: number of neighbours (default 1)RESULT: dict with keys'distances'and'indices'
KdTreeQueryBall¶
Find all points within RADIUS of each query point.
RESULT: list of index lists (or a flat list ifXis a single point)
KdTreeQueryPairs¶
Find all pairs of points in the tree within RADIUS of each other.
RESULT: set of(i, j)index pairs
Tier 3 — ConvexHull¶
MakeConvexHull¶
Compute the convex hull of a set of points.
Wraps scipy.spatial.ConvexHull.
POINTS:(n, d)arrayRESULT: integer handle
ConvexHullAttr¶
Retrieve an attribute of the ConvexHull object.
ATTR |
Type | Description |
|---|---|---|
'vertices' |
int array | Indices of hull vertices |
'simplices' |
int array | Facet index sets |
'equations' |
float array | Hyperplane equations (normals + offsets) |
'area' |
float | Surface area (perimeter in 2-D) |
'volume' |
float | Volume (area in 2-D) |
'neighbors' |
int array | Neighbour facet indices |
'coplanar' |
int array | Coplanar points not on hull |
Tier 3 — Delaunay Triangulation¶
MakeDelaunay¶
Compute the Delaunay triangulation.
Wraps scipy.spatial.Delaunay.
RESULT: integer handle
DelaunayFindSimplex¶
Find the simplex containing each point in XI.
XI:(m, d)query pointsBRUTEFORCE: ifTrue, bypass spatial index (defaultFalse)RESULT: int array;-1for points outside the triangulation
# skip
MakeDelaunay(PTS, H),
DelaunayFindSimplex(H, ++([[0.5, 0.3]]), IDX),
++(int(IDX[0]) >= 0) % point is inside
Tier 3 — Rotation¶
Wraps scipy.spatial.transform.Rotation.
MakeRotation¶
Construct a rotation from a given representation.
METHOD |
DATA |
|---|---|
'quat' |
quaternion array [x, y, z, w] |
'matrix' |
3×3 rotation matrix |
'rotvec' |
rotation vector (axis × angle) |
'mrp' |
Modified Rodrigues Parameters |
'euler' |
tuple (seq, angles) e.g. ('xyz', [0.0, 0.0, 1.57]) |
RESULT: integer handle
RotationApply¶
Apply the rotation to an array of 3-D vectors.
INVERSE: ifTrue, apply the inverse rotationRESULT: rotated vectors array
RotationAs¶
Export the rotation to a different representation.
FORM:'quat','matrix','rotvec','mrp', or'euler'SEQ: required whenFORM='euler'(e.g.'xyz')
RotationCompose¶
Compose two rotations: HANDLE_B is applied first, then HANDLE_A.
Returns a new handle.
RotationInverse¶
Return the inverse of the rotation as a new handle.
Lifecycle — Free¶
Release the object registered under HANDLE. Always succeeds, even if the
handle is unknown or already freed.
Complete Example¶
# skip
-import_from(scipy_spatial, [CrossDistance, MakeKdTree, KdTreeQuery,
MakeRotation, RotationApply, RotationAs, Free])
% Find the two nearest neighbours of each point
nearest_two(POINTS, INDICES) <- (
MakeKdTree(POINTS, KD),
KdTreeQuery(KD, POINTS, 2, R),
INDICES is ++(R['indices'][:, 1]),
Free(KD))
% Rotate a batch of vectors by 90 degrees around Z
rotate_z90(VECTORS, ROTATED) <- (
MakeRotation('rotvec', ++([0.0, 0.0, 1.5707963267948966]), ROT),
RotationApply(ROT, VECTORS, ROTATED),
Free(ROT))