-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.py
294 lines (239 loc) · 10 KB
/
prepare.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import os
import shutil
import numpy as np
from config_training import config
from scipy.io import loadmat
import numpy as np
import h5py
import pandas
import scipy
from scipy.ndimage.interpolation import zoom
from skimage import measure
import SimpleITK as sitk
from scipy.ndimage.morphology import binary_dilation,generate_binary_structure
from skimage.morphology import convex_hull_image
import pandas
from multiprocessing import Pool
from functools import partial
import sys
import warnings
import pdb
def resample(imgs, spacing, new_spacing,order=2):
if len(imgs.shape)==3:
new_shape = np.round(imgs.shape * spacing / new_spacing)
true_spacing = spacing * imgs.shape / new_shape
resize_factor = new_shape / imgs.shape
imgs = zoom(imgs, resize_factor, mode = 'nearest',order=order)
return imgs, true_spacing
elif len(imgs.shape)==4:
n = imgs.shape[-1]
newimg = []
for i in range(n):
slice = imgs[:,:,:,i]
newslice,true_spacing = resample(slice,spacing,new_spacing)
newimg.append(newslice)
newimg=np.transpose(np.array(newimg),[1,2,3,0])
return newimg,true_spacing
else:
raise ValueError('wrong shape')
def worldToVoxelCoord(worldCoord, origin, spacing):
stretchedVoxelCoord = np.absolute(worldCoord - origin)
voxelCoord = stretchedVoxelCoord / spacing
return voxelCoord
def load_itk_image(filename):
with open(filename) as f:
contents = f.readlines()
line = [k for k in contents if k.startswith('TransformMatrix')][0]
transformM = np.array(line.split(' = ')[1].split(' ')).astype('float')
transformM = np.round(transformM)
if np.any( transformM!=np.array([1,0,0, 0, 1, 0, 0, 0, 1])):
isflip = True
else:
isflip = False
itkimage = sitk.ReadImage(filename)
numpyImage = sitk.GetArrayFromImage(itkimage)
numpyOrigin = np.array(list(reversed(itkimage.GetOrigin())))
numpySpacing = np.array(list(reversed(itkimage.GetSpacing())))
return numpyImage, numpyOrigin, numpySpacing,isflip
def process_mask(mask):
convex_mask = np.copy(mask)
for i_layer in range(convex_mask.shape[0]):
mask1 = np.ascontiguousarray(mask[i_layer])
if np.sum(mask1)>0:
mask2 = convex_hull_image(mask1)
if np.sum(mask2)>1.5*np.sum(mask1):
mask2 = mask1
else:
mask2 = mask1
convex_mask[i_layer] = mask2
struct = generate_binary_structure(3,1)
dilatedMask = binary_dilation(convex_mask,structure=struct,iterations=10)
return dilatedMask
def lumTrans(img):
lungwin = np.array([-1200.,600.])
newimg = (img-lungwin[0])/(lungwin[1]-lungwin[0])
newimg[newimg<0]=0
newimg[newimg>1]=1
newimg = (newimg*255).astype('uint8')
return newimg
def savenpy_luna(id, annos, filelist, luna_segment, luna_data,savepath):
islabel = True
isClean = True
resolution = np.array([1,1,1])
# resolution = np.array([2,2,2])
name = filelist[id]
sliceim,origin,spacing,isflip = load_itk_image(os.path.join(luna_data,name+'.mhd'))
Mask,origin,spacing,isflip = load_itk_image(os.path.join(luna_segment,name+'.mhd'))
if isflip:
Mask = Mask[:,::-1,::-1]
newshape = np.round(np.array(Mask.shape)*spacing/resolution).astype('int')
m1 = Mask==3
m2 = Mask==4
Mask = m1+m2
xx,yy,zz= np.where(Mask)
box = np.array([[np.min(xx),np.max(xx)],[np.min(yy),np.max(yy)],[np.min(zz),np.max(zz)]])
box = box*np.expand_dims(spacing,1)/np.expand_dims(resolution,1)
box = np.floor(box).astype('int')
margin = 5
extendbox = np.vstack([np.max([[0,0,0],box[:,0]-margin],0),np.min([newshape,box[:,1]+2*margin],axis=0).T]).T
this_annos = np.copy(annos[annos[:,0]==float(name)])
if isClean:
convex_mask = m1
dm1 = process_mask(m1)
dm2 = process_mask(m2)
dilatedMask = dm1+dm2
Mask = m1+m2
extramask = dilatedMask ^ Mask
bone_thresh = 210
pad_value = 170
if isflip:
sliceim = sliceim[:,::-1,::-1]
print('flip!')
sliceim = lumTrans(sliceim)
sliceim = sliceim*dilatedMask+pad_value*(1-dilatedMask).astype('uint8')
bones = (sliceim*extramask)>bone_thresh
sliceim[bones] = pad_value
sliceim1,_ = resample(sliceim,spacing,resolution,order=1)
sliceim2 = sliceim1[extendbox[0,0]:extendbox[0,1],
extendbox[1,0]:extendbox[1,1],
extendbox[2,0]:extendbox[2,1]]
sliceim = sliceim2[np.newaxis,...]
np.save(os.path.join(savepath, name+'_clean.npy'), sliceim)
np.save(os.path.join(savepath, name+'_spacing.npy'), spacing)
np.save(os.path.join(savepath, name+'_extendbox.npy'), extendbox)
np.save(os.path.join(savepath, name+'_origin.npy'), origin)
np.save(os.path.join(savepath, name+'_mask.npy'), Mask)
if islabel:
this_annos = np.copy(annos[annos[:,0]==float(name)])
label = []
if len(this_annos)>0:
for c in this_annos:
pos = worldToVoxelCoord(c[1:4][::-1],origin=origin,spacing=spacing)
if isflip:
pos[1:] = Mask.shape[1:3]-pos[1:]
label.append(np.concatenate([pos,[c[4]/spacing[1]]]))
label = np.array(label)
if len(label)==0:
label2 = np.array([[0,0,0,0]])
else:
label2 = np.copy(label).T
label2[:3] = label2[:3]*np.expand_dims(spacing,1)/np.expand_dims(resolution,1)
label2[3] = label2[3]*spacing[1]/resolution[1]
label2[:3] = label2[:3]-np.expand_dims(extendbox[:,0],1)
label2 = label2[:4].T
np.save(os.path.join(savepath,name+'_label.npy'), label2)
print(name)
def preprocess_luna():
luna_segment = config['luna_segment']
savepath = config['preprocess_result_path']
luna_data = config['luna_data']
luna_label = config['luna_label']
finished_flag = '.flag_preprocessluna'
print('starting preprocessing luna')
if not os.path.exists(finished_flag):
filelist = [f.split('.mhd')[0] for f in os.listdir(luna_data) if f.endswith('.mhd') ]
annos = np.array(pandas.read_csv(luna_label))
if not os.path.exists(savepath):
os.mkdir(savepath)
pool = Pool()
partial_savenpy_luna = partial(savenpy_luna,annos=annos,filelist=filelist,
luna_segment=luna_segment,luna_data=luna_data,savepath=savepath)
N = len(filelist)
#savenpy(1)
_=pool.map(partial_savenpy_luna,range(N))
pool.close()
pool.join()
print('end preprocessing luna')
f= open(finished_flag,"w+")
def prepare_luna():
print('start changing luna name')
luna_raw = config['luna_raw']
luna_abbr = config['luna_abbr']
luna_data = config['luna_data']
luna_segment = config['luna_segment']
finished_flag = '.flag_prepareluna'
if not os.path.exists(finished_flag):
subsetdirs = [os.path.join(luna_raw,f) for f in os.listdir(luna_raw) if f.startswith('subset') and os.path.isdir(os.path.join(luna_raw,f))]
if not os.path.exists(luna_data):
os.mkdir(luna_data)
# allnames = []
# for d in subsetdirs:
# files = os.listdir(d)
# names = [f[:-4] for f in files if f.endswith('mhd')]
# allnames = allnames + names
# allnames = np.array(allnames)
# allnames = np.sort(allnames)
# ids = np.arange(len(allnames)).astype('str')
# ids = np.array(['0'*(3-len(n))+n for n in ids])
# pds = pandas.DataFrame(np.array([ids,allnames]).T)
# namelist = list(allnames)
abbrevs = np.array(pandas.read_csv(config['luna_abbr'],header=None))
namelist = list(abbrevs[:,1])
ids = abbrevs[:,0]
for d in subsetdirs:
files = os.listdir(d)
files.sort()
for f in files:
name = f[:-4]
id = ids[namelist.index(name)]
filename = '0'*(3-len(str(id)))+str(id)
shutil.move(os.path.join(d,f),os.path.join(luna_data,filename+f[-4:]))
print(os.path.join(luna_data,str(id)+f[-4:]))
files = [f for f in os.listdir(luna_data) if f.endswith('mhd')]
for file in files:
with open(os.path.join(luna_data,file),'r') as f:
content = f.readlines()
id = file.split('.mhd')[0]
filename = '0'*(3-len(str(id)))+str(id)
content[-1]='ElementDataFile = '+filename+'.raw\n'
print(content[-1])
with open(os.path.join(luna_data,file),'w') as f:
f.writelines(content)
seglist = os.listdir(luna_segment)
for f in seglist:
if f.endswith('.mhd'):
name = f[:-4]
lastfix = f[-4:]
else:
name = f[:-5]
lastfix = f[-5:]
if name in namelist:
id = ids[namelist.index(name)]
filename = '0'*(3-len(str(id)))+str(id)
shutil.move(os.path.join(luna_segment,f),os.path.join(luna_segment,filename+lastfix))
print(os.path.join(luna_segment,filename+lastfix))
files = [f for f in os.listdir(luna_segment) if f.endswith('mhd')]
for file in files:
with open(os.path.join(luna_segment,file),'r') as f:
content = f.readlines()
id = file.split('.mhd')[0]
filename = '0'*(3-len(str(id)))+str(id)
content[-1]='ElementDataFile = '+filename+'.zraw\n'
print(content[-1])
with open(os.path.join(luna_segment,file),'w') as f:
f.writelines(content)
print('end changing luna name')
f= open(finished_flag,"w+")
if __name__=='__main__':
prepare_luna()
preprocess_luna()