-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisuals.py
More file actions
90 lines (71 loc) · 3.19 KB
/
visuals.py
File metadata and controls
90 lines (71 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import matplotlib.pyplot as plt
import numpy as np
from analytics import moving_average, fft_analysis
import matplotlib.dates as mdates
def plot_health_trends(df):
"""Plot glucose and heart rate trends with smoothing + anomaly markers and formatted date axis."""
fig, ax = plt.subplots(figsize=(8, 4))
# Smoothed series
smooth_glucose = moving_average(df["Glucose"])
smooth_hr = moving_average(df["Heart Rate"])
# --- Glucose ---
ax.plot(df.index, df["Glucose"], label="Glucose (raw)", alpha=0.3)
ax.plot(df.index, smooth_glucose, label="Glucose (smoothed)", linewidth=2, color="tab:blue")
# Highlight anomalies
g_high = df["Glucose"] > 180
g_low = df["Glucose"] < 70
ax.scatter(df.index[g_high], df["Glucose"][g_high], color="red", label="Glucose High", zorder=5)
ax.scatter(df.index[g_low], df["Glucose"][g_low], color="orange", label="Glucose Low", zorder=5)
# --- Heart Rate ---
ax.plot(df.index, df["Heart Rate"], label="Heart Rate (raw)", alpha=0.3)
ax.plot(df.index, smooth_hr, label="Heart Rate (smoothed)", linewidth=2, color="tab:red")
# Highlight anomalies
h_high = df["Heart Rate"] > 100
h_low = df["Heart Rate"] < 60
ax.scatter(df.index[h_high], df["Heart Rate"][h_high], color="purple", label="HR High", zorder=5)
ax.scatter(df.index[h_low], df["Heart Rate"][h_low], color="pink", label="HR Low", zorder=5)
# Titles and labels
ax.set_title("Health Trends with Anomalies Highlighted")
ax.set_xlabel("Date")
ax.set_ylabel("Levels")
ax.legend()
ax.grid(True)
# --- Fix date label formatting ---
ax.xaxis.set_major_locator(mdates.AutoDateLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %d"))
fig.autofmt_xdate(rotation=45, ha="right")
return fig
def plot_fft(df):
"""Plot frequency spectrum of glucose and heart rate."""
fig, ax = plt.subplots(figsize=(8, 4))
freqs_g, mag_g = fft_analysis(df["Glucose"])
freqs_h, mag_h = fft_analysis(df["Heart Rate"])
ax.plot(freqs_g, mag_g, label="Glucose Frequency Spectrum", color="tab:blue")
ax.plot(freqs_h, mag_h, label="Heart Rate Frequency Spectrum", color="tab:red")
ax.set_title("Frequency Analysis (FFT)")
ax.set_xlabel("Frequency (1/day)")
ax.set_ylabel("Signal Strength")
ax.legend()
ax.grid(True)
return fig
def generate_anomaly_summary(df):
"""
Generates a short textual summary of glucose and heart rate anomalies.
Returns a string.
"""
high_glucose = (df["Glucose"] > 180).sum()
low_glucose = (df["Glucose"] < 70).sum()
high_hr = (df["Heart Rate"] > 100).sum()
low_hr = (df["Heart Rate"] < 60).sum()
summary = []
if high_glucose > 0:
summary.append(f"⚠ {high_glucose} high glucose readings detected.")
if low_glucose > 0:
summary.append(f"⚠ {low_glucose} low glucose readings detected.")
if high_hr > 0:
summary.append(f"💓 {high_hr} elevated heart rate readings detected.")
if low_hr > 0:
summary.append(f"💤 {low_hr} low heart rate readings detected.")
if not summary:
summary.append("✅ No major anomalies detected — great job maintaining stability!")
return " ".join(summary)