Skip to content

Commit 40a8140

Browse files
authored
Feature: GSTools covariance model as variogram_model input (#125)
* added GSTools CovModel as possible variogram_model input; added example * README GSTools example * raise ValueError for wrong dim; remove links in comments * add test for gstools interface * update README gstools example * update gstools example; remove png * README: remove link to GSTools before PyKrige Documentation * GSTools example: apply CI hack for plotting * GSTools: update link in DOC * CI: add gstools as dep * GSTools: fix example * CI: gstools test only on travis and py36 * CI: skip additional installation if sklearn and gstools
1 parent a31dbbb commit 40a8140

File tree

9 files changed

+405
-149
lines changed

9 files changed

+405
-149
lines changed

.travis.yml

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ services:
55
matrix:
66
include:
77
- python: "3.6"
8-
env: DEPS="numpy scipy cython nose matplotlib scikit-learn"
9-
- python: "3.5"
8+
env: DEPS="numpy scipy cython nose matplotlib scikit-learn gstools"
9+
- python: "3.5"
1010
env: DEPS="numpy scipy=0.18 cython nose matplotlib scikit-learn=0.18.1"
11-
- python: "2.7"
11+
- python: "2.7"
1212
env: DEPS="numpy=1.10.4 scipy=0.17 cython nose matplotlib scikit-learn=0.17.1"
1313

1414
# setup adapted from https://github.com/soft-matter/trackpy/blob/master/.travis.yml
@@ -32,13 +32,13 @@ before_script:
3232
- "export DISPLAY=:99.0"
3333

3434
# command to run tests
35-
script:
35+
script:
3636
- source activate krige-env
3737
- pytest -sv ./pykrige/ --cov=pykrige
3838
- |
3939
# run examples
4040
# scikit-learn needed by the regression kriging example
41-
conda install -y scikit-learn
41+
# conda install -y scikit-learn gstools
4242
set -x
4343
for f in examples/*.py; do
4444
python $f 2>&1 | tee -a ~/log.txt
@@ -47,4 +47,3 @@ script:
4747
4848
#after_success:
4949
# coveralls
50-

README.rst

100755100644
+65-23
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,35 @@ First we will create a 2D dataset together with the associated x, y grids,
6262
import numpy as np
6363
import pykrige.kriging_tools as kt
6464
from pykrige.ok import OrdinaryKriging
65-
65+
6666
data = np.array([[0.3, 1.2, 0.47],
6767
[1.9, 0.6, 0.56],
6868
[1.1, 3.2, 0.74],
6969
[3.3, 4.4, 1.47],
7070
[4.7, 3.8, 1.74]])
71-
71+
7272
gridx = np.arange(0.0, 5.5, 0.5)
7373
gridy = np.arange(0.0, 5.5, 0.5)
74-
74+
7575
# Create the ordinary kriging object. Required inputs are the X-coordinates of
7676
# the data points, the Y-coordinates of the data points, and the Z-values of the
7777
# data points. If no variogram model is specified, defaults to a linear variogram
7878
# model. If no variogram model parameters are specified, then the code automatically
79-
# calculates the parameters by fitting the variogram model to the binned
79+
# calculates the parameters by fitting the variogram model to the binned
8080
# experimental semivariogram. The verbose kwarg controls code talk-back, and
8181
# the enable_plotting kwarg controls the display of the semivariogram.
8282
OK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear',
8383
verbose=False, enable_plotting=False)
84-
84+
8585
# Creates the kriged grid and the variance grid. Allows for kriging on a rectangular
8686
# grid of points, on a masked rectangular grid of points, or with arbitrary points.
8787
# (See OrdinaryKriging.__doc__ for more information.)
8888
z, ss = OK.execute('grid', gridx, gridy)
89-
89+
9090
# Writes the kriged grid to an ASCII grid file.
9191
kt.write_asc_grid(gridx, gridy, z, filename="output.asc")
9292
93+
9394
Universal Kriging Example
9495
^^^^^^^^^^^^^^^^^^^^^^^^^
9596

@@ -111,16 +112,17 @@ Universal Kriging Example
111112
# the data points, the Y-coordinates of the data points, and the Z-values of the
112113
# data points. Variogram is handled as in the ordinary kriging case.
113114
# drift_terms is a list of the drift terms to include; currently supported terms
114-
# are 'regional_linear', 'point_log', and 'external_Z'. Refer to
115+
# are 'regional_linear', 'point_log', and 'external_Z'. Refer to
115116
# UniversalKriging.__doc__ for more information.
116117
UK = UniversalKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear',
117118
drift_terms=['regional_linear'])
118-
119+
119120
# Creates the kriged grid and the variance grid. Allows for kriging on a rectangular
120121
# grid of points, on a masked rectangular grid of points, or with arbitrary points.
121122
# (See UniversalKriging.__doc__ for more information.)
122123
z, ss = UK.execute('grid', gridx, gridy)
123124
125+
124126
Three-Dimensional Kriging Example
125127
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126128

@@ -140,44 +142,84 @@ Three-Dimensional Kriging Example
140142
gridy = np.arange(0.0, 0.6, 0.01)
141143
gridz = np.arange(0.0, 0.6, 0.1)
142144
143-
# Create the 3D ordinary kriging object and solves for the three-dimension kriged
145+
# Create the 3D ordinary kriging object and solves for the three-dimension kriged
144146
# volume and variance. Refer to OrdinaryKriging3D.__doc__ for more information.
145147
ok3d = OrdinaryKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3],
146148
variogram_model='linear')
147149
k3d, ss3d = ok3d.execute('grid', gridx, gridy, gridz)
148150
149-
# Create the 3D universal kriging object and solves for the three-dimension kriged
151+
# Create the 3D universal kriging object and solves for the three-dimension kriged
150152
# volume and variance. Refer to UniversalKriging3D.__doc__ for more information.
151-
uk3d = UniversalKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3],
153+
uk3d = UniversalKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3],
152154
variogram_model='linear', drift_terms=['regional_linear'])
153155
k3d, ss3d = uk3d.execute('grid', gridx, gridy, gridz)
154156
155-
# To use the generic 'specified' drift term, the user must provide the drift values
156-
# at each data point and at every grid point. The following example is equivalent to
157+
# To use the generic 'specified' drift term, the user must provide the drift values
158+
# at each data point and at every grid point. The following example is equivalent to
157159
# using a linear drift in all three spatial dimensions. Refer to
158160
# UniversalKriging3D.__doc__ for more information.
159161
zg, yg, xg = np.meshgrid(gridz, gridy, gridx, indexing='ij')
160-
uk3d = UniversalKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3],
162+
uk3d = UniversalKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3],
161163
variogram_model='linear', drift_terms=['specified'],
162164
specified_drift=[data[:, 0], data[:, 1]])
163165
k3d, ss3d = uk3d.execute('grid', gridx, gridy, gridz, specified_drift_arrays=[xg, yg, zg])
164166
165-
# To use the generic 'functional' drift term, the user must provide a callable
166-
# function that takes only the spatial dimensions as arguments. The following example
167-
# is equivalent to using a linear drift only in the x-direction. Refer to
167+
# To use the generic 'functional' drift term, the user must provide a callable
168+
# function that takes only the spatial dimensions as arguments. The following example
169+
# is equivalent to using a linear drift only in the x-direction. Refer to
168170
# UniversalKriging3D.__doc__ for more information.
169171
func = lambda x, y, z: x
170-
uk3d = UniversalKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3],
172+
uk3d = UniversalKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3],
171173
variogram_model='linear', drift_terms=['functional'],
172174
functional_drift=[func])
173175
k3d, ss3d = uk3d.execute('grid', gridx, gridy, gridz)
174176
175-
# Note that the use of the 'specified' and 'functional' generic drift capabilities is
176-
# essentially identical in the two-dimensional universal kriging class (except for a
177-
# difference in the number of spatial coordinates for the passed drift functions).
177+
# Note that the use of the 'specified' and 'functional' generic drift capabilities is
178+
# essentially identical in the two-dimensional universal kriging class (except for a
179+
# difference in the number of spatial coordinates for the passed drift functions).
178180
# See UniversalKriging.__doc__ for more information.
179181
180182
183+
GSTools covariance models
184+
^^^^^^^^^^^^^^^^^^^^^^^^^
185+
186+
You can also use `GSTools <https://github.com/GeoStat-Framework/GSTools>`_
187+
covariance models as input for the ``variogram_model`` in the
188+
PyKrige routines:
189+
190+
.. code:: python
191+
192+
import numpy as np
193+
from gstools import Gaussian
194+
from pykrige.ok import OrdinaryKriging
195+
from matplotlib import pyplot as plt
196+
197+
# conditioning data
198+
data = np.array([[0.3, 1.2, 0.47],
199+
[1.9, 0.6, 0.56],
200+
[1.1, 3.2, 0.74],
201+
[3.3, 4.4, 1.47],
202+
[4.7, 3.8, 1.74]])
203+
# grid definition for output field
204+
gridx = np.arange(0.0, 5.5, 0.1)
205+
gridy = np.arange(0.0, 6.5, 0.1)
206+
# a GSTools based covariance model
207+
cov_model = Gaussian(dim=2, len_scale=1, anis=0.2, angles=-0.5, var=0.5, nugget=0.1)
208+
# ordinary kriging with pykrige
209+
OK1 = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], cov_model)
210+
z1, ss1 = OK1.execute('grid', gridx, gridy)
211+
plt.imshow(z1, origin="lower")
212+
plt.show()
213+
214+
Which gives:
215+
216+
.. image:: https://raw.githubusercontent.com/GeoStat-Framework/GSTools/master/docs/source/pics/20_pykrige.png
217+
:width: 400px
218+
:align: center
219+
220+
Have a look at the `documentation about the Covariance Model of GSTools <https://geostat-framework.readthedocs.io/projects/gstools/en/latest/tutorial_02_cov.html>`_.
221+
222+
181223
Kriging Parameters Tuning
182224
^^^^^^^^^^^^^^^^^^^^^^^^^
183225

@@ -194,8 +236,8 @@ Regression Kriging
194236
with `pykrige.rk.RegressionKriging <http://pykrige.readthedocs.io/en/latest/examples/regression_kriging2d.html>`_.
195237
This class takes as parameters a scikit-learn regression model, and details of either either
196238
the ``OrdinaryKriging`` or the ``UniversalKriging`` class, and performs a correction steps on the ML regression prediction.
197-
198-
A demonstration of the regression kriging is provided in the
239+
240+
A demonstration of the regression kriging is provided in the
199241
`corresponding example <http://pykrige.readthedocs.io/en/latest/examples/regression_kriging2d.html#sphx-glr-examples-regression-kriging2d-py>`_.
200242

201243
License

appveyor.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ install:
3232

3333
test_script:
3434
- pytest -sv --cov=pykrige pykrige/
35-
- ps: |
35+
- ps: |
3636
cd ".\\examples"
37-
Get-ChildItem ".\\" -Filter *.py |
37+
Get-ChildItem ".\\" -Filter *.py |
3838
Foreach-Object {
3939
python $_.FullName 2>&1 | Tee-Object -Append -FilePath "C:\\log.txt"
4040
}

examples/gstools_covmodel.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
GSTools Interface
4+
=================
5+
6+
Example how to use the PyKrige routines with a GSTools CovModel.
7+
"""
8+
import os
9+
10+
import numpy as np
11+
from pykrige.ok import OrdinaryKriging
12+
from matplotlib import pyplot as plt
13+
try:
14+
from gstools import Gaussian
15+
GS_IMP = True
16+
except ImportError:
17+
GS_IMP = False
18+
19+
# conditioning data
20+
data = np.array([[0.3, 1.2, 0.47],
21+
[1.9, 0.6, 0.56],
22+
[1.1, 3.2, 0.74],
23+
[3.3, 4.4, 1.47],
24+
[4.7, 3.8, 1.74]])
25+
# grid definition for output field
26+
gridx = np.arange(0.0, 5.5, 0.1)
27+
gridy = np.arange(0.0, 6.5, 0.1)
28+
# a GSTools based covariance model
29+
if GS_IMP:
30+
cov_model = Gaussian(
31+
dim=2, len_scale=4, anis=.2, angles=-.5, var=.5, nugget=.1
32+
)
33+
else:
34+
cov_model = "gaussian"
35+
# ordinary kriging with pykrige
36+
OK1 = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], cov_model)
37+
z1, ss1 = OK1.execute('grid', gridx, gridy)
38+
plt.imshow(z1, origin="lower")
39+
if 'CI' not in os.environ:
40+
# skip in continous integration
41+
plt.show()

0 commit comments

Comments
 (0)