Skip to main content

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 grid
    • area(), 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
  • PolyWindow – polygon/multipolygon (with holes) via Shapely
    • Exact area, contains, erode (buffer), overlap_fraction (intersection), dtb_at_points, raster distance_to_boundary, sample_uniform (rejection in bbox), rescale

Notes

  • erode(r) is Minkowski erosion (negative buffer for polygons).
  • distance_to_boundary() returns an Image with 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) and PolyWindow (Shapely intersection).

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 (unit string 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 showplot argument for each summary statistic, False by default. Setting True gives 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


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.7.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyspat-0.1.7-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file pyspat-0.1.7.tar.gz.

File metadata

  • Download URL: pyspat-0.1.7.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for pyspat-0.1.7.tar.gz
Algorithm Hash digest
SHA256 169b2f1d5d25abb8b53313d9bb8a94ff1b6177cd6a467ffe9a713e7c0d891d40
MD5 2168b4b0685cdc18d7ca14690e390aa8
BLAKE2b-256 bfd485cac83ca227ea16586a117afc6533c8fa298625a0f8bbedc8ffcccb5950

See more details on using hashes here.

File details

Details for the file pyspat-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: pyspat-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for pyspat-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 2681e5b654596e17d649e081742c8ff3c0f5332d12085a3d2ad4cb97b2dd648a
MD5 ea076e8de1f340cbbd8f99954b63893b
BLAKE2b-256 c55f7db0dc2d400affc0dfe02287bd0f49b4d3a29c9f88fa3b543fda13388415

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page