Esempio n. 1
0
def test_plot_normalization_result(
        test_output_dirs: TestOutputDirectories) -> None:
    """
    Tests plotting of before/after histograms in photometric normalization.
    :return:
    """
    size = (3, 3, 3)
    image = np.zeros((1, ) + size)
    for i, (z, y, x) in enumerate(
            itertools.product(range(size[0]), range(size[1]), range(size[2]))):
        image[0, z, y, x] = i
    labels = np.zeros((2, ) + size)
    labels[1, 1, 1, 1] = 1
    sample = Sample(image=image,
                    labels=labels,
                    mask=np.ones(size),
                    metadata=DummyPatientMetadata)
    config = SegmentationModelBase(
        norm_method=PhotometricNormalizationMethod.CtWindow,
        window=4,
        level=13,
        should_validate=False)
    normalizer = PhotometricNormalization(config)
    folder = Path(test_output_dirs.root_dir)
    files = plotting.plot_normalization_result(sample, normalizer, folder)
    expected = ["042_slice_001.png", "042_slice_001_contour.png"]
    compare_files(files, expected)
Esempio n. 2
0
def test_plot_contours_for_all_classes(
        test_output_dirs: TestOutputDirectories) -> None:
    size = (3, 3, 3)
    image = np.zeros((1, ) + size)
    for i, (z, y, x) in enumerate(
            itertools.product(range(size[0]), range(size[1]), range(size[2]))):
        image[0, z, y, x] = i
    # Create a fake label array: For each class, there is exactly 1 pixel foreground, at the z slice that is
    # equal to the class index
    labels = np.zeros((3, ) + size)
    labels[0, 0, 1, 1] = 1
    labels[1, 1, 1, 1] = 1
    labels[2, 2, 1, 1] = 1
    # Fake segmentation: Classifies all foreground pixels correctly...
    segmentation = np.zeros(size)
    segmentation[1, 1, 1] = 1
    segmentation[2, 1, 1] = 2
    # ...but has an extra foreground pixel in the largest z slice in either top left or bottom right corner:
    segmentation[1, 0, 0] = 1
    segmentation[2, 2, 2] = 2
    sample = Sample(image=image,
                    labels=labels,
                    mask=np.ones(size),
                    metadata=DummyPatientMetadata)
    plots = plotting.plot_contours_for_all_classes(
        sample,
        segmentation,
        foreground_class_names=["class1", "class2"],
        result_folder=Path(test_output_dirs.root_dir),
        result_prefix="prefix")
    expected = [
        "prefix042_class1_slice_001.png", "prefix042_class2_slice_002.png"
    ]
    compare_files(plots, expected)
    with pytest.raises(ValueError) as err:
        plotting.plot_contours_for_all_classes(
            sample,
            segmentation,
            foreground_class_names=["background", "class1", "class2"],
            result_folder=Path(test_output_dirs.root_dir),
            result_prefix="prefix")
    assert "3 classes" in str(err)
    assert "background" in str(err)