Geostatex

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:

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).

Milestone Scope Status
M0 Foundation: Mix + Rust workspace, input contract, oracle harness, CI done
M1 Experimental variogram (scikit-gstat) done
M2 Variogram models and fitting done
M3 Ordinary kriging (PyKrige) done
M4 Universal kriging done
M5 3D kriging done
M6 Directional and space-time variograms done
M7 Cross-validation done
M8 GeoFrame integration and real-data notebooks done
M9 Experimental variogram options: metrics, cross-variograms, pair sampling (0.2.0) done
M10 Model fitting: maximum likelihood, entropy weights, summed models, harmonized variograms (0.2.0) done
M11 Kriging: custom variogram functions, regression kriging (0.2.0) done
M12 Large data sets: benchmark, faster neighbour search and prediction, memory guard (0.3.0) done
M13 Simple kriging and Gaussian simulation (GSTools) (0.3.0) done
M14 Indicator kriging (0.3.0) done
M15 Block kriging and cokriging (R gstat) (0.4.0) done
M16 Classification kriging (PyKrige) (0.5.0) done
M17 GSTools covariance models (0.5.0) done
M18 Manual 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:

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:

Call 1k points 2k points 5k points 10k points
Variogram.new/3, every pair 0.013 s, 13 MB 0.049 s, 60 MB 0.29 s, 381 MB 1.3 s, 1.5 GB
Variogram.new/3, samples: 2000 0.036 s, 21 MB 0.14 s, 90 MB 0.13 s, 90 MB 0.14 s, 93 MB
Kriging.ordinary/3, automatic variogram 0.007 s 0.020 s 0.10 s 0.41 s, 4 MB
Kriging.execute/5, every point 0.098 s, 37 MB 0.57 s, 154 MB 7.9 s, 940 MB 51 s, 3.8 GB
Kriging.execute/5, n_closest_points: 20 0.003 s 0.003 s 0.003 s 0.004 s
Kriging.statistics/1 1.5 s, 227 MB 15 s, 928 MB
Kriging.cross_validate/4, every point 11 s, 580 MB
Kriging.cross_validate/4, n_closest_points: 20 0.60 s 4.3 s

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/.