Ejemplo n.º 1
0
def test_image_builder_tiff(tmpdir_factory,):
    # Copy resource files to writable temp folder
    temp_dir = Path(tmpdir_factory.mktemp("temp") / "resources")
    output_dir = Path(tmpdir_factory.mktemp("output"))

    shutil.copytree(
        RESOURCE_PATH,
        temp_dir,
        ignore=shutil.ignore_patterns("dicom*", "complex_tiff", "dzi_tiff"),
    )
    files = [Path(d[0]).joinpath(f) for d in os.walk(temp_dir) for f in d[2]]

    image_builder_result = _build_files(
        builder=image_builder_tiff, files=files, output_directory=output_dir
    )

    expected_files = [
        temp_dir / "valid_tiff.tif",
        temp_dir / "no_dzi.tif",
    ]

    assert sorted(image_builder_result.consumed_files) == sorted(
        expected_files
    )

    file_to_pk = {i.name: i.pk for i in image_builder_result.new_images}

    for file in expected_files:
        pk = file_to_pk[file.name]
        assert os.path.isfile(output_dir / file.name / f"{pk}.tif")

    # Assert that both tiff images are imported
    assert len(image_builder_result.new_image_files) == 2
def test_image_builder_with_other_file_extension(tmpdir):
    dest = Path(tmpdir) / "image10x10x10.mha"
    shutil.copy(RESOURCE_PATH / dest.name, dest)
    files = {Path(d[0]).joinpath(f) for d in os.walk(tmpdir) for f in d[2]}
    with TemporaryDirectory() as output:
        result = _build_files(builder=image_builder_nifti,
                              files=files,
                              output_directory=output)
    assert result.consumed_files == set()
    assert len(result.new_images) == 0
Ejemplo n.º 3
0
def test_error_handling(tmpdir_factory):
    # Copy resource files to writable temp folder
    # The content files are dummy files and won't compile to tiff.
    # The point is to test the loading of gc_files and make sure all
    # related files are associated with the gc_file
    temp_dir = Path(tmpdir_factory.mktemp("temp") / "resources")
    shutil.copytree(RESOURCE_PATH / "complex_tiff", temp_dir)
    files = {Path(d[0]).joinpath(f) for d in os.walk(temp_dir) for f in d[2]}

    with TemporaryDirectory() as output:
        image_builder_result = _build_files(
            builder=image_builder_tiff, files=files, output_directory=output
        )

    assert len(image_builder_result.file_errors) == 14
Ejemplo n.º 4
0
def test_image_builder_fallback_corrupt_file(tmpdir):
    src = RESOURCE_PATH / "corrupt.png"
    dest = Path(tmpdir) / src.name
    shutil.copy(str(src), str(dest))

    files = {Path(d[0]).joinpath(f) for d in os.walk(tmpdir) for f in d[2]}
    with TemporaryDirectory() as output:
        result = _build_files(
            builder=image_builder_fallback,
            files=files,
            output_directory=output,
        )

    assert result.file_errors == {
        dest: [format_error("Not a valid image file")],
    }
    assert result.consumed_files == set()
Ejemplo n.º 5
0
def test_image_builder_fallback(tmpdir, src, colorspace):
    dest = Path(tmpdir) / src.name
    shutil.copy(str(src), str(dest))
    files = {Path(d[0]).joinpath(f) for d in os.walk(tmpdir) for f in d[2]}
    with TemporaryDirectory() as output:
        result = _build_files(
            builder=image_builder_fallback,
            files=files,
            output_directory=output,
        )
    assert result.consumed_files == {dest}
    assert len(result.new_images) == 1
    image = result.new_images.pop()
    assert image.color_space == colorspace
    assert image.voxel_width_mm is None
    assert image.voxel_height_mm is None
    assert image.voxel_depth_mm is None
def test_image_builder_dicom_4dct(tmpdir):
    files = {Path(d[0]).joinpath(f) for d in os.walk(DICOM_DIR) for f in d[2]}
    result = _build_files(builder=image_builder_dicom,
                          files=files,
                          output_directory=tmpdir)
    assert result.consumed_files == {
        Path(DICOM_DIR).joinpath(f"{x}.dcm")
        for x in range(1, 77)
    }

    assert len(result.new_images) == 1
    image = Image(**asdict(result.new_images.pop()))
    assert image.shape == [19, 4, 2, 3]
    assert len(result.new_image_files) == 1
    mha_file_obj = [
        x for x in result.new_image_files if x.file.suffix == ".mha"
    ][0]

    headers = parse_mh_header(mha_file_obj.file)

    direction = headers["TransformMatrix"].split()
    origin = headers["Offset"].split()
    spacing = headers["ElementSpacing"].split()
    exposures = headers["Exposures"].split()
    content_times = headers["ContentTimes"].split()

    assert len(exposures) == 19
    assert exposures == [str(x) for x in range(100, 2000, 100)]
    assert len(content_times) == 19
    assert content_times == [str(x) for x in range(214501, 214520)]

    dcm_ref = pydicom.dcmread(str(DICOM_DIR / "1.dcm"))
    assert np.array_equal(
        np.array(list(map(float, direction))).reshape((4, 4)), np.eye(4))
    assert np.allclose(
        list(map(float, spacing))[:2],
        list(map(
            float,
            list(dcm_ref.PixelSpacing),
        )),
    )
    assert np.allclose(
        list(map(float, origin)),
        list(map(float, dcm_ref.ImagePositionPatient)) + [0.0],
    )
def test_dicom_rescaling(folder, element_type, tmpdir):
    """
    2.dcm in dicom_intercept and dicom_slope has been modified to add a
    small intercept (0.01) or slope (1.001) respectively.
    """
    files = [
        Path(d[0]).joinpath(f) for d in os.walk(RESOURCE_PATH / folder)
        for f in d[2]
    ]
    result = _build_files(builder=image_builder_dicom,
                          files=files,
                          output_directory=tmpdir)

    assert len(result.new_image_files) == 1
    mha_file_obj = [
        x for x in result.new_image_files if x.file.suffix == ".mha"
    ][0]

    headers = parse_mh_header(mha_file_obj.file)
    assert headers["ElementType"] == element_type
def test_dicom_window_level(tmpdir):
    files = {
        Path(d[0]).joinpath(f)
        for d in os.walk(RESOURCE_PATH / "dicom") for f in d[2]
    }
    result = _build_files(builder=image_builder_dicom,
                          files=files,
                          output_directory=tmpdir)

    assert len(result.new_image_files) == 1
    mha_file_obj = [
        x for x in result.new_image_files if x.file.suffix == ".mha"
    ][0]

    headers = parse_mh_header(mha_file_obj.file)
    assert headers["WindowCenter"] == "30"
    assert headers["WindowWidth"] == "200"

    assert len(result.new_images) == 1
    image_obj = result.new_images.pop()
    assert image_obj.window_center == 30.0
    assert image_obj.window_width == 200.0
def test_image_builder_nifti(tmpdir_factory, src: Path):
    dest = Path(tmpdir_factory.mktemp("input"))

    shutil.copy(src, dest / src.name)

    files = {*dest.glob("*")}

    result = _build_files(
        builder=image_builder_nifti,
        files=files,
        output_directory=tmpdir_factory.mktemp("output"),
    )

    assert result.consumed_files == files
    assert len(result.new_images) == 1

    image = result.new_images.pop()
    assert image.color_space == ColorSpace.GRAY.value
    assert image.width == 10
    assert image.height == 11
    assert image.depth == 12
    assert image.voxel_width_mm == pytest.approx(1.0)
    assert image.voxel_height_mm == pytest.approx(2.0)
    assert image.voxel_depth_mm == pytest.approx(3.0)