-
Notifications
You must be signed in to change notification settings - Fork 0
/
processdata.py
118 lines (101 loc) · 3.69 KB
/
processdata.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
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
import random
import numpy as np
import scipy.stats as st
np.seterr(all='raise')
_FRAC = [0.5,0.7]
def writedata(series, name):
f = open(name, 'w')
for s in series:
label, values = s
f.write(str(label + 1))
for v in values:
f.write(',' + str(v))
f.write('\n')
f.close()
def writedatalines(series, name):
f = open(name, 'w')
for s in series:
label, values = s
for v in values:
f.write(str(label) + ',' + str(v))
f.write('\n')
f.close()
def partition(snames, classes):
# Sample data
for i,sname in enumerate(snames):
train,test,ex = [],[],[]
for cl in classes:
cls = cl[i]
if len(cls) > 0:
train.extend(cls[:int(len(cls) * _FRAC[0])])
test.extend(cls[int(len(cls) * _FRAC[0]):int(len(cls) * _FRAC[1])])
ex.extend(cls[int(len(cls) * _FRAC[1]):])
writedata(train, 'ArmSensor_' + sname + '_TRAIN')
writedata(test, 'ArmSensor_' + sname + '_TEST')
writedata(test, 'ArmSensor_' + sname + '_EX')
def writeout(snames, classes):
for i,sname in enumerate(snames):
out = []
for cl in classes:
cls = cl[i]
if len(cls) > 0:
out.extend(cls)
writedata(out, 'ArmSensor_' + sname + '_EX')
def red(x, y):
if x == y:
return 1
else:
return 0
def process(dirnames, seed, sample):
random.seed(seed)
snames = ['s0','s1','s2','s3']
sensors = ([],[],[],[])
classes = [[[],[],[],[]], [[],[],[],[]], [[],[],[],[]], [[],[],[],[]], [[],[],[],[]], [[],[],[],[]]]
# Organize and normalize data
'''
for dname in dirnames:
for name,sensor in zip(snames, sensors):
f = open(dname + '/' + name + '.csv', 'r')
for line in f:
sep = line.split(',')
label = int(sep.pop(0))
values = np.array([float(x) for x in sep])
try:
normvalues = st.zscore(values)
except Exception:
print('Divide by zero')
continue
sensor.append((label,normvalues))
f.close()
'''
for dname in dirnames:
files = [open(dname + '/' + name + '.csv', 'r') for name in snames]
for lines in zip(*files):
separated = [line.split(',') for line in lines]
labels = [red(int(sep.pop(0)),4) for sep in separated]
try:
normalized = [st.zscore(np.array([float(x) for x in sep])) for sep in separated]
except Exception:
print('Divide by zero')
continue
for label,normed,sensor in zip(labels,normalized, sensors):
sensor.append((label,normed))
# Separate classes
for i,sensor in enumerate(sensors):
for s in sensor:
label,values = s
classes[label][i].append(s)
# Shuffle lists, sample data
for cl,value in enumerate(classes):
rows = [row for row in zip(*value)]
random.shuffle(rows)
reshape = [[],[],[],[]]
for row in rows:
for ent,lst in zip(row,reshape):
lst.append(ent)
classes[cl] = reshape
#for sensor in value:
#random.shuffle(sensor)
sample(snames, classes)
if __name__ == '__main__':
process(['theospread','theodbtap','theowr','theowl','theofist','johnfist','johnwl','johnspread','johnwr','johndbtap','phucdbtap','phucwr','phucspread','phucwl','phucfist'], 48093720473, partition)