-
Notifications
You must be signed in to change notification settings - Fork 1
/
SAH_data.py
195 lines (153 loc) · 7.51 KB
/
SAH_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
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
185
186
187
188
189
190
191
192
193
194
195
import pandas as pd
import numpy as np
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import SimpleITK as sitk
import numpy as np
import torch
def collect_image(img_path):
collect_image_data = np.zeros((3,224,224))
get_img = sitk.ReadImage('../data/img/' + img_path[0]+'/Img_final_0.nii.gz')
return_img = sitk.GetArrayFromImage(get_img).astype(np.float32)
collect_image_data = return_img[10:13]
for num in range(1,len(img_path)):
get_img = sitk.ReadImage('../data/img/' + img_path[num]+'/Img_final_0.nii.gz')
return_img = sitk.GetArrayFromImage(get_img).astype(np.float32)
num_index = len(return_img) // 2
collect_image_data = np.concatenate((collect_image_data, return_img[num_index-2:num_index+1]),0)
return collect_image_data
class trainerData_collect(Dataset):
def __init__(self, img_data, data, outcome, treatment, is_train = True):
self.is_train = is_train
self.img_data = img_data
self.data = data
self.outcome = outcome
self.treatment = treatment
def __getitem__(self, index):
return_data = torch.from_numpy(self.data[index]).float().cuda()
return_yt = torch.from_numpy(np.concatenate([self.outcome[index], self.treatment[index]], 0)).float().cuda()
return_img = torch.from_numpy(self.img_data[index*3:index*3+3]).float().cuda()
return return_data, return_yt, return_img
def __len__(self):
return len(self.data)
class trainerData3d(Dataset):
def __init__(self, img_path, data, outcome, treatment, is_train = True):
self.is_train = is_train
self.img_path = img_path
self.data = data
self.outcome = outcome
self.treatment = treatment
def __getitem__(self, index):
return_data = torch.from_numpy(self.data[index]).float().cuda()
return_yt = torch.from_numpy(np.concatenate([self.outcome[index], self.treatment[index]], 0)).float().cuda()
get_img = sitk.ReadImage('../data/img/' + self.img_path[index]+'/Img_final_0.nii.gz')
return_img = sitk.GetArrayFromImage(get_img).astype(np.float32)
num_index = len(return_img) // 2
return_img = return_img[num_index-10:num_index+10]
return_img = return_img[np.newaxis,:,:,:]
#return_img = return_img.repeat(3,axis=0)
return_img = torch.from_numpy(return_img).float().cuda()
return return_data, return_yt, return_img
def __len__(self):
return len(self.img_path)
class trainerData3d_preload(Dataset):
def __init__(self, img_path, data, outcome, treatment, is_train = True):
self.is_train = is_train
self.img_path = img_path
self.data = data
self.outcome = outcome
self.treatment = treatment
self.all_image_data = []
for index in range(len(self.img_path)):
get_img = sitk.ReadImage('../data/img/' + self.img_path[index]+'/Img_final_0.nii.gz')
return_img = sitk.GetArrayFromImage(get_img).astype(np.float32)
num_index = len(return_img) // 2
return_img = return_img[num_index-10:num_index+10]
return_img = return_img[np.newaxis,:,:,:]
self.all_image_data.append(return_img)
def __getitem__(self, index):
return_data = torch.from_numpy(self.data[index]).float().cuda()
return_yt = torch.from_numpy(np.concatenate([self.outcome[index], self.treatment[index]], 0)).float().cuda()
return_img = self.all_image_data[index]
#return_img = return_img.repeat(3,axis=0)
return_img = torch.from_numpy(return_img).float().cuda()
return return_data, return_yt, return_img
def __len__(self):
return len(self.img_path)
class trainerData_cli(Dataset):
def __init__(self, img_path, data, outcome, treatment, is_train = True):
self.is_train = is_train
self.img_path = img_path
self.data = data
self.outcome = outcome
self.treatment = treatment
def __getitem__(self, index):
return_data = torch.from_numpy(self.data[index]).float().cuda()
return_yt = torch.from_numpy(np.concatenate([self.outcome[index], self.treatment[index]], 0)).float().cuda()
#return_outcome = torch.from_numpy(self.outcome[index]).float().cuda()
#return_treatment = torch.from_numpy(self.return_treatment[index]).float().cuda()
return return_data, return_yt
def __len__(self):
return len(self.img_path)
class trainerData(Dataset):
def __init__(self, img_path, data, outcome, treatment, is_train = True):
self.is_train = is_train
self.img_path = img_path
self.data = data
self.outcome = outcome
self.treatment = treatment
def __getitem__(self, index):
return_data = torch.from_numpy(self.data[index]).float().cuda()
return_yt = torch.from_numpy(np.concatenate([self.outcome[index], self.treatment[index]], 0)).float().cuda()
#return_outcome = torch.from_numpy(self.outcome[index]).float().cuda()
#return_treatment = torch.from_numpy(self.return_treatment[index]).float().cuda()
get_img = sitk.ReadImage('../data/img/' + self.img_path[index]+'/Img_final_0.nii.gz')
return_img = sitk.GetArrayFromImage(get_img).astype(np.float32)
num_index = len(return_img) // 2
return_img = torch.from_numpy(return_img[num_index - 2: num_index + 1]).float().cuda()
return return_data, return_yt, return_img
def __len__(self):
return len(self.img_path)
class trainerData_single(Dataset):
def __init__(self, img_path, data, outcome, treatment, is_train = True):
self.is_train = is_train
self.img_path = img_path
self.data = data
self.outcome = outcome
self.treatment = treatment
def __getitem__(self, index):
return_data = torch.from_numpy(self.data[index]).float().cuda()
return_yt = torch.from_numpy(np.concatenate([self.outcome[index], self.treatment[index]], 0)).float().cuda()
#return_outcome = torch.from_numpy(self.outcome[index]).float().cuda()
#return_treatment = torch.from_numpy(self.return_treatment[index]).float().cuda()
return return_data, return_yt
def __len__(self):
return len(self.img_path)
def convert_file(x):
x = x.values
x = x.astype(float)
return x
def load_and_format_covariates_hadcl(file_path):
data = pd.read_excel(file_path)
data = data.values[1:, ]
#binfeats = list(range(6,37))
#contfeats = [i for i in range(37) if i not in binfeats]
mu_0, mu_1, path, x = data[:, 3][:, None], data[:, 4][:, None], data[:, 5], data[:, 6:]
#perm = binfeats
#x = x[:, perm].astype(float)
return x.astype(float), path
def load_all_other_crap(file_path):
data = np.loadtxt(file_path, delimiter=',')
t, y, y_cf = data[:, 0], data[:, 1][:, None], data[:, 2][:, None]
mu_0, mu_1, x = data[:, 3][:, None], data[:, 4][:, None], data[:, 5:]
return t.reshape(-1, 1), y, y_cf, mu_0, mu_1
def load_all_other_crap_hadcl(file_path):
data = pd.read_excel(file_path)
data = data.values[1:, ]
t, y, y_cf = data[:, 0], data[:, 1][:, None], data[:, 2][:, None]
mu_0, mu_1, x = data[:, 3][:, None], data[:, 4][:, None], data[:, 6:]
return t.reshape(-1, 1).astype(float), y.astype(float), y_cf.astype(float), mu_0.astype(float), mu_1.astype(float)
def main():
pass
if __name__ == '__main__':
main()