-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
38 lines (34 loc) · 813 Bytes
/
utils.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
def time_based_sliding_window_agg(arr, timestamps, N, func, ind):
i, j = 0, 1
sum = arr[0]
l = len(arr)
ret = ind
while j < l:
if timestamps[j-1] - timestamps[i] < N:
sum += arr[j]
j += 1
elif timestamps[j-1] - timestamps[i] > N:
sum -= arr[i]
i += 1
else:
ret = func(ret, sum)
sum += arr[j]
j += 1
return ret
def sliding_window_agg(arr, N, func, ind):
i, j = 0, 1
sum = arr[0]
l = len(arr)
ret = ind
while j < l:
if j-1-i < N:
sum += arr[j]
j += 1
elif j-1-i > N:
sum -= arr[i]
i += 1
else:
ret = func(ret, sum)
sum += arr[j]
j += 1
return ret