示例#1
0
        def tmp_create_rotated_train_data(ignore_me = None):
            # if i don't do this then each worker from the pool has the same random state -> same rotation = bad!
            np.random.seed()
            # randomly rotate coordinates using rotation matrix
            rotation_matrix = create_random_rotation()
            slice_coords_0 = np.dot(default_slice_0, rotation_matrix)

            # create orthogonal slices
            slice_coords_1 = np.dot(np.dot(default_slice_0, create_matrix_rotation_y(-np.pi/2.)), rotation_matrix)
            slice_coords_2 = np.dot(np.dot(default_slice_0, create_matrix_rotation_z(-np.pi/2.)), rotation_matrix)

            # offset coordinates to center
            slice_coords_0 += center
            slice_coords_1 += center
            slice_coords_2 += center

            slice_t1km_0 = map_coordinates(t1km_image, slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_1 = map_coordinates(t1km_image, slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_2 = map_coordinates(t1km_image, slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_flair_0 = map_coordinates(flair_img, slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_flair_1 = map_coordinates(flair_img, slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_flair_2 = map_coordinates(flair_img, slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_adc_0 = map_coordinates(adc_img, slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_adc_1 = map_coordinates(adc_img, slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_adc_2 = map_coordinates(adc_img, slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_cbv_0 = map_coordinates(cbv_img, slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_cbv_1 = map_coordinates(cbv_img, slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_cbv_2 = map_coordinates(cbv_img, slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_donwsampled_0 = map_coordinates(t1km_downsampled, (default_slice_0 + center_downsampled).transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_donwsampled_1 = map_coordinates(t1km_downsampled, (default_slice_1 + center_downsampled).transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_donwsampled_2 = map_coordinates(t1km_downsampled, (default_slice_2 + center_downsampled).transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_seg_0 = np.round(map_coordinates(seg_combined.astype(float), slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)).astype(np.int32)
            slice_seg_1 = np.round(map_coordinates(seg_combined.astype(float), slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)).astype(np.int32)
            slice_seg_2 = np.round(map_coordinates(seg_combined.astype(float), slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)).astype(np.int32)
            slice_seg_0[slice_seg_0 < 0] = 0
            slice_seg_0[slice_seg_0 > 3] = 3
            slice_seg_1[slice_seg_1 < 0] = 0
            slice_seg_1[slice_seg_1 > 3] = 3
            slice_seg_2[slice_seg_2 < 0] = 0
            slice_seg_2[slice_seg_2 > 3] = 3

            return (slice_t1km_0,
                    slice_t1km_1,
                    slice_t1km_2,
                    slice_flair_0,
                    slice_flair_1,
                    slice_flair_2,
                    slice_adc_0,
                    slice_adc_1,
                    slice_adc_2,
                    slice_cbv_0,
                    slice_cbv_1,
                    slice_cbv_2,
                    slice_t1km_donwsampled_0,
                    slice_t1km_donwsampled_1,
                    slice_t1km_donwsampled_2,
                    slice_seg_0,
                    slice_seg_1,
                    slice_seg_2)
        def tmp_create_rotated_train_data(ignore_me = None):
            # if i don't do this then each worker from the pool has the same random state -> same rotation = bad!
            np.random.seed()
            # randomly rotate coordinates using rotation matrix
            rotation_matrix = create_random_rotation()
            slice_coords_0 = np.dot(default_slice_0, rotation_matrix)

            # create orthogonal slices
            slice_coords_1 = np.dot(np.dot(default_slice_0, create_matrix_rotation_y(-np.pi/2.)), rotation_matrix)
            slice_coords_2 = np.dot(np.dot(default_slice_0, create_matrix_rotation_z(-np.pi/2.)), rotation_matrix)

            # offset coordinates to center
            slice_coords_0 += center
            slice_coords_1 += center
            slice_coords_2 += center

            slice_t1km_0 = map_coordinates(t1km_image, slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_1 = map_coordinates(t1km_image, slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_2 = map_coordinates(t1km_image, slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_flair_0 = map_coordinates(flair_img, slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_flair_1 = map_coordinates(flair_img, slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_flair_2 = map_coordinates(flair_img, slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_adc_0 = map_coordinates(adc_img, slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_adc_1 = map_coordinates(adc_img, slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_adc_2 = map_coordinates(adc_img, slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_cbv_0 = map_coordinates(cbv_img, slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_cbv_1 = map_coordinates(cbv_img, slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_cbv_2 = map_coordinates(cbv_img, slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_donwsampled_0 = map_coordinates(t1km_downsampled, (default_slice_0 + center_downsampled).transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_donwsampled_1 = map_coordinates(t1km_downsampled, (default_slice_1 + center_downsampled).transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_t1km_donwsampled_2 = map_coordinates(t1km_downsampled, (default_slice_2 + center_downsampled).transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)
            slice_seg_0 = np.round(map_coordinates(seg_combined.astype(float), slice_coords_0.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)).astype(np.int32)
            slice_seg_1 = np.round(map_coordinates(seg_combined.astype(float), slice_coords_1.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)).astype(np.int32)
            slice_seg_2 = np.round(map_coordinates(seg_combined.astype(float), slice_coords_2.transpose(), None, 3, "nearest").reshape(PATCH_SIZE, PATCH_SIZE)).astype(np.int32)
            slice_seg_0[slice_seg_0 < 0] = 0
            slice_seg_0[slice_seg_0 > 3] = 3
            slice_seg_1[slice_seg_1 < 0] = 0
            slice_seg_1[slice_seg_1 > 3] = 3
            slice_seg_2[slice_seg_2 < 0] = 0
            slice_seg_2[slice_seg_2 > 3] = 3

            return (slice_t1km_0,
                    slice_t1km_1,
                    slice_t1km_2,
                    slice_flair_0,
                    slice_flair_1,
                    slice_flair_2,
                    slice_adc_0,
                    slice_adc_1,
                    slice_adc_2,
                    slice_cbv_0,
                    slice_cbv_1,
                    slice_cbv_2,
                    slice_t1km_donwsampled_0,
                    slice_t1km_donwsampled_1,
                    slice_t1km_donwsampled_2,
                    slice_seg_0,
                    slice_seg_1,
                    slice_seg_2)
示例#3
0

n_negative_train = 0
n_negative_val = 0
n_positive_train = 0
n_positive_val = 0


path = "/home/fabian/datasets/Hirntumor_von_David/"
subdirs = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
subdirs.sort()
voxels_in_patch = PATCH_SIZE**2

default_slice_0 = create_default_slice()
default_slice_1 = np.dot(default_slice_0, create_matrix_rotation_y(-np.pi/2.))
default_slice_2 = np.dot(default_slice_0, create_matrix_rotation_z(-np.pi/2.))

def correct_nans(image):
    t1_image_corr = np.array(image)
    isnan_coords = np.where(np.isnan(image))
    if len(isnan_coords[0]) > 0:
        for coord in zip(isnan_coords[0], isnan_coords[1], isnan_coords[2]):
            coord = list(coord)
            region = image[coord[0]-5 : coord[0]+5, coord[1]-5 : coord[1]+5, coord[2]-5 : coord[2]+5]
            t1_image_corr[tuple(coord)] = np.max(region[~np.isnan(region)])
    return t1_image_corr

val_dirs = ['001', '002', '004', '005', '006', '007', '008', '009', '011', '012', '013', '014', '015', '016']
labels = []
valid_patient_ids = []
for curr_dir in subdirs:
n_positive_val_MGMT = 0
n_negative_train_EGFR = 0
n_negative_val_EGFR = 0
n_positive_train_EGFR = 0
n_positive_val_EGFR = 0

path = "/home/fabian/datasets/Hirntumor_von_David/"
subdirs = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
subdirs.sort()
voxels_in_patch = PATCH_SIZE**2

default_slice_0 = create_default_slice()
default_slice_1 = np.dot(default_slice_0,
                         create_matrix_rotation_y(-np.pi / 2.))
default_slice_2 = np.dot(default_slice_0,
                         create_matrix_rotation_z(-np.pi / 2.))


def correct_nans(image):
    t1_image_corr = np.array(image)
    isnan_coords = np.where(np.isnan(image))
    if len(isnan_coords[0]) > 0:
        for coord in zip(isnan_coords[0], isnan_coords[1], isnan_coords[2]):
            coord = list(coord)
            region = image[coord[0] - 5:coord[0] + 5,
                           coord[1] - 5:coord[1] + 5,
                           coord[2] - 5:coord[2] + 5]
            t1_image_corr[tuple(coord)] = np.max(region[~np.isnan(region)])
    return t1_image_corr

n_negative_val_MGMT = 0
n_positive_train_MGMT = 0
n_positive_val_MGMT = 0
n_negative_train_EGFR = 0
n_negative_val_EGFR = 0
n_positive_train_EGFR = 0
n_positive_val_EGFR = 0

path = "/home/fabian/datasets/Hirntumor_von_David/"
subdirs = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
subdirs.sort()
voxels_in_patch = PATCH_SIZE**2

default_slice_0 = create_default_slice()
default_slice_1 = np.dot(default_slice_0, create_matrix_rotation_y(-np.pi/2.))
default_slice_2 = np.dot(default_slice_0, create_matrix_rotation_z(-np.pi/2.))

def correct_nans(image):
    t1_image_corr = np.array(image)
    isnan_coords = np.where(np.isnan(image))
    if len(isnan_coords[0]) > 0:
        for coord in zip(isnan_coords[0], isnan_coords[1], isnan_coords[2]):
            coord = list(coord)
            region = image[coord[0]-5 : coord[0]+5, coord[1]-5 : coord[1]+5, coord[2]-5 : coord[2]+5]
            t1_image_corr[tuple(coord)] = np.max(region[~np.isnan(region)])
    return t1_image_corr

val_dirs = ['001', '002', '004', '005', '006', '007', '008', '009', '011', '012', '013', '014', '015', '016']
labels = []
valid_patient_ids = []
for curr_dir in subdirs: