Skip to main content

Counterfactual explanation algorithms for time-series models (Wachter, Native-Guide, etc.)

Project description

Counterfactual Explanation Algorithms for Time Series Models

License: MIT Python 3.9+ PyTorch Code style: black

A comprehensive collection of counterfactual explanation algorithms for time series classification with PyTorch implementations. This library provides state-of-the-art methods for generating and evaluating counterfactual explanations, helping to understand and interpret deep learning models for time series data.

Table of Contents

Quick Start

# Install dependencies
pip install -r requirements.txt

# Run all examples and evaluations
cd examples
python run_all.py

# Run individual examples
python example_univariate.py          # FordA dataset
python example_multivariate.py   # Multi-channel Arabic digits
python metrics_evaluation_example.py  # Comprehensive metrics

Implemented Algorithms

This library implements 28 state-of-the-art counterfactual explanation methods for time series classification, organized into:

  • Optimization-Based Methods: Wachter, COMTE, TSCF, TS-Tweaking, FFT-CF
  • Evolutionary Methods: MOC/DANDL, TSEvo, Multi-SpaCE, Sub-SpaCE
  • Instance-Based Methods: Native Guide, CELS/M-CELS, AB-CF
  • Latent Space Methods: CGM, CounTS, Latent-CF, LASTS, GLACIER
  • Segment-Based Methods: SETS, SG-CF, DisCOX, CFWoT, TS-CEM
  • Hybrid Methods: SPARCE, Time-CF, TeRCE, MG-CF, TimeX, TimeX++

๐Ÿ“š For detailed descriptions, key features, academic references, and code examples for each method, see REFERENCES.md

Comprehensive Evaluation Metrics

The library includes a complete suite of metrics for evaluating counterfactual quality across six key dimensions:

Validity Metrics (cfts/metrics/validity.py)

  • prediction_change: Verifies target class prediction is achieved
  • class_probability_confidence: Measures prediction confidence
  • decision_boundary_distance: Distance from decision boundary

Proximity Metrics (cfts/metrics/proximity.py)

  • l2_distance: Euclidean distance between time series
  • manhattan_distance: L1 distance measure
  • dtw_distance: Dynamic Time Warping distance
  • frechet_distance: Temporal ordering-aware distance
  • normalized_distance: Scale-invariant distance

Sparsity Metrics (cfts/metrics/sparsity.py)

  • l0_norm: Number of modified time points
  • percentage_changed_points: Fraction of changes
  • segment_based_sparsity: Continuous segment modifications
  • gini_sparsity_coefficient: Distribution of change magnitudes

Realism Metrics (cfts/metrics/realism.py)

  • domain_constraint_violations: Domain-specific rule violations
  • statistical_similarity: Distribution similarity to original data
  • temporal_consistency: Temporal pattern preservation
  • autocorrelation_preservation: Time dependency maintenance
  • spectral_similarity: Frequency domain characteristics

Diversity Metrics (cfts/metrics/diversity.py)

  • pairwise_distance: Diversity between multiple counterfactuals
  • coverage_metric: Feature space coverage
  • novelty_metric: Uniqueness compared to training data
  • diversity_index: Shannon diversity index

Stability Metrics (cfts/metrics/stability.py)

  • algorithmic_stability: Consistency across runs
  • input_stability: Robustness to input perturbations
  • hyperparameter_sensitivity: Parameter stability analysis

Examples and Datasets

Available Examples

  • example_univariate.py: FordA automotive dataset (UCR Archive)
  • example_multivariate.py: Multi-channel spoken Arabic digits
  • metrics_evaluation_example.py: Comprehensive metrics demonstration

Supported Datasets

  • UCR Time Series Archive: Automatic download and preprocessing
  • Synthetic Data: Built-in generators for controlled experiments
  • Custom Datasets: Easy integration with custom time series data

Pre-trained Models

  • simple_cnn_2.pth: Binary classification (FordA)
  • cnn_multi_arabicdigits_10ch.pth: Multi-class, multi-channel

Visualization Examples

The library generates publication-ready visualizations:

FordA Counterfactual Explanations Individual counterfactuals and overlay comparisons for FordA dataset

Arabic Digits Counterfactual Explanations Multi-channel counterfactual analysis for Arabic digits

Keane Metrics Comparison Evaluation using Keane et al. (2021) metrics: Validity, Proximity, and Compactness across all algorithms

Installation

Requirements

  • Python 3.9+
  • PyTorch 2.0+
  • NumPy, SciPy, Matplotlib
  • Scikit-learn
  • Captum (for attribution methods)
  • Optional: dtaidistance (for DTW distance metric)

Setup

# Clone the repository
git clone https://github.com/visual-xai-for-time-series/counterfactual-explanations-for-time-series.git
cd counterfactual-explanations-for-time-series

# Install dependencies
pip install -r requirements.txt

Usage

Basic Usage

import numpy as np
from cfts.cf_wachter import wachter_genetic_cf
from cfts.cf_native_guide import native_guide_uni_cf
from cfts.metrics import l2_distance, prediction_change

# Generate counterfactual
cf, prediction = wachter_genetic_cf(sample, model, step_size=0.1)

# Evaluate quality
proximity = l2_distance(sample, cf)
validity = prediction_change(model, sample, cf, target_class=1)

Advanced Example

from cfts.metrics import CounterfactualEvaluator, benchmark_algorithms

# Comprehensive evaluation
evaluator = CounterfactualEvaluator()
results = benchmark_algorithms(
    algorithms=['wachter', 'native_guide', 'comte', 'sets'],
    samples=test_samples,
    model=trained_model,
    dataset=dataset
)

Running All Examples

# Execute complete pipeline
python examples/run_all.py

Key Features

  • Research-Ready: Implementations of state-of-the-art algorithms
  • Comprehensive Metrics: Six categories of evaluation measures
  • Rich Visualizations: Publication-quality plots and comparisons
  • Easy Integration: Simple API for custom models and datasets
  • Efficient: Optimized implementations with GPU support
  • Well-Documented: Extensive examples and documentation
  • Reproducible: Seed control and deterministic results
  • Robust: Error handling and input validation

Project Structure

counterfactual-explanations-for-time-series/
โ”œโ”€โ”€ cfts/                          # Main library
โ”‚   โ”œโ”€โ”€ cf_ab_cf/                 # AB-CF implementation
โ”‚   โ”œโ”€โ”€ cf_cels/                  # CELS implementation
โ”‚   โ”œโ”€โ”€ cf_cem/                   # TS-CEM implementation
โ”‚   โ”œโ”€โ”€ cf_cfwot/                 # CFWoT implementation
โ”‚   โ”œโ”€โ”€ cf_cgm/                   # CGM implementation
โ”‚   โ”œโ”€โ”€ cf_comte/                 # COMTE implementation
โ”‚   โ”œโ”€โ”€ cf_counts/                # CoUNTS implementation
โ”‚   โ”œโ”€โ”€ cf_dandl/                 # MOC implementation
โ”‚   โ”œโ”€โ”€ cf_discox/                # DisCOX implementation
โ”‚   โ”œโ”€โ”€ cf_fastpace/              # FastPACE implementation
โ”‚   โ”œโ”€โ”€ cf_fft_cf/                # FFT-CF implementation
โ”‚   โ”œโ”€โ”€ cf_glacier/               # GLACIER implementation
โ”‚   โ”œโ”€โ”€ cf_lasts/                 # LASTS implementation
โ”‚   โ”œโ”€โ”€ cf_latent_cf/             # Latent CF implementation
โ”‚   โ”œโ”€โ”€ cf_mg_cf/                 # MG-CF implementation
โ”‚   โ”œโ”€โ”€ cf_multispace/            # Multi-SpaCE implementation
โ”‚   โ”œโ”€โ”€ cf_native_guide/          # Native Guide implementation
โ”‚   โ”œโ”€โ”€ cf_sets/                  # SETS implementation
โ”‚   โ”œโ”€โ”€ cf_sg_cf/                 # SG-CF implementation
โ”‚   โ”œโ”€โ”€ cf_sparce/                # SpArCE implementation
โ”‚   โ”œโ”€โ”€ cf_subspace/              # Sub-SpaCE implementation
โ”‚   โ”œโ”€โ”€ cf_terce/                 # TERCE implementation
โ”‚   โ”œโ”€โ”€ cf_time_cf/               # Time-CF implementation
โ”‚   โ”œโ”€โ”€ cf_timex/                 # TimeX implementation
โ”‚   โ”œโ”€โ”€ cf_timex_plus_plus/       # TimeX++ implementation
โ”‚   โ”œโ”€โ”€ cf_ts_tweaking/           # TS-Tweaking implementation
โ”‚   โ”œโ”€โ”€ cf_tscf/                  # TSCF implementation
โ”‚   โ”œโ”€โ”€ cf_tsevo/                 # TSEvo implementation
โ”‚   โ”œโ”€โ”€ cf_wachter/               # Wachter et al. implementation
โ”‚   โ””โ”€โ”€ metrics/                  # Evaluation metrics
โ”‚       โ”œโ”€โ”€ validity.py           # Validity metrics
โ”‚       โ”œโ”€โ”€ proximity.py          # Proximity metrics
โ”‚       โ”œโ”€โ”€ sparsity.py           # Sparsity metrics
โ”‚       โ”œโ”€โ”€ realism.py            # Realism metrics
โ”‚       โ”œโ”€โ”€ diversity.py          # Diversity metrics
โ”‚       โ””โ”€โ”€ stability.py          # Stability metrics
โ”œโ”€โ”€ examples/                      # Usage examples
โ”‚   โ”œโ”€โ”€ example_univariate.py     # FordA dataset example
โ”‚   โ”œโ”€โ”€ example_multivariate.py   # Arabic digits example
โ”‚   โ”œโ”€โ”€ metrics_evaluation_example.py  # Metrics demo
โ”‚   โ””โ”€โ”€ run_all.py               # Execute all examples
โ”œโ”€โ”€ models/                       # Pre-trained models
โ””โ”€โ”€ requirements.txt              # Dependencies

License

Released under MIT License. See the LICENSE file for details.

References and Citations

If you use the library, please cite one of the following sources (the second one is preferred).

Core Library Citation

@software{cfts-us-2025,
  author = {Schlegel, Udo},
  title = {Counterfactual Explanation Algorithms for Time Series Models},
  url = {https://github.com/visual-xai-for-time-series/counterfactual-explanations-for-time-series},
  year = {2025}
}

Reference Paper Citation

@inproceedings{schlegel_what-if_2026,
  title={What-If Explanations Over Time: Counterfactuals for Time Series Classification},
  author={Schlegel, Udo and Seidl, Thomas},
  booktitle={World Conference on Explainable Artificial Intelligence (XAI)},
  year={2026}
}

Related Work and Surveys

  • Guidotti, R. (2022). "Counterfactual explanations and how to find them: literature review and benchmarking." Data Mining and Knowledge Discovery, 1-55.

  • Verma, S., et al. (2020). "Counterfactual explanations for machine learning: A review." arXiv preprint arXiv:2010.10596.

  • Molnar, C. (2020). "Interpretable machine learning: A guide for making black box models explainable." christophm.github.io/interpretable-ml-book/


Disclaimer

AI-Assisted Development: Please note that portions of this codebase have been generated or enhanced with the assistance of AI coding tools. While we have thoroughly tested and validated all implementations, users are encouraged to review the code and verify its correctness for their specific use cases.


Acknowledgments

This library builds upon numerous research contributions in explainable AI and counterfactual explanations. We thank all researchers and developers who have contributed to this field, particularly the authors of the implemented algorithms.

Special thanks to:

  • The UCR Time Series Classification Archive for providing standard datasets
  • The PyTorch and scikit-learn communities for excellent tools
  • All contributors and users who help improve this library

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

counterfactuals_for_time_series-0.1.5.tar.gz (203.2 kB view details)

Uploaded Source

Built Distribution

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

counterfactuals_for_time_series-0.1.5-py3-none-any.whl (254.5 kB view details)

Uploaded Python 3

File details

Details for the file counterfactuals_for_time_series-0.1.5.tar.gz.

File metadata

File hashes

Hashes for counterfactuals_for_time_series-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f7b8a96d0cf954321b8e195891cb78c8fe8a6becdb6053235e67ec45d76f39b6
MD5 314d2c7c3b22b098872c36b0eabd7c20
BLAKE2b-256 a8e3fe3feb73595140f30df153ece527d16999ea687b3c1e5afa289fcce1f655

See more details on using hashes here.

Provenance

The following attestation bundles were made for counterfactuals_for_time_series-0.1.5.tar.gz:

Publisher: publish.yml on visual-xai-for-time-series/counterfactual-explanations-for-time-series

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

File details

Details for the file counterfactuals_for_time_series-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for counterfactuals_for_time_series-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 789b2c41a5d4d17e454a57eca3484602fef835b890b923d89fa3ffdde098150c
MD5 4b9d610553b07bce4094a26cd8ed2336
BLAKE2b-256 cc05a80d4587dcb460de38637cf1841095aa05c1263db1c540e9f6ee21f8494b

See more details on using hashes here.

Provenance

The following attestation bundles were made for counterfactuals_for_time_series-0.1.5-py3-none-any.whl:

Publisher: publish.yml on visual-xai-for-time-series/counterfactual-explanations-for-time-series

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