Metadata-Version: 2.4
Name: pynrpf
Version: 0.4.0
Summary: Detect and correct a wrong reverse-power-flow sign in fifteen-minute net-load data (M9).
Author-email: "M. Syahman Samhan" <m.samhan@unsw.edu.au>
License: MIT License
        
        Copyright (c) 2026 Samhan Samhan
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy<3,>=1.26
Requires-Dist: pandas<3,>=2.1
Provides-Extra: fit
Requires-Dist: scikit-learn<2,>=1.4; extra == "fit"
Provides-Extra: spark
Requires-Dist: pyspark<4,>=3.4; extra == "spark"
Provides-Extra: paper
Requires-Dist: scikit-learn<2,>=1.4; extra == "paper"
Requires-Dist: xgboost<3,>=2; extra == "paper"
Requires-Dist: holidays<1,>=0.40; extra == "paper"
Requires-Dist: pyyaml<7,>=6; extra == "paper"
Requires-Dist: pyarrow<23,>=14; extra == "paper"
Requires-Dist: matplotlib<4,>=3.7; extra == "paper"
Requires-Dist: jupyterlab<5,>=4; extra == "paper"
Requires-Dist: ipykernel<8,>=6; extra == "paper"
Requires-Dist: tabulate<1,>=0.9; extra == "paper"
Provides-Extra: dev
Requires-Dist: pytest<10,>=8; extra == "dev"
Requires-Dist: ruff<1,>=0.9; extra == "dev"
Requires-Dist: build<2,>=1.2; extra == "dev"
Requires-Dist: mkdocs<2,>=1.5; extra == "dev"
Requires-Dist: mkdocs-material<10,>=9; extra == "dev"
Requires-Dist: pyarrow<23,>=14; extra == "dev"
Requires-Dist: scikit-learn<2,>=1.4; extra == "dev"
Dynamic: license-file

# PyNRPF: Python for Network Reverse Power Flow

[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.19211355.svg)](https://doi.org/10.5281/zenodo.19211355)

PyNRPF detects and corrects a wrong reverse power flow (RPF) sign in fifteen-minute
substation net-load data.

## The problem

A substation meter records net load: demand minus local generation. When rooftop solar
exceeds demand the substation exports and net load is genuinely negative. Some meters store
that export as an import, so the recorded trace dips towards zero instead of crossing it.
The reading passes ordinary validation but is wrong by twice its value, and it corrupts
every minimum-demand study and net-load forecast built on it.

## The method

M9 asks one physical question of every day: for which daytime window would flipping the
recorded sign make the implied demand more plausible than keeping it, how strong is that
evidence, and is it strong enough to act on? The score needs no training; two fitted
numbers turn it into a probability; one control decides whether a day is corrected
automatically, kept automatically, or sent to a person. Raw readings are never altered.
The nine steps are one file each under `src/pynrpf/m9/` and are explained in
[docs/method.md](docs/method.md).

## Install

Python 3.10 or later. The package depends on numpy and pandas only. Pin the version in anything
that has to be reproducible:

```powershell
python -m pip install pynrpf==0.4.0
```

On Databricks, install the same pin on the cluster or job, or copy the release wheel into a
Unity Catalog Volume; [docs/install.md](docs/install.md) gives both routes.
[examples/databricks_smoke.py](examples/databricks_smoke.py) is a notebook that installs the
pin and runs the method on synthetic data, in pandas and through Spark, before any real
readings are touched.

## Quick start

```python
import pandas as pd
import pynrpf

frame = pd.read_parquet("readings.parquet")   # substation_id, timestamp, net_load_MW, solar_MW
result = pynrpf.run(frame, c=0.7)
result.site_days      # one row per site and day: outcome, probability, window, MWh
result.intervals      # one row per reading: flag and corrected net load
```

On Spark: `pynrpf.spark.run_per_site(sdf)`. From a CSV without code:
`pynrpf run readings.csv --out results/`. Column names, the control and the calibration are
documented in [docs/quickstart.md](docs/quickstart.md).

## Repository layout

```
src/pynrpf/                 the package: m9/ (one file per step), run, validate, schemas, impact, spark, cli
docs/                       one page per topic; mkdocs serve for a local site
tests/                      package tests on fixtures, paper tests on the reference run
publication/2_journal_article/
    paper/                  the code the paper's notebooks call: baselines, folds, metrics, figures, tables
    notebooks/              five notebooks that regenerate the reference run, two that draw every figure and table
    results/                the reference run, with a manifest per stage
    dataset/                the Alpha, Beta and Gamma datasets (anonymised Ausgrid data)
    config/evaluation.yaml  the one configuration of the reference run
    sandbox/                development history and dated studies; not maintained, not cited
publication/1_conference_paper/   the conference paper's archive, self-contained
```

## Branches

| branch | holds | rule |
|---|---|---|
| `main` | the released, Zenodo-archived version | changes arrive by pull request; a GitHub Release `vX.Y.Z` publishes it |
| `dev` | the 0.3 package that Ausgrid's current pipeline depends on | do not modify it, merge into it, or branch new work from it |
| feature branches | work in progress | branched from `main`, merged back by pull request |

## Data

All datasets derive from anonymised Ausgrid distribution network data and are committed so
that the paper reproduces from the repository alone: Alpha (ten stations, three years, the
wrong sign imitated by taking the absolute value), Beta (eight stations, one year, real
errors labelled by two reviewers) and Gamma (one station for the forecasting case study).
Details in [docs/data.md](docs/data.md).

## Reproducing the paper

`python -m pip install -e .[dev,paper]`, then open the notebooks under
`publication/2_journal_article/notebooks/`: `paper_figures.ipynb` and `paper_tables.ipynb`
regenerate every figure and table from the committed results in minutes; notebooks 01 to
05 regenerate the results themselves. See [docs/reproduce.md](docs/reproduce.md).

## Tests

```powershell
python -m pip install -e .[dev,paper]
pytest -q
ruff check src/pynrpf tests publication/2_journal_article/paper
```

## Documentation

| page | what it covers |
|---|---|
| [docs/quickstart.md](docs/quickstart.md) | one call, the output tables column by column |
| [docs/spark.md](docs/spark.md) | the per-site Spark adapter |
| [docs/cli.md](docs/cli.md) | the command line |
| [docs/api.md](docs/api.md) | every public name |
| [docs/method.md](docs/method.md) | the nine steps of M9 with the formulas and a worked day |
| [docs/calibration.md](docs/calibration.md) | the release numbers and refitting for a new population |
| [docs/overrides.md](docs/overrides.md) | the review-table schema for human decisions |
| [docs/data.md](docs/data.md) | the datasets and how they were labelled |
| [docs/evaluation.md](docs/evaluation.md) | how the paper judges a method |
| [docs/reproduce.md](docs/reproduce.md) | the notebooks and the reference run |
| [docs/development.md](docs/development.md) | tests, lint, continuous integration, releases |

## Licence

MIT. See [LICENSE](LICENSE).

## Acknowledgements

This project is part of Samhan's PhD study, supported by the University International
Postgraduate Award (UIPA) Scholarship from UNSW, the Industry Collaboration Project
Scholarship from Ausgrid, the RACE for 2030 Scholarship, and the NSW Decarbonisation
Innovation Hub (NSW Decarb Hub).

## Contributors

- M. Syahman Samhan (contact: m.samhan@unsw.edu.au)
- Anna Bruce
- Baran Yildiz

## Citation

If you use this software, please cite it using the metadata in [CITATION.cff](CITATION.cff),
or the archived release:

> Samhan, M. S., Bruce, A., & Yildiz, B. (2026). *PyNRPF: Detecting and rectifying wrong
> reverse power flow readings* [Software]. Zenodo. https://doi.org/10.5281/zenodo.18764943

The DOI above is the concept DOI and resolves to the latest archived release; each release
also has its own version DOI on Zenodo.
