Metadata-Version: 2.5
Name: anywatch
Version: 0.1.0
Summary: Anytime-valid post-market monitoring of deployed AI systems, with subgroup guarantees, drift alarms and EU AI Act and FDA PCCP reports.
Project-URL: Homepage, https://github.com/yunusimanov/anywatch
Project-URL: Documentation, https://yunusimanov.github.io/anywatch
Project-URL: Repository, https://github.com/yunusimanov/anywatch
Project-URL: Issues, https://github.com/yunusimanov/anywatch/issues
Project-URL: Changelog, https://github.com/yunusimanov/anywatch/blob/main/CHANGELOG.md
Author-email: Yunus Imanov <yunus.imanov.fz@student.aztu.edu.az>
Maintainer-email: Yunus Imanov <yunus.imanov.fz@student.aztu.edu.az>
License-Expression: MPL-2.0
License-File: LICENSE
Keywords: ai-act,ai-governance,anytime-valid-inference,confidence-sequences,drift-detection,e-values,fda,model-monitoring,post-market-surveillance,sequential-testing
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Healthcare Industry
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: jinja2>=3.1
Requires-Dist: numpy>=1.24
Provides-Extra: dev
Requires-Dist: mkdocs-material<10,>=9.5; extra == 'dev'
Requires-Dist: mkdocs<2,>=1.6; extra == 'dev'
Requires-Dist: mkdocstrings[python]>=0.26; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: nox>=2024.4; extra == 'dev'
Requires-Dist: pre-commit>=3.7; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Provides-Extra: docs
Requires-Dist: mkdocs-material<10,>=9.5; extra == 'docs'
Requires-Dist: mkdocs<2,>=1.6; extra == 'docs'
Requires-Dist: mkdocstrings[python]>=0.26; extra == 'docs'
Provides-Extra: lint
Requires-Dist: mypy>=1.10; extra == 'lint'
Requires-Dist: ruff>=0.6; extra == 'lint'
Provides-Extra: test
Requires-Dist: pytest-cov>=5.0; extra == 'test'
Requires-Dist: pytest>=8.0; extra == 'test'
Description-Content-Type: text/markdown

# anywatch

[![CI](https://github.com/yunusimanov/anywatch/actions/workflows/ci.yml/badge.svg)](https://github.com/yunusimanov/anywatch/actions/workflows/ci.yml)
[![Docs](https://github.com/yunusimanov/anywatch/actions/workflows/docs.yml/badge.svg)](https://yunusimanov.github.io/anywatch)
[![PyPI](https://img.shields.io/pypi/v/anywatch.svg)](https://pypi.org/project/anywatch/)
[![Python](https://img.shields.io/pypi/pyversions/anywatch.svg)](https://pypi.org/project/anywatch/)
[![License: MPL 2.0](https://img.shields.io/badge/License-MPL_2.0-blue.svg)](LICENSE)

**Anytime-valid post-market monitoring for deployed AI systems.**

anywatch turns post-market monitoring of AI models from a checklist into
measurable statistical evidence. It tracks performance in every subgroup,
raises alarms on data drift and performance degradation, and writes audit
ready reports structured along Article 72 of the EU AI Act and the FDA
guidance on Predetermined Change Control Plans (PCCP).

Every test in the package is **anytime valid**. You may look at the results
after every new case, every day or whenever an auditor asks, and the error
guarantees still hold. Classical tests lose their guarantees under this kind
of continuous monitoring; e-values and confidence sequences do not.

## Why

Regulation now asks for continuous monitoring after deployment:

* The **EU AI Act** (Regulation (EU) 2024/1689, Article 72) requires providers
  of high-risk AI systems to actively and systematically collect, document
  and analyse performance data throughout the lifetime of the system.
* The **FDA** expects a PCCP for AI-enabled device software functions to
  include performance evaluation protocols and real-world monitoring.

Most existing governance tools check documents and keywords. Few provide a
statistically valid answer to the question *"is the model still performing
as declared, in every patient group, right now?"* anywatch does.

## Installation

```bash
pip install anywatch
```

Python 3.10 or newer is required. The only runtime dependencies are NumPy and
Jinja2.

## Quick start

```python
import anywatch as aw
from anywatch.datasets import make_classification_stream

# Simulated stream: sensitivity is fine in two sites and too low in the third.
stream = make_classification_stream(
    6000,
    groups=["site_a", "site_b", "site_c"],
    prevalence=0.3,
    sensitivity={"site_a": 0.95, "site_b": 0.94, "site_c": 0.82},
    seed=1,
)

monitor = aw.SubgroupMonitor(
    metric="sensitivity",
    target=0.90,
    groups=["site_a", "site_b", "site_c"],
    alpha=0.05,  # probability of any false alarm, in any group, at any time
)

for start in range(0, len(stream), 500):  # daily batches, inspected every time
    batch = slice(start, start + 500)
    for alarm in monitor.update(stream.y_true[batch], stream.y_pred[batch], stream.group[batch]):
        print(alarm.message)

for row in monitor.status():
    print(f"{row.group}: {row.estimate:.3f} [{row.ci_lower:.3f}, {row.ci_upper:.3f}]")
```

Create an Article 72 report from the same monitor:

```python
report = aw.build_art72_report(
    system=aw.SystemInfo(
        name="Chest X-ray triage",
        version="2.1.0",
        provider="Example Health Ltd",
        intended_purpose="Prioritise suspected pneumothorax for radiologist review",
    ),
    monitors=[monitor],
    plan=aw.PostMarketMonitoringPlan(
        data_sources=["Radiologist confirmed reads from deploying sites"],
        collection_frequency="Daily",
        responsible_person="Head of Clinical Safety",
    ),
    audit=aw.AuditTrail(),
)
report.save("art72_report.html")  # also .md and .json
```

## What is inside

| Module | Purpose | Main objects |
| --- | --- | --- |
| `anywatch.sequential` | Anytime-valid tests and confidence sequences for bounded means | `BettingEProcess`, `EmpiricalBernsteinCS`, `BettingCS`, `PairedDifferenceEProcess` |
| `anywatch.subgroups` | Sensitivity, specificity and error rates per subgroup with multiplicity control | `SubgroupMonitor`, `ebh`, `confusion_counts` |
| `anywatch.drift` | Data drift and performance degradation alarms | `QuantileDriftDetector`, `MeanShiftDetector`, `PerformanceDegradationDetector`, `ChangeDetector` |
| `anywatch.reporting` | Tamper evident audit trail and regulatory reports | `AuditTrail`, `build_art72_report`, `build_pccp_report` |
| `anywatch.datasets` | Synthetic streams for examples and power studies | `make_classification_stream` |

## Guarantees

| Tool | Guarantee |
| --- | --- |
| `BettingEProcess` | P(ever reject a true null) <= alpha, for any stopping rule |
| `EmpiricalBernsteinCS`, `BettingCS` | P(true mean outside the interval at any time) <= alpha |
| `SubgroupMonitor` (Bonferroni) | P(any false alarm in any subgroup at any time) <= alpha |
| `SubgroupMonitor.ebh_discoveries` | False discovery rate <= alpha at the time of the call, under any dependence |
| `ChangeDetector` | Expected time to a false alarm >= `arl` |

All guarantees are nonasymptotic and need only that observations are bounded,
which holds for every classification metric. No independence or
distributional assumption beyond the null hypothesis itself is required for
the betting tests.

## Command line

```bash
anywatch report cases.csv --metric sensitivity --target 0.9 --group-col site \
    --system-name "Triage model" --system-version 2.1 --provider "Example Ltd" \
    --framework art72 --format html --output report.html
```

## Documentation

Full documentation, including the theory behind e-values and confidence
sequences, is at <https://yunusimanov.github.io/anywatch>.

## Relation to other work

anywatch covers the post-deployment phase of the clinical AI lifecycle. A
companion library for pre-deployment local validation and threshold
selection is planned, so that the two together cover the full lifecycle.

## Citing

If you use anywatch in academic work, please cite it using the metadata in
[CITATION.cff](CITATION.cff) or the "Cite this repository" button on GitHub.

## Contributing

Contributions are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) and
the [Code of Conduct](CODE_OF_CONDUCT.md).

## License

anywatch is distributed under the [Mozilla Public License 2.0](LICENSE).

## Author

**Yunus Imanov**, Researcher, Department of Engineering Mathematics and
Artificial Intelligence, Azerbaijan Technical University, Baku, Azerbaijan.
Contact: yunus.imanov.fz@student.aztu.edu.az

## Disclaimer

anywatch produces statistical evidence to support post-market documentation.
It does not by itself establish regulatory compliance.
