Metadata-Version: 2.4
Name: limen-core
Version: 0.5.0
Summary: The joins of Limen: what rain, landslides, fires, floods and earthquakes mean together, as pure functions over the open-data libraries
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: Typing :: Typed
Requires-Dist: pydantic>=2.9
Requires-Dist: pyyaml>=6.0
Requires-Dist: rainfall-thresholds~=0.5.0
Requires-Dist: fwi-chain~=1.3
Requires-Python: >=3.12
Project-URL: Homepage, https://github.com/agent-engineering-studio/limen-core
Project-URL: Issues, https://github.com/agent-engineering-studio/limen-core/issues
Project-URL: Documentation, https://agentengineering.it/limen-core/
Project-URL: Changelog, https://github.com/agent-engineering-studio/limen-core/blob/main/CHANGELOG.md
Description-Content-Type: text/markdown

# limen-core

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

**limen-core** holds the joins of [Limen](https://github.com/agent-engineering-studio/limen),
an open early-warning system for landslides, floods and wildfires in Italy.

Each open-data library of the project reads one source and stops there:
`dpc-radar` the rain radar, `ispra-idrogeo` the landslide inventory,
`firms-client` the fires seen from satellite, and so on. None of them imports
another. What two sources say *together* is a join, and it lives here: a burn
seen by satellite inside a mapped perimeter, an earthquake and the rain that
follows it, an index we compute next to the official one.

```mermaid
flowchart LR
    L1["dpc-radar · rainfall-thresholds<br/>ispra-idrogeo · egms-client"] --> C
    L2["firms-client · effis-client<br/>fwi-chain"] --> C
    L3["glofas-ewds · copernicus-ems<br/>ingv-shakemap"] --> C
    C["limen-core<br/>the joins, pure functions"] --> A["Limen<br/>database · jobs · API · map"]
```

It is **alpha**. Two things are in it: the scoring engines of Limen, moved
as they are with the tests they had, and three joins rewritten as plain
functions and checked number by number against the code they come from.

## What is in it

| Module | What it answers | It joins |
|---|---|---|
| `limen_core.seismic` | how long a slope stays shaken, and how much less rain it takes after an earthquake | the shaking of `ingv-shakemap` with the thresholds of `rainfall-thresholds` |
| `limen_core.post_fire` | for how long, and how much, a burnt slope is more ready to fail | the fire power of `firms-client` with the perimeters of `effis-client` |
| `limen_core.landslide` | the rain before an instant at a place, against the threshold of the place, lowered after an earthquake | a `RainSource` (`dpc-radar`), the thresholds of `rainfall-thresholds`, `seismic` |
| `limen_core.wildfire` | what the satellite saw inside a burnt perimeter around its day: power, and power per hectare; when each cell last burnt and how hard (`latest_fire`) | the detections of `firms-client` with the perimeters of `effis-client`; the density feeds `post_fire` |
| `limen_core.flood` | a forecast river peak with how far to trust it, when the fluvial branch should not speak alone, and whether a place was seen flooded, dry or not seen | the credibility and trust of `glofas-ewds`, the water over a place of `copernicus-ems` |
| `limen_core.agreement` | how far two measures of the same thing are from each other | the index of `fwi-chain` with the official ones of `effis-client` and `glofas-ewds` |

```python
from datetime import UTC, datetime

from limen_core.post_fire import PostFireParams, frp_density, frp_severity, post_fire_factor
from limen_core.seismic import SeismicParams, Shaking, ThresholdMemory, rain_threshold_reduction

# An earthquake a month ago: the ground is still, the slope is not as it was.
params = SeismicParams(
    tau_days=2,
    min_magnitude=4,
    lookback_days=7,
    pga_threshold_g=0.1,
    pga_scale_g=0.03,
    pga_min_g=0.02,
)
memory = ThresholdMemory(max_reduction=0.3, memory_days=90, lookback_days=365)
quake = Shaking(datetime(2026, 10, 29, 12, tzinfo=UTC), magnitude=5.5, pga_g=0.2)
lower = rain_threshold_reduction(
    [quake], as_of=datetime(2026, 11, 28, 12, tzinfo=UTC), params=params, memory=memory
)
# 0.21: the rain threshold of this place is 21 % lower today.

# A burn of 100 hectares where the satellite read 150 MW, six months ago.
fire = PostFireParams(
    peak_months=6,
    curve_denominator=72,
    window_months_max=24,
    severity_floor=0.4,
    frp_floor_mw_per_ha=0.2,
    frp_saturation_mw_per_ha=2.2,
)
severity = frp_severity(frp_density(150.0, 100.0), params=fire)  # 0.65
post_fire_factor(6.0, params=fire, severity=severity)  # 0.79
```

The numbers in the examples are made up to show the calls: they are not a
calibration.

## The rules

- **Pure functions.** No database, no network, no clock: the time is always an
  argument. Whatever needs I/O stays in Limen. The one file read is the
  calibration, from the folder the application names.
- **Calibration is not shipped.** No threshold, weight or time constant has a
  default here unless it switches something off. The values belong to whoever
  measured them, and Limen keeps them in its configuration.
- **Not measured is not zero.** `None` stays `None`: a burnt perimeter with no
  detection inside is not a weak fire.
- **One direction.** limen-core may use the libraries; no library imports
  limen-core, and limen-core never imports Limen. A test checks the last one.
- **Checked against the code it replaces.** Every function that comes from
  Limen has a test that compares it, on hundreds of seeded inputs, with Limen's
  own code frozen in `tests/limen_reference.py`.
- **Nothing here is an alert.** These are factors and measures. The official
  warning is the criticality bulletin of the Civil Protection Department.

## Conventions shared with the libraries

- **The instant of a dated landslide.** With a time: that time, read as
  Europe/Rome. With no time: the next local midnight in Europe/Rome after its
  day; an interval gives the end of its last day. `ispra-idrogeo` computes it
  (`dated.instant_utc`).
- **Records.** Landslides and assessments travel between packages as
  `opendata-record/v2`, the schema `egms-client` publishes.

## The scoring engines

Limen's engines live here since 0.2.0, as they were in Limen: the same code
and the same tests, so the same numbers.

| Package | What it holds |
|---|---|
| `limen_core.models` | what an engine reads and gives back: the features of a cell, the score with its breakdown, the hazards |
| `limen_core.scoring` | the landslide, flood and wildfire engines, their factors, and the schema of their calibration |
| `limen_core.cascades` | what one hazard does to another: a fire and the flood after it, three hazards on one municipality |

**The calibration stays with the application.** The engines read thresholds,
weights and class cut-offs from YAML files, and this package ships none: the
numbers belong to whoever measured them. Name the folder once:

```python
from limen_core import calibration
from limen_core.scoring import score

calibration.use(
    "/path/to/config"
)  # hazards/landslide.yaml, flood.yaml, wildfire.yaml, cascades.yaml
result = score(bundle)  # a CellFeatureBundle in, a RiskScore out
```

or set `LIMEN_CALIBRATION_DIR`. A loader given an explicit path looks nowhere
else.

What did **not** come: the choice of the engine from Limen's settings, and the
machine-learning engine with its model registry. They are wiring, and they
stay in Limen.

The moved code keeps Limen's names, some of them Italian, and uses pydantic.
It will be cleaned up a piece at a time; until then the two styles sit side
by side, and where a moved module and a rewritten one say the same thing
(`scoring.seismic_decay` and `seismic`, `scoring.wildfire.confronto` and
`agreement`) a test checks that they give the same numbers.

## Where it stands

Done: the engines (`models`, `scoring`, `cascades`), the joins `landslide`,
`wildfire`, `flood`, `seismic`, `post_fire`, `agreement`, and `cases` with
the measure of ground motion before a landslide.

Next:

1. Limen calls the joins instead of its own code: the FRP of the burnt
   perimeters (its SQL), the instant and the rain of a landslide, the fluvial
   decision with `hold_back`, the flood truth set with `observed`;
2. the scoring engines take the joins as inputs where they now read Limen's
   own numbers, and their Italian names are cleaned up a piece at a time.

It depends on pydantic, PyYAML, `rainfall-thresholds` 0.5 and `fwi-chain` 1.3.
The other libraries are not imported: the joins take anything shaped like
their types (a rain source, a hotspot, a perimeter, a credibility, the water
over a place), so a library can be released without a release here.

## Develop

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

## Licence

Code: Apache-2.0.
