From 24c0c4282a4d88b35db7579ca319ce67af40bc05 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Mon, 8 Jul 2024 11:05:40 -0500 Subject: [PATCH] Fix failing unit-test test_wsireader Conversion of units when the unit if '' caused the test to fail. 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' Signed-off-by: Hans Johnson --- 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 b31d4d9c3ab..96d84d8cf19 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 96a59e1b355..e8a46ecc617 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