Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

GSTools: A geostatistical toolbox.

Project description

Welcome to GSTools

DOI PyPI version Conda Version Build Status Coverage Status Documentation Status Code style: black

GSTools-LOGO

Purpose

GeoStatTools provides geostatistical tools for various purposes:

  • random field generation
  • conditioned field generation
  • incompressible random vector field generation
  • simple and ordinary kriging
  • variogram estimation and fitting
  • many readily provided and even user-defined covariance models
  • plotting and exporting routines

Installation

conda

GSTools can be installed via conda on Linux, Mac, and Windows. Install the package by typing the following command in a command terminal:

conda install gstools

In case conda forge is not set up for your system yet, see the easy to follow instructions on conda forge. Using conda, the parallelized version of GSTools should be installed.

pip

GSTools can be installed via pip on Linux, Mac, and Windows. On Windows you can install WinPython to get Python and pip running. Install the package by typing the following command in a command terminal:

pip install gstools

To install the latest development version via pip, see the documentation.

Citation

At the moment you can cite the Zenodo code publication of GSTools:

Sebastian Müller & Lennart Schüler. GeoStat-Framework/GSTools. Zenodo. https://doi.org/10.5281/zenodo.1313628

If you want to cite a specific version, have a look at the Zenodo site.

A publication for the GeoStat-Framework is in preperation.

Documentation for GSTools

You can find the documentation under geostat-framework.readthedocs.io.

Tutorials and Examples

The documentation also includes some tutorials, showing the most important use cases of GSTools, which are

The associated python scripts are provided in the examples folder.

Spatial Random Field Generation

The core of this library is the generation of spatial random fields. These fields are generated using the randomisation method, described by Heße et al. 2014.

Examples

Gaussian Covariance Model

This is an example of how to generate a 2 dimensional spatial random field with a gaussian covariance model.

import gstools as gs
# structured field with a size 100x100 and a grid-size of 1x1
x = y = range(100)
model = gs.Gaussian(dim=2, var=1, len_scale=10)
srf = gs.SRF(model)
srf((x, y), mesh_type='structured')
srf.plot()

Random field

A similar example but for a three dimensional field is exported to a VTK file, which can be visualized with ParaView or PyVista in Python:

import gstools as gs
# structured field with a size 100x100x100 and a grid-size of 1x1x1
x = y = z = range(100)
model = gs.Gaussian(dim=3, var=0.6, len_scale=20)
srf = gs.SRF(model)
srf((x, y, z), mesh_type='structured')
srf.vtk_export('3d_field') # Save to a VTK file for ParaView

mesh = srf.to_pyvista() # Create a PyVista mesh for plotting in Python
mesh.threshold_percent(0.5).plot()

3d Random field

Estimating and Fitting Variograms

The spatial structure of a field can be analyzed with the variogram, which contains the same information as the covariance function.

All covariance models can be used to fit given variogram data by a simple interface.

Example

This is an example of how to estimate the variogram of a 2 dimensional unstructured field and estimate the parameters of the covariance model again.

import numpy as np
import gstools as gs
# generate a synthetic field with an exponential model
x = np.random.RandomState(19970221).rand(1000) * 100.
y = np.random.RandomState(20011012).rand(1000) * 100.
model = gs.Exponential(dim=2, var=2, len_scale=8)
srf = gs.SRF(model, mean=0, seed=19970221)
field = srf((x, y))
# estimate the variogram of the field with 40 bins
bins = np.arange(40)
bin_center, gamma = gs.vario_estimate_unstructured((x, y), field, bins)
# fit the variogram with a stable model. (no nugget fitted)
fit_model = gs.Stable(dim=2)
fit_model.fit_variogram(bin_center, gamma, nugget=False)
# output
ax = fit_model.plot(x_max=40)
ax.plot(bin_center, gamma)
print(fit_model)

Which gives:

Stable(dim=2, var=1.92, len_scale=8.15, nugget=0.0, anis=[1.], angles=[0.], alpha=1.05)

Variogram

Kriging and Conditioned Random Fields

An important part of geostatistics is Kriging and conditioning spatial random fields to measurements. With conditioned random fields, an ensemble of field realizations with their variability depending on the proximity of the measurements can be generated.

Example

For better visualization, we will condition a 1d field to a few "measurements", generate 100 realizations and plot them:

import numpy as np
import matplotlib.pyplot as plt
import gstools as gs

# conditions
cond_pos = [0.3, 1.9, 1.1, 3.3, 4.7]
cond_val = [0.47, 0.56, 0.74, 1.47, 1.74]

gridx = np.linspace(0.0, 15.0, 151)

# spatial random field class
model = gs.Gaussian(dim=1, var=0.5, len_scale=2)
srf = gs.SRF(model)
srf.set_condition(cond_pos, cond_val, "ordinary")

# generate the ensemble of field realizations
fields = []
for i in range(100):
    fields.append(srf(gridx, seed=i))
    plt.plot(gridx, fields[i], color="k", alpha=0.1)
plt.scatter(cond_pos, cond_val, color="k")
plt.show()

Conditioned

User Defined Covariance Models

One of the core-features of GSTools is the powerful CovModel class, which allows to easy define covariance models by the user.

Example

Here we re-implement the Gaussian covariance model by defining just a correlation function, which takes a non-dimensional distance h = r/l:

import numpy as np
import gstools as gs
# use CovModel as the base-class
class Gau(gs.CovModel):
    def cor(self, h):
        return np.exp(-h**2)

And that's it! With Gau you now have a fully working covariance model, which you could use for field generation or variogram fitting as shown above.

Have a look at the documentation for further information on incorporating optional parameters and optimizations.

Incompressible Vector Field Generation

Using the original Kraichnan method, incompressible random spatial vector fields can be generated.

Example

import numpy as np
import gstools as gs
x = np.arange(100)
y = np.arange(100)
model = gs.Gaussian(dim=2, var=1, len_scale=10)
srf = gs.SRF(model, generator='VectorField')
srf((x, y), mesh_type='structured', seed=19841203)
srf.plot()

yielding

vector field

VTK/PyVista Export

After you have created a field, you may want to save it to file, so we provide a handy VTK export routine using the .vtk_export() or you could create a VTK/PyVista dataset for use in Python with to .to_pyvista() method:

import gstools as gs
x = y = range(100)
model = gs.Gaussian(dim=2, var=1, len_scale=10)
srf = gs.SRF(model)
srf((x, y), mesh_type='structured')
srf.vtk_export("field") # Saves to a VTK file
mesh = srf.to_pyvista() # Create a VTK/PyVista dataset in memory
mesh.plot()

Which gives a RectilinearGrid VTK file field.vtr or creates a PyVista mesh in memory for immediate 3D plotting in Python.

pyvista export

Requirements:

Optional

Contact

You can contact us via info@geostat-framework.org.

License

LGPLv3 © 2018-2020

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for gstools, version 1.2.1
Filename, size File type Python version Upload date Hashes
Filename, size gstools-1.2.1-cp35-cp35m-macosx_10_9_x86_64.whl (692.9 kB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size gstools-1.2.1-cp35-cp35m-manylinux1_i686.whl (1.3 MB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size gstools-1.2.1-cp35-cp35m-manylinux1_x86_64.whl (1.6 MB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size gstools-1.2.1-cp35-cp35m-manylinux2010_i686.whl (1.3 MB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size gstools-1.2.1-cp35-cp35m-manylinux2010_x86_64.whl (1.6 MB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size gstools-1.2.1-cp35-cp35m-win32.whl (636.9 kB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size gstools-1.2.1-cp35-cp35m-win_amd64.whl (688.7 kB) File type Wheel Python version cp35 Upload date Hashes View
Filename, size gstools-1.2.1-cp36-cp36m-macosx_10_9_x86_64.whl (704.0 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size gstools-1.2.1-cp36-cp36m-manylinux1_i686.whl (1.4 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size gstools-1.2.1-cp36-cp36m-manylinux1_x86_64.whl (1.7 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size gstools-1.2.1-cp36-cp36m-manylinux2010_i686.whl (1.4 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size gstools-1.2.1-cp36-cp36m-manylinux2010_x86_64.whl (1.7 MB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size gstools-1.2.1-cp36-cp36m-win32.whl (641.0 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size gstools-1.2.1-cp36-cp36m-win_amd64.whl (694.3 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size gstools-1.2.1-cp37-cp37m-macosx_10_9_x86_64.whl (704.4 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size gstools-1.2.1-cp37-cp37m-manylinux1_i686.whl (1.4 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size gstools-1.2.1-cp37-cp37m-manylinux1_x86_64.whl (1.6 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size gstools-1.2.1-cp37-cp37m-manylinux2010_i686.whl (1.4 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size gstools-1.2.1-cp37-cp37m-manylinux2010_x86_64.whl (1.6 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size gstools-1.2.1-cp37-cp37m-win32.whl (640.7 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size gstools-1.2.1-cp37-cp37m-win_amd64.whl (694.4 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size gstools-1.2.1-cp38-cp38-macosx_10_9_x86_64.whl (708.0 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size gstools-1.2.1-cp38-cp38-manylinux1_i686.whl (1.5 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size gstools-1.2.1-cp38-cp38-manylinux1_x86_64.whl (1.8 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size gstools-1.2.1-cp38-cp38-manylinux2010_i686.whl (1.5 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size gstools-1.2.1-cp38-cp38-manylinux2010_x86_64.whl (1.8 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size gstools-1.2.1-cp38-cp38-win32.whl (644.5 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size gstools-1.2.1-cp38-cp38-win_amd64.whl (697.5 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size gstools-1.2.1.tar.gz (12.4 MB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page