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 withgit 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"
)
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.
Metadata
Release files for confetti-ts 1.0.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 |
|---|---|---|---|---|
| confetti_ts-1.0.1.tar.gz | 612.7 kB | With provenance | Details for confetti_ts-1.0.1.tar.gz |
Built distributions (wheels)
Total release size: 5.0 MB
Release files / confetti_ts-1.0.1.tar.gz
| Download URL | confetti_ts-1.0.1.tar.gz |
|---|---|
| Size | 612.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e8ee65c08e479871cd1fbde864e5e236fd53a433b942c36fd7b56fb6f9aa3273
|
|
BLAKE2b-256 checksum How to use checksums |
9434baf2c6d8ddd4349deb8fbb59a7b6acf15bf91f89e61af72ca69a5f7fd570
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp313-cp313-win_amd64.whl
| Download URL | confetti_ts-1.0.1-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 330.0 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
e15047fb5275c1e06f2f4eabb4845e8dbe3af0e143daa67fdb99fd74b7d307bc
|
|
BLAKE2b-256 checksum How to use checksums |
f6353b099109aa1aa5b7eb717c7698308bfba5eec400a0b28fb46ae44d413954
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 497.5 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
3660510812aa2140e76fe08ffd80388c6aa7b75894057d5daa3891aa3973f8ee
|
|
BLAKE2b-256 checksum How to use checksums |
e8d7c8e71a48916336c94e1d3bdddb4c503bfd4d339a5a49e4fa24a82aa56488
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | confetti_ts-1.0.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 486.9 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
3ad5a414b8158759a0876b6a4c7e6296922f18e6117b8686170d3ef1e0f86095
|
|
BLAKE2b-256 checksum How to use checksums |
8c9fad68043a87f39791b0324d971a162130c4293cdab20925b98f8be314845c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | confetti_ts-1.0.1-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 434.3 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
708f13e858ef2a9088067dd1aee83ac7b8272cc5aae50b26135cc600940c50d4
|
|
BLAKE2b-256 checksum How to use checksums |
f867472801a18efdcce343845d5486a6a909e9901efd6a74b3312346e1eb3b87
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp313-cp313-macosx_10_12_x86_64.whl
| Download URL | confetti_ts-1.0.1-cp313-cp313-macosx_10_12_x86_64.whl |
|---|---|
| Size | 449.0 kB |
| Tags | CPython 3.13 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
e587d16647554239bcd0898b5de7889696e06169fd9d04be5e974cac50609f72
|
|
BLAKE2b-256 checksum How to use checksums |
1ee39aa92788c5813bcfdfcac16af288f9df8fd731b5161df464393e8c893f49
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp312-cp312-win_amd64.whl
| Download URL | confetti_ts-1.0.1-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 330.4 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
e2c7a48982109b0db326757e6e77c56942e95c0cf326cb8818ecfc38b02a5a76
|
|
BLAKE2b-256 checksum How to use checksums |
d032a5e06bef16bd356ff52487e66f7cbc7a67c7169d6db02b08f0fea5e43863
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 497.9 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
df2c87a8dbea46c7438e6823b8bcc6e4314b50580cee2537b50ad975a5e977a2
|
|
BLAKE2b-256 checksum How to use checksums |
1bc9663565e2a5974b113f26530c285ec2e91e14d1c656c96f5ee567d0538a00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | confetti_ts-1.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 486.4 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
d2aa91e2002b8fbb2e181d46232f0306869563572b582198c41630d344b0fc26
|
|
BLAKE2b-256 checksum How to use checksums |
04975d1b804c6fed263b497967ecadcfc77e84042e3a0d37ab4d29831fc89dd3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | confetti_ts-1.0.1-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 434.3 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
71f3888c8fa282d5c30fdd29de14242296800959626f51e20a815e3b23492436
|
|
BLAKE2b-256 checksum How to use checksums |
bbcb718f906794f3118ae217b2c69debd75380693b0fa07ac0512c57d5a06c38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logRelease files / confetti_ts-1.0.1-cp312-cp312-macosx_10_12_x86_64.whl
| Download URL | confetti_ts-1.0.1-cp312-cp312-macosx_10_12_x86_64.whl |
|---|---|
| Size | 449.3 kB |
| Tags | CPython 3.12 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
9307b06a9d0eed8a20cca1b3447741e5dfbe2fe7552c498d7046b05da77d91ec
|
|
BLAKE2b-256 checksum How to use checksums |
b69182109a64ffa840f96c8f8ce04ba319137e7d9d2f51b155819b72ce508d29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logSecurity for confetti-ts 1.0.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.
Full provenance
All 11 files in this release have been uploaded with provenance metadata.
Attestation for confetti_ts-1.0.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 21, 2026.
Transparency logA 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.