-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.py
More file actions
394 lines (318 loc) · 14.3 KB
/
Copy pathfunc.py
File metadata and controls
394 lines (318 loc) · 14.3 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
import os
import json
import argparse
import numpy as np
from typing import List
from utils.es_data_utils import get_label_pretty_name, merged_label_names
from utils.es_data_utils import all_user_ids
from utils.utils import match_list, match_date
from utils.utils import smooth_pred, count_edges
from utils.utils import get_start_end_unix_time, unix_to_time_string
def calculate_duration(data_Y: np.ndarray,
data_T: np.ndarray,
activity: str,
all_activities: List[str],
date: str,
all_dates: List[str],
time_of_day: str,
thres: int=10):
#### activity should be a str, either one concrete activity or 'all activities'
#### date should be a str, either a concrete day or 'all days'
# make the data smooth
data_Y = smooth_pred(data_T, data_Y, window_size=3)
# get activity index
if activity == 'all activities':
matched_activity = all_activities
matched_act_idx = np.arange(len(all_activities))
else:
matched_activity, matched_act_idx = match_list(activity, all_activities)
matched_activity, matched_act_idx = [matched_activity], [matched_act_idx]
if date in ['all days', 'last week']: # Need to iterate through all days
search_dates = all_dates
else: # Only search the given date
search_dates = [date]
result = ""
for d in search_dates:
# get date time range
matched_date, match_date_idx = match_date(d, all_dates)
# get start and end time of the date
start_unix_time, end_unix_time = get_start_end_unix_time(matched_date, time_of_day)
# get the T that falls in this range
mask_1 = data_T > start_unix_time
mask_2 = data_T < end_unix_time
mask = mask_1 & mask_2
# get the final time results
for act, act_idx in zip(matched_activity, matched_act_idx):
minutes = data_Y[mask, act_idx].sum()
if minutes // 60 > 0:
if len(time_of_day) > 0:
result += f"You spent {minutes // 60} hours and {minutes % 60} minutes {act} in the {time_of_day} on {matched_date}. "
else:
result += f"You spent {minutes // 60} hours and {minutes % 60} minutes {act} on {matched_date}. "
else:
if len(time_of_day) > 0:
result += f"You spent {minutes % 60} minutes {act} in the {time_of_day} on {matched_date}. "
else:
result += f"You spent {minutes % 60} minutes {act} on {matched_date}. "
return result
def calculate_days(data_Y: np.ndarray,
data_T: np.ndarray,
activity: str,
all_activities: List[str],
date: str, # NOT USED
all_dates: List[str],
time_of_day: str, # NOT USED
thres: int=10):
# thres is a threshold for judging whether this activity happens
# make the data smooth
data_Y = smooth_pred(data_T, data_Y, window_size=3)
# get activity index
matched_activity, matched_act_idx = match_list(activity, all_activities)
cnt = 0
for d in all_dates:
# get date time range
matched_date, match_date_idx = match_date(d, all_dates)
# get start and end time of the date
start_unix_time, end_unix_time = get_start_end_unix_time(matched_date)
# get the T that falls in this range
mask_1 = data_T > start_unix_time
mask_2 = data_T < end_unix_time
mask = mask_1 & mask_2
# get the final time results
minutes = data_Y[mask, matched_act_idx].sum()
#print(minutes)
if minutes > thres:
cnt += 1 # Add one more day
digit_words = {
0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine'
}
if cnt < 2:
result = f"You were {matched_activity} {digit_words[cnt]} day. "
elif cnt < 10:
result = f"You were {matched_activity} {digit_words[cnt]} days. "
else:
result = f"You were {matched_activity} {cnt} days. "
return result
def calculate_frequency(data_Y: np.ndarray,
data_T: np.ndarray,
activity: str,
all_activities: List[str],
date: str,
all_dates: List[str],
time_of_day: str):
#### date can be a string, or None (search on all dates)
# make the data smooth
data_Y = smooth_pred(data_T, data_Y, window_size=5)
# get activity index
# get activity index
if activity == 'all activities':
matched_activity = all_activities
matched_act_idx = np.arange(len(all_activities))
else:
matched_activity, matched_act_idx = match_list(activity, all_activities)
matched_activity, matched_act_idx = [matched_activity], [matched_act_idx]
if date in ['all days', 'last week']: # Need to iterate through all days
search_dates = all_dates
else: # Only search the given date
search_dates = [date]
result = ""
for d in search_dates:
# get date time range
matched_date, match_date_idx = match_date(d, all_dates)
# get start and end time of the date
start_unix_time, end_unix_time = get_start_end_unix_time(matched_date, time_of_day)
# get the T that falls in this range
mask_1 = data_T > start_unix_time
mask_2 = data_T < end_unix_time
mask = mask_1 & mask_2
# get the final frequency results
for act, act_idx in zip(matched_activity, matched_act_idx):
edges = count_edges(data_Y[mask, act_idx])
if edges > 1:
if len(time_of_day) > 0:
result += f"You were {act} for {edges} times in the {time_of_day} on {matched_date}. "
else:
result += f"You were {act} for {edges} times on {matched_date}. "
else:
if len(time_of_day) > 0:
result += f"You were {act} for {edges} time in the {time_of_day} on {matched_date}. "
else:
result += f"You were {act} for {edges} time on {matched_date}. "
return result
def detect_first_time(data_Y: np.ndarray,
data_T: np.ndarray,
activity: str,
all_activities: List[str],
date: str,
all_dates: List[str],
time_of_day: str):
#### date must be a string!
# make the data smooth
data_Y = smooth_pred(data_T, data_Y, window_size=2) # Usually first time is more accurate
# get activity index
if activity == 'all activities':
matched_activity = all_activities
matched_act_idx = np.arange(len(all_activities))
else:
matched_activity, matched_act_idx = match_list(activity, all_activities)
matched_activity, matched_act_idx = [matched_activity], [matched_act_idx]
# get date time range
matched_date, match_date_idx = match_date(date, all_dates)
# get start and end time of the date
start_unix_time, end_unix_time = get_start_end_unix_time(matched_date, time_of_day)
# get the T that falls in this range
mask_1 = data_T > start_unix_time
mask_2 = data_T < end_unix_time
mask = mask_1 & mask_2
# get the final time results
result = ""
for act, act_idx in zip(matched_activity, matched_act_idx):
if data_Y[mask, act_idx].sum() > 0:
first_idx = np.argmax(data_Y[mask, act_idx])
formatted_time = unix_to_time_string(data_T[mask][first_idx])
if len(time_of_day) > 0:
result += f"You were {act} first time at {formatted_time} in the {time_of_day} on {matched_date}. "
else:
result += f"You were {act} first time at {formatted_time} on {matched_date}. "
else:
if len(time_of_day) > 0:
result += f"You were not {act} in the {time_of_day} on {matched_date}. "
else:
result += f"You were not {act} on {matched_date}. "
return result
def detect_last_time(data_Y: np.ndarray,
data_T: np.ndarray,
activity: str,
all_activities: List[str],
date: str,
all_dates: List[str],
time_of_day: str):
#### date must be a string!
# make the data smooth
data_Y = smooth_pred(data_T, data_Y, window_size=3)
# get activity index
matched_activity, matched_act_idx = match_list(activity, all_activities)
# get date time range
matched_date, match_date_idx = match_date(date, all_dates)
# get start and end time of the date
start_unix_time, end_unix_time = get_start_end_unix_time(matched_date, time_of_day)
# get the T that falls in this range
mask_1 = data_T > start_unix_time
mask_2 = data_T < end_unix_time
mask = mask_1 & mask_2
# get the final time results
if data_Y[mask, matched_act_idx].sum() > 0:
last_idx = np.argmax(data_Y[mask, matched_act_idx][::-1])
formatted_time = unix_to_time_string(data_T[mask][::-1][last_idx])
if len(time_of_day) > 0:
result = f"You were {matched_activity} last time at {formatted_time} in the {time_of_day} on {matched_date}. "
else:
result = f"You were {matched_activity} last time at {formatted_time} on {matched_date}. "
else:
if len(time_of_day) > 0:
result = f"You were not {matched_activity} in the {time_of_day} on {matched_date}. "
else:
result = f"You were not {matched_activity} on {matched_date}. "
return result
def find_activity(data_Y: np.ndarray,
data_T: np.ndarray,
activity: str,
all_activities: List[str],
date: str,
all_dates: List[str],
time_of_day: str,
thres: int=10):
# thres is a threshold for judging whether this activity happens
#### date must be a string!
# make the data smooth
data_Y = smooth_pred(data_T, data_Y, window_size=2)
# get date time range
matched_date, match_date_idx = match_date(date, all_dates)
# get start and end time of the date
start_unix_time, end_unix_time = get_start_end_unix_time(matched_date, time_of_day)
# get the T that falls in this range
mask_1 = data_T > start_unix_time
mask_2 = data_T < end_unix_time
mask = mask_1 & mask_2
# get the activities that happened
act_vec = data_Y[mask].sum(axis=0)
#print(act_vec)
act_str = ""
for i in range(len(all_activities)):
if act_vec[i] > thres:
if len(act_str) > 0:
act_str += ", "
act_str += all_activities[i]
if len(time_of_day) > 0:
result = f"You were {act_str} in the {time_of_day} on {matched_date}. "
else:
result = f"You were {act_str} on {matched_date}. "
return result
"""def find_cooccurence_activity(data_Y: np.ndarray,
data_T: np.ndarray,
co_activity: str,
all_activities: List[str],
date: str,
all_dates: List[str],
thres: int=10):
# thres is a threshold for judging whether this activity happens
# co_activity: the question is asking about the activity
# happening at the same time of this co_activity
#### date must be a string!
# make the data smooth
data_Y = smooth_pred(data_T, data_Y, window_size=2)
# get date time range
matched_date, match_date_idx = match_date(date, all_dates)
# get start and end time of the date
start_unix_time, end_unix_time = get_start_end_unix_time(matched_date)
# get the T that falls in this range
mask_1 = data_T > start_unix_time
mask_2 = data_T < end_unix_time
mask = mask_1 & mask_2
# if co_activity is given, only focusing on the activities that happened
matched_activity, matched_act_idx = match_activity(co_activity, all_activities)
co_act_mask = data_Y[mask, matched_act_idx].astype(bool)
act_vec = data_Y[mask][co_act_mask].sum(axis=0)
#print(act_vec)
act_str = ""
for i in range(len(all_activities)):
if act_vec[i] > thres and all_activities[i] != co_activity:
if len(act_str) > 0:
act_str += ", "
act_str += all_activities[i]
result = f"You were {act_str} while you were {matched_activity} on {matched_date}. "
return result"""
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', type=str, help="the path to ExtraSensory.per_uuid_features_labels")
args = parser.parse_args()
user_idx = 31
user_id = all_user_ids[user_idx]
data = np.load(os.path.join(args.data_path, '{}.npz'.format(user_id)))
all_dates = json.load(open('full_dates.json'))
all_activities = list(map(get_label_pretty_name, merged_label_names))
print(all_activities)
print(all_dates[str(user_idx)])
candidate_labels = ['at home', 'talking', 'with friends', 'with co-workers']
ind = np.array([all_activities.index(a) for a in candidate_labels])
result = calculate_duration(data['Y'][:, ind], data['T'], 'at home', candidate_labels, 'all days', all_dates[str(user_idx)], '')
print(result)
"""result = calculate_frequency(data['Y'], data['T'], 'eat', all_activities, 'all days', all_dates[str(user_idx)])
print(result)
result = detect_first_time(data['Y'], data['T'], 'groom', all_activities, 'Friday', all_dates[str(user_idx)])
print(result)
result = detect_last_time(data['Y'], data['T'], 'run', all_activities, 'Wednesday', all_dates[str(user_idx)])
print(result)
result = find_activity(data['Y'], data['T'], all_activities, 'Wednesday', all_dates[str(user_idx)])
print(result)
result = calculate_days(data['Y'], data['T'], 'meeting', all_activities, all_dates[str(user_idx)])
print(result)"""