def test_ordinary(self): for Model in self.cov_models: model = Model(dim=1, var=0.5, len_scale=2, anis=[0.1, 1], angles=[0.5, 0, 0]) ordinary = krige.Ordinary(model, self.cond_pos[0], self.cond_val) field_1, __ = ordinary.unstructured(self.pos[0]) field_2, __ = ordinary.structured(self.pos[0]) for i, val in enumerate(self.cond_val): self.assertAlmostEqual(val, field_1[i], places=2) self.assertAlmostEqual(val, field_2[(i, )], places=2) for dim in self.dims[1:]: model = Model( dim=dim, var=0.5, len_scale=2, anis=[0.1, 1], angles=[0.5, 0, 0], ) ordinary = krige.Ordinary(model, self.cond_pos[:dim], self.cond_val) field_1, __ = ordinary.unstructured(self.pos[:dim]) field_2, __ = ordinary.structured(self.pos[:dim]) for i, val in enumerate(self.cond_val): self.assertAlmostEqual(val, field_1[i], places=2) self.assertAlmostEqual(val, field_2[dim * (i, )], places=2)
def test_ordinary(self): for trend_func in [None, trend]: for Model in self.cov_models: for dim in self.dims: model = Model( dim=dim, var=5, len_scale=10, anis=[0.9, 0.8], angles=[2, 1, 0.5], ) ordinary = krige.Ordinary(model, self.cond_pos[:dim], self.cond_val, trend_func) field_1, __ = ordinary.unstructured(self.grids[dim - 1]) field_1 = field_1.reshape(self.grid_shape[:dim]) field_2, __ = ordinary.structured(self.pos[:dim]) self.assertAlmostEqual(np.max(np.abs(field_1 - field_2)), 0.0, places=2) for i, val in enumerate(self.cond_val): self.assertAlmostEqual(field_1[self.data_idx[:dim]][i], val, places=2)
def test_error(self): for Model in self.cov_models: for dim in self.dims: model = Model( dim=dim, var=5, len_scale=10, nugget=0.1, anis=[0.9, 0.8], angles=[2, 1, 0.5], ) ordinary = krige.Ordinary( model, self.cond_pos[:dim], self.cond_val, exact=False, cond_err=self.cond_err, ) field, err = ordinary(self.cond_pos[:dim]) # when the given measurement error is 0, the kriging-var # should equal the nugget of the model self.assertAlmostEqual(err[1], model.nugget, places=2) self.assertAlmostEqual(err[4], model.nugget, places=2)
from gstools import Gaussian, krige 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() # ordinary kriging with gstools for comparison OK2 = krige.Ordinary(cov_model, [data[:, 0], data[:, 1]], data[:, 2]) OK2.structured([gridx, gridy]) OK2.plot()
inverse for the kriging matrix, which is enabled by default. This will result in the average value for the redundant data. Example ^^^^^^^ In the following we have two different values at the same location. The resulting kriging field will hold the average at this point. """ import numpy as np from gstools import Gaussian, krige # condtions cond_pos = [0.3, 1.9, 1.1, 3.3, 1.1] cond_val = [0.47, 0.56, 0.74, 1.47, 1.14] # resulting grid gridx = np.linspace(0.0, 8.0, 81) # spatial random field class model = Gaussian(dim=1, var=0.5, len_scale=1) ############################################################################### krig = krige.Ordinary(model, cond_pos=cond_pos, cond_val=cond_val) krig(gridx) ############################################################################### ax = krig.plot() ax.scatter(cond_pos, cond_val, color="k", zorder=10, label="Conditions") ax.legend()
""" Detrended Ordinary Kriging -------------------------- """ import numpy as np from gstools import SRF, Gaussian, krige def trend(x): """Example for a simple linear trend.""" return x * 0.1 + 1 # synthetic condtions with trend/drift drift_model = Gaussian(dim=1, var=0.1, len_scale=2) drift = SRF(drift_model, seed=101) cond_pos = np.linspace(0.1, 8, 10) cond_val = drift(cond_pos) + trend(cond_pos) # resulting grid gridx = np.linspace(0.0, 15.0, 151) drift_field = drift(gridx) + trend(gridx) # kriging model = Gaussian(dim=1, var=0.1, len_scale=2) krig_trend = krige.Ordinary(model, cond_pos, cond_val, trend) krig_trend(gridx) ax = krig_trend.plot() ax.scatter(cond_pos, cond_val, color="k", zorder=10, label="Conditions") ax.plot(gridx, trend(gridx), ":", label="linear trend") ax.plot(gridx, drift_field, "--", label="original field") ax.legend()
plt.figure(figsize=(10, 5)) plt.plot(bin_center, gamma) plot_variogram(fit_model, x_max=bins[-1], ax=plt.gca()) plt.xlabel("Lag Distance") plt.ylabel("Variogram") plt.show() ############################################################################### # Performing the Kriging # ^^^^^^^^^^^^^^^^^^^^^^ # Then we pass the fitted exponential model when instantiating the kriging operator from GSTools. # Create the kriging model krig = krige.Ordinary( fit_model, project["Observed Temperature"].points.T, project["Observed Temperature"]["temperature (C)"], ) ############################################################################### # After instantiating the kriging operator, we can have it operate on the nodes of our 3D grid that we created earlier and collect the results back onto the grid. krig.mesh( grid, name="temperature (C)", ) ############################################################################### # And now the `grid` model has the temperature scalar field and kriging variance as data arrays. project["Kriged Temperature Model"] = grid project