Skip to content

Commit

Permalink
minor fixes in fitting; unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaelicke committed Nov 1, 2018
1 parent 949d909 commit ebae4ef
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 9 deletions.
25 changes: 16 additions & 9 deletions skgstat/Variogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,14 @@ def __init__(self,
# specify if the experimental variogram shall be harmonized
self.harmonize = harmonize

# set if nugget effect shall be used
self.use_nugget = use_nugget

# set the fitting method and sigma array
self.fit_method = fit_method
self._fit_sigma = None
self.fit_sigma = fit_sigma

# set if nugget effect shall be used
self.use_nugget = use_nugget

# set attributes to be filled during calculation
self.cov = None
Expand Down Expand Up @@ -614,23 +615,25 @@ def fit_sigma(self):

# discrete uncertainties
elif isinstance(self._fit_sigma, (list, tuple, np.ndarray)):
assert len(self._fit_sigma) == len(self.bins)
return self._fit_sigma
if len(self._fit_sigma) == len(self._bins):
return self._fit_sigma
else:
raise AttributeError('fit_sigma and bins need the same length.')

# linear function of distance
elif self.fit_sigma == 'linear':
elif self._fit_sigma == 'linear':
return self.bins / np.max(self.bins)

# e function of distance
elif self.fit_sigma == 'exp':
return np.exp(1. / (self.bins / np.max(self.bins)))
elif self._fit_sigma == 'exp':
return 1. / np.exp(1. / (self.bins / np.max(self.bins)))

# sqrt function of distance
elif self.fit_sigma == 'sqrt':
elif self._fit_sigma == 'sqrt':
return np.sqrt(self.bins / np.max(self.bins))

# squared function of distance
elif self.fit_sigma == 'sq':
elif self._fit_sigma == 'sq':
return (self.bins / np.max(self.bins)) ** 2
else:
raise ValueError("fit_sigma is not understood. It has to be an " +
Expand All @@ -640,6 +643,10 @@ def fit_sigma(self):
def fit_sigma(self, sigma):
self._fit_sigma = sigma

# remove fitting parameters
self.cof = None
self.cov = None

def lag_groups(self):
"""Lag class groups
Expand Down
103 changes: 103 additions & 0 deletions skgstat/tests/Variogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,109 @@ def test_maxlag_setting_as_max_ratio(self):
self.assertAlmostEqual(V.maxlag, 25.38, places=2)


class TestVariogramFittingProcedure(unittest.TestCase):
def setUp(self):
np.random.seed(1337)
self.c = np.random.gamma(10, 8, (50, 3))
np.random.seed(1337)
self.v = np.random.normal(10, 4, 50)

# build a standard variogram to be used
self.V = Variogram(self.c, self.v, n_lags=5, use_nugget=True)

def test_fit_sigma_is_None(self):
self.V.fit_sigma = None

self.assertIsNone(self.V.fit_sigma)

def test_fit_sigma_explicit(self):
sigs = [.8, .5, 2., 2., 5.]
self.V.fit_sigma = sigs

for x, y in zip(sigs, self.V.fit_sigma):
self.assertEqual(x, y)

# test parameter estimated
self.V.fit()
assert_array_almost_equal(
self.V.parameters,
[3035.357, 318.608, 18.464], decimal=3
)

def test_fit_sigma_raises_AttributeError(self):
self.V.fit_sigma = (0, 1, 2)

with self.assertRaises(AttributeError) as e:
self.V.fit_sigma
self.assertEqual(
str(e),
'fit_sigma and bins need the same length.'
)

def test_fit_sigma_raises_ValueError(self):
self.V.fit_sigma = 'notAnFunction'

with self.assertRaises(ValueError) as e:
self.V.fit_sigma
self.assertEqual(
str(e),
"fit_sigma is not understood. It has to be an array or" +
"one of ['linear', 'exp', 'sqrt', 'sq']."
)

def test_fit_sigma_linear(self):
self.V.fit_sigma = 'linear'

# test the sigmas
sigma = self.V.fit_sigma
for s, _s in zip(sigma, [.2, .4, .6, .8, 1.]):
self.assertAlmostEqual(s, _s, places=8)

# test parameters:
self.V.fit()
assert_array_almost_equal(
self.V.parameters, [3170.532, 324.385, 17.247], decimal=3
)

def test_fit_sigma_exp(self):
self.V.fit_sigma = 'exp'

# test the sigmas
sigma = self.V.fit_sigma
for s, _s in zip(sigma, [0.0067, 0.0821, 0.1889, 0.2865, 0.3679]):
self.assertAlmostEqual(s, _s, places=4)

# test parameters
assert_array_almost_equal(
self.V.parameters, [3195.6, 329.8, 17.9], decimal=1
)

def test_fit_sigma_sqrt(self):
self.V.fit_sigma = 'sqrt'

# test the sigmas
assert_array_almost_equal(
self.V.fit_sigma, [0.447, 0.632, 0.775, 0.894, 1.], decimal=3
)

# test the parameters
assert_array_almost_equal(
self.V.parameters, [2902.1, 315.2, 18.3], decimal=1
)

def test_fit_sigma_sq(self):
self.V.fit_sigma = 'sq'

# test the sigmas
assert_array_almost_equal(
self.V.fit_sigma, [0.04, 0.16, 0.36, 0.64, 1.], decimal=2
)

# test the parameters
assert_array_almost_equal(
self.V.parameters, [3195., 328.9, 17.8], decimal=1
)


class TestVariogramQaulityMeasures(unittest.TestCase):
def setUp(self):
Expand Down
1 change: 1 addition & 0 deletions skgstat/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from skgstat.tests.Variogram import (
TestVariogramInstatiation,
TestVariogramArguments,
TestVariogramFittingProcedure,
TestVariogramQaulityMeasures,
TestVariogramPlots,
)
Expand Down

0 comments on commit ebae4ef

Please sign in to comment.