Metadata-Version: 2.4
Name: limen-ml
Version: 0.1.1
Summary: The machine learning of Limen: train, calibrate, monitor and serve hazard models on spatial folds
Keywords: machine learning,landslide,wildfire,flood,spatial cross-validation,lightgbm
Author: Giuseppe Zileni
Author-email: Giuseppe Zileni <giuseppe.zileni@gmail.com>
License-Expression: Apache-2.0
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Dist: numpy>=2.1
Requires-Dist: limen-core>=0.6,<0.8
Requires-Dist: torch>=2.4 ; extra == 'dl'
Requires-Dist: onnx>=1.17 ; extra == 'dl'
Requires-Dist: onnxruntime>=1.20 ; extra == 'serve'
Requires-Dist: lightgbm>=4.5 ; extra == 'train'
Requires-Dist: scikit-learn>=1.5 ; extra == 'train'
Requires-Dist: optuna>=4.0 ; extra == 'train'
Requires-Dist: shap>=0.46 ; extra == 'train'
Requires-Python: >=3.12
Project-URL: Homepage, https://github.com/agent-engineering-studio/limen-ml
Project-URL: Issues, https://github.com/agent-engineering-studio/limen-ml/issues
Project-URL: Documentation, https://agentengineering.it/limen-ml/
Project-URL: Changelog, https://github.com/agent-engineering-studio/limen-ml/blob/main/CHANGELOG.md
Provides-Extra: dl
Provides-Extra: serve
Provides-Extra: train
Description-Content-Type: text/markdown

# limen-ml

[Documentation](https://agentengineering.it/limen-ml/) · [Changelog](CHANGELOG.md)

**limen-ml** is the machine learning of [Limen](https://github.com/agent-engineering-studio/limen),
an open early-warning system for landslides, floods and wildfires in Italy: it
trains a hazard model on spatial folds, calibrates it, tells when the world has
drifted from its data, and serves a small network over a week of rain.

```bash
pip install "limen-ml[train]"         # LightGBM, scikit-learn, Optuna, SHAP
pip install "limen-ml[serve]"         # onnxruntime, to score with the rainfall network
pip install "limen-ml[dl]"            # PyTorch, to train it
```

```python
from limen_ml import folds, matrix, metrics, train

m = matrix.to_matrix(samples, ("static.slope_deg", "rain.rain_72h_mm", "insar.velocity_mmy"))
k = folds.spatial_block_folds(list(m.groups), 5, rng_seed=42)

best = train.tune(m, k, trials=50, seed=42)
cv = train.cross_validate(m, k, best)  # AUC-PR, Brier, out-of-fold probabilities
calibrator = train.fit_isotonic(cv.oof_prob, cv.y)
model = train.fit_final(m, best, seed=42)
print(cv.auc_pr_mean, metrics.conformal_quantiles(cv.y, calibrator.transform(cv.oof_prob)))
```

A sample is anything with `features` (blocks of numbers), `label` (0/1) and
`split_block` (its spatial block).

## The rules

- **Spatial folds, the same for every model.** Two cells a kilometre apart share
  their rain and their rock: a random split measures memory. Whole blocks go to
  one fold, and every algorithm is compared on the same folds.
- **AUC-PR, not accuracy.** Landslides are one row in many; the area under the
  precision-recall curve stays honest where accuracy does not.
- **Not measured stays not measured.** The application says which absent
  feature is NaN (a cell the satellite does not see is not a still slope) and
  which is a true zero; the logistic reference imputes with a "was missing"
  column.
- **A linear reference.** A standardised logistic regression runs next to the
  boosted trees: if they do not beat it, the non-linear structure buys nothing.
- **Reproducible.** Seeded search and folds, deterministic LightGBM, and a
  matrix hash on values rounded to nine decimals, so threads do not change it.
- **No database, no tracking, no schema.** The application brings its samples,
  its feature schema and its baselines, and logs where it wants.
- **Checked against the code it replaces.** Every function that comes from Limen
  is compared with Limen's own, frozen in `tests/limen_reference.py`.
- **A model never decides an alert.** It is a challenger, promoted only by a
  person after a backtest. The official warning is the criticality bulletin of
  the Civil Protection Department.

## Develop

```bash
make help    # the targets
make check   # ruff, mypy --strict, pytest: the gate CI runs
```

## Licence

Code: Apache-2.0.
