-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizations.py
More file actions
165 lines (137 loc) · 4.4 KB
/
Copy pathvisualizations.py
File metadata and controls
165 lines (137 loc) · 4.4 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import plotly.graph_objects as go
import plotly.express as px
from datetime import datetime, timedelta
import pandas as pd
def create_mood_trend(mood_history):
if not mood_history:
return None
df = pd.DataFrame(mood_history)
df['created_at'] = pd.to_datetime(df['created_at'])
fig = go.Figure()
# Add mood score line
fig.add_trace(go.Scatter(
x=df['created_at'],
y=df['mood_score'],
mode='lines+markers',
name='Mood Score',
line=dict(color='#4A90E2', width=2),
marker=dict(size=8, symbol='circle')
))
# Add moving average
df['moving_avg'] = df['mood_score'].rolling(window=7).mean()
fig.add_trace(go.Scatter(
x=df['created_at'],
y=df['moving_avg'],
mode='lines',
name='7-Day Average',
line=dict(color='#FF9999', width=2, dash='dash')
))
# Customize layout
fig.update_layout(
title='Your Mood Trend',
xaxis_title='Date',
yaxis_title='Mood Score',
yaxis=dict(
tickmode='array',
ticktext=['Very Low', 'Low', 'Neutral', 'Good', 'Excellent'],
tickvals=[1, 2, 3, 4, 5],
range=[0.5, 5.5]
),
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
hovermode='x unified'
)
return fig
def create_mood_distribution(mood_history):
if not mood_history:
return None
df = pd.DataFrame(mood_history)
# Calculate mood distribution
mood_counts = df['mood_score'].value_counts().sort_index()
# Create pie chart
fig = go.Figure(data=[go.Pie(
labels=['Very Low', 'Low', 'Neutral', 'Good', 'Excellent'],
values=mood_counts,
hole=.3,
marker_colors=['#FF9999', '#FFB366', '#FFFF99', '#99FF99', '#99CCFF']
)])
fig.update_layout(
title='Mood Distribution',
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'
)
return fig
def create_weekly_summary(mood_history):
if not mood_history:
return None
df = pd.DataFrame(mood_history)
df['created_at'] = pd.to_datetime(df['created_at'])
df['week'] = df['created_at'].dt.strftime('%Y-%U')
weekly_stats = df.groupby('week').agg({
'mood_score': ['mean', 'min', 'max', 'count']
}).reset_index()
weekly_stats.columns = ['week', 'avg_mood', 'min_mood', 'max_mood', 'entries']
fig = go.Figure()
# Add range of moods
fig.add_trace(go.Bar(
name='Mood Range',
x=weekly_stats['week'],
y=weekly_stats['max_mood'] - weekly_stats['min_mood'],
base=weekly_stats['min_mood'],
marker_color='rgba(74, 144, 226, 0.3)',
hovertemplate='Week: %{x}<br>Range: %{base} - %{y}<extra></extra>'
))
# Add average line
fig.add_trace(go.Scatter(
name='Average Mood',
x=weekly_stats['week'],
y=weekly_stats['avg_mood'],
mode='lines+markers',
line=dict(color='#4A90E2', width=2),
marker=dict(size=8)
))
fig.update_layout(
title='Weekly Mood Summary',
xaxis_title='Week',
yaxis_title='Mood Score',
yaxis=dict(
tickmode='array',
ticktext=['Very Low', 'Low', 'Neutral', 'Good', 'Excellent'],
tickvals=[1, 2, 3, 4, 5],
range=[0.5, 5.5]
),
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
hovermode='x unified'
)
return fig
def analyze_mood_patterns(mood_history):
if not mood_history:
return None
df = pd.DataFrame(mood_history)
df['created_at'] = pd.to_datetime(df['created_at'])
df['day_of_week'] = df['created_at'].dt.day_name()
df['hour'] = df['created_at'].dt.hour
# Create heatmap data
day_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
heatmap_data = df.pivot_table(
values='mood_score',
index='day_of_week',
columns='hour',
aggfunc='mean'
).reindex(day_order)
fig = go.Figure(data=go.Heatmap(
z=heatmap_data.values,
x=heatmap_data.columns,
y=heatmap_data.index,
colorscale='RdYlBu',
hoverongaps=False
))
fig.update_layout(
title='Mood Patterns by Day and Time',
xaxis_title='Hour of Day',
yaxis_title='Day of Week',
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'
)
return fig