def __call__(self, **data_dict):
     data_dict[self.data_key] = cut_off_outliers(
         data_dict[self.data_key],
         self.percentile_lower,
         self.percentile_upper,
         per_channel=self.per_channel)
     return data_dict
    def test_cut_off_outliers_whole_image(self):
        print('Test test_cut_off_outliers_whole_image. [START]')
        data = np.ones((32, 4, 64, 56, 48))

        data[:, :, 1, 1, 1] = 999999
        data[:, :, 1, 1, 2] = -999999

        data_normalized = cut_off_outliers(data, per_channel=False)
        for b in range(data.shape[0]):
            self.assertAlmostEqual(data_normalized[b].min(), 1.0, msg="Lowe outlier not removed.")
            self.assertAlmostEqual(data_normalized[b].max(), 1.0, msg="Upper outlier not removed.")

        print('Test test_cut_off_outliers_whole_image. [START]')
Example #3
0
    def generate_train_batch(self):

        channels_img = 3
        channels_label = 2
        img_size = 200

        if self.test == True:

            img = np.empty((self.BATCH_SIZE, channels_img, img_size, img_size))
            label = np.empty(
                (self.BATCH_SIZE, channels_label, img_size, img_size))

            if self._count < len(self._split_idx):
                idx = self._split_idx[self._count]
                z_dim = self._data[idx]['image'].shape[3]
                self.BATCH_SIZE = z_dim

                custom_batch = (z_dim / self.BATCH_SIZE) * self.BATCH_SIZE

                if self._count < len(self._split_idx):
                    img = self._data[idx]['image']
                    img = img.transpose((3, 0, 1, 2))
                    label = self._data[idx]['label']
                    label = label.transpose((3, 0, 1, 2))
                    self._count += 1

                else:
                    for b in range(self.BATCH_SIZE):
                        if self._s < custom_batch:
                            img[b, :, :, :] = self._data[idx][
                                'image'][:channels_img, :, :, self._s]
                            label[b, :, :, :] = self._data[idx][
                                'label'][:channels_label, :, :, self._s]
                            self._s += 1
                        else:
                            self._s = 0
                            self._count += 1
                            self._batches_generated = self._count
                            if self._count < len(self._split_idx):
                                idx = self._split_idx[self._batches_generated]
                                img[b, :, :, :] = self._data[idx][
                                    'image'][:channels_img, :, :, self._s]
                                label[b, :, :, :] = self._data[idx][
                                    'label'][:channels_label, :, :, self._s]
                                self._s += 1
                            else:
                                pass

            else:
                pass

        else:

            idx = np.random.choice(self._split_idx, self.BATCH_SIZE, False,
                                   None)
            img = np.empty((self.BATCH_SIZE, channels_img, img_size, img_size))
            label = np.empty(
                (self.BATCH_SIZE, channels_label, img_size, img_size))

            for b in range(self.BATCH_SIZE):

                if self._ProbabilityTumorSlices is not None:
                    LabelData = self._data[idx[b]]['label']
                    z_dim = self._data[idx[b]]['image'].shape[3]
                    CancerSlices = []
                    for Slice in range(z_dim):

                        bool = np.where(LabelData[:, :, :, Slice] == 2, True,
                                        False)

                        if bool.any() == True:
                            CancerSlices.append(Slice)

                    if sum(CancerSlices) is not 0:
                        CancerSlices = np.array(CancerSlices)
                        totalTumorSliceProb = float(
                            self._ProbabilityTumorSlices)
                        totalOtherSliceProb = float(1) - float(
                            totalTumorSliceProb)

                        ProbabilityMap = np.array(np.zeros(z_dim))
                        ProbabilityMap[
                            CancerSlices] = self._ProbabilityTumorSlices / float(
                                len(CancerSlices))

                        NoTumorSlices = float(z_dim - len(CancerSlices))
                        ProbPerNoTumorSlice = totalOtherSliceProb / NoTumorSlices

                        ProbabilityMap = [
                            ProbPerNoTumorSlice if g == 0 else g
                            for g in ProbabilityMap
                        ]

                    else:
                        ProbabilityMap = np.zeros(z_dim)
                        ProbabilityMap = [
                            float(1) / float(z_dim) if g == 0 else g
                            for g in ProbabilityMap
                        ]

                else:
                    ProbabilityMap = np.zeros(z_dim)
                    ProbabilityMap = [
                        float(1) / float(z_dim) if g == 0 else g
                        for g in ProbabilityMap
                    ]

                randint = np.random.choice(z_dim, p=ProbabilityMap)

                img[b, :, :, :] = self._data[idx[b]]['image'][:, :, :, randint]
                label[b, :, :, :] = self._data[idx[b]]['label'][:, :, :,
                                                                randint]

        # cut off outliers before image normalization

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

        img[:,
            0, :, :] = (img[:, 0, :, :] - np.float(self.ADC_mean)) / np.float(
                self.ADC_std)
        img[:,
            1, :, :] = (img[:, 1, :, :] - np.float(self.BVAL_mean)) / np.float(
                self.BVAL_std)
        img[:,
            2, :, :] = (img[:, 2, :, :] - np.float(self.T2_mean)) / np.float(
                self.T2_std)

        img = np.float32(img)

        data_dict = {"data": img, "seg": label}

        return data_dict
Example #4
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
 def __call__(self, **data_dict):
     data_dict[self.data_key] = cut_off_outliers(data_dict[self.data_key], self.percentile_lower,
                                                 self.percentile_upper,
                                                 per_channel=self.per_channel)
     return data_dict