Spatial point pattern analysis in Python.
Project description
pySpat
pySpat is a small, native Python library for spatial point pattern analysis. It is also very much a work in progress.
What’s implemented
Windows (study regions)
MaskWindow– binary raster window on a regular gridarea(),contains(xy),sample_uniform(n),erode(r),distance_to_boundary(),overlap_fraction(shift),rescale(factor)
RectangleWindow– axis-aligned rectangle [xmin, xmax) × [ymin, ymax)- Analytic
contains,area,sample_uniform,erode,overlap_fraction,distance_to_boundary(raster image),rescale
- Analytic
PolyWindow– polygon/multipolygon (with holes) via Shapely- Exact
area,contains,erode(buffer),overlap_fraction(intersection),dtb_at_points, rasterdistance_to_boundary,sample_uniform(rejection in bbox),rescale
- Exact
Notes
erode(r)is Minkowski erosion (negative buffer for polygons).distance_to_boundary()returns anImagewith pixel-centre distances inside the window; useful for border corrections.overlap_fraction(shift)gives |W ∩ (W+shift)| / |W|.- Fast FFT path for
MaskWindow(sub-pixel via bilinear interpolation). - Exact path for
RectangleWindow(closed form) andPolyWindow(Shapely intersection).
- Fast FFT path for
Point patterns
PPP– planar point pattern with:- Validation that all points lie inside the window
- Optional
marks bbox(),intensity(),thin(p),rescale(factor),nndist(k)- Simple unit labelling (
unitstring carried through)
Summary statistics
- Ripley’s K:
Kest(ppp, r, correction="translation"|"border") - Besag’s L:
Lest(ppp, r, correction=...) - Pair correlation:
pcf(ppp, r_edges, correction=...)(annular estimator) - Nearest-neighbour distribution:
Gest(ppp, r, method="border"|"raw") - Empty-space function:
Fest(ppp, r, method="border"|"raw", nsamp=..., rng=...) - J-function:
Jest(ppp, r, method_G=..., method_F=..., nsamp_F=..., rng=...)
Edge correction details
- Translation: weights are
1 / overlap_fraction(Δ).MaskWindow: fast via FFT precomputation + bilinear interpolation.RectangleWindow/PolyWindow: exact overlap per query (memoised).
- Border: anchors (or sample locations) require distance-to-boundary ≥ r; distances are sampled from the window’s
distance_to_boundary()image.
Optional Plots
- Boolean
showplotargument for each summary statistic,Falseby default. SettingTruegives a basic matplotlib figure of the function against radius, with the function-specific CSR baseline also plotted.
Dependencies
- Required:
numpy,scipy - Optional:
shapely(>=2.0 recommended)
Install
Not always up to date on PyPI. Install from source:
git clone https://github.com/j-peyton/pySpat
pip install ./pySpat
# or editable:
pip install -e ./pySpat
Quick start
import numpy as np
from pyspat.core import RectangleWindow, PPP
from pyspat.stats import Kest, Lest, pcf
# Window: 200 x 200 square
W = RectangleWindow(0.0, 0.0, 200.0, 200.0)
# CSR sample
rng = np.random.default_rng(7)
xy = W.sample_uniform(n=3000, rng=rng)
X = PPP(xy, W, unit="µm")
# K and L
r = np.linspace(2.0, 40.0, 25)
K = Kest(X, r, correction="translation")
L = Lest(X, r, correction="translation")
# Pair correlation
edges = np.linspace(2.0, 40.0, 60)
r_mid, g = pcf(X, edges, correction="translation")
Licence
MIT
Author
Jack Peyton, November 2025
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyspat-0.1.8.tar.gz
(36.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
pyspat-0.1.8-py3-none-any.whl
(23.5 kB
view details)
File details
Details for the file pyspat-0.1.8.tar.gz.
File metadata
- Download URL: pyspat-0.1.8.tar.gz
- Upload date:
- Size: 36.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee0b7d3c4b81de22b36a377e6cea91ad8e6a51f094e89bed90574c65822f87bd
|
|
| MD5 |
1128da15a3f2af1ad30d2f9f4723f881
|
|
| BLAKE2b-256 |
3096732c7f009524f9c50825757befb170b98ac7a21f0e81fcf3aac058443594
|
File details
Details for the file pyspat-0.1.8-py3-none-any.whl.
File metadata
- Download URL: pyspat-0.1.8-py3-none-any.whl
- Upload date:
- Size: 23.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42fcb34880a4f5da80dab56bd3de546dd229329dc62e78e6020493804bcc796f
|
|
| MD5 |
d7ffd2eaa9d1d18d580ce7d9a00373f9
|
|
| BLAKE2b-256 |
78588037c7242701b81b772456522399744a994922343e6dc62bd2579638ed93
|