Пример #1
0
def test_load_dicom_image_random(test_output_dirs: OutputFolderForTests,
                                 is_signed: bool, is_monochrome2: bool, bits_stored: int) -> None:
    """
    Test loading of 2D Dicom images of type (uint16) and (int16).
    """
    array_size = (20, 30)
    if not is_signed:
        array = np.random.randint(0, 200, size=array_size, dtype='uint16')
    else:
        array = np.random.randint(-200, 200, size=array_size, dtype='int16')
    assert array.shape == array_size

    if is_monochrome2:
        to_write = array
    else:
        if not is_signed:
            to_write = 2 ** bits_stored - 1 - array
        else:
            to_write = -1 * array - 1

    dcm_file = test_output_dirs.root_dir / "file.dcm"
    assert is_dicom_file_path(dcm_file)
    write_test_dicom(array=to_write, path=dcm_file)

    with mock.patch.object(sitk.ImageFileReader, 'GetMetaData',
                           new=get_mock_function(is_monochrome2=is_monochrome2, bits_stored=bits_stored)):
        image = load_dicom_image(dcm_file)
        assert image.ndim == 3 and image.shape == (1,) + array_size
        assert np.array_equal(image, array[None, ...])

        image_and_segmentation = load_image_in_known_formats(dcm_file, load_segmentation=False)
        assert image_and_segmentation.images.ndim == 3 and image_and_segmentation.images.shape == (1,) + array_size
        assert np.array_equal(image_and_segmentation.images, array[None, ...])
Пример #2
0
def plot_image_from_filepath(filepath: Path, im_size: Tuple) -> bool:
    """
    Plots a 2D image given the filepath. Returns false if the image could not be plotted (for example, if it was 3D).
    :param filepath: Path to image
    :param im_size: Display size for image
    :return: True if image was plotted, False otherwise
    """

    image = load_image_in_known_formats(filepath,
                                        load_segmentation=False).images
    if not image.ndim == 2 and not (image.ndim == 3 and image.shape[0] == 1):
        print_header(f"Image has unsupported shape {image.shape}", level=0)
        return False

    if image.ndim == 3 and image.shape[0] == 1:
        image = image.squeeze(0)

    # normalize to make sure pixels are plottable
    min = image.min()
    max = image.max()
    image = (image - min) / (max - min)

    image = image * 255.
    image = np.repeat(image[:, :, None], 3, 2)
    image = image.astype(np.uint8)
    display(Image.fromarray(image).resize(im_size))
    return True
Пример #3
0
def test_load_numpy_image(test_output_dirs: OutputFolderForTests) -> None:
    array_size = (20, 30, 40)
    array = np.ones(array_size)
    assert array.shape == array_size
    npy_file = test_output_dirs.root_dir / "file.npy"
    assert is_numpy_file_path(npy_file)
    np.save(npy_file, array)
    image = load_numpy_image(npy_file)
    assert image.shape == array_size
    image_and_segmentation = load_image_in_known_formats(npy_file, load_segmentation=False)
    assert image_and_segmentation.images.shape == array_size
def test_load_dicom_image_ones(test_output_dirs: TestOutputDirectories,
                               is_signed: bool, is_monochrome2: bool) -> None:
    """
    Test loading of 2D Dicom images filled with binary array of type (uint16) and (int16).
    """
    array_size = (20, 30)
    if not is_signed:
        array = np.ones(array_size, dtype='uint16')
        array[::2] = 0
    else:
        array = -1 * np.ones(array_size, dtype='int16')
        array[::2] = 0

    assert array.shape == array_size

    if is_monochrome2:
        to_write = array
    else:
        if not is_signed:
            to_write = np.zeros(array_size, dtype='uint16')
            to_write[::2] = 1
        else:
            to_write = np.zeros(array_size, dtype='int16')
            to_write[::2] = -1

    dcm_file = Path(test_output_dirs.root_dir) / "file.dcm"
    assert is_dicom_file_path(dcm_file)
    write_test_dicom(array=to_write, path=dcm_file)

    with mock.patch.object(sitk.ImageFileReader,
                           'GetMetaData',
                           new=get_mock_function(is_monochrome2=is_monochrome2,
                                                 bits_stored=1)):
        image = load_dicom_image(dcm_file)
        assert image.ndim == 3 and image.shape == (1, ) + array_size
        assert np.array_equal(image, array[None, ...])

        image_and_segmentation = load_image_in_known_formats(
            dcm_file, load_segmentation=False)
        assert image_and_segmentation.images.ndim == 3 and image_and_segmentation.images.shape == (
            1, ) + array_size
        assert np.array_equal(image_and_segmentation.images, array[None, ...])
Пример #5
0
def test_load_dicom_image_ones(test_output_dirs: OutputFolderForTests,
                               is_signed: bool, is_monochrome2: bool) -> None:
    """
    Test loading of 2D Dicom images filled with binary array of type (uint16) and (int16).
    """
    array_size = (20, 30)
    if not is_signed:
        array = np.ones(array_size, dtype='uint16')
        array[::2] = 0
    else:
        array = -1 * np.ones(array_size, dtype='int16')
        array[::2] = 0

    assert array.shape == array_size

    if is_monochrome2:
        to_write = array
    else:
        if not is_signed:
            to_write = np.zeros(array_size, dtype='uint16')
            to_write[::2] = 1
        else:
            to_write = np.zeros(array_size, dtype='int16')
            to_write[::2] = -1

    dcm_file = test_output_dirs.root_dir / "file.dcm"
    assert is_dicom_file_path(dcm_file)
    write_test_dicom(array=to_write,
                     path=dcm_file,
                     is_monochrome2=is_monochrome2,
                     bits_stored=1)

    image = load_dicom_image(dcm_file)
    assert image.ndim == 2 and image.shape == array_size
    assert np.array_equal(image, array)

    image_and_segmentation = load_image_in_known_formats(
        dcm_file, load_segmentation=False)
    assert image_and_segmentation.images.ndim == 2 and image_and_segmentation.images.shape == array_size
    assert np.array_equal(image_and_segmentation.images, array)
def test_load_dicom_image_random_signed(
        test_output_dirs: TestOutputDirectories) -> None:
    """
    Test loading of 2D Dicom images of type (int16).
    """
    array_size = (20, 30)
    array = np.random.randint(-200, 200, size=array_size, dtype='int16')
    assert array.shape == array_size

    dcm_file = Path(test_output_dirs.root_dir) / "file.dcm"
    assert is_dicom_file_path(dcm_file)
    write_test_dicom(array, dcm_file)

    image = load_dicom_image(dcm_file)
    assert image.ndim == 3 and image.shape == (1, ) + array_size
    assert np.array_equal(image, array[None, ...])

    image_and_segmentation = load_image_in_known_formats(
        dcm_file, load_segmentation=False)
    assert image_and_segmentation.images.ndim == 3 and image_and_segmentation.images.shape == (
        1, ) + array_size
    assert np.array_equal(image_and_segmentation.images, array[None, ...])
Пример #7
0
def test_load_image(file_path: str, expected_shape: Tuple) -> None:
    full_file_path = full_ml_test_data_path() / file_path
    image_and_segmentation = load_image_in_known_formats(full_file_path, load_segmentation=False)
    assert image_and_segmentation.images.shape == expected_shape