def test_store_posteriors_nifti_invalid_entries(test_output_dirs: TestOutputDirectories) -> None: image = np.array([0, 1, 2.71, np.nan]) header = ImageHeader(origin=(1, 1, 1), direction=(1, 0, 0, 1, 0, 0, 1, 0, 0), spacing=(1, 1, 1)) with pytest.raises(ValueError) as ex: io_util.store_posteriors_as_nifti(image, header, test_output_dirs.create_file_or_folder_path(default_image_name)) assert "invalid values" in ex.value.args[0] assert "2.71" in ex.value.args[0] assert "nan" in ex.value.args[0]
def test_store_posteriors_nifti_fail(test_output_dirs: TestOutputDirectories, image: Any) -> None: image = np.array(image) header = ImageHeader(origin=(1, 1, 1), direction=(1, 0, 0, 1, 0, 0, 1, 0, 0), spacing=(1, 1, 1)) with pytest.raises(Exception): io_util.store_posteriors_as_nifti( image, header, test_output_dirs.create_file_or_folder_path(default_image_name))
def test_store_posteriors_nifti(test_output_dirs: TestOutputDirectories, image: Any, expected: Any) -> None: image = np.array(image) header = ImageHeader(origin=(1, 1, 1), direction=(1, 0, 0, 0, 1, 0, 0, 0, 1), spacing=(1, 1, 1)) io_util.store_posteriors_as_nifti( image, header, test_output_dirs.create_file_or_folder_path(default_image_name)) assert_nifti_content( test_output_dirs.create_file_or_folder_path(default_image_name), image.shape, header, list(expected), np.ubyte)
def store_inference_results(inference_result: InferencePipeline.Result, config: SegmentationModelBase, results_folder: Path, image_header: ImageHeader) -> List[str]: """ Store the segmentation, posteriors, and binary predictions into Nifti files. :param inference_result: The inference result for a given patient_id and epoch. Posteriors must be in (Classes x Z x Y x X) shape, segmentation in (Z, Y, X) :param config: The test configurations. :param results_folder: The folder where the prediction should be stored. :param image_header: The image header that was used in the input image. """ def create_file_path(_results_folder: Path, _file_name: str) -> Path: """ Create filename with Nifti extension :param _results_folder: The results folder :param _file_name: The name of the file :return: A full path to the results folder for the file """ file_path = _file_name + MedicalImageFileType.NIFTI_COMPRESSED_GZ.value return _results_folder / Path(file_path) # create the directory for the given patient inside the results dir patient_results_folder = get_patient_results_folder( results_folder, inference_result.patient_id) patient_results_folder.mkdir(exist_ok=True, parents=True) # write the segmentations to disk image_paths = [ io_util.store_as_ubyte_nifti(image=inference_result.segmentation, header=image_header, file_name=str( create_file_path( patient_results_folder, "segmentation"))) ] class_names_and_indices = config.class_and_index_with_background().items() binaries = binaries_from_multi_label_array(inference_result.segmentation, config.number_of_classes) # rescale posteriors if required and save them for (class_name, index), binary in zip(class_names_and_indices, binaries): posterior = inference_result.posteriors[index, ...] # save the posterior map file_name = "posterior_{}".format(class_name) image_path = io_util.store_posteriors_as_nifti( image=posterior, header=image_header, file_name=str(create_file_path(patient_results_folder, file_name))) image_paths.append(image_path) # save the binary mask image_path = io_util.store_binary_mask_as_nifti( image=binary, header=image_header, file_name=str(create_file_path(patient_results_folder, class_name))) image_paths.append(image_path) # rescale and store uncertainty map as nifti image_path = io_util.store_posteriors_as_nifti( image=inference_result.uncertainty, header=image_header, file_name=str(create_file_path(patient_results_folder, "uncertainty"))) image_paths.append(image_path) return image_paths