Skip to main content

Project Logo

PyPI version Docs Python versions License

SafetyCage is a Python package for detecting misclassified predictions from machine learning models in classification tasks. It provides a unified interface for multiple statistical detection methods, enabling users to quantify prediction reliability and flag potentially incorrect outputs across different models and datasets easily.

Available on PyPI: https://pypi.org/project/safetycage/.

Full documentation is available at https://safetycage.readthedocs.io/.

Background

The idea behind safetycage is that we can find statistics on each predicted sample and compare that statistic to some statistic threshold to predict whether the sample prediction was incorrectly classified.

Description

Machine learning models can produce incorrect predictions with high confidence. SafetyCage addresses this by providing post-hoc misclassification detection methods that operate on model outputs or internal representations.

The package includes several methods:

  • MSP (Maximum Softmax Probability)
  • DOCTOR (Error probability estimation)
  • Mahalanobis (Distance-based statistical testing)
  • SPARDACUS (Projection + density estimation approach)

Each method outputs a statistic or p-value that reflects how likely a prediction is to be incorrect.

Alternatively, you can implement your own method by initializing a base class from the safetycage abstract base class, which defines how methods should be implemented.

Requirements

safetycage requires Python 3.13 or later.

Core dependencies (installed automatically): joblib, matplotlib, numpy.

Installation

pip install safetycage
# or: uv add safetycage

Some methods need extra dependencies, installed via pip install safetycage[extra] (or uv add "safetycage[extra]"):

Extra Uses Adds
red RED torch, gpytorch
spardacus SPARDACUS statsmodels, scipy, scikit-learn, tqdm
mahalanobis Mahalanobis statsmodels, scipy
torch TorchModelModule torch

Tutorials & Examples

To learn how to use safetycage, check out the examples/ directory in this repository. It contains complete integrations with runnable notebooks, including scripts to train models to test the safetycage methods on.

Changelog

See the CHANGELOG.MD for details on versioning.

Support

If you encounter issues or have questions:

Contributing

If you would like to contribute, please reach out to our safetycage team, listed below!

Authors

Acknowledgment

The MSP method was introduced by Hendrycks and Gimpel in A Baseline for Detecting Misclassified and Out-of-Distribution Examples in Neural Networks.

The DOCTOR method was introduced by Granese et al. in DOCTOR: A Simple Method for Detecting Misclassification Errors.

The Mahalanobis method is described in Johnsen et al..

The SPARDACUS method is described in Johnsen et al..

A proper citation for these methods is provided in the docstring of the code using these methods.

A special thank you goes to previous co-authors of the methods we have built, Filippo Remonato, Shawn Benedict, and Albert Ndur-Osei.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Project status

Active and under development!

Citation

If you use safetycage, please cite us!

Metadata

Release files for safetycage 0.0.65

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

Source distribution (sdist)

Source distribution for safetycage 0.0.65
File Size Uploaded Provenance Actions
safetycage-0.0.65.tar.gz 41.3 kB Without provenance Details for safetycage-0.0.65.tar.gz

Built distribution (wheel)

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

Total release size: 93.6 kB

Release files / safetycage-0.0.65.tar.gz

Download URL safetycage-0.0.65.tar.gz
Size 41.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9de4f74d7b19ed3686c6386cc2ce9902e35fe2bfba44227460a59c76b3225347
BLAKE2b-256 checksum
How to use checksums
51e2e53a50d1a14a8d4482a1b32f6530993b631068e1e9284ebc2ffce63d957a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.14

Release files / safetycage-0.0.65-py3-none-any.whl

Download URL safetycage-0.0.65-py3-none-any.whl
Size 52.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22041e5f81812e7e19a6544139030587e70b42f4565095e8c25dab2b85c10642
BLAKE2b-256 checksum
How to use checksums
91e6c0b8a7f5ffe0b4ffb70d994a8fa7a5fbf6430903376d05adabf19ddc6659
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.14

Security for safetycage 0.0.65

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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