def create_slices(imagePath, maskPath, cads):
    # if os.path.isfile(imagePath.replace('original', SAVE_FOLDER_image)) == False:
    img, origin, spacing = load_itk(imagePath)
    mask, _, _ = load_itk(maskPath)

    # determine the cads in a lung from csv file
    imageName = os.path.split(imagePath)[1].replace('.mhd', '')
    image_cads = cads[cads['seriesuid'] == imageName]

    # calculate resize factor
    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 & resize lung-mask
    lung_img = scipy.ndimage.interpolation.zoom(img, real_resize)
    lung_mask = scipy.ndimage.interpolation.zoom(mask, real_resize)
    # print lung_mask.shape()

    # lung_mask to 0-1
    lung_mask[lung_mask > 0] = 1

    # create nodule mask
    nodule_mask = draw_circles(lung_img, image_cads, origin, new_spacing)

    # Determine which slices contain nodules
    sliceList = []
    for z in range(nodule_mask.shape[2]):
        if np.sum(nodule_mask[:, :, z]) > 0:
            sliceList.append(z)

    # save slices
    for z in sliceList:
        lung_slice = lung_img[:, :, z]
        lung_mask_slice = lung_mask[:, :, z]
        nodule_mask_slice = nodule_mask[:, :, z]

        # padding to 512x512
        original_shape = lung_img.shape
        lung_slice_512 = np.zeros((512, 512)) - 3000
        lung_mask_slice_512 = np.zeros((512, 512))
        nodule_mask_slice_512 = np.zeros((512, 512))

        offset = (512 - original_shape[1])
        upper_offset = np.round(offset / 2)
        lower_offset = offset - upper_offset

        new_origin = voxel_2_world([-upper_offset, -lower_offset, 0], origin,
                                   new_spacing)

        lung_slice_512[upper_offset:-lower_offset,
                       upper_offset:-lower_offset] = lung_slice
        lung_mask_slice_512[upper_offset:-lower_offset,
                            upper_offset:-lower_offset] = lung_mask_slice
        nodule_mask_slice_512[upper_offset:-lower_offset,
                              upper_offset:-lower_offset] = nodule_mask_slice

        # save the lung slice
        savePath = imagePath.replace('original_lungs', SAVE_FOLDER_image)
        f = gzip.open(savePath.replace('.mhd', '_slice{}.pkl.gz'.format(z)),
                      'wb')
        pickle.dump(lung_slice_512, f, protocol=-1)
        pickle.dump(new_spacing, f, protocol=-1)
        pickle.dump(new_origin, f, protocol=-1)
        f.close()

        savePath = imagePath.replace('original_lungs', SAVE_FOLDER_lung_mask)
        f = gzip.open(savePath.replace('.mhd', '_slice{}.pkl.gz'.format(z)),
                      'wb')
        pickle.dump(lung_mask_slice_512, f, protocol=-1)
        pickle.dump(new_spacing, f, protocol=-1)
        pickle.dump(new_origin, f, protocol=-1)
        f.close()

        savePath = imagePath.replace('original_lungs', SAVE_FOLDER_nodule_mask)
        f = gzip.open(savePath.replace('.mhd', '_slice{}.pkl.gz'.format(z)),
                      'wb')
        pickle.dump(nodule_mask_slice_512, f, protocol=-1)
        pickle.dump(new_spacing, f, protocol=-1)
        pickle.dump(new_origin, f, protocol=-1)
        f.close()
def create_slices(imagePath, maskPath, cads):
    #if os.path.isfile(imagePath.replace('original',SAVE_FOLDER_image)) == False:
    img, origin, spacing = load_itk(imagePath)
    mask, _, _ = load_itk(maskPath)

    #determine the cads in a lung from csv file
    imageName = os.path.split(imagePath)[1].replace('.mhd','')
    image_cads = cads[cads['seriesuid'] == imageName]

    #calculate resize factor
    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 & resize lung-mask
    lung_img = scipy.ndimage.interpolation.zoom(img, real_resize)
    lung_mask = scipy.ndimage.interpolation.zoom(mask, real_resize)
    #print lung_mask.shape()

    #lung_mask to 0-1
    lung_mask[lung_mask >0] = 1
    
    #create nodule mask
    nodule_mask = draw_circles(lung_img,image_cads,origin,new_spacing)

    #Determine which slices contain nodules
    sliceList = []
    for z in range(nodule_mask.shape[2]):
        if np.sum(nodule_mask[:,:,z]) > 0:
    	    sliceList.append(z)

    #save slices
    for z in sliceList:
        lung_slice = lung_img[:,:,z]
        lung_mask_slice = lung_mask[:,:,z]
        nodule_mask_slice = nodule_mask[:,:,z]
        
        #padding to 512x512
        original_shape = lung_img.shape
        lung_slice_512 = np.zeros((512,512)) - 3000
        lung_mask_slice_512 = np.zeros((512,512))
        nodule_mask_slice_512 = np.zeros((512,512))

        offset = (512 - original_shape[1])
        upper_offset = np.round(offset/2)
        lower_offset = offset - upper_offset

        new_origin = voxel_2_world([-upper_offset,-lower_offset,0],origin,new_spacing)

        lung_slice_512[upper_offset:-lower_offset,upper_offset:-lower_offset] = lung_slice
        lung_mask_slice_512[upper_offset:-lower_offset,upper_offset:-lower_offset] = lung_mask_slice
        nodule_mask_slice_512[upper_offset:-lower_offset,upper_offset:-lower_offset] = nodule_mask_slice

        #save the lung slice
        savePath = imagePath.replace('original_lungs',SAVE_FOLDER_image)
        file = gzip.open(savePath.replace('.mhd','_slice{}.pkl.gz'.format(z)),'wb')
        pickle.dump(lung_slice_512,file,protocol=-1)
        pickle.dump(new_spacing,file, protocol=-1)
        pickle.dump(new_origin,file, protocol=-1)
        file.close()

        savePath = imagePath.replace('original_lungs',SAVE_FOLDER_lung_mask)
        file = gzip.open(savePath.replace('.mhd','_slice{}.pkl.gz'.format(z)),'wb')
        pickle.dump(lung_mask_slice_512,file,protocol=-1)
        pickle.dump(new_spacing,file, protocol=-1)
        pickle.dump(new_origin,file, protocol=-1)
        file.close()

        savePath = imagePath.replace('original_lungs',SAVE_FOLDER_nodule_mask)
        file = gzip.open(savePath.replace('.mhd','_slice{}.pkl.gz'.format(z)),'wb')
        pickle.dump(nodule_mask_slice_512,file,protocol=-1)
        pickle.dump(new_spacing,file, protocol=-1)
        pickle.dump(new_origin,file, protocol=-1)
        file.close()