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_creation(self):
with self.assertRaises(TypeError):
CovModel()
class User(CovModel):
def cor(self, h):
return np.exp(-h ** 2)
user = User(len_scale=2)
self.assertAlmostEqual(user.correlation(1), np.exp(-0.25))
for Model in self.cov_models:
for dim in self.dims:
for angles in self.angles:
for nugget in self.nuggets:
for len_scale, anis in zip(self.lens, self.anis):
model = Model(
dim=dim, len_scale=len_scale, angles=angles
)
def test_creation(self):
with self.assertRaises(TypeError):
CovModel()
class User(CovModel):
def cor(self, h):
return np.exp(-h ** 2)
user = User(len_scale=2)
self.assertAlmostEqual(user.correlation(1), np.exp(-0.25))
for Model in self.cov_models:
for dim in self.dims:
for angles in self.angles:
for nugget in self.nuggets:
for len_scale, anis in zip(self.lens, self.anis):
model = Model(
dim=dim, len_scale=len_scale, angles=angles
)
model1 = Model(
dim=dim, len_scale=10, anis=anis, angles=angles
Additional Parameters
=====================
Let's pimp our self-defined model ``Gau`` from the introductory example
by setting the exponent as an additional parameter:
.. math::
\rho(r) := \exp\left(-\left(\frac{r}{\ell}\right)^{\alpha}\right)
This leads to the so called **stable** covariance model and we can define it by
"""
import numpy as np
import gstools as gs
class Stab(gs.CovModel):
def default_opt_arg(self):
return {"alpha": 1.5}
def cor(self, h):
return np.exp(-h ** self.alpha)
###############################################################################
# As you can see, we override the method :any:`CovModel.default_opt_arg`
# to provide a standard value for the optional argument ``alpha``.
# We can access it in the correlation function by ``self.alpha``
#
# Now we can instantiate this model by either setting alpha implicitly with
# the default value or explicitly:
model1 = Stab(dim=2, var=2.0, len_scale=10)
model = Gau(dim=3, var=2.0, len_scale=10)
print(model.integral_scale)
print(model.integral_scale_vec)
model = Gau(dim=3, var=2.0, integral_scale=[10, 4, 2])
print(model.anis)
print(model.len_scale)
print(model.len_scale_vec)
print(model.integral_scale)
print(model.integral_scale_vec)
model = Gau(dim=3, var=2.0, len_scale=10)
print(model.percentile_scale(0.9))
class Stab(CovModel):
def default_opt_arg(self):
return {"alpha": 1.5}
def correlation(self, r):
return np.exp(-(r / self.len_scale) ** self.alpha)
model1 = Stab(dim=2, var=2.0, len_scale=10)
model2 = Stab(dim=2, var=2.0, len_scale=10, alpha=0.5)
print(model1)
print(model2)
# data
x = [1.0, 3.0, 5.0, 7.0, 9.0, 11.0]
y = [0.2, 0.5, 0.6, 0.8, 0.8, 0.9]
# fitting model
"""
Fitting variogram data
======================
The model class comes with a routine to fit the model-parameters to given
variogram data. In the following we will use the self defined stable model
from a previous example.
"""
import numpy as np
import gstools as gs
class Stab(gs.CovModel):
def default_opt_arg(self):
return {"alpha": 1.5}
def cor(self, h):
return np.exp(-h ** self.alpha)
# Exemplary variogram data (e.g. estimated from field observations)
bins = [1.0, 3.0, 5.0, 7.0, 9.0, 11.0]
est_vario = [0.2, 0.5, 0.6, 0.8, 0.8, 0.9]
# fitting model
model = Stab(dim=2)
# we have to provide boundaries for the parameters
model.set_arg_bounds(alpha=[0, 3])
results, pcov = model.fit_variogram(bins, est_vario, nugget=False)
print("Results:", results)
from gstools import CovModel
import numpy as np
# use CovModel as the base-class
class Gau(CovModel):
def correlation(self, r):
return np.exp(-(r / self.len_scale) ** 2)
model = Gau(dim=2, var=2.0, len_scale=10)
model.plot()
print(model.dim, model.var, model.len_scale, model.nugget, model.sill)
model.dim = 3
model.var = 1
model.len_scale = 15
model.nugget = 0.1
print(model.dim, model.var, model.len_scale, model.nugget, model.sill)
model = Gau(dim=3, var=2.0, len_scale=10, anis=0.5)
def gstools_cov_model(variogram, **kwargs):
"""GSTools Interface
Pass a :class:`skgstat.Variogram` instance.
Returns an **already fitted** variogram model
inherited from `gstools.CovModel`. All
kwargs passed will be passed to
`gstools.CovModel`.
"""
# extract the fitted variogram model
fitted_model = variogram.fitted_model
# define the CovModel
class VariogramModel(gstools.CovModel):
def variogram(self, r):
if isinstance(r, np.ndarray):
return fitted_model(r.flatten()).reshape(r.shape)
else:
return fitted_model(r)
# dim can be infered from variogram
if 'dim' not in kwargs.keys():
kwargs['dim'] = variogram.coordinates.ndim
# Create the instance
model = VariogramModel(**kwargs)
model.fit_variogram(variogram.bins, variogram.experimental)
return model
Let us start with a short example of a self defined model (Of course, we
provide a lot of predefined models [See: :any:`gstools.covmodel`],
but they all work the same way).
Therefore we reimplement the Gaussian covariance model
by defining just the "normalized"
`correlation `_
function:
"""
import numpy as np
import gstools as gs
# use CovModel as the base-class
class Gau(gs.CovModel):
def cor(self, h):
return np.exp(-(h ** 2))
###############################################################################
# Here the parameter ``h`` stands for the normalized range ``r / len_scale``.
# Now we can instantiate this model:
model = Gau(dim=2, var=2.0, len_scale=10)
###############################################################################
# To have a look at the variogram, let's plot it:
model.plot()
###############################################################################