DSatishchandra's picture
Update services/anomaly_service.py
69a3b59 verified
raw
history blame contribute delete
935 Bytes
import numpy as np # Import NumPy for statistical calculations
# Function to track fault counts and calculate statistics
def track_faults(fault_counts):
if not fault_counts:
return {"mean": 0, "std": 0} # Return default values if no data
mean = np.mean(fault_counts) # Calculate mean of fault counts
std = np.std(fault_counts) # Calculate standard deviation
return {"mean": mean, "std": std}
# Function to predict potential fault escalation
def predict_fault(fault_counts, threshold=2.5):
if len(fault_counts) < 5: # Require at least 5 frames for prediction
return False
stats = track_faults(fault_counts) # Get statistics
recent_counts = fault_counts[-5:] # Look at last 5 frames
for count in recent_counts:
if stats["std"] > 0 and (count - stats["mean"]) / stats["std"] > threshold: # Check for spike
return True
return False # No escalation detected