A unified framework for detecting anomalies and crisis events across multiple domains including finance, seismology, gravitational waves, and neurophysiology.
Crisis Detector is a powerful Python library that identifies potential crisis events in time-series data using a combination of statistical analysis and machine learning techniques. The detector uses a sliding window approach with multiple detection methods:
- Statistical Thresholds: Z-score and moving average analysis
- Isolation Forest: Machine learning-based anomaly detection
- Volatility Analysis: Rate-of-change and trend detection
- Multi-domain Support: Works with finance, seismic, gravitational wave, and EEG data
- 🔍 Multi-method Detection: Combines statistical and ML approaches for robust crisis identification
- 📊 Comprehensive Visualization: Generates detailed plots showing signal analysis and detected crises
- 🌐 Multi-domain Support: Built-in data loaders for various scientific and financial domains
- ⚙️ Configurable: Easily adjust detection sensitivity and parameters
- 🧪 Well-tested: Comprehensive test suite with synthetic data validation
- 📈 Production-ready: CI/CD pipeline with automated testing and linting
pip install crisis-detectorgit clone https://github.com/or4k2l/Crisis-Detector.git
cd Crisis-Detector
pip install -e .Install domain-specific dependencies as needed:
# For financial data analysis
pip install crisis-detector[finance]
# For seismic data analysis
pip install crisis-detector[seismic]
# For gravitational wave analysis
pip install crisis-detector[gravitational]
# For neurophysiology analysis
pip install crisis-detector[neuro]
# For development (testing, linting)
pip install crisis-detector[dev]
# Install everything
pip install crisis-detector[all]- Python 3.9+
- numpy, pandas, scipy
- scikit-learn, matplotlib
- Finance: yfinance, statsmodels
- Seismic: obspy
- Gravitational Waves: gwpy
- Neurophysiology: mne
from crisis_detector import CrisisDetector
import numpy as np
import logging
# Optional: Enable logging to see processing details
logging.basicConfig(level=logging.INFO)
# Create a detector instance
detector = CrisisDetector(
window_size=50,
threshold=2.5,
min_crisis_duration=5
)
# Generate or load your time-series data
signal = np.random.randn(1000)
# Process the signal
results = detector.process_signal(signal)
# Visualize the results
detector.plot_analysis(results, title="My Analysis", save_path="output.png")
# Access metrics
print(f"Detected {results['metrics']['n_crisis_events']} crisis events")
print(f"Crisis ratio: {results['metrics']['crisis_ratio']:.2%}")from crisis_detector import CrisisDetector, load_finance_data
# Load S&P 500 data
data = load_finance_data(ticker="^GSPC", start_date="2020-01-01", end_date="2023-12-31")
# Initialize detector
detector = CrisisDetector(window_size=20, threshold=2.5)
# Analyze closing prices
results = detector.process_signal(data, column='Close')
# Generate visualization
detector.plot_analysis(results, title="S&P 500 Crisis Detection", save_path="sp500_analysis.png")The library includes built-in loaders for multiple domains:
from crisis_detector import (
load_finance_data, # Financial markets (yfinance)
load_seismic_data, # Seismology (obspy)
load_gravitational_wave_data, # Gravitational waves (gwpy)
load_eeg_data, # Neurophysiology (mne)
load_economic_data # Economic indicators (statsmodels)
)
# Load financial data
finance_data = load_finance_data(ticker="^GSPC")
# Load seismic data
seismic_data = load_seismic_data(network="IU", station="ANMO")
# Load gravitational wave data
gw_data = load_gravitational_wave_data(detector="H1")
# Load EEG data
eeg_data = load_eeg_data()A demo script is provided that demonstrates the detector on S&P 500 financial data:
python scripts/run_demo.pyThis will:
- Download recent S&P 500 data
- Run the crisis detector
- Print summary statistics
- Save a visualization to
plots/sp500_demo.png
The Crisis Detector generates comprehensive visualizations showing:
- Original Signal with Crisis Regions: Highlights detected crisis periods
- Crisis Score Timeline: Shows the anomaly score over time
- Z-Score Analysis: Statistical deviation from rolling mean
- Volatility: Rate of change and trend analysis
Below are example outputs from the detector on various datasets:
Note: The plots show crisis detection on real-world data from various domains. Crisis regions are highlighted in red, showing periods of significant anomalies.
The CrisisDetector class accepts several parameters for customization:
detector = CrisisDetector(
window_size=50, # Size of rolling window for statistics
threshold=2.5, # Z-score threshold for anomaly detection
min_crisis_duration=5, # Minimum consecutive anomalies for a crisis
use_isolation_forest=True, # Enable ML-based detection
contamination=0.1 # Expected proportion of outliers
)- window_size: Larger windows smooth out noise but may miss short crises (20-100 typical)
- threshold: Higher values reduce false positives but may miss subtle crises (2.0-3.0 typical)
- min_crisis_duration: Filters out brief spikes (3-10 typical)
- contamination: Adjust based on expected anomaly rate in your domain (0.01-0.2 typical)
Run the test suite to validate the installation:
# Run all tests
pytest tests/ -v
# Run specific test file
pytest tests/test_detector_basic.py -v
# Run advanced tests
pytest tests/test_detector_advanced.py -v
# Run with coverage
pytest tests/ --cov=crisis_detector --cov-report=htmlThe test suite includes:
- Basic tests: Core functionality validation
- Advanced tests: Edge cases, performance, and domain-specific scenarios
The project uses black for code formatting and flake8 for linting:
# Format code
black .
# Check formatting
black --check .
# Run linter
flake8 . --max-line-length=100- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run tests and linters
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Crisis-Detector/
├── crisis_detector.py # Main detector implementation
├── scripts/
│ └── run_demo.py # Demo script
├── tests/
│ └── test_detector_basic.py # Test suite
├── .github/
│ └── workflows/
│ └── ci.yml # CI/CD pipeline
├── requirements.txt # Dependencies
├── README.md # Documentation
├── LICENSE # MIT License
└── .gitignore # Git ignore rules
- Market crash detection
- Volatility spike identification
- Trading anomaly detection
- Earthquake detection
- Aftershock identification
- Seismic event classification
- Binary merger detection
- Glitch identification
- Signal quality monitoring
- Seizure detection
- Brain activity anomalies
- Sleep stage transitions
Methods:
process_signal(data, timestamps=None, column=None): Process time-series data and detect crisesplot_analysis(results, title, save_path=None): Visualize detection results_identify_crisis_regions(anomalies): Internal method to identify continuous crisis regions_calculate_metrics(signal, crisis_score, crisis_regions, anomalies): Calculate summary statistics
Returns:
The process_signal method returns a dictionary containing:
signal: Processed signal valuestimestamps: Time indicescrisis_score: Anomaly scores (0-1 scale)crisis_regions: Boolean mask of crisis regionsvolatility: Rolling volatility measurez_scores: Statistical z-scoresanomalies: Boolean mask of detected anomaliesmetrics: Dictionary of summary statistics
- For large datasets (>100K points), consider downsampling or processing in chunks
- Isolation Forest is disabled for signals with <100 points
- Adjust
window_sizebased on your data frequency and expected crisis duration - Use
contaminationparameter to tune sensitivity for your specific domain
This project is licensed under the MIT License - see the LICENSE file for details.
- Uses scikit-learn for machine learning components
- Integrates with domain-specific libraries (yfinance, obspy, gwpy, mne)
- Inspired by research in multi-domain anomaly detection
If you use this software in your research, please cite:
@software{crisis_detector,
title={Crisis Detector: A Multi-Domain Anomaly Detection Framework},
author={or4k2l},
year={2026},
url={https://github.com/or4k2l/Crisis-Detector}
}- GitHub: @or4k2l
- Issues: GitHub Issues
- Add support for multivariate time-series
- Implement additional ML models (LSTM, Transformer)
- Add real-time streaming detection
- Create interactive web dashboard
- Add more domain-specific loaders
- Improve documentation and tutorials
Made with ❤️ for data scientists, researchers, and engineers working with time-series data.


