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 MIT License - see the LICENSE file for details.

Project status

Active and under development!

Citation

If you use safetycage, please cite us!

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safetycage-0.0.63.tar.gz (37.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safetycage-0.0.63-py3-none-any.whl (48.6 kB view details)

Uploaded Python 3

File details

Details for the file safetycage-0.0.63.tar.gz.

File metadata

  • Download URL: safetycage-0.0.63.tar.gz
  • Upload date:
  • Size: 37.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.14

File hashes

Hashes for safetycage-0.0.63.tar.gz
Algorithm Hash digest
SHA256 1ac517b50fe02e40aed0b3bb979410473da70f248cc997840bd1952124a46d91
MD5 e29694b4bb3ffcea664e09036ab035fd
BLAKE2b-256 a4e94908494d447a5f8c0e0b3ec14435fd06e725402ec57d814622f6fac1efa6

See more details on using hashes here.

File details

Details for the file safetycage-0.0.63-py3-none-any.whl.

File metadata

File hashes

Hashes for safetycage-0.0.63-py3-none-any.whl
Algorithm Hash digest
SHA256 594369bedf31fb62b3b4e9376648e1f3776f573ab1026411d9f5413b54e842dd
MD5 580d3cff4d707eb054f9a2c1476378a8
BLAKE2b-256 37cb54980f26111aa39692b018154ac0ce51e511ee3992fe20cf300a3ff5e915

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.63 This release

2 files

0.0.62

2 files

0.0.61

2 files

0.0.60

2 files

0.0.59

2 files

0.0.58

2 files

0.0.57

2 files

0.0.56

2 files

0.0.55

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

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