-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_input.py
67 lines (57 loc) · 2.62 KB
/
ml_input.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
import os
currentdir = os.getcwd()
walking = os.path.join(currentdir, 'filtered_data/walk')
upstairs = os.path.join(currentdir, 'filtered_data/upstairs')
downstairs = os.path.join(currentdir, 'filtered_data/downstairs')
# print (currentdir)
x = pd.DataFrame()
def get_subject_and_foot(filename):
base = os.path.basename(filename)
base = base.split(".")
subject = base[0].split('_')
subject, foot, secs, file_number = subject[2], subject[1], subject[3], subject[4]
return (subject, foot, secs, file_number)
def allData(dirname, action):
global x
for filename in os.listdir(dirname):
# print(x)
# print ("!!!!!!!!!!!!!!!!!!",filename)
file = os.path.join(dirname, filename)
data = pd.read_csv(file)
# print (data)
noise_filtered = data[["time","ax","ay","az"]].copy()
noise_filtered['prev_time'] = noise_filtered['time'].shift(1)
noise_filtered = noise_filtered.dropna()
noise_filtered['del_t'] = noise_filtered['time'] - noise_filtered['prev_time']
noise_filtered['vx'] = noise_filtered['ax']*noise_filtered['del_t']
noise_filtered['vy'] = noise_filtered['ay']*noise_filtered['del_t']
noise_filtered['vz'] = noise_filtered['az']*noise_filtered['del_t']
noise_filtered['px'] = noise_filtered['vx']*noise_filtered['del_t']
noise_filtered['py'] = noise_filtered['vy']*noise_filtered['del_t']
noise_filtered['pz'] = noise_filtered['vz']*noise_filtered['del_t']
speed = None
if (action in ['walking', 'jogging', 'running']):
subject, foot, secs, file_number = get_subject_and_foot(filename)
speed = 19.5/float(secs) # m/s
# print(noise_filtered)
# noise_filtered = noise_filtered.drop(['ax', 'ay', 'az'], axis = 1).copy()
noise_filtered = noise_filtered.drop(['time', 'prev_time', 'del_t'], axis = 1).copy()
noise_filtered = noise_filtered.drop(noise_filtered.index[0])
noise_filtered = noise_filtered.values.flatten()
noise_filtered = pd.DataFrame(noise_filtered).transpose()
action_col = pd.DataFrame([[action, speed]], columns=['action', 'speed'])
noise_filtered = pd.concat([action_col, noise_filtered], axis=1)
# print(noise_filtered.shape)
x = x.append(noise_filtered)
def createMlData():
allData(walking, 'walking')
allData(upstairs, 'upstairs')
allData(downstairs, 'downstairs')
print(x.shape)
x.to_csv('.\ml_input_data.csv', index=False)
if __name__ == '__main__':
createMlData()