Skip to content

Commit cbe02ba

Browse files
committed
simplify io with imageio.v3
1 parent 7be6a7e commit cbe02ba

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

bioimageio/core/io.py

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import collections.abc
2+
from os import PathLike
23
from pathlib import Path
34
from typing import Any, Mapping, Optional, Sequence, Union
45

56
import imageio
7+
from imageio.v3 import imread, imwrite
68
from loguru import logger
79
from numpy.typing import NDArray
810
from pydantic import BaseModel, ConfigDict, TypeAdapter
@@ -16,63 +18,49 @@
1618
from .tensor import Tensor
1719

1820

19-
def load_image(path: Path, is_volume: bool) -> NDArray[Any]:
20-
"""load a single image as numpy array"""
21+
def load_image(path: Path, is_volume: Optional[bool] = None) -> NDArray[Any]:
22+
"""load a single image as numpy array
23+
24+
Args:
25+
path: image path
26+
is_volume: deprecated
27+
"""
2128
ext = path.suffix
2229
if ext == ".npy":
2330
return load_array(path)
2431
else:
25-
return imageio.volread(path) if is_volume else imageio.imread(path)
32+
return imread(path) # pyright: ignore[reportUnknownVariableType]
2633

2734

2835
def load_tensor(path: Path, axes: Optional[Sequence[AxisLike]] = None) -> Tensor:
2936
# TODO: load axis meta data
30-
array = load_image(
31-
path,
32-
is_volume=(
33-
axes is None or sum(Axis.create(a).type != "channel" for a in axes) > 2
34-
),
35-
)
37+
array = load_image(path)
3638

3739
return Tensor.from_numpy(array, dims=axes)
3840

3941

40-
def save_tensor(path: Path, tensor: Tensor) -> None:
42+
def save_tensor(path: PathLike[str], tensor: Tensor) -> None:
4143
# TODO: save axis meta data
4244

4345
data: NDArray[Any] = tensor.data.to_numpy()
46+
path = Path(path)
4447
path.parent.mkdir(exist_ok=True, parents=True)
4548
if path.suffix == ".npy":
4649
save_array(path, data)
4750
else:
48-
if singleton_axes := [a for a, s in tensor.tagged_shape.items() if s == 1]:
49-
tensor = tensor[{a: 0 for a in singleton_axes}]
50-
singleton_axes_msg = f"(without singleton axes {singleton_axes}) "
51-
else:
52-
singleton_axes_msg = ""
53-
54-
# attempt to write a volume or an image with imageio
55-
error = None
56-
for d in (data, data.T):
57-
for write in ( # pyright: ignore[reportUnknownVariableType]
58-
imageio.volwrite,
59-
imageio.imwrite,
60-
):
61-
try:
62-
write(path, d)
63-
except ValueError as e:
64-
error = e
65-
else:
66-
logger.info(
67-
"wrote tensor {} {}to {} using imageio.{}",
68-
dict(tensor.tagged_shape),
69-
singleton_axes_msg,
70-
path,
71-
write.__name__,
72-
)
73-
74-
if error is not None:
75-
raise error
51+
# if singleton_axes := [a for a, s in tensor.tagged_shape.items() if s == 1]:
52+
# tensor = tensor[{a: 0 for a in singleton_axes}]
53+
# singleton_axes_msg = f"(without singleton axes {singleton_axes}) "
54+
# else:
55+
singleton_axes_msg = ""
56+
57+
logger.debug(
58+
"writing tensor {} {}to {}",
59+
dict(tensor.tagged_shape),
60+
singleton_axes_msg,
61+
path,
62+
)
63+
imwrite(path, data)
7664

7765

7866
def save_sample(path: Union[Path, str, PerMember[Path]], sample: Sample) -> None:

0 commit comments

Comments
 (0)