-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilities.py
More file actions
53 lines (41 loc) · 1.65 KB
/
utilities.py
File metadata and controls
53 lines (41 loc) · 1.65 KB
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
from scipy.io import loadmat
import numpy as np
import pandas as pd
def getData(data):
samplingRate = data['data'][0][0][0][0][0][0]
numChannels = data['data'][0][0][1][0][0][0]
timePointLabels = data['data'][0][0][3][0][0]
rawSignal = data['data'][0][0][4][0]
timeOfEventandLabel = data['data'][2][0]
return (samplingRate, numChannels, timePointLabels, rawSignal, timeOfEventandLabel)
def createSamples(x,y, samplingRate):
timetoLabel = pd.DataFrame(y)
timetoLabel['samples'] = timetoLabel[0] * samplingRate
count = 0
X_train = []
labels = []
maxTime = timetoLabel.samples.max()
labeltoIndex = [10.0, 1.0, 7.0, 3.0, 8.0, 4.0]
while (count + 1) * 500 < maxTime:
median = (500 * count + 500 * (count + 1)) / 2
labelVal = timetoLabel[timetoLabel.samples > median].iloc[0][1]
if labelVal >= 0:
sample = x[500 * count: 500 * (count + 1),:]
X_train.append(sample)
labelVal = timetoLabel[timetoLabel.samples > median].iloc[0][1]
label = np.zeros(6)
label[labeltoIndex.index(labelVal)] = 1
labels.append(label)
count = count + 1
return np.array(X_train).astype('float32'), np.array(labels).astype('float32')
def getAllData(lstofFiles):
path = "./data"
X_trains = []
labels = []
for i in lstofFiles:
matFile = loadmat(path + '/' + i)
samplingRate, numChannels, timePointLabels, x, y = getData(matFile)
X_train, label = createSamples(x= x, y = y, samplingRate=samplingRate)
X_trains.append(X_train)
labels.append(label)
return np.concatenate(X_trains, axis = 0), np.concatenate(labels, axis = 0)