Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Schmitz committed Mar 24, 2024
1 parent feac0dc commit 8194026
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions monai/data/wsi_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
# -> Should not throw ValueError, instead just return the closest value; how to select tolerances?

mpp_closest_lvl = mpp_list[closest_lvl]
closest_lvl_dim = wsi.resolutions['level_dimensions'][closest_lvl] # x,y notation
closest_lvl_dim = wsi.resolutions['level_dimensions'][closest_lvl]

print(f'Closest Level: {closest_lvl} with MPP: {mpp_closest_lvl}')
mpp_closest_lvl_x, mpp_closest_lvl_y = mpp_closest_lvl
Expand All @@ -809,7 +809,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
if within_tolerance:
# Take closest_level and continue with returning img at level
print(f'User-provided MPP lies within tolerance of level {closest_lvl}, returning wsi at this level.')
closest_lvl_wsi = wsi.read_region((0, 0), level=closest_lvl, size=closest_lvl_dim, num_workers=self.num_workers) # size in x,y notation
closest_lvl_wsi = wsi.read_region((0, 0), level=closest_lvl, size=closest_lvl_dim, num_workers=self.num_workers)

else:
# If mpp_closest_level < mpp -> closest_level has higher res than img at mpp => downscale from closest_level to mpp
Expand All @@ -827,13 +827,13 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
target_res_x = int(np.round(closest_lvl_dim[1] * ds_factor_x))
target_res_y = int(np.round(closest_lvl_dim[0] * ds_factor_y))

closest_lvl_wsi = cucim_resize(wsi_arr, (target_res_x, target_res_y), order=0) # output_shape in row, col notation
closest_lvl_wsi = cucim_resize(wsi_arr, (target_res_x, target_res_y), order=0)
print(f'Case 1: Downscaling using factor {(ds_factor_x, ds_factor_y)}')

else:
# Else: increase resolution (ie, decrement level) and then downsample
closest_lvl = closest_lvl - 1
mpp_closest_lvl = mpp_list[closest_lvl] # Update MPP
mpp_closest_lvl = mpp_list[closest_lvl]
mpp_closest_lvl_x, mpp_closest_lvl_y = mpp_closest_lvl

ds_factor_x = mpp_closest_lvl_x / user_mpp_x
Expand Down Expand Up @@ -1115,7 +1115,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
else:
# Else: increase resolution (ie, decrement level) and then downsample
closest_lvl = closest_lvl - 1
mpp_closest_lvl = mpp_list[closest_lvl] # Update MPP
mpp_closest_lvl = mpp_list[closest_lvl]
mpp_closest_lvl_x, mpp_closest_lvl_y = mpp_closest_lvl

ds_factor_x = mpp_closest_lvl_x / user_mpp_x
Expand All @@ -1127,7 +1127,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
target_res_x = int(np.round(closest_lvl_dim[0] * ds_factor_x))
target_res_y = int(np.round(closest_lvl_dim[1] * ds_factor_y))

closest_lvl_wsi = closest_lvl_wsi.resize((target_res_x, target_res_y), pil_image.BILINEAR) # Output size in x,y notation
closest_lvl_wsi = closest_lvl_wsi.resize((target_res_x, target_res_y), pil_image.BILINEAR)
print(f'Case 2: Downscaling using factor {(ds_factor_x, ds_factor_y)}, now from level {closest_lvl}')

wsi_arr = np.array(closest_lvl_wsi)
Expand Down Expand Up @@ -1289,9 +1289,9 @@ def get_mpp(self, wsi, level: int) -> tuple[float, float]:
and wsi.pages[level].tags["YResolution"].value
):
unit = wsi.pages[level].tags.get("ResolutionUnit")
if unit is not None: # Needs to be extended
# unit = str(unit.value)[8:]
unit = str(unit.value.name).lower() # TODO: Merge both methods
if unit is not None: # Needs to be improved
unit = str(unit.value)[8:]
# unit = str(unit.value.name).lower() # TODO: Merge both methods

else:
warnings.warn("The resolution unit is missing. `micrometer` will be used as default.")
Expand All @@ -1309,8 +1309,10 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
"""
Returns the representation of the whole slide image at a given micro-per-pixel (mpp) resolution.
The optional tolerance parameters are considered at the level whose mpp value is closest to the one provided by the user.
If the user-provided mpp is larger than the mpp of the closest level the image is downscaled to a resolution that matches the user-provided mpp.
Otherwise, if the closest level's resolution is not sufficient to meet the user's requested resolution, the next lower level (which has a higher resolution) is chosen.
If the user-provided mpp is larger than the mpp of the closest level,
the image is downscaled to a resolution that matches the user-provided mpp.
Otherwise, if the closest level's resolution is not sufficient to meet the user's requested resolution,
the next lower level (which has a higher resolution) is chosen.
The image from this level is then down-scaled to achieve a resolution at the user-provided mpp value.
Args:
Expand All @@ -1329,7 +1331,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05

mpp_closest_lvl = mpp_list[closest_lvl]

lvl_dims = [self.get_size(wsi, lvl) for lvl in range(len(wsi.pages))] # Returns size in (height, width)
lvl_dims = [self.get_size(wsi, lvl) for lvl in range(len(wsi.pages))]
closest_lvl_dim = lvl_dims[closest_lvl]
closest_lvl_dim = (closest_lvl_dim[1], closest_lvl_dim[0])

Expand Down

0 comments on commit 8194026

Please sign in to comment.