Skip to main content

pyGWRx

A research-oriented Python library for geographically weighted regression, local spatial statistics, spatiotemporal modelling, diagnostics, and visualization.

License: MIT Python Status Models Public API examples Examples

English · 简体中文 · Documentation · Model Handbook · Examples · API


What pyGWRx is

pyGWRx is a Python implementation and research platform for geographically weighted modelling. It provides a common numerical foundation for classic GWR, multiscale and robust extensions, generalized responses, spatiotemporal neighbourhoods, local regularization, multivariate methods, scalable approximations, similarity-based weighting, and original research models.

The library is designed around five layers:

  1. Models — 19 supported public model classes.
  2. Core numerics — kernels, distances, local solvers, bandwidth selection, optimization, metrics, validation, and base classes.
  3. Diagnostics — model summaries, residuals, influence, parameter inference, local collinearity, time, weight, and regime diagnostics.
  4. Visualization — 56 model-aware and array-based Matplotlib functions.
  5. I/O and examples — NumPy, pandas, GeoPandas, and Shapely data contracts plus 45 isolated runnable scripts.

pyGWRx follows a consistent fit → inspect → diagnose → visualize style. It deliberately does not implement the scikit-learn estimator contract, Pipeline, GridSearchCV, clone, or check_estimator.

Why use it

  • One documented model family: classic, multiscale, robust, generalized, temporal, regularized, multivariate, scalable, similarity-based, and research models in one package.
  • Explicit capability boundaries: regression, classification, transformation, local statistics, and inference models are not presented as interchangeable predictors.
  • Complete documentation: every model page includes theory, equations, fitting steps, parameters, outputs, diagnostics, limitations, reporting guidance, figures, and a full runnable example.
  • 174/174 API-to-example coverage: every public symbol is mapped to a maintained script and generated API page.
  • Complete spatial base install: NumPy, SciPy, pandas, Matplotlib, GeoPandas, and Shapely are installed together so mapping and GeoDataFrame workflows work immediately.
  • Research reproducibility: deterministic example data, explicit random seeds, strict documentation builds, and reference-comparison tests where available.

Installation

pyGWRx supports Python 3.11–3.14. During the Alpha phase, install from a source checkout:

git clone https://github.com/hujinghaoabcd/pyGWRx.git
cd pyGWRx
python -m pip install --upgrade pip
python -m pip install -e .

Matplotlib, GeoPandas, and Shapely are included in the normal installation. Add only the remaining optional features you need:

python -m pip install -e ".[ml]"        # GWLasso, GWPCA, GRGWR
python -m pip install -e ".[parquet]"   # PyArrow persistence
python -m pip install -e ".[all]"       # all remaining user-facing extras
python -m pip install -e ".[test]"      # tests
python -m pip install -e ".[dev]"       # development/build tooling
python -m pip install -e ".[docs]"      # MkDocs documentation toolchain
python -m pip install -e ".[reference]" # optional numerical-reference tests

The base installation includes Matplotlib, GeoPandas, and Shapely. scikit-learn and PyArrow remain optional.

Five-minute GWR example

import numpy as np
import pandas as pd
from pygwrx import GWR

rng = np.random.default_rng(42)
n = 80
coords = pd.DataFrame(rng.uniform(0, 10, size=(n, 2)), columns=["east", "north"])
X = pd.DataFrame(rng.normal(size=(n, 2)), columns=["income", "access"])
local_income = 1.0 + 0.15 * coords["east"].to_numpy()
y = 2.0 + local_income * X["income"] - 0.7 * X["access"]
y += rng.normal(scale=0.35, size=n)

model = GWR(kernel="bisquare", bandwidth=28, adaptive=True)
model.fit(X, y, coords)

print(model.summary())
print(model.to_frame().head())
print("R-squared:", model.score(X, y, coords))

result = model.predict_result(X.iloc[:4], coords.iloc[:4])
print(result.to_frame())

Diagnostics and plotting

from pygwrx.diagnostics import (
    diagnostics_frame,
    local_diagnostic_frame,
    parameter_significance,
)
from pygwrx.plotting import plot_coefficient_map, plot_diagnostic_panel

print(diagnostics_frame([model], labels=["GWR"]))
print(local_diagnostic_frame(model).head())
print(parameter_significance(model, alpha=0.05, correction="fdr_bh").head())

fig, ax = plot_coefficient_map(model, feature="income", theme="paper")
fig.savefig("income_coefficient.png", dpi=200, bbox_inches="tight")

fig, axes = plot_diagnostic_panel(model, theme="paper")
fig.savefig("gwr_diagnostics.png", dpi=200, bbox_inches="tight")

Plotting functions return Matplotlib objects and never call plt.show() automatically.

Model catalogue and capability matrix

Model Purpose Required inputs New-location operation Extra Example
GWR Classic local regression X, y, coords predict / predict_result base code
MGWR Variable-specific spatial scales X, y, coords calibration only base code
RGWR Outlier-resistant local regression X, y, coords predict / predict_result base code
STWR Stage-based spatiotemporal regression stage lists + intervals predict / predict_result base code
GTWR Row-wise space-time regression X, y, coords, times predict / predict_result base code
GWGLM Gaussian, binomial, Poisson local GLM X, y, coords (+ exposure) predict / predict_result base code
GWLasso Locally sparse regression X, y, coords predict ml code
MixedGWR Global + local coefficients X, y, coords + variable sets predict base code
GWPCA Local principal components X, coords transform ml code
GWDA Local discriminant classification X, labels, coords predict / predict_proba base code
GWSS Local descriptive statistics X, coords statistics only base code
ScalableGWR Polynomial-kernel approximation X, y, coords predict / predict_result base code
LCRGWR Local ridge compensation X, y, coords predict / predict_result base code
BootstrapGWR Non-stationarity inference X, y, coords inference only base code
SGWR Geography + attribute similarity X, y, coords + similarity vars predict / predict_result base code
SGTWR Space + time + similarity X, y, coords, times + similarity vars predict / predict_result base code
MGTWR Variable-specific space-time scales X, y, coords, times calibration only base code
LGGWR Learned latent neighbourhood geometry X, y, coords, attributes predict / predict_result base code
GRGWR Connected spatial regimes X, y, coords predict / predict_result ml code

Important boundaries

  • MGWR and MGTWR provide calibration-location results but intentionally reject unvalidated independent-target prediction.
  • GWPCA is a local transformer and uses transform().
  • GWSS computes local descriptive statistics.
  • BootstrapGWR performs coefficient non-stationarity inference rather than response prediction.
  • GWDA is a classifier and provides predict()/predict_proba().
  • MGTWR is implemented entirely inside pyGWRx and has no model-specific runtime dependency.
  • mgwr and spglm are used only in optional GWGLM reference-comparison tests, not during ordinary GWGLM fitting.
  • LGGWR and GRGWR are original research models; report sensitivity, initialization, convergence, and validation scope.

Choosing a model

Scientific need Start with Add only when justified
Continuous response, smooth spatial variation GWR MGWR, RGWR, LCRGWR, ScalableGWR
Binary or count response global GLM + GWGLM family-specific local diagnostics
Space and row-wise time GTWR SGTWR, MGTWR
Snapshot/stage history STWR parameter sensitivity across stages
Global and local effects global regression + MixedGWR theory-supported variable partition
Local variable selection GWLasso stability and resampling analysis
Local multivariate structure GWSS, GWPCA local classification with GWDA
Geography plus functional similarity SGWR SGTWR or research LGGWR
Contiguous spatial mechanisms standard GWR research GRGWR

Read the full model selection guide and 19-model handbook.

Kernels, bandwidths, and distances

Built-in kernels:

from pygwrx.core import (
    gaussian_kernel,
    bisquare_kernel,
    exponential_kernel,
    tricube_kernel,
    boxcar_kernel,
)
  • Fixed bandwidth: a distance in the chosen coordinate metric.
  • Adaptive bandwidth: an integer neighbour count; the corresponding distance changes by focal location.
  • Compact kernels: bisquare, tricube, and boxcar assign exact zero weight outside the local threshold.
  • Continuous kernels: Gaussian and exponential retain positive weights with distance decay.

Report coordinate reference system, distance metric, kernel, fixed/adaptive mode, selection criterion, search range, and final bandwidth. For multiscale models, report every parameter-specific scale.

Public functions and examples

The project contains 174 public API symbols:

Namespace Scope
pygwrx.models models and typed prediction-result objects
pygwrx.core kernels, distances, bandwidths, optimization, solvers, metrics, base classes
pygwrx.diagnostics model, residual, influence, inference, collinearity, temporal, weight, regime diagnostics
pygwrx.plotting coefficient, residual, comparison, temporal, decomposition, latent-geometry, and regime plots
pygwrx.io data conversion, persistence, and dataset registry

Every symbol has:

  • a generated API entry with signature and full docstring;
  • a purpose summary and import path;
  • a link to the maintained example that exercises it;
  • the complete example source embedded in the relevant API page;
  • a row in examples/API_COVERAGE.json and .csv.

Validate the contract:

python tools/generate_api_docs.py
python tools/generate_example_docs.py
python examples/validate_coverage.py

Run examples:

python -m pip install -e ".[all,test]"
python examples/run_all.py

Example inventory:

  • 19 model examples
  • 8 core numerical examples
  • 5 diagnostics examples
  • 6 plotting examples
  • 4 I/O examples
  • 3 end-to-end workflows

Documentation

The MkDocs site includes:

  • Getting Started and data contracts
  • detailed English guides for all 19 models
  • detailed Chinese guides for all 19 models
  • core, diagnostics, plotting, and I/O function manuals
  • full source for all 45 examples
  • a 47-figure visualization gallery
  • generated API pages for all 174 symbols
  • complete algorithm encyclopedia and original-model monographs
  • development, testing, release, citation, and API-stability guidance

Build locally:

python -m pip install -e ".[docs]"
python tools/generate_api_docs.py
python tools/generate_example_docs.py
mkdocs serve
# open http://127.0.0.1:8000

mkdocs build --strict --clean

Validation and project status

Version 0.1.2 is an Alpha research release. The supported public tree is documented and represented by examples, but model capabilities are intentionally heterogeneous and 0.x APIs may evolve.

Current stable-suite baseline:

363 passed

This local Linux/Python 3.13 result includes 360 non-reference tests, including a frozen self-contained MGTWR numerical fixture, and 3 independently maintained GWGLM comparisons from the optional reference extra. The 12-combination operating-system/Python matrix is configured as a blocking GitHub Actions workflow and must not be described as passed until the corresponding remote run succeeds.

Data and legal notice

The development tree contains third-party example datasets. A literature citation, permission for academic analysis, and permission to redistribute the data inside a Python wheel are different legal questions. Verify the original licence and attribution requirements of every dataset before public redistribution.

The MIT licence covers pyGWRx-owned source code; it does not automatically relicense third-party datasets or dependencies.

Project layout

pyGWRx/
├── src/pygwrx/          # formal package
├── tests/               # stable test suite
├── examples/            # 45 runnable examples + coverage manifests
├── docs/                # English and Chinese manuals, API, theory, gallery
├── tools/               # API/documentation generators
├── pyproject.toml       # package metadata and optional extras
├── mkdocs.yml           # documentation layout
├── README.md            # English project overview
└── README.zh.md         # Chinese project overview

Historical prototypes, previous examples, previous documentation, legacy GTWR reporting code, and non-release audit materials are kept in separate archives rather than the formal package tree.

Citation, author, and licence

Real-data five-minute start

from pygwrx import GWR
from pygwrx.io import load_columbus

data = load_columbus(return_type="dict")
model = GWR(kernel="bisquare", bandwidth=24, adaptive=True).fit(
    data["data"], data["target"], data["coords"]
)
print(model.summary())

pyGWRx uses task-specific spatial modelling contracts; it does not promise scikit-learn cloning, pipelines, get_params(), or set_params(). Bundled datasets retain their upstream licenses; see DATA_LICENSES.md.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygwrx-0.1.2.tar.gz (7.4 MB view details)

Uploaded Source

Built Distribution

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

pygwrx-0.1.2-py3-none-any.whl (1.4 MB view details)

Uploaded Python 3

File details

Details for the file pygwrx-0.1.2.tar.gz.

File metadata

  • Download URL: pygwrx-0.1.2.tar.gz
  • Upload date:
  • Size: 7.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pygwrx-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ce972d7d7dfdb38634d730a23fd3d82afeeed8cd6b5ea9168588dbdfcb7552c9
MD5 67dace81cb31ba10cfef81b158f96194
BLAKE2b-256 ad001fcac52eae286561857af25bc0209eaf6da5a334a57f2ed4ed00320f7ba2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygwrx-0.1.2.tar.gz:

Publisher: release.yml on hujinghaoabcd/pyGWRx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pygwrx-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pygwrx-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pygwrx-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a9beb31ea6e6fb27c1263589e0b2258be762ae325cf5fdabce2e6f4e54ebe597
MD5 84a05ff1387cabc3cbaece77c1c129f9
BLAKE2b-256 ff670322805214397abe335a17b6d82500ef034c37e0b4413aea7df5df0d3058

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygwrx-0.1.2-py3-none-any.whl:

Publisher: release.yml on hujinghaoabcd/pyGWRx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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