Distances and validation¶
This page documents 12 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
euclidean_distance¶
Compute pairwise Euclidean distances. Compute Euclidean distances between coordinate arrays.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import euclidean_distance |
| Signature | euclidean_distance(coords1: 'np.ndarray', coords2: 'np.ndarray') -> 'np.ndarray' |
| Maintained example | examples/core/02_distances_and_validation.py |
euclidean_distance ¶
Compute pairwise Euclidean distances. Compute Euclidean distances between coordinate arrays.
Integer inputs are converted to float before arithmetic to prevent overflow. Inputs are converted to floating point before squaring to avoid integer overflow.
Source code in src/pygwrx/core/utils.py
manhattan_distance¶
Compute pairwise Manhattan (L1/city-block) distances.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import manhattan_distance |
| Signature | manhattan_distance(coords1: 'np.ndarray', coords2: 'np.ndarray') -> 'np.ndarray' |
| Maintained example | examples/core/02_distances_and_validation.py |
manhattan_distance ¶
Compute pairwise Manhattan (L1/city-block) distances.
Source code in src/pygwrx/core/utils.py
chebyshev_distance¶
Compute pairwise Chebyshev (L-infinity) distances.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import chebyshev_distance |
| Signature | chebyshev_distance(coords1: 'np.ndarray', coords2: 'np.ndarray') -> 'np.ndarray' |
| Maintained example | examples/core/02_distances_and_validation.py |
chebyshev_distance ¶
Compute pairwise Chebyshev (L-infinity) distances.
Source code in src/pygwrx/core/utils.py
minkowski_distance¶
Compute pairwise Minkowski (Lp) distances. Compute Minkowski distances between coordinate arrays.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import minkowski_distance |
| Signature | minkowski_distance(coords1: 'np.ndarray', coords2: 'np.ndarray', p: 'float' = 2.0) -> 'np.ndarray' |
| Maintained example | examples/core/02_distances_and_validation.py |
minkowski_distance ¶
Compute pairwise Minkowski (Lp) distances. Compute Minkowski distances between coordinate arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
Norm order. It must be finite and >= 1, or positive infinity. |
2.0
|
Source code in src/pygwrx/core/utils.py
haversine_distance¶
Compute great-circle distances using the Haversine formula.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import haversine_distance |
| Signature | haversine_distance(coords1: 'np.ndarray', coords2: 'np.ndarray', radius: 'float' = 6371.0) -> 'np.ndarray' |
| Maintained example | examples/core/02_distances_and_validation.py |
haversine_distance ¶
Compute great-circle distances using the Haversine formula.
Coordinates must be ordered as [longitude, latitude] in degrees.
The output unit is the same as the unit used for radius.
Source code in src/pygwrx/core/utils.py
compute_distance_matrix¶
Compute a pairwise distance matrix.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_distance_matrix |
| Signature | compute_distance_matrix(coords1: 'np.ndarray', coords2: 'Optional[np.ndarray]' = None, metric: 'str' = 'euclidean', **kwargs) -> 'np.ndarray' |
| Maintained example | examples/core/02_distances_and_validation.py |
compute_distance_matrix ¶
compute_distance_matrix(
coords1: ndarray,
coords2: Optional[ndarray] = None,
metric: str = "euclidean",
**kwargs
) -> np.ndarray
Compute a pairwise distance matrix.
Supported metrics are euclidean, manhattan, chebyshev,
minkowski, and haversine.
Source code in src/pygwrx/core/utils.py
DistanceCache¶
Distance-matrix cache policy based on actual matrix memory. Decide whether a distance matrix is small enough to cache.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.core import DistanceCache |
| Signature | DistanceCache() |
| Maintained example | examples/core/02_distances_and_validation.py |
DistanceCache ¶
Distance-matrix cache policy based on actual matrix memory. Decide whether a distance matrix is small enough to cache.
The class retains the original public name for compatibility. It is a policy/advisor; it does not itself store matrices.
should_cache
staticmethod
¶
Return whether the required distance matrix fits the default cache budget.
Source code in src/pygwrx/core/utils.py
estimate_memory
staticmethod
¶
Estimate memory occupied by a float64 distance matrix.
Source code in src/pygwrx/core/utils.py
get_strategy
staticmethod
¶
Return 'cache' or 'on-the-fly'.
print_recommendation
staticmethod
¶
Print a detailed distance-matrix caching recommendation.
Source code in src/pygwrx/core/utils.py
validate_coords¶
Validate coordinate data and return a floating-point array of shape (n, 2).
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import validate_coords |
| Signature | validate_coords(coords: "Union[np.ndarray, pd.DataFrame, 'gpd.GeoDataFrame']") -> 'np.ndarray' |
| Maintained example | examples/core/02_distances_and_validation.py |
validate_coords ¶
Validate coordinate data and return a floating-point array of shape (n, 2).
Source code in src/pygwrx/core/utils.py
validate_data¶
Validate a single-response feature matrix and target vector.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import validate_data |
| Signature | validate_data(X: 'Union[np.ndarray, pd.DataFrame]', y: 'Union[np.ndarray, pd.Series]') -> 'Tuple[np.ndarray, np.ndarray]' |
| Maintained example | examples/core/02_distances_and_validation.py |
validate_data ¶
validate_data(
X: Union[ndarray, DataFrame], y: Union[ndarray, Series]
) -> Tuple[np.ndarray, np.ndarray]
Validate a single-response feature matrix and target vector.
Source code in src/pygwrx/core/utils.py
add_intercept¶
Add a leading intercept column of ones to a feature matrix.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import add_intercept |
| Signature | add_intercept(X: 'np.ndarray') -> 'np.ndarray' |
| Maintained example | examples/core/02_distances_and_validation.py |
add_intercept ¶
Add a leading intercept column of ones to a feature matrix.
Source code in src/pygwrx/core/utils.py
extract_geopandas_coords¶
Extract [x, y] coordinates from the active Point geometry column.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import extract_geopandas_coords |
| Signature | extract_geopandas_coords(gdf: "'gpd.GeoDataFrame'") -> 'np.ndarray' |
| Maintained example | examples/core/03_geopandas_coordinates.py |
extract_geopandas_coords ¶
Extract [x, y] coordinates from the active Point geometry column.
Source code in src/pygwrx/core/utils.py
chunked_computation¶
Yield half-open (start, end) index ranges for chunked processing.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import chunked_computation |
| Signature | chunked_computation(n_items: 'int', chunk_size: 'int' = 1000) -> 'Iterator[Tuple[int, int]]' |
| Maintained example | examples/core/02_distances_and_validation.py |
chunked_computation ¶
Yield half-open (start, end) index ranges for chunked processing.
Source code in src/pygwrx/core/utils.py
Runnable examples used on this page¶
examples/core/02_distances_and_validation.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Use all public distance, validation, caching, and chunk helpers."""
# Allow this script to run directly from any working directory.
import sys
from pathlib import Path
_PROJECT_ROOT = Path(__file__).resolve().parents[2]
_EXAMPLES_ROOT = _PROJECT_ROOT / "examples"
_SRC_ROOT = _PROJECT_ROOT / "src"
for _path in (_SRC_ROOT, _EXAMPLES_ROOT):
if str(_path) not in sys.path:
sys.path.insert(0, str(_path))
import numpy as np
import pandas as pd
from pygwrx.core import (
DistanceCache,
add_intercept,
chebyshev_distance,
chunked_computation,
compute_distance_matrix,
euclidean_distance,
haversine_distance,
manhattan_distance,
minkowski_distance,
validate_coords,
validate_data,
)
a = np.array([[0.0, 0.0], [1.0, 2.0]])
b = np.array([[2.0, 1.0], [3.0, 4.0]])
print("euclidean=", euclidean_distance(a, b))
print("manhattan=", manhattan_distance(a, b))
print("chebyshev=", chebyshev_distance(a, b))
print("minkowski_p3=", minkowski_distance(a, b, p=3.0))
print(
"haversine_km=",
haversine_distance(np.array([[116.4, 39.9]]), np.array([[121.5, 31.2]])),
)
print("matrix=", compute_distance_matrix(a, metric="euclidean"))
X, y = validate_data(pd.DataFrame({"x": [1, 2]}), pd.Series([3, 4]))
coords = validate_coords(pd.DataFrame(a, columns=["x", "y"]))
print("validated_shapes=", X.shape, y.shape, coords.shape)
print("with_intercept=", add_intercept(X))
print("chunks=", list(chunked_computation(10, chunk_size=4)))
print("cache_memory=", DistanceCache.estimate_memory(100, 50))
print("cache_strategy=", DistanceCache.get_strategy(100, 50, task="gwr"))
print("should_cache=", DistanceCache.should_cache(100, 50))
DistanceCache.print_recommendation(100, 50)
examples/core/03_geopandas_coordinates.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Extract coordinates from a GeoDataFrame using the base installation."""
# Allow this script to run directly from any working directory.
import sys
from pathlib import Path
_PROJECT_ROOT = Path(__file__).resolve().parents[2]
_EXAMPLES_ROOT = _PROJECT_ROOT / "examples"
_SRC_ROOT = _PROJECT_ROOT / "src"
for _path in (_SRC_ROOT, _EXAMPLES_ROOT):
if str(_path) not in sys.path:
sys.path.insert(0, str(_path))
import geopandas as gpd
from shapely.geometry import Point
from pygwrx.core import extract_geopandas_coords
gdf = gpd.GeoDataFrame(
{"name": ["a", "b"]},
geometry=[Point(0.0, 1.0), Point(2.0, 3.0)],
crs="EPSG:3857",
)
print(extract_geopandas_coords(gdf))