예제 #1
0
    def scale_input_to_unet_shape(img4d, dataset, resolution="1.25mm"):
        '''
        Scale input image to right isotropic resolution and pad/cut image to make it square to fit UNet input shape

        :param img4d: (x, y, z, userdefined)  (userdefined could be gradients or classes)
        :param resolution: "1.25mm" / "2mm" / "2.5mm"     results in UNet input shape of (144,144,144) or (80,80,80)
        :return: img with dim 1mm: (144,144,144,none) or 2mm: (80,80,80,none) or 2.5mm: (80,80,80,none)
                    (note: 2.5mm padded with more zeros to reach 80,80,80)
        '''

        if resolution == "1.25mm":
            if dataset == "HCP":  # (145,174,145)
                # no resize needed
                return img4d[1:, 15:159, 1:]  # (144,144,144)
            elif dataset == "HCP_32g":  # (73,87,73)
                # return img4d[1:, 15:159, 1:]  # (144,144,144) #OLD when HCP_32g was still 125mm
                img4d = ImgUtils.resize_first_three_dims(img4d, zoom=2)  # (146,174,146,none)
                img4d = img4d[:-1,:,:-1]  #remove one voxel that came from upsampling   #(145,174,145)
                return img4d[1:, 15:159, 1:]  # (144,144,144)
            elif dataset == "TRACED":  # (78,93,75)
                raise ValueError("resolution '1.25mm' not supported for dataset 'TRACED'")

        elif resolution == "2mm":
            if dataset == "HCP":  # (145,174,145)
                img4d = ImgUtils.resize_first_three_dims(img4d, zoom=0.62)  # (90,108,90)
                return img4d[5:85, 14:94, 5:85, :]  # (80,80,80)
            elif dataset == "HCP_32g":  # (145,174,145)
                img4d = ImgUtils.resize_first_three_dims(img4d, zoom=0.62)  # (90,108,90)
                return img4d[5:85, 14:94, 5:85, :]  # (80,80,80)
            elif dataset == "HCP_2mm":  # (90,108,90)
                # no resize needed
                return img4d[5:85, 14:94, 5:85, :]  # (80,80,80)
            elif dataset == "TRACED":  # (78,93,75)
                raise ValueError("resolution '2mm' not supported for dataset 'TRACED'")

        elif resolution == "2.5mm":
            if dataset == "HCP":  # (145,174,145)
                img4d = ImgUtils.resize_first_three_dims(img4d, zoom=0.5)  # (73,87,73,none)
                bg = np.zeros((80, 80, 80, img4d.shape[3])).astype(img4d.dtype)
                bg = bg + img4d[0,0,0,:] #make bg have same value as bg from original img  (this adds last dim of img4d to last dim of bg)
                bg[4:77, :, 4:77] = img4d[:, 4:84, :, :]
                return bg  # (80,80,80)
            elif dataset == "HCP_2.5mm":  # (73,87,73,none)
                #no resize needed
                bg = np.zeros((80, 80, 80, img4d.shape[3])).astype(img4d.dtype)
                bg = bg + img4d[0,0,0,:] #make bg have same value as bg from original img  (this adds last dim of img4d to last dim of bg)
                bg[4:77, :, 4:77] = img4d[:, 4:84, :, :]
                return bg  # (80,80,80)
            elif dataset == "HCP_32g":  # (73,87,73,none)
                bg = np.zeros((80, 80, 80, img4d.shape[3])).astype(img4d.dtype)
                bg = bg + img4d[0, 0, 0, :]  # make bg have same value as bg from original img  (this adds last dim of img4d to last dim of bg)
                bg[4:77, :, 4:77] = img4d[:, 4:84, :, :]
                return bg  # (80,80,80)
            elif dataset == "TRACED":  # (78,93,75)
                # no resize needed
                bg = np.zeros((80, 80, 80, img4d.shape[3])).astype(img4d.dtype)
                bg = bg + img4d[0, 0, 0, :]  # make bg have same value as bg from original img
                bg[1:79, :, 3:78, :] = img4d[:, 7:87, :, :]
                return bg  # (80,80,80)
예제 #2
0
    def create_one_3D_file():
        '''
        Create one big file which contains all 3D Images (not slices).
        '''

        class HP:
            DATASET = "HCP"
            RESOLUTION = "1.25mm"
            FEATURES_FILENAME = "270g_125mm_peaks"
            LABELS_TYPE = np.int16
            DATASET_FOLDER = "HCP"

        data_all = []
        seg_all = []

        print("\n\nProcessing Data...")
        for s in get_all_subjects():
            print("processing data subject {}".format(s))
            data = nib.load(join(C.HOME, HP.DATASET_FOLDER, s, HP.FEATURES_FILENAME + ".nii.gz")).get_data()
            data = np.nan_to_num(data)
            data = DatasetUtils.scale_input_to_unet_shape(data, HP.DATASET, HP.RESOLUTION)
        data_all.append(np.array(data))
        np.save("data.npy", data_all)
        del data_all  # free memory

        print("\n\nProcessing Segs...")
        for s in get_all_subjects():
            print("processing seg subject {}".format(s))
            seg = ImgUtils.create_multilabel_mask(HP, s, labels_type=HP.LABELS_TYPE)
            if HP.RESOLUTION == "2.5mm":
                seg = ImgUtils.resize_first_three_dims(seg, order=0, zoom=0.5)
            seg = DatasetUtils.scale_input_to_unet_shape(seg, HP.DATASET, HP.RESOLUTION)
        seg_all.append(np.array(seg))
        print("SEG TYPE: {}".format(seg_all.dtype))
        np.save("seg.npy", seg_all)
예제 #3
0
    def cut_and_scale_img_back_to_original_img(data, t):
        '''
        Undo the transformations done with pad_and_scale_img_to_square_img

        data: 3D or 4D image
        t: transformation dict
        '''

        # Back to old size
        # use order=0, otherwise image values of a DWI will be quite different after downsampling and upsampling
        if len(data.shape) == 3:
            new_data = ndimage.zoom(data, (1. / t["zoom"]), order=0)
        elif len(data.shape) == 4:
            new_data = ImgUtils.resize_first_three_dims(data, order=0, zoom=(1. / t["zoom"]))

        x_residual = 0
        y_residual = 0
        z_residual = 0

        # check if has 0.5 residual -> we have to cut 1 pixel more at the end
        if t["pad_x"] - int(t["pad_x"]) == 0.5:
            x_residual = 1
        if t["pad_y"] - int(t["pad_y"]) == 0.5:
            y_residual = 1
        if t["pad_z"] - int(t["pad_z"]) == 0.5:
            z_residual = 1

        # Cut padding
        shape = new_data.shape
        new_data = new_data[int(t["pad_x"]): shape[0] - int(t["pad_x"]) - x_residual,
                   int(t["pad_y"]): shape[1] - int(t["pad_y"]) - y_residual,
                   int(t["pad_z"]): shape[2] - int(t["pad_z"]) - z_residual]

        return new_data
예제 #4
0
    def cut_and_scale_img_back_to_original_img(data, t):
        '''
        Undo the transformations done with pad_and_scale_img_to_square_img

        data: 3D or 4D image
        t: transformation dict
        '''
        nr_dims = len(data.shape)
        assert (nr_dims >= 3 and nr_dims <= 4)

        # Back to old size
        # use order=0, otherwise image values of a DWI will be quite different after downsampling and upsampling
        if nr_dims == 3:
            new_data = ndimage.zoom(data, (1. / t["zoom"]), order=0)
        elif nr_dims == 4:
            new_data = ImgUtils.resize_first_three_dims(data, order=0, zoom=(1. / t["zoom"]))

        x_residual = 0
        y_residual = 0
        z_residual = 0

        # check if has 0.5 residual -> we have to cut 1 pixel more at the end
        if t["pad_x"] - int(t["pad_x"]) == 0.5:
            x_residual = 1
        if t["pad_y"] - int(t["pad_y"]) == 0.5:
            y_residual = 1
        if t["pad_z"] - int(t["pad_z"]) == 0.5:
            z_residual = 1

        # Cut padding
        shape = new_data.shape
        new_data = new_data[int(t["pad_x"]): shape[0] - int(t["pad_x"]) - x_residual,
                            int(t["pad_y"]): shape[1] - int(t["pad_y"]) - y_residual,
                            int(t["pad_z"]): shape[2] - int(t["pad_z"]) - z_residual]
        return new_data
예제 #5
0
파일: Slicer.py 프로젝트: silongGG/TractSeg
    def save_fusion_nifti_as_npy():

        #Can leave this always the same (for 270g and 32g)
        class HP:
            DATASET = "HCP"
            RESOLUTION = "1.25mm"
            FEATURES_FILENAME = "270g_125mm_peaks"
            LABELS_TYPE = np.int16
            LABELS_FILENAME = "bundle_masks"
            DATASET_FOLDER = "HCP"

        #change this for 270g and 32g
        DIFFUSION_FOLDER = "32g_25mm"

        subjects = get_all_subjects()
        # fold0 = ['687163', '685058', '683256', '680957', '679568', '677968', '673455', '672756', '665254', '654754', '645551', '644044', '638049', '627549', '623844', '622236', '620434', '613538', '601127', '599671', '599469']
        # fold1 = ['992774', '991267', '987983', '984472', '983773', '979984', '978578', '965771', '965367', '959574', '958976', '957974', '951457', '932554', '930449', '922854', '917255', '912447', '910241', '907656', '904044']
        # fold2 = ['901442', '901139', '901038', '899885', '898176', '896879', '896778', '894673', '889579', '887373', '877269', '877168', '872764', '872158', '871964', '871762', '865363', '861456', '859671', '857263', '856766']
        # fold3 = ['849971', '845458', '837964', '837560', '833249', '833148', '826454', '826353', '816653', '814649', '802844', '792766', '792564', '789373', '786569', '784565', '782561', '779370', '771354', '770352', '765056']
        # fold4 = ['761957', '759869', '756055', '753251', '751348', '749361', '748662', '748258', '742549', '734045', '732243', '729557', '729254', '715647', '715041', '709551', '705341', '704238', '702133', '695768', '690152']
        # subjects = fold2 + fold3 + fold4

        # subjects = ['654754', '645551', '644044', '638049', '627549', '623844', '622236', '620434', '613538', '601127', '599671', '599469']

        print("\n\nProcessing Data...")
        for s in subjects:
            print("processing data subject {}".format(s))
            start_time = time.time()
            data = nib.load(
                join(C.NETWORK_DRIVE, "HCP_fusion_" + DIFFUSION_FOLDER,
                     s + "_probmap.nii.gz")).get_data()
            print("Done Loading")
            data = np.nan_to_num(data)
            data = DatasetUtils.scale_input_to_unet_shape(
                data, HP.DATASET, HP.RESOLUTION)
            data = data[:-1, :, :
                        -1, :]  # cut one pixel at the end, because in scale_input_to_world_shape we ouputted 146 -> one too much at the end
            ExpUtils.make_dir(
                join(C.NETWORK_DRIVE, "HCP_fusion_npy_" + DIFFUSION_FOLDER, s))
            np.save(
                join(C.NETWORK_DRIVE, "HCP_fusion_npy_" + DIFFUSION_FOLDER, s,
                     DIFFUSION_FOLDER + "_xyz.npy"), data)
            print("Took {}s".format(time.time() - start_time))

            print("processing seg subject {}".format(s))
            start_time = time.time()
            # seg = ImgUtils.create_multilabel_mask(HP, s, labels_type=HP.LABELS_TYPE)
            seg = nib.load(
                join(C.NETWORK_DRIVE, "HCP_for_training_COPY", s,
                     HP.LABELS_FILENAME + ".nii.gz")).get_data()
            if HP.RESOLUTION == "2.5mm":
                seg = ImgUtils.resize_first_three_dims(seg, order=0, zoom=0.5)
            seg = DatasetUtils.scale_input_to_unet_shape(
                seg, HP.DATASET, HP.RESOLUTION)
            np.save(
                join(C.NETWORK_DRIVE, "HCP_fusion_npy_" + DIFFUSION_FOLDER, s,
                     "bundle_masks.npy"), seg)
            print("Took {}s".format(time.time() - start_time))
예제 #6
0
    def pad_and_scale_img_to_square_img(data, target_size=144):
        '''
        Expects 3D or 4D image as input.

        Does
        1. Pad image with 0 to make it square
            (if uneven padding -> adds one more px "behind" img; but resulting img shape will be correct)
        2. Scale image to UNet size (144, 144, 144)
        '''
        nr_dims = len(data.shape)
        assert (nr_dims >= 3 and nr_dims <= 4)

        shape = data.shape
        biggest_dim = max(shape)

        # Pad to make square
        if nr_dims == 4:
            new_img = np.zeros((biggest_dim, biggest_dim, biggest_dim,
                                shape[3])).astype(data.dtype)
        else:
            new_img = np.zeros(
                (biggest_dim, biggest_dim, biggest_dim)).astype(data.dtype)
        pad1 = (biggest_dim - shape[0]) / 2.
        pad2 = (biggest_dim - shape[1]) / 2.
        pad3 = (biggest_dim - shape[2]) / 2.
        new_img[int(pad1):int(pad1) + shape[0],
                int(pad2):int(pad2) + shape[1],
                int(pad3):int(pad3) + shape[2]] = data

        # Scale to right size
        zoom = float(target_size) / biggest_dim
        if nr_dims == 4:
            #use order=0, otherwise image values of a DWI will be quite different after downsampling and upsampling
            new_img = ImgUtils.resize_first_three_dims(new_img,
                                                       order=0,
                                                       zoom=zoom)
        else:
            new_img = ndimage.zoom(new_img, zoom, order=0)

        transformation = {
            "original_shape": shape,
            "pad_x": pad1,
            "pad_y": pad2,
            "pad_z": pad3,
            "zoom": zoom
        }

        return new_img, transformation
예제 #7
0
    def save_fusion_nifti_as_npy():

        #Can leave this always the same (for 270g and 32g)
        class HP:
            DATASET = "HCP"
            RESOLUTION = "1.25mm"
            FEATURES_FILENAME = "270g_125mm_peaks"
            LABELS_TYPE = np.int16
            LABELS_FILENAME = "bundle_masks"
            DATASET_FOLDER = "HCP"

        #change this for 270g and 32g
        DIFFUSION_FOLDER = "32g_25mm"

        subjects = get_all_subjects()
        # fold0 = ['687163', '685058', '683256', '680957', '679568', '677968', '673455', '672756', '665254', '654754', '645551', '644044', '638049', '627549', '623844', '622236', '620434', '613538', '601127', '599671', '599469']
        # fold1 = ['992774', '991267', '987983', '984472', '983773', '979984', '978578', '965771', '965367', '959574', '958976', '957974', '951457', '932554', '930449', '922854', '917255', '912447', '910241', '907656', '904044']
        # fold2 = ['901442', '901139', '901038', '899885', '898176', '896879', '896778', '894673', '889579', '887373', '877269', '877168', '872764', '872158', '871964', '871762', '865363', '861456', '859671', '857263', '856766']
        # fold3 = ['849971', '845458', '837964', '837560', '833249', '833148', '826454', '826353', '816653', '814649', '802844', '792766', '792564', '789373', '786569', '784565', '782561', '779370', '771354', '770352', '765056']
        # fold4 = ['761957', '759869', '756055', '753251', '751348', '749361', '748662', '748258', '742549', '734045', '732243', '729557', '729254', '715647', '715041', '709551', '705341', '704238', '702133', '695768', '690152']
        # subjects = fold2 + fold3 + fold4

        # subjects = ['654754', '645551', '644044', '638049', '627549', '623844', '622236', '620434', '613538', '601127', '599671', '599469']

        print("\n\nProcessing Data...")
        for s in subjects:
            print("processing data subject {}".format(s))
            start_time = time.time()
            data = nib.load(join(C.NETWORK_DRIVE, "HCP_fusion_" + DIFFUSION_FOLDER, s + "_probmap.nii.gz")).get_data()
            print("Done Loading")
            data = np.nan_to_num(data)
            data = DatasetUtils.scale_input_to_unet_shape(data, HP.DATASET, HP.RESOLUTION)
            data = data[:-1, :, :-1, :]  # cut one pixel at the end, because in scale_input_to_world_shape we ouputted 146 -> one too much at the end
            ExpUtils.make_dir(join(C.NETWORK_DRIVE, "HCP_fusion_npy_" + DIFFUSION_FOLDER, s))
            np.save(join(C.NETWORK_DRIVE, "HCP_fusion_npy_" + DIFFUSION_FOLDER, s, DIFFUSION_FOLDER + "_xyz.npy"), data)
            print("Took {}s".format(time.time() - start_time))

            print("processing seg subject {}".format(s))
            start_time = time.time()
            # seg = ImgUtils.create_multilabel_mask(HP, s, labels_type=HP.LABELS_TYPE)
            seg = nib.load(join(C.NETWORK_DRIVE, "HCP_for_training_COPY", s, HP.LABELS_FILENAME + ".nii.gz")).get_data()
            if HP.RESOLUTION == "2.5mm":
                seg = ImgUtils.resize_first_three_dims(seg, order=0, zoom=0.5)
            seg = DatasetUtils.scale_input_to_unet_shape(seg, HP.DATASET, HP.RESOLUTION)
            np.save(join(C.NETWORK_DRIVE, "HCP_fusion_npy_" + DIFFUSION_FOLDER, s, "bundle_masks.npy"), seg)
            print("Took {}s".format(time.time() - start_time))
예제 #8
0
    def scale_input_to_world_shape(img4d, dataset, resolution="1.25mm"):
        '''
        Scale input image to original resolution and pad/cut image to make it original size

        :param img4d: (x, y, z, userdefined)  (userdefined could be gradients or classes)
        :param resolution: "1.25mm" / "2mm" / "2.5mm"
        :return: img with original size
        '''

        if resolution == "1.25mm":
            if dataset == "HCP":  # (144,144,144)
                # no resize needed
                return ImgUtils.pad_4d_image_left(img4d, np.array([1,15,1,0]), [146,174,146,img4d.shape[3]], pad_value=0)  # (146, 174, 146, none)
            elif dataset == "HCP_32g":  # (144,144,144)
                # no resize needed
                return ImgUtils.pad_4d_image_left(img4d, np.array([1,15,1,0]), [146,174,146,img4d.shape[3]], pad_value=0)  # (146, 174, 146, none)
            elif dataset == "TRACED":  # (78,93,75)
                raise ValueError("resolution '1.25mm' not supported for dataset 'TRACED'")
            elif dataset == "Schizo":  # (144,144,144)
                img4d = ImgUtils.pad_4d_image_left(img4d, np.array([1,15,1,0]), [145,174,145,img4d.shape[3]], pad_value=0)  # (145, 174, 145, none)
                return ImgUtils.resize_first_three_dims(img4d, zoom=0.62)  # (91,109,91)

        elif resolution == "2mm":
            if dataset == "HCP":  # (80,80,80)
                return ImgUtils.pad_4d_image_left(img4d, np.array([5,14,5,0]), [90,108,90,img4d.shape[3]], pad_value=0)  # (90, 108, 90, none)
            elif dataset == "HCP_32g":  # (80,80,80)
                return ImgUtils.pad_4d_image_left(img4d, np.array([5,14,5,0]), [90,108,90,img4d.shape[3]], pad_value=0)  # (90, 108, 90, none)
            elif dataset == "HCP_2mm":  # (80,80,80)
                return ImgUtils.pad_4d_image_left(img4d, np.array([5,14,5,0]), [90,108,90,img4d.shape[3]], pad_value=0)  # (90, 108, 90, none)
            elif dataset == "TRACED":  # (78,93,75)
                raise ValueError("resolution '2mm' not supported for dataset 'TRACED'")

        elif resolution == "2.5mm":
            if dataset == "HCP":  # (80,80,80)
                img4d = ImgUtils.pad_4d_image_left(img4d, np.array([0,4,0,0]), [80,87,80,img4d.shape[3]], pad_value=0) # (80,87,80,none)
                return img4d[4:77,:,4:77, :] # (73, 87, 73, none)
            elif dataset == "HCP_2.5mm":  # (80,80,80)
                img4d = ImgUtils.pad_4d_image_left(img4d, np.array([0,4,0,0]), [80,87,80,img4d.shape[3]], pad_value=0)  # (80,87,80,none)
                return img4d[4:77,:,4:77,:]  # (73, 87, 73, none)
            elif dataset == "HCP_32g":  # ((80,80,80)
                img4d = ImgUtils.pad_4d_image_left(img4d, np.array([0, 4, 0, 0]), [80, 87, 80, img4d.shape[3]], pad_value=0)  # (80,87,80,none)
                return img4d[4:77, :, 4:77, :]  # (73, 87, 73, none)
            elif dataset == "TRACED":  # (80,80,80)
                img4d = ImgUtils.pad_4d_image_left(img4d, np.array([0,7,0,0]), [80,93,80,img4d.shape[3]],pad_value=0)  # (80,93,80,none)
                return img4d[1:79, :, 3:78, :]  # (78,93,75,none)
예제 #9
0
    def pad_and_scale_img_to_square_img(data, target_size=144):
        '''
        Expects 3D or 4D image as input.

        Does
        1. Pad image with 0 to make it square
            (if uneven padding -> adds one more px "behind" img; but resulting img shape will be correct)
        2. Scale image to UNet size (144, 144, 144)
        '''
        nr_dims = len(data.shape)
        assert (nr_dims >= 3 and nr_dims <= 4)

        shape = data.shape
        biggest_dim = max(shape)

        # Pad to make square
        if nr_dims == 4:
            new_img = np.zeros((biggest_dim, biggest_dim, biggest_dim, shape[3])).astype(data.dtype)
        else:
            new_img = np.zeros((biggest_dim, biggest_dim, biggest_dim)).astype(data.dtype)
        pad1 = (biggest_dim - shape[0]) / 2.
        pad2 = (biggest_dim - shape[1]) / 2.
        pad3 = (biggest_dim - shape[2]) / 2.
        new_img[int(pad1):int(pad1) + shape[0],
                int(pad2):int(pad2) + shape[1],
                int(pad3):int(pad3) + shape[2]] = data

        # Scale to right size
        zoom = float(target_size) / biggest_dim
        if nr_dims == 4:
            #use order=0, otherwise image values of a DWI will be quite different after downsampling and upsampling
            new_img = ImgUtils.resize_first_three_dims(new_img, order=0, zoom=zoom)
        else:
            new_img = ndimage.zoom(new_img, zoom, order=0)

        transformation = {
            "original_shape": shape,
            "pad_x": pad1,
            "pad_y": pad2,
            "pad_z": pad3,
            "zoom": zoom
        }

        return new_img, transformation
예제 #10
0
파일: Slicer.py 프로젝트: silongGG/TractSeg
    def create_one_3D_file():
        '''
        Create one big file which contains all 3D Images (not slices).
        '''
        class HP:
            DATASET = "HCP"
            RESOLUTION = "1.25mm"
            FEATURES_FILENAME = "270g_125mm_peaks"
            LABELS_TYPE = np.int16
            DATASET_FOLDER = "HCP"

        data_all = []
        seg_all = []

        print("\n\nProcessing Data...")
        for s in get_all_subjects():
            print("processing data subject {}".format(s))
            data = nib.load(
                join(C.HOME, HP.DATASET_FOLDER, s,
                     HP.FEATURES_FILENAME + ".nii.gz")).get_data()
            data = np.nan_to_num(data)
            data = DatasetUtils.scale_input_to_unet_shape(
                data, HP.DATASET, HP.RESOLUTION)
        data_all.append(np.array(data))
        np.save("data.npy", data_all)
        del data_all  # free memory

        print("\n\nProcessing Segs...")
        for s in get_all_subjects():
            print("processing seg subject {}".format(s))
            seg = ImgUtils.create_multilabel_mask(HP,
                                                  s,
                                                  labels_type=HP.LABELS_TYPE)
            if HP.RESOLUTION == "2.5mm":
                seg = ImgUtils.resize_first_three_dims(seg, order=0, zoom=0.5)
            seg = DatasetUtils.scale_input_to_unet_shape(
                seg, HP.DATASET, HP.RESOLUTION)
        seg_all.append(np.array(seg))
        print("SEG TYPE: {}".format(seg_all.dtype))
        np.save("seg.npy", seg_all)
예제 #11
0
    def scale_input_to_world_shape(img4d, dataset, resolution="1.25mm"):
        '''
        Scale input image to original resolution and pad/cut image to make it original size

        :param img4d: (x, y, z, userdefined)  (userdefined could be gradients or classes)
        :param resolution: "1.25mm" / "2mm" / "2.5mm"
        :return: img with original size
        '''

        if resolution == "1.25mm":
            if dataset == "HCP":  # (144,144,144)
                # no resize needed
                return ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([1, 15, 1, 0]), [146, 174, 146, img4d.shape[3]],
                    pad_value=0)  # (146, 174, 146, none)
            elif dataset == "HCP_32g":  # (144,144,144)
                # no resize needed
                return ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([1, 15, 1, 0]), [146, 174, 146, img4d.shape[3]],
                    pad_value=0)  # (146, 174, 146, none)
            elif dataset == "TRACED":  # (78,93,75)
                raise ValueError(
                    "resolution '1.25mm' not supported for dataset 'TRACED'")
            elif dataset == "Schizo":  # (144,144,144)
                img4d = ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([1, 15, 1, 0]), [145, 174, 145, img4d.shape[3]],
                    pad_value=0)  # (145, 174, 145, none)
                return ImgUtils.resize_first_three_dims(
                    img4d, zoom=0.62)  # (91,109,91)

        elif resolution == "2mm":
            if dataset == "HCP":  # (80,80,80)
                return ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([5, 14, 5, 0]), [90, 108, 90, img4d.shape[3]],
                    pad_value=0)  # (90, 108, 90, none)
            elif dataset == "HCP_32g":  # (80,80,80)
                return ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([5, 14, 5, 0]), [90, 108, 90, img4d.shape[3]],
                    pad_value=0)  # (90, 108, 90, none)
            elif dataset == "HCP_2mm":  # (80,80,80)
                return ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([5, 14, 5, 0]), [90, 108, 90, img4d.shape[3]],
                    pad_value=0)  # (90, 108, 90, none)
            elif dataset == "TRACED":  # (78,93,75)
                raise ValueError(
                    "resolution '2mm' not supported for dataset 'TRACED'")

        elif resolution == "2.5mm":
            if dataset == "HCP":  # (80,80,80)
                img4d = ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([0, 4, 0, 0]), [80, 87, 80, img4d.shape[3]],
                    pad_value=0)  # (80,87,80,none)
                return img4d[4:77, :, 4:77, :]  # (73, 87, 73, none)
            elif dataset == "HCP_2.5mm":  # (80,80,80)
                img4d = ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([0, 4, 0, 0]), [80, 87, 80, img4d.shape[3]],
                    pad_value=0)  # (80,87,80,none)
                return img4d[4:77, :, 4:77, :]  # (73, 87, 73, none)
            elif dataset == "HCP_32g":  # ((80,80,80)
                img4d = ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([0, 4, 0, 0]), [80, 87, 80, img4d.shape[3]],
                    pad_value=0)  # (80,87,80,none)
                return img4d[4:77, :, 4:77, :]  # (73, 87, 73, none)
            elif dataset == "TRACED":  # (80,80,80)
                img4d = ImgUtils.pad_4d_image_left(
                    img4d,
                    np.array([0, 7, 0, 0]), [80, 93, 80, img4d.shape[3]],
                    pad_value=0)  # (80,93,80,none)
                return img4d[1:79, :, 3:78, :]  # (78,93,75,none)
예제 #12
0
    def _create_slices_file(HP, subjects, filename, slice, shuffle=True):
        data_dir = join(C.HOME, HP.DATASET_FOLDER)

        dwi_slices = []
        mask_slices = []

        print("\n\nProcessing Data...")
        for s in subjects:
            print("processing dwi subject {}".format(s))

            dwi = nib.load(join(data_dir, s, HP.FEATURES_FILENAME + ".nii.gz"))
            dwi_data = dwi.get_data()
            dwi_data = np.nan_to_num(dwi_data)
            dwi_data = DatasetUtils.scale_input_to_unet_shape(dwi_data, HP.DATASET, HP.RESOLUTION)

            # if slice == "x":
            #     for z in range(dwi_data.shape[0]):
            #         dwi_slices.append(dwi_data[z, :, :, :])
            #
            # if slice == "y":
            #     for z in range(dwi_data.shape[1]):
            #         dwi_slices.append(dwi_data[:, z, :, :])
            #
            # if slice == "z":
            #     for z in range(dwi_data.shape[2]):
            #         dwi_slices.append(dwi_data[:, :, z, :])

            #Use slices from all directions in one dataset
            for z in range(dwi_data.shape[0]):
                dwi_slices.append(dwi_data[z, :, :, :])
            for z in range(dwi_data.shape[1]):
                dwi_slices.append(dwi_data[:, z, :, :])
            for z in range(dwi_data.shape[2]):
                dwi_slices.append(dwi_data[:, :, z, :])

        dwi_slices = np.array(dwi_slices)
        random_idxs = None
        if shuffle:
            random_idxs = np.random.choice(len(dwi_slices), len(dwi_slices))
            dwi_slices = dwi_slices[random_idxs]

        np.save(filename + "_data.npy", dwi_slices)
        del dwi_slices  #free memory


        print("\n\nProcessing Segs...")
        for s in subjects:
            print("processing seg subject {}".format(s))

            mask_data = ImgUtils.create_multilabel_mask(HP, s, labels_type=HP.LABELS_TYPE)
            if HP.RESOLUTION == "2.5mm":
                mask_data = ImgUtils.resize_first_three_dims(mask_data, order=0, zoom=0.5)
            mask_data = DatasetUtils.scale_input_to_unet_shape(mask_data, HP.DATASET, HP.RESOLUTION)

            # if slice == "x":
            #     for z in range(dwi_data.shape[0]):
            #         mask_slices.append(mask_data[z, :, :, :])
            #
            # if slice == "y":
            #     for z in range(dwi_data.shape[1]):
            #         mask_slices.append(mask_data[:, z, :, :])
            #
            # if slice == "z":
            #     for z in range(dwi_data.shape[2]):
            #         mask_slices.append(mask_data[:, :, z, :])

            # Use slices from all directions in one dataset
            for z in range(dwi_data.shape[0]):
                mask_slices.append(mask_data[z, :, :, :])
            for z in range(dwi_data.shape[1]):
                mask_slices.append(mask_data[:, z, :, :])
            for z in range(dwi_data.shape[2]):
                mask_slices.append(mask_data[:, :, z, :])

        mask_slices = np.array(mask_slices)
        print("SEG TYPE: {}".format(mask_slices.dtype))
        if shuffle:
            mask_slices = mask_slices[random_idxs]

        np.save(filename + "_seg.npy", mask_slices)
예제 #13
0
파일: Slicer.py 프로젝트: silongGG/TractSeg
    def _create_slices_file(HP, subjects, filename, slice, shuffle=True):
        data_dir = join(C.HOME, HP.DATASET_FOLDER)

        dwi_slices = []
        mask_slices = []

        print("\n\nProcessing Data...")
        for s in subjects:
            print("processing dwi subject {}".format(s))

            dwi = nib.load(join(data_dir, s, HP.FEATURES_FILENAME + ".nii.gz"))
            dwi_data = dwi.get_data()
            dwi_data = np.nan_to_num(dwi_data)
            dwi_data = DatasetUtils.scale_input_to_unet_shape(
                dwi_data, HP.DATASET, HP.RESOLUTION)

            # if slice == "x":
            #     for z in range(dwi_data.shape[0]):
            #         dwi_slices.append(dwi_data[z, :, :, :])
            #
            # if slice == "y":
            #     for z in range(dwi_data.shape[1]):
            #         dwi_slices.append(dwi_data[:, z, :, :])
            #
            # if slice == "z":
            #     for z in range(dwi_data.shape[2]):
            #         dwi_slices.append(dwi_data[:, :, z, :])

            #Use slices from all directions in one dataset
            for z in range(dwi_data.shape[0]):
                dwi_slices.append(dwi_data[z, :, :, :])
            for z in range(dwi_data.shape[1]):
                dwi_slices.append(dwi_data[:, z, :, :])
            for z in range(dwi_data.shape[2]):
                dwi_slices.append(dwi_data[:, :, z, :])

        dwi_slices = np.array(dwi_slices)
        random_idxs = None
        if shuffle:
            random_idxs = np.random.choice(len(dwi_slices), len(dwi_slices))
            dwi_slices = dwi_slices[random_idxs]

        np.save(filename + "_data.npy", dwi_slices)
        del dwi_slices  #free memory

        print("\n\nProcessing Segs...")
        for s in subjects:
            print("processing seg subject {}".format(s))

            mask_data = ImgUtils.create_multilabel_mask(
                HP, s, labels_type=HP.LABELS_TYPE)
            if HP.RESOLUTION == "2.5mm":
                mask_data = ImgUtils.resize_first_three_dims(mask_data,
                                                             order=0,
                                                             zoom=0.5)
            mask_data = DatasetUtils.scale_input_to_unet_shape(
                mask_data, HP.DATASET, HP.RESOLUTION)

            # if slice == "x":
            #     for z in range(dwi_data.shape[0]):
            #         mask_slices.append(mask_data[z, :, :, :])
            #
            # if slice == "y":
            #     for z in range(dwi_data.shape[1]):
            #         mask_slices.append(mask_data[:, z, :, :])
            #
            # if slice == "z":
            #     for z in range(dwi_data.shape[2]):
            #         mask_slices.append(mask_data[:, :, z, :])

            # Use slices from all directions in one dataset
            for z in range(dwi_data.shape[0]):
                mask_slices.append(mask_data[z, :, :, :])
            for z in range(dwi_data.shape[1]):
                mask_slices.append(mask_data[:, z, :, :])
            for z in range(dwi_data.shape[2]):
                mask_slices.append(mask_data[:, :, z, :])

        mask_slices = np.array(mask_slices)
        print("SEG TYPE: {}".format(mask_slices.dtype))
        if shuffle:
            mask_slices = mask_slices[random_idxs]

        np.save(filename + "_seg.npy", mask_slices)
예제 #14
0
    def scale_input_to_unet_shape(img4d, dataset, resolution="1.25mm"):
        '''
        Scale input image to right isotropic resolution and pad/cut image to make it square to fit UNet input shape

        :param img4d: (x, y, z, userdefined)  (userdefined could be gradients or classes)
        :param resolution: "1.25mm" / "2mm" / "2.5mm"     results in UNet input shape of (144,144,144) or (80,80,80)
        :return: img with dim 1mm: (144,144,144,none) or 2mm: (80,80,80,none) or 2.5mm: (80,80,80,none)
                    (note: 2.5mm padded with more zeros to reach 80,80,80)
        '''

        if resolution == "1.25mm":
            if dataset == "HCP":  # (145,174,145)
                # no resize needed
                return img4d[1:, 15:159, 1:]  # (144,144,144)
            elif dataset == "HCP_32g":  # (73,87,73)
                # return img4d[1:, 15:159, 1:]  # (144,144,144) #OLD when HCP_32g was still 125mm
                img4d = ImgUtils.resize_first_three_dims(img4d, zoom=2)  # (146,174,146,none)
                img4d = img4d[:-1,:,:-1]  #remove one voxel that came from upsampling   #(145,174,145)
                return img4d[1:, 15:159, 1:]  # (144,144,144)
            elif dataset == "TRACED":  # (78,93,75)
                raise ValueError("resolution '1.25mm' not supported for dataset 'TRACED'")
            elif dataset == "Schizo":  # (91,109,91)
                img4d = ImgUtils.resize_first_three_dims(img4d, zoom=1.60)  # (146,174,146)
                return img4d[1:145, 15:159, 1:145]                                # (144,144,144)

        elif resolution == "2mm":
            if dataset == "HCP":  # (145,174,145)
                img4d = ImgUtils.resize_first_three_dims(img4d, zoom=0.62)  # (90,108,90)
                return img4d[5:85, 14:94, 5:85, :]  # (80,80,80)
            elif dataset == "HCP_32g":  # (145,174,145)
                img4d = ImgUtils.resize_first_three_dims(img4d, zoom=0.62)  # (90,108,90)
                return img4d[5:85, 14:94, 5:85, :]  # (80,80,80)
            elif dataset == "HCP_2mm":  # (90,108,90)
                # no resize needed
                return img4d[5:85, 14:94, 5:85, :]  # (80,80,80)
            elif dataset == "TRACED":  # (78,93,75)
                raise ValueError("resolution '2mm' not supported for dataset 'TRACED'")
            elif dataset == "Schizo":  # (91,109,91)
                return img4d[:, 9:100, :]                                # (91,91,91)

        elif resolution == "2.5mm":
            if dataset == "HCP":  # (145,174,145)
                img4d = ImgUtils.resize_first_three_dims(img4d, zoom=0.5)  # (73,87,73,none)
                bg = np.zeros((80, 80, 80, img4d.shape[3])).astype(img4d.dtype)
                bg = bg + img4d[0,0,0,:] #make bg have same value as bg from original img  (this adds last dim of img4d to last dim of bg)
                bg[4:77, :, 4:77] = img4d[:, 4:84, :, :]
                return bg  # (80,80,80)
            elif dataset == "HCP_2.5mm":  # (73,87,73,none)
                #no resize needed
                bg = np.zeros((80, 80, 80, img4d.shape[3])).astype(img4d.dtype)
                bg = bg + img4d[0,0,0,:] #make bg have same value as bg from original img  (this adds last dim of img4d to last dim of bg)
                bg[4:77, :, 4:77] = img4d[:, 4:84, :, :]
                return bg  # (80,80,80)
            elif dataset == "HCP_32g":  # (73,87,73,none)
                bg = np.zeros((80, 80, 80, img4d.shape[3])).astype(img4d.dtype)
                bg = bg + img4d[0, 0, 0, :]  # make bg have same value as bg from original img  (this adds last dim of img4d to last dim of bg)
                bg[4:77, :, 4:77] = img4d[:, 4:84, :, :]
                return bg  # (80,80,80)
            elif dataset == "TRACED":  # (78,93,75)
                # no resize needed
                bg = np.zeros((80, 80, 80, img4d.shape[3])).astype(img4d.dtype)
                bg = bg + img4d[0, 0, 0, :]  # make bg have same value as bg from original img
                bg[1:79, :, 3:78, :] = img4d[:, 7:87, :, :]
                return bg  # (80,80,80)