[ebf7be]: / code / vowel-extract.py

Download this file

264 lines (217 with data), 10.5 kB

  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
"""
Utility for vowel extraction
"""
import numpy as np
import pandas as pd
import os
import csv
from skimage.segmentation import clear_border
from skimage.measure import label,regionprops, perimeter
from skimage.morphology import ball, disk, dilation, binary_erosion, remove_small_objects, erosion, closing, reconstruction, binary_closing
from skimage.filters import roberts, sobel
from scipy import ndimage as ndi
import pandas as pd
from glob import glob
from tqdm import tqdm
import SimpleITK as sitk
import scipy.misc
import matplotlib.pyplot as plt
import traceback
import random
from PIL import Image
from project_config import *
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from skimage import measure, feature
import time
def draw_circles(image,cands,origin,spacing):
#make empty matrix, which will be filled with the mask
RESIZE_SPACING = [1, 1, 1]
image_mask = np.zeros(image.shape)
#run over all the nodules in the lungs
for ca in cands.values:
#get middel x-,y-, and z-worldcoordinate of the nodule
radius = np.ceil(ca[4])/2
coord_x = ca[1]
coord_y = ca[2]
coord_z = ca[3]
image_coord = np.array((coord_z,coord_y,coord_x))
#determine voxel coordinate given the worldcoordinate
image_coord = world_2_voxel(image_coord,origin,spacing)
#determine the range of the nodule
noduleRange = seq(-radius, radius, RESIZE_SPACING[0])
#create the mask
for x in noduleRange:
for y in noduleRange:
for z in noduleRange:
coords = world_2_voxel(np.array((coord_z+z,coord_y+y,coord_x+x)),origin,spacing)
if (np.linalg.norm(image_coord-coords) * RESIZE_SPACING[0]) < radius:
image_mask[int(np.round(coords[0])),int(np.round(coords[1])),int(np.round(coords[2]))] = int(1)
return image_mask
def get_segmented_lungs(im, plot=False):
binary = im < 604
cleared = clear_border(binary)
label_image = label(cleared)
areas = [r.area for r in regionprops(label_image)]
areas.sort()
if len(areas) > 2:
for region in regionprops(label_image):
if region.area < areas[-2]:
for coordinates in region.coords:
label_image[coordinates[0], coordinates[1]] = 0
binary = label_image > 0
selem = disk(2)
binary = binary_erosion(binary, selem)
selem = disk(10)
binary = binary_closing(binary, selem)
edges = roberts(binary)
binary = ndi.binary_fill_holes(edges)
get_high_vals = binary == 0
im[get_high_vals] = 0
return im
def segment_lung_from_ct_scan(ct_scan):
return np.asarray([get_segmented_lungs(slice) for slice in ct_scan])
def load_itk(filename):
itkimage = sitk.ReadImage(filename)
ct_scan = sitk.GetArrayFromImage(itkimage)
origin = np.array(list(reversed(itkimage.GetOrigin())))
spacing = np.array(list(reversed(itkimage.GetSpacing())))
return ct_scan, origin, spacing
def world_2_voxel(world_coordinates, origin, spacing):
stretched_voxel_coordinates = np.absolute(world_coordinates - origin)
voxel_coordinates = stretched_voxel_coordinates / spacing
return voxel_coordinates
def voxel_2_world(voxel_coordinates, origin, spacing):
stretched_voxel_coordinates = voxel_coordinates * spacing
world_coordinates = stretched_voxel_coordinates + origin
return world_coordinates
def seq(start, stop, step=1):
n = int(round((stop - start)/float(step)))
if n > 1:
return([start + step*i for i in range(n+1)])
else:
return([])
def truncate_hu(image_array):
image_array[image_array > 400] = 0
image_array[image_array <-1000] = 0
# LUNA2016 data prepare ,second step: normalzation the HU
def normalazation(image_array):
max = image_array.max()
min = image_array.min()
image_array = (image_array-min)/(max-min) # float cannot apply the compute,or array error will occur
avg = image_array.mean()
image_array = image_array-avg
return image_array # a bug here, a array must be returned,directly appling function did't work
def extract(imagePath, cands, anno, fcount, normalization_output_path):
print('file %d pre-processing starts...' % fcount)
img, origin, spacing = load_itk(imagePath)
print('origin: ', origin)
print('spacing: ', spacing)
#calculate resize factor
RESIZE_SPACING = [1, 1, 1]
resize_factor = spacing / RESIZE_SPACING
new_real_shape = img.shape * resize_factor
new_shape = np.round(new_real_shape)
real_resize = new_shape / img.shape
new_spacing = spacing / real_resize
#resize image
lung_img = scipy.ndimage.interpolation.zoom(img, real_resize)
# Segment the lung structure
lung_img = lung_img + 1024
lung_mask = segment_lung_from_ct_scan(lung_img)
lung_img = lung_img - 1024
nodule_mask = draw_circles(lung_img,anno,origin,new_spacing)
#create nodule mask
lung_mask_512 = np.zeros((lung_mask.shape[0], 512, 512))
nodule_mask_512 = np.zeros((nodule_mask.shape[0], 512, 512))
original_shape = lung_img.shape
for z in range(lung_img.shape[0]):
offset = (512 - original_shape[1])
upper_offset = int(np.round(offset/2))
lower_offset = int(offset - upper_offset)
new_origin = voxel_2_world([-upper_offset,-lower_offset,0],origin,new_spacing)
lung_mask_512[z, upper_offset:-lower_offset,upper_offset:-lower_offset] = lung_mask[z,:,:] * nodule_mask[z,:,:]
print('file %d pre-processing over...' % fcount)
print('file %d cubic extract strats...' % fcount)
for node_idx, cur_row in cands.iterrows():
node_x = cur_row["coordX"]
node_y = cur_row["coordY"]
node_z = cur_row["coordZ"]
nodule_class = cur_row['class']
center = np.array([node_x, node_y, node_z]) # nodule center
v_center = world_2_voxel(center,origin,spacing) # nodule center in voxel space ( x,y,z ordering)
# every nodules saved into size of 20x20x6,30x30x10,40x40x26
if nodule_class == 0:
imgs_fake1 = np.zeros([26, 40, 40], dtype=np.float32)
try:
# these following imgs saves for plot
imgs_fake1 = lung_mask_512[int(v_center[0]-13):int(v_center[0]+13),int(v_center[1]-20):int(v_center[1]+20),int(v_center[2]-20):int(v_center[2]+20)]
if(np.max(imgs_fake1) != 0.0):
np.save(os.path.join(normalization_output_path, "%d_fake_size40x40.npy" % node_idx), imgs_fake1)
print('file %d nodule %d fake saves...' % (fcount, node_idx))
except Exception as e:
print('*')
elif nodule_class == 1:
imgs_true1 = np.zeros([26, 40, 40], dtype=np.float32)
try:
# these following imgs saves for plot
imgs_true1 = lung_mask_512[int(v_center[0]-13):int(v_center[0]+13),int(v_center[1]-20):int(v_center[1]+20),int(v_center[2]-20):int(v_center[2]+20)]
if(np.max(imgs_true1) != 0.0):
np.save(os.path.join(normalization_output_path, "%d_true_size40x40.npy" % node_idx), imgs_true1)
print('file %d nodule %d true saves...' % (fcount, node_idx))
except Exception as e:
print('*')
def get_filename(file_list, case):
for f in file_list:
if case in f:
return(f)
if __name__ =='__main__':
'''
base_dir = '/home/ubuntu/data/'
annatation_file = '/home/ubuntu/data/CSVFILES/annotations.csv'
candidate_file = '/home/ubuntu/data/CSVFILES/candidates.csv'
plot_output_path = '/home/ubuntu/data/output'
normalization_output_path = '/home/ubuntu/data/train-3d'
test_path = '/home/ubuntu/data/test-3d'
'''
print('test set starts processing...')
for i in range(9, 10):
print('subset %d preprocessing starts...' % i)
LUNA_SUBSET_PATH = base_dir + 'subset'+str(i)+'/'
FILE_LIST = glob(LUNA_SUBSET_PATH + '*.mhd')
cand_df_node = pd.read_csv(candidate_file)
cand_df_node["file"] = cand_df_node["seriesuid"].map(lambda file_name: get_filename(FILE_LIST, file_name))
cand_df_node = cand_df_node.dropna()
anno_df_node = pd.read_csv(annatation_file)
anno_df_node["file"] = anno_df_node["seriesuid"].map(lambda file_name: get_filename(FILE_LIST, file_name))
anno_df_node = anno_df_node.dropna()
# Looping over the image files
for fcount, img_file in enumerate(tqdm(FILE_LIST)):
cand_mini_df = cand_df_node[cand_df_node["file"]==img_file] # get all nodules associate with file
anno_mini_df = anno_df_node[anno_df_node["file"]==img_file]
if cand_mini_df.shape[0]>0 and anno_mini_df.shape[0]>0: # some files may not have a nodule--skipping those
extract(img_file, cand_mini_df, anno_mini_df, fcount, test_path)
print('subset %d preprocessing ends...' % i)
print('*'*20)
print('test set ends processing...')
print('-'*25)
print('train set starts processing...')
for i in range(0, 9):
print('subset %d preprocessing starts...' % i)
LUNA_SUBSET_PATH = base_dir + 'subset'+str(i)+'/'
FILE_LIST = glob(LUNA_SUBSET_PATH + '*.mhd')
cand_df_node = pd.read_csv(candidate_file)
cand_df_node["file"] = cand_df_node["seriesuid"].map(lambda file_name: get_filename(FILE_LIST, file_name))
cand_df_node = cand_df_node.dropna()
anno_df_node = pd.read_csv(annatation_file)
anno_df_node["file"] = anno_df_node["seriesuid"].map(lambda file_name: get_filename(FILE_LIST, file_name))
anno_df_node = anno_df_node.dropna()
# Looping over the image files
for fcount, img_file in enumerate(tqdm(FILE_LIST)):
cand_mini_df = cand_df_node[cand_df_node["file"]==img_file] # get all nodules associate with file
anno_mini_df = anno_df_node[anno_df_node["file"]==img_file]
if cand_mini_df.shape[0]>0 and anno_mini_df.shape[0]>0: # some files may not have a nodule--skipping those
extract(img_file, cand_mini_df, anno_mini_df, fcount, normalization_output_path)
print('subset %d preprocessing ends...' % i)
print('*'*20)
print('train set ends processing...')
print('-'*25)