SAFE-AI Metrics
SAFE-AI Metrics is a Python package for evaluating machine learning models with accuracy-, robustness-, and explainability-oriented Rank Graduation metrics.
The package provides a unified public API for:
- RGA — Rank Graduation Accuracy
- RGR — Rank Graduation Robustness
- RGE — Rank Graduation Explainability
Installation
Install from PyPI:
pip install safe-ai-metrics
Documentation
Full documentation is available on ReadTheDocs:
https://safeai.readthedocs.io/
If your ReadTheDocs project slug is different, replace the link above with the URL shown by your ReadTheDocs project.
Quickstart
from safeai.rga import rga_score, aurga_score, rga_curve, compare_rga
from safeai.rgr import rgr_score, aurgr_score, rgr_curve, compare_rgr
from safeai.rge import rge_score, aurge_score, rge_curve, compare_rge
Use *_score(...) for one scalar value, aur*_score(...) for only the area under a curve, *_curve(...) for the full curve result, and compare_*(...) for comparing several models.
Basic usage
Rank Graduation Accuracy
Compute a single RGA value:
from safeai.rga import rga_score
score = rga_score(
y_true,
y_score
)
print(score)
Compute only AURGA:
from safeai.rga import aurga_score
aurga = aurga_score(
y_true,
y_score,
n_segments=10,
curve_method='auto'
)
print(aurga)
Get the full RGA curve result:
from safeai.rga import rga_curve
result = rga_curve(
y_true,
y_score,
n_segments=10,
curve_method='auto'
)
print(result['rga'])
print(result['aurga'])
print(result['curve'])
Compare several models or probability arrays:
from safeai.rga import compare_rga
results = compare_rga(
{
'Model A': y_score_a,
'Model B': y_score_b
},
y_true,
n_segments=10
)
Rank Graduation Robustness
Compute a single RGR value between original and perturbed predictions:
from safeai.rgr import rgr_score
score = rgr_score(
pred_original,
pred_perturbed,
class_order=[0, 1]
)
print(score)
Compute only AURGR for a robustness curve:
from safeai.rgr import aurgr_score
aurgr = aurgr_score(
model,
x_data,
strengths=[0.0, 0.05, 0.10],
method='noise',
prob_original=prob_original,
model_class_order=model.classes_,
class_order=[0, 1]
)
print(aurgr)
Get the full RGR curve result:
from safeai.rgr import rgr_curve
result = rgr_curve(
model,
x_data,
strengths=[0.0, 0.05, 0.10],
method='noise',
prob_original=prob_original,
model_class_order=model.classes_,
class_order=[0, 1]
)
print(result['aurgr'])
print(result['rgr_scores'])
Compare several models:
from safeai.rgr import compare_rgr
results = compare_rgr(
{
'Model A': (model_a, x_data, prob_a, model_a.classes_, 'sklearn', None),
'Model B': (model_b, x_data, prob_b, model_b.classes_, 'sklearn', None),
},
strengths=[0.0, 0.05, 0.10],
class_order=[0, 1],
method='noise'
)
Rank Graduation Explainability
Compute a single RGE value between full and reduced predictions:
from safeai.rge import rge_score
score = rge_score(
pred_full,
pred_reduced,
class_order=[0, 1]
)
print(score)
Compute only AURGE for a feature-removal curve:
from safeai.rge import aurge_score
aurge = aurge_score(
model,
x_data,
method='tabular',
feature_names=feature_names,
model_class_order=model.classes_,
class_order=[0, 1],
n_steps=10
)
print(aurge)
Get the full RGE curve result:
from safeai.rge import rge_curve
result = rge_curve(
model,
x_data,
method='tabular',
feature_names=feature_names,
model_class_order=model.classes_,
class_order=[0, 1],
n_steps=10
)
print(result['aurge'])
print(result['rge_scores'])
Compare several models:
from safeai.rge import compare_rge
results = compare_rge(
{
'Model A': (model_a, x_data, feature_names, prob_a, model_a.classes_, 'sklearn', None),
'Model B': (model_b, x_data, feature_names, prob_b, model_b.classes_, 'sklearn', None),
},
class_order=[0, 1],
method='tabular',
n_steps=10
)
Main API
The main public functions are:
RGA
rga_scorerga_curveaurga_scorecompare_rgaplot_rga
RGR
rgr_scorergr_curveaurgr_scorecompare_rgrplot_rgr
RGE
rge_scorerge_curveaurge_scorecompare_rgeplot_rge
Package structure
The main modules are:
safeai.rga— Rank Graduation Accuracysafeai.rgr— Rank Graduation Robustnesssafeai.rge— Rank Graduation Explainabilitysafeai.cramer— Lorenz/concordance Cramer-von Mises utilitiessafeai.utils— shared utility functions
Acknowledgements
The development of this package builds on the safeaipackage project by Golnoosh Babaei.
The original safeaipackage repository is available at:
https://github.com/GolnooshBabaei/safeaipackage
This repository is currently maintained as a separate implementation for development and packaging purposes, but it is expected to be merged or aligned with the original SAFE-AI package in the future.
If you use this package in academic work, please consider citing the related SAFE-AI and RGB paper referenced in the original project.
Metadata
Release files for safe-ai-metrics 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| safe_ai_metrics-0.2.1.tar.gz | 37.1 kB | Without provenance | Details for safe_ai_metrics-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| safe_ai_metrics-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for safe_ai_metrics-0.2.1-py3-none-any.whl |
Total release size: 77.1 kB
Release files / safe_ai_metrics-0.2.1.tar.gz
| Download URL | safe_ai_metrics-0.2.1.tar.gz |
|---|---|
| Size | 37.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4947043670b9528cd79b51846f026f9811df7ceb2c64f6d4203650120c3cf867
|
|
BLAKE2b-256 checksum How to use checksums |
f8125c7eae73031d3e32b2e8c126501ee7a4e4f4ab4c39b37a6724a20e89aeab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|
Release files / safe_ai_metrics-0.2.1-py3-none-any.whl
| Download URL | safe_ai_metrics-0.2.1-py3-none-any.whl |
|---|---|
| Size | 40.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
022d386212d7d8ef879655fa2c7969379285e3dd1df92430ffcd0df579547705
|
|
BLAKE2b-256 checksum How to use checksums |
344f48bb0c96e2b361675a00f39203e8ed387a24cece70cf817953267b4655c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|
Security for safe-ai-metrics 0.2.1
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.