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_ok3d_update_variogram_model(sample_data_3d):
data, (gridx_ref, gridy_ref, gridz_ref), mask_ref = sample_data_3d
with pytest.raises(ValueError):
OrdinaryKriging3D(
data[:, 0], data[:, 1], data[:, 2], data[:, 3], variogram_model="blurg"
)
k3d = OrdinaryKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3])
variogram_model = k3d.variogram_model
variogram_parameters = k3d.variogram_model_parameters
anisotropy_scaling_y = k3d.anisotropy_scaling_y
anisotropy_scaling_z = k3d.anisotropy_scaling_z
anisotropy_angle_x = k3d.anisotropy_angle_x
anisotropy_angle_y = k3d.anisotropy_angle_y
anisotropy_angle_z = k3d.anisotropy_angle_z
with pytest.raises(ValueError):
k3d.update_variogram_model("blurg")
k3d.update_variogram_model(
"power",
anisotropy_scaling_y=3.0,
anisotropy_scaling_z=3.0,
anisotropy_angle_x=45.0,
anisotropy_angle_y=45.0,
assert_allclose(k[0, :, :], 0.0, atol=0.01)
assert_allclose(k[1, :, :], 1.0, rtol=1.0e-2)
assert_allclose(k[2, :, :], 2.0, rtol=1.0e-2)
assert_allclose(k[3, :, :], 3.0, rtol=1.0e-2)
k, ss = k3d.execute(
"grid",
np.arange(2.0, 3.0, 0.1),
np.arange(2.0, 3.0, 0.1),
np.arange(0.0, 4.0, 1.0),
backend="loop",
)
assert_allclose(k[0, :, :], 0.0, atol=0.01)
assert_allclose(k[1, :, :], 1.0, rtol=1.0e-2)
assert_allclose(k[2, :, :], 2.0, rtol=1.0e-2)
assert_allclose(k[3, :, :], 3.0, rtol=1.0e-2)
k3d = OrdinaryKriging3D(
data[:, 0], data[:, 1], data[:, 2], data[:, 3], variogram_model="linear"
)
k, ss = k3d.execute(
"points",
[2.5, 2.5, 2.5],
[2.5, 2.5, 2.5],
[1.0, 2.0, 3.0],
backend="vectorized",
)
assert_allclose(k[0], 1.0, atol=0.01)
assert_allclose(k[1], 2.0, rtol=1.0e-2)
assert_allclose(k[2], 3.0, rtol=1.0e-2)
k, ss = k3d.execute(
"points", [2.5, 2.5, 2.5], [2.5, 2.5, 2.5], [1.0, 2.0, 3.0], backend="loop"
)
assert_allclose(k[0], 1.0, atol=0.01)
)
uk_v, uss_v = uk3d.execute(
"grid", gridx_ref, gridy_ref, np.array([0.0]), backend="vectorized"
)
assert_allclose(uk_v, ok_v)
uk_l, uss_l = uk3d.execute(
"grid", gridx_ref, gridy_ref, np.array([0.0]), backend="loop"
)
assert_allclose(uk_l, ok_l)
assert_allclose(uk_l, uk_v)
assert_allclose(uss_l, uss_v)
data = np.genfromtxt(
os.path.join(BASE_DIR, "test_data", "test3d_data.txt"), skip_header=1
)
ok3d = OrdinaryKriging3D(
data[:, 0],
data[:, 1],
data[:, 2],
data[:, 3],
variogram_model="linear",
variogram_parameters=[1.0, 0.1],
)
ok_v, oss_v = ok3d.execute(
"grid", np.arange(10.0), np.arange(10.0), np.arange(10.0), backend="vectorized"
)
ok_l, oss_l = ok3d.execute(
"grid", np.arange(10.0), np.arange(10.0), np.arange(10.0), backend="loop"
)
uk3d = UniversalKriging3D(
data[:, 0],
)
k, ss = k3d.execute(
"grid", gridx_ref, gridy_ref, np.array([0.0]), backend="vectorized"
)
assert_allclose(np.squeeze(k), ok_test_answer)
k, ss = k3d.execute("grid", gridx_ref, gridy_ref, np.array([0.0]), backend="loop")
assert_allclose(np.squeeze(k), ok_test_answer)
# Test to compare K3D results to those obtained using KT3D.
data = np.genfromtxt(
os.path.join(BASE_DIR, "test_data", "test3d_data.txt"), skip_header=1
)
ans = np.genfromtxt(os.path.join(BASE_DIR, "test_data", "test3d_answer.txt"))
ans_z = ans[:, 0].reshape((10, 10, 10))
ans_ss = ans[:, 1].reshape((10, 10, 10))
k3d = OrdinaryKriging3D(
data[:, 0],
data[:, 1],
data[:, 2],
data[:, 3],
variogram_model="linear",
variogram_parameters=[1.0, 0.1],
)
k, ss = k3d.execute(
"grid", np.arange(10.0), np.arange(10.0), np.arange(10.0), backend="vectorized"
)
assert_allclose(k, ans_z, rtol=1e-3, atol=1e-8)
assert_allclose(ss, ans_ss, rtol=1e-3, atol=1e-8)
k3d = OrdinaryKriging3D(
data[:, 0],
data[:, 1],
data[:, 2],
def test_ok3d_backends_produce_same_result(sample_data_3d):
data, (gridx_ref, gridy_ref, gridz_ref), mask_ref = sample_data_3d
k3d = OrdinaryKriging3D(
data[:, 0], data[:, 1], data[:, 2], data[:, 3], variogram_model="linear"
)
k_k3d_v, ss_k3d_v = k3d.execute(
"grid", gridx_ref, gridy_ref, gridz_ref, backend="vectorized"
)
k_k3d_l, ss_k3d_l = k3d.execute(
"grid", gridx_ref, gridy_ref, gridz_ref, backend="loop"
)
assert_allclose(k_k3d_v, k_k3d_l, rtol=1e-05, atol=1e-8)
assert_allclose(ss_k3d_v, ss_k3d_l, rtol=1e-05, atol=1e-8)
def test_ok3d(validation_ref):
data, (ok_test_answer, gridx_ref, gridy_ref), _ = validation_ref
# Test to compare K3D results to those obtained using KT3D_H2O.
# (M. Karanovic, M. Tonkin, and D. Wilson, 2009, Groundwater, vol. 47,
# no. 4, 580-586.)
k3d = OrdinaryKriging3D(
data[:, 0],
data[:, 1],
np.zeros(data[:, 1].shape),
data[:, 2],
variogram_model="exponential",
variogram_parameters=[500.0, 3000.0, 0.0],
)
k, ss = k3d.execute(
"grid", gridx_ref, gridy_ref, np.array([0.0]), backend="vectorized"
)
assert_allclose(np.squeeze(k), ok_test_answer)
k, ss = k3d.execute("grid", gridx_ref, gridy_ref, np.array([0.0]), backend="loop")
assert_allclose(np.squeeze(k), ok_test_answer)
# Test to compare K3D results to those obtained using KT3D.
data = np.genfromtxt(
def test_force_exact_3d(sample_data_3d):
data, (gridx_ref, gridy_ref, gridz_ref), mask_ref = sample_data_3d
k3d = OrdinaryKriging3D(
data[:, 0], data[:, 1], data[:, 2], data[:, 3], variogram_model="linear"
)
k, ss = k3d.execute(
"grid", [0.1, 0.2, 0.3], [0.1, 0.2, 0.3], [0.1, 0.2, 0.3], backend="vectorized"
)
assert k[2, 0, 0] == approx(0.9)
assert ss[2, 0, 0] == approx(0.0)
assert k[0, 2, 0] == approx(0.9)
assert ss[0, 2, 0] == approx(0.0)
assert k[1, 2, 2] == approx(0.7)
assert ss[1, 2, 2] == approx(0.0)
assert ss[2, 2, 2] != approx(0.0)
assert ss[0, 0, 0] != approx(0.0)
k, ss = k3d.execute(
"grid", [0.1, 0.2, 0.3], [0.1, 0.2, 0.3], [0.1, 0.2, 0.3], backend="loop"
def test_ok3d_update_variogram_model(sample_data_3d):
data, (gridx_ref, gridy_ref, gridz_ref), mask_ref = sample_data_3d
with pytest.raises(ValueError):
OrdinaryKriging3D(
data[:, 0], data[:, 1], data[:, 2], data[:, 3], variogram_model="blurg"
)
k3d = OrdinaryKriging3D(data[:, 0], data[:, 1], data[:, 2], data[:, 3])
variogram_model = k3d.variogram_model
variogram_parameters = k3d.variogram_model_parameters
anisotropy_scaling_y = k3d.anisotropy_scaling_y
anisotropy_scaling_z = k3d.anisotropy_scaling_z
anisotropy_angle_x = k3d.anisotropy_angle_x
anisotropy_angle_y = k3d.anisotropy_angle_y
anisotropy_angle_z = k3d.anisotropy_angle_z
with pytest.raises(ValueError):
k3d.update_variogram_model("blurg")
k3d.update_variogram_model(
"power",
def execute(self, points, *args, **kwargs):
# TODO array of Points, (x, y) pairs of shape (N, 2)
"""
Parameters
----------
points: dict
Returns:
-------
Prediction array
Variance array
"""
if isinstance(self.model, OrdinaryKriging) or isinstance(
self.model, OrdinaryKriging3D
):
prediction, variance = self.model.execute(
"points",
n_closest_points=self.n_closest_points,
backend="loop",
**points
)
else:
print("n_closest_points will be ignored for UniversalKriging")
prediction, variance = self.model.execute(
"points", backend="loop", **points
)
return prediction, variance