Skip to main content

A package with utility functions for evaluating conformal predictors

Project description

Conformal-eval

A Python library for simplifying the evaluation of conformal predictors.

Installation

Installation of Conformal-eval is made by;

pip install conformal-eval

or including the extras report:

pip install conformal-eval[report]
# or, depending on your shell you might need:
pip install "conformal-eval[report]"

Examples

Examples of using the package can be seen in our examples notebooks:

Package dependencies

Package dependencies can be found in the pyproject.toml under [tool.poetry.dependencies], noting that the Jinja2 package is only required for for the report extras (installing conformal-eval[report]).

API

Data format

The code internally use numpy ndarrays for matrices and vectors, but tries to be agnostic about input being either list, arrays or Pandas equivalents. For performance reasons it is recommended that conversion to numpy format is done when using several of the methods in this library, as this is internally made in most functions.

For regression we require predictions to be the same as used in nonconformist, using 2D or 3D tensors in numpy ndarrays of shape (num_examples,2) or (num_examples,2,num_significance_levels), where the second dimension contains the lower and upper limits of the prediction intervals.

Rendering backend

Plotting is made based on the matplotlib library and Seaborn for the function plot_confusion_matrix_heatmap. To easily set a nicer plotting style (in our opinion) you can use the function conf_eval.plot.update_plot_settings which uses Seaborn to configure the default settings in matplotlib and e.g. easily scales font size.

The conf_eval.cpsign module

This module and submodules are intended for easily loading results from CPSign in the format required by Conformal-eval

Extras report

This extras include the functionality to generate a "report" in HTML format for a model generated by CPSign. This report is at this point only in beta testing and contains rudimentary information - this could be altered in the future. Further note that this will install a CLI script called cpsign-report once installing the conformal-eval[report] package, to see the usage of this simply run cpsign-report --help in your terminal environment.

Supported plots

Classification

  • Calibration plot
  • Label ratio plot, showing ratio of single/multi/empty predictions for each significance level
  • p-value distribution plot: plot p-values as a scatter plot
  • "Bubble plot" confusion matrix
  • Heatmap confusion matrix

Regression

  • Calibration plot
  • Efficiency plot (mean or median prediction interval width vs significance)
  • Prediction intervals (for a given significance level)

Developer notes

Testing

All python-tests are located in the tests folder and are meant to be run using pytest. Test should be started from standing in the python folder and can be run "all at once" (python -m pytest), "per file" (python -m pytest tests/conf_eval/metrics/clf_metrics_test.py), or a single test function (python -m pytest tests/conf_eval/metrics/clf_metrics_test.py::TestConfusionMatrix::test_with_custom_labels).

  • Note1: The invocation python -m pytest [opt args] is preferred here as the current directory is added to the python path and resolves the application code automatically. Simply running pytest requires manual setup of the PYTHONPATH instead.
  • Note2: The plotting tests generate images that are saved in the test_output directory and these should be checked manually (no good way of automating plotting-tests).

Before running:

For the report module there are CLI tests that require the package to be installed before running them (otherwise the cpsign-report program is not available, or not updated). To do this you should use the following;

# Set up an venv to run in
poetry shell
# Install dependencies from the pyproject.toml
poetry install
# Run all (or subset) of tests
python -m pytest

TODOs:

Add/finish the following plots:

  • calibration plot - Staffan
  • 'area plot' with label-distributions - Staffan
  • bubbel-plot - Jonathan
  • heatmap - Staffan
  • p0-p1 plot - Staffan
  • Add regression metrics
  • Add plots regression

Change log:

  • 1.0.0b3:
    • Allow to configure the 'static' output directory, to e.g. avoid path clashes with other files.
    • Change links in README to absolute urls to github, so they work at PyPI when deployed.
  • 1.0.0b2:
    • Added the png-logo files for the cpsign-report utility (gitignore had removed them before).
  • 1.0.0b1:
    • Forked the plot_utils library to make an extensive refactor of module names.
    • Added conf_eval.cpsign.report module for generating model-reports in HTML format. The extras report is needed to run this code.
    • Fixed bugs when loading data from CPSign, e.g. missing standard-deviation columns in validation metrics.
    • Added new flag (skip_inf) when loading regression predictions and results, where a too high confidence level leads to infinitely large prediction intervals. This simply filters out the rows that have infinitely large outputs.
    • Added new function to load confidence-independent metrics from CPSign validation output (cpsign.load_conf_independent_metrics).
    • Added Type hints on a few places (not finished everywhere yet).
  • 0.1.0:
    • Added pharmbio.cpsign package with loading functionality for CPSign generated files, loading calibration statistics, efficiency statistics and predictions.
    • Updated plotting functions in order to use pre-computed metrics where applicable (e.g. when computed by CPSign).
    • Added possibility to add a shading for +/- standard deviation where applicable, e.g. calibration curve
    • Updated calibration curve plotting to have a general plotting.plot_calibration acting on pre-computed values or for classification using plotting.plot_calibration_clf where true labels and p-values can be given.
    • Update parameter order to make it consistent across plotting functions, e.g. ordered as x, y (significance vs error rate) in the plots.
    • Added a utility function for setting the seaborn theme and context using plotting.update_plot_settings which updates the matplotlib global settings. Note this will have effect on all other plots generated in the same python session if those rely on matplotlib.

Project details


Download files

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

Source Distribution

conformal_eval-1.0.0b3.tar.gz (111.7 kB view details)

Uploaded Source

Built Distribution

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

conformal_eval-1.0.0b3-py3-none-any.whl (115.2 kB view details)

Uploaded Python 3

File details

Details for the file conformal_eval-1.0.0b3.tar.gz.

File metadata

  • Download URL: conformal_eval-1.0.0b3.tar.gz
  • Upload date:
  • Size: 111.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.8.18 Linux/6.5.0-1016-azure

File hashes

Hashes for conformal_eval-1.0.0b3.tar.gz
Algorithm Hash digest
SHA256 56d96220cba3ad9467bcac98b4cd1adc976de7a2a8d47b7feded05c5cbbc5d2d
MD5 315b0866dd0e18d9e9be4d1ebf47542b
BLAKE2b-256 8217e4fbab9e9d3c720776e74a09c7a1f26e5672c89a2ed1607c1bad36a05123

See more details on using hashes here.

File details

Details for the file conformal_eval-1.0.0b3-py3-none-any.whl.

File metadata

  • Download URL: conformal_eval-1.0.0b3-py3-none-any.whl
  • Upload date:
  • Size: 115.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.8.18 Linux/6.5.0-1016-azure

File hashes

Hashes for conformal_eval-1.0.0b3-py3-none-any.whl
Algorithm Hash digest
SHA256 34fd3d18f6510787cd0a96d818adbd38e04a5d2147195ea219c779a6966fa3f6
MD5 3b3f8db0be8e49d51566a49d86d14bd5
BLAKE2b-256 30c75fbd871f1a0dc2e8073643a3041b8b1a702b7e8a1980373b9c88811974af

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page