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_sampling_1d(self):
x = np.linspace(0.0, 100.0, 21000)
rng = np.random.RandomState(1479373475)
field = rng.rand(len(x))
bins = np.arange(0, 100, 10)
bin_centres, gamma = vario_estimate_unstructured(
[x], field, bins, sampling_size=5000, sampling_seed=1479373475
)
var = 1.0 / 12.0
self.assertAlmostEqual(gamma[0], var, places=2)
self.assertAlmostEqual(gamma[len(gamma) // 2], var, places=2)
self.assertAlmostEqual(gamma[-1], var, places=2)
def test_sampling_3d(self):
x_c = np.linspace(0.0, 100.0, 100)
y_c = np.linspace(0.0, 100.0, 100)
z_c = np.linspace(0.0, 100.0, 100)
x, y, z = np.meshgrid(x_c, y_c, z_c)
x = np.reshape(x, len(x_c) * len(y_c) * len(z_c))
y = np.reshape(y, len(x_c) * len(y_c) * len(z_c))
z = np.reshape(z, len(x_c) * len(y_c) * len(z_c))
rng = np.random.RandomState(1479373475)
field = rng.rand(len(x))
bins = np.arange(0, 100, 10)
bin_centres, gamma = vario_estimate_unstructured(
(x, y, z),
field,
bins,
sampling_size=2000,
sampling_seed=1479373475,
)
var = 1.0 / 12.0
self.assertAlmostEqual(gamma[0], var, places=2)
self.assertAlmostEqual(gamma[len(gamma) // 2], var, places=2)
self.assertAlmostEqual(gamma[-1], var, places=2)
def test_uncorrelated_2d(self):
x_c = np.linspace(0.0, 100.0, 60)
y_c = np.linspace(0.0, 100.0, 60)
x, y = np.meshgrid(x_c, y_c)
x = np.reshape(x, len(x_c) * len(y_c))
y = np.reshape(y, len(x_c) * len(y_c))
rng = np.random.RandomState(1479373475)
field = rng.rand(len(x))
bins = np.arange(0, 100, 10)
bin_centres, gamma = vario_estimate_unstructured((x, y), field, bins)
var = 1.0 / 12.0
self.assertAlmostEqual(gamma[0], var, places=2)
self.assertAlmostEqual(gamma[len(gamma) // 2], var, places=2)
self.assertAlmostEqual(gamma[-1], var, places=2)
def test_np_int(self):
x = np.arange(1, 5, 1, dtype=np.int)
z = np.array((10, 20, 30, 40), dtype=np.int)
bins = np.arange(1, 11, 1, dtype=np.int)
bin_centres, gamma = vario_estimate_unstructured([x], z, bins)
self.assertAlmostEqual(gamma[0], 50.0, places=4)
def test_doubles(self):
x = np.arange(1, 11, 1, dtype=np.double)
z = np.array(
(41.2, 40.2, 39.7, 39.2, 40.1, 38.3, 39.1, 40.0, 41.1, 40.3),
dtype=np.double,
)
bins = np.arange(1, 11, 1, dtype=np.double)
bin_centres, gamma = vario_estimate_unstructured([x], z, bins)
self.assertAlmostEqual(gamma[0], 0.4917, places=4)
def test_1d(self):
x = np.arange(1, 11, 1, dtype=np.double)
# literature values
z = np.array(
(41.2, 40.2, 39.7, 39.2, 40.1, 38.3, 39.1, 40.0, 41.1, 40.3),
dtype=np.double,
)
bins = np.arange(1, 11, 1, dtype=np.double)
bin_centres, gamma = vario_estimate_unstructured([x], z, bins)
self.assertAlmostEqual(gamma[0], 0.4917, places=4)
self.assertAlmostEqual(gamma[1], 0.7625, places=4)
def test_ints(self):
x = np.arange(1, 5, 1, dtype=int)
z = np.array((10, 20, 30, 40), dtype=int)
bins = np.arange(1, 11, 1, dtype=int)
bin_centres, gamma = vario_estimate_unstructured([x], z, bins)
self.assertAlmostEqual(gamma[0], 50.0, places=4)
from matplotlib import pyplot as plt
###############################################################################
# Generate a synthetic field with an exponential model.
x = np.random.RandomState(19970221).rand(1000) * 100.0
y = np.random.RandomState(20011012).rand(1000) * 100.0
model = gs.Exponential(dim=2, var=2, len_scale=8)
srf = gs.SRF(model, mean=0, seed=19970221)
field = srf((x, y))
###############################################################################
# Estimate the variogram of the field with 40 bins and plot the result.
bins = np.arange(40)
bin_center, gamma = gs.vario_estimate_unstructured((x, y), field, bins)
plt.scatter(bin_center, gamma, label="data")
ax = plt.gca()
###############################################################################
# Define a set of models to test.
models = {
"gaussian": gs.Gaussian,
"exponential": gs.Exponential,
"matern": gs.Matern,
"stable": gs.Stable,
"rational": gs.Rational,
"linear": gs.Linear,
"circular": gs.Circular,
"spherical": gs.Spherical,
}
# Finally, everything is ready for the variogram estimation. For the unstructured
# method, we have to define the bins on which the variogram will be estimated.
# Through expert knowledge (i.e. fiddling around), we assume that the main
# features of the variogram will be below 10 metres distance. And because the
# data has a high spatial resolution, the resolution of the bins can also be
# high. The transmissivity data is still defined on a structured grid, but we can
# simply flatten it with :any:`numpy.ndarray.flatten`, in order to bring it into
# the right shape. It might be more memory efficient to use
# ``herten_log_trans.reshape(-1)``, but for better readability, we will stick to
# :any:`numpy.ndarray.flatten`. Taking all data points into account would take a
# very long time (expert knowledge \*wink\*), thus we will only take 2000 datapoints into account, which are sampled randomly. In order to make the exact
# results reproducible, we can also set a seed.
bins = np.linspace(0, 10, 50)
bin_center, gamma = gs.vario_estimate_unstructured(
(x_u, y_u),
herten_log_trans.reshape(-1),
bins,
sampling_size=2000,
sampling_seed=19920516,
)
###############################################################################
# The estimated variogram is calculated on the centre of the given bins,
# therefore, the ``bin_center`` array is also returned.
###############################################################################
# Fitting the Variogram
# ^^^^^^^^^^^^^^^^^^^^^
#
# Now, we can see, if the estimated variogram can be modelled by a common