-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1582 lines (1257 loc) · 61.5 KB
/
main.py
File metadata and controls
1582 lines (1257 loc) · 61.5 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# %% [markdown]
# # Imports & Constants
# %%
import matplotlib.pyplot as plt
import os
import pandas as pd
import random
import numpy as np
import shutil
import time
import json
from time import sleep
from datetime import datetime
import re
import seaborn as sns
from loguru import logger
def extract_time_format_from_datetime(formatted_time):
if("T" in formatted_time):
formatted_time = formatted_time.split("+")[0]
formatted_time = formatted_time.replace("T", " ")
return "%Y-%m-%dT%H:%M:%S"
return "%Y-%m-%d %H:%M:%S.%f"
def convert_datetime_to_time_since_last_epoch(formatted_time):
time_format = extract_time_format_from_datetime(formatted_time)
time_struct = time.strptime(formatted_time, time_format)
datetime_obj = datetime.fromtimestamp(time.mktime(time_struct))
return datetime_obj.timestamp()
def find_most_recent_directory(dir):
"""
This will do this by seeing the most recent directory in the directory, looking at the directory date
"""
dirs = os.listdir(dir)
for i in range(len(dirs)):
if not os.path.isdir(dir + dirs[i]):
dirs.pop(i)
most_recent_dir = dirs[0]
for _dir in dirs:
if os.path.getmtime(dir + _dir) > os.path.getmtime(dir + most_recent_dir):
most_recent_dir = _dir
return dir + most_recent_dir + "/"
# Most recent directory
galaxy_watch_folder_dirs = os.listdir("./data/galaxywatch/")
galaxy_watch_folder_dirs.sort()
galaxy_watch_folder_dir = "./data/galaxywatch/"+galaxy_watch_folder_dirs[-1] + "/"
exercise_logs_folder_dir = "./data/exerciselogs/"
computer_usage_folder_dir = "./data/computerusage/"
calendar_events_folder_dir = find_most_recent_directory("./data/calendar/")
# No data filter
# data_start_time = 0
# First day of college
data_end_time = 9e50
data_start_time = 0
# 3 weeks out to a good time
data_start_time = round(convert_datetime_to_time_since_last_epoch("2023-8-5 00:00:00.0"))
data_end_time = round(convert_datetime_to_time_since_last_epoch("2024-5-17 0:0:00.0"))
# testing celander
# data_start_time = round(convert_datetime_to_time_since_last_epoch("2023-10-23 00:00:00.0"))
# data_end_time = round(convert_datetime_to_time_since_last_epoch("2023-10-30 00:00:00.0"))
if data_end_time >= 9e20:
data_end_time = time.time()
current_date = time.time()
# Create a common dataframe that holds information about stuff I do every day, it will be indexed by a date and it will hold all of the dates from data_start_time to dtata_end_time
daily_df = pd.DataFrame()
daily_df["date"] = pd.date_range(start=datetime.fromtimestamp(data_start_time), end=datetime.fromtimestamp(data_end_time), freq="D")
daily_df["date"] = daily_df["date"].dt.date
daily_df = daily_df.set_index("date")
daily_df.index = pd.to_datetime(daily_df.index)
# Excluse some dates from the daily range (the ones that we don't want to care about)
dates_to_exclude = []
for date in dates_to_exclude:
daily_df = daily_df.drop(pd.to_datetime(date).date())
# %% [markdown]
# # Data Management
# %%
# Check to see if there is any new data (from my android phone)
# Define the source and destination directories
src_dir = "/run/user/1000/gvfs/mtp:host=motorola_motorola_one_5G_ace_ZY22DDHW4G/Internal shared storage/Download/Samsung Health"
dest_dir = "./data/galaxywatch"
if os.path.exists(src_dir):
# Loop through all the directories in the source directory
for dir_name in os.listdir(src_dir):
# Check if the item is a directory
if os.path.isdir(os.path.join(src_dir, dir_name)):
# Check if the directory already exists in the destination directory
if not os.path.exists(os.path.join(dest_dir, dir_name)):
# If it doesn't exist, copy it over
shutil.copytree(os.path.join(src_dir, dir_name), os.path.join(dest_dir, dir_name))
dest_dir = "./data/galaxywatch/"
for dir_name in os.listdir(dest_dir):
folder_dir = f"{dest_dir}/{dir_name}/"
# Do some cleaning on the folder data
date_time_of_upload = folder_dir.split("_")[-1][:-1]
# Rename all of the files so that they are easier to deal with
for file_name in os.listdir(folder_dir):
if os.path.isdir(folder_dir + file_name):
continue
if "com.samsung" not in file_name:
continue
new_file_name = file_name.replace(".".join(file_name.split(
".")[:3]) + ".", "").replace(date_time_of_upload + ".", "")
os.rename(folder_dir + file_name, folder_dir + new_file_name)
# Clean csv's so that they dont have that first line with junk
for file_name in os.listdir(folder_dir):
if os.path.isdir(folder_dir + file_name):
continue
file_text = ""
with open(folder_dir + file_name, "r") as f:
file_text = f.read()
if "com.samsung" in (file_text.split("\n")[0]):
file_text = "\n".join(file_text.split("\n")[1:])
with open(folder_dir + file_name, "w") as f:
f.write(file_text)
else:
# See if its been more than 2 months since health data has been updated to the computer
is_older_than_2_months = lambda dir: (time.time() - os.path.getmtime(dir) > 60 * 60 * 24 * 30 * 2)
if all(os.listdir(dest_dir)):
logger.warning("It's been a long time since you uploaded data, you should consider uploading again!")
# %%
# Clean up the data a but
# %% [markdown]
# # Helper functions
# %%
def list_intersection(list_1, list_2) -> list:
return list(set(list_1) & set(list_2))
def union_dataframes(dataframe_1, dataframe_2, on_column="date"):
for column in dataframe_2.columns:
if column not in dataframe_1.columns:
dataframe_1[column] = dataframe_2[column]
else:
print(f"Warning: {column} already exists in dataframe_1")
return dataframe_1
def compute_correlation(df_1, df_2, x_axis, y_axies):
# Convert the "date" column to a datetime type
if x_axis == "date":
df_1[x_axis] = pd.to_datetime(df_1[x_axis])
df_2[x_axis] = pd.to_datetime(df_2[x_axis])
# Merge the sleep and computer usage DataFrames on the date column
if x_axis != "index":
merged_df = pd.merge(df_1, df_2, on=x_axis)
else:
merged_df = pd.concat([df_1, df_2])
# Compute the correlation between "last_time_used" and "sleep_amount"
correlation = merged_df[y_axies[0]].corr(merged_df[y_axies[1]])
return correlation
def average_series(series_1, series_2):
new_series = pd.Series(dtype=pd.Float64Dtype)
for column in series_1.index:
if type(series_1[column]) == str:
continue
new_series[column] = (series_1[column] + series_2[column]) / 2
return new_series
# Define a function to find the closest weight entry in fitdays_data for a given row in shealth_data
def average_measurements_that_are_close_in_time(dataframe: pd.DataFrame, time_column_name, group_1_function, group_2_function, time_difference_threshold, mode="mean"):
# Calculate the time difference between the current row and all rows in fitdays_data
group_1_dataframe = group_1_function(dataframe)
group_2_dataframe = group_2_function(dataframe)
group_1_dataframe = group_1_dataframe.sort_values(by=time_column_name)
group_2_dataframe = group_2_dataframe.sort_values(by=time_column_name)
new_dataframe = pd.DataFrame(columns=group_1_dataframe.columns)
# for each entry in the group_1_dataframe, find the neastest entry in time
for (index, row) in group_1_dataframe.iterrows():
candidates = (group_2_dataframe[abs(
group_2_dataframe[time_column_name] - row[time_column_name]) < time_difference_threshold])
if candidates.shape[0] == 0:
continue
match mode:
case "mean":
another_row = average_series(candidates.iloc[0], row)
new_dataframe = pd.concat(
[new_dataframe, another_row.to_frame().T], ignore_index=True)
return new_dataframe
def plot_variables(df, x_axis_column_name, y_axis_column_names, y_axis_visible, title, image_name="plot.png", convert_to_date=False):
if (len(y_axis_column_names) < 2):
print("Warning: only the you have only passed in 1 variable to plot")
color_hex_codes = ['#FF5733', '#40E0D0', '#4169E1', '#32CD32',
'#FFD700', '#DA70D6', '#FF7F50', '#708090', '#FF1493', '#008B8B']
axes = []
fig, ax1 = plt.subplots(figsize=(16, 9))
ax2 = ax1.twinx()
axes = [ax1, ax2]
if len(y_axis_column_names) > 2:
for i in range(2, len(y_axis_column_names)):
axes.append(axes[i % 2].twinx())
elif len(y_axis_column_names) == 2:
# Label the axes with the two different column names
axes[0].set_ylabel(
convert_snake_case_to_pascal_case(y_axis_column_names[0]))
axes[1].set_ylabel(
convert_snake_case_to_pascal_case(y_axis_column_names[1]))
else:
axes[0].set_ylabel(
convert_snake_case_to_pascal_case(y_axis_column_names[0]))
for ax, column_name in zip(axes, y_axis_column_names):
random.seed(69)
color = random.choice(color_hex_codes)
color_hex_codes.remove(color)
print(column_name, " is ", color)
ax.plot(df[x_axis_column_name], df[column_name],
label=column_name, color=color)
ax.get_yaxis().set_visible(y_axis_visible)
plt.legend()
if convert_to_date or "date" in x_axis_column_name:
convert_time_since_last_epoch_x_axis_to_date(ax)
plt.title(title)
savefig(title)
def savefig(title="", image_name="plot.png", type_="plot"):
if title == "" and image_name == "plot.png":
raise Exception("You must pass in a title or an image name")
if title != "":
image_name = type_ + "_for_" + to_snake_case(title)+ ".png"
plt.savefig(f"./figs/{image_name}")
def convert_time_since_last_epoch_to_date(time_since_last_epoch):
time_struct = time.localtime(time_since_last_epoch)
datetime_obj = datetime.fromtimestamp(time.mktime(time_struct))
return datetime_obj.strftime("%Y/%m/%d")
def convert_snake_case_to_pascal_case(snake_case_string):
return "".join([word.capitalize() for word in snake_case_string.split("_")])
def extract_time_format_from_datetime(formatted_time:str):
if formatted_time.count(":") < 1:
return "%Y-%m-%d"
elif "T" in formatted_time:
formatted_time = formatted_time.split("+")[0]
formatted_time = formatted_time.replace("T", " ")
return "%Y-%m-%dT%H:%M:%S"
return "%Y-%m-%d %H:%M:%S.%f"
def convert_datetime_to_time_since_last_epoch(formatted_time):
formatted_time = formatted_time.replace("/", "-")
time_format = extract_time_format_from_datetime(formatted_time)
time_struct = time.strptime(formatted_time, time_format)
datetime_obj = datetime.fromtimestamp(time.mktime(time_struct))
return datetime_obj.timestamp()
def apply_conversion_to_muliple_columns(dataframe, column_names, conversion: float):
if type(conversion) == list:
raise Exception("muliptle conversions not implemented yet")
for column in column_names:
dataframe[column] = dataframe[column].apply(lambda x: float(x) * conversion)
return dataframe
def convert_time_since_last_epoch_x_axis_to_date(ax):
labels = ax.get_xticklabels()
# Define the new x-axis tick labels
new_labels = [convert_time_since_last_epoch_to_date(float(label.get_text()) * 1e9) for label in labels]
# Set the new x-axis tick labels
ax.set_xticklabels(new_labels)
def convert_utc_offset_to_hours(utc_offset):
return int(utc_offset.split("-")[1][:2])
def remove_columns(dataframe, columns):
return dataframe.drop(columns=columns)
weekday_int_to_day = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
def convert_datetime_to_weekday(dataframe, column_name, weekday_int_to_day = False):
if(weekday_int_to_day):
dataframe["day_of_the_week"] = dataframe[column_name].apply(lambda x: weekday_int_to_day[datetime.fromtimestamp(x).weekday()])
else:
dataframe["day_of_the_week"] = dataframe[column_name].apply(lambda x: datetime.fromtimestamp(x).weekday())
return dataframe
def to_snake_case(string):
return "_".join(string.split(" ")).lower()
def clamp_dataframe_time(dataframe, time_name, start_time, end_time):
return dataframe[(dataframe[time_name] >= start_time) & (dataframe[time_name] <= end_time)]
def plot_box_and_whisker(dataframe: pd.DataFrame, x_axis_column_name, y_axis_column_name, title, x_axis_label, y_axis_label, image_name="box_and_whisker.png"):
if(image_name == "box_and_whisker.png"):
image_name = image_name.replace(".png", "_for_") + to_snake_case(title) + ".png"
axes = dataframe.boxplot(column=y_axis_column_name, by=x_axis_column_name, return_type='axes', figsize=(16, 9))
axes = (axes[y_axis_column_name])
axes.set_xlabel(x_axis_label)
axes.set_ylabel(y_axis_label)
axes.set_title(title)
# Get the current x-axis tick labels
# labels = axes.get_xticklabels()
if x_axis_column_name == "day_of_the_week":
new_labels = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
# Set the new x-axis tick labels
axes.set_xticklabels(new_labels)
plt.savefig(f"./figs/{image_name}")
def compute_r_squared(x, y, m, b):
# Compute the residuals
y_pred = m*x + b
residuals = y - y_pred
# Compute the mean of the residuals
mean_residuals = np.mean(residuals)
# Compute the total sum of squares
total_sum_squares = np.sum((y - mean_residuals)**2)
# Compute the residual sum of squares
residual_sum_squares = np.sum(residuals**2)
# Compute the R-squared
r_squared = 1 - (residual_sum_squares / total_sum_squares)
return r_squared
# Plot the line of best fit for the data
def plot_line_of_best_fit(dataframe, x_axis_column_name, y_axis_column_name, title, x_axis_label, y_axis_label, image_name = "line_of_best_fit.png"):
if(image_name == "line_of_best_fit.png"):
image_name = image_name.replace(".png", "_for_") + to_snake_case(title) + ".png"
x = dataframe[x_axis_column_name]
y = dataframe[y_axis_column_name]
m, b = np.polyfit(x, y, 1)
fig, ax = plt.subplots(figsize=(16, 9))
ax.set_title(title)
ax.set_xlabel(x_axis_label)
ax.set_ylabel(y_axis_label)
ax.plot(x, y, '.', label="data")
ax.plot(x, m*x + b, '-', label="line of best fit")
print(m)
# label the slope of the line in minutes/day
# Calculate the position of the annotation
x_pos = 0.5 # Adjust this value to move the annotation along the x-axis
y_pos = m * x_pos + b # This will place the annotation on the line of best fit
# Create the annotation
ax.annotate(f"{round(m * 3600 * 24 * 60, 5)} minutes/day",
xy=(x_pos, y_pos),
xytext=(20, 20),
textcoords='offset points',
arrowprops=dict(facecolor='black', shrink=0.05, width=1, headwidth=7),
fontsize=12,
color='red',
ha='center')
if "date" not in x_axis_column_name:
convert_time_since_last_epoch_x_axis_to_date(ax)
ax.legend()
plt.savefig(f"./figs/{image_name}")
def do_the_rudamentary_time_changes(dataframe: pd.DataFrame, use_end_time_for_date = False):
columns_to_convert_to_time_since_last_epoch = ["start_time", "end_time"]
if "time_offset" in dataframe.columns:
dataframe["time_offset"] = convert_utc_offset_to_hours(dataframe["time_offset"].iloc[0])
else:
dataframe["time_offset"] = 0
for column in columns_to_convert_to_time_since_last_epoch:
if column in dataframe.columns and type(dataframe[column].iloc[0]) == str:
dataframe[column] = dataframe[column].apply(convert_datetime_to_time_since_last_epoch) - dataframe["time_offset"] * 3600
dataframe = dataframe.sort_values(by="start_time")
if "end_time" in dataframe.columns:
dataframe = convert_datetime_to_weekday(dataframe=dataframe, column_name="end_time")
elif "start_time" in dataframe.columns:
dataframe = convert_datetime_to_weekday(dataframe=dataframe, column_name="start_time")
print(dataframe.head(5))
dataframe = clamp_dataframe_time(dataframe, "start_time", data_start_time, data_end_time)
if not use_end_time_for_date:
dataframe["date"] = dataframe["start_time"].apply(convert_time_since_last_epoch_to_date)
else:
dataframe["date"] = dataframe["end_time"].apply(convert_time_since_last_epoch_to_date)
return dataframe
def sum_df_by_variable(df: pd.DataFrame, x_axis, y_axies, modes):
# Group computer usage events by date
grouped = df.groupby(df[x_axis])
if type(y_axies) == str:
y_axies = [y_axies]
if type(modes) == str:
modes = [modes]
if len(y_axies) > len(modes) and len(modes) == 1:
modes = modes * len(y_axies)
elif len(y_axies) < len(modes) and len(y_axies) == 1:
y_axies = y_axies * len(modes)
elif len(y_axies) != len(modes):
raise Exception("y_axies and modes must be the same length")
# Create new DataFrame with the total durations
result_df = pd.DataFrame({
# x_axis: grouped.groups,
})
for axis, mode in zip(y_axies, modes):
match mode:
case "mean":
result_df[axis] = grouped[axis].mean()
case "sum":
result_df[axis] = grouped[axis].sum()
case "max":
result_df[axis] = grouped[axis].max()
case "min":
result_df[axis] = grouped[axis].min()
case "first":
result_df[axis] = grouped[axis].first()
case _:
raise Exception("Invalid mode")
# if ""
# "last_time_used" : grouped["start_time"].max()
# result_df["start_of_the_day"] = result_df["date"].apply(lambda x: str(x) + " 00:00:00.00").apply(convert_datetime_to_time_since_last_epoch)
# result_df["last_time_used"] = (result_df["last_time_used"] - result_df["start_of_the_day"]) / 3600
return result_df
# %% [markdown]
# # Weight
# %%
# Steps
steps = pd.read_csv(galaxy_watch_folder_dir + "tracker.pedometer_day_summary.csv", index_col=False)
steps = steps.rename(columns={"create_time" : "start_time"})
steps["start_time"] = steps["start_time"].apply(convert_datetime_to_time_since_last_epoch)
steps = do_the_rudamentary_time_changes(steps)
steps = pd.DataFrame(steps.groupby("date")["step_count"].max()).reset_index()
print(daily_df.head(10))
steps = steps.set_index("date")
steps.index = pd.to_datetime(steps.index)
steps = steps.rename(columns ={"step_count" : "num_steps"})
daily_df = union_dataframes(daily_df, steps, "date")
print(steps.head(10))
print(daily_df.head(10))
# %%
weight = pd.read_csv(galaxy_watch_folder_dir + "weight.csv", index_col=False)
# filter weight
# weight = weight[weight["pkg_name"].isin(["cn.fitdays.fitdays"])]
weight = weight[pd.notna(weight["body_fat_mass"])]
weight["start_time"] = weight["start_time"].apply(convert_datetime_to_time_since_last_epoch)
weight = weight.rename(columns = {"total_body_water" : "water_mass"})
weight = apply_conversion_to_muliple_columns(weight, ["body_fat_mass", "weight", "muscle_mass", "skeletal_muscle_mass", "fat_free_mass", "water_mass"], 2.2)
# weight = clamp_dataframe_time(weight, "start_time", data_start_time, data_end_time)
weight = do_the_rudamentary_time_changes(weight)
# For every weight entry that has a package name of "com.sec.android.app.shealth" find the closest weight entry from cn.fitdays.fitdays, make sure they are less than 7200 seconds apart
weight.drop(["deviceuuid", "vfa_level", "deviceuuid", ], axis=1, inplace=True)
group_1_function = lambda df: df[df["pkg_name"] == "com.sec.android.app.shealth"]
group_2_function = lambda df: df[df["pkg_name"] == "cn.fitdays.fitdays"]
weight = average_measurements_that_are_close_in_time(weight, "start_time", group_1_function, group_2_function, 3600 * 12)
weight = weight.sort_values(by="start_time")
weight.sort_index(inplace=True)
weight["date"] = weight["start_time"].apply(convert_time_since_last_epoch_to_date)
weight = weight.set_index("date")
weight.index = pd.to_datetime(weight.index)
# add weight data to daily_df
daily_df = union_dataframes(daily_df, weight, "date")
# for the daily_df, figure out what the daily change in weight is per day, when there are long stretchs without data then use the next days change in weight to compute on average how much weight was lost
# ex. 11/10 - NaN
# 11/11 - NaN
# 11/12 - -3
# TURNS INTO:
# ex. 11/10 - -1
# 11/11 - -1
# 11/12 - -1
#
def imputate_missing_data(df, column_name):
i = 0
while i < df.shape[0]:
# count number of instances of NaN
j = i
while j < df.shape[0] and pd.isna(df[column_name].iloc[j]):
j += 1
# if there are more than 0 NaN's in a row, then fill them in with the average of the next non-NaN value
if j == i:
i += 1
else:
for b in range(i ,j+1):
try:
df[column_name].iloc[b] = df[column_name].iloc[j] / (j - i+1)
except:
print("ERROR", b)
pass
i = j
return df
daily_df["delta_weight"] = daily_df["weight"].diff()
daily_df["delta_muscle_mass"] = daily_df["muscle_mass"].diff()
daily_df["delta_fat_free_mass"] = daily_df["fat_free_mass"].diff()
daily_df["delta_water_mass"] = daily_df["water_mass"].diff()
daily_df["delta_body_fat_mass"] = daily_df["body_fat_mass"].diff()
daily_df["delta_skeletal_muscle_mass"] = daily_df["skeletal_muscle_mass"].diff()
daily_df = imputate_missing_data(daily_df, "delta_weight")
daily_df = imputate_missing_data(daily_df, "delta_muscle_mass")
daily_df = imputate_missing_data(daily_df, "delta_fat_free_mass")
daily_df = imputate_missing_data(daily_df, "delta_water_mass")
daily_df = imputate_missing_data(daily_df, "delta_body_fat_mass")
daily_df = imputate_missing_data(daily_df, "delta_skeletal_muscle_mass")
# %%
# Create a rolling average calculation using the previous 7 days worth of data. If there is not any data within those 7 days
# then interpolate the data from other points
def create_rolling_average_dataframe(df: pd.DataFrame):
'''
The purpose of this function is that it will make a dataframe that is a rolling average of the input dataframe, but it will do that using the
df["start_time"] as the time since last epoch to make the rolling average calculations. This is useful because the data is not evenly spaced out
'''
rolling_average_df = pd.DataFrame()
rolling_average_df["start_time"] = df["start_time"]
rolling_average_df["rolling_average"] = df["weight"]
# Create the date column from the starttime column
rolling_average_df["date"] = rolling_average_df["start_time"].apply(convert_time_since_last_epoch_to_date)
rolling_average_df.set_index("date", inplace=True)
rolling_average_df.index = pd.to_datetime(rolling_average_df.index)
rolling_average_df = rolling_average_df.resample('D').mean()
# Calculate the rolling average before interpolating
rolling_average_df["rolling_average"] = rolling_average_df["rolling_average"].rolling(7, min_periods=1).median()
# Then interpolate the missing values
rolling_average_df["rolling_average"] = rolling_average_df["rolling_average"].interpolate()
# fill in the start_times for the new rolling averages
rolling_average_df["start_time"] = rolling_average_df.index.map(lambda x: convert_datetime_to_time_since_last_epoch(str(x) + ".00"))
# Find the slope of the rolling average to figure out weight loss per day
rolling_average_df["rolling_average_slope"] = rolling_average_df["rolling_average"].diff()
rolling_average_df["rolling_average_slope"] = rolling_average_df["rolling_average_slope"].rolling(7, min_periods=1).mean()
return rolling_average_df
rolling_average_df = create_rolling_average_dataframe(weight)
# rolling_average_df = rolling_average_df.dropna()
rolling_average_df = rolling_average_df.sort_values(by="start_time")
rolling_average_df = rolling_average_df.reset_index(drop=True)
rolling_average_df.head(5)
rolling_average_df.shape[0]
# %%
rolling_average_df
plot_line_of_best_fit(rolling_average_df, "start_time", "rolling_average", "Weight Loss", "Date", "Weight (lbs)")
# %%
plot_variables(rolling_average_df, "start_time", ["rolling_average", "rolling_average_slope"], True, title="Weight vs. Time", convert_to_date=True)
# %% [markdown]
# ## Sleepla
# %%
sleep_column_names = pd.read_csv(galaxy_watch_folder_dir + "sleep.csv", index_col=False, header=None, nrows=1).iloc[0].to_list ()
# sleep_column_names = ["idk", "mental_recovery", *[f"factor{i}" for i in range(9)], "idk", "idk", "idk", "idk", "idk", "efficiency", "idk","idk", "idk", "idk", "physical_recovery", "idk", "idk", "idk", "start_time", "idk","idk","idk","idk", "sleep_cycle", "idk", "restfulness", "sleep_score", "sleep_duration", "idk", "idk", "idk","idk", "idk", "idk", "idk", "time_offset", "idk", "idk", "idk", "end_time", "idk", "idk", "idk", "idk", "idk"]
# sleep_column_names = [x + ("{}".format(i) if "idk" in x else "") for i,x in enumerate(sleep_column_names)]
print(sleep_column_names)
sleep = pd.read_csv(galaxy_watch_folder_dir + "sleep.csv", index_col=False, header=None, skiprows=1, names = sleep_column_names)
print(sleep.iloc[0])
print()
print()
print()
print(sleep.iloc[-3])
print()
print()
print()
print(sleep.iloc[-2])
print()
print()
print()
print(sleep.iloc[-1])
# add sleep column
columns_to_remove = ["comment", "datauuid", "custom","deviceuuid","pkg_name", "original_efficiency", "extra_data", "quality", "original_bed_time", "create_time", "update_time", "combined_id", "has_sleep_data", "sleep_type", "data_version", *["idk" + str(i) for i in range(1, 60)]]
remove_prefix = lambda x: x.replace("com.samsung.health.sleep.", "")
# Remove columns from sleep
# Rename the columns using the lambda function
sleep = sleep.rename(columns=remove_prefix)
sleep = sleep.drop(columns=list_intersection(columns_to_remove, sleep.columns))
print(sleep.head(5))
sleep["sleep_duration"] = sleep["sleep_duration"].apply(lambda x: float(x) / 60)
# Change starttime so its local timzone
sleep = do_the_rudamentary_time_changes(sleep, use_end_time_for_date=True)
daily_sleep = sum_df_by_variable(sleep, "date", ["sleep_duration", "day_of_the_week", "start_time", "sleep_score", "efficiency", "mental_recovery", "physical_recovery"], ["sum", "first", "mean", "mean", "mean", "mean", "mean"])
daily_sleep.index = pd.to_datetime(daily_sleep.index)
temp_daily_sleep = daily_sleep.copy()
# rename all columns of sleep to have sleep_ in the column name
for column in daily_sleep.columns:
if column == "date" or "sleep" in column:
continue
temp_daily_sleep = temp_daily_sleep.rename(columns={column: "sleep_" + column})
# Now add every column in daily_sleep to daily_df if it doesn't already exist
union_dataframes(daily_df, temp_daily_sleep, "date")
# %%
# Plot box and whisker chart for day of the week and amount slept
plot_box_and_whisker(daily_sleep, "day_of_the_week", "sleep_duration", "Sleep Duration vs Day of the Week", "Day of the Week", "Sleep Duration (hours)")
plot_box_and_whisker(daily_sleep, "day_of_the_week", "sleep_score", "Sleep Score vs Day of the Week", "Day of the Week", "Sleep Score (0-100)")
plot_box_and_whisker(daily_sleep, "day_of_the_week", "efficiency", "Sleep Efficiency vs Day of the Week", "Day of the Week", "Sleep Efficiency (0-100)")
plot_box_and_whisker(daily_sleep, "day_of_the_week", "mental_recovery", "Mental Recovery vs Day of the Week", "Day of the Week", "Mental Recovery (0-100)")
plot_box_and_whisker(daily_sleep, "day_of_the_week", "physical_recovery", "Physical Recovery vs Day of the Week", "Day of the Week", "Physical Recovery (0-100)")
# %%
# plot_scatter(sleep, "com.samsung.health.sleep.start_time","sleep_duration")
plot_line_of_best_fit(daily_sleep, "start_time","sleep_duration", "Sleep Duration (hours) vs. Time", "Time", "Sleep Duration (hours)")
# %% [markdown]
# # Stress
# %%
stress = pd.read_csv(galaxy_watch_folder_dir + "stress.csv", index_col=False)
stress = stress.rename(columns=remove_prefix)
columns_to_remove = ["custom", "binning_data", "tag_id", "create_time", "algorithm", "deviceuuid", "comment", "pkg_name", "datauuid"]
stress = stress.drop(columns=list_intersection(columns_to_remove, stress.columns))
stress = do_the_rudamentary_time_changes(stress)
stress["hour"] = stress["start_time"].apply(lambda x: datetime.fromtimestamp(x).hour)
stress.head(10)
daily_stress = sum_df_by_variable(stress, "date", ["score", "max", "min", "score", "day_of_the_week"], ["mean"])
daily_stress.rename(columns={"score" : "stress_score", "max" : "stress_max", "min" : "stress_min", "score" : "stress_score"}, inplace=True)
daily_stress.index = pd.to_datetime(daily_stress.index)
daily_df.drop(columns=["score"], inplace=True, errors="ignore")
union_dataframes(daily_df, daily_stress, "date")
# %%
plot_box_and_whisker(daily_stress, "day_of_the_week", "stress_score", "Stress Level vs Day of the Week", "Day of the Week", "Stress Level")
plot_box_and_whisker(daily_stress, "day_of_the_week", "stress_max", "Peak Stress Level vs Day of the Week", "Day of the Week", "Stress Level")
# %%
plot_box_and_whisker(stress, "hour", "score", "Stress Level vs Hour of the Day", "Hour of the Day", "Stress Level")
# %%
def plot_matrix(dataframe: pd.DataFrame, x_axies, y_axis, title):
# This function will be used to see the average stress given a specific hour and given a specific day
assert len(x_axies) == 2
fig, ax = plt.subplots(figsize=(16, 9))
avg_stress = dataframe.groupby([x_axies[0], x_axies[1]])[y_axis].mean()
# convert the resulting series to a matrix with days as rows and hours as columns
matrix = avg_stress.unstack(level=1)
im = ax.imshow(matrix, cmap="hot", interpolation="nearest")
# convert the x axis into days of the week
new_labels = ["","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
ax.set_yticklabels(new_labels)
ax.set_xticklabels([""] + [str(z) + ":00" for z in range(6, 24, 2)])
ax.set_title(title)
colorbar = ax.figure.colorbar(im, ax=None)
colorbar.set_label("Stress Level (0-100)")
savefig(title, type_="matrix")
plot_matrix(stress[stress["hour"] > 6], ["day_of_the_week", "hour"], "score", "Stress Correlation Matrix For Days of the Week vs. Hour of the Day")
# %% [markdown]
# # Computer Usage
# %%
data = json.loads(open(computer_usage_folder_dir + "aw-bucket-export_aw-watcher-window_matts-computer.json").read())
# %%
computer_usage_raw = pd.DataFrame(data["buckets"]["aw-watcher-window_matts-computer"]["events"])
computer_usage_raw["start_time"] = computer_usage_raw["timestamp"].apply(lambda string: string.split("+")[0].split(".")[0]).apply(convert_datetime_to_time_since_last_epoch) - 5 * 3600
computer_usage_raw["timestamp"] = computer_usage_raw["start_time"].apply(convert_time_since_last_epoch_to_date)
computer_usage_raw.sort_values(by="start_time",ascending=False )
computer_usage_raw = do_the_rudamentary_time_changes(computer_usage_raw)
computer_usage = sum_df_by_variable(computer_usage_raw, "date", ["duration", "day_of_the_week", "start_time"], ["mean", "first", "max"])
computer_usage["last_time_used"] = computer_usage["start_time"]
computer_usage["start_time"] = (computer_usage["start_time"]).apply(convert_time_since_last_epoch_to_date).apply(convert_datetime_to_time_since_last_epoch)
computer_usage["last_time_used"] = (abs(computer_usage["start_time"] - computer_usage["last_time_used"])) / 3600
# %% [markdown]
# ## Computer Usage vs. Sleep
# %%
def pretty(item, name):
if name == "correlation":
print(f"The correlation between computer usage and sleep is {item}")
pretty(compute_correlation(computer_usage, daily_sleep, "index", ["last_time_used", "sleep_score"]), "correlation")
# %%
computer_usage["datetime"] = computer_usage["start_time"].apply(convert_time_since_last_epoch_to_date)
daily_sleep["datetime"] = daily_sleep["start_time"].apply(convert_time_since_last_epoch_to_date)
# %%
merged = pd.merge(computer_usage, daily_sleep, on="datetime")
print(merged.head(5))
# plot_variables(merged, "datetime", ["last_time_used", "sleep_score"], True, "Last Time Used vs. Sleep Score")
# %% [markdown]
# # Exercise
# %%
exercise = pd.read_csv(galaxy_watch_folder_dir + "exercise.csv", index_col=False)
columns_to_remove = ["subset_data", "routine_datauuid", "activity_type", "title", "tracking_status", "source_type", "reward_status", "mission_extra_value", "program_schedule_id", "program_id", "mean_caloricburn_rate", "heart_rate_deviceuuid", "live_data_internal", "mission_value", "pace_info_id", "pace_live_data", "mission_type", "location_data_internal", "additional_internal", "min_altitude", "max_altitude", "deviceuuid","completion_status" , "comment", "location_data", "sensing_status", "incline_distance", "decline_distance", "live_data", "datauuid", "max_cadence", "altitude_gain", "update_time", "create_time"]
remove_prefix = lambda x: x.replace("com.samsung.health.exercise.", "")
load_json_from_file = lambda file_name: json.loads(open(file_name).read())
save_json_to_file = lambda file_name, data: open(file_name, "w").write(json.dumps(data, indent=2))
exercise_type_number_to_string_map = load_json_from_file("exercise_label_map.json")
exercise = exercise.rename(columns=remove_prefix)
exercise = exercise.drop(columns = columns_to_remove)
exercise = do_the_rudamentary_time_changes(exercise)
exercise["exercise_type"] = exercise["exercise_type"].apply(lambda x: str(x)) # so it works with the json label map
print(exercise.head(10))
exercise["duration"] /= 1000
# use the exercise_type_number_to_string_map
# find any exerices not in the map
grouped = exercise.groupby("exercise_type").groups
if any([key not in exercise_type_number_to_string_map for key in list(grouped.keys())]):
logger.error("There are some exercises that I haven't labeled yet!")
unknown = (grouped.keys() - exercise_type_number_to_string_map.keys())
for key in unknown:
logger.error(f"Unknown exercise type: {key}")
logger.info("You did this exercise on the following dates: " + ", ".join(exercise.loc[grouped[key], "date"]))
new_label = input("What should the label be?\n")
exercise_type_number_to_string_map[key] = new_label
save_json_to_file("exercise_label_map.json", exercise_type_number_to_string_map)
exercise["exercise_type"] = exercise["exercise_type"].apply(lambda x: exercise_type_number_to_string_map[x])
# %% [markdown]
# ## Exercise & Sleep
# %%
# Find days when we go to the gym (weight lifting)
days_worked_out = exercise[exercise["exercise_type"] == "Weight Lifting"]["date"].unique()
print(daily_sleep.head(5))
daily_sleep["datetime"] = daily_sleep["start_time"].apply(convert_time_since_last_epoch_to_date)
daily_sleep["worked_out"] = daily_sleep["datetime"].apply(lambda x: float(x in days_worked_out))
daily_sleep["worked_out"].corr(daily_sleep["sleep_score"])
# %% [markdown]
# # Weight Lifting
# %%
data_file_path = exercise_logs_folder_dir + "gym_data_2022.txt"
raw = ""
with open(data_file_path, "r") as f:
raw = f.read()
# Split by paragraph
raw = raw.split("\n\n")
# Clean data
raw = [r.lower() for r in raw]
print(raw)
import json
def get_first_non_alpha_character_index(string):
for i, c in enumerate(string):
if not c.isalpha() and c != " ":
return i
raise Exception("No non-alpha characters found in " + repr(string))
exercises = {}
with open(exercise_logs_folder_dir + "exercise_aliases.json", "r") as f:
exercises = json.loads(f.read())
raw_exercises = [line.split("\n")[1:] for line in raw]
# flatten all_exercises array
raw_exercises = [item for sublist in raw_exercises for item in sublist]
# remove empty strings
raw_exercises = [x for x in raw_exercises if x and x != " "]
all_exercise_names = []
for exercise in raw_exercises:
print(exercise)
all_exercise_names.append(exercise[:get_first_non_alpha_character_index(exercise)])
all_exercise_names = [z.strip() for z in all_exercise_names]
# %% [markdown]
# ## Run this whenever importing new data to see if there are any new exercises
# %%
for exercise in all_exercise_names:
exists = False
for exercise_type in exercises:
if exercise in exercises[exercise_type]:
exists = True
break
if not exists:
answer = input("Ayo, we couldn't find an alias for " + exercise + ". What should its alias be?\nOptions:" + str(list(exercises.keys())) + "\n")
exercise = exercise.strip()
answer = answer.strip()
if answer in exercises:
exercises[answer].append(exercise)
else:
ohOh = input("We couldn't find " + answer + " in the list of exercise types. Should we add it?\n")
if "y" in ohOh:
exercises[answer] = [exercise]
else:
print("We didn't add the exercise:\t" + exercise)
#
# response = input("Do you want to save this data? (y/n)\n")
# save_json_to_file(exercise_logs_folder_dir + "exercise_aliases.json", exercises)
# %%
# Turn date into a time object
def convert_date_to_days(date):
if type(date) == list:
if len(date) == 1:
date = date[0]
else:
raise Exception("Homie wrong data type", date)
try:
if "-" in date:
date = date.split('-')
elif "/" in date:
date = date.split('/')
else:
raise Exception("Homie wrong date", date)
year = int(date[2])
month = int(date[1])
day = int(date[0])
return (year - 22) * 365 + month * 30.5 + day
except:
print(date)
raise Exception("Error: " + date)
def get_first_non_number_or_space_index(string):
for i in range(len(string)):
if not string[i].isdigit() and string[i] != ' ' and string[i] != "/":
return i
return -1
def get_first_number(string):
if len(string) == 0:
raise Exception("Not a proper string, length is 0")
for i in range(len(string)):
if string[i].isdigit():
start_index = i
break
else:
raise ValueError(string + " does not have a number!")
for i in range(start_index, len(string)):
if not string[i].isdigit() and string[i] != ".":
return string[start_index:i]
return string[start_index:]
class Set:
def __init__(self):
self.weight = 0
self.reps = 0
class Workout:
def __init__(self, day, sets):
self.day = day
self.sets = sets
def get_set_info_from_string(string):
string = string.strip()
sets = [Set() for i in range(len(string.split(",")))]
weight = get_first_number(string)
remainding_string = string[string.index(weight) + len(weight):]
for i in range(len(sets)):
sets[i].weight = float(weight)
reps = get_first_number(remainding_string)
sets[i].reps = float(reps)
remainding_string = remainding_string[remainding_string.index(reps) + len(reps):]
# print("Weight: ", sets[i].weight, "Reps: ", reps)
return sets
workouts = []
previous_day_of_workout = -999999
for i in range(len(raw)):
day_of_workout = (convert_date_to_days(raw[i][:get_first_non_number_or_space_index(raw[i])]))
if day_of_workout < previous_day_of_workout:
print("Error: ", raw[i])
exercises_on_that_day = raw[i].split("\n")[1:]
exercises_on_that_day = [exercise for exercise in exercises_on_that_day if exercise != '']
sets = []
for exercise in exercises_on_that_day:
exercise_type = exercise[:get_first_non_alpha_character_index(exercise)]
exercise_type = exercise_type.strip()
for possible_exercise_type in exercises:
if exercise_type in exercises[possible_exercise_type]:
exercise_type = possible_exercise_type
break
# if possible_exercise_type == "bench press":
if exercise_type == "lat pulldown":
print("For day of", raw[i][:get_first_non_number_or_space_index(raw[i])], "exercise type is", exercise_type)
exercise_set = exercise[get_first_non_alpha_character_index(exercise):].split(";")
exercise_set = [set.strip() for set in exercise_set]
exercise_set = [set for set in exercise_set if set != '']
for set in exercise_set:
# print(set)
if set[0] == "-":
set = set[1:]
# sets.extend(get_set_info_from_string(set))
for set in get_set_info_from_string(set):
sets.append(set)
print(len(sets))