This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Data.py
82 lines (73 loc) · 3.04 KB
/
Data.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
import glob
import numpy
from Setting import *
from MatFile import *
from Subject import *
from SignalUtils import *
class Data:
def __init__(self):
self.mat = MatFile()
self.subject = Subject()
self.signal = SignalUtils()
self.subject_name = ""
def processMat(self, data_file, is_train, contest):
print(data_file)
self.mat.readMat(data_file, contest)
self.mat.name = data_file
self.mat.sampling_rate = 400
if contest == 1:
self.mat.data = self.signal.resample(self.mat.data, 400)
self.mat.data = self.signal.butterWorthBandpassFilter(self.mat.data, band=[0.1, 180], frequency = 400)
mat_list = self.mat.getDataList(time_length = self.mat.time_length)
list_length = mat_list.shape[0]
sizex, sizey = mat_list[0].data.shape
matBagX = numpy.zeros((list_length, sizex, sizey))
matBagY = []
for i in range(0, list_length):
matBagX[i, :, :] = mat_list[i].data
if is_train:
matBagY.append(self.mat.getMatLabel())
else:
matBagY.append(self.mat.name.split("/")[-1])
return matBagX, matBagY
def processMatList(self, mat_list, contest, sequence = 0, is_train = True):
dim0 = len(mat_list)
assert dim0 != 0
X, Y = self.processMat(mat_list[0], is_train, contest)
xdim0, xdim1, xdim2 = X.shape
dim0 = dim0 * xdim0
data_x = numpy.zeros((dim0, xdim1, xdim2))
data_y = []
data_x[0: xdim0, :, :] = X
data_y += Y
for i in range(1, len(mat_list)):
tmp_x, tmp_y = self.processMat(mat_list[i], is_train, contest)
data_x[i * xdim0: (i + 1) * xdim0, :, :] = tmp_x
data_y += tmp_y
if contest == 2:
data_x = numpy.transpose(data_x, (0, 2, 1))
return data_x, data_y
def processSubject(self, subject, is_train, contest, split_number = 1, sequence = 0):
data_list = None
if is_train:
data_list = self.subject.getTrainFileList(contest = contest, name = subject)
else:
data_list = self.subject.getTestFileList(contest = contest, name = subject)
if contest == 1:
if subject.find("Dog") != -1:
index = subject.find("Dog")
self.subject_name = subject[index: index + len("Dog") + 2]
else:
index = subject.find("Patient")
self.subject_name = subject[index: index + len("Patient") + 2]
amount = None
if len(data_list) % split_number == 0:
amount = int(len(data_list) / split_number)
else:
amount = int(len(data_list) / split_number + 1)
for i in range(split_number):
if i == sequence:
return self.processMatList(data_list[i * amount: (i + 1) * amount], contest = contest, sequence = sequence, is_train = is_train)
if __name__ == "__main__":
data = Data()
data.processSubject("/home/xiaobin/Downloads/train_1", is_train = True)