From 492fa9db43c97c663721581a335ea30359231b38 Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Tue, 18 Aug 2020 15:34:16 +0200 Subject: [PATCH 1/8] RK: add option for pseudo-inv --- pykrige/rk.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pykrige/rk.py b/pykrige/rk.py index 2b3b40f..e7b4760 100644 --- a/pykrige/rk.py +++ b/pykrige/rk.py @@ -118,6 +118,8 @@ def __init__( n_closest_points=10, verbose=False, exact_values=True, + pseudo_inv=False, + pseudo_inv_type="pinv", variogram_parameters=None, variogram_function=None, anisotropy_scaling=(1.0, 1.0), @@ -137,6 +139,8 @@ def __init__( self.weight = weight self.verbose = verbose self.exact_values = exact_values + self.pseudo_inv = pseudo_inv + self.pseudo_inv_type = pseudo_inv_type self.anisotropy_scaling = anisotropy_scaling self.anisotropy_angle = anisotropy_angle self.enable_statistics = enable_statistics @@ -170,6 +174,8 @@ def fit(self, x, y, *args, **kwargs): weight=self.weight, verbose=self.verbose, exact_values=self.exact_values, + pseudo_inv=self.pseudo_inv, + pseudo_inv_type=self.pseudo_inv_type, ) add_setup = dict( anisotropy_scaling=self.anisotropy_scaling[0], @@ -319,6 +325,8 @@ def __init__( weight=False, verbose=False, exact_values=True, + pseudo_inv=False, + pseudo_inv_type="pinv", variogram_parameters=None, variogram_function=None, anisotropy_scaling=(1.0, 1.0), @@ -341,6 +349,8 @@ def __init__( n_closest_points=n_closest_points, verbose=verbose, exact_values=exact_values, + pseudo_inv=pseudo_inv, + pseudo_inv_type=pseudo_inv_type, variogram_parameters=variogram_parameters, variogram_function=variogram_function, anisotropy_scaling=anisotropy_scaling, From 464c203ea46469b73f31ec0e9c0bb7d6b0247e4e Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Wed, 19 Aug 2020 13:13:45 +0200 Subject: [PATCH 2/8] update examples --- examples/01_ordinary.py | 48 +++++++++++++++++++ examples/02_universal.py | 41 ++++++++++++++++ ...ols_covmodel.py => 03_gstools_covmodel.py} | 0 ...ige_geometric.py => 04_krige_geometric.py} | 31 +++++------- examples/{kriging_1D.py => 05_kriging_1D.py} | 0 ...le_1D.py => 06_exact_values_example_1D.py} | 2 +- ...riging2d.py => 07_regression_kriging2d.py} | 0 examples/{krige_cv.py => 08_krige_cv.py} | 0 ...ing_meuse.ipynb => 09_kriging_meuse.ipynb} | 0 examples/output.asc | 18 +++++++ 10 files changed, 121 insertions(+), 19 deletions(-) create mode 100755 examples/01_ordinary.py create mode 100755 examples/02_universal.py rename examples/{gstools_covmodel.py => 03_gstools_covmodel.py} (100%) rename examples/{krige_geometric.py => 04_krige_geometric.py} (75%) rename examples/{kriging_1D.py => 05_kriging_1D.py} (100%) rename examples/{exact_values_example_1D.py => 06_exact_values_example_1D.py} (98%) rename examples/{regression_kriging2d.py => 07_regression_kriging2d.py} (100%) rename examples/{krige_cv.py => 08_krige_cv.py} (100%) rename examples/{kriging_meuse.ipynb => 09_kriging_meuse.ipynb} (100%) create mode 100644 examples/output.asc diff --git a/examples/01_ordinary.py b/examples/01_ordinary.py new file mode 100755 index 0000000..f0cd162 --- /dev/null +++ b/examples/01_ordinary.py @@ -0,0 +1,48 @@ +""" +Ordinary Kriging Example +======================== + +First we will create a 2D dataset together with the associated x, y grids. + +""" + +import numpy as np +import pykrige.kriging_tools as kt +from pykrige.ok import OrdinaryKriging +import matplotlib.pyplot as plt + + +data = np.array([[0.3, 1.2, 0.47], + [1.9, 0.6, 0.56], + [1.1, 3.2, 0.74], + [3.3, 4.4, 1.47], + [4.7, 3.8, 1.74]]) + +gridx = np.arange(0.0, 5.5, 0.5) +gridy = np.arange(0.0, 5.5, 0.5) + +############################################################################### +# Create the ordinary kriging object. Required inputs are the X-coordinates of +# the data points, the Y-coordinates of the data points, and the Z-values of the +# data points. If no variogram model is specified, defaults to a linear variogram +# model. If no variogram model parameters are specified, then the code automatically +# calculates the parameters by fitting the variogram model to the binned +# experimental semivariogram. The verbose kwarg controls code talk-back, and +# the enable_plotting kwarg controls the display of the semivariogram. + +OK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear', + verbose=False, enable_plotting=False) + +############################################################################### +# Creates the kriged grid and the variance grid. Allows for kriging on a rectangular +# grid of points, on a masked rectangular grid of points, or with arbitrary points. +# (See OrdinaryKriging.__doc__ for more information.) + +z, ss = OK.execute('grid', gridx, gridy) + +############################################################################### +# Writes the kriged grid to an ASCII grid file and plot it. + +kt.write_asc_grid(gridx, gridy, z, filename="output.asc") +plt.imshow(z) +plt.show() diff --git a/examples/02_universal.py b/examples/02_universal.py new file mode 100755 index 0000000..ea59232 --- /dev/null +++ b/examples/02_universal.py @@ -0,0 +1,41 @@ +""" +Universal Kriging Example +========================= + +In this example we apply a regional linear trend to the kriging system. + +""" + +from pykrige.uk import UniversalKriging +import numpy as np +import matplotlib.pyplot as plt + + +data = np.array([[0.3, 1.2, 0.47], + [1.9, 0.6, 0.56], + [1.1, 3.2, 0.74], + [3.3, 4.4, 1.47], + [4.7, 3.8, 1.74]]) + +gridx = np.arange(0.0, 5.5, 0.5) +gridy = np.arange(0.0, 5.5, 0.5) + +############################################################################### +# Create the universal kriging object. Required inputs are the X-coordinates of +# the data points, the Y-coordinates of the data points, and the Z-values of the +# data points. Variogram is handled as in the ordinary kriging case. +# drift_terms is a list of the drift terms to include; currently supported terms +# are 'regional_linear', 'point_log', and 'external_Z'. Refer to +# UniversalKriging.__doc__ for more information. + +UK = UniversalKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear', + drift_terms=['regional_linear']) + +############################################################################### +# Creates the kriged grid and the variance grid. Allows for kriging on a rectangular +# grid of points, on a masked rectangular grid of points, or with arbitrary points. +# (See UniversalKriging.__doc__ for more information.) + +z, ss = UK.execute('grid', gridx, gridy) +plt.imshow(z) +plt.show() diff --git a/examples/gstools_covmodel.py b/examples/03_gstools_covmodel.py similarity index 100% rename from examples/gstools_covmodel.py rename to examples/03_gstools_covmodel.py diff --git a/examples/krige_geometric.py b/examples/04_krige_geometric.py similarity index 75% rename from examples/krige_geometric.py rename to examples/04_krige_geometric.py index 41732b1..0756b8b 100644 --- a/examples/krige_geometric.py +++ b/examples/04_krige_geometric.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ Geometric example ------------------- +================= A small example script showing the usage of the 'geographic' coordinates type for ordinary kriging on a sphere. @@ -9,6 +9,8 @@ from pykrige.ok import OrdinaryKriging import numpy as np +from matplotlib import pyplot as plt + # Make this example reproducible: np.random.seed(89239413) @@ -47,7 +49,9 @@ # Execute on grid: z2, ss2 = OK.execute("grid", grid_lon, grid_lat) +############################################################################### # Print data at equator (last longitude index will show periodicity): + print("Original data:") print("Longitude:", lon.astype(int)) print("Latitude: ", lat.astype(int)) @@ -60,26 +64,17 @@ print("Value: ", np.array_str(z2[5, :], precision=2)) print("Sigma²: ", np.array_str(ss2[5, :], precision=2)) -# ====================================OUTPUT================================== -# >>> Original data: -# >>> Longitude: [122 166 92 138 86 122 136] -# >>> Latitude: [-46 -36 -25 -73 -25 50 -29] -# >>> z: [ 2.75 3.36 2.24 3.07 3.37 5.25 2.82] -# >>> -# >>> Krige at 60° latitude: -# >>> ====================== -# >>> Longitude: [ 0. 60. 120. 180. 240. 300. 360.] -# >>> Value: [ 5.32 5.14 5.3 5.18 5.35 5.61 5.32] -# >>> Sigma²: [ 2.19 1.31 0.41 1.22 2.1 2.46 2.19] -# >>> -# >>> Ignoring curvature: -# >>> ===================== -# >>> Value: [ 4.55 4.72 5.25 4.82 4.61 4.53 4.48] -# >>> Sigma²: [ 3.77 1.99 0.39 1.84 3.52 5.43 7.5 ] -# +############################################################################### # We can see that the data point at longitude 122, latitude 50 correctly # dominates the kriged results, since it is the closest node in spherical # distance metric, as longitude differences scale with cos(latitude). # When kriging using longitude / latitude linearly, the value for grid points # with longitude values further away as longitude is now incorrectly # weighted equally as latitude. + +fig, (ax1, ax2) = plt.subplots(1, 2) +ax1.imshow(z1, extent=[0, 360, -90, 90]) +ax1.set_title("geo-coordinates") +ax2.imshow(z2, extent=[0, 360, -90, 90]) +ax2.set_title("non geo-coordinates") +plt.show() diff --git a/examples/kriging_1D.py b/examples/05_kriging_1D.py similarity index 100% rename from examples/kriging_1D.py rename to examples/05_kriging_1D.py diff --git a/examples/exact_values_example_1D.py b/examples/06_exact_values_example_1D.py similarity index 98% rename from examples/exact_values_example_1D.py rename to examples/06_exact_values_example_1D.py index 5ceeadc..715ac85 100644 --- a/examples/exact_values_example_1D.py +++ b/examples/06_exact_values_example_1D.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ Exact Values -================= +============ PyKrige demonstration and usage as a non-exact interpolator in 1D. diff --git a/examples/regression_kriging2d.py b/examples/07_regression_kriging2d.py similarity index 100% rename from examples/regression_kriging2d.py rename to examples/07_regression_kriging2d.py diff --git a/examples/krige_cv.py b/examples/08_krige_cv.py similarity index 100% rename from examples/krige_cv.py rename to examples/08_krige_cv.py diff --git a/examples/kriging_meuse.ipynb b/examples/09_kriging_meuse.ipynb similarity index 100% rename from examples/kriging_meuse.ipynb rename to examples/09_kriging_meuse.ipynb diff --git a/examples/output.asc b/examples/output.asc new file mode 100644 index 0000000..0f3d43b --- /dev/null +++ b/examples/output.asc @@ -0,0 +1,18 @@ +NCOLS 11 +NROWS 11 +XLLCENTER 0.00 +YLLCENTER 0.00 +DX 0.50 +DY 0.50 +NODATA_VALUE -999.00 +0.90 0.95 1.02 1.11 1.20 1.31 1.42 1.52 1.61 1.67 1.72 +0.83 0.89 0.96 1.05 1.15 1.27 1.40 1.51 1.61 1.69 1.73 +0.77 0.81 0.88 0.97 1.08 1.21 1.34 1.47 1.59 1.70 1.74 +0.70 0.74 0.79 0.88 1.00 1.13 1.27 1.40 1.52 1.64 1.69 +0.64 0.67 0.71 0.81 0.93 1.05 1.18 1.31 1.43 1.53 1.59 +0.58 0.61 0.66 0.75 0.85 0.97 1.09 1.21 1.32 1.42 1.48 +0.53 0.56 0.61 0.69 0.78 0.89 1.01 1.12 1.23 1.32 1.39 +0.49 0.51 0.56 0.62 0.71 0.81 0.92 1.04 1.14 1.23 1.30 +0.46 0.48 0.52 0.56 0.63 0.74 0.85 0.96 1.06 1.15 1.22 +0.45 0.46 0.49 0.53 0.58 0.69 0.80 0.90 1.00 1.08 1.15 +0.44 0.46 0.49 0.52 0.58 0.67 0.76 0.86 0.94 1.02 1.09 \ No newline at end of file From d821cc689881d5c6b479225192611b07f51dfc4a Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Wed, 19 Aug 2020 13:16:08 +0200 Subject: [PATCH 3/8] blacken examples --- examples/01_ordinary.py | 26 ++++++++++++++++++-------- examples/02_universal.py | 25 +++++++++++++++++-------- examples/07_regression_kriging2d.py | 22 +--------------------- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/examples/01_ordinary.py b/examples/01_ordinary.py index f0cd162..de15d1d 100755 --- a/examples/01_ordinary.py +++ b/examples/01_ordinary.py @@ -12,11 +12,15 @@ import matplotlib.pyplot as plt -data = np.array([[0.3, 1.2, 0.47], - [1.9, 0.6, 0.56], - [1.1, 3.2, 0.74], - [3.3, 4.4, 1.47], - [4.7, 3.8, 1.74]]) +data = np.array( + [ + [0.3, 1.2, 0.47], + [1.9, 0.6, 0.56], + [1.1, 3.2, 0.74], + [3.3, 4.4, 1.47], + [4.7, 3.8, 1.74], + ] +) gridx = np.arange(0.0, 5.5, 0.5) gridy = np.arange(0.0, 5.5, 0.5) @@ -30,15 +34,21 @@ # experimental semivariogram. The verbose kwarg controls code talk-back, and # the enable_plotting kwarg controls the display of the semivariogram. -OK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear', - verbose=False, enable_plotting=False) +OK = OrdinaryKriging( + data[:, 0], + data[:, 1], + data[:, 2], + variogram_model="linear", + verbose=False, + enable_plotting=False, +) ############################################################################### # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular # grid of points, on a masked rectangular grid of points, or with arbitrary points. # (See OrdinaryKriging.__doc__ for more information.) -z, ss = OK.execute('grid', gridx, gridy) +z, ss = OK.execute("grid", gridx, gridy) ############################################################################### # Writes the kriged grid to an ASCII grid file and plot it. diff --git a/examples/02_universal.py b/examples/02_universal.py index ea59232..3b5ba01 100755 --- a/examples/02_universal.py +++ b/examples/02_universal.py @@ -11,11 +11,15 @@ import matplotlib.pyplot as plt -data = np.array([[0.3, 1.2, 0.47], - [1.9, 0.6, 0.56], - [1.1, 3.2, 0.74], - [3.3, 4.4, 1.47], - [4.7, 3.8, 1.74]]) +data = np.array( + [ + [0.3, 1.2, 0.47], + [1.9, 0.6, 0.56], + [1.1, 3.2, 0.74], + [3.3, 4.4, 1.47], + [4.7, 3.8, 1.74], + ] +) gridx = np.arange(0.0, 5.5, 0.5) gridy = np.arange(0.0, 5.5, 0.5) @@ -28,14 +32,19 @@ # are 'regional_linear', 'point_log', and 'external_Z'. Refer to # UniversalKriging.__doc__ for more information. -UK = UniversalKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear', - drift_terms=['regional_linear']) +UK = UniversalKriging( + data[:, 0], + data[:, 1], + data[:, 2], + variogram_model="linear", + drift_terms=["regional_linear"], +) ############################################################################### # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular # grid of points, on a masked rectangular grid of points, or with arbitrary points. # (See UniversalKriging.__doc__ for more information.) -z, ss = UK.execute('grid', gridx, gridy) +z, ss = UK.execute("grid", gridx, gridy) plt.imshow(z) plt.show() diff --git a/examples/07_regression_kriging2d.py b/examples/07_regression_kriging2d.py index 46d25fd..19b927d 100644 --- a/examples/07_regression_kriging2d.py +++ b/examples/07_regression_kriging2d.py @@ -4,6 +4,7 @@ An example of regression kriging """ + import sys from sklearn.svm import SVR @@ -42,24 +43,3 @@ m_rk.fit(p_train, x_train, target_train) print("Regression Score: ", m_rk.regression_model.score(p_test, target_test)) print("RK score: ", m_rk.score(p_test, x_test, target_test)) - -# ====================================OUTPUT================================== - -# ======================================== -# regression model: -# Finished learning regression model -# Finished kriging residuals -# Regression Score: -0.034053855457 -# RK score: 0.66195576665 -# ======================================== -# regression model: -# Finished learning regression model -# Finished kriging residuals -# Regression Score: 0.699771164651 -# RK score: 0.737574040386 -# ======================================== -# regression model: -# Finished learning regression model -# Finished kriging residuals -# Regression Score: 0.527796839838 -# RK score: 0.604908933617 From 4132431f51afbb1e541a3d1da1c09f9bbba2b049 Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Wed, 19 Aug 2020 14:24:31 +0200 Subject: [PATCH 4/8] move examples from readme to gallery --- README.rst | 201 +++--------------- examples/{01_ordinary.py => 00_ordinary.py} | 0 examples/{02_universal.py => 01_universal.py} | 0 examples/02_kriging3D.py | 103 +++++++++ 4 files changed, 138 insertions(+), 166 deletions(-) rename examples/{01_ordinary.py => 00_ordinary.py} (100%) rename examples/{02_universal.py => 01_universal.py} (100%) create mode 100755 examples/02_kriging3D.py diff --git a/README.rst b/README.rst index 9812510..f1c3e20 100644 --- a/README.rst +++ b/README.rst @@ -60,176 +60,45 @@ If you use conda, PyKrige can be installed from the `conda-forge` channel with, conda install -c conda-forge pykrige -Ordinary Kriging Example -^^^^^^^^^^^^^^^^^^^^^^^^ +Features +^^^^^^^^ -First we will create a 2D dataset together with the associated x, y grids, - -.. code:: python - - import numpy as np - import pykrige.kriging_tools as kt - from pykrige.ok import OrdinaryKriging - - data = np.array([[0.3, 1.2, 0.47], - [1.9, 0.6, 0.56], - [1.1, 3.2, 0.74], - [3.3, 4.4, 1.47], - [4.7, 3.8, 1.74]]) - - gridx = np.arange(0.0, 5.5, 0.5) - gridy = np.arange(0.0, 5.5, 0.5) - - # Create the ordinary kriging object. Required inputs are the X-coordinates of - # the data points, the Y-coordinates of the data points, and the Z-values of the - # data points. If no variogram model is specified, defaults to a linear variogram - # model. If no variogram model parameters are specified, then the code automatically - # calculates the parameters by fitting the variogram model to the binned - # experimental semivariogram. The verbose kwarg controls code talk-back, and - # the enable_plotting kwarg controls the display of the semivariogram. - OK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear', - verbose=False, enable_plotting=False) - - # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular - # grid of points, on a masked rectangular grid of points, or with arbitrary points. - # (See OrdinaryKriging.__doc__ for more information.) - z, ss = OK.execute('grid', gridx, gridy) - - # Writes the kriged grid to an ASCII grid file. - kt.write_asc_grid(gridx, gridy, z, filename="output.asc") - - -Universal Kriging Example -^^^^^^^^^^^^^^^^^^^^^^^^^ - -.. code:: python - - from pykrige.uk import UniversalKriging - import numpy as np - - data = np.array([[0.3, 1.2, 0.47], - [1.9, 0.6, 0.56], - [1.1, 3.2, 0.74], - [3.3, 4.4, 1.47], - [4.7, 3.8, 1.74]]) - - gridx = np.arange(0.0, 5.5, 0.5) - gridy = np.arange(0.0, 5.5, 0.5) - - # Create the ordinary kriging object. Required inputs are the X-coordinates of - # the data points, the Y-coordinates of the data points, and the Z-values of the - # data points. Variogram is handled as in the ordinary kriging case. - # drift_terms is a list of the drift terms to include; currently supported terms - # are 'regional_linear', 'point_log', and 'external_Z'. Refer to - # UniversalKriging.__doc__ for more information. - UK = UniversalKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear', - drift_terms=['regional_linear']) - - # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular - # grid of points, on a masked rectangular grid of points, or with arbitrary points. - # (See UniversalKriging.__doc__ for more information.) - z, ss = UK.execute('grid', gridx, gridy) - - -Three-Dimensional Kriging Example -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -.. code:: python - - from pykrige.ok3d import OrdinaryKriging3D - from pykrige.uk3d import UniversalKriging3D - import numpy as np - - data = np.array([[0.1, 0.1, 0.3, 0.9], - [0.2, 0.1, 0.4, 0.8], - [0.1, 0.3, 0.1, 0.9], - [0.5, 0.4, 0.4, 0.5], - [0.3, 0.3, 0.2, 0.7]]) - - gridx = np.arange(0.0, 0.6, 0.05) - gridy = np.arange(0.0, 0.6, 0.01) - gridz = np.arange(0.0, 0.6, 0.1) - - # Create the 3D ordinary kriging object and solves for the three-dimension kriged - # volume and variance. Refer to OrdinaryKriging3D.__doc__ for more information. - ok3d = OrdinaryKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3], - variogram_model='linear') - k3d, ss3d = ok3d.execute('grid', gridx, gridy, gridz) - - # Create the 3D universal kriging object and solves for the three-dimension kriged - # volume and variance. Refer to UniversalKriging3D.__doc__ for more information. - uk3d = UniversalKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3], - variogram_model='linear', drift_terms=['regional_linear']) - k3d, ss3d = uk3d.execute('grid', gridx, gridy, gridz) - - # To use the generic 'specified' drift term, the user must provide the drift values - # at each data point and at every grid point. The following example is equivalent to - # using a linear drift in all three spatial dimensions. Refer to - # UniversalKriging3D.__doc__ for more information. - zg, yg, xg = np.meshgrid(gridz, gridy, gridx, indexing='ij') - uk3d = UniversalKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3], - variogram_model='linear', drift_terms=['specified'], - specified_drift=[data[:, 0], data[:, 1]]) - k3d, ss3d = uk3d.execute('grid', gridx, gridy, gridz, specified_drift_arrays=[xg, yg, zg]) - - # To use the generic 'functional' drift term, the user must provide a callable - # function that takes only the spatial dimensions as arguments. The following example - # is equivalent to using a linear drift only in the x-direction. Refer to - # UniversalKriging3D.__doc__ for more information. - func = lambda x, y, z: x - uk3d = UniversalKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3], - variogram_model='linear', drift_terms=['functional'], - functional_drift=[func]) - k3d, ss3d = uk3d.execute('grid', gridx, gridy, gridz) - - # Note that the use of the 'specified' and 'functional' generic drift capabilities is - # essentially identical in the two-dimensional universal kriging class (except for a - # difference in the number of spatial coordinates for the passed drift functions). - # See UniversalKriging.__doc__ for more information. - - -GSTools covariance models -^^^^^^^^^^^^^^^^^^^^^^^^^ - -You can also use `GSTools `_ -covariance models as input for the ``variogram_model`` in the -PyKrige routines: - -.. code:: python - - import numpy as np - from gstools import Gaussian - from pykrige.ok import OrdinaryKriging - from matplotlib import pyplot as plt - - # conditioning data - data = np.array([[0.3, 1.2, 0.47], - [1.9, 0.6, 0.56], - [1.1, 3.2, 0.74], - [3.3, 4.4, 1.47], - [4.7, 3.8, 1.74]]) - # grid definition for output field - gridx = np.arange(0.0, 5.5, 0.1) - gridy = np.arange(0.0, 6.5, 0.1) - # a GSTools based covariance model - cov_model = Gaussian(dim=2, len_scale=1, anis=0.2, angles=-0.5, var=0.5, nugget=0.1) - # ordinary kriging with pykrige - OK1 = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], cov_model) - z1, ss1 = OK1.execute('grid', gridx, gridy) - plt.imshow(z1, origin="lower") - plt.show() - -Which gives: - -.. image:: https://raw.githubusercontent.com/GeoStat-Framework/GSTools/master/docs/source/pics/20_pykrige.png - :width: 400px - :align: center -Have a look at the `documentation about the Covariance Model of GSTools `_. +Krigging algorithms +------------------- + + +.. autosummary:: + :toctree: ./generated/ + + pykrige.ok.OrdinaryKriging + pykrige.uk.UniversalKriging + pykrige.ok3d.OrdinaryKriging3D + pykrige.uk3d.UniversalKriging3D + pykrige.rk.RegressionKriging + + +Wrappers +-------- + +.. autosummary:: + :toctree: ./generated/ + + pykrige.rk.Krige + + +Tools +----- + +.. autosummary:: + :toctree: ./generated/ + + pykrige.kriging_tools.write_asc_grid + pykrige.kriging_tools.read_asc_grid Kriging Parameters Tuning -^^^^^^^^^^^^^^^^^^^^^^^^^ +------------------------- A scikit-learn compatible API for parameter tuning by cross-validation is exposed in `sklearn.model_selection.GridSearchCV `_. @@ -238,7 +107,7 @@ example for a more practical illustration. Regression Kriging -^^^^^^^^^^^^^^^^^^ +------------------ `Regression kriging `_ can be performed with `pykrige.rk.RegressionKriging `_. diff --git a/examples/01_ordinary.py b/examples/00_ordinary.py similarity index 100% rename from examples/01_ordinary.py rename to examples/00_ordinary.py diff --git a/examples/02_universal.py b/examples/01_universal.py similarity index 100% rename from examples/02_universal.py rename to examples/01_universal.py diff --git a/examples/02_kriging3D.py b/examples/02_kriging3D.py new file mode 100755 index 0000000..590ed2c --- /dev/null +++ b/examples/02_kriging3D.py @@ -0,0 +1,103 @@ +""" +Three-Dimensional Kriging Example +================================= + +""" + +from pykrige.ok3d import OrdinaryKriging3D +from pykrige.uk3d import UniversalKriging3D +import numpy as np +from matplotlib import pyplot as plt + + +data = np.array( + [ + [0.1, 0.1, 0.3, 0.9], + [0.2, 0.1, 0.4, 0.8], + [0.1, 0.3, 0.1, 0.9], + [0.5, 0.4, 0.4, 0.5], + [0.3, 0.3, 0.2, 0.7], + ] +) + +gridx = np.arange(0.0, 0.6, 0.05) +gridy = np.arange(0.0, 0.6, 0.01) +gridz = np.arange(0.0, 0.6, 0.1) + +############################################################################### +# Create the 3D ordinary kriging object and solves for the three-dimension kriged +# volume and variance. Refer to OrdinaryKriging3D.__doc__ for more information. + +ok3d = OrdinaryKriging3D( + data[:, 0], data[:, 1], data[:, 2], data[:, 3], variogram_model="linear" +) +k3d1, ss3d = ok3d.execute("grid", gridx, gridy, gridz) + +############################################################################### +# Create the 3D universal kriging object and solves for the three-dimension kriged +# volume and variance. Refer to UniversalKriging3D.__doc__ for more information. + +uk3d = UniversalKriging3D( + data[:, 0], + data[:, 1], + data[:, 2], + data[:, 3], + variogram_model="linear", + drift_terms=["regional_linear"], +) +k3d2, ss3d = uk3d.execute("grid", gridx, gridy, gridz) + +############################################################################### +# To use the generic 'specified' drift term, the user must provide the drift values +# at each data point and at every grid point. The following example is equivalent to +# using a linear drift in all three spatial dimensions. Refer to +# UniversalKriging3D.__doc__ for more information. + +zg, yg, xg = np.meshgrid(gridz, gridy, gridx, indexing="ij") +uk3d = UniversalKriging3D( + data[:, 0], + data[:, 1], + data[:, 2], + data[:, 3], + variogram_model="linear", + drift_terms=["specified"], + specified_drift=[data[:, 0], data[:, 1], data[:, 2]], +) +k3d3, ss3d = uk3d.execute( + "grid", gridx, gridy, gridz, specified_drift_arrays=[xg, yg, zg] +) + +############################################################################### +# To use the generic 'functional' drift term, the user must provide a callable +# function that takes only the spatial dimensions as arguments. The following example +# is equivalent to using a linear drift only in the x-direction. Refer to +# UniversalKriging3D.__doc__ for more information. + +func = lambda x, y, z: x +uk3d = UniversalKriging3D( + data[:, 0], + data[:, 1], + data[:, 2], + data[:, 3], + variogram_model="linear", + drift_terms=["functional"], + functional_drift=[func], +) +k3d4, ss3d = uk3d.execute("grid", gridx, gridy, gridz) + +############################################################################### +# Note that the use of the 'specified' and 'functional' generic drift capabilities is +# essentially identical in the two-dimensional universal kriging class (except for a +# difference in the number of spatial coordinates for the passed drift functions). +# See UniversalKriging.__doc__ for more information. + +fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) +ax1.imshow(k3d1[:, :, 0]) +ax1.set_title("ordinary kriging") +ax2.imshow(k3d2[:, :, 0]) +ax2.set_title("regional lin. drift") +ax3.imshow(k3d3[:, :, 0]) +ax3.set_title("specified drift") +ax4.imshow(k3d3[:, :, 0]) +ax4.set_title("functional drift") +plt.show() From 8fc2f54dd286848521977e14416a518127a705b0 Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Wed, 19 Aug 2020 14:25:09 +0200 Subject: [PATCH 5/8] update changelog for 1.5.1 --- CHANGELOG.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9930b3..76f7ba1 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,22 @@ Changelog ========= +Version 1.5.1 +------------- +*August 20, 2020* + +**New features** + +* update Regression Kriging class to be compatible with all kriging features (#158) +* added option to enable/disable "exact values" to all kriging routines (#153) +* added option to use the pseudo-inverse in all kriging routines (#151) + +**Changes** + +* removed compat-layer for sklearn (#157) +* updated examples in documentation + + Version 1.5.0 ------------- *April 04, 2020* From 722f822335f34fc1303be7efbd37360ca8efc16e Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Wed, 19 Aug 2020 14:49:28 +0200 Subject: [PATCH 6/8] update examples and readme --- README.rst | 33 +++++++++++++-------------------- examples/02_kriging3D.py | 11 ++++++----- examples/04_krige_geometric.py | 4 ++-- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/README.rst b/README.rst index f1c3e20..e2be898 100644 --- a/README.rst +++ b/README.rst @@ -26,6 +26,7 @@ PyKrige Kriging Toolkit for Python. + Purpose ^^^^^^^ @@ -39,7 +40,9 @@ point and all grid points. With the 'functional' drift capability, the user may of the spatial coordinates that define the drift(s). The package includes a module that contains functions that should be useful in working with ASCII grid files (`*.asc`). -See the documentation at `http://pykrige.readthedocs.io/ `_ for more details. +See the documentation at `http://pykrige.readthedocs.io/ `_ +for more details and examples. + Installation ^^^^^^^^^^^^ @@ -63,38 +66,27 @@ If you use conda, PyKrige can be installed from the `conda-forge` channel with, Features ^^^^^^^^ - Krigging algorithms ------------------- - -.. autosummary:: - :toctree: ./generated/ - - pykrige.ok.OrdinaryKriging - pykrige.uk.UniversalKriging - pykrige.ok3d.OrdinaryKriging3D - pykrige.uk3d.UniversalKriging3D - pykrige.rk.RegressionKriging +* ``OrdinaryKriging``: 2D ordinary kriging with estimated mean +* ``UniversalKriging``: 2D universal kriging providing drift terms +* ``OrdinaryKriging3D``: 3D ordinary kriging +* ``UniversalKriging3D``: 3D universal kriging +* ``RegressionKriging``: An implementation of Regression-Kriging Wrappers -------- -.. autosummary:: - :toctree: ./generated/ - - pykrige.rk.Krige +* ``rk.Krige``: A scikit-learn wrapper class for Ordinary and Universal Kriging Tools ----- -.. autosummary:: - :toctree: ./generated/ - - pykrige.kriging_tools.write_asc_grid - pykrige.kriging_tools.read_asc_grid +* ``kriging_tools.write_asc_grid``: Writes gridded data to ASCII grid file (*.asc) +* ``kriging_tools.read_asc_grid``: Reads ASCII grid file (*.asc) Kriging Parameters Tuning @@ -117,6 +109,7 @@ the ``OrdinaryKriging`` or the ``UniversalKriging`` class, and performs a correc A demonstration of the regression kriging is provided in the `corresponding example `_. + License ^^^^^^^ diff --git a/examples/02_kriging3D.py b/examples/02_kriging3D.py index 590ed2c..7cf14ea 100755 --- a/examples/02_kriging3D.py +++ b/examples/02_kriging3D.py @@ -91,13 +91,14 @@ # difference in the number of spatial coordinates for the passed drift functions). # See UniversalKriging.__doc__ for more information. -fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) -ax1.imshow(k3d1[:, :, 0]) +fig, (ax1, ax2, ax3, ax4) = plt.subplots(4) +ax1.imshow(k3d1[:, :, 0], origin="lower") ax1.set_title("ordinary kriging") -ax2.imshow(k3d2[:, :, 0]) +ax2.imshow(k3d2[:, :, 0], origin="lower") ax2.set_title("regional lin. drift") -ax3.imshow(k3d3[:, :, 0]) +ax3.imshow(k3d3[:, :, 0], origin="lower") ax3.set_title("specified drift") -ax4.imshow(k3d3[:, :, 0]) +ax4.imshow(k3d3[:, :, 0], origin="lower") ax4.set_title("functional drift") +plt.tight_layout() plt.show() diff --git a/examples/04_krige_geometric.py b/examples/04_krige_geometric.py index 0756b8b..f641ff4 100644 --- a/examples/04_krige_geometric.py +++ b/examples/04_krige_geometric.py @@ -73,8 +73,8 @@ # weighted equally as latitude. fig, (ax1, ax2) = plt.subplots(1, 2) -ax1.imshow(z1, extent=[0, 360, -90, 90]) +ax1.imshow(z1, extent=[0, 360, -90, 90], origin="lower") ax1.set_title("geo-coordinates") -ax2.imshow(z2, extent=[0, 360, -90, 90]) +ax2.imshow(z2, extent=[0, 360, -90, 90], origin="lower") ax2.set_title("non geo-coordinates") plt.show() From 252b56217859d1210be146c35be58e2b0bd21cda Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Wed, 19 Aug 2020 14:53:38 +0200 Subject: [PATCH 7/8] update readme doc links --- README.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index e2be898..c45e4e2 100644 --- a/README.rst +++ b/README.rst @@ -94,7 +94,7 @@ Kriging Parameters Tuning A scikit-learn compatible API for parameter tuning by cross-validation is exposed in `sklearn.model_selection.GridSearchCV `_. -See the `Krige CV `_ +See the `Krige CV `_ example for a more practical illustration. @@ -102,12 +102,12 @@ Regression Kriging ------------------ `Regression kriging `_ can be performed -with `pykrige.rk.RegressionKriging `_. +with `pykrige.rk.RegressionKriging `_. This class takes as parameters a scikit-learn regression model, and details of either either the ``OrdinaryKriging`` or the ``UniversalKriging`` class, and performs a correction steps on the ML regression prediction. A demonstration of the regression kriging is provided in the -`corresponding example `_. +`corresponding example `_. License From 72b9c9863d456ad11183f659a7df3e6fa16b2de7 Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Wed, 19 Aug 2020 15:07:57 +0200 Subject: [PATCH 8/8] update gitignore --- .gitignore | 1 + examples/output.asc | 18 ------------------ 2 files changed, 1 insertion(+), 18 deletions(-) delete mode 100644 examples/output.asc diff --git a/.gitignore b/.gitignore index 353c781..9a5248a 100644 --- a/.gitignore +++ b/.gitignore @@ -121,3 +121,4 @@ pykrige/_version.py *.vtr examples/meuse_example_data/* *.ipynb_checkpoints/* +examples/output.asc diff --git a/examples/output.asc b/examples/output.asc deleted file mode 100644 index 0f3d43b..0000000 --- a/examples/output.asc +++ /dev/null @@ -1,18 +0,0 @@ -NCOLS 11 -NROWS 11 -XLLCENTER 0.00 -YLLCENTER 0.00 -DX 0.50 -DY 0.50 -NODATA_VALUE -999.00 -0.90 0.95 1.02 1.11 1.20 1.31 1.42 1.52 1.61 1.67 1.72 -0.83 0.89 0.96 1.05 1.15 1.27 1.40 1.51 1.61 1.69 1.73 -0.77 0.81 0.88 0.97 1.08 1.21 1.34 1.47 1.59 1.70 1.74 -0.70 0.74 0.79 0.88 1.00 1.13 1.27 1.40 1.52 1.64 1.69 -0.64 0.67 0.71 0.81 0.93 1.05 1.18 1.31 1.43 1.53 1.59 -0.58 0.61 0.66 0.75 0.85 0.97 1.09 1.21 1.32 1.42 1.48 -0.53 0.56 0.61 0.69 0.78 0.89 1.01 1.12 1.23 1.32 1.39 -0.49 0.51 0.56 0.62 0.71 0.81 0.92 1.04 1.14 1.23 1.30 -0.46 0.48 0.52 0.56 0.63 0.74 0.85 0.96 1.06 1.15 1.22 -0.45 0.46 0.49 0.53 0.58 0.69 0.80 0.90 1.00 1.08 1.15 -0.44 0.46 0.49 0.52 0.58 0.67 0.76 0.86 0.94 1.02 1.09 \ No newline at end of file