Hannah commited on
Commit
ff310de
·
1 Parent(s): 33ecdbc

fix thresholds

Browse files
Files changed (1) hide show
  1. app.py +68 -35
app.py CHANGED
@@ -39,53 +39,86 @@ def get_device_data(device):
39
 
40
  return df[device_cols]
41
 
42
- def get_color_for_value(val, min_val, max_val):
43
- """Return a color based on where the value falls in the range"""
44
- if min_val == max_val:
45
- return "" # No color if all values are the same
 
 
46
 
47
- normalized = (val - min_val) / (max_val - min_val)
48
-
49
- if normalized < 0.33:
50
- return "" # Low values are blank
51
- elif normalized < 0.66:
52
- # Medium values are green
53
- intensity = int(255 * ((normalized - 0.33) / 0.33))
54
- return f"background-color: rgba(0, {intensity}, 0, 0.3)"
55
- else:
56
- # High values are red
57
- intensity = int(255 * ((normalized - 0.66) / 0.34))
58
- return f"background-color: rgba({intensity}, 0, 0, 0.3)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  def prepare_data_with_styling(df):
61
  data = df.values.tolist()
62
  headers = list(df.columns)
63
 
64
- # Get min/max for each numeric column
65
- col_ranges = {}
66
- for i, col in enumerate(headers):
67
- if col not in ['Day', 'Date', 'Day of Week', 'Notes']:
68
- try:
69
- values = [float(row[i]) for row in data if row[i] != "" and pd.notna(row[i])]
70
- if values:
71
- col_ranges[i] = {'min': min(values), 'max': max(values)}
72
- except (ValueError, TypeError):
73
- continue
74
-
75
  # Create styling
76
  styling = []
77
  for row in data:
78
  row_styling = []
79
  for i, val in enumerate(row):
80
- if i in col_ranges and val != "" and pd.notna(val):
81
- try:
82
- val_float = float(val)
83
- style = get_color_for_value(val_float, col_ranges[i]['min'], col_ranges[i]['max'])
84
- row_styling.append(["", f"width: var(--cell-width-3); left: auto; {style}"])
85
- except (ValueError, TypeError):
86
- row_styling.append(["", ""])
87
- else:
88
  row_styling.append(["", ""])
 
 
 
89
  styling.append(row_styling)
90
 
91
  return {
 
39
 
40
  return df[device_cols]
41
 
42
+ def get_color_for_metric(val, metric_name):
43
+ """Return a color based on the metric type and value"""
44
+ try:
45
+ val = float(val)
46
+ except (ValueError, TypeError):
47
+ return ""
48
 
49
+ # Define thresholds for different metrics
50
+ if "BPM" in metric_name:
51
+ # Lower is better for BPM
52
+ if val <= 50:
53
+ return "background-color: rgba(0, 180, 0, 0.3)" # Green
54
+ elif val >= 54:
55
+ return "background-color: rgba(180, 0, 0, 0.3)" # Red
56
+ else:
57
+ return "background-color: rgba(180, 180, 180, 0.1)" # Light gray for middle range
58
+
59
+ elif "Score" in metric_name:
60
+ # Higher is better for Score
61
+ if val >= 85:
62
+ return "background-color: rgba(0, 180, 0, 0.3)" # Dark green
63
+ elif val >= 75:
64
+ return "background-color: rgba(0, 140, 0, 0.2)" # Medium green
65
+ elif val <= 70:
66
+ return "background-color: rgba(180, 0, 0, 0.3)" # Red
67
+ else:
68
+ return "background-color: rgba(180, 180, 180, 0.1)" # Light gray
69
+
70
+ elif "HRV" in metric_name:
71
+ # Higher is better for HRV
72
+ if val >= 80:
73
+ return "background-color: rgba(0, 180, 0, 0.3)" # Dark green
74
+ elif val <= 55:
75
+ return "background-color: rgba(180, 0, 0, 0.3)" # Red
76
+ else:
77
+ return "background-color: rgba(180, 180, 180, 0.1)" # Light gray
78
+
79
+ elif "Deep" in metric_name:
80
+ # Higher might be better for Deep sleep
81
+ if val >= 1.0:
82
+ return "background-color: rgba(0, 180, 0, 0.3)" # Green
83
+ elif val <= 0.2:
84
+ return "background-color: rgba(180, 0, 0, 0.3)" # Red
85
+ else:
86
+ return "background-color: rgba(180, 180, 180, 0.1)" # Light gray
87
+
88
+ elif "REM" in metric_name:
89
+ # Higher might be better for REM
90
+ if val >= 1.5:
91
+ return "background-color: rgba(0, 180, 0, 0.3)" # Green
92
+ elif val <= 0.8:
93
+ return "background-color: rgba(180, 0, 0, 0.3)" # Red
94
+ else:
95
+ return "background-color: rgba(180, 180, 180, 0.1)" # Light gray
96
+
97
+ elif "Light" in metric_name:
98
+ # Medium values might be ideal for Light sleep
99
+ if 5.0 <= val <= 6.0:
100
+ return "background-color: rgba(0, 180, 0, 0.3)" # Green
101
+ elif val >= 6.3 or val <= 4.5:
102
+ return "background-color: rgba(180, 0, 0, 0.3)" # Red
103
+ else:
104
+ return "background-color: rgba(180, 180, 180, 0.1)" # Light gray
105
+
106
+ return "" # Default no color
107
 
108
  def prepare_data_with_styling(df):
109
  data = df.values.tolist()
110
  headers = list(df.columns)
111
 
 
 
 
 
 
 
 
 
 
 
 
112
  # Create styling
113
  styling = []
114
  for row in data:
115
  row_styling = []
116
  for i, val in enumerate(row):
117
+ if headers[i] in ['Day', 'Date', 'Day of Week', 'Notes'] or val == "":
 
 
 
 
 
 
 
118
  row_styling.append(["", ""])
119
+ else:
120
+ style = get_color_for_metric(val, headers[i])
121
+ row_styling.append(["", f"width: var(--cell-width-3); left: auto; {style}"])
122
  styling.append(row_styling)
123
 
124
  return {