CI Hex.pm Docs License

Geostatistics for Elixir: experimental variograms, theoretical variogram models and their fitting, and kriging, with a pure-Rust core behind a thin Rustler NIF layer.

Geostatex ports the functionality of mature geostatistics libraries and verifies its results against them:

  • scikit-gstat for variograms: lag binning, estimators, models, fitting, directional and space-time variograms;
  • PyKrige for ordinary, universal, indicator, regression and classification kriging in 2D and 3D;
  • GSTools for simple kriging, Gaussian random fields (unconditional and conditioned on data) and its covariance models;
  • R gstat for block kriging and cokriging with a linear model of coregionalisation.

It follows the conventions of its sibling library Spatex (PySAL for Elixir): every result is checked against committed fixtures generated with the reference libraries, every deviation is documented in docs/parity.md, and every random step takes a seed.

Status

The API is complete for the milestones below and still pre-1.0: minor versions may change it (see the changelog).

MilestoneScopeStatus
M0Foundation: Mix + Rust workspace, input contract, oracle harness, CIdone
M1Experimental variogram (scikit-gstat)done
M2Variogram models and fittingdone
M3Ordinary kriging (PyKrige)done
M4Universal krigingdone
M53D krigingdone
M6Directional and space-time variogramsdone
M7Cross-validationdone
M8GeoFrame integration and real-data notebooksdone
M9Experimental variogram options: metrics, cross-variograms, pair sampling (0.2.0)done
M10Model fitting: maximum likelihood, entropy weights, summed models, harmonized variograms (0.2.0)done
M11Kriging: custom variogram functions, regression kriging (0.2.0)done
M12Large data sets: benchmark, faster neighbour search and prediction, memory guard (0.3.0)done
M13Simple kriging and Gaussian simulation (GSTools) (0.3.0)done
M14Indicator kriging (0.3.0)done
M15Block kriging and cokriging (R gstat) (0.4.0)done
M16Classification kriging (PyKrige) (0.5.0)done
M17GSTools covariance models (0.5.0)done
M18Manual fit controls: fit_bounds, p0 (0.5.0)done

Notebooks

Notebooks with Python cells install their Python packages with uv through Pythonx the first time.

They use Geostatex.GeoFrame, the adapter to GeoFrame, geospatial data frames for Elixir; see each notebook's setup.

Install

def deps do
  [
    {:geostatex, "~> 0.6"}
  ]
end

The Hex package downloads a precompiled NIF for the common platforms (listed below), so no Rust toolchain is needed. On any other platform, or to build it yourself, add {:rustler, "~> 0.38", runtime: false} to your dependencies, install Rust 1.91 or newer and set GEOSTATEX_BUILD=1.

Nx tensors and Explorer series are accepted as inputs when :nx and :explorer are among your dependencies; plain lists always are.

Quick start

points = [{0, 0}, {1, 0}, {2, 0}, {3, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}]
values = [1.0, 2.0, 4.0, 3.0, 1.5, 2.5, 3.5, 3.0]

# Experimental variogram with a fitted spherical model.
variogram = Geostatex.Variogram.new(points, values, model: :spherical, n_lags: 3)
variogram.experimental

# Ordinary kriging: estimates and kriging variances at new points.
kriging = Geostatex.Kriging.ordinary(points, values, variogram_model: :spherical)
{estimates, variances} = Geostatex.Kriging.execute(kriging, :points, [0.5, 2.5], [0.5, 0.5])

The notebooks work through complete cases on real data, and docs/parity.md lists what is ported from each reference library and every deviation.

Source build versus precompiled NIF

The internal NIF module uses rustler_precompiled. It downloads a precompiled NIF from the GitHub release v<version> only when all of these hold; otherwise it compiles native/geostatex_native with Cargo:

  • GEOSTATEX_BUILD is not 1/true,
  • the version in mix.exs is not a pre-release, and
  • the file checksum-Elixir.Geostatex.Native.exs exists (it ships only inside a published Hex package).

A checkout, a GitHub dependency and CI therefore always build from source. Precompiled targets: aarch64-apple-darwin, x86_64-apple-darwin, x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl, aarch64-unknown-linux-gnu, x86_64-pc-windows-msvc, for NIF versions 2.16 and 2.17.

Development

mix deps.get
mix test
cargo test --workspace

Fixture generation (Python, dev-time only) is described in test/reference/README.md.

A dev-only benchmark of large data sets runs with MIX_ENV=prod mix run bench/large_data.exs (the prod environment builds the NIF in release mode); see the script for its options.

Large data sets

Measured with the benchmark above on 16 logical cores and 15 GB of memory (Linux, release build, 1000 prediction points); time and the peak memory the call adds:

Call1k points2k points5k points10k points
Variogram.new/3, every pair0.013 s, 13 MB0.049 s, 60 MB0.29 s, 381 MB1.3 s, 1.5 GB
Variogram.new/3, samples: 20000.036 s, 21 MB0.14 s, 90 MB0.13 s, 90 MB0.14 s, 93 MB
Kriging.ordinary/3, automatic variogram0.007 s0.020 s0.10 s0.41 s, 4 MB
Kriging.execute/5, every point0.098 s, 37 MB0.57 s, 154 MB7.9 s, 940 MB51 s, 3.8 GB
Kriging.execute/5, n_closest_points: 200.003 s0.003 s0.003 s0.004 s
Kriging.statistics/11.5 s, 227 MB15 s, 928 MB
Kriging.cross_validate/4, every point11 s, 580 MB
Kriging.cross_validate/4, n_closest_points: 200.60 s4.3 s
  • Every-pair variograms hold about 30 bytes per pair, and global kriging about five (n + 1)² matrices of f64 (4 GB at 10k points, 16 GB at 20k). Before such an allocation Geostatex compares it with the available memory (MemAvailable on Linux, a reservation probe elsewhere) and raises ArgumentError instead of letting the VM abort: use samples: for the variogram and n_closest_points: for kriging.
  • Global kriging grows as n³, the statistics and global cross-validation as n⁴: beyond a few thousand points, use a moving window.
  • Prediction, the statistics and cross-validation use every core (threads inside the NIF, tasks for the cross-validation folds) and give the same results as one at a time, bit for bit; the statistics and the folds run fewer at a time when memory is short.

Releasing (maintainers)

Publishing is an owner decision; nothing below runs automatically. The workflow .github/workflows/release.yml builds the precompiled NIFs when a v* tag is pushed (and, as a dry run, on pull requests that touch the build inputs).

  1. Set the release version in mix.exs and CHANGELOG.md, merge, then tag v<version> and push the tag. The workflow builds every target and attaches the .tar.gz artifacts to the GitHub release (the tag must equal v plus the mix.exs version).
  2. Generate the checksum file from the published artifacts: mix rustler_precompiled.download Geostatex.Native --all --print. This writes checksum-Elixir.Geostatex.Native.exs, which the Hex package then includes.
  3. mix hex.build to inspect the package, then mix hex.publish.

License

LGPL-3.0-only: see LICENSE, COPYING and NOTICE. You may use Geostatex as a dependency of software under any licence, open or closed; changes to Geostatex itself are shared under the same licence, and whoever receives your software must be able to replace Geostatex in it (with a Mix dependency and its NIF, a separate library, that is the case).

Geostatex is a port written from the sources of scikit-gstat (MIT), PyKrige, NumPy, SciPy and scikit-learn (BSD-3-Clause) and GSTools (LGPL-3.0); GSTools' licence is the one that governs the whole. Their copyright notices and licence texts are in NOTICE and LICENSES/.