-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_patterns.py
More file actions
307 lines (263 loc) · 13.2 KB
/
analyze_patterns.py
File metadata and controls
307 lines (263 loc) · 13.2 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python3
"""
Comprehensive pattern analysis for Eva's sleep and feeding data
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime, timedelta
import warnings
warnings.filterwarnings('ignore')
# Set style for better-looking plots
sns.set_style("whitegrid")
plt.rcParams['figure.figsize'] = (14, 8)
# Load the CSV data
print("Loading data...")
df = pd.read_csv('eva.csv')
# Data preprocessing
df['Date and Time'] = pd.to_datetime(df['Date and Time'], format='%m/%d/%y %I:%M %p')
df['End Time'] = pd.to_datetime(df['End Time'], format='%m/%d/%y %I:%M %p', errors='coerce')
# Extract temporal features
df['Hour'] = df['Date and Time'].dt.hour
df['DayOfWeek'] = df['Date and Time'].dt.dayofweek
df['DayName'] = df['Date and Time'].dt.day_name()
df['Date'] = df['Date and Time'].dt.date
df['Week'] = df['Date and Time'].dt.isocalendar().week
df['Month'] = df['Date and Time'].dt.month
df['Age_Days'] = (df['Date and Time'].max() - df['Date and Time']).dt.days
print(f"Data loaded successfully!")
print(f"Total records: {len(df)}")
print(f"Date range: {df['Date and Time'].min()} to {df['Date and Time'].max()}")
print(f"Duration: {(df['Date and Time'].max() - df['Date and Time'].min()).days} days")
print("\n" + "="*80 + "\n")
# ============================================================================
# INSIGHT 1: Activity Distribution
# ============================================================================
print("INSIGHT 1: ACTIVITY DISTRIBUTION")
print("-" * 80)
activity_counts = df['Activity'].value_counts()
print("\nActivity breakdown:")
for activity, count in activity_counts.items():
percentage = (count / len(df)) * 100
print(f" {activity}: {count} occurrences ({percentage:.1f}%)")
# ============================================================================
# INSIGHT 2: Sleep Pattern Analysis
# ============================================================================
print("\n" + "="*80 + "\n")
print("INSIGHT 2: SLEEP PATTERNS")
print("-" * 80)
sleep_df = df[df['Activity'] == 'Sleep'].copy()
sleep_df = sleep_df[sleep_df['Duration (min)'].notna()]
print(f"\nTotal sleep sessions: {len(sleep_df)}")
print(f"Average sleep duration: {sleep_df['Duration (min)'].mean():.1f} minutes ({sleep_df['Duration (min)'].mean()/60:.1f} hours)")
print(f"Median sleep duration: {sleep_df['Duration (min)'].median():.1f} minutes")
print(f"Longest sleep: {sleep_df['Duration (min)'].max():.1f} minutes ({sleep_df['Duration (min)'].max()/60:.1f} hours)")
print(f"Shortest sleep: {sleep_df['Duration (min)'].min():.1f} minutes")
# Night sleep vs day naps (night = 7pm to 7am)
sleep_df['IsNightSleep'] = sleep_df['Hour'].apply(lambda x: x >= 19 or x < 7)
night_sleep = sleep_df[sleep_df['IsNightSleep']]
day_sleep = sleep_df[~sleep_df['IsNightSleep']]
print(f"\nNight sleep sessions: {len(night_sleep)}")
print(f" Average duration: {night_sleep['Duration (min)'].mean():.1f} minutes ({night_sleep['Duration (min)'].mean()/60:.1f} hours)")
print(f"\nDay nap sessions: {len(day_sleep)}")
print(f" Average duration: {day_sleep['Duration (min)'].mean():.1f} minutes ({day_sleep['Duration (min)'].mean()/60:.1f} hours)")
# Total sleep per day
daily_sleep = sleep_df.groupby('Date')['Duration (min)'].sum() / 60 # in hours
print(f"\nAverage total sleep per day: {daily_sleep.mean():.1f} hours")
print(f"Min total sleep in a day: {daily_sleep.min():.1f} hours")
print(f"Max total sleep in a day: {daily_sleep.max():.1f} hours")
# ============================================================================
# INSIGHT 3: Feeding Pattern Analysis
# ============================================================================
print("\n" + "="*80 + "\n")
print("INSIGHT 3: FEEDING PATTERNS")
print("-" * 80)
feeding_df = df[df['Activity'].isin(['Bottle', 'Breastfeeding', 'Solid food'])].copy()
feeding_df = feeding_df[feeding_df['Quantity'].notna()]
print(f"\nTotal feeding sessions: {len(feeding_df)}")
print(f"\nFeeding types:")
for activity in feeding_df['Activity'].unique():
count = len(feeding_df[feeding_df['Activity'] == activity])
print(f" {activity}: {count} sessions")
# Bottle feeding analysis
bottle_df = feeding_df[feeding_df['Activity'] == 'Bottle']
if len(bottle_df) > 0:
print(f"\nBottle feeding details:")
print(f" Average amount: {bottle_df['Quantity'].mean():.1f} oz")
print(f" Total consumed: {bottle_df['Quantity'].sum():.1f} oz")
print(f" Range: {bottle_df['Quantity'].min():.1f} - {bottle_df['Quantity'].max():.1f} oz")
# Daily bottle consumption
daily_bottle = bottle_df.groupby('Date')['Quantity'].sum()
print(f" Average daily consumption: {daily_bottle.mean():.1f} oz")
print(f" Daily range: {daily_bottle.min():.1f} - {daily_bottle.max():.1f} oz")
# Feeding frequency
daily_feeding = feeding_df.groupby('Date').size()
print(f"\nAverage feedings per day: {daily_feeding.mean():.1f}")
print(f"Range: {daily_feeding.min()} - {daily_feeding.max()} feedings per day")
# ============================================================================
# INSIGHT 4: Temporal Patterns
# ============================================================================
print("\n" + "="*80 + "\n")
print("INSIGHT 4: TEMPORAL PATTERNS")
print("-" * 80)
# Most common hours for activities
print("\nMost common hours for sleep:")
sleep_hours = sleep_df['Hour'].value_counts().head(5)
for hour, count in sleep_hours.items():
print(f" {hour:02d}:00 - {count} times")
print("\nMost common hours for feeding:")
feeding_hours = feeding_df['Hour'].value_counts().head(5)
for hour, count in feeding_hours.items():
print(f" {hour:02d}:00 - {count} times")
# ============================================================================
# INSIGHT 5: Trends Over Time
# ============================================================================
print("\n" + "="*80 + "\n")
print("INSIGHT 5: TRENDS OVER TIME")
print("-" * 80)
# Sleep duration trend
sleep_by_week = sleep_df.groupby(sleep_df['Date and Time'].dt.to_period('W'))['Duration (min)'].agg(['mean', 'count'])
print("\nSleep duration trend (first 10 weeks):")
print(sleep_by_week.head(10))
# Check if sleep is getting longer over time
if len(sleep_by_week) > 1:
first_weeks_avg = sleep_by_week['mean'].head(4).mean()
last_weeks_avg = sleep_by_week['mean'].tail(4).mean()
change = ((last_weeks_avg - first_weeks_avg) / first_weeks_avg) * 100
print(f"\nSleep duration change from start to end: {change:+.1f}%")
print(f" First month average: {first_weeks_avg:.1f} minutes")
print(f" Last month average: {last_weeks_avg:.1f} minutes")
# ============================================================================
# INSIGHT 6: Caregiver Patterns
# ============================================================================
print("\n" + "="*80 + "\n")
print("INSIGHT 6: CAREGIVER PATTERNS")
print("-" * 80)
caregiver_stats = df.groupby('Caregiver')['Activity'].value_counts()
print("\nActivities by caregiver:")
print(caregiver_stats)
# ============================================================================
# INSIGHT 7: Interesting Patterns
# ============================================================================
print("\n" + "="*80 + "\n")
print("INSIGHT 7: INTERESTING PATTERNS")
print("-" * 80)
# Sleep sessions at night that are over 10 hours
long_night_sleep = night_sleep[night_sleep['Duration (min)'] > 600]
print(f"\nNights with >10 hour sleep: {len(long_night_sleep)}")
if len(long_night_sleep) > 0:
print("Sample dates:")
for date in long_night_sleep['Date'].values[:10]:
print(f" {date}")
# ============================================================================
# SUMMARY
# ============================================================================
print("\n" + "="*80 + "\n")
print("SUMMARY: KEY FINDINGS")
print("-" * 80)
print("\n1. SLEEP CONSOLIDATION:")
if night_sleep['Duration (min)'].mean() > day_sleep['Duration (min)'].mean() * 2:
print(" ✓ Night sleep is well-consolidated (much longer than day naps)")
else:
print(" • Sleep is still fragmenting between day and night")
print("\n2. FEEDING CONSISTENCY:")
if daily_feeding.std() < 2:
print(f" ✓ Very consistent feeding schedule ({daily_feeding.mean():.1f} ± {daily_feeding.std():.1f} feeds/day)")
else:
print(f" • Variable feeding schedule ({daily_feeding.mean():.1f} ± {daily_feeding.std():.1f} feeds/day)")
print("\n3. TOTAL SLEEP:")
expected_sleep = 14 # hours for ~6-12 month old
actual_sleep = daily_sleep.mean()
if abs(actual_sleep - expected_sleep) < 2:
print(f" ✓ Sleep duration is within expected range ({actual_sleep:.1f}h vs ~{expected_sleep}h expected)")
else:
print(f" • Sleep duration is {actual_sleep:.1f}h (expected ~{expected_sleep}h for this age)")
print("\n" + "="*80)
print("\nCreating visualizations...")
# ============================================================================
# VISUALIZATIONS
# ============================================================================
# Figure 1: Activity distribution
plt.figure(figsize=(12, 6))
activity_counts.plot(kind='bar', color='skyblue')
plt.title('Distribution of Activities', fontsize=16, fontweight='bold')
plt.xlabel('Activity Type', fontsize=12)
plt.ylabel('Count', fontsize=12)
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.savefig('analysis_activity_distribution.png', dpi=300, bbox_inches='tight')
print("✓ Saved: analysis_activity_distribution.png")
# Figure 2: Sleep duration over time
plt.figure(figsize=(14, 6))
plt.scatter(sleep_df['Date and Time'], sleep_df['Duration (min)'], alpha=0.5, s=50, c=sleep_df['IsNightSleep'].map({True: 'darkblue', False: 'lightblue'}))
plt.plot(sleep_df.groupby('Date')['Duration (min)'].mean(), color='red', linewidth=2, label='Daily Average')
plt.title('Sleep Duration Over Time', fontsize=16, fontweight='bold')
plt.xlabel('Date', fontsize=12)
plt.ylabel('Duration (minutes)', fontsize=12)
plt.axhline(y=night_sleep['Duration (min)'].mean(), color='darkblue', linestyle='--', alpha=0.5, label='Night Sleep Avg')
plt.axhline(y=day_sleep['Duration (min)'].mean(), color='lightblue', linestyle='--', alpha=0.5, label='Day Nap Avg')
plt.legend()
plt.tight_layout()
plt.savefig('analysis_sleep_duration_timeline.png', dpi=300, bbox_inches='tight')
print("✓ Saved: analysis_sleep_duration_timeline.png")
# Figure 3: Hourly activity heatmap
plt.figure(figsize=(14, 6))
hourly_activity = df.groupby(['Hour', 'Activity']).size().unstack(fill_value=0)
sns.heatmap(hourly_activity.T, cmap='YlOrRd', annot=True, fmt='g', cbar_kws={'label': 'Count'})
plt.title('Activity Patterns by Hour of Day', fontsize=16, fontweight='bold')
plt.xlabel('Hour of Day', fontsize=12)
plt.ylabel('Activity', fontsize=12)
plt.tight_layout()
plt.savefig('analysis_hourly_heatmap.png', dpi=300, bbox_inches='tight')
print("✓ Saved: analysis_hourly_heatmap.png")
# Figure 4: Daily total sleep trend
plt.figure(figsize=(14, 6))
daily_sleep.plot(marker='o', linewidth=2, markersize=6, color='steelblue')
plt.axhline(y=daily_sleep.mean(), color='red', linestyle='--', label=f'Average: {daily_sleep.mean():.1f}h')
plt.fill_between(daily_sleep.index, daily_sleep.mean() - daily_sleep.std(),
daily_sleep.mean() + daily_sleep.std(), alpha=0.2, color='red')
plt.title('Total Sleep per Day', fontsize=16, fontweight='bold')
plt.xlabel('Date', fontsize=12)
plt.ylabel('Hours', fontsize=12)
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('analysis_daily_total_sleep.png', dpi=300, bbox_inches='tight')
print("✓ Saved: analysis_daily_total_sleep.png")
# Figure 5: Feeding patterns
if len(bottle_df) > 0:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))
# Daily bottle consumption
daily_bottle.plot(kind='bar', ax=ax1, color='coral')
ax1.axhline(y=daily_bottle.mean(), color='darkred', linestyle='--', label=f'Average: {daily_bottle.mean():.1f}oz')
ax1.set_title('Daily Bottle Consumption', fontsize=14, fontweight='bold')
ax1.set_xlabel('Date', fontsize=12)
ax1.set_ylabel('Ounces', fontsize=12)
ax1.legend()
ax1.tick_params(axis='x', rotation=45)
# Bottle size distribution
ax2.hist(bottle_df['Quantity'], bins=20, color='coral', edgecolor='black')
ax2.axvline(bottle_df['Quantity'].mean(), color='darkred', linestyle='--', linewidth=2, label=f'Mean: {bottle_df["Quantity"].mean():.1f}oz')
ax2.set_title('Bottle Size Distribution', fontsize=14, fontweight='bold')
ax2.set_xlabel('Ounces', fontsize=12)
ax2.set_ylabel('Frequency', fontsize=12)
ax2.legend()
plt.tight_layout()
plt.savefig('analysis_feeding_patterns.png', dpi=300, bbox_inches='tight')
print("✓ Saved: analysis_feeding_patterns.png")
# Figure 6: Sleep probability by hour
plt.figure(figsize=(14, 6))
sleep_prob_by_hour = sleep_df.groupby('Hour').size() / df.groupby('Hour').size() * 100
sleep_prob_by_hour.plot(kind='bar', color='skyblue', edgecolor='darkblue')
plt.title('Sleep Probability by Hour of Day', fontsize=16, fontweight='bold')
plt.xlabel('Hour', fontsize=12)
plt.ylabel('Probability (%)', fontsize=12)
plt.axhline(y=50, color='red', linestyle='--', alpha=0.5, label='50%')
plt.legend()
plt.tight_layout()
plt.savefig('analysis_sleep_probability_hourly.png', dpi=300, bbox_inches='tight')
print("✓ Saved: analysis_sleep_probability_hourly.png")
print("\n" + "="*80)
print("ALL VISUALIZATIONS SAVED!")
print("="*80)