-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline-old.py
77 lines (64 loc) · 2.21 KB
/
pipeline-old.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
import os
import sys
import pandas as pd
import numpy as np
import plotly.express as px
from datetime import timedelta
event_id_mapping = {
0: "false negative reported",
1: "Puff Detected",
2: "Session detected",
3: "User Started Smoking Session",
4: "AI Started Smoking Session",
5: "User Stopped Smoking Session",
6: "Timer Stop Smoking"
}
if len(sys.argv) != 2:
print("Usage: python3 timedeltas.py [path-to-data-directory]")
exit(1)
os.system('mkdir -p processed')
dir = sys.argv[1]
df = pd.DataFrame()
for file in os.listdir(dir):
if file.startswith('Session'):
filename = file.split('.')[1]
with open(f'{dir}/{file}') as f:
starttime_millis = int(f.readline().rstrip()[17:])
session = pd.read_csv(f'{dir}/{file}', skiprows=1)
elif file.startswith("Info"):
continue
elif file.startswith("puffs"):
continue
else:
tmp = pd.read_csv(f'{dir}/{file}', skiprows=1)
df = pd.concat([df, tmp], ignore_index=True)
df = df.sort_values(by=['timestamp'], ignore_index=True)
print(len(df))
# get readable time (delta from start time)
real_times = []
for time_millis in df['real time']:
delta = (time_millis - starttime_millis) # convert to seconds
real_times.append(str(timedelta(milliseconds=delta)))
df['human time'] = real_times
# get time delta at each sample
deltas = []
for i, timestamp in enumerate(df['timestamp']):
if i == 0:
deltas.append(np.nan)
continue
# get time since last sample, convert to Hz
deltas.append( (df['timestamp'][i] - df['timestamp'][i-1]) / 1e9)
df['delta'] = deltas
print(df['delta'].describe())
print(df)
df.to_csv(f'processed/{filename}-processed.csv')
# fig = px.line(df.iloc[:len(df)], x='timestamp', y='delta', title='Time delta',
# labels={'timestamp': 'Time',
# 'delta': 'Time delta (s)'})
# fig.show(renderer='browser')
fig = px.line(df, x='timestamp', y=['acc_x', 'acc_y', 'acc_z', 'label'],
# color='activity',
title='Accelerometer Data',
labels={'timestamp': 'Time', 'variable': 'Legend'})
fig.update_layout(yaxis_title="Acceleration (m/s^2)")
fig.show(renderer='browser')