Skip to content

Commit 45ff9e8

Browse files
committed
interpolate_points() - introduction of new parameters
1 parent 6047084 commit 45ff9e8

File tree

5 files changed

+7014
-14
lines changed

5 files changed

+7014
-14
lines changed

src/pyinterpolate/core/pipelines/interpolate.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from numpy.typing import ArrayLike
12
import os
23

34
import dask
@@ -7,12 +8,15 @@
78

89
from pyinterpolate.kriging.point.ordinary import ok_calc
910
from pyinterpolate.semivariogram.theoretical.classes.theoretical_variogram import TheoreticalVariogram
11+
from transform.geo import geometry_and_values_array
1012

1113

1214
def interpolate_points(
1315
theoretical_model: TheoreticalVariogram,
14-
known_locations: np.ndarray,
15-
unknown_locations: np.ndarray,
16+
unknown_locations: ArrayLike,
17+
known_locations: ArrayLike = None,
18+
known_values: ArrayLike = None,
19+
known_geometries: ArrayLike = None,
1620
neighbors_range=None,
1721
no_neighbors=4,
1822
max_tick=5.,
@@ -29,13 +33,22 @@ def interpolate_points(
2933
theoretical_model : TheoreticalVariogram
3034
Fitted theoretical variogram model.
3135
32-
known_locations : numpy array
33-
The known locations: x, y, value.
34-
3536
unknown_locations : numpy array
3637
Points where you want to estimate value
3738
``[(x, y), ...] <-> [(lon, lat), ...]``.
3839
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+
3952
neighbors_range : float, default=None
4053
The maximum distance where we search for the neighbors.
4154
If ``None`` is given then range is selected from
@@ -69,6 +82,12 @@ def interpolate_points(
6982
``[predicted value, variance error, longitude (x), latitude (y)]``
7083
"""
7184

85+
if known_locations is None:
86+
known_locations = geometry_and_values_array(
87+
geometry=known_geometries,
88+
values=known_values
89+
)
90+
7291
interpolated_results = []
7392

7493
_disable_progress_bar = not progress_bar
@@ -94,8 +113,10 @@ def interpolate_points(
94113

95114
def interpolate_points_dask(
96115
theoretical_model: TheoreticalVariogram,
97-
known_locations: np.ndarray,
98-
unknown_locations: np.ndarray,
116+
unknown_locations: ArrayLike,
117+
known_locations: ArrayLike = None,
118+
known_values: ArrayLike = None,
119+
known_geometries: ArrayLike = None,
99120
neighbors_range=None,
100121
no_neighbors=4,
101122
max_tick=5.,
@@ -114,13 +135,22 @@ def interpolate_points_dask(
114135
theoretical_model : TheoreticalVariogram
115136
Fitted theoretical variogram model.
116137
117-
known_locations : numpy array
118-
The known locations: x, y, value.
119-
120138
unknown_locations : numpy array
121139
Points where you want to estimate value
122140
``[(x, y), ...] <-> [(lon, lat), ...]``.
123141
142+
known_locations : numpy array, optional
143+
The known locations: ``[x, y, value]``.
144+
145+
known_values : ArrayLike, optional
146+
Observation in the i-th geometry (from ``known_geometries``). Optional
147+
parameter, if not given then ``known_locations`` must be provided.
148+
149+
known_geometries : ArrayLike, optional
150+
Array or similar structure with geometries. It must have the same
151+
length as ``known_values``. Optional parameter, if not given then
152+
``known_locations`` must be provided. Point type geometry.
153+
124154
neighbors_range : float, default=None
125155
The maximum distance where we search for the neighbors.
126156
If ``None`` is given then range is selected from
@@ -159,6 +189,12 @@ def interpolate_points_dask(
159189
``[predicted value, variance error, longitude (x), latitude (y)]``
160190
"""
161191

192+
if known_locations is None:
193+
known_locations = geometry_and_values_array(
194+
geometry=known_geometries,
195+
values=known_values
196+
)
197+
162198
if number_of_workers == -1:
163199
core_num = os.cpu_count()
164200
if core_num > 1:

src/pyinterpolate/semivariogram/experimental/experimental_semivariogram.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def calculate_semivariance(ds: Union[ArrayLike, VariogramPoints] = None,
3535
``[x, y, value]``
3636
3737
values : ArrayLike, optional
38-
Aggregated values of each block. Optional parameter, if not
39-
given then ``ds`` must be provided.
38+
Observation in the i-th geometry (from ``geometries``). Optional
39+
parameter, if not given then ``ds`` must be provided.
4040
4141
geometries : ArrayLike, optional
4242
Array or similar structure with geometries. It must have the same
4343
length as ``values``. Optional parameter, if not given then ``ds``
44-
must be provided. Those must be point geometries!
44+
must be provided. Point type geometry.
4545
4646
step_size : float
4747
The fixed distance between lags grouping point neighbors.

src/pyinterpolate/transform/geo.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,45 @@
55
import numpy as np
66
import pandas as pd
77

8-
from shapely.geometry import Polygon, MultiPolygon
8+
from shapely.geometry import Polygon, MultiPolygon, Point
9+
10+
11+
def geometry_and_values_array(geometry,
12+
values) -> ArrayLike:
13+
"""
14+
Function creates single object from geometries and aggregated values.
15+
16+
Parameters
17+
----------
18+
geometry : ArrayLike
19+
20+
values : ArrayLike
21+
22+
Returns
23+
-------
24+
: numpy array
25+
"""
26+
27+
if len(geometry) != len(values):
28+
raise ValueError(
29+
'Number of geometries must be equal to number of values'
30+
)
31+
32+
arr = []
33+
34+
is_point = isinstance(geometry[0], Point)
35+
36+
if is_point:
37+
for idx, rec in enumerate(geometry):
38+
arr.append(
39+
[rec.x, rec.y, values[idx]]
40+
)
41+
else:
42+
for idx, rec in enumerate(geometry):
43+
arr.append(
44+
[rec[0], rec[1], values[idx]]
45+
)
46+
return np.array(arr)
947

1048

1149
def largest_geometry(geometry: MultiPolygon) -> Polygon:

0 commit comments

Comments
 (0)