apbase.idw
High-level inverse distance weighting API.
This package exports the public class used to run IDW interpolation.
- class apbase.idw.IDW(*, radius=None, power=2.0, max_neighbors=40, min_neighbors=3)
Bases:
objectInterpolate projected coordinates with inverse distance weighting.
Stores finite source samples with coordinates
x/yand valuesz, then estimates values for target coordinates using inverse distance weights. Coordinates are not projected internally; pass metric/projected coordinates whenradiusis in meters.- Parameters:
radius (float | None) – Local search radius. If
None,fitestimates a variogram and usesrange(range / 3with 10 000 or more points).power (float) – Exponent applied to distance in the IDW weights.
2.0is the common inverse-square weighting.max_neighbors (int) – Maximum source points used per target coordinate.
min_neighbors (int) – Minimum sources within
radius; below it themax_neighborsnearest sources are used regardless of distance.
Examples
>>> import numpy as np >>> from apbase.idw import IDW >>> x = np.ascontiguousarray([0.0, 10.0, 0.0, 10.0]) >>> y = np.ascontiguousarray([0.0, 0.0, 10.0, 10.0]) >>> z = np.ascontiguousarray([5.0, 7.0, 8.0, 10.0]) >>> targets = np.ascontiguousarray([[5.0, 5.0], [10.0, 10.0]]) >>> values = IDW(radius=20.0, min_neighbors=1).fit(x, y, z).interpolate(targets)- property radius: float | None
Local search radius.
- Returns:
Explicit radius, or
Noneto derive it from the variogram range duringfit().- Return type:
float or None
- property power: float
Exponent applied to distance in the IDW weights.
- Returns:
Positive distance exponent.
- Return type:
float
- property max_neighbors: int
Maximum source points used per target coordinate.
- Returns:
Upper bound on local neighbors.
- Return type:
int
- property min_neighbors: int
Minimum sources within
radiusbefore the nearest-sources fallback.- Returns:
Lower bound on local neighbors.
- Return type:
int
- property n_threads: int
OpenMP thread count passed to native routines.
Resolved live from
apbase.config["n_threads"](see the class docstring) on every access – not cached at construction time.- Returns:
Current OpenMP thread count.
- Return type:
int
- classmethod from_data(x, y, z, **kwargs)
Create and fit an IDW interpolator.
- property is_fitted: bool
Whether finite source data are available.
- Returns:
Trueafterfit()succeeds.- Return type:
bool
- property model_params: dict[str, float | int | str]
Readable parameters for the variogram used by the default radius.
- Returns:
Variogram parameters used to derive the implicit radius.
- Return type:
dict
- Raises:
ValueError – If the interpolator is not fitted or was fitted with an explicit radius, so no variogram was estimated.
- fit(x, y, z)
Store finite source samples used by later interpolation.
- Parameters:
x (ArrayLike) – One-dimensional source x coordinates. Use projected/metric units when
radiusis metric.y (ArrayLike) – One-dimensional source y coordinates. Must have the same length as
x.z (ArrayLike) – One-dimensional source values to interpolate. Must have the same length as
xandy.
- Returns:
The fitted interpolator. Rows where
x,y, orzareNaNor infinite are ignored.- Return type:
- Raises:
ValueError – If input arrays have inconsistent size or fewer than
min_neighborsfinite rows.
Examples
>>> idw = IDW(radius=30.0, min_neighbors=1) >>> idw.fit(x, y, z) >>> idw.is_fitted True
- interpolate(targets)
Estimate values at target coordinates.
- Parameters:
targets (Grid | ArrayLike) – Target coordinates as an
(n_targets, 2)array, where column0is x and column1is y. A fittedGrid, GeoDataFrame/GeoSeries, or list ofPoint-like objects is also accepted; these are parsed by the sharedextract_target_xyhelper.- Returns:
One-dimensional
float64array with one interpolated value per target coordinate. Targets with fewer thanmin_neighborssources withinradiususe themax_neighborsnearest ones.- Return type:
numpy.ndarray
Examples
>>> targets = np.ascontiguousarray([[5.0, 5.0], [20.0, 20.0]]) >>> values = idw.interpolate(targets)
- predict(targets)
Estimate values at target coordinates.
- Parameters:
targets (Grid or array_like) – Target coordinates accepted by
interpolate().- Returns:
One interpolated value per target coordinate.
- Return type:
numpy.ndarray
- apbase.idw.idw(x, y, z, targets, *, radius=None, power=2.0, max_neighbors=40, min_neighbors=3)
Fit IDW from source data and estimate target values.
- Parameters:
x (array_like) – Source coordinates and values.
y (array_like) – Source coordinates and values.
z (array_like) – Source coordinates and values.
targets (array_like) – Target coordinates accepted by
apbase.idw.IDW.interpolate().radius (float or None, default None) – Local search radius. If
None, deriverangefrom a fitted variogram (range / 3with 10 000 or more points).power (float, default 2.0) – IDW distance exponent.
max_neighbors (int) – Neighbor bounds used by the local search.
min_neighbors (int) – Neighbor bounds used by the local search.
- Returns:
One interpolated value per target coordinate.
- Return type:
numpy.ndarray