Skip to main content

A multi-objective counterfactual explanation method for time series classifiers.

Project description

PyPI Version Python Versions Wheel CI Status Documentation Status License

CONFETTI Logo


Counterfactual Explanations for Time Series

CONFETTI is a multi-objective method for generating counterfactual explanations for multivariate time series classifiers. It identifies the most influential features or temporal regions, constructs a minimal perturbation, and optimizes it under multiple objectives to produce sparse, realistic, and confidence-increasing counterfactuals.

CONFETTI is model-agnostic and works with any classifier — Keras, PyTorch, or scikit-learn.


✨ Highlights

  • Multi-objective optimization using NSGA-III
  • Time series: works with any Torch/Keras/scikit-learn multivariate time series classifier
  • Rust-accelerated backend for distances and NSGA-III
  • Optional use of class activation maps for feature-weighted perturbations
  • Built-in distance metrics: Euclidean, Manhattan, DTW, Soft-DTW, GAK, CTW
  • Generates multiple diverse counterfactuals per instance
  • Parallelized counterfactual generation

🚀 Installation

PyPI Installation

pip install confetti-ts

Pre-built wheels are available for common platforms. If no wheel is available for your system, the install requires a Rust toolchain.

Development Installation

git clone https://github.com/serval-uni-lu/confetti.git
cd confetti

uv venv
source .venv/bin/activate
uv pip install -e .

Core dependencies:

  • Python 3.12+
  • NumPy, pandas, scikit-learn, joblib, matplotlib

Optional (model backends):

  • Keras 3.x + TensorFlow (pip install confetti-ts[keras])
  • PyTorch (pip install confetti-ts[torch])
  • All backends (pip install confetti-ts[all])

⚡ Quick Example — Time Series

Below is a minimal end-to-end example for multivariate time series counterfactuals. It loads a trained model, prepares a dataset, and generates counterfactuals for a single instance.

The example files are included in the repository under examples/. Clone it first with git clone https://github.com/serval-uni-lu/confetti.git.

from confetti import CONFETTI
from confetti.attribution import cam
from confetti.utils import load_multivariate_ts_from_csv
from confetti.visualizations import plot_counterfactual
import keras

# Load model
model_path = "examples/models/toy_fcn.keras"
model = keras.models.load_model(model_path)

# Load dataset in (n_samples, time_steps, n_features) format
X_train, y_train = load_multivariate_ts_from_csv("examples/data/toy_train.csv")
X_test, y_test   = load_multivariate_ts_from_csv("examples/data/toy_test.csv")

# Select instance to explain
instance = X_test[0:1]

# Generate CAM weights for training data (optional)
training_weights = cam(model, X_train)

# Initialize explainer
explainer = CONFETTI(model_path=model_path)

# Generate counterfactuals
results = explainer.generate_counterfactuals(
    instances_to_explain=instance,
    reference_data=X_train,
    reference_weights=training_weights,      # or None if not available
)

# results is a CounterfactualResults list — one CounterfactualSet per instance.
# Each set contains the original instance, the best counterfactual, all
# candidates, and the NUN's CAM feature-importance weights (when provided).
cf_set = results[0]

# Visualize the best counterfactual
plot_counterfactual(
    original=cf_set.original_instance,
    counterfactual=cf_set.best,
    cam_weights=cf_set.feature_importance,
    cam_mode="heatmap",
    title="Counterfactual Explanation"
)

Counterfactual Example

In the visualization:

  • green curves represent the original instance
  • red curves represent the counterfactual subsequence
  • the heatmap corresponds to CAM scores of the nearest unlike neighbor

🆕 What's New in v1.0.1

  • Stable release — production-ready API with no breaking changes expected
  • Rust-accelerated backend — distances (DTW, Soft-DTW, GAK, Manhattan) and NSGA-III components via PyO3
  • Custom NSGA-III — zero-dependency genetic algorithm (pymoo removed)
  • Built-in distance metrics — pure-numpy DTW, Soft-DTW, GAK, CTW, Manhattan (tslearn removed)
  • PyTorch adapter — use PyTorch models alongside Keras and scikit-learn
  • Visualization theming — light/dark theme support with improved plot aesthetics

See the full CHANGELOG for details.


📚 Documentation

The full documentation, including usage guides, API reference, and examples, is available at:

👉 https://confetti-ts.readthedocs.io/en/latest/


📄 License

CONFETTI is released under the MIT License.


📝 Citing CONFETTI

If you use CONFETTI in your research, please consider citing the following paper:

@inproceedings{cetina2026counterfactual,
  title={Counterfactual Explainable AI (XAI) Method for Deep Learning-Based Multivariate Time Series Classification},
  author={Cetina, Alan Gabriel Paredes and Benguessoum, Kaouther and Lourenco, Raoni and Kubler, Sylvain},
  booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},
  volume={40},
  number={21},
  pages={17393--17400},
  year={2026}
}

The original code used when the method was published is at the paper branch of this repository. It contains the experiment scripts, model configurations, and dataset handling used in the publication.

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

confetti_ts-1.0.1.tar.gz (612.7 kB view details)

Uploaded Source

Built Distributions

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

confetti_ts-1.0.1-cp313-cp313-win_amd64.whl (330.0 kB view details)

Uploaded CPython 3.13Windows x86-64

confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (497.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (486.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

confetti_ts-1.0.1-cp313-cp313-macosx_11_0_arm64.whl (434.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

confetti_ts-1.0.1-cp313-cp313-macosx_10_12_x86_64.whl (449.0 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

confetti_ts-1.0.1-cp312-cp312-win_amd64.whl (330.4 kB view details)

Uploaded CPython 3.12Windows x86-64

confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (497.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (486.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

confetti_ts-1.0.1-cp312-cp312-macosx_11_0_arm64.whl (434.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

confetti_ts-1.0.1-cp312-cp312-macosx_10_12_x86_64.whl (449.3 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

Details for the file confetti_ts-1.0.1.tar.gz.

File metadata

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

File hashes

Hashes for confetti_ts-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e8ee65c08e479871cd1fbde864e5e236fd53a433b942c36fd7b56fb6f9aa3273
MD5 aa9efc775fa659b111d06834a3add467
BLAKE2b-256 9434baf2c6d8ddd4349deb8fbb59a7b6acf15bf91f89e61af72ca69a5f7fd570

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1.tar.gz:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: confetti_ts-1.0.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 330.0 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for confetti_ts-1.0.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e15047fb5275c1e06f2f4eabb4845e8dbe3af0e143daa67fdb99fd74b7d307bc
MD5 13986e7ad8803499232f9dc7c3944fad
BLAKE2b-256 f6353b099109aa1aa5b7eb717c7698308bfba5eec400a0b28fb46ae44d413954

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp313-cp313-win_amd64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3660510812aa2140e76fe08ffd80388c6aa7b75894057d5daa3891aa3973f8ee
MD5 e75e13c29f5d859dc7f5493289a571bd
BLAKE2b-256 e8d7c8e71a48916336c94e1d3bdddb4c503bfd4d339a5a49e4fa24a82aa56488

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3ad5a414b8158759a0876b6a4c7e6296922f18e6117b8686170d3ef1e0f86095
MD5 9cd4772dbeaab58dc1c19aea1f29f049
BLAKE2b-256 8c9fad68043a87f39791b0324d971a162130c4293cdab20925b98f8be314845c

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 708f13e858ef2a9088067dd1aee83ac7b8272cc5aae50b26135cc600940c50d4
MD5 5ae2876df370c6d066cb785f52d72e9c
BLAKE2b-256 f867472801a18efdcce343845d5486a6a909e9901efd6a74b3312346e1eb3b87

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.1-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e587d16647554239bcd0898b5de7889696e06169fd9d04be5e974cac50609f72
MD5 0fedf5e25bf71f8639d63f163f49f7a8
BLAKE2b-256 1ee39aa92788c5813bcfdfcac16af288f9df8fd731b5161df464393e8c893f49

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp313-cp313-macosx_10_12_x86_64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: confetti_ts-1.0.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 330.4 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for confetti_ts-1.0.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e2c7a48982109b0db326757e6e77c56942e95c0cf326cb8818ecfc38b02a5a76
MD5 93cda267408ded6e7bedee8fe474546b
BLAKE2b-256 d032a5e06bef16bd356ff52487e66f7cbc7a67c7169d6db02b08f0fea5e43863

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp312-cp312-win_amd64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 df2c87a8dbea46c7438e6823b8bcc6e4314b50580cee2537b50ad975a5e977a2
MD5 531ddbfce7f92e684381d32bce79fffa
BLAKE2b-256 1bc9663565e2a5974b113f26530c285ec2e91e14d1c656c96f5ee567d0538a00

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d2aa91e2002b8fbb2e181d46232f0306869563572b582198c41630d344b0fc26
MD5 6efceb03a94d9593ff54043eaebf8e77
BLAKE2b-256 04975d1b804c6fed263b497967ecadcfc77e84042e3a0d37ab4d29831fc89dd3

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 71f3888c8fa282d5c30fdd29de14242296800959626f51e20a815e3b23492436
MD5 4dfaea6d0edd705be7bd70e5a625555b
BLAKE2b-256 bbcb718f906794f3118ae217b2c69debd75380693b0fa07ac0512c57d5a06c38

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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

File details

Details for the file confetti_ts-1.0.1-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9307b06a9d0eed8a20cca1b3447741e5dfbe2fe7552c498d7046b05da77d91ec
MD5 46cf9c17be9e95b0dd56cbe96d2f923b
BLAKE2b-256 b69182109a64ffa840f96c8f8ce04ba319137e7d9d2f51b155819b72ce508d29

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.1-cp312-cp312-macosx_10_12_x86_64.whl:

Publisher: release.yml on serval-uni-lu/confetti

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