-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemSetupAndSimulationSaveErrors.sh
184 lines (128 loc) · 7.27 KB
/
systemSetupAndSimulationSaveErrors.sh
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
import argparse
import keras
import pandas as pd
import numpy as np
import itertools
import my_modules.preprocessdata
import my_modules.nnmodelsV4
import my_modules.offline
from my_modules.preprocessdata import Activities, IMUSensors
from my_modules.nnmodelsV4 import NNModel, LSTMModelFactory, BaseCallbacksListFactory, loadNNModel
from my_modules.offline import ActivityModule, Classifier, Sensor, ArgMinStrategy, SingleSensorSystem, PyPlotter, Reasoner, MostFrequentStrategy, WindowSelector
# # reload modules (useful for development)
# import importlib
# importlib.reload(preprocessdata)
# importlib.reload(nnmodelsV4)
# importlib.reload(offline)
# from preprocessdata import Activities, IMUSensors
# from nnmodelsV4 import NNModel, LSTMModelFactory, BaseCallbacksListFactory, loadNNModel
# from offline import ActivityModule, Classifier, Sensor, ArgMinStrategy, SingleSensorSystem, PyPlotter, Reasoner, MostFrequentStrategy, WindowSelector
# Define the parser
parser = argparse.ArgumentParser(description='System Save Errors')
# Declare an argument (`--algo`), telling that the corresponding value should be stored in the `algo` field, and using a default value if the argument isn't given
parser.add_argument('--actcat', action="store", dest='activityCategory', default='Locomotion')
# Now, parse the command line arguments and store the values in the `args` variable
args = parser.parse_args() # Individual arguments can be accessed as attributes of this object
baseDir = 'NNModels' # NNModels base directory
#*****************************************************************************
#SYSTEM SPECIFICS
testPersonsSessions = [('P01', 'S1'), ('P02', 'S2'), ('P03', 'S3'), ('P04', 'S4'), ('P05', 'S1'), ('P06', 'S4')] # final test set
evaluationPersonsSessions = [('P07', 'S1'), ('P08', 'S4')] # evaluation set
personsSessions = [('P09', 'S2'), ('P10', 'S3')] # tuning set
personsSessions = personsSessions + testPersonsSessions + evaluationPersonsSessions
lookback = 15
sensorChannels = 6
# Choose activity category from BothArmsLabel, RightArmLabel, LeftArmLabel, Locomotion
activityCategory = args.activityCategory
print(f'\n{activityCategory}\n')
# Choose Sensors among ['backImu' , 'llaImu', 'luaImu', 'rtImu', 'rlaImu', 'ruaImu']
sensorNames = ['backImu' , 'llaImu', 'luaImu', 'rtImu', 'rlaImu', 'ruaImu']
# Activities (get all activityNames)
activities = Activities()
activityNames = activities.getNamesWithCategory(activityCategory)
print('\nSELECTED activity names:\n', activityNames)
classifyStrategy = ArgMinStrategy()
reasonerSelectionStartegy = MostFrequentStrategy()
windowSelectionStrategy = MostFrequentStrategy()
windowLength = 1
#*****************************************************************************
print('\nSESNOR SYSTEM MODULES SETUP START')
# SYSTEM SETUP
identifier = {
'activityCategory' : activityCategory,
'sensor' : None,
'activityName' : None,
}
# setup of the sensorSystems and the corresponding, windowSelections and Classifiers
sensorSystems = []
classifiers = []
windowSelectors = []
for sensorName in sensorNames:
identifier['sensor'] = sensorName
activityModules = []
for activityName in activityNames:
identifier['activityName'] = activityName
nnModel = loadNNModel(identifier, lookback = lookback, sensorChannels = sensorChannels, baseDir = baseDir)
activityModules.append(ActivityModule(nnModel))
sensorSystems.append(SingleSensorSystem(activityModules))
classifiers.append(Classifier(classifyStrategy, sensor = sensorName, activityCategory = activityCategory))
windowSelectors.append(WindowSelector(windowLength, windowSelectionStrategy, sensor = sensorName, activityCategory = activityCategory))
print(f'\n sensor system {sensorName} set')
print('\nSESNOR SYSTEM MODULES SETUP END')
errorsDfList = []
for person, session in personsSessions:
print(f'\n({person}, {session}) SENSORS SETUP START')
# setup of the Sensors
imuSensorsDataFrame = pd.read_csv('imuSensorsWithQuaternions.csv', header = [0,1], index_col = [0,1,2])
imuSensors = IMUSensors(imuSensorsDataFrame)
idx = pd.IndexSlice
identifier = {
'activityCategory' : activityCategory,
'sensor' : None,
}
sensors = []
for sensorName in sensorNames:
identifier['sensor'] = sensorName
sensorDf = imuSensors.singleSensorDf(sensorName).loc[idx[person, session], :]
sensors.append(Sensor(sensorDf, identifier = identifier, sensorChannels = sensorChannels))
print(f'\n sensor {sensorName} set')
# setup aggregation Module
reasoner = Reasoner(reasonerSelectionStartegy)
print(f'\n({person}, {session}) SENSORS SETUP END')
#*****************************************************************************
# SIMULATION
print(f'\n({person}, {session}) SYSTEM SIMULATION START\n')
tiSim = 0 # simulation initial timestep (sec = timsteps / freq)
#tfSim = 10 # simulation final timestep (sec = timsteps / freq)
tfSim = len(imuSensorsDataFrame.loc[idx[person, session], :].values)
# errors per sensor and per activity at sensor frequency
sensorSystemsErrors = np.empty((len(sensors), tfSim-tiSim, len(activityNames)))
# activity selected by the classifier at sensor frequency
selectedActivityId = np.empty((tfSim-tiSim, len(sensors)), dtype=object) # id are dictionaries
# activity selected by window selection at sensor frequency // windowLength frequency
numOfFinalSamples = (tfSim-tiSim) // windowLength
windowSelectedActivityId = np.empty((numOfFinalSamples, len(sensors)), dtype=object)
windowSelectedActivityName = np.empty((numOfFinalSamples, len(sensors)), dtype=object) # names are string
windowResultantActivityName = np.empty(numOfFinalSamples, dtype=object)
for t in range(tiSim, tfSim):
for i in range(len(sensors)): # for each sensor
sensorData = sensors[i].getDataWithTimeIndex(t)
errorsAndIds = sensorSystems[i].getErrorsAndIds(sensorData) # errorAndIds is list of (float, dictionary)
for j in range(len(activityNames)): # for each actvityModule in the sensorSystem
sensorSystemsErrors[i,t - tiSim,j] = errorsAndIds[j][0] # t timestep, i sensor system, , j activity module
if t % 100 == 0:
print(f'simulation time {t}')
# t timestep, i sensor system, , j activity module
# sensorSystemsErrors[i,:,:] rows are the timestep and columns refers to the activities
allSensorsErrors = np.concatenate([sensorSystemsErrors[i,:,:] for i in range(len(sensorNames))], axis = 1)
print(f'\n{sensorSystemsErrors[0,:,:].shape}, {allSensorsErrors.shape}')
numOftimesteps = sensorSystemsErrors.shape[1]
columnsFirstLevel = [[sensorName]*len(activityNames) for sensorName in sensorNames]
columnsFirstLevel = list(itertools.chain.from_iterable(columnsFirstLevel))
columns = [columnsFirstLevel, activityNames * len(sensorNames)]
index = [[person] * numOftimesteps, [session] * numOftimesteps]
errorsDfList.append(pd.DataFrame(allSensorsErrors, columns = columns, index = index))
print('\n({person}, {session}) SYSTEM SIMULATION END\n')
errorsDf = pd.concat(errorsDfList, axis = 0)
errorsDf.to_csv(f'{activityCategory}_errors.csv')