-
Notifications
You must be signed in to change notification settings - Fork 1
/
timedeltas.py
65 lines (53 loc) · 1.87 KB
/
timedeltas.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
import os
import sys
import pandas as pd
import numpy as np
import plotly.express as px
from datetime import timedelta
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.iloc[:len(df)], x='human time', y='delta', title='Time delta',
# labels={'human time': 'Time',
# 'delta': 'Time delta (s)'})
# fig.show(renderer='browser')