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.

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.6.tar.gz (23.9 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.6-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyspat-0.1.6.tar.gz
  • Upload date:
  • Size: 23.9 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.6.tar.gz
Algorithm Hash digest
SHA256 af41b526770cf9f5065ae27a2a1a8e44b426019692057a841d4afc1fb9ce3bfe
MD5 7940333c2ab6ecd0aca054565515e748
BLAKE2b-256 825aa6810003c68c05c6f237f557924a11d521457f15605d5b17207cdc5fec8c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyspat-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 16.3 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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e09eb4315e1bd0f4b0a4757428c2d238f8c34db1726a02d89db39629788a813a
MD5 236f4c53d8503b95611ced70571729d3
BLAKE2b-256 141cc89fc94a7452548be12be616920109fabe46afdddb94a8178137033c5b5d

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