Skip to content

Commit 41c1d18

Browse files
committed
idw - known_values and known_geometries parameters, tests
1 parent 1a43b5b commit 41c1d18

File tree

4 files changed

+76
-31
lines changed

4 files changed

+76
-31
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Changes - from version >= 1.x
88

99
* [enhancement] `Blocks` has the new parameters `values` and `geometries`, that might be provided instead of `ds` parameter
1010
* [enhancement] `calculate_semivariance()` function has the new parameters `values` and `geometries`, that might be provided instead of `ds` parameter
11-
* [enhancement] `interpolate_points()` function has the new parameters `values` and `geometries`, that might be provided instead of `ds` parameter
12-
* [enhancement] `validate_kriging()` function has the new parameters `values` and `geometries`, that might be provided instead of `ds` parameter
11+
* [enhancement] `interpolate_points()` function has the new parameters `known_values` and `known_geometries`, that might be provided instead of the `known_locations` parameter
12+
* [enhancement] `validate_kriging()` function has the new parameters `known_values` and `known_geometries`, that might be provided instead of the `known_locations` parameter
1313
* [enhancement] `validate_kriging()` has new parameter: `progress_bar` that controls `tqdm` progress bar
14-
*
14+
* [enhancement] `inverse_distance_weighting()` function has the new parameters `known_values` and `known_geometries`, that might be provided instead of the `known_locations` parameter
1515

1616
2025-10-11
1717
----------

src/pyinterpolate/idw/idw.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
TODO
99
- eval with IDW
1010
"""
11-
from typing import Iterable
11+
from numpy.typing import ArrayLike
1212

1313
import numpy as np
1414

1515
from pyinterpolate.core.data_models.points import VariogramPoints
1616
from pyinterpolate.distance.point import point_distance
17+
from transform.geo import geometry_and_values_array
1718

1819

19-
def inverse_distance_weighting(known_locations: np.ndarray,
20-
unknown_location: Iterable,
20+
def inverse_distance_weighting(unknown_location: ArrayLike,
21+
known_locations: ArrayLike = None,
22+
known_values: ArrayLike = None,
23+
known_geometries: ArrayLike = None,
2124
no_neighbors=-1,
2225
power=2.) -> float:
2326
"""
@@ -26,11 +29,6 @@ def inverse_distance_weighting(known_locations: np.ndarray,
2629
2730
Parameters
2831
----------
29-
known_locations : numpy array
30-
The MxN array, where **M** is a number of rows (points) and **N**
31-
is the number of columns, where the last column represents a value
32-
observed in a known point. (It could be **(N-1)**-dimensional data).
33-
3432
unknown_location : Iterable
3533
Array or list with coordinates of the unknown point.
3634
Its length is N-1 (number of dimensions). The unknown
@@ -39,6 +37,18 @@ def inverse_distance_weighting(known_locations: np.ndarray,
3937
is added once - vector of points ``[x, y]``
4038
becomes ``[[x, y]]`` for 2-dimensional data.
4139
40+
known_locations : numpy array, optional
41+
The known locations: ``[x, y, value]``.
42+
43+
known_values : ArrayLike, optional
44+
Observation in the i-th geometry (from ``known_geometries``). Optional
45+
parameter, if not given then ``known_locations`` must be provided.
46+
47+
known_geometries : ArrayLike, optional
48+
Array or similar structure with geometries. It must have the same
49+
length as ``known_values``. Optional parameter, if not given then
50+
``known_locations`` must be provided. Point type geometry.
51+
4252
no_neighbors : int, default = -1
4353
If default value **(-1)** then all known points will be used to
4454
estimate value at the unknown location.
@@ -68,6 +78,13 @@ def inverse_distance_weighting(known_locations: np.ndarray,
6878
if power < 0:
6979
raise ValueError('Power cannot be smaller than 0')
7080

81+
# Get known locations
82+
if known_locations is None:
83+
known_locations = geometry_and_values_array(
84+
geometry=known_geometries,
85+
values=known_values
86+
)
87+
7188
# Check known points parameter
7289
# Check if known locations are in the right format
7390
known_locations = VariogramPoints(known_locations).points

src/pyinterpolate/transform/geo.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,25 @@ def geometry_and_values_array(geometry,
3232
arr = []
3333

3434
is_point = isinstance(geometry[0], Point)
35+
greater_than_2_dims = len(geometry[0]) > 2
3536

36-
if is_point:
37+
if greater_than_2_dims:
3738
for idx, rec in enumerate(geometry):
38-
arr.append(
39-
[rec.x, rec.y, values[idx]]
40-
)
39+
rlist = rec.tolist()
40+
rlist.append(values[idx])
41+
arr.append(rlist)
4142
else:
42-
for idx, rec in enumerate(geometry):
43-
arr.append(
44-
[rec[0], rec[1], values[idx]]
45-
)
43+
if is_point:
44+
for idx, rec in enumerate(geometry):
45+
arr.append(
46+
[rec.x, rec.y, values[idx]]
47+
)
48+
else:
49+
for idx, rec in enumerate(geometry):
50+
arr.append(
51+
[rec[0], rec[1], values[idx]]
52+
)
53+
4654
return np.array(arr)
4755

4856

@@ -217,5 +225,3 @@ def reproject_flat(ds: Union[pd.DataFrame, np.ndarray],
217225
mpol = MultiPolygon([pol1, pol2])
218226

219227
assert largest_geometry(mpol) == pol2
220-
221-

tests/test_idw/test_idw.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ def test_idw_2d():
88
pos1 = [[11, 1, 1], [23, 2, 2], [33, 3, 3], [14, 44, 4], [13, 10, 9], [12, 55, 35], [11, 9, 7]]
99
pos2 = [[11, 1, 1], [23, 2, 2], [33, 3, 3], [14, 44, 4], [10, 10, 999], [12, 55, 35], [11, 9, 7]]
1010

11-
u_val1 = inverse_distance_weighting(np.array(pos1),
12-
unknown_pos,
13-
-1, 0.5)
11+
u_val1 = inverse_distance_weighting(unknown_pos,
12+
known_locations=np.array(pos1),
13+
no_neighbors=-1,
14+
power=0.5)
1415

15-
u_val2 = inverse_distance_weighting(np.array(pos1),
16-
np.array(unknown_pos),
17-
3)
16+
u_val2 = inverse_distance_weighting(np.array(unknown_pos),
17+
known_locations=np.array(pos1),
18+
no_neighbors=3)
1819

19-
u_val3 = inverse_distance_weighting(np.array(pos2),
20-
np.array(unknown_pos),
21-
3)
20+
u_val3 = inverse_distance_weighting(np.array(unknown_pos),
21+
known_locations=np.array(pos2),
22+
no_neighbors=3)
2223

2324
# Test case 1: u_val1 > u_val2 > 7
2425

@@ -39,6 +40,27 @@ def test_multidimensional_idw():
3940
coords = np.array(coords)
4041
u_coords = [[0, 1, 2]]
4142

42-
res = inverse_distance_weighting(coords, u_coords, 2, 10)
43+
res = inverse_distance_weighting(u_coords,
44+
known_locations=coords,
45+
no_neighbors=2,
46+
power=10)
47+
48+
assert res == 1
49+
50+
51+
def test_separate_geometry_idw():
52+
coords = [[0, 0, 0, 1],
53+
[1, 1, 1, 1],
54+
[2, 2, 2, 1],
55+
[5, 5, 5, 1]]
56+
57+
coords = np.array(coords)
58+
u_coords = [[0, 1, 2]]
59+
60+
res = inverse_distance_weighting(u_coords,
61+
known_values=coords[:, -1],
62+
known_geometries=coords[:, :-1],
63+
no_neighbors=2,
64+
power=10)
4365

4466
assert res == 1

0 commit comments

Comments
 (0)