Skip to content

Show pims import errors only once #1266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 39 additions & 27 deletions webknossos/webknossos/dataset/_utils/pims_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,45 @@
pims.ImageIOReader.frame_shape = pims.FramesSequenceND.frame_shape


def _pims_imports() -> str | None:
import_exceptions = []

try:
from .pims_czi_reader import PimsCziReader # noqa: F401 unused-import
except ImportError as import_error:
import_exceptions.append(f"PimsCziReader: {import_error.msg}")

try:
from .pims_dm_readers import ( # noqa: F401 unused-import
PimsDm3Reader,
PimsDm4Reader,
)
except ImportError as import_error:
import_exceptions.append(f"PimsDmReaders: {import_error.msg}")

try:
from .pims_tiff_reader import PimsTiffReader # noqa: F401 unused-import
except ImportError as import_error:
import_exceptions.append(f"PimsTiffReader: {import_error.msg}")

if import_exceptions:
import_exception_string = "".join(
f"\t- {import_exception}\n" for import_exception in import_exceptions
)

return import_exception_string
return None


if (errors := _pims_imports()) is not None:
warnings.warn(
f"[WARNING] Not all pims readers could be imported:\n{errors}Install the readers you need or use 'webknossos[all]' to install all readers.",
category=UserWarning,
source=None,
stacklevel=2,
)


def _assume_color_channel(dim_size: int, dtype: np.dtype) -> bool:
return dim_size == 1 or (dim_size == 3 and dtype == np.dtype("uint8"))

Expand Down Expand Up @@ -306,33 +345,6 @@ def _disable_pil_image_size_limit(self) -> None:
def _try_open_pims_images(
self, original_images: str | list[str], exceptions: list[Exception]
) -> pims.FramesSequence | None:
import_exceptions = []

try:
from .pims_czi_reader import PimsCziReader # noqa: F401 unused-import
except ImportError as import_error:
import_exceptions.append(f"PimsCziReader: {import_error.msg}")

try:
from .pims_dm_readers import ( # noqa: F401 unused-import
PimsDm3Reader,
PimsDm4Reader,
)
except ImportError as import_error:
import_exceptions.append(f"PimsDmReaders: {import_error.msg}")

try:
from .pims_tiff_reader import PimsTiffReader # noqa: F401 unused-import
except ImportError as import_error:
import_exceptions.append(f"PimsTiffReader: {import_error.msg}")

if import_exceptions:
import_exception_string = "\n\t" + "\n\t".join(import_exceptions)
warnings.warn(
f"[WARNING] Not all pims readers could be imported: {import_exception_string}\nInstall the readers you need or use 'webknossos[all]' to install all readers.",
category=UserWarning,
)

if self._use_bioformats:
return None

Expand Down
5 changes: 4 additions & 1 deletion webknossos/webknossos/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from ..geometry.nd_bounding_box import derive_nd_bounding_box_from_shape
from ._array import ArrayException, ArrayInfo, BaseArray
from ._metadata import DatasetMetadata
from ._utils import pims_images
from .defaults import (
DEFAULT_BIT_DEPTH,
DEFAULT_CHUNK_SHAPE,
Expand Down Expand Up @@ -240,6 +239,7 @@ def _to_callable(
use_bioformats: bool | None,
) -> Callable[[Path], str]:
ConversionLayerMapping = Dataset.ConversionLayerMapping
from ._utils import pims_images

if self == ConversionLayerMapping.ENFORCE_LAYER_PER_FILE:
return lambda p: p.as_posix().replace("/", "_")
Expand Down Expand Up @@ -897,6 +897,7 @@ def from_images(
This method needs extra packages like tifffile or pylibczirw.
Install with `pip install "webknossos[all]"` and `pip install --extra-index-url https://pypi.scm.io/simple/ "webknossos[czi]"`.
"""
from ._utils import pims_images

input_upath = UPath(input_path)

Expand Down Expand Up @@ -1650,6 +1651,8 @@ def add_layer_from_images(
* `truncate_rgba_to_rgb`: only applies if `allow_multiple_layers=True`, set to `False` to write four channels into layers instead of an RGB channel
* `executor`: pass a `ClusterExecutor` instance to parallelize the conversion jobs across the batches
"""
from ._utils import pims_images

if category is None:
image_path_for_category_guess: Path
if isinstance(images, str) or isinstance(images, PathLike):
Expand Down
Loading