|
2 | 2 | from collections.abc import Iterable, Iterator, Sequence |
3 | 3 | from contextlib import AbstractContextManager, contextmanager, nullcontext |
4 | 4 | from itertools import chain |
5 | | -from os import PathLike |
| 5 | +from os import PathLike, environ |
6 | 6 | from pathlib import Path |
7 | 7 | from typing import TypeVar, Union, cast |
8 | 8 | from urllib.error import HTTPError |
|
22 | 22 | pims.ImageIOReader.frame_shape = pims.FramesSequenceND.frame_shape |
23 | 23 |
|
24 | 24 |
|
| 25 | +def _pims_imports() -> str | None: |
| 26 | + import_exceptions = [] |
| 27 | + |
| 28 | + try: |
| 29 | + from .pims_czi_reader import PimsCziReader # noqa: F401 unused-import |
| 30 | + except ImportError as import_error: |
| 31 | + import_exceptions.append(f"PimsCziReader: {import_error.msg}") |
| 32 | + |
| 33 | + try: |
| 34 | + from .pims_dm_readers import ( # noqa: F401 unused-import |
| 35 | + PimsDm3Reader, |
| 36 | + PimsDm4Reader, |
| 37 | + ) |
| 38 | + except ImportError as import_error: |
| 39 | + import_exceptions.append(f"PimsDmReaders: {import_error.msg}") |
| 40 | + |
| 41 | + try: |
| 42 | + from .pims_tiff_reader import PimsTiffReader # noqa: F401 unused-import |
| 43 | + except ImportError as import_error: |
| 44 | + import_exceptions.append(f"PimsTiffReader: {import_error.msg}") |
| 45 | + |
| 46 | + if import_exceptions: |
| 47 | + import_exception_string = "".join( |
| 48 | + f"\t- {import_exception}\n" for import_exception in import_exceptions |
| 49 | + ) |
| 50 | + |
| 51 | + return import_exception_string |
| 52 | + return None |
| 53 | + |
| 54 | + |
| 55 | +if (pims_warnings := _pims_imports()) is not None: |
| 56 | + if environ.get("WEBKNOSSOS_SHOWED_PIMS_IMPORT_WARNING", "False") == "False": |
| 57 | + # If the environment variable is not set, we assume that the user has not seen the warning yet. |
| 58 | + # We set it to True to prevent showing the warning again. |
| 59 | + environ["WEBKNOSSOS_SHOWED_PIMS_IMPORT_WARNING"] = "True" |
| 60 | + warnings.warn( |
| 61 | + f"[WARNING] Not all pims readers could be imported:\n{pims_warnings}Install the readers you need or use 'webknossos[all]' to install all readers.", |
| 62 | + category=UserWarning, |
| 63 | + source=None, |
| 64 | + stacklevel=2, |
| 65 | + ) |
| 66 | + |
| 67 | + |
25 | 68 | def _assume_color_channel(dim_size: int, dtype: np.dtype) -> bool: |
26 | 69 | return dim_size == 1 or (dim_size == 3 and dtype == np.dtype("uint8")) |
27 | 70 |
|
@@ -306,33 +349,6 @@ def _disable_pil_image_size_limit(self) -> None: |
306 | 349 | def _try_open_pims_images( |
307 | 350 | self, original_images: str | list[str], exceptions: list[Exception] |
308 | 351 | ) -> pims.FramesSequence | None: |
309 | | - import_exceptions = [] |
310 | | - |
311 | | - try: |
312 | | - from .pims_czi_reader import PimsCziReader # noqa: F401 unused-import |
313 | | - except ImportError as import_error: |
314 | | - import_exceptions.append(f"PimsCziReader: {import_error.msg}") |
315 | | - |
316 | | - try: |
317 | | - from .pims_dm_readers import ( # noqa: F401 unused-import |
318 | | - PimsDm3Reader, |
319 | | - PimsDm4Reader, |
320 | | - ) |
321 | | - except ImportError as import_error: |
322 | | - import_exceptions.append(f"PimsDmReaders: {import_error.msg}") |
323 | | - |
324 | | - try: |
325 | | - from .pims_tiff_reader import PimsTiffReader # noqa: F401 unused-import |
326 | | - except ImportError as import_error: |
327 | | - import_exceptions.append(f"PimsTiffReader: {import_error.msg}") |
328 | | - |
329 | | - if import_exceptions: |
330 | | - import_exception_string = "\n\t" + "\n\t".join(import_exceptions) |
331 | | - warnings.warn( |
332 | | - 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.", |
333 | | - category=UserWarning, |
334 | | - ) |
335 | | - |
336 | 352 | if self._use_bioformats: |
337 | 353 | return None |
338 | 354 |
|
|
0 commit comments