DSatishchandra commited on
Commit
cd8f20d
·
verified ·
1 Parent(s): bcf0698

Update modules/visuals.py

Browse files
Files changed (1) hide show
  1. modules/visuals.py +12 -7
modules/visuals.py CHANGED
@@ -19,20 +19,25 @@ def display_charts(df):
19
  def display_heatmap(df):
20
  # Map AlertLevel to numeric values for heatmap intensity
21
  alert_map = {"Green": 0, "Yellow": 1, "Red": 2}
 
22
  df["AlertValue"] = df["AlertLevel"].map(alert_map)
23
 
24
- # Pivot table: Locations as rows, Poles as columns
25
  pivot_df = df.pivot_table(index="Location", columns="PoleID", values="AlertValue", fill_value=0)
26
 
27
- # Create hover text with PoleID, AlertLevel, and Anomalies
28
- hover_df = df.pivot_table(index="Location", columns="PoleID", values=["PoleID", "AlertLevel", "Anomalies"], aggfunc="first")
29
- hover_text = pivot_df.copy()
30
  for loc in pivot_df.index:
31
  for pole in pivot_df.columns:
32
- if pole in hover_df.loc[loc, "PoleID"].columns:
33
- alert = hover_df.loc[loc, ("AlertLevel", pole)] if pd.notna(hover_df.loc[loc, ("AlertLevel", pole)]) else "None"
34
- anomalies = hover_df.loc[loc, ("Anomalies", pole)] if pd.notna(hover_df.loc[loc, ("Anomalies", pole)]) else "None"
 
 
35
  hover_text.loc[loc, pole] = f"Pole: {pole}<br>Alert: {alert}<br>Anomalies: {anomalies}"
 
 
36
 
37
  # Create heatmap using Plotly
38
  fig = px.imshow(
 
19
  def display_heatmap(df):
20
  # Map AlertLevel to numeric values for heatmap intensity
21
  alert_map = {"Green": 0, "Yellow": 1, "Red": 2}
22
+ df = df.copy() # Avoid modifying the original DataFrame
23
  df["AlertValue"] = df["AlertLevel"].map(alert_map)
24
 
25
+ # Pivot table for heatmap values (AlertValue)
26
  pivot_df = df.pivot_table(index="Location", columns="PoleID", values="AlertValue", fill_value=0)
27
 
28
+ # Create hover text DataFrame
29
+ hover_data = df[["Location", "PoleID", "AlertLevel", "Anomalies"]].copy()
30
+ hover_text = pivot_df.copy().astype(str)
31
  for loc in pivot_df.index:
32
  for pole in pivot_df.columns:
33
+ # Find matching row in hover_data
34
+ match = hover_data[(hover_data["Location"] == loc) & (hover_data["PoleID"] == pole)]
35
+ if not match.empty:
36
+ alert = match["AlertLevel"].iloc[0]
37
+ anomalies = match["Anomalies"].iloc[0]
38
  hover_text.loc[loc, pole] = f"Pole: {pole}<br>Alert: {alert}<br>Anomalies: {anomalies}"
39
+ else:
40
+ hover_text.loc[loc, pole] = f"Pole: {pole}<br>Alert: None<br>Anomalies: None"
41
 
42
  # Create heatmap using Plotly
43
  fig = px.imshow(