Esempio n. 1
0
def test_RandomAffine(im_seg, transform):
    im, seg = im_seg
    metadata_in = [SampleMetadata({})
                   for _ in im] if isinstance(im, list) else SampleMetadata({})

    # Transform on Numpy
    do_im, metadata_do = transform(im.copy(), metadata_in)
    do_seg, metadata_do = transform(seg.copy(), metadata_do)

    if DEBUGGING and len(im[0].shape) == 2:
        plot_transformed_sample(im[0], do_im[0], ['raw', 'do'])
        plot_transformed_sample(seg[0], do_seg[0], ['raw', 'do'])

    # Transform on Numpy
    undo_im, _ = transform.undo_transform(do_im, metadata_do)
    undo_seg, _ = transform.undo_transform(do_seg, metadata_do)

    if DEBUGGING and len(im[0].shape) == 2:
        # TODO: ERROR for image but not for seg.....
        plot_transformed_sample(im[0], undo_im[0], ['raw', 'undo'])
        plot_transformed_sample(seg[0], undo_seg[0], ['raw', 'undo'])

    # Check data type and shape
    _check_dtype(im, [do_im, undo_im])
    _check_shape(im, [do_im, undo_im])
    _check_dtype(seg, [undo_seg, do_seg])
    _check_shape(seg, [undo_seg, do_seg])

    # Loop and check
    for idx, i in enumerate(im):
        # Data consistency
        assert dice_score(undo_seg[idx], seg[idx]) > 0.85
Esempio n. 2
0
def test_Clahe(im_seg, clahe):
    im, seg = im_seg
    metadata_in = [SampleMetadata({})
                   for _ in im] if isinstance(im, list) else SampleMetadata({})

    # Transform on Numpy
    do_im, metadata_do = clahe(im.copy(), metadata_in)

    _check_dtype(im, [do_im])
    _check_shape(im, [do_im])

    if DEBUGGING and len(im[0].shape) == 2:
        plot_transformed_sample(im[0], do_im[0], ['raw', 'do'])
Esempio n. 3
0
def _test_Resample(im_seg, resample_transform, native_resolution, is_2D=False):
    im, seg = im_seg
    metadata_ = SampleMetadata({
        'zooms':
        native_resolution,
        'data_shape':
        im[0].shape if len(im[0].shape) == 3 else list(im[0].shape) + [1],
        'data_type':
        'im'
    })
    metadata_in = [metadata_
                   for _ in im] if isinstance(im, list) else SampleMetadata({})

    # Resample input data
    do_im, do_metadata = resample_transform(sample=im, metadata=metadata_in)
    # Undo Resample on input data
    undo_im, _ = resample_transform.undo_transform(sample=do_im,
                                                   metadata=do_metadata)

    # Resampler for label data
    resample_transform.interpolation_order = 0
    metadata_ = SampleMetadata({
        'zooms':
        native_resolution,
        'data_shape':
        seg[0].shape if len(seg[0].shape) == 3 else list(seg[0].shape) + [1],
        'data_type':
        'gt'
    })
    metadata_in = [metadata_ for _ in seg] if isinstance(
        seg, list) else SampleMetadata({})
    # Resample label data
    do_seg, do_metadata = resample_transform(sample=seg, metadata=metadata_in)
    # Undo Resample on label data
    undo_seg, _ = resample_transform.undo_transform(sample=do_seg,
                                                    metadata=do_metadata)

    # Check data type and shape
    _check_dtype(im, [undo_im])
    _check_shape(im, [undo_im])
    _check_dtype(seg, [undo_seg])
    _check_shape(seg, [undo_seg])

    # Check data content and data shape between input data and undo
    for idx, i in enumerate(im):
        # Plot for debugging
        if DEBUGGING and is_2D:
            plot_transformed_sample(im[idx], undo_im[idx], ['raw', 'undo'])
            plot_transformed_sample(seg[idx], undo_seg[idx], ['raw', 'undo'])
        # Data consistency
        assert dice_score(undo_seg[idx], seg[idx]) > 0.8
Esempio n. 4
0
def test_ElasticTransform(im_seg, elastic_transform):
    im, seg = im_seg
    metadata_in = [SampleMetadata({})
                   for _ in im] if isinstance(im, list) else SampleMetadata({})

    # Transform on Numpy
    do_im, metadata_do = elastic_transform(im.copy(), metadata_in)
    do_seg, metadata_do = elastic_transform(seg.copy(), metadata_do)

    if DEBUGGING and len(im[0].shape) == 2:
        plot_transformed_sample(im[0], do_im[0], ['raw', 'do'])
        plot_transformed_sample(seg[0], do_seg[0], ['raw', 'do'])

    _check_dtype(im, [do_im])
    _check_shape(im, [do_im])
    _check_dtype(seg, [do_seg])
    _check_shape(seg, [do_seg])
Esempio n. 5
0
def _test_Crop(im_seg, crop_transform):
    im, seg = im_seg
    metadata_ = SampleMetadata({'data_shape': im[0].shape, 'crop_params': {}})
    metadata_in = [metadata_ for _ in im] if isinstance(im, list) else {}
    if crop_transform.__class__.__name__ == "ROICrop":
        _, metadata_in = crop_transform(seg, metadata_in)
        for metadata in metadata_in:
            assert crop_transform.__class__.__name__ in metadata["crop_params"]

    # Apply transform
    do_im, do_metadata = crop_transform(im, metadata_in)
    do_seg, do_seg_metadata = crop_transform(seg, metadata_in)
    crop_transfrom_size = crop_transform.size if not len(
        do_im[0].shape) == 2 else crop_transform.size[:2]

    # Loop and check
    for idx, i in enumerate(im):
        # Check data shape
        assert list(do_im[idx].shape) == crop_transfrom_size
        assert list(do_seg[idx].shape) == crop_transfrom_size
        # Check metadata
        assert do_metadata[idx]['crop_params'][crop_transform.__class__.__name__] == \
               do_seg_metadata[idx]['crop_params'][crop_transform.__class__.__name__]

    # Apply undo transform
    undo_im, _ = crop_transform.undo_transform(do_im, do_metadata)
    undo_seg, _ = crop_transform.undo_transform(do_seg, do_seg_metadata)

    # Check data type and shape
    _check_dtype(im, [undo_im])
    _check_shape(im, [undo_im])
    _check_dtype(seg, [undo_seg])
    _check_shape(seg, [undo_seg])

    # Loop and check
    for idx, i in enumerate(im):
        # Check data consistency
        fh, fw, fd, _, _, _ = do_metadata[idx]['crop_params'][
            crop_transform.__class__.__name__]
        th, tw, td = crop_transform.size
        if not td:
            assert np.array_equal(i[fh:fh + th, fw:fw + tw],
                                  undo_im[idx][fh:fh + th, fw:fw + tw])
            assert np.array_equal(seg[idx][fh:fh + th, fw:fw + tw],
                                  undo_seg[idx][fh:fh + th, fw:fw + tw])
            # Plot for debugging
            if DEBUGGING:
                plot_transformed_sample(seg[idx], undo_seg[idx],
                                        ['raw', 'undo'])
                plot_transformed_sample(i, undo_im[idx], ['raw', 'undo'])
        else:
            assert np.array_equal(
                i[fh:fh + th, fw:fw + tw, fd:fd + td],
                undo_im[idx][fh:fh + th, fw:fw + tw, fd:fd + td])
            assert np.array_equal(
                seg[idx][fh:fh + th, fw:fw + tw, fd:fd + td],
                undo_seg[idx][fh:fh + th, fw:fw + tw, fd:fd + td])
Esempio n. 6
0
def test_NormalizeInstance(im_seg):
    im, seg = im_seg
    metadata_in = [SampleMetadata({})
                   for _ in im] if isinstance(im, list) else SampleMetadata({})

    # Transform on Numpy
    transform = NormalizeInstance()
    do_im, _ = transform(im.copy(), metadata_in)
    # Check normalization
    for i in do_im:
        assert abs(np.mean(i) - 0.0) <= 1e-2
        assert abs(np.std(i) - 1.0) <= 1e-2

    # Transform on Tensor
    tensor, metadata_tensor = NumpyToTensor()(im, metadata_in)
    do_tensor, _ = transform(tensor, metadata_tensor)
    # Check normalization
    assert abs(do_tensor.mean() - 0.0) <= 1e-2
    assert abs(do_tensor.std() - 1.0) <= 1e-2
Esempio n. 7
0
def test_DilateGT(im_seg, dilate_transform):
    im, seg = im_seg
    metadata_in = [SampleMetadata({})
                   for _ in im] if isinstance(im, list) else SampleMetadata({})

    # Transform on Numpy
    do_seg, metadata_do = dilate_transform(seg.copy(), metadata_in)

    if DEBUGGING and len(im[0].shape) == 2:
        plot_transformed_sample(seg[0], do_seg[0], ['raw', 'do'])

    # Check data shape and type
    _check_shape(ref=seg, list_mov=[do_seg])

    # Check data augmentation
    for idx, i in enumerate(seg):
        # data aug
        assert np.sum((do_seg[idx] > 0).astype(np.int)) >= np.sum(i)
        # same number of objects
        assert label((do_seg[idx] > 0).astype(np.int))[1] == label(i)[1]
Esempio n. 8
0
def test_HistogramClipping(im_seg):
    im, _ = im_seg
    # Transform
    transform = HistogramClipping()
    # Apply Transform
    metadata = [SampleMetadata({})
                for _ in im] if isinstance(im, list) else SampleMetadata({})
    do_im, _ = transform(sample=im, metadata=metadata)
    # Check result has the same number of contrasts
    assert len(do_im) == len(im)
    # Check clipping
    min_percentile = transform.min_percentile
    max_percentile = transform.max_percentile
    for i, r in zip(im, do_im):
        assert isclose(np.min(r),
                       np.percentile(i, min_percentile),
                       rel_tol=1e-02)
        assert isclose(np.max(r),
                       np.percentile(i, max_percentile),
                       rel_tol=1e-02)
Esempio n. 9
0
def test_RandomReverse(im_seg, reverse_transform):
    im, seg = im_seg
    metadata_in = [SampleMetadata({})
                   for _ in im] if isinstance(im, list) else SampleMetadata({})

    # Transform on Numpy
    do_im, metadata_do = reverse_transform(im.copy(), metadata_in)
    do_seg, metadata_do = reverse_transform(seg.copy(), metadata_do)

    # Transform on Numpy
    undo_im, _ = reverse_transform.undo_transform(do_im, metadata_do)
    undo_seg, _ = reverse_transform.undo_transform(do_seg, metadata_do)

    if DEBUGGING and len(im[0].shape) == 2:
        plot_transformed_sample(seg[0], do_seg[0], ['raw', 'do'])
        plot_transformed_sample(seg[0], undo_seg[0], ['raw', 'undo'])

    _check_dtype(im, [do_im])
    _check_shape(im, [do_im])
    _check_dtype(seg, [do_seg])
    _check_shape(seg, [do_seg])
Esempio n. 10
0
def test_NumpyToTensor(im_seg):
    im, seg = im_seg
    metadata_in = [SampleMetadata({})
                   for _ in im] if isinstance(im, list) else SampleMetadata({})

    # Transform
    transform = NumpyToTensor()

    for im_cur in [im, seg]:
        # Numpy to Tensor
        do_im, do_metadata = transform(sample=im_cur, metadata=metadata_in)
        for idx, i in enumerate(do_im):
            assert torch.is_tensor(i)

        # Tensor to Numpy
        undo_im, undo_metadata = transform.undo_transform(sample=do_im,
                                                          metadata=do_metadata)
        for idx, i in enumerate(undo_im):
            assert isinstance(i, np.ndarray)
            assert np.array_equal(i, im_cur[idx])
            assert i.dtype == im_cur[idx].dtype