-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyse_entries.py
107 lines (98 loc) · 4.86 KB
/
analyse_entries.py
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
import database
from datetime import datetime
import matplotlib
from dateutil import rrule
from datetime import datetime
from datetime import timedelta
import pymongo
import pprint
matplotlib.use('Agg')
import matplotlib.pyplot as plt
db_inserts, db_extends = database.init()
def analyse(user: str, day: datetime):
cooldown_value_list = db_extends.find_data_full(user, day, 'Cooldown', 'Heart rate')
training_value_list = db_extends.find_data_full(user, day, 'TrainingHR', 'Heart rate')
if len(cooldown_value_list) == 0 or len(training_value_list) == 0:
print("No data for", user, "(at", day, ")")
return
cooldown_value_list.sort(key=lambda key: key.time_stamp)
training_value_list.sort(key=lambda key: key.time_stamp)
cooldown_start_time = cooldown_value_list[0].time_stamp
cooldown_end_time = cooldown_value_list[-1].time_stamp
training_start_time = training_value_list[0].time_stamp
training_end_time = training_value_list[-1].time_stamp
x_axis_cooldown_values = []
x_axis_training_values = []
y_axis_cooldown_values = []
y_axis_training_values = []
for value in training_value_list:
x_axis_training_values.append((value.time_stamp - training_start_time).seconds)
y_axis_training_values.append(value.val0)
line = plt.plot(x_axis_training_values, y_axis_training_values, 'ro', color="red", linewidth=50)
for value in cooldown_value_list:
x_axis_cooldown_values.append((value.time_stamp - training_start_time).seconds)
y_axis_cooldown_values.append(value.val0)
plt.plot(x_axis_cooldown_values, y_axis_cooldown_values, 'ro', color="blue", linewidth=50)
minimal_value = min(cooldown_value_list, key=lambda val: val.val0).val0
maximal_value = max(cooldown_value_list, key=lambda val: val.val0).val0
time_delta = cooldown_end_time - training_start_time
plt.xticks(range(0, time_delta.seconds, 120))
plt.axis([0, time_delta.seconds, minimal_value, maximal_value])
plt.show()
plt.xlabel('seconds')
plt.ylabel('BPM')
plt.savefig(('%s_%s.png' % (user, day.date())), dpi=600)
plt.clf()
print('\n\n\n----------------------------------------------------------------------')
print('For user', user)
print('training took:', training_end_time - training_start_time, 'minutes from:', training_start_time, 'to',
training_end_time)
print('cooldown took:', cooldown_end_time - cooldown_start_time, ' minutes from:', cooldown_start_time, 'to',
cooldown_end_time)
print('----------------------------------------------------------------------\n\n\n')
def analyse_patients ():
first_day = datetime(2016, 12, 12, 0, 0, 0)
second_day = datetime(2016, 12, 14, 0, 0, 0)
third_day = datetime(2016, 12, 15, 0, 0, 0)
fourth_day = datetime(2016, 12, 19, 0, 0, 0)
users = [('[email protected]', first_day),
('[email protected]', second_day),
('[email protected]', fourth_day),
('[email protected]', first_day),
('[email protected]', fourth_day),
('[email protected]', first_day),
('[email protected]', third_day),
('[email protected]', fourth_day),
('[email protected]', fourth_day),
('[email protected]', third_day),
('[email protected]', fourth_day),
('[email protected]', fourth_day),
('[email protected]', fourth_day),
('[email protected]', second_day)]
for user in users:
analyse(user[0], user[1])
def analyse_data_all_days ():
users = ['[email protected]', '[email protected]']
for user in users:
tags = ['Cooldown', 'TrainingHR', 'Step Tracking', 'Sleep Tracking']
start_date = db_extends.db_base._db['%s_measure' % user].find().sort('effectiveDateTime', pymongo.ASCENDING).limit(1)[0]['effectiveDateTime']
end_date = db_extends.db_base._db['%s_measure' % user].find().sort('effectiveDateTime', pymongo.DESCENDING).limit(1)[0]['effectiveDateTime']
dates = []
for day in rrule.rrule(rrule.DAILY, dtstart=start_date,
until=end_date):
dates.append(day.date())
all_data = list(db_extends.db_base._db['%s_measure' % user].find())
print(len(all_data))
res = []
for day in dates:
temp = {}
for tag in tags:
data_size = len(list(filter(lambda entry: entry['effectiveDateTime'].date() == day and entry['category']['coding'][0]['display'] == tag, all_data)))
if data_size > 0:
temp[tag] = data_size
if len(temp) > 0:
res.append({day.strftime('%d.%m.%Y') :temp })
res = {user : res}
pprint.pprint(res, indent=4)
if __name__ == '__main__':
analyse_data_all_days()