Skip to content

The rainfall network

limen_ml.rainfall_cnn

A small 1D-CNN over a week of hourly rain: the probability of a landslide.

The network is deliberately small (three convolutions and a sigmoid head) so that once exported to ONNX it weighs tens of kilobytes and scores a cell in milliseconds on a CPU.

  • :func:build_rainfall_cnn: an untrained network (needs the dl extra, PyTorch);
  • :func:train_rainfall_cnn: train it on windows of :data:SEQUENCE_LENGTH_HOURS and export it to ONNX;
  • :class:RainfallProbability: score a window with the ONNX model (needs the serve extra, onnxruntime). Without a usable model it answers 0.5, "no signal", so the caller never crashes on it.

RainfallProbability

An ONNX rainfall model: predict(window) is P(landslide | window).

Answers :data:NEUTRAL when onnxruntime is missing, the model file is missing or unreadable, or a prediction fails.

predict

predict(window: Sequence[float]) -> float

P(landslide | window); :data:NEUTRAL on any failure.

build_rainfall_cnn

build_rainfall_cnn() -> Any

A fresh, untrained 1D-CNN. The caller sets up the optimiser.

train_rainfall_cnn

train_rainfall_cnn(sequences: Any, labels: Any, *, epochs: int = 12, batch_size: int = 64, learning_rate: float = 0.001, onnx_path: Path | None = None) -> TrainedCNN

Train the network on sequences ([N, 168]) and labels ([N]).

With onnx_path the trained network is exported there. Raises ValueError on an empty input or windows of the wrong length.

export_onnx

export_onnx(model: Any, dest: Path) -> Path

Export the network to ONNX at dest, with a dynamic batch.

pad_or_trim

pad_or_trim(values: Sequence[float], *, length: int = SEQUENCE_LENGTH_HOURS) -> list[float]

The last length values, padded with zeros in front when fewer.