Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_simple(self):
for Model in self.cov_models:
for dim in self.dims:
model = Model(
dim=dim,
var=2,
len_scale=2,
anis=[0.9, 0.8],
angles=[2, 1, 0.5],
)
simple = krige.Simple(
model, self.cond_pos[:dim], self.cond_val, self.mean
)
field_1, __ = simple.unstructured(self.grids[dim - 1])
field_1 = field_1.reshape(self.grid_shape[:dim])
field_2, __ = simple.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
)
import numpy as np
from gstools import Gaussian, krige
import matplotlib.pyplot as plt
# condtions
cond_pos = [0.3, 1.9, 1.1, 3.3, 4.7]
cond_val = [0.47, 0.56, 0.74, 1.47, 1.74]
# resulting grid
gridx = np.linspace(0.0, 15.0, 151)
###############################################################################
# spatial random field class
model = Gaussian(dim=1, var=0.5, len_scale=2)
###############################################################################
kr1 = krige.Simple(model=model, mean=1, cond_pos=cond_pos, cond_val=cond_val)
kr2 = krige.Ordinary(model=model, cond_pos=cond_pos, cond_val=cond_val)
kr1(gridx)
kr2(gridx)
###############################################################################
plt.plot(gridx, kr1.field, label="simple kriged field")
plt.plot(gridx, kr2.field, label="ordinary kriged field")
plt.scatter(cond_pos, cond_val, color="k", zorder=10, label="Conditions")
plt.legend()
plt.show()
The mean of the field has to be given beforehand.
"""
import numpy as np
from gstools import Gaussian, krige
# condtions
cond_pos = [0.3, 1.9, 1.1, 3.3, 4.7]
cond_val = [0.47, 0.56, 0.74, 1.47, 1.74]
# resulting grid
gridx = np.linspace(0.0, 15.0, 151)
# spatial random field class
model = Gaussian(dim=1, var=0.5, len_scale=2)
###############################################################################
krig = krige.Simple(model, mean=1, 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()
krige_sk = Simple(
model=srf.model,
mean=srf.mean,
cond_pos=srf.cond_pos,
cond_val=srf.cond_val,
)
krige_field, krige_var = krige_sk(srf.pos, srf.mesh_type)
# evaluate the field at the conditional points
x, y, z = pos2xyz(srf.cond_pos, max_dim=srf.model.dim)
if srf.model.do_rotation:
x, y, z = unrotate_mesh(srf.model.dim, srf.model.angles, x, y, z)
y, z = make_isotropic(srf.model.dim, srf.model.anis, y, z)
err_data = srf.generator.__call__(x, y, z, "unstructured") + srf.mean
err_sk = Simple(
model=srf.model,
mean=srf.mean,
cond_pos=srf.cond_pos,
cond_val=err_data,
)
err_field, __ = err_sk(srf.pos, srf.mesh_type)
cond_field = srf.raw_field + krige_field - err_field + srf.mean
info = {}
return cond_field, krige_field, err_field, krige_var, info
The spatial random field class containing all information
Returns
-------
cond_field : :class:`numpy.ndarray`
the conditioned field
krige_field : :class:`numpy.ndarray`
the kriged field
err_field : :class:`numpy.ndarray`
the error field to set the given random field to zero at the conditions
krige_var : :class:`numpy.ndarray`
the variance of the kriged field
"""
if srf._value_type != "scalar":
raise ValueError("Conditioned SRF: only scalar fields allowed.")
krige_sk = Simple(
model=srf.model,
mean=srf.mean,
cond_pos=srf.cond_pos,
cond_val=srf.cond_val,
)
krige_field, krige_var = krige_sk(srf.pos, srf.mesh_type)
# evaluate the field at the conditional points
x, y, z = pos2xyz(srf.cond_pos, max_dim=srf.model.dim)
if srf.model.do_rotation:
x, y, z = unrotate_mesh(srf.model.dim, srf.model.angles, x, y, z)
y, z = make_isotropic(srf.model.dim, srf.model.anis, y, z)
err_data = srf.generator.__call__(x, y, z, "unstructured") + srf.mean
err_sk = Simple(
model=srf.model,