-
Notifications
You must be signed in to change notification settings - Fork 74
/
base.py
1285 lines (1088 loc) · 42.9 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
GStools subpackage providing the base class for covariance models.
.. currentmodule:: gstools.covmodel.base
The following classes are provided
.. autosummary::
CovModel
"""
# pylint: disable=C0103, R0201
import warnings
import copy
import numpy as np
from scipy.integrate import quad as integral
from scipy.optimize import root
from hankel import SymmetricFourierTransform as SFT
from gstools.field.tools import make_isotropic, unrotate_mesh
from gstools.tools.geometric import pos2xyz
from gstools.covmodel.tools import (
InitSubclassMeta,
rad_fac,
set_len_anis,
set_angles,
check_bounds,
check_arg_in_bounds,
default_arg_from_bounds,
)
from gstools.covmodel import plot
from gstools.covmodel.fit import fit_variogram
__all__ = ["CovModel"]
# default arguments for hankel.SymmetricFourierTransform
HANKEL_DEFAULT = {"a": -1, "b": 1, "N": 200, "h": 0.001, "alt": True}
class AttributeWarning(UserWarning):
pass
# The CovModel Base-Class #####################################################
class CovModel(metaclass=InitSubclassMeta):
r"""Base class for the GSTools covariance models.
Parameters
----------
dim : :class:`int`, optional
dimension of the model. Default: ``3``
var : :class:`float`, optional
variance of the model (the nugget is not included in "this" variance)
Default: ``1.0``
len_scale : :class:`float` or :class:`list`, optional
length scale of the model.
If a single value is given, the same length-scale will be used for
every direction. If multiple values (for main and transversal
directions) are given, `anis` will be
recalculated accordingly. If only two values are given in 3D,
the latter one will be used for both transversal directions.
Default: ``1.0``
nugget : :class:`float`, optional
nugget of the model. Default: ``0.0``
anis : :class:`float` or :class:`list`, optional
anisotropy ratios in the transversal directions [e_y, e_z].
* e_y = l_y / l_x
* e_z = l_z / l_x
If only one value is given in 3D, e_y will be set to 1.
This value will be ignored, if multiple len_scales are given.
Default: ``1.0``
angles : :class:`float` or :class:`list`, optional
angles of rotation (given in rad):
* in 2D: given as rotation around z-axis
* in 3D: given by yaw, pitch, and roll (known as Tait–Bryan angles)
Default: ``0.0``
integral_scale : :class:`float` or :class:`list` or :any:`None`, optional
If given, ``len_scale`` will be ignored and recalculated,
so that the integral scale of the model matches the given one.
Default: ``None``
var_raw : :class:`float` or :any:`None`, optional
raw variance of the model which will be multiplied with
:any:`CovModel.var_factor` to result in the actual variance.
If given, ``var`` will be ignored.
(This is just for models that override :any:`CovModel.var_factor`)
Default: :any:`None`
hankel_kw: :class:`dict` or :any:`None`, optional
Modify the init-arguments of
:any:`hankel.SymmetricFourierTransform`
used for the spectrum calculation. Use with caution (Better: Don't!).
``None`` is equivalent to ``{"a": -1, "b": 1, "N": 1000, "h": 0.001}``.
Default: :any:`None`
Examples
--------
>>> from gstools import CovModel
>>> import numpy as np
>>> class Gau(CovModel):
... def cor(self, h):
... return np.exp(-h**2)
...
>>> model = Gau()
>>> model.spectrum(2)
0.00825830126008459
"""
def __init__(
self,
dim=3,
var=1.0,
len_scale=1.0,
nugget=0.0,
anis=1.0,
angles=0.0,
integral_scale=None,
var_raw=None,
hankel_kw=None,
**opt_arg
):
# assert, that we use a subclass
# this is the case, if __init_subclass__ is called, which creates
# the "variogram"... so we check for that
if not hasattr(self, "variogram"):
raise TypeError("Don't instantiate 'CovModel' directly!")
# prepare dim setting
self._dim = None
self._hankel_kw = None
self._sft = None
# prepare parameters (they are checked in dim setting)
self._len_scale = None
self._anis = None
self._angles = None
# prepare parameters boundaries
self._var_bounds = None
self._len_scale_bounds = None
self._nugget_bounds = None
self._opt_arg_bounds = {}
# SFT class will be created within dim.setter but needs hankel_kw
self.hankel_kw = hankel_kw
self.dim = dim
# optional arguments for the variogram-model
self._opt_arg = []
# look up the defaults for the optional arguments (defined by the user)
default = self.default_opt_arg()
for opt_name in opt_arg:
if opt_name not in default:
warnings.warn(
"The given optional argument '{}' ".format(opt_name)
+ "is unknown or has at least no defined standard value. "
+ "Or you made a Typo... hehe.",
AttributeWarning,
)
# add the default vaules if not specified
for def_arg in default:
if def_arg not in opt_arg:
opt_arg[def_arg] = default[def_arg]
# save names of the optional arguments
self._opt_arg = list(opt_arg.keys())
# add the optional arguments as attributes to the class
for opt_name in opt_arg:
if opt_name in dir(self): # "dir" also respects properties
raise ValueError(
"parameter '"
+ opt_name
+ "' has a 'bad' name, since it is already present in "
+ "the class. It could not be added to the model"
)
# Magic happens here
setattr(self, opt_name, opt_arg[opt_name])
# set standard boundaries for variance, len_scale, nugget and opt_arg
bounds = self.default_arg_bounds()
bounds.update(self.default_opt_arg_bounds())
self.set_arg_bounds(check_args=False, **bounds)
# set parameters
self._nugget = nugget
self._angles = set_angles(self.dim, angles)
self._len_scale, self._anis = set_len_anis(self.dim, len_scale, anis)
# set var at last, because of the var_factor (to be right initialized)
if var_raw is None:
self._var = None
self.var = var
else:
self._var = var_raw
self._integral_scale = None
self.integral_scale = integral_scale
# set var again, if int_scale affects var_factor
if var_raw is None:
self._var = None
self.var = var
else:
self._var = var_raw
# final check for parameter bounds
self.check_arg_bounds()
# additional checks for the optional arguments (provided by user)
self.check_opt_arg()
###########################################################################
### one of these functions needs to be overridden #########################
###########################################################################
def __init_subclass__(cls):
r"""Initialize gstools covariance model.
Warnings
--------
Don't instantiate ``CovModel`` directly. You need to inherit a
child class which overrides one of the following methods:
* ``model.variogram(r)``
:math:`\gamma\left(r\right)=
\sigma^2\cdot\left(1-\rho\left(r\right)\right)+n`
* ``model.covariance(r)``
:math:`C\left(r\right)=
\sigma^2\cdot\rho\left(r\right)`
* ``model.correlation(r)``
:math:`\rho\left(r\right)`
Best practice is to use the ``correlation`` function, or the ``cor``
function. The latter one takes the dimensionles distance h=r/l.
"""
# override one of these ###############################################
def variogram(self, r):
r"""Isotropic variogram of the model.
Given by: :math:`\gamma\left(r\right)=
\sigma^2\cdot\left(1-\rho\left(r\right)\right)+n`
Where :math:`\rho(r)` is the correlation function.
"""
return self.var - self.covariance(r) + self.nugget
def covariance(self, r):
r"""Covariance of the model.
Given by: :math:`C\left(r\right)=
\sigma^2\cdot\rho\left(r\right)`
Where :math:`\rho(r)` is the correlation function.
"""
return self.var * self.correlation(r)
def correlation(self, r):
r"""Correlation function (or normalized covariance) of the model.
Given by: :math:`\rho\left(r\right)`
It has to be a monotonic decreasing function with
:math:`\rho(0)=1` and :math:`\rho(\infty)=0`.
"""
return 1.0 - (self.variogram(r) - self.nugget) / self.var
def correlation_from_cor(self, r):
r"""Correlation function (or normalized covariance) of the model.
Given by: :math:`\rho\left(r\right)`
It has to be a monotonic decreasing function with
:math:`\rho(0)=1` and :math:`\rho(\infty)=0`.
"""
r = np.array(np.abs(r), dtype=np.double)
return self.cor(r / self.len_scale)
def cor_from_correlation(self, h):
r"""Normalziled correlation function taking a normalized range.
Given by: :math:`\mathrm{cor}\left(r/\ell\right) = \rho(r)`
"""
h = np.array(np.abs(h), dtype=np.double)
return self.correlation(h * self.len_scale)
#######################################################################
abstract = True
if hasattr(cls, "cor"):
cls.correlation = correlation_from_cor
abstract = False
else:
cls.cor = cor_from_correlation
if not hasattr(cls, "variogram"):
cls.variogram = variogram
else:
abstract = False
if not hasattr(cls, "covariance"):
cls.covariance = covariance
else:
abstract = False
if not hasattr(cls, "correlation"):
cls.correlation = correlation
else:
abstract = False
if abstract:
raise TypeError(
"Can't instantiate class '"
+ cls.__name__
+ "', "
+ "without overriding at least on of the methods "
+ "'variogram', 'covariance' or 'correlation'."
)
# modify the docstrings ###############################################
# class docstring gets attributes added
if cls.__doc__ is None:
cls.__doc__ = (
"User defined GSTools Covariance-Model "
+ CovModel.__doc__[44:-296]
)
else:
cls.__doc__ += CovModel.__doc__[44:-296]
# overridden functions get standard doc if no new doc was created
ignore = ["__", "variogram", "covariance", "correlation"]
for attr in cls.__dict__:
if any(
[attr.startswith(ign) for ign in ignore]
) or attr not in dir(CovModel):
continue
attr_doc = getattr(CovModel, attr).__doc__
attr_cls = cls.__dict__[attr]
if attr_cls.__doc__ is None:
attr_cls.__doc__ = attr_doc
### special variogram functions ###########################################
def _get_iso_rad(self, pos):
x, y, z = pos2xyz(pos, max_dim=self.dim)
if self.do_rotation:
x, y, z = unrotate_mesh(self.dim, self.angles, x, y, z)
if not self.is_isotropic:
y, z = make_isotropic(self.dim, self.anis, y, z)
return np.linalg.norm((x, y, z)[: self.dim], axis=0)
def vario_spatial(self, pos):
r"""Spatial variogram respecting anisotropy and rotation."""
return self.variogram(self._get_iso_rad(pos))
def cov_spatial(self, pos):
r"""Spatial covariance respecting anisotropy and rotation."""
return self.covariance(self._get_iso_rad(pos))
def cor_spatial(self, pos):
r"""Spatial correlation respecting anisotropy and rotation."""
return self.correlation(self._get_iso_rad(pos))
def cov_nugget(self, r):
r"""Covariance of the model respecting the nugget at r=0.
Given by: :math:`C\left(r\right)=
\sigma^2\cdot\rho\left(r\right)`
Where :math:`\rho(r)` is the correlation function.
"""
r = np.array(np.abs(r), dtype=np.double)
r_gz = np.logical_not(np.isclose(r, 0))
res = np.empty_like(r, dtype=np.double)
res[r_gz] = self.covariance(r[r_gz])
res[np.logical_not(r_gz)] = self.sill
return res
def vario_nugget(self, r):
r"""Isotropic variogram of the model respecting the nugget at r=0.
Given by: :math:`\gamma\left(r\right)=
\sigma^2\cdot\left(1-\rho\left(r\right)\right)+n`
Where :math:`\rho(r)` is the correlation function.
"""
r = np.array(np.abs(r), dtype=np.double)
r_gz = np.logical_not(np.isclose(r, 0))
res = np.empty_like(r, dtype=np.double)
res[r_gz] = self.variogram(r[r_gz])
res[np.logical_not(r_gz)] = 0.0
return res
def plot(self, func="variogram", **kwargs): # pragma: no cover
"""
Plot a function of a the CovModel.
Parameters
----------
func : :class:`str`, optional
Function to be plotted. Could be one of:
* "variogram"
* "covariance"
* "correlation"
* "vario_spatial"
* "cov_spatial"
* "cor_spatial"
* "spectrum"
* "spectral_density"
* "spectral_rad_pdf"
**kwargs
Keyword arguments forwarded to the plotting function
`"plot_" + func` in :any:`gstools.covmodel.plot`.
See Also
--------
gstools.covmodel.plot
"""
routine = getattr(plot, "plot_" + func)
return routine(self, **kwargs)
###########################################################################
### pykrige functions #####################################################
###########################################################################
def pykrige_vario(self, args=None, r=0):
r"""Isotropic variogram of the model for pykrige.
Given by: :math:`\gamma\left(r\right)=
\sigma^2\cdot\left(1-\rho\left(r\right)\right)+n`
Where :math:`\rho(r)` is the correlation function.
"""
return self.variogram(r)
@property
def pykrige_anis(self):
"""2D anisotropy ratio for pykrige."""
if self.dim == 2:
return 1 / self.anis[0]
return 1.0
@property
def pykrige_anis_y(self):
"""3D anisotropy ratio in y direction for pykrige."""
if self.dim >= 2:
return 1 / self.anis[0]
return 1.0
@property
def pykrige_anis_z(self):
"""3D anisotropy ratio in z direction for pykrige."""
if self.dim == 3:
return 1 / self.anis[1]
return 1.0
@property
def pykrige_angle(self):
"""2D rotation angle for pykrige."""
if self.dim == 2:
return self.angles[0] / np.pi * 180
return 0.0
@property
def pykrige_angle_z(self):
"""3D rotation angle around z for pykrige."""
if self.dim >= 2:
return self.angles[0] / np.pi * 180
return 0.0
@property
def pykrige_angle_y(self):
"""3D rotation angle around y for pykrige."""
if self.dim == 3:
return self.angles[1] / np.pi * 180
return 0.0
@property
def pykrige_angle_x(self):
"""3D rotation angle around x for pykrige."""
if self.dim == 3:
return self.angles[2] / np.pi * 180
return 0.0
@property
def pykrige_kwargs(self):
"""Keyword arguments for pykrige routines."""
kwargs = {
"variogram_model": "custom",
"variogram_parameters": [],
"variogram_function": self.pykrige_vario,
}
if self.dim == 1:
add_kwargs = {}
elif self.dim == 2:
add_kwargs = {
"anisotropy_scaling": self.pykrige_anis,
"anisotropy_angle": self.pykrige_angle,
}
else:
add_kwargs = {
"anisotropy_scaling_y": self.pykrige_anis_y,
"anisotropy_scaling_z": self.pykrige_anis_z,
"anisotropy_angle_x": self.pykrige_angle_x,
"anisotropy_angle_y": self.pykrige_angle_y,
"anisotropy_angle_z": self.pykrige_angle_z,
}
kwargs.update(add_kwargs)
return kwargs
###########################################################################
### methods for optional arguments (can be overridden) ####################
###########################################################################
def default_opt_arg(self):
"""Provide default optional arguments by the user.
Should be given as a dictionary.
"""
res = {}
for opt in self.opt_arg:
res[opt] = 0.0
return res
def default_opt_arg_bounds(self):
"""Provide default boundaries for optional arguments."""
res = {}
for opt in self.opt_arg:
res[opt] = [-np.inf, np.inf]
return res
def check_opt_arg(self):
"""Run checks for the optional arguments.
This is in addition to the bound-checks
Notes
-----
* You can use this to raise a ValueError/warning
* Any return value will be ignored
* This method will only be run once, when the class is initialized
"""
pass
def fix_dim(self):
"""Set a fix dimension for the model."""
return None
def var_factor(self):
"""Factor for the variance."""
return 1.0
### calculation of different scales #######################################
def calc_integral_scale(self):
"""Calculate the integral scale of the isotrope model."""
self._integral_scale = integral(self.correlation, 0, np.inf)[0]
return self._integral_scale
def percentile_scale(self, per=0.9):
"""Calculate the percentile scale of the isotrope model.
This is the distance, where the given percentile of the variance
is reached by the variogram
"""
# check the given percentile
if not 0.0 < per < 1.0:
raise ValueError(
"percentile needs to be within (0, 1), got: " + str(per)
)
# define a curve, that has its root at the wanted point
def curve(x):
return 1.0 - self.correlation(x) - per
# take 'per * len_scale' as initial guess
return root(curve, per * self.len_scale)["x"][0]
###########################################################################
### spectrum methods (can be overridden for speedup) ######################
###########################################################################
def spectrum(self, k):
r"""
Spectrum of the covariance model.
This is given by:
.. math:: S(k) = \left(\frac{1}{2\pi}\right)^n
\int C(r) e^{i b\mathbf{k}\cdot\mathbf{r}} d^n\mathbf{r}
Internally, this is calculated by the hankel transformation:
.. math:: S(k) = \left(\frac{1}{2\pi}\right)^n \cdot
\frac{(2\pi)^{n/2}}{k^{n/2-1}}
\int_0^\infty r^{n/2} C(r) J_{n/2-1}(kr) dr
Where :math:`C(r)` is the covariance function of the model.
Parameters
----------
k : :class:`float`
Radius of the phase: :math:`k=\left\Vert\mathbf{k}\right\Vert`
"""
return self.spectral_density(k) * self.var
def spectral_density(self, k):
r"""
Spectral density of the covariance model.
This is given by:
.. math:: \tilde{S}(k) = \frac{S(k)}{\sigma^2}
Where :math:`S(k)` is the spectrum of the covariance model.
Parameters
----------
k : :class:`float`
Radius of the phase: :math:`k=\left\Vert\mathbf{k}\right\Vert`
"""
k = np.array(np.abs(k), dtype=np.double)
return self._sft.transform(self.correlation, k, ret_err=False)
def spectral_rad_pdf(self, r):
"""Radial spectral density of the model."""
r = np.array(np.abs(r), dtype=np.double)
if self.dim > 1:
r_gz = np.logical_not(np.isclose(r, 0))
# to prevent numerical errors, we just calculate where r>0
res = np.zeros_like(r, dtype=np.double)
res[r_gz] = rad_fac(self.dim, r[r_gz]) * np.abs(
self.spectral_density(r[r_gz])
)
else:
res = rad_fac(self.dim, r) * np.abs(self.spectral_density(r))
# prevent numerical errors in hankel for small r values (set 0)
res[np.logical_not(np.isfinite(res))] = 0.0
# prevent numerical errors in hankel for big r (set non-negative)
res = np.maximum(res, 0.0)
return res
def ln_spectral_rad_pdf(self, r):
"""Log radial spectral density of the model."""
with np.errstate(divide="ignore"):
return np.log(self.spectral_rad_pdf(r))
def _has_cdf(self):
"""State if a cdf is defined with 'spectral_rad_cdf'."""
return hasattr(self, "spectral_rad_cdf")
def _has_ppf(self):
"""State if a ppf is defined with 'spectral_rad_ppf'."""
return hasattr(self, "spectral_rad_ppf")
### fitting routine #######################################################
def fit_variogram(
self,
x_data,
y_data,
sill=None,
init_guess="default",
weights=None,
method="trf",
loss="soft_l1",
max_eval=None,
return_r2=False,
curve_fit_kwargs=None,
**para_select
):
"""
Fiting the isotropic variogram-model to given data.
Parameters
----------
x_data : :class:`numpy.ndarray`
The radii of the meassured variogram.
y_data : :class:`numpy.ndarray`
The messured variogram
sill : :class:`float` or :class:`bool`, optional
Here you can provide a fixed sill for the variogram.
It needs to be in a fitting range for the var and nugget bounds.
If variance or nugget are not selected for estimation,
the nugget will be recalculated to fulfill:
* sill = var + nugget
* if the variance is bigger than the sill,
nugget will bet set to its lower bound
and the variance will be set to the fitting partial sill.
If variance is deselected, it needs to be less than the sill,
otherwise a ValueError comes up. Same for nugget.
If sill=False, it will be deslected from estimation
and set to the current sill of the model.
Then, the procedure above is applied.
Default: None
init_guess : :class:`str` or :class:`dict`, optional
Initial guess for the estimation. Either:
* "default": using the default values of the covariance model
* "current": using the current values of the covariance model
* dict(name: val): specified value for each parameter by name
Default: "default"
weights : :class:`str`, :class:`numpy.ndarray`, :class:`callable`, optional
Weights applied to each point in the estimation. Either:
* 'inv': inverse distance ``1 / (x_data + 1)``
* list: weights given per bin
* callable: function applied to x_data
If callable, it must take a 1-d ndarray.
Then ``weights = f(x_data)``.
Default: None
method : {'trf', 'dogbox'}, optional
Algorithm to perform minimization.
* 'trf' : Trust Region Reflective algorithm,
particularly suitable for large sparse problems with bounds.
Generally robust method.
* 'dogbox' : dogleg algorithm with rectangular trust regions,
typical use case is small problems with bounds.
Not recommended for problems with rank-deficient Jacobian.
Default: 'trf'
loss : :class:`str` or :class:`callable`, optional
Determines the loss function in scipys curve_fit.
The following keyword values are allowed:
* 'linear' (default) : ``rho(z) = z``. Gives a standard
least-squares problem.
* 'soft_l1' : ``rho(z) = 2 * ((1 + z)**0.5 - 1)``. The smooth
approximation of l1 (absolute value) loss. Usually a good
choice for robust least squares.
* 'huber' : ``rho(z) = z if z <= 1 else 2*z**0.5 - 1``. Works
similarly to 'soft_l1'.
* 'cauchy' : ``rho(z) = ln(1 + z)``. Severely weakens outliers
influence, but may cause difficulties in optimization process.
* 'arctan' : ``rho(z) = arctan(z)``. Limits a maximum loss on
a single residual, has properties similar to 'cauchy'.
If callable, it must take a 1-d ndarray ``z=f**2`` and return an
array_like with shape (3, m) where row 0 contains function values,
row 1 contains first derivatives and row 2 contains second
derivatives. Default: 'soft_l1'
max_eval : :class:`int` or :any:`None`, optional
Maximum number of function evaluations before the termination.
If None (default), the value is chosen automatically: 100 * n.
return_r2 : :class:`bool`, optional
Whether to return the r2 score of the estimation.
Default: False
curve_fit_kwargs : :class:`dict`, optional
Other keyword arguments passed to scipys curve_fit. Default: None
**para_select
You can deselect parameters from fitting, by setting
them "False" using their names as keywords.
You could also pass fixed values for each parameter.
Then these values will be applied and the involved parameters wont
be fitted.
By default, all parameters are fitted.
Returns
-------
fit_para : :class:`dict`
Dictonary with the fitted parameter values
pcov : :class:`numpy.ndarray`
The estimated covariance of `popt` from
:any:`scipy.optimize.curve_fit`.
To compute one standard deviation errors
on the parameters use ``perr = np.sqrt(np.diag(pcov))``.
r2_score : :class:`float`, optional
r2 score of the curve fitting results. Only if return_r2 is True.
Notes
-----
You can set the bounds for each parameter by accessing
:any:`CovModel.set_arg_bounds`.
The fitted parameters will be instantly set in the model.
"""
return fit_variogram(
model=self,
x_data=x_data,
y_data=y_data,
sill=sill,
init_guess=init_guess,
weights=weights,
method=method,
loss=loss,
max_eval=max_eval,
return_r2=return_r2,
curve_fit_kwargs=curve_fit_kwargs,
**para_select
)
### bounds setting and checks #############################################
def default_arg_bounds(self):
"""Provide default boundaries for arguments.
Given as a dictionary.
"""
res = {
"var": (0.0, np.inf, "oo"),
"len_scale": (0.0, np.inf, "oo"),
"nugget": (0.0, np.inf, "co"),
}
return res
def set_arg_bounds(self, check_args=True, **kwargs):
r"""Set bounds for the parameters of the model.
Parameters
----------
check_args : bool, optional
Whether to check if the arguments need to resetted to be in the
given bounds. In case, a propper default value will be determined
from the given bounds.
Default: True
**kwargs
Parameter name as keyword ("var", "len_scale", "nugget", <opt_arg>)
and a list of 2 or 3 values as value:
* ``[a, b]`` or
* ``[a, b, <type>]``
<type> is one of ``"oo"``, ``"cc"``, ``"oc"`` or ``"co"``
to define if the bounds are open ("o") or closed ("c").
"""
# if variance needs to be resetted, do this at last
var_bnds = []
for arg in kwargs:
if not check_bounds(kwargs[arg]):
raise ValueError(
"Given bounds for '{0}' are not valid, got: {1}".format(
arg, kwargs[arg]
)
)
if arg in self.opt_arg:
self._opt_arg_bounds[arg] = kwargs[arg]
elif arg == "var":
var_bnds = kwargs[arg]
continue
elif arg == "len_scale":
self.len_scale_bounds = kwargs[arg]
elif arg == "nugget":
self.nugget_bounds = kwargs[arg]
else:
raise ValueError(
"set_arg_bounds: unknown argument '{}'".format(arg)
)
if check_args and check_arg_in_bounds(self, arg) > 0:
setattr(self, arg, default_arg_from_bounds(kwargs[arg]))
# set var last like allways
if var_bnds:
self.var_bounds = var_bnds
if check_args and check_arg_in_bounds(self, "var") > 0:
self.var = default_arg_from_bounds(var_bnds)
def check_arg_bounds(self):
"""Check arguments to be within the given bounds."""
# check var, len_scale, nugget and optional-arguments
for arg in self.arg_bounds:
bnd = list(self.arg_bounds[arg])
val = getattr(self, arg)
error_case = check_arg_in_bounds(self, arg)
if error_case == 1:
raise ValueError(
"{0} needs to be >= {1}, got: {2}".format(arg, bnd[0], val)
)
if error_case == 2:
raise ValueError(
"{0} needs to be > {1}, got: {2}".format(arg, bnd[0], val)
)
if error_case == 3:
raise ValueError(
"{0} needs to be <= {1}, got: {2}".format(arg, bnd[1], val)
)
if error_case == 4:
raise ValueError(
"{0} needs to be < {1}, got: {2}".format(arg, bnd[1], val)
)
### bounds properties #####################################################
@property
def var_bounds(self):
""":class:`list`: Bounds for the variance.
Notes
-----
Is a list of 2 or 3 values:
* ``[a, b]`` or
* ``[a, b, <type>]``
<type> is one of ``"oo"``, ``"cc"``, ``"oc"`` or ``"co"``
to define if the bounds are open ("o") or closed ("c").
"""
return self._var_bounds
@var_bounds.setter
def var_bounds(self, bounds):
if not check_bounds(bounds):
raise ValueError(
"Given bounds for 'var' are not "
+ "valid, got: "
+ str(bounds)
)
self._var_bounds = bounds
@property
def len_scale_bounds(self):
""":class:`list`: Bounds for the lenght scale.
Notes
-----
Is a list of 2 or 3 values:
* ``[a, b]`` or
* ``[a, b, <type>]``
<type> is one of ``"oo"``, ``"cc"``, ``"oc"`` or ``"co"``
to define if the bounds are open ("o") or closed ("c").
"""
return self._len_scale_bounds
@len_scale_bounds.setter
def len_scale_bounds(self, bounds):
if not check_bounds(bounds):
raise ValueError(
"Given bounds for 'len_scale' are not "
+ "valid, got: "
+ str(bounds)
)
self._len_scale_bounds = bounds
@property
def nugget_bounds(self):
""":class:`list`: Bounds for the nugget.
Notes
-----
Is a list of 2 or 3 values:
* ``[a, b]`` or
* ``[a, b, <type>]``
<type> is one of ``"oo"``, ``"cc"``, ``"oc"`` or ``"co"``
to define if the bounds are open ("o") or closed ("c").
"""
return self._nugget_bounds
@nugget_bounds.setter
def nugget_bounds(self, bounds):
if not check_bounds(bounds):
raise ValueError(
"Given bounds for 'nugget' are not "
+ "valid, got: "
+ str(bounds)
)
self._nugget_bounds = bounds
@property
def opt_arg_bounds(self):
""":class:`dict`: Bounds for the optional arguments.
Notes
-----
Keys are the opt-arg names and values are lists of 2 or 3 values:
* ``[a, b]`` or
* ``[a, b, <type>]``
<type> is one of ``"oo"``, ``"cc"``, ``"oc"`` or ``"co"``
to define if the bounds are open ("o") or closed ("c").
"""
return self._opt_arg_bounds
@property
def arg_bounds(self):
""":class:`dict`: Bounds for all parameters.
Notes
-----
Keys are the opt-arg names and values are lists of 2 or 3 values:
* ``[a, b]`` or
* ``[a, b, <type>]``
<type> is one of ``"oo"``, ``"cc"``, ``"oc"`` or ``"co"``
to define if the bounds are open ("o") or closed ("c").
"""
res = {
"var": self.var_bounds,
"len_scale": self.len_scale_bounds,
"nugget": self.nugget_bounds,
}
res.update(self.opt_arg_bounds)
return res
### standard parameters ###################################################
@property
def dim(self):
""":class:`int`: The dimension of the model."""