Skip to content

Commit 773c538

Browse files
authored
Merge pull request #160 from GeoStat-Framework/pykrige_compat
Krige: Adapting to PyKrige
2 parents fe9b745 + b667e17 commit 773c538

3 files changed

Lines changed: 68 additions & 69 deletions

File tree

examples/05_kriging/02_pykrige_interface.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,58 @@
55
To use fancier methods like
66
`regression kriging <https://en.wikipedia.org/wiki/Regression-kriging>`__,
77
we provide an interface to
8-
`PyKrige <https://github.com/bsmurphy/PyKrige>`__.
8+
`PyKrige <https://github.com/GeoStat-Framework/PyKrige>`__ (>v1.5), which means
9+
you can pass a GSTools covariance model to the kriging routines of PyKrige.
910
10-
In the future you can pass a GSTools Covariance Model
11-
to the PyKrige routines as ``variogram_model``.
12-
13-
At the moment we only provide prepared
14-
keyword arguments for the pykrige routines.
15-
16-
To demonstrate the general workflow, we compare the ordinary kriging of PyKrige
17-
with GSTools in 2D:
11+
To demonstrate the general workflow, we compare ordinary kriging of PyKrige
12+
with the corresponding GSTools routine in 2D:
1813
"""
1914
import numpy as np
2015
import gstools as gs
2116
from pykrige.ok import OrdinaryKriging
2217
from matplotlib import pyplot as plt
2318

2419
# conditioning data
25-
data = np.array(
26-
[
27-
[0.3, 1.2, 0.47],
28-
[1.9, 0.6, 0.56],
29-
[1.1, 3.2, 0.74],
30-
[3.3, 4.4, 1.47],
31-
[4.7, 3.8, 1.74],
32-
]
33-
)
20+
cond_x = [0.3, 1.9, 1.1, 3.3, 4.7]
21+
cond_y = [1.2, 0.6, 3.2, 4.4, 3.8]
22+
cond_val = [0.47, 0.56, 0.74, 1.47, 1.74]
3423

3524
# grid definition for output field
3625
gridx = np.arange(0.0, 5.5, 0.1)
3726
gridy = np.arange(0.0, 6.5, 0.1)
3827

3928
###############################################################################
40-
# A GSTools based covariance model.
29+
# A GSTools based :any:`Gaussian` covariance model:
4130

42-
cov_model = gs.Gaussian(
31+
model = gs.Gaussian(
4332
dim=2, len_scale=1, anis=0.2, angles=-0.5, var=0.5, nugget=0.1
4433
)
4534

4635
###############################################################################
47-
# Ordinary kriging with pykrige.
48-
# A dictionary containing keyword arguments for the pykrige routines is
49-
# provided by the gstools covariance models.
50-
51-
pk_kwargs = cov_model.pykrige_kwargs
52-
OK1 = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], **pk_kwargs)
36+
# Ordinary Kriging with PyKrige
37+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
#
39+
# One can pass the defined GSTools model as
40+
# variogram model, which will `not` be fitted to the given data.
41+
# By providing the GSTools model, rotation and anisotropy are also
42+
# automatically defined:
43+
44+
OK1 = OrdinaryKriging(cond_x, cond_y, cond_val, variogram_model=model)
5345
z1, ss1 = OK1.execute("grid", gridx, gridy)
5446
plt.imshow(z1, origin="lower")
5547
plt.show()
5648

5749
###############################################################################
58-
# Ordinary kriging with gstools for comparison.
59-
60-
OK2 = gs.krige.Ordinary(cov_model, [data[:, 0], data[:, 1]], data[:, 2])
50+
# Ordinary Kriging with GSTools
51+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
#
53+
# The :any:`Ordinary` kriging class is provided by GSTools as a shortcut to
54+
# define ordinary kriging with the general :any:`Krige` class.
55+
#
56+
# PyKrige's routines are using exact kriging by default (when given a nugget).
57+
# To reproduce this behavior in GSTools, we have to set ``exact=True``.
58+
59+
OK2 = gs.krige.Ordinary(model, [cond_x, cond_y], cond_val, exact=True)
6160
OK2.structured([gridx, gridy])
6261
ax = OK2.plot()
6362
ax.set_aspect("equal")

gstools/krige/base.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
__all__ = ["Krige"]
2929

3030

31-
P_INV = {1: spl.pinv, 2: spl.pinv2, 3: spl.pinvh}
31+
P_INV = {"pinv": spl.pinv, "pinv2": spl.pinv2, "pinvh": spl.pinvh}
3232
"""dict: Standard pseudo-inverse routines"""
3333

3434

@@ -93,16 +93,16 @@ class Krige(Field):
9393
kriging matrix. If `True`, this leads to more numerical stability
9494
and redundant points are averaged. But it can take more time.
9595
Default: True
96-
pseudo_inv_type : :class:`int` or :any:`callable`, optional
96+
pseudo_inv_type : :class:`str` or :any:`callable`, optional
9797
Here you can select the algorithm to compute the pseudo-inverse matrix:
9898
99-
* `1`: use `pinv` from `scipy` which uses `lstsq`
100-
* `2`: use `pinv2` from `scipy` which uses `SVD`
101-
* `3`: use `pinvh` from `scipy` which uses eigen-values
99+
* `"pinv"`: use `pinv` from `scipy` which uses `lstsq`
100+
* `"pinv2"`: use `pinv2` from `scipy` which uses `SVD`
101+
* `"pinvh"`: use `pinvh` from `scipy` which uses eigen-values
102102
103103
If you want to use another routine to invert the kriging matrix,
104104
you can pass a callable which takes a matrix and returns the inverse.
105-
Default: `1`
105+
Default: `"pinv"`
106106
fit_normalizer : :class:`bool`, optional
107107
Wheater to fit the data-normalizer to the given conditioning data.
108108
Default: False
@@ -139,7 +139,7 @@ def __init__(
139139
exact=False,
140140
cond_err="nugget",
141141
pseudo_inv=True,
142-
pseudo_inv_type=1,
142+
pseudo_inv_type="pinv",
143143
fit_normalizer=False,
144144
fit_variogram=False,
145145
):
@@ -650,13 +650,13 @@ def pseudo_inv(self):
650650

651651
@property
652652
def pseudo_inv_type(self):
653-
""":class:`int`: Method selector for pseudo inverse calculation."""
653+
""":class:`str`: Method selector for pseudo inverse calculation."""
654654
return self._pseudo_inv_type
655655

656656
@pseudo_inv_type.setter
657657
def pseudo_inv_type(self, val):
658-
if val not in [1, 2, 3] and not callable(val):
659-
raise ValueError("Krige: pseudo_inv_type needs to be in [1,2,3]")
658+
if val not in P_INV and not callable(val):
659+
raise ValueError(f"Krige: pseudo_inv_type not in {sorted(P_INV)}")
660660
self._pseudo_inv_type = val
661661

662662
@property

gstools/krige/methods.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ class Simple(Krige):
6464
kriging matrix. If `True`, this leads to more numerical stability
6565
and redundant points are averaged. But it can take more time.
6666
Default: True
67-
pseudo_inv_type : :class:`int` or :any:`callable`, optional
67+
pseudo_inv_type : :class:`str` or :any:`callable`, optional
6868
Here you can select the algorithm to compute the pseudo-inverse matrix:
6969
70-
* `1`: use `pinv` from `scipy` which uses `lstsq`
71-
* `2`: use `pinv2` from `scipy` which uses `SVD`
72-
* `3`: use `pinvh` from `scipy` which uses eigen-values
70+
* `"pinv"`: use `pinv` from `scipy` which uses `lstsq`
71+
* `"pinv2"`: use `pinv2` from `scipy` which uses `SVD`
72+
* `"pinvh"`: use `pinvh` from `scipy` which uses eigen-values
7373
7474
If you want to use another routine to invert the kriging matrix,
7575
you can pass a callable which takes a matrix and returns the inverse.
76-
Default: `1`
76+
Default: `"pinv"`
7777
fit_normalizer : :class:`bool`, optional
7878
Wheater to fit the data-normalizer to the given conditioning data.
7979
Default: False
@@ -96,7 +96,7 @@ def __init__(
9696
exact=False,
9797
cond_err="nugget",
9898
pseudo_inv=True,
99-
pseudo_inv_type=1,
99+
pseudo_inv_type="pinv",
100100
fit_normalizer=False,
101101
fit_variogram=False,
102102
):
@@ -159,16 +159,16 @@ class Ordinary(Krige):
159159
kriging matrix. If `True`, this leads to more numerical stability
160160
and redundant points are averaged. But it can take more time.
161161
Default: True
162-
pseudo_inv_type : :class:`int` or :any:`callable`, optional
162+
pseudo_inv_type : :class:`str` or :any:`callable`, optional
163163
Here you can select the algorithm to compute the pseudo-inverse matrix:
164164
165-
* `1`: use `pinv` from `scipy` which uses `lstsq`
166-
* `2`: use `pinv2` from `scipy` which uses `SVD`
167-
* `3`: use `pinvh` from `scipy` which uses eigen-values
165+
* `"pinv"`: use `pinv` from `scipy` which uses `lstsq`
166+
* `"pinv2"`: use `pinv2` from `scipy` which uses `SVD`
167+
* `"pinvh"`: use `pinvh` from `scipy` which uses eigen-values
168168
169169
If you want to use another routine to invert the kriging matrix,
170170
you can pass a callable which takes a matrix and returns the inverse.
171-
Default: `1`
171+
Default: `"pinv"`
172172
fit_normalizer : :class:`bool`, optional
173173
Wheater to fit the data-normalizer to the given conditioning data.
174174
Default: False
@@ -190,7 +190,7 @@ def __init__(
190190
exact=False,
191191
cond_err="nugget",
192192
pseudo_inv=True,
193-
pseudo_inv_type=1,
193+
pseudo_inv_type="pinv",
194194
fit_normalizer=False,
195195
fit_variogram=False,
196196
):
@@ -264,16 +264,16 @@ class Universal(Krige):
264264
kriging matrix. If `True`, this leads to more numerical stability
265265
and redundant points are averaged. But it can take more time.
266266
Default: True
267-
pseudo_inv_type : :class:`int` or :any:`callable`, optional
267+
pseudo_inv_type : :class:`str` or :any:`callable`, optional
268268
Here you can select the algorithm to compute the pseudo-inverse matrix:
269269
270-
* `1`: use `pinv` from `scipy` which uses `lstsq`
271-
* `2`: use `pinv2` from `scipy` which uses `SVD`
272-
* `3`: use `pinvh` from `scipy` which uses eigen-values
270+
* `"pinv"`: use `pinv` from `scipy` which uses `lstsq`
271+
* `"pinv2"`: use `pinv2` from `scipy` which uses `SVD`
272+
* `"pinvh"`: use `pinvh` from `scipy` which uses eigen-values
273273
274274
If you want to use another routine to invert the kriging matrix,
275275
you can pass a callable which takes a matrix and returns the inverse.
276-
Default: `1`
276+
Default: `"pinv"`
277277
fit_normalizer : :class:`bool`, optional
278278
Wheater to fit the data-normalizer to the given conditioning data.
279279
Default: False
@@ -296,7 +296,7 @@ def __init__(
296296
exact=False,
297297
cond_err="nugget",
298298
pseudo_inv=True,
299-
pseudo_inv_type=1,
299+
pseudo_inv_type="pinv",
300300
fit_normalizer=False,
301301
fit_variogram=False,
302302
):
@@ -366,16 +366,16 @@ class ExtDrift(Krige):
366366
kriging matrix. If `True`, this leads to more numerical stability
367367
and redundant points are averaged. But it can take more time.
368368
Default: True
369-
pseudo_inv_type : :class:`int` or :any:`callable`, optional
369+
pseudo_inv_type : :class:`str` or :any:`callable`, optional
370370
Here you can select the algorithm to compute the pseudo-inverse matrix:
371371
372-
* `1`: use `pinv` from `scipy` which uses `lstsq`
373-
* `2`: use `pinv2` from `scipy` which uses `SVD`
374-
* `3`: use `pinvh` from `scipy` which uses eigen-values
372+
* `"pinv"`: use `pinv` from `scipy` which uses `lstsq`
373+
* `"pinv2"`: use `pinv2` from `scipy` which uses `SVD`
374+
* `"pinvh"`: use `pinvh` from `scipy` which uses eigen-values
375375
376376
If you want to use another routine to invert the kriging matrix,
377377
you can pass a callable which takes a matrix and returns the inverse.
378-
Default: `1`
378+
Default: `"pinv"`
379379
fit_normalizer : :class:`bool`, optional
380380
Wheater to fit the data-normalizer to the given conditioning data.
381381
Default: False
@@ -398,7 +398,7 @@ def __init__(
398398
exact=False,
399399
cond_err="nugget",
400400
pseudo_inv=True,
401-
pseudo_inv_type=1,
401+
pseudo_inv_type="pinv",
402402
fit_normalizer=False,
403403
fit_variogram=False,
404404
):
@@ -461,16 +461,16 @@ class Detrended(Krige):
461461
kriging matrix. If `True`, this leads to more numerical stability
462462
and redundant points are averaged. But it can take more time.
463463
Default: True
464-
pseudo_inv_type : :class:`int` or :any:`callable`, optional
464+
pseudo_inv_type : :class:`str` or :any:`callable`, optional
465465
Here you can select the algorithm to compute the pseudo-inverse matrix:
466466
467-
* `1`: use `pinv` from `scipy` which uses `lstsq`
468-
* `2`: use `pinv2` from `scipy` which uses `SVD`
469-
* `3`: use `pinvh` from `scipy` which uses eigen-values
467+
* `"pinv"`: use `pinv` from `scipy` which uses `lstsq`
468+
* `"pinv2"`: use `pinv2` from `scipy` which uses `SVD`
469+
* `"pinvh"`: use `pinvh` from `scipy` which uses eigen-values
470470
471471
If you want to use another routine to invert the kriging matrix,
472472
you can pass a callable which takes a matrix and returns the inverse.
473-
Default: `1`
473+
Default: `"pinv"`
474474
fit_variogram : :class:`bool`, optional
475475
Wheater to fit the given variogram model to the data.
476476
This is done by using isotropy settings of the given model,
@@ -488,7 +488,7 @@ def __init__(
488488
exact=False,
489489
cond_err="nugget",
490490
pseudo_inv=True,
491-
pseudo_inv_type=1,
491+
pseudo_inv_type="pinv",
492492
fit_variogram=False,
493493
):
494494
super().__init__(

0 commit comments

Comments
 (0)