Esempio n. 1
0
def test_load_from_url():
    url = "https://downloads.openmicroscopy.org/images/OME-TIFF/2016-06/bioformats-artificial/multi-channel.ome.tif"
    img = intake_io.imload(url)
    assert img["image"].shape == (3, 167, 439)
    assert img["image"].dtype == np.int8
    assert intake_io.get_axes(img) == "cyx"

    url = "https://downloads.openmicroscopy.org/images/OME-TIFF/2016-06/bioformats-artificial/multi-channel-4D-series.ome.tif"
    img = intake_io.imload(url)
    assert img["image"].shape == (7, 3, 5, 167, 439)
    assert img["image"].dtype == np.int8
    assert intake_io.get_axes(img) == "tczyx"
Esempio n. 2
0
def test_round_trip_nifti_version_1(tmp_path):
    fpath = os.path.join(tmp_path, "compressed.nii.gz")
    if os.path.exists(fpath):
        os.remove(fpath)

    for img0, shape, axes, spacing, units in random_images():
        if axes != "zyx":
            continue
        try:
            assert not os.path.exists(fpath)
            intake_io.imsave(img0, fpath, nifti_version=1)
            assert os.path.exists(fpath)

            with intake_io.source.NiftiSource(fpath) as src:
                img1 = intake_io.imload(src)["image"]

            assert axes == intake_io.get_axes(img1)
            assert shape == img1.shape
            #assert spacing == intake_io.get_spacing(img1)
            np.testing.assert_array_almost_equal(spacing, intake_io.get_spacing(img1))
            assert units == intake_io.get_spacing_units(img1)
            assert np.mean(img0.data) not in (0, 1)
            assert np.mean(img0.data) == np.mean(img1.data)
        finally:
            if os.path.exists(fpath):
                os.remove(fpath)
Esempio n. 3
0
def test_round_trip_uncompressed(tmp_path):
    fpath = os.path.join(tmp_path, "uncompressed.klb")
    if os.path.exists(fpath):
        os.remove(fpath)

    for img0, shape, axes, spacing, units in random_images():
        if "i" in axes:
            continue
        try:
            assert not os.path.exists(fpath)
            intake_io.imsave(img0, fpath, compress=False)
            assert os.path.exists(fpath)

            with intake_io.source.KlbSource(fpath) as src:
                img1 = intake_io.imload(src)["image"]

            assert axes == intake_io.get_axes(img1)
            assert shape == img1.shape
            _spacing = [1.0 if i is None else i for i in spacing]
            np.testing.assert_array_almost_equal(_spacing,
                                                 intake_io.get_spacing(img1))
            # assert units == intake_io.get_spacing_units(img1)
            assert np.mean(img0.data) not in (0, 1)
            assert np.mean(img0.data) == np.mean(img1.data)
        finally:
            if os.path.exists(fpath):
                os.remove(fpath)
Esempio n. 4
0
def test_round_trip_uncompressed(tmp_path):
    fpath = os.path.join(tmp_path, "uncompressed.tif")
    if os.path.exists(fpath):
        os.remove(fpath)

    for img0, shape, axes, spacing, units in random_images():
        if img0.dtype in (np.int8, np.int32, np.int64, np.uint32, np.uint64, np.float64):
            continue
        if "i" in axes:
            continue
        try:
            assert not os.path.exists(fpath)
            intake_io.imsave(img0, fpath, compress=False)
            assert os.path.exists(fpath)

            with intake_io.source.TifSource(fpath) as src:
                img1 = intake_io.imload(src)["image"]

            assert axes == intake_io.get_axes(img1)
            assert shape == img1.shape
            assert spacing == intake_io.get_spacing(img1)
            assert units == intake_io.get_spacing_units(img1)
            assert np.mean(img0.data) not in (0, 1)
            assert np.mean(img0.data) == np.mean(img1.data)
        finally:
            if os.path.exists(fpath):
                os.remove(fpath)
Esempio n. 5
0
def test_loading(tmp_path):
    url = "https://downloads.openmicroscopy.org/images/OME-TIFF/2016-06/bioformats-artificial/multi-channel.ome.tif"
    fpath = os.path.join(tmp_path, url.rsplit("/", 1)[-1])
    with open(fpath, "wb") as fh:
        fh.write(requests.get(url).content)
    img = intake_io.imload(fpath)
    assert img["image"].shape == (3, 167, 439)
    assert img["image"].dtype == np.int8
    assert intake_io.get_axes(img) == "cyx"

    url = "https://downloads.openmicroscopy.org/images/OME-TIFF/2016-06/bioformats-artificial/multi-channel-4D-series.ome.tif"
    fpath = os.path.join(tmp_path, url.rsplit("/", 1)[-1])
    with open(fpath, "wb") as fh:
        fh.write(requests.get(url).content)
    img = intake_io.imload(fpath)
    assert img["image"].shape == (7, 3, 5, 167, 439)
    assert img["image"].dtype == np.int8
    assert intake_io.get_axes(img) == "tczyx"
Esempio n. 6
0
def test_load_from_url():
    urls = [
        "https://nifti.nimh.nih.gov/nifti-1/data/avg152T1_LR_nifti.nii.gz",
        "https://nifti.nimh.nih.gov/pub/dist/data/nifti2/avg152T1_LR_nifti2.nii.gz"
    ]
    for url in urls:
        img = intake_io.imload(url)
        assert img["image"].shape == (91, 109, 91)
        if "nifti-1" in url:
            assert img["image"].dtype == np.uint8
        else:
            assert img["image"].dtype == np.float32
        assert intake_io.get_axes(img) == "zyx"
        assert intake_io.get_spacing(img) == (2.0, 2.0, 2.0)
        assert intake_io.get_spacing_units(img) == ("mm", "mm", "mm")
Esempio n. 7
0
def test_round_trip_uncompressed(tmp_path):
    fpath = os.path.join(tmp_path, "uncompressed.nrrd")
    if os.path.exists(fpath):
        os.remove(fpath)

    for img0, shape, axes, spacing, units in random_images():
        try:
            assert not os.path.exists(fpath)
            intake_io.imsave(img0, fpath, compress=False)
            assert os.path.exists(fpath)

            with intake_io.source.NrrdSource(fpath) as src:
                img1 = intake_io.imload(src)["image"]

            assert axes == intake_io.get_axes(img1)
            assert shape == img1.shape
            assert spacing == intake_io.get_spacing(img1)
            assert units == intake_io.get_spacing_units(img1)
            assert np.mean(img0.data) not in (0, 1)
            assert np.mean(img0.data) == np.mean(img1.data)
        finally:
            if os.path.exists(fpath):
                os.remove(fpath)
Esempio n. 8
0
def test_round_trip_compressed(tmp_path):
    fpaths = {
        False: os.path.join(tmp_path, "uncompressed.klb"),
        True: os.path.join(tmp_path, "compressed.klb")
    }
    for fpath in fpaths.values():
        if os.path.exists(fpath):
            os.remove(fpath)

    for img0 in ramp_images():
        if "i" in intake_io.get_axes(img0):
            continue
        try:
            for compress, fpath in fpaths.items():
                if os.path.exists(fpath):
                    os.remove(fpath)

                assert not os.path.exists(fpath)
                intake_io.imsave(img0, fpath, compress=compress)
                assert os.path.exists(fpath)

                with intake_io.source.KlbSource(fpath) as src:
                    img1 = intake_io.imload(src)["image"]
                img2 = intake_io.imload(fpath)["image"]

                assert img0.shape == img1.shape == img2.shape
                assert img0.dims == img1.dims == img2.dims
                assert np.mean(img0.data) not in (0, 1)
                assert np.mean(img0.data) == np.mean(img1.data) == np.mean(
                    img2.data)

            assert os.path.getsize(
                fpaths[True]) <= 0.9 * os.path.getsize(fpaths[False])
        finally:
            for fpath in fpaths.values():
                if os.path.exists(fpath):
                    os.remove(fpath)