Example #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, ...])
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, ...])
Example #3
0
 def __getitem__(self, index: int) -> OptionalIndexInputAndLabel:
     """
     :param index: The index of the sample to be fetched
     :return: The image and (fake) label tensors
     """
     filename = self.filenames[index]
     target = self.targets[index] if self.targets is not None else 0
     if is_dicom_file_path(str(filename)):
         scan_image = load_dicom_image(filename)
         # Dicom files have arbitrary pixel intensities, convert to [0,255] range so that PIL can
         # read the array into an Image object.
         scan_image = (scan_image - scan_image.min()) * 255. / (scan_image.max() - scan_image.min())
         # Load as PIL Image in grey-scale (convert("L") step), yields a 1-channel image.
         scan_image = Image.fromarray(scan_image).convert("L")
     else:
         # Load as PIL Image in grey-scale (convert("L") step), yields a 1-channel image with pixel values in range
         # [0,1] (float).
         scan_image = Image.open(filename).convert("L")
     if self.transforms is not None:
         scan_image = self.transforms(scan_image)
     return scan_image, target
Example #4
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, ...])
Example #6
0
def test_is_dicom_file(input: Tuple[str, bool]) -> None:
    file, expected = input
    assert is_dicom_file_path(file) == expected
    assert is_dicom_file_path(Path(file)) == expected