Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

KalmanFlow

Status
Testing Tests
Package PyPI Python versions
License License

KalmanFlow estimates an inflow contribution from noisy storage and measured discharge. It provides a three-state physical water-balance model, causal Kalman filtering, and fixed-lag Rauch–Tung–Striebel (RTS) smoothing for delayed inflow revisions.

The package is intentionally data-source agnostic: applications are responsible for parsing, cleaning, aligning, and persisting reservoir data.

KalmanFlow workflow: aligned storage and outflow observations feed a causal Kalman filter, followed by delayed fixed-lag RTS revisions; uncertainty is optional.

Overview

Reservoir inflow is important for water-supply planning and flood-response operations, but it is often difficult to measure directly. A reservoir may receive water from many tributaries, drainage areas, or stormwater inputs, so installing and maintaining flow sensors at every inflow point is not practical.

Timestamped reservoir storage and outflow data can be used to infer a net balance contribution with reverse level-pool routing: if storage and accounted outflow are known, the residual contribution can be recovered from the water balance. Direct back-calculation, however, is sensitive to sensor noise and timing differences. Small storage or outflow errors can yield unrealistic inflow spikes or negative values.

KalmanFlow estimates a more stable inflow time series from those noisy observations. After the two-observation startup phase, a causal inflow is published as observations arrive; a later observation can provide an absolute, fixed-lag-smoothed replacement for that same timestamp. It also provides documented assumptions and reviewed noise parameters for a measurable, defensible deployment.

The reported inflow is the residual term in the supplied storage and outflow balance. It is a net balance contribution, not automatically gross watershed inflow. Measured outflow should cover outlet releases, spills, and outward diversions or withdrawals as applicable. Precipitation, evaporation, seepage, and other water exchanges are not separate model terms; account for them with separate justified inputs or treat them as model mismatch. Sensor bias, storage-datum changes, and rating-curve changes are also possible mismatch sources. The linear-Gaussian model has no nonnegativity constraint, so negative estimates remain possible.

How KalmanFlow works

This 87-second animation introduces the reservoir inflow problem and the filter-and-smoother workflow, then walks through the Kalman prediction and correction steps and fixed-lag RTS revisions. It uses synthetic observations and illustrative noise settings with a three-hour smoothing lag.

https://github.com/user-attachments/assets/8f3b4a5b-467d-434d-b264-94e0a61b023e

Example results

These charts use the numerical results saved in the reservoir example notebook, using Chesbro Reservoir observations from January 1–15, 2023 and a four-hour smoothing lag.

Chesbro inflow comparison: raw water balance, six-hour centered rolling mean, revised inflow, and an upstream proxy, in cfs.

The comparison includes the raw water balance, its six-hour centered rolling mean, and the revised inflow estimate. The centered mean uses future observations; it is a retrospective comparison. The upstream series is a proxy rather than a direct measurement of total reservoir inflow.

Chesbro revised inflow with a shaded pointwise model-based 95% uncertainty interval, in cfs.

The shaded interval comes from the revised inflow covariance and configured noise settings. It describes model-based uncertainty, not independently calibrated accuracy. Negative lower bounds are preserved.

Install

KalmanFlow requires Python 3.12 or newer.

pip install kalmanflow

For experimental noise tuning, install the optional extra:

pip install "kalmanflow[tuning]"

The base installation supports filtering, smoothing, and frozen-configuration evaluation without SciPy. Noise tuning is experimental: its API, selection rules, and result schema may change between releases.

For development in this repository:

uv sync --all-groups --extra tuning

The source distribution includes the library and its Documentation pages. The full test suite, application workflows, notebooks, and reservoir data remain in the GitHub development checkout; run the test commands there.

Quick start

Use OnlineReservoirInflow when observations arrive one at a time. Timestamps must be timezone-aware and no finer than microsecond precision; the first completed initialization emits estimates for its first finite-storage/discharge anchor and the next finite storage observation. The anchor estimate uses a steady-state inflow prior based only on its own outflow, so it never looks ahead to the second storage value.

from datetime import UTC, datetime, timedelta
from kalmanflow import Observation, OnlineReservoirInflow

stream = OnlineReservoirInflow(
    # Parameters are for demonstration only. (See Configuration docs)
    q_storage=1.0,
    q_inflow=1.0,
    q_outflow=1.0,
    r_storage=100.0,
    r_outflow=25.0,
    smoothing_lag=timedelta(hours=12),
)

stream.process(
    Observation(datetime(2026, 1, 1, tzinfo=UTC), storage=10_000.0, discharge=25.0)
)
update = stream.process(
    Observation(
        datetime(2026, 1, 1, 0, 15, tzinfo=UTC),
        storage=10_001.0,
        discharge=25.5,
    )
)

for estimate in update.filtered_inflows:
    print(estimate.timestamp, estimate.value, estimate.prediction_flag)

for revision in update.revised_inflows:
    # Replace the causal value at revision.timestamp; do not add a delta.
    print(revision.timestamp, revision.value, revision.smoothing_flag)

Optional inflow uncertainty

Inflow uncertainty is opt-in. The default include_uncertainty=False keeps the existing six-column batch result, and streaming estimates expose standard_deviation=None. Set include_uncertainty=True on a batch adapter or stream constructor to include the inflow standard deviation calculated from the filter covariance:

from datetime import UTC, datetime, timedelta

import pandas as pd

from kalmanflow import (
    Observation,
    OnlineReservoirInflow,
    add_inflow_uncertainty_intervals,
    get_reservoir_inflow,
)

stream = OnlineReservoirInflow(
    q_storage=1.0,
    q_inflow=1.0,
    q_outflow=1.0,
    r_storage=100.0,
    r_outflow=25.0,
    smoothing_lag=timedelta(minutes=15),
    include_uncertainty=True,
)
stream.process(
    Observation(
        datetime(2026, 1, 1, tzinfo=UTC),
        storage=10_000.0,
        discharge=25.0,
    )
)
update = stream.process(
    Observation(
        datetime(2026, 1, 1, 0, 15, tzinfo=UTC),
        storage=10_001.0,
        discharge=25.5,
    )
)
for estimate in update.filtered_inflows:
    print(estimate.value, estimate.standard_deviation)
    print(estimate.uncertainty_interval(level=0.95))

index = pd.date_range("2026-01-01", periods=3, freq="15min", tz="UTC")
storage = pd.Series([10_000.0, 10_001.0, 10_003.0], index=index)
discharge = pd.Series([25.0, 25.5, 26.0], index=index)
result = get_reservoir_inflow(
    storage,
    discharge,
    q_storage=1.0,
    q_inflow=1.0,
    q_outflow=1.0,
    r_storage=100.0,
    r_outflow=25.0,
    smoothing_lag=timedelta(minutes=15),
    include_uncertainty=True,
)
result_with_intervals = add_inflow_uncertainty_intervals(result, level=0.95)

The opt-in batch result appends estimated_inflow_standard_deviation and revised_inflow_standard_deviation. add_inflow_uncertainty_intervals returns a copy with lower and upper columns for both estimates. These are pointwise, model-based intervals for the net inflow contribution, in the same flow units as the estimate. Bounds are not clipped at zero. A revised record replaces both the earlier causal value and its standard deviation; unreleased revisions, including the trailing row, remain NaN.

Primary APIs

API Use it when
get_reservoir_inflow You have aligned pandas storage and discharge series and scalar noise settings.
get_reservoir_inflow_from_config You have batch data and a validated ReservoirConfig.
OnlineReservoirInflow You process one reservoir’s observations as they arrive.
OnlineReservoirInflow.from_config You need a configured, checkpoint-capable streaming estimator.
OnlineInflowPipeline You are integrating a custom backend or smoother.
add_inflow_uncertainty_intervals You want normal-theory lower and upper interval columns for an opt-in batch result.
tune_inflow_noise (experimental) You want to propose five diagonal q/r noise terms by causal maximum likelihood; requires kalmanflow[tuning].
evaluate_configuration You want to assess one frozen configuration on an untouched period without searching.
Notebook-local validation.py You want hourly proxy-agreement, lag, and storage-closure validation tables and plots.

Documentation

  • Architecture: modules, state-space model, units, and library boundaries.
  • Model behavior: input contract, initialization, missing values, and batch/streaming outputs.
  • Online pipeline: lifecycle, checkpoints, and failure behavior.
  • Configuration: complete configuration example, unit conventions, and parameter selection.
  • Validation: regular comparison frames, upstream-proxy metrics, lag selection, and storage closure.
  • Noise tuning: five-parameter maximum-likelihood covariance search without true inflow labels.
  • API reference: every package-level export, plus the advanced module interfaces.

Requirements and units

Inputs must be pre-cleaned and indexed by unique, timezone-aware, strictly increasing timestamps with at most microsecond precision. Batch storage and discharge series must have exactly matching indexes. Missing storage or discharge is represented by NaN; available components still participate in a partial update.

Storage defaults to acre-feet and flow to cfs. For m³ and m³/s, import UnitSystem from kalmanflow and pass unit_system=UnitSystem.si() to the batch or streaming estimator, or set it on ReservoirConfig. Input data and noise settings must already match the selected units; inflow results use the same flow unit as discharge. See Configuration for converting existing settings and data.

The default state is [storage, inflow_rate, true_outflow_rate]. Storage and measured outflow are model inputs, and true outflow is an internal state. The public API returns causal inflow followed by absolute revised inflow values; it never returns outflow estimates.

Development

uv run --extra tuning pytest --cov --cov-report=term-missing
uv run ruff check .

Coverage includes statements and branches in the package, application scripts, notebook Python helpers, and main.py. Coverage is reported without a minimum threshold.

Metadata

Release files for kalmanflow 0.1.0a4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kalmanflow 0.1.0a4
File Size Uploaded Provenance Actions
kalmanflow-0.1.0a4.tar.gz 248.0 kB With provenance Details for kalmanflow-0.1.0a4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kalmanflow 0.1.0a4
File Interpreter ABI Platform Actions
kalmanflow-0.1.0a4-py3-none-any.whl Python 3 none any Details for kalmanflow-0.1.0a4-py3-none-any.whl

Total release size: 327.7 kB

Release files / kalmanflow-0.1.0a4.tar.gz

Download URL kalmanflow-0.1.0a4.tar.gz
Size 248.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1720c786fdaf17ba094889f5d922901fe8094584caa229294655d4e01bd1e537
BLAKE2b-256 checksum
How to use checksums
559e4410c81de18fe9ff5adfad3f858454d1e434d56c209740a4027758ef7b3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / kalmanflow-0.1.0a4-py3-none-any.whl

Download URL kalmanflow-0.1.0a4-py3-none-any.whl
Size 79.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b3d0f9791adf30fa4b85c76b012020fdd3982651aa10a00cffe80e3f9c8576dc
BLAKE2b-256 checksum
How to use checksums
4e9e54343d3dc1612f706d17d5df5d76a0bf17b098b9e59dfab319fe5caf896a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for kalmanflow 0.1.0a4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for kalmanflow-0.1.0a4.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page