-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload_data.py
141 lines (111 loc) · 5.11 KB
/
upload_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
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
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('../../../../' + 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 = 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()
try:
get_img = sitk.ReadImage('../../../../' + self.img_path[index]+'/Img_final_0.nii.gz')
return_img = sitk.GetArrayFromImage(get_img).astype(np.float32)
if return_img.shape[0] < 14:
print(self.img_path[index])
return_img = np.zeros((25,224,224))
except:
return_img = np.zeros((25,224,224))
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(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)
# for num in range(len(x)):
# a1 = x[num].astype(float)
# print(num)
return x.astype(float), path
def load_all_other_crap(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()