Skip to content

Commit

Permalink
Fix failing unit-test test_wsireader
Browse files Browse the repository at this point in the history
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 <hans-johnson@uiowa.edu>
  • Loading branch information
hjmjohnson committed Jul 8, 2024
1 parent 3a0c2d5 commit 24c0c42
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion monai/data/wsi_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions monai/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 24c0c42

Please sign in to comment.