-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_processing.py
144 lines (123 loc) · 4.08 KB
/
pre_processing.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
141
142
143
144
import numpy as np
import h5py
def get_data():
A01T = h5py.File("./project_datasets/A01T_slice.mat", 'r')
X = np.copy(A01T["image"])
y = np.copy(A01T["type"])
y = y[0, 0:X.shape[0]:1]
y = np.asarray(y, dtype=np.int32)
X = X[:, :22, :]
for i in range(len(y)):
if y[i] == 769:
y[i] = 0
elif y[i] == 770:
y[i] = 1
elif y[i] == 771:
y[i] = 2
elif y[i] == 772:
y[i] = 3
else:
y[i] = 0
X = X.reshape(-1, 22*1000)
y = y[~np.isnan(X).any(axis=1)]
X = X[~np.isnan(X).any(axis=1)]
X = X.reshape(-1, 22, 1000)
# Define training and testing data
t_size = np.array(range(X.shape[0]))
np.random.shuffle(t_size)
test_mask = t_size[:50]
train_mask = t_size[50:]
X_test = X[test_mask]
y_test = y[test_mask]
X_train = X[train_mask]
y_train = y[train_mask]
return X_test, y_test, X_train, y_train
def get_all_data():
A01T = h5py.File("./project_datasets/A01T_slice.mat", 'r')
X = np.copy(A01T["image"])
y = np.copy(A01T["type"])
y = y[0, 0:X.shape[0]:1]
y = np.asarray(y, dtype=np.int32)
X = X[:, :22, :]
for i in range(len(y)):
if y[i] == 769:
y[i] = 0
elif y[i] == 770:
y[i] = 1
elif y[i] == 771:
y[i] = 2
elif y[i] == 772:
y[i] = 3
else:
y[i] = 0
X = X.reshape(-1, 22*1000)
y = y[~np.isnan(X).any(axis=1)]
X = X[~np.isnan(X).any(axis=1)]
X = X.reshape(-1, 22, 1000)
# Define training and testing data
t_size = np.array(range(X.shape[0]))
np.random.shuffle(t_size)
test_mask = t_size[:50]
train_mask = t_size[50:]
X_test = X[test_mask]
y_test = y[test_mask]
X_train = X[train_mask]
y_train = y[train_mask]
for j in range(2, 10):
string_name = "./project_datasets/A0" + str(j) + "T_slice.mat"
A01T = h5py.File(string_name, 'r')
X = np.copy(A01T["image"])
y = np.copy(A01T["type"])
y = y[0, 0:X.shape[0]:1]
y = np.asarray(y, dtype=np.int32)
X = X[:, :22, :]
for i in range(len(y)):
if y[i] == 769:
y[i] = 0
elif y[i] == 770:
y[i] = 1
elif y[i] == 771:
y[i] = 2
elif y[i] == 772:
y[i] = 3
else:
y[i] = 0
X = X.reshape(-1, 22 * 1000)
y = y[~np.isnan(X).any(axis=1)]
X = X[~np.isnan(X).any(axis=1)]
X = X.reshape(-1, 22, 1000)
# Define training and testing data
t_size = np.array(range(X.shape[0]))
np.random.shuffle(t_size)
test_mask = t_size[:50]
train_mask = t_size[50:]
X_test = np.concatenate((X_test, X[test_mask]), axis=0)
y_test = np.concatenate((y_test, y[test_mask]), axis=0)
X_train = np.concatenate((X_train, X[train_mask]), axis=0)
y_train = np.concatenate((y_train, y[train_mask]), axis=0)
return X_test, y_test, X_train, y_train
def crop_data(X_test, y_test, X_train, y_train):
X_test_out = []
y_test_out = []
X_train_out = []
y_train_out = []
X_test_out = X_test[:, :, :500]
y_test_out = y_test
X_train_out = X_train[:, :, :500]
y_train_out = y_train
for i in range(1, 501, 25):
X_test_out = np.concatenate((X_test_out, X_test[:, :, i:i+500]), axis=0)
# y_test_out = np.concatenate((y_test_out, y_test))
X_train_out = np.concatenate((X_train_out, X_train[:, :, i:i+500]), axis=0)
y_train_out = np.concatenate((y_train_out, y_train))
print(X_test.shape)
return X_test_out, y_test_out, X_train_out, y_train_out
def noise_data(X_test, y_test, X_train, y_train):
X_test_out = X_test
y_test_out = y_test
X_train_out = X_train
y_train_out = y_train
for _ in range(4):
X_train_out = np.concatenate((X_train_out, np.random.normal(size=X_train.shape) + X_train), axis=0)
y_train_out = np.concatenate((y_train_out, y_train))
return X_test_out, y_test_out, X_train_out, y_train_out