A case-based reasoning python library that aims to help researchers find similar cases according to an input case with a wide range of methods that can detect similarity based on the features of each time series
Project description
CBR-FoX: Case-Based Reasoning for Time Series Forecasting
CBR-FoX is a Python library designed to provide case-based reasoning explanations for time series prediction models. This approach enhances the transparency and interpretability of machine learning models applied to sequential data through explainable AI capabilities.
Table of Contents
- Features
- Installation
- Quick Start
- Usage Workflow
- Documentation
- Architecture
- Citation
- Support
- Operating Environment
- License
- Contributing
- Acknowledgments
Features
- Case-Based Reasoning (CBR) Implementation: Utilizes case-based reasoning to enhance explainability in time series predictions
- Multiple Distance Metrics: DTW, Euclidean, Pearson correlation, and custom CCI metric
- Explainable AI: Visualize and understand case selection process with clear, human-readable insights
- Flexible Architecture: Easy integration with custom metrics via sktime adapter
- Versatile & Adaptable: Supports various types of time series data (univariate, multivariate)
- ML Model Compatibility: Easily integrates with common machine learning models
- Comprehensive Visualization: Built-in plotting utilities with matplotlib and plotly
- Production Ready: Fully tested, documented, and containerized
Installation
Install via PyPI
pip install CBR-FoX
Using conda
conda env create -f environment.yml
conda activate cbr_fox
Install via GitHub (Development)
# Clone the repository
git clone https://github.com/aaaimx/CBR-FoX.git
cd CBR-FoX
# Install required dependencies
pip install -r requirements.txt
# Install in development mode
pip install -e .
Using Docker
# Build and run with docker-compose
docker-compose up cbr_fox
# Or run directly
docker build -t cbr_fox .
docker run cbr_fox python scripts/examples/reproduce_all_figures.py
Quick Start
from cbr_fox.core import cbr_fox
from cbr_fox.builder import cbr_fox_builder
import numpy as np
# Create sample data
# training_windows: shape [n_windows, window_len, n_features]
training_windows = np.random.randn(100, 24, 3)
target_windows = np.random.randn(100, 3)
forecast_window = np.random.randn(24, 3)
# Initialize CBR with DTW metric
cbr = cbr_fox(metric="dtw", smoothness_factor=0.2)
# Fit and predict
cbr.fit(training_windows, target_windows, forecast_window)
prediction = np.random.randn(3)
cbr.predict(prediction, num_cases=5, mode="simple")
# Get analysis report
report = cbr.get_analysis_report()
print(report)
# Visualize results
from cbr_fox.utils import plot_utils
plot_utils.visualize_pyplot(cbr)
Usage Workflow
Follow these steps to use CBR-FoX in your projects:
1. Retrieve Model Information
Extract the relevant inputs and outputs from your AI model:
# Get your model's training windows, targets, and forecasts
training_windows = model.get_training_windows() # shape: [n_windows, window_len, n_features]
target_windows = model.get_targets() # shape: [n_windows, n_features]
forecast_window = model.get_current_window() # shape: [window_len, n_features]
prediction = model.predict() # shape: [n_features]
2. Create CBR-FoX Instances
from cbr_fox.core import cbr_fox
# Single metric instance
cbr_instance = cbr_fox(metric="dtw", smoothness_factor=0.2)
# Or multiple instances for comparison
cbr_dtw = cbr_fox(metric="dtw")
cbr_euclidean = cbr_fox(metric="euclidean")
cbr_cci = cbr_fox(metric="cci_distance", kwargs={"punishedSumFactor": 0.5})
3. Initialize the Builder (Optional - for comparing multiple techniques)
from cbr_fox.builder import cbr_fox_builder
# Create builder with multiple techniques
builder = cbr_fox_builder([cbr_dtw, cbr_euclidean, cbr_cci])
4. Train the Instance
# Single instance
cbr_instance.fit(training_windows, target_windows, forecast_window)
# Or with builder
builder.fit(training_windows, target_windows, forecast_window)
5. Obtain Explanations
# Single instance
cbr_instance.predict(prediction=prediction, num_cases=5, mode="simple")
report = cbr_instance.get_analysis_report()
# Or with builder
builder.predict(prediction=prediction, num_cases=5, mode="simple")
6. Visualize Results
# Single instance visualization
plot_utils.visualize_pyplot(
cbr_instance,
fmt='--d',
scatter_params={'s': 50},
xtick_rotation=50,
title='CBR-FoX Analysis',
xlabel='Time Steps',
ylabel='Values'
)
# Or with builder (compares all techniques)
builder.visualize_pyplot(
mode="individual", # or "combined"
fmt='--o',
title='Multi-Metric Comparison'
)
Documentation
Full documentation is available at https://cbr-fox.readthedocs.io
Reproducing Paper Figures
To reproduce all figures from the publication:
python scripts/examples/reproduce_all_figures.py --output-dir figures
Individual figures can be generated using:
python scripts/examples/figure_1_architecture.py
python scripts/examples/figure_5_metric_comparison.py
Architecture
Library Usage Diagram
The following diagram illustrates the typical workflow of CBR-FoX, from retrieving AI model outputs to generating visual explanations:
Class Diagram
Detailed class relationships and module dependencies:
classDiagram
class cbr_fox {
+metric: Union[str, Callable]
+fit(training_windows, target_windows, forecast_window)
+predict(prediction, num_cases, mode)
+get_analysis_report() DataFrame
}
class cbr_fox_builder {
+techniques: List[cbr_fox]
+fit(training_windows, target_windows, forecast_window)
+predict(prediction, num_cases, mode)
+visualize_pyplot(mode)
}
class sktime_interface {
+compute_distance_interface(input_dict, metric, kwargs)
}
cbr_fox_builder --> cbr_fox
cbr_fox --> sktime_interface
Module File Relations
The following diagram represents the core classes and their interactions within the library:
For detailed architecture documentation including type hints and interface specifications, see the Architecture Documentation.
Citation
If you use CBR-FoX in your research, please cite:
BibTeX
@article{cbr_fox2025,
title = {CBR-FoX: A Python Framework for Case-Based Reasoning in Time Series Forecasting},
author = {Gerardo A. Pérez-Pérez and
Moisés F. Valdez-Ávila and
Mauricio G. Orozco-del-Castillo and
Carlos Bermejo-Sabbagh and
Juan A. Recio-García},
journal = {SoftwareX},
year = {2025},
pages = {15},
url = {https://github.com/aaaimx/CBR-FoX}
}
APA
[Your Name]. (2025). CBR-FoX: A Python Framework for Case-Based Reasoning
in Time Series Forecasting. SoftwareX, [Volume]([Issue]), [Pages].
https://doi.org/[DOI]
For the software itself, please also cite:
@software{cbr_fox_software,
author = {Gerardo A. Pérez-Pérez and
Moisés F. Valdez-Ávila and
Mauricio G. Orozco-del-Castillo and
Carlos Bermejo-Sabbagh and
Juan A. Recio-García},
title = {CBR-FoX: Case-Based Reasoning for Time Series Forecasting},
year = {2025},
version = {1.0.1},
url = {https://github.com/aaaimx/CBR-FoX},
doi = {10.5281/zenodo.XXXXXXX}
}
See CITATION.cff for machine-readable citation metadata.
💬 Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Read the Docs
- Email: your-email@example.com
🖥️ Operating Environment
Supported Platforms
| Platform | Versions | Status |
|---|---|---|
| Linux | Ubuntu 20.04+, Debian 10+, CentOS 8+ | ✅ Fully Supported |
| macOS | 11 (Big Sur) and later | ✅ Fully Supported |
| Windows | 10, 11 | ✅ Fully Supported |
Python Support
- Minimum Python Version: 3.9
- Recommended: Python 3.11
- Maximum Tested: Python 3.12
Dependencies
All dependencies are pinned in requirements.txt and environment.yml for reproducibility. Key dependencies include:
- NumPy >= 1.24.0, < 2.1.0
- SciPy >= 1.10.0, < 1.14.0
- pandas >= 2.0.0, < 2.4.0
- scikit-learn >= 1.3.0, < 1.7.0
- sktime == 0.38.5
- matplotlib >= 3.7.0, < 3.10.0
- statsmodels >= 0.14.0, < 0.15.0
See NOTICE for third-party license information.
Installation Verification
# Run tests to verify installation
pytest tests/ -v
# Check coverage
pytest tests/ --cov=cbr_fox --cov-report=html
📄 License
This project is licensed under the European Union Public Licence (EUPL) v. 1.2.
- License: EUPL-1.2
- SPDX:
EUPL-1.2 - OSI Approved: Yes
- Compatible with: GPL, MIT, BSD, Apache, MPL
Third-Party Compliance
All third-party dependencies have been verified for EUPL 1.2 compatibility. See NOTICE for detailed license information of all dependencies.
Commercial Use
EUPL allows commercial use, modification, and distribution under the same license (copyleft). For specific use cases, please review the full license text.
🤝 Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Key areas for contribution:
- New distance metrics
- Visualization enhancements
- Documentation improvements
- Bug reports and fixes
- Performance optimizations
🌟 Acknowledgments
This work was supported by [Your Funding Source/Institution].
Special thanks to:
- The sktime community for their excellent time series toolkit
- The scikit-learn community for machine learning foundations
- All contributors and users of CBR-FoX
Maintained by: AAAIMX
Institution: AAAIMX - Advanced Analytics and AI Research Group
Repository: https://github.com/aaaimx/CBR-FoX
Last Updated: October 2025
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cbr_fox-1.0.8.tar.gz.
File metadata
- Download URL: cbr_fox-1.0.8.tar.gz
- Upload date:
- Size: 34.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81261df85dbe3c4e89bc0f1b1ef973f878503b2cbc323d71f731c914fb48f3ae
|
|
| MD5 |
0f05eb7fb372e824c0d1243dda62ec1c
|
|
| BLAKE2b-256 |
2dac7f4c880e691de7be6201b61c1d85db439c4a989d6a453fa749f10a91a100
|
Provenance
The following attestation bundles were made for cbr_fox-1.0.8.tar.gz:
Publisher:
publish.yml on aaaimx/CBR-FoX
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cbr_fox-1.0.8.tar.gz -
Subject digest:
81261df85dbe3c4e89bc0f1b1ef973f878503b2cbc323d71f731c914fb48f3ae - Sigstore transparency entry: 621116614
- Sigstore integration time:
-
Permalink:
aaaimx/CBR-FoX@edc5508314bd8b4833497fdcfcac7c046a94db91 -
Branch / Tag:
refs/tags/v.1.0.8 - Owner: https://github.com/aaaimx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@edc5508314bd8b4833497fdcfcac7c046a94db91 -
Trigger Event:
push
-
Statement type:
File details
Details for the file cbr_fox-1.0.8-py3-none-any.whl.
File metadata
- Download URL: cbr_fox-1.0.8-py3-none-any.whl
- Upload date:
- Size: 44.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5c3bbb5c92f199b1cc35ae555e704f258cf0ab2ae33aea1123903a2c56002b7
|
|
| MD5 |
fbbe5a190c80d675587e77fa3913b4ec
|
|
| BLAKE2b-256 |
43b0c506ba2e23a9ad4ff5e6e08ccf3be37dc1824a2aaa43c6b30bad4632b098
|
Provenance
The following attestation bundles were made for cbr_fox-1.0.8-py3-none-any.whl:
Publisher:
publish.yml on aaaimx/CBR-FoX
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cbr_fox-1.0.8-py3-none-any.whl -
Subject digest:
f5c3bbb5c92f199b1cc35ae555e704f258cf0ab2ae33aea1123903a2c56002b7 - Sigstore transparency entry: 621116618
- Sigstore integration time:
-
Permalink:
aaaimx/CBR-FoX@edc5508314bd8b4833497fdcfcac7c046a94db91 -
Branch / Tag:
refs/tags/v.1.0.8 - Owner: https://github.com/aaaimx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@edc5508314bd8b4833497fdcfcac7c046a94db91 -
Trigger Event:
push
-
Statement type: