From 848005d2d325f20e78c82efff6df987c9e63eb9f Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Sun, 14 Jul 2024 09:30:34 -0500 Subject: [PATCH] Fix failing unit-test test_wsireader (#7905) Conversion of units when the unit if '' caused the test to fail. ```bash pytest -k tiff ``` FAILED tests/test_wsireader.py::TestTiffFile::test_resolution_mpp_0__home_johnsonhj_src_MONAI_tests_testing_data_temp_wsi_generic_tiff_tiff - ValueError: Currently, it only supports length conversion but `` is given. FAILED tests/test_wsireader.py::TestTiffFile::test_resolution_mpp_0__home_johnsonhj_src_MONAI_tests_testing_data_temp_wsi_generic_tiff_tiff - AttributeError: 'ConvertUnits' object has no attribute 'conversion_factor' ### Description Fixes a failing test. ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [x] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. Signed-off-by: Hans Johnson Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/data/wsi_reader.py | 2 +- monai/utils/misc.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/monai/data/wsi_reader.py b/monai/data/wsi_reader.py index b31d4d9c3a..96d84d8cf1 100644 --- a/monai/data/wsi_reader.py +++ b/monai/data/wsi_reader.py @@ -1098,7 +1098,7 @@ def get_mpp(self, wsi, level: int) -> tuple[float, float]: unit = wsi.pages[level].tags.get("ResolutionUnit") if unit is not None: unit = str(unit.value)[8:] - else: + if unit is None or len(unit) == 0: warnings.warn("The resolution unit is missing. `micrometer` will be used as default.") unit = "micrometer" diff --git a/monai/utils/misc.py b/monai/utils/misc.py index 96a59e1b35..e8a46ecc61 100644 --- a/monai/utils/misc.py +++ b/monai/utils/misc.py @@ -814,7 +814,7 @@ def __init__(self, input_unit: str, target_unit: str) -> None: "Both input and target units should be from the same quantity. " f"Input quantity is {input_base} while target quantity is {target_base}" ) - self._calculate_conversion_factor() + self.conversion_factor = self._calculate_conversion_factor() def _get_valid_unit_and_base(self, unit): unit = str(unit).lower() @@ -841,7 +841,7 @@ def _calculate_conversion_factor(self): return 1.0 input_power = self._get_unit_power(self.input_unit) target_power = self._get_unit_power(self.target_unit) - self.conversion_factor = 10 ** (input_power - target_power) + return 10 ** (input_power - target_power) def __call__(self, value: int | float) -> Any: return float(value) * self.conversion_factor