| Crates.io | libspot-rs |
| lib.rs | libspot-rs |
| version | 0.1.0 |
| created_at | 2025-08-18 05:19:26.896057+00 |
| updated_at | 2025-08-18 05:19:26.896057+00 |
| description | Pure Rust implementation of the SPOT algorithm for time series anomaly detection |
| homepage | |
| repository | https://github.com/shenxiangzhuang/libspot-rs |
| max_upload_size | |
| id | 1799982 |
| size | 87,320 |
A pure Rust implementation of the SPOT (Streaming Peaks Over Threshold) algorithm for real-time anomaly detection in time series data.
cargo add libspot-rs
use libspot_rs::{SpotDetector, SpotConfig, SpotStatus};
# fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create detector with default configuration
let config = SpotConfig::default();
let mut detector = SpotDetector::new(config)?;
// Fit with training data (normal distribution around 5.0)
let training_data: Vec<f64> = (0..1000)
.map(|i| 5.0 + (i as f64 * 0.01).sin() * 2.0)
.collect();
detector.fit(&training_data)?;
// Detect anomalies in real-time
let test_value = 50.0; // This should be an anomaly
match detector.step(test_value)? {
SpotStatus::Normal => println!("Normal data point"),
SpotStatus::Excess => println!("In the tail distribution"),
SpotStatus::Anomaly => println!("Anomaly detected! 🚨"),
}
# Ok(())
# }
The SPOT algorithm can be configured with various parameters:
use libspot_rs::SpotConfig;
let config = SpotConfig {
q: 0.0001, // Anomaly probability threshold
low_tail: false, // Monitor upper tail (set true for lower tail)
discard_anomalies: true, // Exclude anomalies from model updates
level: 0.998, // Quantile level that defines the tail
max_excess: 200, // Maximum number of excess values to store
};
use libspot_rs::{SpotDetector, SpotConfig};
# fn main() -> Result<(), Box<dyn std::error::Error>> {
// More sensitive detector (lower anomaly threshold)
let sensitive_config = SpotConfig {
q: 0.01, // Higher probability = more sensitive
level: 0.95, // Lower level = larger tail region
..SpotConfig::default()
};
let mut detector = SpotDetector::new(sensitive_config)?;
# Ok(())
# }
use libspot_rs::{SpotDetector, SpotConfig};
# fn main() -> Result<(), Box<dyn std::error::Error>> {
# let cpu_history = vec![1.0, 2.0, 3.0];
# let memory_history = vec![1.0, 2.0, 3.0];
# let network_history = vec![1.0, 2.0, 3.0];
# fn get_cpu_usage() -> f64 { 1.0 }
# fn get_memory_usage() -> f64 { 1.0 }
# fn get_network_usage() -> f64 { 1.0 }
// Create separate detectors for different metrics
let mut cpu_detector = SpotDetector::new(SpotConfig::default())?;
let mut memory_detector = SpotDetector::new(SpotConfig::default())?;
let mut network_detector = SpotDetector::new(SpotConfig::default())?;
// Train each detector with historical data
cpu_detector.fit(&cpu_history)?;
memory_detector.fit(&memory_history)?;
network_detector.fit(&network_history)?;
// Monitor in real-time
for _ in 0..3 { // Limited loop for doctest
let cpu_status = cpu_detector.step(get_cpu_usage())?;
let memory_status = memory_detector.step(get_memory_usage())?;
let network_status = network_detector.step(get_network_usage())?;
// Handle anomalies...
break; // Exit early for doctest
}
# Ok(())
# }
# use libspot_rs::{SpotDetector, SpotConfig};
# fn main() -> Result<(), Box<dyn std::error::Error>> {
# let mut detector = SpotDetector::new(SpotConfig::default())?;
# let training_data = vec![1.0, 2.0, 3.0];
# detector.fit(&training_data)?;
// Get detector statistics
println!("Total samples: {}", detector.n());
println!("Excess count: {}", detector.nt());
println!("Anomaly threshold: {}", detector.anomaly_threshold());
println!("Excess threshold: {}", detector.excess_threshold());
// Get tail distribution parameters
let (gamma, sigma) = detector.tail_parameters();
println!("Tail shape: {}, scale: {}", gamma, sigma);
// Get peaks statistics
println!("Peaks mean: {}", detector.peaks_mean());
println!("Peaks variance: {}", detector.peaks_variance());
# Ok(())
# }
The SPOT algorithm is designed for online anomaly detection in time series data using:
Key concepts:
SpotDetector]: Main SPOT detector implementationSpotConfig]: Configuration parameters for the detectorSpotStatus]: Status returned by the detector for each data pointUbend]: Circular buffer for storing dataPeaks]: Statistics computation over peaks dataTail]: Generalized Pareto Distribution tail modelinglibspot-rs is optimized for real-time processing:
| Feature | libspot-rs (Pure Rust) | libspot (C + FFI) |
|---|---|---|
| Dependencies | None | C library, bindgen |
| Memory Safety | ✅ Guaranteed | ⚠️ Manual management |
| Performance | ✅ Excellent | ✅ Excellent |
| Cross-platform | ✅ Easy | ⚠️ Build complexity |
| WebAssembly | ✅ Full support | ❌ Limited |
See the examples/ directory for more comprehensive usage examples:
basic.rs: Basic usage with synthetic dataContributions are welcome! Please feel free to submit issues and pull requests.
This project is licensed under the GNU Lesser General Public License v3.0 - see the LICENSE file for details.