-
Notifications
You must be signed in to change notification settings - Fork 1
/
datareader.py
213 lines (190 loc) · 7.79 KB
/
datareader.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
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
import csv
import pickle
import os
from dateutil import rrule
from datetime import datetime
from datetime import timedelta
from typing import Dict, List, Optional
from matplotlib.dates import hours
from linear_datascience import Comp1D
from datawrapper.value_wrapper import ValueWrapper
import numpy
import database
import math
class DataReader:
def __init__(self):
'''
CSV reader, to get healt data stored in csv
:param date: format: datetime
:param user_id: int
'''
self._db_inserts, self._db_extended = database.init()
def read_data(self, user_id: str, start_date: datetime, end_date: datetime, measurement_type) -> Dict:
ret_json = {}
for day in rrule.rrule(rrule.DAILY, dtstart=start_date,
until=end_date):
for row in self._db_extended.find_data(user_id, day, measurement_type):
if measurement_to_valuenumb[
int(row['type'])] == 1:
ret_json[row['time_stamp']] = {
'value': row['val1']}
elif measurement_to_valuenumb[
int(row.type)] == 2:
ret_json[row.time_stamp] = {
'value': {'value_1': row.val1,
'value_2': row.val2}}
elif measurement_to_valuenumb[
int(row.type)] == 3:
ret_json[row.time_stamp] = {
'value': {'value_1': row.val1,
'value_2': row.val2,
'value_3': row.val3}}
return ret_json
def read_sleep_data(self, user_id: str, start_date: datetime, end_date: datetime) -> List[Dict]:
ret_list = []
dates = []
data = []
cursors = self._db_extended.find_data_no_date(user_id, 777) #TODO: 777 to sleep
for day in rrule.rrule(rrule.DAILY, dtstart=start_date,
until=end_date):
dates.append(day.strftime('%d.%m.%Y'))
for cursor in cursors:
if cursor.time_stamp.strftime('%d.%m.%Y') in dates:
data.append(cursor)
for row in data:
wake_up = row.time_stamp
drop_off = row.val1
duration = wake_up - drop_off
deep_sleep = float(row.val2) * 100
if deep_sleep > 0:
ret_list.append(
{'user_id': user_id, 'date': (wake_up.strftime('%d.%m.%Y')),
'x': float(duration.seconds / 3600),
'y': deep_sleep})
return ret_list
def heart_rate_special(self, user_id: str, start_date:datetime, end_date: datetime) -> List[Dict]:
ret_list = []
dates = []
correl_data_wrappers = self._db_extended.find_correl_data(user_id)
values = []
values.extend(self._db_extended.find_data_tag(user_id, 'Cooldown', 'Heart rate'))
values.extend(self._db_extended.find_data_tag(user_id, 'Recovery', 'Heart rate'))
values = values[::10]
for day in rrule.rrule(rrule.DAILY, dtstart=start_date,
until=end_date):
dates.append(day.date())
for day in dates:
data = list(filter(lambda entry: entry.time_stamp().date() == day, correl_data_wrappers))
if len(data) > 0:
data = data[0]
new_json = {}
new_json['user_id'] = user_id
new_json['date'] = day.strftime('%d.%m.%Y')
new_json['a'] = data.a()
new_json['t'] = data.t()
new_json['c'] = data.c()
hr_lst_current_day = filter(lambda entry: entry.time_stamp.date() == day, values)
hr_lst_current_day = sorted(hr_lst_current_day, key=lambda entry: entry.time_stamp)
if len(hr_lst_current_day) < 1:
continue
base_time = hr_lst_current_day[0].time_stamp
datapoints = []
for data in hr_lst_current_day:
datapoints.append({'x': (data.time_stamp - base_time).seconds, 'y': data.val0})
new_json['data_points'] = datapoints
new_json['rmse'] = self.calculate_rmse(new_json['data_points'], new_json['a'], new_json['t'], new_json['c'])
ret_list.append(new_json)
return ret_list
def calculate_rmse(self, datapoints, a, t, c):
ret_value = 0
if a and t and c:
for data in datapoints:
function_value = (180-c)*math.exp(-(data['x']-t)/a)+c
ret_value = ret_value + math.pow(function_value-data['y'],2)
ret_value = math.sqrt(ret_value/len(datapoints))
return ret_value
else:
return None
def read_correlation_data(self, user_id: str, x_label: str, y_label: str, next_day: bool) -> Dict:
data_cursor = self._db_extended.find_correl_data(user_id)
data = []
if len(data_cursor) == 0:
return None
if x_label == 'Sleep start':
for d in data_cursor:
data.append(d)
reply = Comp1D(data, x_label, y_label, regr=True, B_next_day=False)
if not reply:
return None
for key, val in reply.items():
if type(val) == numpy.float64:
reply[key] = float(val)
elif type(val) == list:
pass
elif type(val) == numpy.int64:
reply[key] = int(val)
elif type(val) == numpy.int32:
reply[key] = int(val)
reply['x0'] = self.get_timedelta(reply['x0'])
reply['x1'] = self.get_timedelta(reply['x1'])
for data in reply['data']:
data[0] = self.get_timedelta(data[0])
return reply
else:
for d in data_cursor:
data.append(d)
reply = Comp1D(data, x_label, y_label, regr=True, B_next_day=False)
if not reply:
return None
for key, val in reply.items():
if type(val) == numpy.float64:
reply[key] = float(val)
elif type(val) == list:
pass
elif type(val) == numpy.int32:
reply[key] = int(val)
elif type(val) == numpy.int64:
reply[key] = int(val)
return reply
def read_step_counter(self, user_id: str, start_date:datetime, end_date: datetime) -> List[Dict]:
dates = {}
for day in rrule.rrule(rrule.DAILY, dtstart=start_date,
until=end_date):
dates[day.date()] = []
value_list = self._db_extended.find_data_tag(user_id, 'Step Tracking', 'Steps')
for step_value in value_list:
if step_value.time_stamp.date() in dates:
dates[step_value.time_stamp.date()].append(step_value.val0)
for key in dates:
dates[key] = sum(dates[key])
return dates
def convert_from_numpy(self, value):
if type(value) == numpy.float64:
return float(value)
elif type(value) == list:
pass
elif type(value) == numpy.int64:
return int(value)
def get_timedelta(self, value):
midnight = datetime(year=2016, month=12, day=2, hour=0, minute=0, second= 0)
time = timedelta(minutes=60 * value)
time = midnight + time
delta =time - datetime(year=2016, month=12, day=1, hour=0, minute=0, second= 0)
return delta.total_seconds()
measurement_to_valuenumb = {
21: 1,
1: 3,
512: 3,
31: 3,
32: 3,
33: 1,
34: 1,
35: 1,
36: 1,
37: 1,
38: 1,
13: 3,
777: 1
}
if __name__ == '__main__':
pass