Exemple #1
0
    def test_center_crop_2D_padding(self):
        data = np.random.random((8, 4, 30, 30))
        seg = np.random.random(data.shape)
        crop_size = 50

        d, s = center_crop(data, crop_size=crop_size, seg=seg)

        self.assertTrue(
            all(i == j for i, j in zip((8, 4, crop_size, crop_size), d.shape)),
            "data has unexpected return shape")
        self.assertTrue(
            all(i == j for i, j in zip((8, 4, crop_size, crop_size), s.shape)),
            "seg has unexpected return shape")

        tmp_d = d[:, :, 10:40, 10:40]
        tmp_s = s[:, :, 10:40, 10:40]
        np.testing.assert_array_equal(
            tmp_d,
            data,
            err_msg="Original data is not included in padded image")
        self.assertAlmostEqual(np.sum(d.flatten()),
                               np.sum(data.flatten()),
                               msg="Padding of data is not zero")

        np.testing.assert_array_equal(
            tmp_s,
            seg,
            err_msg="Original segmentation is not included in padded image")
        self.assertAlmostEqual(np.sum(d.flatten()),
                               np.sum(data.flatten()),
                               msg="Padding of segmentation is not zero")
Exemple #2
0
    def __call__(self, **data_dict):
        seg = data_dict.get(self.label_key)

        if seg is not None:
            data_dict[self.label_key] = center_crop(seg, self.output_size, None)[0]
        else:
            from warnings import warn
            warn("You shall not pass data_dict without seg: Used CenterCropSegTransform, but there is no seg", Warning)
        return data_dict
Exemple #3
0
    def __call__(self, **data_dict):
        data = data_dict.get(self.data_key)
        seg = data_dict.get(self.label_key)
        data, seg = center_crop(data, self.crop_size, seg)

        data_dict[self.data_key] = data
        if seg is not None:
            data_dict[self.label_key] = seg

        return data_dict
    def __call__(self, **data_dict):
        data = data_dict.get(self.data_key)
        seg = data_dict.get(self.label_key)
        data, seg = center_crop(data, self.crop_size, seg)

        data_dict[self.data_key] = data
        if seg is not None:
            data_dict[self.label_key] = seg

        return data_dict
    def __call__(self, **data_dict):
        data = data_dict.get("data")
        seg = data_dict.get("seg")
        data, seg = center_crop(data, self.crop_size, seg)

        data_dict["data"] = data
        if seg is not None:
            data_dict["seg"] = seg

        return data_dict
    def test_center_crop_negative_margin(self):
        """
        Negative margin means that we are effectively padding if necessary
        :return:
        """
        data = np.ones((8, 4, 30, 30, 30))
        seg = np.ones(data.shape)
        crop_size = np.array([36, 40, 16])
        data_cropped, seg_cropped = center_crop(data, crop_size, seg)

        # data and set are just ones and will be padded of necessary, so the border will be 0
        border = (crop_size - np.array(data.shape[2:])) // 2
        assert np.sum(data_cropped[:, :, 0:border[0]]) == 0
        assert np.sum(data_cropped[:, :, border[0] + crop_size[0]:]) == 0

        assert np.sum(data_cropped[:, :, :, 0:border[1]]) == 0
        assert np.sum(data_cropped[:, :, :, border[1] + crop_size[1]:]) == 0

        data_cropped_back, seg_cropped_back = center_crop(data_cropped, (30, 30, 30), seg_cropped)

        self.assertAlmostEqual(np.sum(data_cropped_back) / np.sum(data), 16 / 30.)
    def test_center_crop_3D_list(self):
        data = np.random.random((8, 4, 30, 30, 30))
        seg = np.random.random(data.shape)
        crop_size = [10, 20, 29]

        d, s = center_crop(data, crop_size=crop_size, seg=seg)

        self.assertTrue(all(i == j for i, j in zip((8, 4, crop_size[0], crop_size[1], crop_size[2]), d.shape)),
                        "data has unexpected return shape")
        self.assertTrue(all(i == j for i, j in zip((8, 4, crop_size[0], crop_size[1], crop_size[2]), s.shape)),
                        "seg has unexpected return shape")

        np.testing.assert_array_equal(data[:, :, 10:20, 5:25, 0:29], d, err_msg="crop not equal image center")
        np.testing.assert_array_equal(seg[:, :, 10:20, 5:25, 0:29], s, err_msg="crop not equal image center")
def center_crop_generator(generator, output_size):
    warn("using deprecated generator center_crop_generator", Warning)
    '''
    yields center crop of size output_size (may be int or tuple) from data and seg
    '''
    for data_dict in generator:
        assert "data" in list(
            data_dict.keys()), "your data generator needs to return a python dictionary with at least a 'data' key value pair"
        data = data_dict["data"]
        seg = None
        if "seg" in list(data_dict.keys()):
            seg = data_dict["seg"]
        data, seg = center_crop(data, output_size, seg)
        data_dict["data"] = data
        if seg is not None:
            data_dict["seg"] = seg
        yield data_dict
Exemple #9
0
def center_crop_generator(generator, output_size):
    warn("using deprecated generator center_crop_generator", Warning)
    '''
    yields center crop of size output_size (may be int or tuple) from data and seg
    '''
    for data_dict in generator:
        assert "data" in list(
            data_dict.keys()
        ), "your data generator needs to return a python dictionary with at least a 'data' key value pair"
        data = data_dict["data"]
        seg = None
        if "seg" in list(data_dict.keys()):
            seg = data_dict["seg"]
        data, seg = center_crop(data, output_size, seg)
        data_dict["data"] = data
        if seg is not None:
            data_dict["seg"] = seg
        yield data_dict
Exemple #10
0
def get_oversampling(Data, train_idx, Batch_Size, patch_size):

    IDs = sorted(train_idx)

    Total_Patients = len(IDs)

    print 'Total_Patients', Total_Patients

    Tumor_slices = 0
    Prostate_slices = 0
    Pos_Patient = 0
    Non_Tumor_Slices = 0
    Non_Prostate_Slices = 0

    for i in range(len(IDs)):
        idx = IDs[i]
        print(idx)
        z_Dim = (Data[idx]['label']).shape
        Data_over = Data[idx]['label']

        center_crop_dimensions = ((patch_size[0], patch_size[1]))
        Data_over_cropped = center_crop(Data_over, center_crop_dimensions)
        Data_over_cropped = Data_over_cropped[0]

        for f in range(z_Dim[3]):
            if np.sum(Data_over_cropped[0, :, :, f] == 2) >= 1:
                Tumor_slices += 1
            else:
                Non_Tumor_Slices += 1

            if np.sum(Data_over_cropped[0, :, :, f] == 1) >= 1:
                Prostate_slices += 1
            else:
                Non_Prostate_Slices += 1

        if np.sum(Data_over_cropped == 2) >= 1:
            Pos_Patient += 1

        print 'Tumor', Tumor_slices
        print 'Non_Tumor', Non_Tumor_Slices
        print 'Pos_Patients', Pos_Patient

        print 'Prostate', Prostate_slices
        print 'Non_Prostate_Slices', Non_Prostate_Slices

    print(Tumor_slices, Non_Tumor_Slices)

    Probability_Pos_Patient = Pos_Patient / np.float(Total_Patients)

    print 'Probability_Pos_Patient', Probability_Pos_Patient

    Slices_total = Tumor_slices + Non_Tumor_Slices

    Natural_probability_tu_slice = Tumor_slices / np.float(Slices_total)

    Natural_probability_Prostate_slice = Prostate_slices / np.float(
        Slices_total)

    print 'Natural_probability_tu_slice', Natural_probability_tu_slice
    print 'Natural_probability_PRO_slice', Natural_probability_Prostate_slice

    Oversampling_Factor = (Natural_probability_tu_slice**(
        1 / np.float(Batch_Size * Probability_Pos_Patient)))

    print 'oversampling_factor', Oversampling_Factor

    return (
        Oversampling_Factor
    ), Slices_total, Natural_probability_tu_slice, Natural_probability_Prostate_slice
Exemple #11
0
def get_class_frequencies(Data, train_idx, patch_size):

    Tumor_frequencie_ADC = 0
    Prostate_frequencie_ADC = 0
    Background_frequencie_ADC = 0

    Tumor_frequencie_T2 = 0
    Prostate_frequencie_T2 = 0
    Background_frequencie_T2 = 0

    T2_mean = 0
    T2_std = 0
    ADC_mean = 0
    ADC_std = 0
    BVAL_mean = 0
    BVAL_std = 0

    for i in range(len(train_idx)):
        idx = train_idx[i]
        Data_class = Data[idx]['label']
        Data_image = Data[idx]['image']

        center_crop_dimensions = ((patch_size[0], patch_size[1]))

        Data_class_Label = center_crop(Data_class, center_crop_dimensions)
        Data_class_Label = Data_class_Label[0]

        Data_image_cropped = center_crop(Data_image, center_crop_dimensions)
        Data_image_cropped = Data_image_cropped[0]

        Data_image_cropped = np.nan_to_num(Data_image_cropped)
        Data_image_cropped = cut_off_outliers(Data_image_cropped,
                                              percentile_lower=0.2,
                                              percentile_upper=99.8,
                                              per_channel=True)

        T2_mean += np.mean(Data_image_cropped[2, :, :, :])
        T2_std += np.std(Data_image_cropped[2, :, :, :])
        ADC_mean += np.mean(Data_image_cropped[0, :, :, :])
        ADC_std += np.std(Data_image_cropped[0, :, :, :])
        BVAL_mean += np.mean(Data_image_cropped[1, :, :, :])
        BVAL_std += np.std(Data_image_cropped[1, :, :, :])

        Tumor_frequencie_ADC += np.sum(Data_class_Label[0, :, :, :] == 2)
        Prostate_frequencie_ADC += np.sum(Data_class_Label[0, :, :, :] == 1)
        Background_frequencie_ADC += np.sum(Data_class_Label[0, :, :, :] == 0)

        Tumor_frequencie_T2 += np.sum(Data_class_Label[1, :, :, :] == 2)
        Prostate_frequencie_T2 += np.sum(Data_class_Label[1, :, :, :] == 1)
        Background_frequencie_T2 += np.sum(Data_class_Label[1, :, :, :] == 0)

    T2_mean = T2_mean / np.float(len(train_idx))
    T2_std = T2_std / np.float(len(train_idx))
    ADC_mean = ADC_mean / np.float(len(train_idx))
    ADC_std = ADC_std / np.float(len(train_idx))
    BVAL_mean = BVAL_mean / np.float(len(train_idx))
    BVAL_std = BVAL_std / np.float(len(train_idx))

    return Tumor_frequencie_ADC, Prostate_frequencie_ADC, Background_frequencie_ADC,\
           Tumor_frequencie_T2, Prostate_frequencie_T2, Background_frequencie_T2, ADC_mean, ADC_std, BVAL_mean, \
           BVAL_std, T2_mean, T2_std