Skip to main content

Critical Difference diagram generator in pure Python

Project description

cddiagram

A pure Python library for generating Critical Difference (CD) diagrams as SVG.

CD diagrams visualize the statistical comparison of multiple classifiers (or models) over multiple datasets, as introduced by Demsar (2006). They show the average rank of each model and connect groups of models whose performance differences are not statistically significant.

J. Demsar, "Statistical Comparisons of Classifiers over Multiple Data Sets", Journal of Machine Learning Research, vol. 7, pp. 1-30, 2006. https://jmlr.org/papers/v7/demsar06a.html

How it works

  1. A Friedman test checks whether at least one model differs significantly from the others (at alpha = 0.05).
  2. If significant, the Nemenyi post-hoc test computes a critical distance (CD) threshold.
  3. Models whose average rank difference is less than CD are grouped together — they are not statistically distinguishable.
  4. The result is rendered as an SVG diagram showing ranked models and significance groups.

Install

pip install cddiagram

Requires Python 3.12+ and depends on numpy and scipy.

Usage

Write to file

import numpy as np
from cddiagram import draw_cd_diagram

rng = np.random.default_rng(1)

models = {
    "model1": rng.normal(loc=0.2, scale=0.1, size=30),
    "model2": rng.normal(loc=0.2, scale=0.1, size=30),
    "model3": rng.normal(loc=0.4, scale=0.1, size=30),
    "model4": rng.normal(loc=0.5, scale=0.1, size=30),
    "model5": rng.normal(loc=0.7, scale=0.1, size=30),
    "model6": rng.normal(loc=0.7, scale=0.1, size=30),
    "model7": rng.normal(loc=0.8, scale=0.1, size=30),
    "model8": rng.normal(loc=0.9, scale=0.1, size=30),
}

samples = np.column_stack(list(models.values()))
draw_cd_diagram(samples, labels=list(models.keys()), out_file="out.svg", title="Model comparison")

Non-significant results

If the Friedman test is not significant, the function issues a warning and returns None — no diagram is produced because the data does not support ranking the models.

API

draw_cd_diagram(
    samples,           # 2D array-like (rows=datasets, columns=models)
    labels,            # Sequence of model names (one per column)
    title=None,        # Optional diagram title
    out_file=None,     # Optional path to write SVG file
    fig_size=None,     # Optional (width, height) tuple in pixels
) -> Element | None

Input formats: NumPy arrays, pandas DataFrames, or any object with a .to_numpy() / .values attribute.

Release Notes

0.0.2

  • Replaced hardcoded Nemenyi critical-value lookup table with SciPy's studentized_range computation.
  • Updated CD diagram drawing to follow continuous rank-axis placement and first-anchor non-significant grouping.
  • Improved readability for larger numbers of algorithms with adaptive multi-row label layout.

License

MIT

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

cddiagram-0.0.5.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

cddiagram-0.0.5-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file cddiagram-0.0.5.tar.gz.

File metadata

  • Download URL: cddiagram-0.0.5.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cddiagram-0.0.5.tar.gz
Algorithm Hash digest
SHA256 10475940239a396c8d3f64a1c2ed09af3c4e344c3e9c9f64cc2421d0d80b46b1
MD5 22919be67e5ab5f79e9dad64eb3fbf23
BLAKE2b-256 ecbf6b8ed4cf60c6154004a7fc629ecdd453ebdbfa01c42d6cd1efca5a3ab23d

See more details on using hashes here.

Provenance

The following attestation bundles were made for cddiagram-0.0.5.tar.gz:

Publisher: publish-pypi.yml on irazza/cddiagram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cddiagram-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: cddiagram-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cddiagram-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 24e5978298c9d6adb9d6215ab99dca8555e682e3695818ae208d704cc6794bf2
MD5 6115c5feffe7eb4639ce86683aaa21aa
BLAKE2b-256 855f0873e2636e1b9835dbe6c0dcda534180cf9ea7bb136d55663e0897d51bb0

See more details on using hashes here.

Provenance

The following attestation bundles were made for cddiagram-0.0.5-py3-none-any.whl:

Publisher: publish-pypi.yml on irazza/cddiagram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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