saadmannan commited on
Commit
debdad5
·
1 Parent(s): 1eb3236

Fix: Improve anomaly visualization graph - Handle numpy arrays properly - Use index-based x-axis for clarity - Add proper y-axis ranges and labels - Enhance hover tooltips

Browse files
Files changed (1) hide show
  1. app.py +40 -10
app.py CHANGED
@@ -76,56 +76,86 @@ def run_diagnostic(vehicle_id, n_readings):
76
  def create_anomaly_visualization(anomaly_result):
77
  """Create visualization of anomaly detection results"""
78
  try:
 
 
79
  timestamps = anomaly_result.get('timestamps', [])
80
  predictions = anomaly_result.get('anomaly_predictions', [])
81
  scores = anomaly_result.get('anomaly_scores', [])
82
 
83
- if len(timestamps) == 0:
 
 
 
 
 
 
 
 
84
  return None
85
 
 
 
 
86
  # Create figure with secondary y-axis
87
  fig = go.Figure()
88
 
89
- # Add anomaly predictions
90
  fig.add_trace(go.Scatter(
91
- x=timestamps,
92
  y=predictions,
93
  mode='lines',
94
  name='Anomaly Detected',
95
  line=dict(color='red', width=2),
96
  fill='tozeroy',
97
- fillcolor='rgba(255, 0, 0, 0.2)'
 
98
  ))
99
 
100
  # Add anomaly scores
101
  fig.add_trace(go.Scatter(
102
- x=timestamps,
103
  y=scores,
104
  mode='lines',
105
  name='Anomaly Score',
106
- line=dict(color='orange', width=1, dash='dot'),
107
- yaxis='y2'
 
108
  ))
109
 
110
  # Update layout
111
  fig.update_layout(
112
  title='Anomaly Detection Over Time',
113
- xaxis_title='Timestamp',
114
  yaxis_title='Anomaly Detected (0/1)',
 
 
 
 
 
115
  yaxis2=dict(
116
  title='Anomaly Score',
117
  overlaying='y',
118
- side='right'
 
119
  ),
120
  hovermode='x unified',
121
  template='plotly_white',
122
- height=400
 
 
 
 
 
 
 
123
  )
124
 
125
  return fig
126
 
127
  except Exception as e:
128
  print(f"Visualization error: {e}")
 
 
129
  return None
130
 
131
 
 
76
  def create_anomaly_visualization(anomaly_result):
77
  """Create visualization of anomaly detection results"""
78
  try:
79
+ import numpy as np
80
+
81
  timestamps = anomaly_result.get('timestamps', [])
82
  predictions = anomaly_result.get('anomaly_predictions', [])
83
  scores = anomaly_result.get('anomaly_scores', [])
84
 
85
+ # Convert numpy arrays to lists if needed
86
+ if isinstance(timestamps, np.ndarray):
87
+ timestamps = timestamps.tolist()
88
+ if isinstance(predictions, np.ndarray):
89
+ predictions = predictions.tolist()
90
+ if isinstance(scores, np.ndarray):
91
+ scores = scores.tolist()
92
+
93
+ if len(timestamps) == 0 or len(predictions) == 0:
94
  return None
95
 
96
+ # Create index-based x-axis for better visualization
97
+ x_values = list(range(len(predictions)))
98
+
99
  # Create figure with secondary y-axis
100
  fig = go.Figure()
101
 
102
+ # Add anomaly predictions as filled area
103
  fig.add_trace(go.Scatter(
104
+ x=x_values,
105
  y=predictions,
106
  mode='lines',
107
  name='Anomaly Detected',
108
  line=dict(color='red', width=2),
109
  fill='tozeroy',
110
+ fillcolor='rgba(255, 0, 0, 0.3)',
111
+ hovertemplate='Reading: %{x}<br>Anomaly: %{y}<extra></extra>'
112
  ))
113
 
114
  # Add anomaly scores
115
  fig.add_trace(go.Scatter(
116
+ x=x_values,
117
  y=scores,
118
  mode='lines',
119
  name='Anomaly Score',
120
+ line=dict(color='orange', width=2, dash='dot'),
121
+ yaxis='y2',
122
+ hovertemplate='Reading: %{x}<br>Score: %{y:.3f}<extra></extra>'
123
  ))
124
 
125
  # Update layout
126
  fig.update_layout(
127
  title='Anomaly Detection Over Time',
128
+ xaxis_title='Reading Index',
129
  yaxis_title='Anomaly Detected (0/1)',
130
+ yaxis=dict(
131
+ range=[-0.1, 1.1],
132
+ tickvals=[0, 1],
133
+ ticktext=['Normal', 'Anomaly']
134
+ ),
135
  yaxis2=dict(
136
  title='Anomaly Score',
137
  overlaying='y',
138
+ side='right',
139
+ range=[0, 1]
140
  ),
141
  hovermode='x unified',
142
  template='plotly_white',
143
+ height=450,
144
+ showlegend=True,
145
+ legend=dict(
146
+ yanchor="top",
147
+ y=0.99,
148
+ xanchor="left",
149
+ x=0.01
150
+ )
151
  )
152
 
153
  return fig
154
 
155
  except Exception as e:
156
  print(f"Visualization error: {e}")
157
+ import traceback
158
+ traceback.print_exc()
159
  return None
160
 
161