Create anomaly_service.py
Browse files- services/anomaly_service.py +21 -0
services/anomaly_service.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
def track_anomalies(anomaly_counts):
|
| 4 |
+
"""Track anomaly counts and return statistics."""
|
| 5 |
+
if not anomaly_counts:
|
| 6 |
+
return {"mean": 0, "std": 0}
|
| 7 |
+
mean = np.mean(anomaly_counts)
|
| 8 |
+
std = np.std(anomaly_counts)
|
| 9 |
+
return {"mean": mean, "std": std}
|
| 10 |
+
|
| 11 |
+
def predict_anomaly(anomaly_counts, threshold=2.5):
|
| 12 |
+
"""Predict potential issues based on anomaly spikes."""
|
| 13 |
+
if len(anomaly_counts) < 5: # Need enough data for prediction
|
| 14 |
+
return False
|
| 15 |
+
stats = track_anomalies(anomaly_counts)
|
| 16 |
+
recent_counts = anomaly_counts[-5:] # Last 5 frames
|
| 17 |
+
# Flag a potential issue if the recent anomaly count is significantly higher than the mean
|
| 18 |
+
for count in recent_counts:
|
| 19 |
+
if stats["std"] > 0 and (count - stats["mean"]) / stats["std"] > threshold:
|
| 20 |
+
return True
|
| 21 |
+
return False
|