Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
  • Loading branch information
wyli committed Feb 10, 2022
1 parent 01853a6 commit 2c09f8e
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions monai/transforms/io/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import inspect
import logging
import sys
import traceback
import warnings
from pathlib import Path
from typing import Dict, List, Optional, Sequence, Union
Expand Down Expand Up @@ -184,7 +185,7 @@ def __call__(self, filename: Union[Sequence[PathLike], PathLike], reader: Option
"""
filename = tuple(f"{Path(s).expanduser()}" for s in ensure_tuple(filename)) # allow Path objects
img = None
img, err = None, []
if reader is not None:
img = reader.read(filename) # runtime specified reader
else:
Expand All @@ -197,20 +198,24 @@ def __call__(self, filename: Union[Sequence[PathLike], PathLike], reader: Option
try:
img = reader.read(filename)
except Exception as e:
logging.getLogger(self.__class__.__name__).debug(
f"{reader.__class__.__name__}: unable to load {filename}.\n" f"Error: {e}"
err.append(traceback.format_exc())
logging.getLogger(self.__class__.__name__).debug(e, exc_info=True)
logging.getLogger(self.__class__.__name__).info(
f"{reader.__class__.__name__}: unable to load {filename}.\n"
)
else:
err = []
break

if img is None or reader is None:
if isinstance(filename, tuple) and len(filename) == 1:
filename = filename[0]
msg = "\n".join([f"{e}" for e in err])
raise RuntimeError(
f"cannot find a suitable reader for file: {filename}.\n"
f"{self.__class__.__name__} cannot find a suitable reader for file: {filename}.\n"
" Please install the reader libraries, see also the installation instructions:\n"
" https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies.\n"
f" The current registered: {self.readers}.\n"
f" The current registered: {self.readers}.\n{msg}"
)

img_array, meta_data = reader.get_data(img)
Expand Down Expand Up @@ -280,6 +285,8 @@ class SaveImage(Transform):
see also: `monai.data.image_writer.SUPPORTED_WRITERS`.
writer: a customised image writer to save data arrays.
if `None`, use the default writer from `monai.data.image_writer` according to `output_ext`.
channel_dim: the index of the channel dimension. Default to `0`.
`None` to indicate no channel dimension.
"""

def __init__(
Expand All @@ -299,6 +306,7 @@ def __init__(
print_log: bool = True,
output_format: str = "",
writer: Optional[image_writer.ImageWriter] = None,
channel_dim: Optional[int] = 0,
) -> None:
self.folder_layout = FolderLayout(
output_dir=output_dir,
Expand All @@ -319,7 +327,7 @@ def __init__(
if self.output_ext == ".dcm" and _output_dtype not in (np.uint8, np.uint16):
_output_dtype = np.uint8
self.init_kwargs = {"output_dtype": _output_dtype, "scale": scale}
self.data_kwargs = {"squeeze_end_dims": squeeze_end_dims, "channel_dim": 0}
self.data_kwargs = {"squeeze_end_dims": squeeze_end_dims, "channel_dim": channel_dim}
self.meta_kwargs = {"resample": resample, "mode": mode, "padding_mode": padding_mode, "dtype": dtype}
self.write_kwargs = {"verbose": print_log}
self._data_index = 0
Expand Down Expand Up @@ -354,7 +362,10 @@ def __call__(self, img: Union[torch.Tensor, np.ndarray], meta_data: Optional[Dic
subject = meta_data[Key.FILENAME_OR_OBJ] if meta_data else str(self._data_index)
patch_index = meta_data.get(Key.PATCH_INDEX, None) if meta_data else None
filename = self.folder_layout.filename(subject=f"{subject}", idx=patch_index)
if meta_data and len(ensure_tuple(meta_data.get("spatial_shape", ()))) == len(img.shape):
self.data_kwargs["channel_dim"] = None

err = []
for writer_cls in self.writers:
try:
writer_obj = writer_cls(**self.init_kwargs)
Expand All @@ -363,16 +374,18 @@ def __call__(self, img: Union[torch.Tensor, np.ndarray], meta_data: Optional[Dic
writer_obj.write(filename, **self.write_kwargs)
self.writer_obj = writer_obj
except Exception as e:
logging.getLogger(self.__class__.__name__).exception(e, exc_info=True)
err.append(traceback.format_exc())
logging.getLogger(self.__class__.__name__).debug(e, exc_info=True)
logging.getLogger(self.__class__.__name__).info(
f"{writer_cls.__class__.__name__}: unable to write {filename}."
f"{writer_cls.__class__.__name__}: unable to write {filename}.\n"
)
else:
self._data_index += 1
return img
msg = "\n".join([f"{e}" for e in err])
raise RuntimeError(
f"cannot find a suitable writer for {filename}.\n"
f"{self.__class__.__name__} cannot find a suitable writer for {filename}.\n"
" Please install the writer libraries, see also the installation instructions:\n"
" https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies.\n"
f" The current registered writers for {self.output_ext}: {self.writers}.\n"
f" The current registered writers for {self.output_ext}: {self.writers}.\n{msg}"
)

0 comments on commit 2c09f8e

Please sign in to comment.