-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreproccess_data.py
159 lines (127 loc) · 4.61 KB
/
preproccess_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
from PIL import Image
import numpy as np
from skimage.transform import resize
import nibabel as nib
import torch
from torchvision import datasets, transforms
import os
from os import listdir
from os.path import isfile, join
from utils import reshape_nifti
from dataset import HeartData
# ct_2_x274y0z0.jpg
# add zeros to filenames
path_to_files = "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64_test/"
counter = 0
for f in listdir(path_to_files):
counter += 1
print(counter)
if isfile(join(path_to_files, f)):
image = Image.open(join(path_to_files, f))
os.remove(join(path_to_files, f))
if "ct_10" in f:
while f[10] != "y":
f = f.replace("x", "x0")
else:
while f[9] != "y":
f = f.replace("x", "x0")
image.save(join(path_to_files, f))
# calculate mean and std
# cts = {
# "ct_2": "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_2_la.nii",
# "ct_3": "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_3_la.nii",
# "ct_4": "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_4_la.nii",
# "ct_5": "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_5_la.nii",
# "ct_6": "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_6_la.nii",
# "ct_7": "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_7_la.nii",
# "ct_8": "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_8_la.nii",
# "ct_9": "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_9_la.nii",
# "ct_1": "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_10_la.nii"
# }
#
# hparams = {
# "batch_size": 1,
# "learning_rate": 0.001,
# "epochs": 1
# }
#
# # load DRR filenames
# path_to_files = "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/"
#
# all_files = []
#
# for f in listdir(path_to_files): # list all files in directory
# if isfile(join(path_to_files, f)):
# all_files.append(f)
#
# all_files = sorted(all_files)
#
# # create train and val dictionaries
# all_data = {
# "path_files": path_to_files,
# "input": all_files,
# "target": cts
# }
#
# # create datasets
# all_data_set = HeartData(all_data)
#
# train_dataloader = torch.utils.data.DataLoader(all_data_set, batch_size=hparams["batch_size"], shuffle=True,
# num_workers=12)
#
# full = torch.zeros(1)
# for img, _ in train_dataloader:
# img = torch.reshape(img, (-1,))
# full = torch.cat((full, img), 0)
#
# mean = torch.mean(full)
# std = torch.std(full)
# mean: tensor(0.1278)
# std: tensor(0.1814)
# all_file = open('/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/all_files.txt', 'r')
# lines_all = all_file.readlines()
#
# names = []
# for line in lines_all:
# names.append(line.strip())
#
# for i in range(len(names)):
# # open and preprocess the input images
# image = Image.open(names[i])
# image = np.array(image)
# image = image.squeeze()
# image = image[0:480, 0:480]
# image = resize(image, output_shape=(64, 64), preserve_range=True)
# image = image.astype(np.int16)
# im_save = Image.fromarray(image)
# im_save = im_save.convert('L')
# im_save.save(names[i])
#
# if i % 10 == 0:
# print(i)
# open and preprocess the target volume
# cts = ["/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_2_la.nii",
# "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_3_la.nii",
# "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_4_la.nii",
# "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_5_la.nii",
# "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_6_la.nii",
# "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_7_la.nii",
# "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_8_la.nii",
# "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_9_la.nii",
# "/home/fryderyk/Desktop/Datasets/STACOM_SLAWT/STACOM_3d_64/cts/ct_10_la.nii"]
#
# for i in range(9):
# buffer = nib.load(cts[i])
# target = np.array(buffer.dataobj)
# target = target.astype('float32')
# print("target1: ", target.shape)
# # after loading there is a '1' dimension at the end, remove it
# target = target.squeeze()
# print("target2: ", target.shape)
#
# # crop to a cube
# target = reshape_nifti(target)
# print("target3: ", target.shape)
#
# target = nib.Nifti1Image(target, affine=np.eye(4))
# nib.save(target, cts[i])