Skip to content

Commit

Permalink
WIP: ENH: Add cuCIM file reading support
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Mar 5, 2024
1 parent 5e2e6d7 commit ae53013
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
24 changes: 24 additions & 0 deletions ngff_zarr/cli_input_to_ngff_image.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import sys

import numpy as np
import zarr
from dask.array.image import imread as daimread
from rich import print

from .detect_cli_io_backend import ConversionBackend
from .from_ngff_zarr import from_ngff_zarr
from .itk_image_to_ngff_image import itk_image_to_ngff_image
from .methods._support import _spatial_dims
from .ngff_image import NgffImage
from .to_ngff_image import to_ngff_image

Expand Down Expand Up @@ -48,6 +50,28 @@ def imread(filename):
return itk_image_to_ngff_image(image)
image = itk.imread(input)
return itk_image_to_ngff_image(image)
if backend is ConversionBackend.CUCIM:
try:
import cucim

cuimage = cucim.CuImage(str(input[0]))
data = np.array(cuimage)
dims = tuple(d for d in cuimage.dims.lower())
spatial_dims = set(dims).intersection(_spatial_dims)
spatial_dims = [d for d in dims if d in spatial_dims]
spatial_dims_str = "".join(spatial_dims).upper()
translation = {d: 0.0 for d in spatial_dims}
for idx, dim in enumerate(spatial_dims):
# cucim: Should origin have a dim_order argument like spacing?
translation[dim] = cuimage.origin[idx]
spacing = cuimage.spacing(spatial_dims_str)
scale = {d: 1.0 for d in spatial_dims}
for idx, dim in enumerate(spatial_dims):
scale[dim] = spacing[idx]
return to_ngff_image(data, dims=dims, translation=translation, scale=scale)
except ImportError:
print("[red]Please install the [i]cucim[/i] package.")
sys.exit(1)
if backend is ConversionBackend.TIFFFILE:
try:
import tifffile
Expand Down
8 changes: 8 additions & 0 deletions ngff_zarr/detect_cli_io_backend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib.util
import sys
from enum import Enum
from pathlib import Path
Expand All @@ -8,6 +9,7 @@
("ZARR_ARRAY", "zarr"),
("ITKWASM", "itkwasm_image_io"),
("ITK", "itk"),
("CUCIM", "cucim"),
("TIFFFILE", "tifffile"),
("IMAGEIO", "imageio"),
]
Expand Down Expand Up @@ -103,6 +105,12 @@ def detect_cli_io_backend(input: List[str]) -> ConversionBackend:
if extension in itk_supported_extensions:
return ConversionBackend.ITK

if importlib.util.find_spec("cucim") is not None:
cucim_supported_extensions = (".svs", ".tif", ".tiff")

if extension in cucim_supported_extensions:
return ConversionBackend.CUCIM

try:
import tifffile

Expand Down
16 changes: 16 additions & 0 deletions test/test_cli_input_to_ngff_image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from ngff_zarr import ConversionBackend, cli_input_to_ngff_image

from ._data import test_data_dir
Expand Down Expand Up @@ -37,6 +38,21 @@ def test_cli_input_to_ngff_image_tifffile(input_images): # noqa: ARG001
assert image.dims == ("z", "y", "x")


def test_cli_input_to_ngff_image_cucim(input_images): # noqa: ARG001
pytest.importorskip("cucim")
input = [
test_data_dir
/ "input"
/ "TCGA-C8-A26W-01A-01-TSA.5870b3d4-a81e-423e-bb93-5897ebc922a3.svs"
]
image = cli_input_to_ngff_image(ConversionBackend.CUCIM, input)
assert image.dims == ("y", "x", "c")
assert image.translation["x"] == 0.0
assert image.translation["y"] == 0.0
assert image.scale["x"] == 1.0
assert image.scale["y"] == 1.0


def test_cli_input_to_ngff_image_imageio(input_images): # noqa: ARG001
input = [
test_data_dir / "input" / "cthead1.png",
Expand Down
7 changes: 6 additions & 1 deletion test/test_detect_cli_input_backend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib.util

from ngff_zarr import ConversionBackend, detect_cli_io_backend


Expand All @@ -18,7 +20,10 @@ def test_detect_tifffile_input_backend():
f"file{extension}",
]
)
assert backend == ConversionBackend.TIFFFILE
if importlib.util.find_spec("cucim") is not None:
assert backend == ConversionBackend.CUCIM
else:
assert backend == ConversionBackend.TIFFFILE


def test_detect_imageio_input_backend():
Expand Down

0 comments on commit ae53013

Please sign in to comment.