SciKit-GStat

repository·main·Indexed 42 days ago

https://github.com/mmaelicke/scikit-gstat

A SciPy-styled geostatistical toolbox for Python designed for variogram estimation and spatial analysis. It provides tools for Ordinary Kriging, directional and spatio-temporal variograms, and theoretical models including spherical, exponential, and Gaussian. Key classes include Variogram, DirectionalVariogram, SpaceTimeVariogram, and OrdinaryKriging, along with utilities for binning, semi-variance estimators, and Jackknife cross-validation.

Tokens
20K
Snippets
71
Records
91
Agent score
54%

What's inside SciKit-GStat

  1. Overview of Variogram Estimators and Models

    main

    SciKit-GStat provides a variety of semi-variance estimators and variogram model functions. Most estimators are implemented using numba's JIT decorator for performance.

    Available Estimators

    • matheron
    • cressie
    • dowd
    • genton
    • entropy
    • quantiles (experimental)
    • minmax (experimental)

    Available Models

    Models are available in both nugget and no-nugget variations:

    • spherical
    • exponential
    • gaussian
    • cubic
    • stable
    • matérn
  2. What is geostatistics and how does it differ from IDW?

    main

    Geostatistics aims to describe and estimate spatial covariance (correlation) in point data. It assumes that closer observation points show higher covariances than distant points.

    Inverse Distance Weighting (IDW)

    IDW is a simple interpolation technique where a value at an unobserved location is estimated as a distance-weighted mean of surrounding points. The weight $w_i$ is inversely proportional to the Euclidean distance between the unobserved point and the sample point:

    $$w_i = \frac{1}{|\overrightarrow{ux_i}|}$$

    Geostatistical Approach

    Unlike IDW, which relies exclusively on distance, geostatistics derives weights from the actual (co)variance found in the observations. This allows the interpolation to incorporate the specific spatial structure of the data, though it requires stricter statistical assumptions.

  3. Use binning functions to aggregate distance matrices

    main

    SciKit-GStat provides several binning functions used to spatially aggregate a distance matrix into lag classes (bins). These functions are located in the skgstat.binning module and are typically used to group distances into meaningful intervals for variogram analysis.

    Available binning methods include:

    • even_width_lags: Creates bins with equal widths.
    • uniform_count_lags: Creates bins with an equal number of samples.
    • auto_derived_lags: Automatically derives lag classes.
    • kmeans: Uses K-means clustering to define bins.
    • ward: Uses Ward's linkage method for binning.
    • stable_entropy_lags: Uses entropy-based methods to define stable lags.
  4. Tune the precision hyperparameter in OrdinaryKriging

    main

    The precision attribute in OrdinaryKriging is a hyperparameter that controls the accuracy of the semivariance estimation in estimate mode.

    • Higher precision: Decreases the lag deviation and results in values closer to the exact calculation, but increases calculation time (potentially by an order of magnitude).
    • Lower precision: Increases calculation speed but may lead to visible differences in the reconstructed variogram or prediction weights.

    The relationship between range and the uncertainty of the lag distance can be thought of as:

    d = range / precision

    Increasing the precision value decreases the lag deviation.

  5. Use standalone estimator functions in skgstat.estimators

    main

    Scikit-GStat provides several semi-variance estimator functions in the skgstat.estimators submodule. These functions can be used independently of the Variogram class.

    Important Requirement: When using these functions directly (instead of through the Variogram class), you must pass an array of pairwise differences to the function, rather than the raw data values themselves.

  6. Configure the Directional Variogram search area

    main

    The search area determines which point pairs are considered for the semivariance calculation. You control the shape, orientation, and extent of this area using the following parameters in the DirectionalVariogram class:

    • directional_model: Determines the shape of the search area. Supported values are 'triangle' (default), 'circle', and 'compass'.
    • azimuth: Rotates the search area. 0° is heading East. Positive values rotate the area clockwise; negative values rotate it counter-clockwise.
    • tolerance: Specifies the angular tolerance (in degrees) relative to the azimuth. It defines how far the angle between two points can deviate from the azimuth to still be considered a directional pair.
    • bandwidth: Defines the maximum coordinate distance a point can have from the azimuth line.

    Shape-specific behaviors:

    • triangle: Uses both tolerance and bandwidth to define the area.
    • circle: Uses bandwidth to define the area.
    • compass: Uses tolerance but does not use bandwidth.
  7. Mutate Variogram parameters in place

    main

    A key feature of the Variogram class is the ability to mutate parameters in place. When you update attributes such as n_lags, maxlag, or bin_func, the object automatically invalidates dependent results and recalculates the experimental variogram and fitted model.

    # Mutating parameters triggers automatic recalculation
    V.n_lags = 25
    V.maxlag = 500
    V.bin_func = 'kmeans'
    
    # The model is now fitted to the new configuration
    V.plot()
  8. Understand the return format of skgstat.data functions

    main

    When calling functions within the skgstat.data module, the returned object is a dictionary containing two specific keys:

    1. 'sample': The actual data values/coordinates used for analysis.
    2. 'description': A string describing the dataset.

    Always access the data via the 'sample' key when passing it to skgstat estimators or variogram models.

    from skgstat import data
    
    dataset = data.pancake()
    # Access the data for use in scikit-gstat
    sample_data = dataset['sample']
    print(dataset['description'])
  9. How Directional Variograms work

    main

    A DirectionalVariogram is a specialized variogram where point pairs are only included in the semivariance calculation if they satisfy a specific spatial relationship. This relationship is defined by a search area around a point of interest (poi).

    While DirectionalVariogram can handle n-dimensional coordinates, the directional dependency is only applied to the first two dimensions.

    Key characteristics:

    • It inherits most behaviors from the base Variogram class.
    • It uses a directional_model to define the shape of the search area.
    • Certain methods like .bins() and ._calc_groups() have been rewritten to accommodate directional dependency and may behave differently than the standard Variogram.
    from skgstat import DirectionalVariogram
    
    # Usage follows the standard Variogram pattern
    dv = DirectionalVariogram(coords, vals, azimuth=0, tolerance=45, directional_model='triangle')
  10. Understand the three main fields of geostatistics

    main

    Geostatistics can be categorized into three main functional areas. While scikit-gstat focuses primarily on the first, it provides interfaces to connect with other libraries for the others:

    1. Variography: Focuses on describing, visualizing, and modelling covariance structures in space and time using the variogram as the primary tool. scikit-gstat is primarily a variography tool.
    2. Kriging: A family of interpolation methods that use a variogram to estimate weights for spatial estimation.
    3. Geostatistical Simulation: Aims to generate random fields that fit a given set of observations or a pre-defined variogram/covariance function.

    To perform Kriging or Simulation using a variogram modeled in scikit-gstat, you can use the Variogram.to_gstools() method to export your model to the gstools library.

  11. Understand the concept of Kriging for spatial interpolation

    main

    In geostatistics, spatial interpolation is known as Kriging. It is a method used to estimate a variable at an unobserved location using observations from nearby locations.

    Kriging is considered a BLUE (Best Linear Unbiased Estimator) because it utilizes the spatial structure described by a variogram to determine the optimal weights for averaging observations. The estimation at an unobserved location $Z^*(s_0)$ is calculated as a weighted mean of the observations $Z(s_i)$:

    $Z^*(s_0) = \sum_{i=0}^N \lambda_i Z(s_i)$

  12. Core classes in SciKit-GStat

    main

    SciKit-GStat is a SciPy-styled module for variogram analysis. Most users will primarily interact with the Variogram class, but the library provides specialized classes for different geostatistical tasks:

    • Variogram: The base class for standard variogram analysis.
    • DirectionalVariogram: Used for computing directional variograms.
    • SpaceTimeVariogram: Used for spatio-temporal variogram analysis.
    • OrdinaryKriging: Used for spatial interpolation.
    • MetricSpace: Used for working with pre-computed spatial samples.

    All variogram classes share a similar interface, allowing them to compute experimental variograms and fit theoretical variogram model functions.