-
Notifications
You must be signed in to change notification settings - Fork 0
/
MGDataLoader.py
140 lines (120 loc) · 4.36 KB
/
MGDataLoader.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import logging
import MGKeyLogger
import MGEmotiveLogger
VERBOSITY = False
DEBUG = False
GLOBAL_PARAMS = {}
def CollectData(params, verb, deb):
VERBOSITY = verb
DEBUG = deb
GLOBAL_PARAMS = params
MGKeyLogger.setup(params, VERBOSITY, DEBUG)
MGEmotiveLogger.setup(params, VERBOSITY, DEBUG)
MGKeyLogger.StartLog()
MGEmotiveLogger.StartLog()
if not params["interval"]:
raw_input('Enter Any Key to stop collectiong Data')
else:
time.sleep(params['interval'])
keyData, imgData = MGKeyLogger.StopLog()
emotiveStateData, emotiveRawData = MGEmotiveLogger.StopLog()
nnInput, meta = formatData(keyData, imgData, emotiveRawData, emotiveStateData)
saveData(nnInput, meta)
return (nnInput, meta)
#format key data
#todo get ascii mappings for bittrip
#scan codes retrieved from here: https://msdn.microsoft.com/en-us/library/aa299374%28VS.60%29.aspx
#order is scancode, ascii
MAPPINGS = {'BIT_TRIP':{
'w':[17, 119, 87, 23],
'a':[30, 97, 65, 1],
's':[31, 115, 83, 19],
'space':[57, 32]}
}
def formatKeyData(rawKeyData):
mapping = MAPPINGS[GLOBAL_PARAMS['game']]
mappingSize = len(mapping)
for i in range(0, rawKeyData):
gameMapping = []
rawKeyData[i]['game'] = []
for k, v in kData.iteritems():
if rawKeyData[i]["key"] in v:
rawKeyData[i]['game'].append(1)
else
rawKeyData[i]['game'].append(0)
if DEBUG:
logging.debug("[%s] Convert key:%d to prob vector:%s"%(__name__, rawKeyData[i]["key"], rawKeyData[i]["game"]))
#synch data for format data funciton
def synchData(rawKeyData, imgData, emotiveRawData, emotiveStateData):
formatKeyData(rawKeyData)
synched = []
i = 0 #key index
j = 0 #emotive raw index
k = 0 #emotive state index
l = 0 #img index
maxLen = max(len(keyData), len(emotiveRawData), len(emotiveStateData))
for currentIndex in range(0, maxLen):
kTime = keyData[i]['timestamp']
iTime = imgData[l]['timestamp']
erTime = emotiveRawData[j]['timestamp']
esTime = emotiveStateData[k]['timestamp']
maxTime = max(kTime, erTime, esTime)
synched.append({'raw':emotiveRawData[j], 'es':emotiveStateData[k], 'key':keyData[i], 'img':imgData[l]})
if kTime <= maxTime:
i++
if erTime <= maxTime:
j++
if esTime <= maxTime:
k++
if iTime <= maxTime:
l++
if DEBUG:
logging.debug('[%s] Synched Data: %s'%(__name__, synched))
return synched
#How many samples per iteration to feed into neural network
#Currently decided to have as many that fit in .3 seconds
SAMPLE_PER_SET = 39
#Percent of data that is training
PERCENT_TRAINING = .8
channels = ["AF3", "F7", "F3", "FC5", "T7", "P7", "O1", "O2", "P8", "T8", "FC6", "F4", "F8", "AF4"]
#format data so that emotive data and keyboard input is synced
def formatData(keyData, imgData, emotiveRawData, emotiveStateData):
#assume synched by samples
synched = synchData(keyData, imgData, emotiveRawData, emotiveStateData)
data = []
for i in range (0, len(synched), SAMPLE_PER_SET):
data.append(synched[i:i+SAMPLE_PER_SET])
#Currently prefer raw data to emotive state data
nnData = {"testing":([],[]), "training":([],[])}
meta = {}
params = {}
trainingCutOff = PERCENT_TRAINING * len(data)
#trX, teX, trY, teY, meta = loadDataset()
#header for channels
for i in range(0, len(data)):
x = []
y = []
sample = data[i]
for j in range(0, len(sample))
for k in range(0, channels)
x.append(sample[j]['raw'][channels[k]])
y.append(sample[j]['key']['game'])
if i < trainingCutOff:
nnData["training"][0].append(x)
nnData["training"][1].append(y)
else:
nnData["testing"][0].append(x)
nnData["testing"][1].append(y)
return (nnData, synched)
#save data collected incase we need to redo
def saveData(nnInput, meta):
data = {}
with open(GLOBAL_PARAMS['infoFile'], "r") as data_file:
data = json.load(data_file)
data['NN_INPUT'] = nnData
data['NN_META'] = meta
with open(GLOBAL_PARAMS['infoFile'], 'w') as data_file:
json.dump(data, data_file)
#save neuralnetwork to data.json
def SaveNetwork(nnTrainingData, nnTestData, currentNetwork):
pass