Skip to content

Commit 848005d

Browse files
hjmjohnsonKumoLiu
andauthored
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 <hans-johnson@uiowa.edu> Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
1 parent 14b086b commit 848005d

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

monai/data/wsi_reader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ def get_mpp(self, wsi, level: int) -> tuple[float, float]:
10981098
unit = wsi.pages[level].tags.get("ResolutionUnit")
10991099
if unit is not None:
11001100
unit = str(unit.value)[8:]
1101-
else:
1101+
if unit is None or len(unit) == 0:
11021102
warnings.warn("The resolution unit is missing. `micrometer` will be used as default.")
11031103
unit = "micrometer"
11041104

monai/utils/misc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ def __init__(self, input_unit: str, target_unit: str) -> None:
814814
"Both input and target units should be from the same quantity. "
815815
f"Input quantity is {input_base} while target quantity is {target_base}"
816816
)
817-
self._calculate_conversion_factor()
817+
self.conversion_factor = self._calculate_conversion_factor()
818818

819819
def _get_valid_unit_and_base(self, unit):
820820
unit = str(unit).lower()
@@ -841,7 +841,7 @@ def _calculate_conversion_factor(self):
841841
return 1.0
842842
input_power = self._get_unit_power(self.input_unit)
843843
target_power = self._get_unit_power(self.target_unit)
844-
self.conversion_factor = 10 ** (input_power - target_power)
844+
return 10 ** (input_power - target_power)
845845

846846
def __call__(self, value: int | float) -> Any:
847847
return float(value) * self.conversion_factor

0 commit comments

Comments
 (0)