def run_preprocessing(self, num_threads):
     if os.path.isdir(
             join(self.preprocessed_output_folder, "gt_segmentations")):
         shutil.rmtree(
             join(self.preprocessed_output_folder, "gt_segmentations"))
     shutil.copytree(
         join(self.folder_with_cropped_data, "gt_segmentations"),
         join(self.preprocessed_output_folder, "gt_segmentations"))
     normalization_schemes = self.plans['normalization_schemes']
     use_nonzero_mask_for_normalization = self.plans['use_mask_for_norm']
     intensityproperties = self.plans['dataset_properties'][
         'intensityproperties']
     preprocessor = GenericPreprocessor(normalization_schemes,
                                        use_nonzero_mask_for_normalization,
                                        self.transpose_forward,
                                        intensityproperties)
     target_spacings = [
         i["current_spacing"] for i in self.plans_per_stage.values()
     ]
     if self.plans['num_stages'] > 1 and not isinstance(
             num_threads, (list, tuple)):
         num_threads = (8, num_threads)
     elif self.plans['num_stages'] == 1 and isinstance(
             num_threads, (list, tuple)):
         num_threads = num_threads[-1]
     preprocessor.run(target_spacings, self.folder_with_cropped_data,
                      self.preprocessed_output_folder,
                      self.plans['data_identifier'], num_threads)
示例#2
0
    def generate_data(self, data_path):
        # form list of file names
        # expected_num_modalities = self.trainer.plans['num_modalities']

        # # check input folder integrity
        # case_ids = check_input_folder_and_return_caseIDs(data_path, expected_num_modalities)

        # all_files = subfiles(data_path, suffix=".nii.gz", join=False, sort=True)
        # list_of_lists = [[join(data_path, i) for i in all_files if i[:len(j)].startswith(j) and
        #               len(i) == (len(j) + 12)] for j in case_ids]

        # part_id = 0
        # num_parts = 1

        file_name = [
            '/home/zhiyuan/project/data/nnUNet_raw/nnUNet_raw_data/Task004_Hippocampus/imagesTs/hippocampus_392_0000.nii.gz'
        ]
        data, seg, properties = ImageCropper.crop_from_list_of_files(file_name)
        plans = self.trainer.plans
        if plans.get('transpose_forward') is None or plans.get(
                'transpose_backward') is None:
            print(
                "WARNING! You seem to have data that was preprocessed with a previous version of nnU-Net. "
                "You should rerun preprocessing. We will proceed and assume that both transpose_foward "
                "and transpose_backward are [0, 1, 2]. If that is not correct then weird things will happen!"
            )
            plans['transpose_forward'] = [0, 1, 2]
            plans['transpose_backward'] = [0, 1, 2]

        # 1. From preprocessing.GenericPreprocessor.preprocess_test_case
        preprocessor = GenericPreprocessor(
            plans['normalization_schemes'], plans['use_mask_for_norm'],
            plans['transpose_forward'],
            plans['dataset_properties']['intensityproperties'])

        data = data.transpose(
            (0, *[i + 1 for i in preprocessor.transpose_forward]))
        seg = seg.transpose(
            (0, *[i + 1 for i in preprocessor.transpose_forward]))

        data, seg, properties = preprocessor.resample_and_normalize(
            data,
            plans['plans_per_stage'][self.trainer.stage]['current_spacing'],
            properties,
            seg,
            force_separate_z=None)

        output_data = data.astype(np.float32)

        return output_data, seg, properties
示例#3
0
    def preprocess_patient(self, input_files):
        """
        Used to predict new unseen data. Not used for the preprocessing of the training/test data
        :param input_files:
        :return:
        """
        from nnunet.preprocessing.preprocessing import GenericPreprocessor, PreprocessorFor2D
        if self.threeD:
            preprocessor = GenericPreprocessor(self.normalization_schemes, self.use_mask_for_norm,
                                               self.intensity_properties)
        else:
            preprocessor = PreprocessorFor2D(self.normalization_schemes, self.use_mask_for_norm,
                                             self.intensity_properties)

        d, s, properties = preprocessor.preprocess_test_case(input_files,
                                                             self.plans['plans_per_stage'][self.stage]['current_spacing'])
        return d, s, properties