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.0

  • 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.0.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.0-cp313-cp313-win_amd64.whl (337.3 kB view details)

Uploaded CPython 3.13Windows x86-64

confetti_ts-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (503.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

confetti_ts-1.0.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (492.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

confetti_ts-1.0.0-cp313-cp313-macosx_11_0_arm64.whl (437.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

confetti_ts-1.0.0-cp313-cp313-macosx_10_12_x86_64.whl (450.6 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

confetti_ts-1.0.0-cp312-cp312-win_amd64.whl (337.3 kB view details)

Uploaded CPython 3.12Windows x86-64

confetti_ts-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (503.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

confetti_ts-1.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (492.2 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

confetti_ts-1.0.0-cp312-cp312-macosx_11_0_arm64.whl (437.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

confetti_ts-1.0.0-cp312-cp312-macosx_10_12_x86_64.whl (450.4 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: confetti_ts-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 b08ce82d6ebd61d7cdefb8723f174c938d0b3aefde0122b55c033011971346b0
MD5 7dc2a04934b7538520c6a76cf8b84e19
BLAKE2b-256 701c7aeb1fec39af850ac77b04e3e65a961602963a2c65de33561d33d6119a84

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0.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.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: confetti_ts-1.0.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 337.3 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.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6fbd3a8f2ed0039547af89425b416d1c40b37b0b5fa0c55e75198ab2ba77def8
MD5 1da4fdda03b38c5799748cfbafbecdda
BLAKE2b-256 f27ae4d114d9059476d2e307974f81fed7a4264eddbd00639b4fe04889f71cf6

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a4bfb1e4890e2dbd1760dd0fe7601d725e8632bc241d698a2dcb4e269b4d449d
MD5 64e239d3d733f37d50d839f226059350
BLAKE2b-256 4075ee0afa5bd375f9ee316e99bf98dffd6328182772a907ce579adab3136cd7

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ac869e64a4ef23075a263a2aa76407ddf6228b848e7ac8de127fe2c6f73187f9
MD5 a93fe0ce36b78d99961ad9766e62fd84
BLAKE2b-256 69775f62d44e8c60aa9132772759d199111c3f3f885fae35d1cfc9170fa106d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dba3753de08d822d2794d3eb08f5bddcfa206a416c2561dfbe01a387b7930757
MD5 9c13b570b2067f9a97492c0772f00880
BLAKE2b-256 5c0e99d541256d452f023320bc6e92d7e924d1670a772bdf61ae9db81d43f783

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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.0-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d99cb9182a824b18a2111983a645d32464ea04724219da4c5014b9e3b3016b25
MD5 ccd0921b9d881537dca0df2ccc29f2f9
BLAKE2b-256 9522f509c84c06c5d16aaece6ae5ec365b33ff2422cb0ccf36c1be22e828c9db

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: confetti_ts-1.0.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 337.3 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.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 880273ce9fe95328f86d4bdff1332c440dfd9543cb03a4e9f8c1275eb5bce3af
MD5 173ee86b45f7f17b31a5dbff83588d90
BLAKE2b-256 b3ad076ac38bb71aeeec6bca74cd4047809d3228bebe467ead5b4af6ec522ebc

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 16241bbe9f9dd5ac65a8323e076ac9177be618f5eb11da016987d5589675cf2c
MD5 f4ffb76d20f6057cb2ff74ff18619dd5
BLAKE2b-256 00e2caf8f843d40124f565aed6d6ccf8c01fcd28f909dc0c534a50e05c0d8bd6

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c6b66975e2e56d61f9080dd9c8dda074707618343869638ef4f197538306858d
MD5 8cb3123da3c5fc8a292b65a93d1c4ed8
BLAKE2b-256 ca2b5fcd8130c48b1dc51ff7c533e042c877fcea5f5714368cd5f82a58ee033f

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 612527505501cf0ca6ff4099e9b26fe0f9f416080d54dfedbbf54fc9f0fb4d29
MD5 1d463b447fae6fcee9503def9dde476d
BLAKE2b-256 15c21ff1dcf76fd3f50724d7a44aabefdf6ee572eb018431e0449b6ba98f34d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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.0-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for confetti_ts-1.0.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c7211226889fc05383deaebb91c503fdeeab17e17c958974a0e4d976c42398fa
MD5 b547ca9db2f8aa6cd36d2da69e60492f
BLAKE2b-256 62d922fb5539d36941951bfb4cf2a4eca174c93261678607be99f06c2470aab4

See more details on using hashes here.

Provenance

The following attestation bundles were made for confetti_ts-1.0.0-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