-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_functions.py
209 lines (160 loc) · 4.98 KB
/
data_functions.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
import json
from datetime import datetime, timedelta
from utils import globals
globals.init()
def process_suffix(pk) -> str:
"""Transforms a datetime field into a string of 'year-month'
Parameters
----------
pk : datetime
Datetime of the first sleep session in this batch.
Returns
-------
str
Year and month of the datetime, in format 'year-month'.
"""
suffix = datetime.strftime(pk, '%Y-%m')
return suffix
def process_pk(key: str) -> int:
"""Handles the primary key from Sleep as Android, which is the session
start Unix timestamp. It is assigned to the global variable 'start_time'
and then transformed for primary key purposes.
Parameters
----------
key : str
The Unix timestamp from the 'Id' field in the CSV file.
Returns
-------
int
Original Unix timestamp in integer form.
"""
datetime_value = datetime.fromtimestamp(int(key)/1000)
globals.start_time = datetime_value
value = process_integer(key)
return value
def process_dates(detail: str) -> str:
"""Parses a string datetime from one format, then returns it as a string
in a better format.
Parameters
----------
detail : str
Original datetime string: day. month. year hour:minute
Returns
-------
str
New datetime string: year-month-day hour:minute
"""
datetime_value = datetime.strptime(detail, '%d. %m. %Y %H:%M')
datetime_string = datetime.strftime(datetime_value, '%Y-%m-%d %H:%M')
return datetime_string
def process_float(detail: str) -> float:
"""Receives a string and returns a float.
Parameters
----------
detail : str
String field.
Returns
-------
float
Field as a float.
"""
value = float(detail)
return value
def process_integer(detail: str) -> int:
"""Receives a string and returns an integer.
Parameters
----------
detail : str
String field.
Returns
-------
int
Field as an integer.
"""
value = int(detail)
return value
def process_event(event: str) -> dict:
"""Specifically handles 'Event' fields from Sleep as Android.
This involves splitting the event type, the Unix timestamp, and
the event's value if it has one.
Parameters
----------
event : str
String with event information separated by hyphens.
Returns
-------
dict
Completed dictionary with event split into type, datetime, and value (if
exists).
"""
event_parts = event.split('-', 2)
event_type = event_parts[0]
timestamp = datetime.fromtimestamp(int(event_parts[1])/1000)
# we want milliseconds, because the DHA event occurs every 1 millisecond
# until you fall asleep
event_time = timestamp.strftime('%Y-%m-%d %H:%M:%S.%f')
if len(event_parts) > 2:
if event_type == 'HR':
event_value = float(event_parts[2])
else:
event_value = event_parts[2]
event_dict = {
'event_type': event_type,
'event_time': event_time,
'event_value': event_value
}
else:
event_value = None
event_dict = {
'event_type': event_type,
'event_time': event_time
}
return event_dict
def process_actigraphy(time: str, value: str, start_time) -> dict[str, str]:
"""Specifically handles actigraphic events from Sleep as Android.
The header fields for these are made of the time (not including date)
of the data recorded, so we want to get the global start time and
use this to add a timestamp to each data point.
Parameters
----------
time : str
Hour and minute in string format.
value : str
Actigraphic value.
start_time : datetime
Global start time of this sleep record.
Returns
-------
dict[str, str]
Completed dictionary of actigraphic event, ready to be inserted into the
record.
"""
act_time_part = datetime.strptime(time, '%H:%M').time()
start_time_part = start_time.time()
start_time_date = start_time.date()
next_day_date = start_time_date + timedelta(days=1)
# the date isn't included in the actigraphic header, so once the time
# recorded is greater than the time that this sleep session started, we
# can assume it's the next day
if act_time_part > start_time_part:
act_datetime = datetime.combine(start_time_date, act_time_part)
else:
act_datetime = datetime.combine(next_day_date, act_time_part)
act_dict = {
'actigraphic_time': act_datetime.strftime('%Y-%m-%d %H:%M'),
'actigraphic_value': value
}
return act_dict
def process_array(records: list) -> str:
"""Receives an array and converts it into a JSON string.
Parameters
----------
records : list
An array of records.
Returns
-------
str
A JSON string.
"""
json_string = json.dumps(records)
return json_string