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)
Ejemplo n.º 2
0
 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 fit_model_var(coords_df, x_c, y_c, values):
    # fit the model varigrom to the experimental variogram
    bin_num = 40  # number of bins in the experimental variogram

    # first get the coords and determine distances
    x_delta = max(x_c) - min(x_c)  # distance across x coords
    y_delta = max(y_c) - min(y_c)  # distance across y coords
    max_dist = np.sqrt(
        np.square(x_delta +
                  y_delta)) / 4  # assume correlated over 1/4 of distance

    # setup bins for the variogram
    bins_c = np.linspace(0, max_dist,
                         bin_num)  # bin edges in variogram, bin_num of bins

    # compute the experimental variogram
    bin_cent_c, gamma_c = gs.vario_estimate_unstructured((x_c, y_c), values,
                                                         bins_c)
    # bin_center_c is the "lag" of the bin, gamma_c is the value

    gamma_c = smooth(
        gamma_c, 5
    )  # smooth the experimental variogram to help fitting - moving window with 5 samples

    # fit the model variogram
    # potential models are: Gaussian, Exponential, Matern, Rational, Stable, Linear,
    #                       Circular, Spherical, and Intersection
    fit_var = gs.Stable(dim=2)
    fit_var.fit_variogram(bin_cent_c,
                          gamma_c,
                          nugget=False,
                          estimator='cressie')  # fit the model variogram

    # print('max distance', max_dist,'var len scale', fit_var.len_scale)
    # check to see of range of varigram is very small (problem with fit), it so, set it to a value
    # also check to see if it is too long and set it to a value
    if fit_var.len_scale < max_dist / 3:  # check if too short
        fit_var.len_scale = max_dist / 3  # set to value
        # print('too short, set len_scale to: ', max_dist/3)
    elif fit_var.len_scale > max_dist * 1.5:  # check if too long
        fit_var.len_scale = max_dist  # set to value
        # print('too long, set len_scale to: ', max_dist)

    plt_var = False
    if plt_var:
        # plot the variogram to show fit and print out variogram paramters
        # should make it save to pdf file
        ax1 = fit_var.plot(x_max=max_dist)  # plot model variogram
        ax1.plot(bin_cent_c, gamma_c)  # plot experimental variogram
        # krig_plots.savefig()
        plt.close('all')
    return fit_var
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
import numpy as np
from gstools import SRF, Exponential, Stable, vario_estimate_unstructured

# 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 = Exponential(dim=2, var=2, len_scale=8)
srf = SRF(model, mean=0, seed=19970221)
field = srf((x, y))
# estimate the variogram of the field with 40 bins
bins = np.arange(40)
bin_center, gamma = vario_estimate_unstructured((x, y), field, bins)
# fit the variogram with a stable model. (no nugget fitted)
fit_model = Stable(dim=2)
fit_model.fit_variogram(bin_center, gamma, nugget=False)
# output
ax = fit_model.plot(x_max=40)
ax.plot(bin_center, gamma)
print(fit_model)
Ejemplo n.º 7
0
pt.imshow(herten_log_trans.T, origin='lower', aspect='equal')
pt.show()

# create an unstructured grid for the variogram estimation
x_u, y_u = create_unstructured_grid(x_s, y_s)

###############################################################################
# estimate the variogram on an unstructured grid ##############################
###############################################################################

bins = np.linspace(0, 10, 50)
print('Estimating unstructured variogram')
bin_center, gamma = vario_estimate_unstructured(
    (x_u, y_u),
    herten_log_trans.reshape(-1),
    bins,
    sampling_size=2000,
    sampling_seed=19920516,
)

# fit an exponential model
fit_model = Exponential(dim=2)
fit_model.fit_variogram(bin_center, gamma, nugget=False)

pt.plot(bin_center, gamma)
plot_variogram(fit_model, x_max=bins[-1])

###############################################################################
# estimate the variogram on a structured grid #################################
###############################################################################
Ejemplo n.º 8
0
 def test_list(self):
     x = np.arange(1, 11, 1, dtype=np.double)
     z = [41.2, 40.2, 39.7, 39.2, 40.1, 38.3, 39.1, 40.0, 41.1, 40.3]
     bins = np.arange(1, 11, 1, dtype=np.double)
     bin_centres, gamma = vario_estimate_unstructured([x], z, bins)
     self.assertAlmostEqual(gamma[1], 0.7625, places=4)
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
    (303509.4197523619, 4279629.689766085, 8053.049483835099),
    (336316.405356571, 4261479.748583805, -1756.358124546427),
    (0.22299463811939535, -0.11978828465250713, 0.9674317331109259),
]
p.show()

###############################################################################
# Variogram Analysis
# ^^^^^^^^^^^^^^^^^^
#
# Next, we need to compute a variogram for the temperature probe data and fit that variogram to an exponential model

bins = np.linspace(0, 12300, 1000)
bin_center, gamma = vario_estimate_unstructured(
    project["Observed Temperature"].points.T,
    project["Observed Temperature"]["temperature (C)"],
    bins,
)

fit_model = Exponential(dim=3)
fit_model.fit_variogram(bin_center, gamma, nugget=False)

plt.figure(figsize=(10, 5))
plt.plot(bin_center, gamma)
plot_variogram(fit_model, x_max=bins[-1], ax=plt.gca())
plt.xlabel("Lag Distance")
plt.ylabel("Variogram")
plt.show()

###############################################################################
# Performing the Kriging