Spaces:
Runtime error
Runtime error
Update services/anomaly_service.py
Browse files- services/anomaly_service.py +11 -9
services/anomaly_service.py
CHANGED
|
@@ -1,18 +1,20 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
|
|
|
|
| 3 |
def track_faults(fault_counts):
|
| 4 |
if not fault_counts:
|
| 5 |
-
return {"mean": 0, "std": 0}
|
| 6 |
-
mean = np.mean(fault_counts)
|
| 7 |
-
std = np.std(fault_counts)
|
| 8 |
return {"mean": mean, "std": std}
|
| 9 |
|
|
|
|
| 10 |
def predict_fault(fault_counts, threshold=2.5):
|
| 11 |
-
if len(fault_counts) < 5:
|
| 12 |
return False
|
| 13 |
-
stats = track_faults(fault_counts)
|
| 14 |
-
recent_counts = fault_counts[-5:]
|
| 15 |
for count in recent_counts:
|
| 16 |
-
if stats["std"] > 0 and (count - stats["mean"]) / stats["std"] > threshold:
|
| 17 |
return True
|
| 18 |
-
return False
|
|
|
|
| 1 |
+
import numpy as np # Import NumPy for statistical calculations
|
| 2 |
|
| 3 |
+
# Function to track fault counts and calculate statistics
|
| 4 |
def track_faults(fault_counts):
|
| 5 |
if not fault_counts:
|
| 6 |
+
return {"mean": 0, "std": 0} # Return default values if no data
|
| 7 |
+
mean = np.mean(fault_counts) # Calculate mean of fault counts
|
| 8 |
+
std = np.std(fault_counts) # Calculate standard deviation
|
| 9 |
return {"mean": mean, "std": std}
|
| 10 |
|
| 11 |
+
# Function to predict potential fault escalation
|
| 12 |
def predict_fault(fault_counts, threshold=2.5):
|
| 13 |
+
if len(fault_counts) < 5: # Require at least 5 frames for prediction
|
| 14 |
return False
|
| 15 |
+
stats = track_faults(fault_counts) # Get statistics
|
| 16 |
+
recent_counts = fault_counts[-5:] # Look at last 5 frames
|
| 17 |
for count in recent_counts:
|
| 18 |
+
if stats["std"] > 0 and (count - stats["mean"]) / stats["std"] > threshold: # Check for spike
|
| 19 |
return True
|
| 20 |
+
return False # No escalation detected
|