Skip to content

Commit fe9b745

Browse files
authored
Merge pull request #162 from GeoStat-Framework/variogram_normalizer_return
return fitted normalizer
2 parents 79e7b60 + 1cbc516 commit fe9b745

3 files changed

Lines changed: 28 additions & 16 deletions

File tree

gstools/normalizer/tools.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def apply_mean_norm_trend(
7777
7878
Returns
7979
-------
80-
:class:`numpy.ndarray`
80+
field : :class:`numpy.ndarray`
8181
The transformed field.
8282
"""
8383
normalizer = _check_normalizer(normalizer)
@@ -153,8 +153,11 @@ def remove_trend_norm_mean(
153153
154154
Returns
155155
-------
156-
:class:`numpy.ndarray`
156+
field : :class:`numpy.ndarray`
157157
The cleaned field.
158+
normalizer : :any:`Normalizer`, optional
159+
The fitted normalizer for the given data.
160+
Only provided if `fit_normalizer` is True.
158161
"""
159162
normalizer = _check_normalizer(normalizer)
160163
if check_shape:
@@ -179,4 +182,5 @@ def remove_trend_norm_mean(
179182
field = normalizer.normalize(field)
180183
for i in range(field_cnt):
181184
field[i] -= eval_func(mean, pos, dim, mesh_type, value_type, True)
182-
return field if stacked else field[0]
185+
out = field if stacked else field[0]
186+
return (out, normalizer) if fit_normalizer else out

gstools/variogram/variogram.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ def vario_estimate(
131131
same points, with the same statistical properties.
132132
bin_edges : :class:`numpy.ndarray`, optional
133133
the bins on which the variogram will be calculated.
134-
If :any:`None` are given, standard bins provided by the :any:`standard_bins`
135-
routine will be used. Default: :any:`None`
134+
If :any:`None` are given, standard bins provided by the
135+
:any:`standard_bins` routine will be used. Default: :any:`None`
136136
sampling_size : :class:`int` or :any:`None`, optional
137137
for large input data, this method can take a long
138138
time to compute the variogram, therefore this argument specifies
@@ -217,11 +217,18 @@ def vario_estimate(
217217
218218
Returns
219219
-------
220-
:class:`tuple` of :class:`numpy.ndarray`
221-
1. the bin centers
222-
2. the estimated variogram values at bin centers
223-
3. (optional) the number of points found at each bin center
224-
(see argument return_counts)
220+
bin_center : (n), :class:`numpy.ndarray`
221+
The bin centers.
222+
gamma : (n) or (d, n), :class:`numpy.ndarray`
223+
The estimated variogram values at bin centers.
224+
Is stacked if multiple `directions` (d>1) are given.
225+
counts : (n) or (d, n), :class:`numpy.ndarray`, optional
226+
The number of point pairs found for each bin.
227+
Is stacked if multiple `directions` (d>1) are given.
228+
Only provided if `return_counts` is True.
229+
normalizer : :any:`Normalizer`, optional
230+
The fitted normalizer for the given data.
231+
Only provided if `fit_normalizer` is True.
225232
226233
Notes
227234
-----
@@ -321,12 +328,14 @@ def vario_estimate(
321328
bin_edges = standard_bins(pos, dim, latlon)
322329
bin_centres = (bin_edges[:-1] + bin_edges[1:]) / 2.0
323330
# normalize field
324-
field = remove_trend_norm_mean(
331+
norm_field_out = remove_trend_norm_mean(
325332
*(pos, field, mean, normalizer, trend),
326333
check_shape=False,
327334
stacked=True,
328335
fit_normalizer=fit_normalizer,
329336
)
337+
field = norm_field_out[0] if fit_normalizer else norm_field_out
338+
norm_out = (norm_field_out[1],) if fit_normalizer else ()
330339
# select variogram estimator
331340
cython_estimator = _set_estimator(estimator)
332341
# run
@@ -355,9 +364,8 @@ def vario_estimate(
355364
)
356365
if dir_no == 1:
357366
estimates, counts = estimates[0], counts[0]
358-
if return_counts:
359-
return bin_centres, estimates, counts
360-
return bin_centres, estimates
367+
est_out = (estimates, counts)
368+
return (bin_centres,) + est_out[: 2 if return_counts else 1] + norm_out
361369

362370

363371
def vario_estimate_axis(

tests/test_normalize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,14 @@ def test_auto_fit(self):
204204
places=4,
205205
)
206206
# test fitting during vario estimate
207-
emp_vario = gs.vario_estimate(
207+
bin_center, gamma, normalizer = gs.vario_estimate(
208208
cond_pos,
209209
cond_val,
210210
normalizer=gs.normalizer.BoxCox,
211211
fit_normalizer=True,
212212
)
213213
model = gs.Stable(dim=2)
214-
model.fit_variogram(*emp_vario)
214+
model.fit_variogram(bin_center, gamma)
215215
self.assertAlmostEqual(model.var, 0.6426670183, places=4)
216216
self.assertAlmostEqual(model.len_scale, 9.635193952, places=4)
217217
self.assertAlmostEqual(model.nugget, 0.001617908408, places=4)

0 commit comments

Comments
 (0)