Skip to content

Commit db07202

Browse files
Daniel-Mrth
authored andcommitted
Added a method to obtain the variogram model points (#94)
1 parent 6a892d4 commit db07202

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

pykrige/ok.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,23 @@ def display_variogram_model(self):
404404
self.lags), 'k-')
405405
plt.show()
406406

407+
def get_variogram_points(self):
408+
"""Returns both the lags and the variogram function evaluated at each
409+
of them.
410+
411+
The evaluation of the variogram function and the lags are produced
412+
internally. This method is convenient when the user wants to access to
413+
the lags and the resulting variogram (according to the model provided)
414+
for further analysis.
415+
416+
Returns
417+
-------
418+
(tuple) tuple containing:
419+
lags (array) - the lags at which the variogram was evaluated
420+
variogram (array) - the variogram function evaluated at the lags
421+
"""
422+
return self.lags, self.variogram_function(self.variogram_model_parameters, self.lags)
423+
407424
def switch_verbose(self):
408425
"""Allows user to switch code talk-back on/off. Takes no arguments."""
409426
self.verbose = not self.verbose

pykrige/tests/test_core.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,32 @@ def test_ok_update_variogram_model(validation_ref):
395395
assert anisotropy_angle != ok.anisotropy_angle
396396

397397

398+
def test_ok_get_variogram_points(validation_ref):
399+
# Test to compare the variogram of OK results to those obtained using
400+
# KT3D_H2O.
401+
# (M. Karanovic, M. Tonkin, and D. Wilson, 2009, Groundwater,
402+
# vol. 47, no. 4, 580-586.)
403+
404+
# Variogram parameters
405+
_variogram_parameters = [500.0, 3000.0, 0.0]
406+
407+
data, _, (ok_test_answer, gridx, gridy) = validation_ref
408+
409+
ok = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2],
410+
variogram_model='exponential',
411+
variogram_parameters=_variogram_parameters)
412+
413+
# Get the variogram points from the UniversalKriging instance
414+
lags, calculated_variogram = ok.get_variogram_points()
415+
416+
# Generate the expected variogram points according to the
417+
# exponential variogram model
418+
expected_variogram = variogram_models.exponential_variogram_model(
419+
_variogram_parameters, lags)
420+
421+
assert_allclose(calculated_variogram, expected_variogram)
422+
423+
398424
def test_ok_execute(sample_data_2d):
399425

400426
data, (gridx, gridy, _), mask_ref = sample_data_2d
@@ -544,6 +570,33 @@ def test_uk_update_variogram_model(sample_data_2d):
544570
assert anisotropy_angle != uk.anisotropy_angle
545571

546572

573+
def test_uk_get_variogram_points(validation_ref):
574+
# Test to compare the variogram of UK with linear drift to results from
575+
# KT3D_H2O.
576+
# (M. Karanovic, M. Tonkin, and D. Wilson, 2009, Groundwater,
577+
# vol. 47, no. 4, 580-586.)
578+
579+
# Variogram parameters
580+
_variogram_parameters = [500.0, 3000.0, 0.0]
581+
582+
data, _, (uk_test_answer, gridx, gridy) = validation_ref
583+
584+
uk = UniversalKriging(data[:, 0], data[:, 1], data[:, 2],
585+
variogram_model='exponential',
586+
variogram_parameters=_variogram_parameters,
587+
drift_terms=['regional_linear'])
588+
589+
# Get the variogram points from the UniversalKriging instance
590+
lags, calculated_variogram = uk.get_variogram_points()
591+
592+
# Generate the expected variogram points according to the
593+
# exponential variogram model
594+
expected_variogram = variogram_models.exponential_variogram_model(
595+
_variogram_parameters, lags)
596+
597+
assert_allclose(calculated_variogram, expected_variogram)
598+
599+
547600
def test_uk_calculate_data_point_zscalars(sample_data_2d):
548601

549602
data, (gridx, gridy, _), mask_ref = sample_data_2d

pykrige/uk.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,23 @@ def display_variogram_model(self):
615615
self.lags), 'k-')
616616
plt.show()
617617

618+
def get_variogram_points(self):
619+
"""Returns both the lags and the variogram function evaluated at each
620+
of them.
621+
622+
The evaluation of the variogram function and the lags are produced
623+
internally. This method is convenient when the user wants to access to
624+
the lags and the resulting variogram (according to the model provided)
625+
for further analysis.
626+
627+
Returns
628+
-------
629+
(tuple) tuple containing:
630+
lags (array) - the lags at which the variogram was evaluated
631+
variogram (array) - the variogram function evaluated at the lags
632+
"""
633+
return self.lags, self.variogram_function(self.variogram_model_parameters, self.lags)
634+
618635
def switch_verbose(self):
619636
"""Allows user to switch code talk-back on/off. Takes no arguments."""
620637
self.verbose = not self.verbose

0 commit comments

Comments
 (0)