Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CovModel: Update and Refactoring #109

Merged
merged 70 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
50e51cd
CovModel: add 'rescale' argument to rescale the len_scale
MuellerSeb Nov 10, 2020
f9ec092
Cleanup: remove doctest calls
MuellerSeb Nov 10, 2020
333321b
covmodel: add attribute 'len_rescaled' which is the rescaled len_scale
MuellerSeb Nov 10, 2020
474e71a
CovModel: rewrite Gaussian covmodel to use 'cor' and 'rescale'
MuellerSeb Nov 10, 2020
60c92aa
CovModel: move **opt_arg doc to main class
MuellerSeb Nov 10, 2020
795b3d2
CovModel: rewrite Exponential covmodel to use 'cor' and 'rescale'
MuellerSeb Nov 10, 2020
d9daac6
CovModel: rewrite Rational and Stable covmodel to use 'cor' and 'resc…
MuellerSeb Nov 10, 2020
b34c372
CovModel: rewrite Matern covmodel to use 'cor' and 'rescale'
MuellerSeb Nov 11, 2020
f324d2a
CovModel: rewrite Linear, Circular and Spherical covmodel to use 'cor…
MuellerSeb Nov 11, 2020
6722f63
CovModel: replace the 'Intersection' model with the 'HyperSpherical' …
MuellerSeb Nov 11, 2020
a1b0b2a
CovModel: replace the 'Intersection' model with the 'HyperSpherical' …
MuellerSeb Nov 11, 2020
a03a2e5
covmodel: comment update
MuellerSeb Nov 11, 2020
7392ea0
CovModel: add the Super-Spherical model
MuellerSeb Nov 11, 2020
fb28c33
SuperSpherical: doc fix
MuellerSeb Nov 11, 2020
61bdee2
CovModel: add the JBessel hole model
MuellerSeb Nov 11, 2020
97c3487
TPLmodels: refactor to use 'cor' and 'rescale'; use a new base-class …
MuellerSeb Nov 11, 2020
b3e6c77
Doc: minor fixes
MuellerSeb Nov 11, 2020
550e02e
TPLCovModel: tweak base class to provide cor and correlation like der…
MuellerSeb Nov 11, 2020
df9fa0e
Merge branch 'develop' of github.com:GeoStat-Framework/GSTools into c…
MuellerSeb Nov 11, 2020
64017b3
CovModel: add TPLSimple model
MuellerSeb Nov 11, 2020
82ab29c
field: unify rotation and anisotropy; simplify code
MuellerSeb Nov 13, 2020
5b29c37
CovModel: add [an]isometrize method; simplify code
MuellerSeb Nov 13, 2020
b31bc8a
tests: adopt tests: don't allow high-dim input in lower dimensions in…
MuellerSeb Nov 13, 2020
5481b17
CovModel: more details in documentation
MuellerSeb Nov 13, 2020
ec7ba70
Tools: add rotated_main_axes routine to determine axes from rotation …
MuellerSeb Nov 13, 2020
b552859
CovModel: simplify Rational model
MuellerSeb Nov 13, 2020
305a0d4
Tools: make use of rotated_main_axes in CovModel and Examples
MuellerSeb Nov 13, 2020
43278f0
CovModel: Fitting anisotropy to directional empirical variograms; var…
MuellerSeb Nov 14, 2020
92287fe
CovModel: update plotting routines: add axis routines; forward kwargs
MuellerSeb Nov 14, 2020
bcd984e
Examples: demonstrate fitting of anisotropy
MuellerSeb Nov 14, 2020
4bdceca
CovModel: fitting with weights respects directional variogram now; re…
MuellerSeb Nov 14, 2020
316ca18
CovModel: always use floats for 'angles' and 'anis'
MuellerSeb Nov 14, 2020
2bea823
CovModel: always use floats for 'len_scale'
MuellerSeb Nov 14, 2020
2fd07e3
Tools: add a string formatter to pretty-print lists of floats
MuellerSeb Nov 14, 2020
b844397
CovModel: always use floats for opt_args; better formatting of CovMod…
MuellerSeb Nov 14, 2020
621db8a
Examples: demonstrate using bin-counts as weights during variogram fi…
MuellerSeb Nov 14, 2020
933aea0
tools/misc: f-string not working in py35; use format
MuellerSeb Nov 14, 2020
916867e
CovModel: use precision as privat attribute to set printing format
MuellerSeb Nov 14, 2020
373251d
Examples: pimp variogram plot
MuellerSeb Nov 14, 2020
24b4f83
Field: better printing format
MuellerSeb Nov 14, 2020
3f1c0ba
CovModel.check_arg_in_bounds: allow checking lists
MuellerSeb Nov 14, 2020
602a90a
CovModel: add bounds for 'anis' since we can fit it now
MuellerSeb Nov 15, 2020
3e2680f
CovModel: add arg_list attribute to get list of values; also for isot…
MuellerSeb Nov 15, 2020
d9ad2e7
CovModel.fit: better handling of bounds for anis when fitting directi…
MuellerSeb Nov 15, 2020
c4e629c
Examples: add overview gallery to tutorials page in Docs
MuellerSeb Nov 16, 2020
49589e7
Docs: remove Overview gallery again: TOC-tree struggles
MuellerSeb Nov 16, 2020
c605cd2
Doc: resolve minor problems
MuellerSeb Nov 16, 2020
f660a48
Fix some typos
LSchueler Nov 16, 2020
5d6815b
CovModel: add routine 'check_dim'
MuellerSeb Nov 17, 2020
0045cb3
CovModel: skip bound checking, when no bounds present (e.g. during in…
MuellerSeb Nov 17, 2020
cd93f0c
CovModels: add dim checks to the linear, circular and spherical model
MuellerSeb Nov 17, 2020
88c6496
CovModel: move spectral_rad_pdf to tools
MuellerSeb Nov 18, 2020
cf14a5f
CovModel: outsource percentile_scale to tools
MuellerSeb Nov 18, 2020
b7462f6
CovModel: add 'set_opt_arg' routine to tools to shrink __init__
MuellerSeb Nov 18, 2020
e805f37
CovModel: move '[set/check]_arg_bounds' routines to tools
MuellerSeb Nov 18, 2020
ce6b8f4
CovModel: add 'set_dim' to tools
MuellerSeb Nov 18, 2020
f40769b
CovModel: add '_init_subclass' to tools
MuellerSeb Nov 18, 2020
791147a
CovModel: add more tests for class functionality
MuellerSeb Nov 18, 2020
38426da
CovModel: minor simplifications for TPL models
MuellerSeb Nov 18, 2020
97f9b44
Tests: covmodel.tools to 100% coverage
MuellerSeb Nov 18, 2020
6e1b0fa
CovModel: test *_axis methods and cor_spatial
MuellerSeb Nov 18, 2020
def555e
CovModel: better testing TPL models
MuellerSeb Nov 18, 2020
ae2ff57
CovModel: check special models; better check-case for TPL vario fitting
MuellerSeb Nov 18, 2020
55d41fd
CovModel: further testing of properties; default_opt_args always empt…
MuellerSeb Nov 18, 2020
d47d74f
CovModel: test all axis specific routines
MuellerSeb Nov 18, 2020
bc2fb16
CovModel.fit: remove redundant 'dict' option for init_guess
MuellerSeb Nov 18, 2020
c4001bf
CovModel.fit: more testing
MuellerSeb Nov 18, 2020
66943a5
CovModel.fit: weights testing
MuellerSeb Nov 18, 2020
4409059
Tests: used model method for main_axes
MuellerSeb Nov 18, 2020
9e83384
Docs: remove overview gallery hack again
MuellerSeb Nov 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ def setup(app):
"pointsize": "10pt",
"papersize": "a4paper",
"fncychap": "\\usepackage[Glenn]{fncychap}",
# 'inputenc': r'\usepackage[utf8]{inputenc}',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
Expand Down
46 changes: 46 additions & 0 deletions docs/source/tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,49 @@ explore its whole beauty and power.
examples/06_conditioned_fields/index
examples/07_transformations/index
examples/00_misc/index

.. comment
Gallery of all Tutorial Examples
--------------------------------

.. include:: examples/01_random_field/index.rst
:start-after: Below is a gallery of examples
:end-line: -11

.. include:: examples/02_cov_model/index.rst
:start-after: Below is a gallery of examples
:end-line: -11

.. include:: examples/03_variogram/index.rst
:start-after: Below is a gallery of examples
:end-line: -11

.. include:: examples/04_vector_field/index.rst
:start-after: Below is a gallery of examples
:end-line: -11

.. include:: examples/05_kriging/index.rst
:start-after: Below is a gallery of examples
:end-line: -11

.. include:: examples/06_conditioned_fields/index.rst
:start-after: Below is a gallery of examples
:end-line: -11

.. include:: examples/07_transformations/index.rst
:start-after: Below is a gallery of examples
:end-line: -11

.. include:: examples/00_misc/index.rst
:start-after: Below is a gallery of examples
:end-line: -11

.. raw:: html

<div class="sphx-glr-clear"></div>

.. only:: html

.. rst-class:: sphx-glr-signature

`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
8 changes: 6 additions & 2 deletions examples/00_misc/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ Miscellaneous

A few miscellaneous examples

Gallery
-------
.. only:: html

Gallery
-------

Below is a gallery of examples
8 changes: 6 additions & 2 deletions examples/01_random_field/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ and its discretised modes are evaluated at random frequencies.

GSTools supports arbitrary and non-isotropic covariance models.

Gallery
-------
.. only:: html

Gallery
-------

Below is a gallery of examples
42 changes: 31 additions & 11 deletions examples/02_cov_model/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,45 @@ class, which allows you to easily define arbitrary covariance models by
yourself. The resulting models provide a bunch of nice features to explore the
covariance models.


A covariance model is used to characterize the
`semi-variogram <https://en.wikipedia.org/wiki/Variogram#Semivariogram>`_,
denoted by :math:`\gamma`, of a spatial random field.
In GSTools, we use the following form for an isotropic and stationary field:

.. math::
\gamma\left(r\right)=
\sigma^2\cdot\left(1-\rho\left(r\right)\right)+n
\sigma^2\cdot\left(1-\mathrm{cor}\left(s\cdot\frac{r}{\ell}\right)\right)+n

Where:

- :math:`\rho(r)` is the so called
`correlation <https://en.wikipedia.org/wiki/Autocovariance#Normalization>`_
function depending on the distance :math:`r`
- :math:`r` is the lag distance
- :math:`\ell` is the main correlation length
- :math:`s` is a scaling factor for unit conversion or normalization
- :math:`\sigma^2` is the variance
- :math:`n` is the nugget (subscale variance)
- :math:`\mathrm{cor}(h)` is the normalized correlation function depending on
the non-dimensional distance :math:`h=s\cdot\frac{r}{\ell}`

Depending on the normalized correlation function, all covariance models in
GSTools are providing the following functions:

- :math:`\rho(r)=\mathrm{cor}\left(s\cdot\frac{r}{\ell}\right)`
is the so called
`correlation <https://en.wikipedia.org/wiki/Autocovariance#Normalization>`_
function
- :math:`C(r)=\sigma^2\cdot\rho(r)` is the so called
`covariance <https://en.wikipedia.org/wiki/Covariance_function>`_
function, which gives the name for our GSTools class

.. note::

We are not limited to isotropic models. GSTools supports anisotropy ratios
for length scales in orthogonal transversal directions like:

- :math:`x` (main direction)
- :math:`y` (1. transversal direction)
- :math:`z` (2. transversal direction)
- :math:`x_0` (main direction)
- :math:`x_1` (1. transversal direction)
- :math:`x_2` (2. transversal direction)
- ...

These main directions can also be rotated.
Just have a look at the corresponding examples.
Expand All @@ -54,14 +67,21 @@ The following standard covariance models are provided by GSTools
Linear
Circular
Spherical
Intersection
HyperSpherical
SuperSpherical
JBessel

As a special feature, we also provide truncated power law (TPL) covariance models

.. autosummary::
TPLGaussian
TPLExponential
TPLStable
TPLSimple

.. only:: html

Gallery
-------

Gallery
-------
Below is a gallery of examples
35 changes: 24 additions & 11 deletions examples/03_variogram/04_directional_2d.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
Directional variogram estimation in 2D
--------------------------------------
Directional variogram estimation and fitting in 2D
--------------------------------------------------

In this example, we demonstrate how to estimate a directional variogram by
setting the direction angles in 2D.

Afterwards we will fit a model to this estimated variogram and show the result.
"""
import numpy as np
import gstools as gs
Expand All @@ -20,29 +22,40 @@

###############################################################################
# Now we are going to estimate a directional variogram with an angular
# tolerance of 11.25 degree and a bandwith of 1.
# We provide the rotation angle of the covariance model and the orthogonal
# direction by adding 90 degree.
# tolerance of 11.25 degree and a bandwith of 8.

bins = range(0, 40, 3)
bin_c, vario, cnt = gs.vario_estimate(
bins = range(0, 40, 2)
bin_center, dir_vario, counts = gs.vario_estimate(
*((x, y), field, bins),
angles=(angle, angle + np.pi / 2), # main dir. and transversal dir.
direction=gs.rotated_main_axes(dim=2, angles=angle),
angles_tol=np.pi / 16,
bandwidth=1.0,
bandwidth=8,
mesh_type="structured",
return_counts=True,
)

###############################################################################
# Afterwards we can use the estimated variogram to fit a model to it:

print("Original:")
print(model)
model.fit_variogram(bin_center, dir_vario)
print("Fitted:")
print(model)

###############################################################################
# Plotting.

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[10, 5])

ax1.plot(bin_c, vario[0], label="emp. vario: pi/8")
ax1.plot(bin_c, vario[1], label="emp. vario: pi*5/8")
ax1.plot(bin_center, dir_vario[0], label="emp. vario: pi/8")
ax1.plot(bin_center, dir_vario[1], label="emp. vario: pi*5/8")
ax1.legend(loc="lower right")

model.plot("vario_axis", axis=0, ax=ax1, x_max=40, label="fit on axis 0")
model.plot("vario_axis", axis=1, ax=ax1, x_max=40, label="fit on axis 1")
ax1.set_title("Fitting an anisotropic model")

srf.plot(ax=ax2)
ax2.set_aspect("equal")

Expand Down
59 changes: 40 additions & 19 deletions examples/03_variogram/05_directional_3d.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
Directional variogram estimation in 3D
--------------------------------------
Directional variogram estimation and fitting in 3D
--------------------------------------------------

In this example, we demonstrate how to estimate a directional variogram by
setting the estimation directions in 3D.

Afterwards we will fit a model to this estimated variogram and show the result.
"""
import numpy as np
import gstools as gs
Expand All @@ -22,31 +24,45 @@
field = srf.structured((x, y, z))

###############################################################################
# Here we generate the rotated coordinate system to get an impression what
# the rotation angles do.
# Here we generate the axes of the rotated coordinate system
# to get an impression what the rotation angles do.

x1, x2, x3 = (1, 0, 0), (0, 1, 0), (0, 0, 1)
ret = np.array(gs.field.tools.rotate_mesh(dim, angles, x1, x2, x3))
dir0 = ret[:, 0] # main direction
dir1 = ret[:, 1] # first lateral direction
dir2 = ret[:, 2] # second lateral direction
# All 3 axes of the rotated coordinate-system
main_axes = gs.rotated_main_axes(dim, angles)
axis1, axis2, axis3 = main_axes

###############################################################################
# Now we estimate the variogram along the main axis. When the main axis is
# Now we estimate the variogram along the main axes. When the main axes are
# unknown, one would need to sample multiple directions and look for the one
# with the longest correlation length (flattest gradient).
# Then check the transversal directions and so on.

bins = range(0, 40, 3)
bin_c, vario = gs.vario_estimate(
bin_center, dir_vario, counts = gs.vario_estimate(
*([x, y, z], field, bins),
direction=(dir0, dir1, dir2),
direction=main_axes,
bandwidth=10,
sampling_size=2000,
sampling_seed=1001,
mesh_type="structured"
mesh_type="structured",
return_counts=True,
)

###############################################################################
# Afterwards we can use the estimated variogram to fit a model to it.
# Note, that the rotation angles need to be set beforehand.
#
# We can use the `counts` of data pairs per bin as weights for the fitting
# routines to give more attention to areas where more data was available.
# In order to not introduce to much offset at the origin, we disable
# fitting the nugget.

print("Original:")
print(model)
model.fit_variogram(bin_center, dir_vario, weights=counts, nugget=False)
print("Fitted:")
print(model)

###############################################################################
# Plotting.

Expand All @@ -58,9 +74,9 @@
srf.plot(ax=ax1)
ax1.set_aspect("equal")

ax2.plot([0, dir0[0]], [0, dir0[1]], [0, dir0[2]], label="1.")
ax2.plot([0, dir1[0]], [0, dir1[1]], [0, dir1[2]], label="2.")
ax2.plot([0, dir2[0]], [0, dir2[1]], [0, dir2[2]], label="3.")
ax2.plot([0, axis1[0]], [0, axis1[1]], [0, axis1[2]], label="0.")
ax2.plot([0, axis2[0]], [0, axis2[1]], [0, axis2[2]], label="1.")
ax2.plot([0, axis3[0]], [0, axis3[1]], [0, axis3[2]], label="2.")
ax2.set_xlim(-1, 1)
ax2.set_ylim(-1, 1)
ax2.set_zlim(-1, 1)
Expand All @@ -70,8 +86,13 @@
ax2.set_title("Tait-Bryan main axis")
ax2.legend(loc="lower left")

ax3.plot(bin_c, vario[0], label="1. axis")
ax3.plot(bin_c, vario[1], label="2. axis")
ax3.plot(bin_c, vario[2], label="3. axis")
ax3.plot(bin_center, dir_vario[0], label="0. axis")
ax3.plot(bin_center, dir_vario[1], label="1. axis")
ax3.plot(bin_center, dir_vario[2], label="2. axis")
model.plot("vario_axis", axis=0, ax=ax3, label="fit on axis 0", linestyle=":")
model.plot("vario_axis", axis=1, ax=ax3, label="fit on axis 1", linestyle=":")
model.plot("vario_axis", axis=2, ax=ax3, label="fit on axis 2", linestyle=":")
ax3.set_title("Fitting an anisotropic model")
ax3.legend()

plt.show()
8 changes: 6 additions & 2 deletions examples/03_variogram/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ The same `(semi-)variogram <https://en.wikipedia.org/wiki/Variogram#Semivariogra
:ref:`tutorial_02_cov` is being used
by this subpackage.

Gallery
-------
.. only:: html

Gallery
-------

Below is a gallery of examples
8 changes: 6 additions & 2 deletions examples/04_vector_field/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ with the projector
By calculating :math:`\nabla \cdot \mathbf U = 0`, it can be verified, that
the resulting field is indeed incompressible.

Gallery
-------
.. only:: html

Gallery
-------

Below is a gallery of examples
10 changes: 7 additions & 3 deletions examples/05_kriging/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ the features you want:
one can set `exact` to `False` and provide either individual measurement errors
for each point or set the nugget as a constant measurement error everywhere.
* `pseudo_inv`: Sometimes the inversion of the kriging matrix can be numerically unstable.
This occures for examples in cases of redundant input values. In this case we provide a switch to
This occurs for examples in cases of redundant input values. In this case we provide a switch to
use the pseudo-inverse of the matrix. Then redundant conditional values will automatically
be averaged.

Expand Down Expand Up @@ -87,5 +87,9 @@ submodule :any:`gstools.krige`.
ExtDrift
Detrended

Gallery
-------
.. only:: html

Gallery
-------

Below is a gallery of examples
8 changes: 6 additions & 2 deletions examples/06_conditioned_fields/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ or:

srf.set_condition(cond_pos, cond_val, "ordinary")

Gallery
-------
.. only:: html

Gallery
-------

Below is a gallery of examples
8 changes: 6 additions & 2 deletions examples/07_transformations/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ Simply import the transform submodule and apply a transformation to the srf clas
...
tf.normal_to_lognormal(srf)

Gallery
-------
.. only:: html

Gallery
-------

Below is a gallery of examples
Loading