Skip to content

Commit 95273c4

Browse files
markbadernormanrz
andauthored
Show pims import errors only once (#1266)
* wip show pims import errors only once. * Avoid wk-libs version warnings on slurm and show pims errors only in from_images. * update changelog. * use env vars for restrict number of warnings * switch order of if statements. --------- Co-authored-by: Norman Rzepka <[email protected]>
1 parent bedef8e commit 95273c4

File tree

3 files changed

+52
-31
lines changed

3 files changed

+52
-31
lines changed

webknossos/Changelog.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ For upgrade instructions, please check the respective _Breaking Changes_ section
1717
### Added
1818

1919
### Changed
20+
- Warnings regarding missing pims image readers are only printed once during conversion. Error messages regarding wk-libs version are not displayed in slurm environments. [#1266](https://github.com/scalableminds/webknossos-libs/pull/1266)
2021

2122
### Fixed
2223

@@ -47,7 +48,6 @@ For upgrade instructions, please check the respective _Breaking Changes_ section
4748
- Fixed the tests for the newest WEBKNOSSOS version. [#1328](https://github.com/scalableminds/webknossos-libs/pull/1328)
4849

4950

50-
5151
## [2.4.1](https://github.com/scalableminds/webknossos-libs/releases/tag/v2.4.1) - 2025-07-03
5252
[Commits](https://github.com/scalableminds/webknossos-libs/compare/v2.4.0...v2.4.1)
5353

@@ -94,7 +94,7 @@ For upgrade instructions, please check the respective _Breaking Changes_ section
9494
### Added
9595
- Added `layers_to_ignore` argument to `Dataset.copy_dataset`. [#1321](https://github.com/scalableminds/webknossos-libs/pull/1321)
9696

97-
### Changed
97+
### Fixed
9898
- Make number of retries and backoff factor configurable (mainly for tensorstore reads/writes). See `DEFAULT_NUM_RETRIES` and `DEFAULT_BACKOFF_FACTOR` environment variables. [#1323](https://github.com/scalableminds/webknossos-libs/pull/1323)
9999

100100

webknossos/webknossos/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636

3737
if not current_version == "0.0.0":
3838
import multiprocessing
39+
import os
3940

40-
if multiprocessing.parent_process() is None:
41+
if (
42+
multiprocessing.parent_process() is None
43+
and os.getenv("WEBKNOSSOS_SKIP_VERSION_CHECK", "False") != "True"
44+
):
45+
os.environ["WEBKNOSSOS_SKIP_VERSION_CHECK"] = "True"
4146
# Schedule the version check to run non-blocking in a background thread
4247
check_version_in_background(current_version)

webknossos/webknossos/dataset/_utils/pims_images.py

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections.abc import Iterable, Iterator, Sequence
33
from contextlib import AbstractContextManager, contextmanager, nullcontext
44
from itertools import chain
5-
from os import PathLike
5+
from os import PathLike, environ
66
from pathlib import Path
77
from typing import TypeVar, Union, cast
88
from urllib.error import HTTPError
@@ -22,6 +22,49 @@
2222
pims.ImageIOReader.frame_shape = pims.FramesSequenceND.frame_shape
2323

2424

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+
2568
def _assume_color_channel(dim_size: int, dtype: np.dtype) -> bool:
2669
return dim_size == 1 or (dim_size == 3 and dtype == np.dtype("uint8"))
2770

@@ -306,33 +349,6 @@ def _disable_pil_image_size_limit(self) -> None:
306349
def _try_open_pims_images(
307350
self, original_images: str | list[str], exceptions: list[Exception]
308351
) -> 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-
336352
if self._use_bioformats:
337353
return None
338354

0 commit comments

Comments
 (0)