nonconform


Namenonconform JSON
Version 0.91.0 PyPI version JSON
download
home_pageNone
SummaryConformal Anomaly Detection
upload_time2025-10-09 12:51:29
maintainerNone
docs_urlNone
authorNone
requires_python>=3.12
licenseBSD 3-Clause License Copyright (c) 2024, Oliver Hennhöfer Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
keywords anomaly detection conformal anomaly detection conformal inference false discovery rate uncertainty quantification
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![Logo](https://raw.githubusercontent.com/OliverHennhoefer/nonconform/main/docs/img/banner_light.png)

---

![License](https://img.shields.io/github/license/OliverHennhoefer/nonconform)
![Python](https://img.shields.io/badge/python-3.12%2B-blue)
![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/OliverHennhoefer/nonconform)
![PyPI](https://img.shields.io/pypi/v/nonconform)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Ruff](https://img.shields.io/badge/linting-ruff-%23FFA500)](https://github.com/astral-sh/ruff)
![Hatch](https://img.shields.io/badge/build-hatch-FF69B4)




**nonconform** enhances anomaly detection by providing uncertainty quantification. It acts as a wrapper around most detectors from [*PyOD*](https://pyod.readthedocs.io/en/latest/) (see [Supported Estimators](#supported-estimators)). By leveraging one-class classification and **conformal inference**, **nonconform** enables **statistically rigorous anomaly detection**.

*   **Uncertainty Quantification:** Turn anomaly scores into statistically valid _p_-values.
*   **Control False Positives:** Reliably control metrics like the False Discovery Rate (FDR).
*   **[*PyOD*](https://pyod.readthedocs.io/en/latest/) Compatibility:** Works with most PyOD anomaly detectors (see [Supported Estimators](#supported-estimators)).

# Getting Started

Installation via [PyPI](https://pypi.org/project/nonconform/):
```sh
pip install nonconform
```

> **Note:** The following examples use the built-in datasets. Install with `pip install nonconform[data]` to run these examples. (see [Optional Dependencies](#optional-dependencies))


## Classical (Conformal) Approach

**Example:** Detecting anomalies with Isolation Forest on the Shuttle dataset. The approach splits data for calibration, trains the model, then converts anomaly scores to statistically valid p-values by comparing test scores against the calibration distribution.

```python
from pyod.models.iforest import IForest
from scipy.stats import false_discovery_control

from nonconform.strategy import Split
from nonconform.estimation import ConformalDetector
from nonconform.utils.data import load, Dataset
from nonconform.utils.stat import false_discovery_rate, statistical_power

x_train, x_test, y_test = load(Dataset.SHUTTLE, setup=True, seed=42)

estimator = ConformalDetector(
    detector=IForest(behaviour="new"),
    strategy=Split(n_calib=2_000),
    seed=42
)

estimator.fit(x_train)

estimates = estimator.predict(x_test)
decisions = false_discovery_control(estimates, method='bh') <= 0.2

print(f"Empirical False Discovery Rate: {false_discovery_rate(y=y_test, y_hat=decisions)}")
print(f"Empirical Statistical Power (Recall): {statistical_power(y=y_test, y_hat=decisions)}")
```

Output:
```text
Empirical False Discovery Rate: 0.198
Empirical Statistical Power (Recall): 0.97
```

# Advanced Methods

For advanced use cases, the unified ``ConformalDetector()`` supports weighted conformal prediction (robust to covariate shifts) by adding a weight_estimator parameter, and sophisticated calibration strategies like ``JackknifeBootstrap()`` for improved results.


# Beyond Static Data

While primarily designed for static (single-batch) applications, the library supports streaming scenarios through ``BatchGenerator()`` and ``OnlineGenerator()``. For statistically valid FDR control in streaming data, use the optional ``onlineFDR`` dependency, which implements appropriate statistical methods.


# Citation

If you find this repository useful for your research, please cite the following papers:

##### Leave-One-Out-, Bootstrap- and Cross-Conformal Anomaly Detectors
```text
@inproceedings{Hennhofer2024,
	title        = {{ Leave-One-Out-, Bootstrap- and Cross-Conformal Anomaly Detectors }},
	author       = {Hennhofer, Oliver and Preisach, Christine},
	year         = 2024,
	month        = {Dec},
	booktitle    = {2024 IEEE International Conference on Knowledge Graph (ICKG)},
	publisher    = {IEEE Computer Society},
	address      = {Los Alamitos, CA, USA},
	pages        = {110--119},
	doi          = {10.1109/ICKG63256.2024.00022},
	url          = {https://doi.ieeecomputersociety.org/10.1109/ICKG63256.2024.00022}
}
```

##### Testing for Outliers with Conformal p-Values
```text
@article{Bates2023,
	title        = {Testing for outliers with conformal p-values},
	author       = {Bates,  Stephen and Candès,  Emmanuel and Lei,  Lihua and Romano,  Yaniv and Sesia,  Matteo},
	year         = 2023,
	month        = feb,
	journal      = {The Annals of Statistics},
	publisher    = {Institute of Mathematical Statistics},
	volume       = 51,
	number       = 1,
	doi          = {10.1214/22-aos2244},
	issn         = {0090-5364},
	url          = {http://dx.doi.org/10.1214/22-AOS2244}
}
```

# Optional Dependencies

_For additional features, you might need optional dependencies:_
- `pip install nonconform[data]` - Includes pyarrow for loading example data (via remote download)
- `pip install nonconform[deep]` - Includes deep learning dependencies (PyTorch)
- `pip install nonconform[fdr]` - Includes advanced FDR control methods (online-fdr)
- `pip install nonconform[dev]` - Includes development documentation tools
- `pip install nonconform[all]` - Includes all optional dependencies

_Please refer to the [pyproject.toml](https://github.com/OliverHennhoefer/nonconform/blob/main/pyproject.toml) for details._

# Supported Estimators

Only anomaly estimators suitable for unsupervised one-class classification are supported. Since detectors are trained exclusively on normal data, threshold parameters are automatically set to minimal values.

Models that are **currently supported** include:

* Angle-Based Outlier Detection (**ABOD**)
* Autoencoder (**AE**)
* Cook's Distance (**CD**)
* Copula-based Outlier Detector (**COPOD**)
* Deep Isolation Forest (**DIF**)
* Empirical-Cumulative-distribution-based Outlier Detection (**ECOD**)
* Gaussian Mixture Model (**GMM**)
* Histogram-based Outlier Detection (**HBOS**)
* Isolation-based Anomaly Detection using Nearest-Neighbor Ensembles (**INNE**)
* Isolation Forest (**IForest**)
* Kernel Density Estimation (**KDE**)
* *k*-Nearest Neighbor (***k*NN**)
* Kernel Principal Component Analysis (**KPCA**)
* Linear Model Deviation-base Outlier Detection (**LMDD**)
* Local Outlier Factor (**LOF**)
* Local Correlation Integral (**LOCI**)
* Lightweight Online Detector of Anomalies (**LODA**)
* Locally Selective Combination of Parallel Outlier Ensembles (**LSCP**)
* GNN-based Anomaly Detection Method (**LUNAR**)
* Median Absolute Deviation (**MAD**)
* Minimum Covariance Determinant (**MCD**)
* One-Class SVM (**OCSVM**)
* Principal Component Analysis (**PCA**)
* Quasi-Monte Carlo Discrepancy Outlier Detection (**QMCD**)
* Rotation-based Outlier Detection (**ROD**)
* Subspace Outlier Detection (**SOD**)
* Scalable Unsupervised Outlier Detection (**SUOD**)

# Contact
**Bug reporting:** [https://github.com/OliverHennhoefer/nonconform/issues](https://github.com/OliverHennhoefer/nonconform/issues)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "nonconform",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": "Oliver Hennhoefer <oliver.hennhoefer@mail.de>",
    "keywords": "anomaly detection, conformal anomaly detection, conformal inference, false discovery rate, uncertainty quantification",
    "author": null,
    "author_email": "Oliver Hennhoefer <oliver.hennhoefer@mail.de>",
    "download_url": "https://files.pythonhosted.org/packages/15/6c/83b41d2f7f6dd5129e1940883f847560b9e76c37e6bffb1fa794526033ed/nonconform-0.91.0.tar.gz",
    "platform": null,
    "description": "![Logo](https://raw.githubusercontent.com/OliverHennhoefer/nonconform/main/docs/img/banner_light.png)\n\n---\n\n![License](https://img.shields.io/github/license/OliverHennhoefer/nonconform)\n![Python](https://img.shields.io/badge/python-3.12%2B-blue)\n![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/OliverHennhoefer/nonconform)\n![PyPI](https://img.shields.io/pypi/v/nonconform)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Ruff](https://img.shields.io/badge/linting-ruff-%23FFA500)](https://github.com/astral-sh/ruff)\n![Hatch](https://img.shields.io/badge/build-hatch-FF69B4)\n\n\n\n\n**nonconform** enhances anomaly detection by providing uncertainty quantification. It acts as a wrapper around most detectors from [*PyOD*](https://pyod.readthedocs.io/en/latest/) (see [Supported Estimators](#supported-estimators)). By leveraging one-class classification and **conformal inference**, **nonconform** enables **statistically rigorous anomaly detection**.\n\n*   **Uncertainty Quantification:** Turn anomaly scores into statistically valid _p_-values.\n*   **Control False Positives:** Reliably control metrics like the False Discovery Rate (FDR).\n*   **[*PyOD*](https://pyod.readthedocs.io/en/latest/) Compatibility:** Works with most PyOD anomaly detectors (see [Supported Estimators](#supported-estimators)).\n\n# Getting Started\n\nInstallation via [PyPI](https://pypi.org/project/nonconform/):\n```sh\npip install nonconform\n```\n\n> **Note:** The following examples use the built-in datasets. Install with `pip install nonconform[data]` to run these examples. (see [Optional Dependencies](#optional-dependencies))\n\n\n## Classical (Conformal) Approach\n\n**Example:** Detecting anomalies with Isolation Forest on the Shuttle dataset. The approach splits data for calibration, trains the model, then converts anomaly scores to statistically valid p-values by comparing test scores against the calibration distribution.\n\n```python\nfrom pyod.models.iforest import IForest\nfrom scipy.stats import false_discovery_control\n\nfrom nonconform.strategy import Split\nfrom nonconform.estimation import ConformalDetector\nfrom nonconform.utils.data import load, Dataset\nfrom nonconform.utils.stat import false_discovery_rate, statistical_power\n\nx_train, x_test, y_test = load(Dataset.SHUTTLE, setup=True, seed=42)\n\nestimator = ConformalDetector(\n    detector=IForest(behaviour=\"new\"),\n    strategy=Split(n_calib=2_000),\n    seed=42\n)\n\nestimator.fit(x_train)\n\nestimates = estimator.predict(x_test)\ndecisions = false_discovery_control(estimates, method='bh') <= 0.2\n\nprint(f\"Empirical False Discovery Rate: {false_discovery_rate(y=y_test, y_hat=decisions)}\")\nprint(f\"Empirical Statistical Power (Recall): {statistical_power(y=y_test, y_hat=decisions)}\")\n```\n\nOutput:\n```text\nEmpirical False Discovery Rate: 0.198\nEmpirical Statistical Power (Recall): 0.97\n```\n\n# Advanced Methods\n\nFor advanced use cases, the unified ``ConformalDetector()`` supports weighted conformal prediction (robust to covariate shifts) by adding a weight_estimator parameter, and sophisticated calibration strategies like ``JackknifeBootstrap()`` for improved results.\n\n\n# Beyond Static Data\n\nWhile primarily designed for static (single-batch) applications, the library supports streaming scenarios through ``BatchGenerator()`` and ``OnlineGenerator()``. For statistically valid FDR control in streaming data, use the optional ``onlineFDR`` dependency, which implements appropriate statistical methods.\n\n\n# Citation\n\nIf you find this repository useful for your research, please cite the following papers:\n\n##### Leave-One-Out-, Bootstrap- and Cross-Conformal Anomaly Detectors\n```text\n@inproceedings{Hennhofer2024,\n\ttitle        = {{ Leave-One-Out-, Bootstrap- and Cross-Conformal Anomaly Detectors }},\n\tauthor       = {Hennhofer, Oliver and Preisach, Christine},\n\tyear         = 2024,\n\tmonth        = {Dec},\n\tbooktitle    = {2024 IEEE International Conference on Knowledge Graph (ICKG)},\n\tpublisher    = {IEEE Computer Society},\n\taddress      = {Los Alamitos, CA, USA},\n\tpages        = {110--119},\n\tdoi          = {10.1109/ICKG63256.2024.00022},\n\turl          = {https://doi.ieeecomputersociety.org/10.1109/ICKG63256.2024.00022}\n}\n```\n\n##### Testing for Outliers with Conformal p-Values\n```text\n@article{Bates2023,\n\ttitle        = {Testing for outliers with conformal p-values},\n\tauthor       = {Bates,  Stephen and Cand\u00e8s,  Emmanuel and Lei,  Lihua and Romano,  Yaniv and Sesia,  Matteo},\n\tyear         = 2023,\n\tmonth        = feb,\n\tjournal      = {The Annals of Statistics},\n\tpublisher    = {Institute of Mathematical Statistics},\n\tvolume       = 51,\n\tnumber       = 1,\n\tdoi          = {10.1214/22-aos2244},\n\tissn         = {0090-5364},\n\turl          = {http://dx.doi.org/10.1214/22-AOS2244}\n}\n```\n\n# Optional Dependencies\n\n_For additional features, you might need optional dependencies:_\n- `pip install nonconform[data]` - Includes pyarrow for loading example data (via remote download)\n- `pip install nonconform[deep]` - Includes deep learning dependencies (PyTorch)\n- `pip install nonconform[fdr]` - Includes advanced FDR control methods (online-fdr)\n- `pip install nonconform[dev]` - Includes development documentation tools\n- `pip install nonconform[all]` - Includes all optional dependencies\n\n_Please refer to the [pyproject.toml](https://github.com/OliverHennhoefer/nonconform/blob/main/pyproject.toml) for details._\n\n# Supported Estimators\n\nOnly anomaly estimators suitable for unsupervised one-class classification are supported. Since detectors are trained exclusively on normal data, threshold parameters are automatically set to minimal values.\n\nModels that are **currently supported** include:\n\n* Angle-Based Outlier Detection (**ABOD**)\n* Autoencoder (**AE**)\n* Cook's Distance (**CD**)\n* Copula-based Outlier Detector (**COPOD**)\n* Deep Isolation Forest (**DIF**)\n* Empirical-Cumulative-distribution-based Outlier Detection (**ECOD**)\n* Gaussian Mixture Model (**GMM**)\n* Histogram-based Outlier Detection (**HBOS**)\n* Isolation-based Anomaly Detection using Nearest-Neighbor Ensembles (**INNE**)\n* Isolation Forest (**IForest**)\n* Kernel Density Estimation (**KDE**)\n* *k*-Nearest Neighbor (***k*NN**)\n* Kernel Principal Component Analysis (**KPCA**)\n* Linear Model Deviation-base Outlier Detection (**LMDD**)\n* Local Outlier Factor (**LOF**)\n* Local Correlation Integral (**LOCI**)\n* Lightweight Online Detector of Anomalies (**LODA**)\n* Locally Selective Combination of Parallel Outlier Ensembles (**LSCP**)\n* GNN-based Anomaly Detection Method (**LUNAR**)\n* Median Absolute Deviation (**MAD**)\n* Minimum Covariance Determinant (**MCD**)\n* One-Class SVM (**OCSVM**)\n* Principal Component Analysis (**PCA**)\n* Quasi-Monte Carlo Discrepancy Outlier Detection (**QMCD**)\n* Rotation-based Outlier Detection (**ROD**)\n* Subspace Outlier Detection (**SOD**)\n* Scalable Unsupervised Outlier Detection (**SUOD**)\n\n# Contact\n**Bug reporting:** [https://github.com/OliverHennhoefer/nonconform/issues](https://github.com/OliverHennhoefer/nonconform/issues)\n",
    "bugtrack_url": null,
    "license": "BSD 3-Clause License\n        \n        Copyright (c) 2024, Oliver Hennh\u00f6fer\n        \n        Redistribution and use in source and binary forms, with or without\n        modification, are permitted provided that the following conditions are met:\n        \n        1. Redistributions of source code must retain the above copyright notice, this\n           list of conditions and the following disclaimer.\n        \n        2. Redistributions in binary form must reproduce the above copyright notice,\n           this list of conditions and the following disclaimer in the documentation\n           and/or other materials provided with the distribution.\n        \n        3. Neither the name of the copyright holder nor the names of its\n           contributors may be used to endorse or promote products derived from\n           this software without specific prior written permission.\n        \n        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.",
    "summary": "Conformal Anomaly Detection",
    "version": "0.91.0",
    "project_urls": {
        "Bugs": "https://github.com/OliverHennhoefer/nonconform/issues",
        "Homepage": "https://github.com/OliverHennhoefer/nonconform"
    },
    "split_keywords": [
        "anomaly detection",
        " conformal anomaly detection",
        " conformal inference",
        " false discovery rate",
        " uncertainty quantification"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "16184a722459a25247503effbf78a8b60951aad98db5a83b49f33f5ab3c28e51",
                "md5": "5fe7a9eeb26a11e7e029b1af91c51076",
                "sha256": "153766c639d730c30cf5b3a3022684acd3d8ce0a1747fb6e100cb5c86957014f"
            },
            "downloads": -1,
            "filename": "nonconform-0.91.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5fe7a9eeb26a11e7e029b1af91c51076",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.12",
            "size": 68853,
            "upload_time": "2025-10-09T12:51:26",
            "upload_time_iso_8601": "2025-10-09T12:51:26.755066Z",
            "url": "https://files.pythonhosted.org/packages/16/18/4a722459a25247503effbf78a8b60951aad98db5a83b49f33f5ab3c28e51/nonconform-0.91.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "156c83b41d2f7f6dd5129e1940883f847560b9e76c37e6bffb1fa794526033ed",
                "md5": "d1c4157a7bc123f901082f03f239b758",
                "sha256": "f91e01a4476ec91256122b140bdaef2ace7c7b44a3422cbf1c49de836c1e53c8"
            },
            "downloads": -1,
            "filename": "nonconform-0.91.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d1c4157a7bc123f901082f03f239b758",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 379717,
            "upload_time": "2025-10-09T12:51:29",
            "upload_time_iso_8601": "2025-10-09T12:51:29.490933Z",
            "url": "https://files.pythonhosted.org/packages/15/6c/83b41d2f7f6dd5129e1940883f847560b9e76c37e6bffb1fa794526033ed/nonconform-0.91.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-09 12:51:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "OliverHennhoefer",
    "github_project": "nonconform",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "nonconform"
}
        
Elapsed time: 1.78618s