Metadata-Version: 2.4
Name: geossm
Version: 3.0.1
Summary: GeoSSM: geostatistics state-space modeling tools
Author-email: Jacopo Rodeschini <jacopo.rodeschini@unibg.it>
License-Expression: MIT
Project-URL: Homepage, https://github.com/jacopoRodeschini/geossm
Project-URL: Repository, https://github.com/jacopoRodeschini/geossm
Project-URL: Issues, https://github.com/jacopoRodeschini/geossm/issues
Keywords: geospatial,state-space-models,python,geostatistics
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Operating System :: POSIX :: Linux
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: psutil>=6.0.0
Requires-Dist: matplotlib>=3.9.1
Requires-Dist: pandas>=2.2.2
Requires-Dist: meshio>=5.3.5
Requires-Dist: geopandas>=1.1.2
Requires-Dist: gmsh>=4.15.0
Requires-Dist: jax>=0.6.2
Requires-Dist: jaxlib>=0.6.2
Requires-Dist: mfem>=4.8.0.1
Requires-Dist: gstools>=1.7.0
Requires-Dist: numpy>=2.2.6
Requires-Dist: patsy>=1.0.2
Requires-Dist: pygmsh>=7.1.17
Requires-Dist: pyarrow>=16.0.0
Requires-Dist: pyogrio>=0.12.1
Requires-Dist: pyproj>=3.7.1
Requires-Dist: scipy>=1.15.3
Requires-Dist: shapely>=2.1.2
Requires-Dist: statsmodels>=0.14.6
Provides-Extra: test
Requires-Dist: pytest>=8.0; extra == "test"
Provides-Extra: docs
Requires-Dist: sphinx>=7.0; extra == "docs"
Requires-Dist: furo>=2024.1.29; extra == "docs"
Requires-Dist: sphinx-design>=0.5.0; extra == "docs"
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: pytest-cov>=5.0; extra == "dev"
Requires-Dist: ruff>=0.6; extra == "dev"
Requires-Dist: mypy>=1.11; extra == "dev"
Requires-Dist: build>=1.2; extra == "dev"
Requires-Dist: twine>=5.1; extra == "dev"
Requires-Dist: pre-commit>=3.7; extra == "dev"
Provides-Extra: interactive
Requires-Dist: ipykernel>=6.29.5; extra == "interactive"
Requires-Dist: ipympl>=0.9.4; extra == "interactive"
Requires-Dist: ipython>=8.27.0; extra == "interactive"
Requires-Dist: ipywidgets>=8.1.5; extra == "interactive"
Requires-Dist: markdown-it-py>=3.0.0; extra == "interactive"
Dynamic: license-file

<p align="center">
  <!-- Dark mode -->
  <img src="docs/images/logo-white.svg#gh-dark-mode-only" width="320">

  <!-- Light mode -->
  <img src="docs/images/logo-dark.svg#gh-light-mode-only" width="320">
</p>

# Geossm 🌍
> **Geo**statistics with **S**tate **S**pace **M**odels

**geossm** is a Python package for applying **state space models** to **spatial and spatiotemporal data**. It is tailored for modern **geostatistical workflows** and natively operates on `GeoDataFrame` objects from the `geopandas` library.

The package is designed with **scalability** and **modularity** in mind, making it suitable for large spatial and spatiotemporal datasets across environmental, climate, and geospatial applications. 


## Table of Contents

- [Overview](#overview)
- [Key Features](#key-features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Examples](#examples)
- [Documentation](#documentation)
- [Contributing](#contributing)
- [License](#license)

## Overview

State space models (SSMs) are powerful statistical tools for modeling dynamic systems. This package extends their application to **geospatial and spatiotemporal contexts**, enabling:

- Efficient filtering and smoothing of spatial processes
- Low-rank approximations for scalability
- Seamless integration with geospatial data workflows
- Support for complex environmental and climate datasets

The package is built on the research presented in the PhD thesis: *A State-Space Modelling Framework in Geostatistics with Application to Environmental Data* by Jacopo Rodeschini.

### Project structure
```
geossm/
├── pyproject.toml         <-- All package config
├── environment.yml        <-- For Conda users
├── README.md
├── LICENSE
├── src/                   <-- The "Source" folder
│   └── geossm/            <-- The actual package folder
│       ├── __init__.py
│       ├── datasets/      <-- Submodule for the datasets
│       ├── ssm/           <-- Submodule for the State-Space model
│       ├── stmodel/       <-- Submodule for the Spatio-temporal model
│       ├── covmodel/      <-- Submodule for the covariance functions
│       ├── data_preparation.py  <-- Design matrix construction utilities
│       └── utils.py       <-- Shared helper functions
└── tests/
```

### Available Datasets
The **geossm** package includes spatio-temporal datasets for benchmarking and testing different modelling approaches.

- **Agrimonia Dataset**: Fassò, Alessandro, et al.  
*"Agrimonia: a dataset on livestock, meteorology and air quality in the Lombardy region, Italy."*  
Scientific Data 10.1 (2023): 143.

```python
import geossm
import geossm.datasets as datasets

# List available datasets
print(datasets.list_datasets())

# Output
Dataset         Type       Ext     Size (MB)
--------------------------------------------
agrimonia       data       csv        10.551 *
agrimonia       geometry   shp         0.005
```
The `*` symbol indicates dataset more than 10MB.

### How to build the regression dataset

1. Create a GeoPandas DataFrame with `geometry` and `time` columns.
2. Create the `Builder` object.
3. Call the `.build()` method.

<img src="docs/images/workflow_data_process.png" alt="data process" width="320">


## Key Features

- **Seamless GeoDataFrame Integration**: Work directly with `geopandas.GeoDataFrame` objects
- **State Space Modeling**: Tools for building, estimating, filtering, and smoothing spatial processes
- **Low-Rank Approximations**: Efficient handling of large-scale spatial data via LRSSM
- **Modular Pipeline**: 
  - Data preprocessing and validation
  - Design matrix construction
  - Model specification and estimation
  - Prediction and simulation
- **Research-Oriented**: Built for extensibility and experimental workflows
- **Multiple Model Types**: Support for linear time-invariant and time-varying SSMs

## Requirements
- **OS**: Linux (x86-64). macOS and Windows are not supported: the `mfem` wheels required on Python 3.10 are Linux-only.
- **Python**: 3.10 to 3.14. These are the tested versions; on 3.10, pip resolves `jax` 0.6.x, the last JAX series that supports it.
- **Key Dependencies**:
  - `geopandas` ≥ 1.1.2 (geospatial data handling; `pyarrow`, `pyogrio` and `pyproj` for its I/O and CRS support)
  - `pandas` ≥ 2.2.2 (data manipulation)
  - `numpy` ≥ 2.2.6 (numerical computing)
  - `scipy` ≥ 1.15.3 (scientific computing)
  - `jax` ≥ 0.6.2 (automatic differentiation & optimization)
  - `statsmodels` ≥ 0.14.6 (statistical modeling)
  - `matplotlib` ≥ 3.9.1 (visualization)
  - Additional spatial & mesh packages: `shapely`, `gmsh`, `meshio`, `pygmsh`, `mfem`, `gstools`

[pyproject.toml](pyproject.toml) holds the complete dependency list and its supported ranges. The lower bounds are tested in CI.
The wheel includes the example datasets, so it is about 43 MB.

## Installation

### Option 1: From pip (Recommended)

```bash
pip install geossm
```

### Option 2: From Source with Conda

1. **Clone or download the repository**:
```bash
git clone https://github.com/jacopoRodeschini/geossm.git
cd geossm
```

2. **Create the conda environment** (named `geossm`). It takes the native geospatial/mesh stack from conda-forge, then uses pip to install geossm in editable mode with the `test` and `interactive` extras. Versions are not pinned: the supported ranges live in `pyproject.toml`.
```bash
conda env create -f environment.yml
```

3. **Activate the environment**:
```bash
conda activate geossm
```

4. **(GPU only) Install the CUDA build of JAX**:
```bash
pip install -U "jax[cuda12]"
```

### Option 3: From Source with uv

[uv](https://docs.astral.sh/uv/) installs geossm and its dependencies from PyPI wheels into a plain virtual environment, without conda. It can also download the Python interpreter itself.

1. **Install uv** (once):
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh    # or: pip install uv
```

2. **Clone the repository**:
```bash
git clone https://github.com/jacopoRodeschini/geossm.git
cd geossm
```

3. **Create and activate a virtual environment** (`.venv` in the repository; any supported Python, 3.10 to 3.14, is downloaded by uv if it is not installed):
```bash
uv venv --python 3.12
source .venv/bin/activate
```

4. **Install geossm** in editable mode, with the test dependencies:
```bash
uv pip install -e ".[test]"
```
Add other extras as needed, e.g. `".[test,docs,interactive]"`.

5. **(GPU only) Install the CUDA build of JAX**:
```bash
uv pip install -U "jax[cuda12]"
```

The `gmsh` wheel needs a few system OpenGL/X libraries. On a minimal Debian/Ubuntu system (e.g. a container), install them with:
```bash
sudo apt-get install -y libglu1-mesa libxcursor1 libxft2 libxinerama1
```

Use the activated environment (`python`, `pytest`, ...) rather than `uv run`: the repository has no `uv.lock`, and `uv run` would create one and re-sync the environment on every call. To remove the environment, delete the `.venv` directory.

### Verify Installation

```python
import geossm
print(geossm.__version__)
```

### Remove the package and the environment
Remove the package
```bash
pip uninstall geossm
```

Remove an entire environment
```bash
conda remove -n geossm --all
```

### JAX configuration (CPU/GPU, memory, float64)

geossm computes with JAX. Some JAX settings are process-wide, so geossm never
changes them itself. Set them at the top of your script, **before** importing
JAX or geossm:

```python
import os
os.environ.setdefault("JAX_PLATFORMS", "cpu")                   # CPU only: leaves the GPU untouched
os.environ.setdefault("XLA_PYTHON_CLIENT_PREALLOCATE", "false") # GPU: allocate on demand, not 75 % upfront
os.environ.setdefault("JAX_COMPILATION_CACHE_DIR", os.path.expanduser("~/.cache/jax"))  # reuse compiled kernels

import jax
jax.config.update("jax_enable_x64", True)  # only if you use dtype=jnp.float64 (otherwise a ValueError is raised)
```

Without `JAX_PLATFORMS=cpu`, JAX reserves 75 % of the GPU memory as soon as a
model is created, even with `backend="cpu"`. See the user guide page
[JAX configuration](https://jacopoRodeschini.github.io/geossm/user_guide/jax_configuration.html)
for details and measurements.

## Quick Start

### Loading Data

```python
import geossm
import geossm.datasets as datasets

# List available datasets
print(datasets.list_datasets())

# Load the Agrimonia dataset
agrimonia_gdf, shapefile = datasets.load_dataset('agrimonia')
print(agrimonia_gdf.head())
print(agrimonia_gdf.columns)
```

### Building a Low-Rank State Space Model

This walks through the core steps: load data, build a spatial mesh, define a
covariance function, and fit the model. The full runnable script (including
plotting) is available at
[examples/example_LRSSM_build.py](examples/example_LRSSM_build.py).

```python
import geossm.datasets as df
from geossm.stmodel import LRStateSpaceModel as lrssm
from geossm.covmodel import SPDEApproxCov, buildMesh2d

# 1. Load a dataset and the domain
agri, shape = df.load_dataset('agrimonia')
domain = list(shape.geometry[0].geoms)[0].buffer(0.3)

# 2. Define the model formula and the observation domain
model = lrssm(agri, ['np.log(AQ_pm10) ~ 1 + WE_temp_2m'], verbose=True, domain=[domain])

# 3. Build a mesh over the domain for the latent spatial field with gmsh
points = model.points[0]
mesh_io, poly = buildMesh2d(points, lowrank=0.75,gradation=1, offset = 1, anchor_spacing=1)

# 4. Build the latent covariance function on the mesh and attach it
cov_fun = SPDEApproxCov(latlon=True).setup(mesh_io, domain=domain)
model = model.setup(cov_fun=[cov_fun])

# 5. Fit the model
results = model.fit()
print(results.summary())
```

> **Note**: `build_mesh` above is simplified for readability. See
> [examples/example_LRSSM_build.py](examples/example_LRSSM_build.py) for the
> full mesh-generation code (mesh-density tuning options) and plotting
> utilities (`FEMSolver.plot_mesh`).

## Examples

The [examples/](examples/) directory contains runnable scripts demonstrating:

- **Data Loading**: [example_datasets_load.py](examples/example_datasets_load.py) — Load and explore geospatial datasets
- **Grid Operations**: [example_datasets_grid.py](examples/example_datasets_grid.py) — Create and manipulate spatial grids
- **SSM Building**: [example_SSM_build.py](examples/example_SSM_build.py) — Construct basic state space models
- **SSM Estimation**: [example_SSM_estimate.py](examples/example_SSM_estimate.py) — Estimate model parameters
- **Filtering & Smoothing**: [example_SSM_filter.py](examples/example_SSM_filter.py), [example_SSM_smooth.py](examples/example_SSM_smooth.py) — Apply Kalman filter and smoother
- **Low-Rank SSM**: [example_LRSSM_build.py](examples/example_LRSSM_build.py), [example_LRSSM_estimate.py](examples/example_LRSSM_estimate.py), [example_LRSSM_simulate.py](examples/example_LRSSM_simulate.py) — Efficient large-scale modeling
- **Mesh & FEM**: [example_mesh_1.py](examples/example_mesh_1.py), [example_spde_pyfem.py](examples/example_spde_pyfem.py), [example_GP_FEM_1.py](examples/example_GP_FEM_1.py) — Finite element methods integration

Run any example with:
```bash
cd examples
python example_datasets_load.py
```

## Documentation

The full documentation — installation, quick start, user guide, examples,
and API reference — is built with [Sphinx](https://www.sphinx-doc.org/)
and published to GitHub Pages at:
**https://jacopoRodeschini.github.io/geossm/**

- **Module Reference**:
  - `geossm.ssm` — Core state space modeling
  - `geossm.stmodel` — Spatiotemporal models (LRSSM, SSM variants)
  - `geossm.datasets` — Built-in datasets and data loaders
  - `geossm.data_preparation` — Data preprocessing utilities
  - `geossm.covmodel` — Covariance model specifications

For detailed information on specific functions and classes, use Python's built-in help:
```python
import geossm
help(geossm.ssm.StateSpaceModel)
```

### Building the docs locally

```bash
pip install -e ".[docs]"
sphinx-build -b html docs/source docs/build/html
python -m http.server --directory docs/build/html 8000  # then open http://localhost:8000
```

`docs/build/` is generated output and is not committed (see
[.gitignore](.gitignore)). A GitHub Actions workflow
([.github/workflows/docs.yml](.github/workflows/docs.yml)) builds the docs
on every push/PR touching `docs/`, `src/`, or `examples/`, and deploys them
to GitHub Pages on pushes to `main`.

## Run the tests 

In the activated environment (`conda activate geossm`, or `source .venv/bin/activate` with uv):

```bash
JAX_PLATFORMS=cpu python -m pytest tests -q -p no:cacheprovider
```

## Contributing

Contributions are welcome! In short: fork the repository, create a branch off
`develop` (`feature/your-feature` or `fix/your-bug`), and open a Pull Request
into `develop`.

See [CONTRIBUTING.md](CONTRIBUTING.md) for the full guide, including the
branching strategy, local dev setup, and the PR checklist. For questions or
bug reports, please open an [Issue](../../issues).

## License

This project is licensed under the MIT License — see the [LICENSE](LICENSE) file for details.

## References

- Rodeschini, J. (2025). *A State-Space Modelling Framework in Geostatistics with Application to Environmental Data*. PhD Thesis, University of Bergamo.

- Rodeschini, J., Tedesco, L., Finazzi, F., Otto, P., & Fassò, A. (2025). *Multivariate Low-Rank State-Space Model with SPDE Approach for High-Dimensional Data*. Spatial Statistics.

## Citation

If you use **GEOSSM** in your research, please cite:

```bibtex
@article{rodeschini2025multivariate,
title = {Multivariate low-rank state–space model with SPDE approach for high-dimensional data},
author = {Jacopo Rodeschini and Lorenzo Tedesco and Francesco Finazzi and Philipp Otto and Alessandro Fassò},
journal = {Spatial Statistics},
volume = {73},
pages = {100971},
year = {2026},
issn = {2211-6753},
doi = {https://doi.org/10.1016/j.spasta.2026.100971},
url = {https://www.sciencedirect.com/science/article/pii/S2211675326000199},
}
```

## Contact

**Author**: Jacopo Rodeschini  

---

<p align="center">
  Made with ❤️ for geospatial data science
</p>
