Metadata-Version: 2.4
Name: pyspat
Version: 0.1.8
Summary: Spatial point pattern analysis in Python.
Author: Jack Peyton
License: MIT
Project-URL: Homepage, https://github.com/j-peyton/pySpat
Project-URL: Issues, https://github.com/j-peyton/pySpat/issues
Keywords: spatial statistics,point-patterns
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENCE
Requires-Dist: numpy>=1.22
Requires-Dist: scipy>=1.11
Provides-Extra: poly
Requires-Dist: shapely>=2.0; extra == "poly"
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: shapely>=2.0; extra == "dev"
Provides-Extra: all
Requires-Dist: shapely>=2.0; extra == "all"
Dynamic: license-file

# 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:

```bash
git clone https://github.com/j-peyton/pySpat
pip install ./pySpat
# or editable:
pip install -e ./pySpat
```

## Quick start

```python
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
