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_directions_3d(self):
x = np.linspace(0.0, 10.0, 20)
y = np.linspace(0.0, 15.0, 25)
z = np.linspace(0.0, 20.0, 30)
rng = np.random.RandomState(1479373475)
x_rand = rng.rand(len(x))
y_rand = rng.rand(len(y))
z_rand = rng.rand(len(z))
field_x = np.tile(x_rand.reshape((len(x), 1, 1)), (1, len(y), len(z)))
field_y = np.tile(y_rand.reshape((1, len(y), 1)), (len(x), 1, len(z)))
field_z = np.tile(z_rand.reshape((1, 1, len(z))), (len(x), len(y), 1))
gamma_x_x = variogram.vario_estimate_structured(field_x, direction="x")
gamma_x_y = variogram.vario_estimate_structured(field_x, direction="y")
gamma_x_z = variogram.vario_estimate_structured(field_x, direction="z")
gamma_y_x = variogram.vario_estimate_structured(field_y, direction="x")
gamma_y_y = variogram.vario_estimate_structured(field_y, direction="y")
gamma_y_z = variogram.vario_estimate_structured(field_y, direction="z")
gamma_z_x = variogram.vario_estimate_structured(field_z, direction="x")
gamma_z_y = variogram.vario_estimate_structured(field_z, direction="y")
gamma_z_z = variogram.vario_estimate_structured(field_z, direction="z")
self.assertAlmostEqual(gamma_x_y[1], 0.0)
self.assertAlmostEqual(gamma_x_y[len(gamma_x_y) // 2], 0.0)
self.assertAlmostEqual(gamma_x_y[-1], 0.0)
self.assertAlmostEqual(gamma_x_z[1], 0.0)
self.assertAlmostEqual(gamma_x_z[len(gamma_x_y) // 2], 0.0)
self.assertAlmostEqual(gamma_x_z[-1], 0.0)
def test_directions_3d(self):
x = np.linspace(0.0, 10.0, 20)
y = np.linspace(0.0, 15.0, 25)
z = np.linspace(0.0, 20.0, 30)
rng = np.random.RandomState(1479373475)
x_rand = rng.rand(len(x))
y_rand = rng.rand(len(y))
z_rand = rng.rand(len(z))
field_x = np.tile(x_rand.reshape((len(x), 1, 1)), (1, len(y), len(z)))
field_y = np.tile(y_rand.reshape((1, len(y), 1)), (len(x), 1, len(z)))
field_z = np.tile(z_rand.reshape((1, 1, len(z))), (len(x), len(y), 1))
gamma_x_x = variogram.vario_estimate_structured(field_x, direction="x")
gamma_x_y = variogram.vario_estimate_structured(field_x, direction="y")
gamma_x_z = variogram.vario_estimate_structured(field_x, direction="z")
gamma_y_x = variogram.vario_estimate_structured(field_y, direction="x")
gamma_y_y = variogram.vario_estimate_structured(field_y, direction="y")
gamma_y_z = variogram.vario_estimate_structured(field_y, direction="z")
gamma_z_x = variogram.vario_estimate_structured(field_z, direction="x")
gamma_z_y = variogram.vario_estimate_structured(field_z, direction="y")
gamma_z_z = variogram.vario_estimate_structured(field_z, direction="z")
self.assertAlmostEqual(gamma_x_y[1], 0.0)
self.assertAlmostEqual(gamma_x_y[len(gamma_x_y) // 2], 0.0)
self.assertAlmostEqual(gamma_x_y[-1], 0.0)
self.assertAlmostEqual(gamma_x_z[1], 0.0)
self.assertAlmostEqual(gamma_x_z[len(gamma_x_y) // 2], 0.0)
def test_directions_2d(self):
x = np.linspace(0.0, 20.0, 100)
y = np.linspace(0.0, 15.0, 80)
rng = np.random.RandomState(1479373475)
x_rand = rng.rand(len(x))
y_rand = rng.rand(len(y))
# random values repeated along y-axis
field_x = np.tile(x_rand, (len(y), 1)).T
# random values repeated along x-axis
field_y = np.tile(y_rand, (len(x), 1))
gamma_x_x = variogram.vario_estimate_structured(field_x, direction="x")
gamma_x_y = variogram.vario_estimate_structured(field_x, direction="y")
gamma_y_x = variogram.vario_estimate_structured(field_y, direction="x")
gamma_y_y = variogram.vario_estimate_structured(field_y, direction="y")
self.assertAlmostEqual(gamma_x_y[1], 0.0)
self.assertAlmostEqual(gamma_x_y[len(gamma_x_y) // 2], 0.0)
self.assertAlmostEqual(gamma_x_y[-1], 0.0)
self.assertAlmostEqual(gamma_y_x[1], 0.0)
self.assertAlmostEqual(gamma_y_x[len(gamma_x_y) // 2], 0.0)
self.assertAlmostEqual(gamma_y_x[-1], 0.0)
gamma_x = variogram.vario_estimate_structured(field_ma, direction="x")
gamma_y = variogram.vario_estimate_structured(field_ma, direction="y")
var = 1.0 / 12.0
self.assertAlmostEqual(gamma_x[0], 0.0, places=2)
self.assertAlmostEqual(gamma_x[len(gamma_x) // 2], var, places=2)
self.assertAlmostEqual(gamma_x[-1], var, places=2)
self.assertAlmostEqual(gamma_y[0], 0.0, places=2)
self.assertAlmostEqual(gamma_y[len(gamma_y) // 2], var, places=2)
self.assertAlmostEqual(gamma_y[-1], var, places=2)
mask = np.zeros_like(field)
mask[0, 0] = 1
field = np.ma.masked_array(field, mask=mask)
gamma_x = variogram.vario_estimate_structured(field_ma, direction="x")
gamma_y = variogram.vario_estimate_structured(field_ma, direction="y")
self.assertAlmostEqual(gamma_x[0], 0.0, places=2)
self.assertAlmostEqual(gamma_y[0], 0.0, places=2)
def test_masked_2d(self):
rng = np.random.RandomState(1479373475)
field = rng.rand(80, 60)
mask = np.zeros_like(field)
field_ma = np.ma.masked_array(field, mask=mask)
gamma_x = variogram.vario_estimate_structured(field_ma, direction="x")
gamma_y = variogram.vario_estimate_structured(field_ma, direction="y")
var = 1.0 / 12.0
self.assertAlmostEqual(gamma_x[0], 0.0, places=2)
self.assertAlmostEqual(gamma_x[len(gamma_x) // 2], var, places=2)
self.assertAlmostEqual(gamma_x[-1], var, places=2)
self.assertAlmostEqual(gamma_y[0], 0.0, places=2)
self.assertAlmostEqual(gamma_y[len(gamma_y) // 2], var, places=2)
self.assertAlmostEqual(gamma_y[-1], var, places=2)
mask = np.zeros_like(field)
mask[0, 0] = 1
field = np.ma.masked_array(field, mask=mask)
gamma_x = variogram.vario_estimate_structured(field_ma, direction="x")
gamma_y = variogram.vario_estimate_structured(field_ma, direction="y")
self.assertAlmostEqual(gamma_x[0], 0.0, places=2)
self.assertAlmostEqual(gamma_y[0], 0.0, places=2)
z = np.linspace(0.0, 20.0, 30)
rng = np.random.RandomState(1479373475)
x_rand = rng.rand(len(x))
y_rand = rng.rand(len(y))
z_rand = rng.rand(len(z))
field_x = np.tile(x_rand.reshape((len(x), 1, 1)), (1, len(y), len(z)))
field_y = np.tile(y_rand.reshape((1, len(y), 1)), (len(x), 1, len(z)))
field_z = np.tile(z_rand.reshape((1, 1, len(z))), (len(x), len(y), 1))
gamma_x_x = variogram.vario_estimate_structured(field_x, direction="x")
gamma_x_y = variogram.vario_estimate_structured(field_x, direction="y")
gamma_x_z = variogram.vario_estimate_structured(field_x, direction="z")
gamma_y_x = variogram.vario_estimate_structured(field_y, direction="x")
gamma_y_y = variogram.vario_estimate_structured(field_y, direction="y")
gamma_y_z = variogram.vario_estimate_structured(field_y, direction="z")
gamma_z_x = variogram.vario_estimate_structured(field_z, direction="x")
gamma_z_y = variogram.vario_estimate_structured(field_z, direction="y")
gamma_z_z = variogram.vario_estimate_structured(field_z, direction="z")
self.assertAlmostEqual(gamma_x_y[1], 0.0)
self.assertAlmostEqual(gamma_x_y[len(gamma_x_y) // 2], 0.0)
self.assertAlmostEqual(gamma_x_y[-1], 0.0)
self.assertAlmostEqual(gamma_x_z[1], 0.0)
self.assertAlmostEqual(gamma_x_z[len(gamma_x_y) // 2], 0.0)
self.assertAlmostEqual(gamma_x_z[-1], 0.0)
self.assertAlmostEqual(gamma_y_x[1], 0.0)
self.assertAlmostEqual(gamma_y_x[len(gamma_x_y) // 2], 0.0)
self.assertAlmostEqual(gamma_y_x[-1], 0.0)
self.assertAlmostEqual(gamma_y_z[1], 0.0)
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_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 = variogram.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)