Skip to content

Commit feac0dc

Browse files
author
Nikolas Schmitz
committed
Small changes
1 parent a9fe772 commit feac0dc

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

monai/data/wsi_reader.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,10 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
607607
"""
608608
Returns the representation of the whole slide image at a given micro-per-pixel (mpp) resolution.
609609
The optional tolerance parameters are considered at the level whose mpp value is closest to the one provided by the user.
610-
If the user-provided mpp is larger than the mpp of the closest level—indicating that the closest level has a higher resolution than requested—the image is downscaled to a resolution that matches the user-provided mpp.
611-
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.
610+
If the user-provided mpp is larger than the mpp of the closest level,
611+
the image is downscaled to a resolution that matches the user-provided mpp.
612+
Otherwise, if the closest level's resolution is not sufficient to meet the user's requested resolution,
613+
the next lower level (which has a higher resolution) is chosen.
612614
The image from this level is then down-scaled to achieve a resolution at the user-provided mpp value.
613615
614616
Args:
@@ -765,8 +767,10 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
765767
"""
766768
Returns the representation of the whole slide image at a given micro-per-pixel (mpp) resolution.
767769
The optional tolerance parameters are considered at the level whose mpp value is closest to the one provided by the user.
768-
If the user-provided mpp is larger than the mpp of the closest level—indicating that the closest level has a higher resolution than requested—the image is downscaled to a resolution that matches the user-provided mpp.
769-
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.
770+
If the user-provided mpp is larger than the mpp of the closest level,
771+
the image is downscaled to a resolution that matches the user-provided mpp.
772+
Otherwise, if the closest level's resolution is not sufficient to meet the user's requested resolution,
773+
the next lower level (which has a higher resolution) is chosen.
770774
The image from this level is then down-scaled to achieve a resolution at the user-provided mpp value.
771775
772776
Args:
@@ -786,7 +790,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
786790
# -> Should not throw ValueError, instead just return the closest value; how to select tolerances?
787791

788792
mpp_closest_lvl = mpp_list[closest_lvl]
789-
closest_lvl_dim = wsi.resolutions['level_dimensions'][closest_lvl]
793+
closest_lvl_dim = wsi.resolutions['level_dimensions'][closest_lvl] # x,y notation
790794

791795
print(f'Closest Level: {closest_lvl} with MPP: {mpp_closest_lvl}')
792796
mpp_closest_lvl_x, mpp_closest_lvl_y = mpp_closest_lvl
@@ -805,7 +809,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
805809
if within_tolerance:
806810
# Take closest_level and continue with returning img at level
807811
print(f'User-provided MPP lies within tolerance of level {closest_lvl}, returning wsi at this level.')
808-
closest_lvl_wsi = wsi.read_region((0, 0), level=closest_lvl, size=closest_lvl_dim, num_workers=self.num_workers)
812+
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
809813

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

826-
# closest_lvl_wsi = closest_lvl_wsi.resize((target_res_x, target_res_y), Image.BILINEAR)
827-
closest_lvl_wsi = cucim_resize(wsi_arr, (target_res_x, target_res_y), order=0)
830+
closest_lvl_wsi = cucim_resize(wsi_arr, (target_res_x, target_res_y), order=0) # output_shape in row, col notation
828831
print(f'Case 1: Downscaling using factor {(ds_factor_x, ds_factor_y)}')
829832

830833
else:
@@ -843,7 +846,6 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
843846
target_res_x = int(np.round(closest_lvl_dim[1] * ds_factor_x))
844847
target_res_y = int(np.round(closest_lvl_dim[0] * ds_factor_y))
845848

846-
# closest_lvl_wsi = closest_lvl_wsi.resize((target_res_x, target_res_y), Image.BILINEAR)
847849
closest_lvl_wsi = cucim_resize(wsi_arr, (target_res_x, target_res_y), order=0)
848850
print(f'Case 2: Downscaling using factor {(ds_factor_x, ds_factor_y)}, now from level {closest_lvl}')
849851

@@ -1050,8 +1052,10 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
10501052
"""
10511053
Returns the representation of the whole slide image at a given micro-per-pixel (mpp) resolution.
10521054
The optional tolerance parameters are considered at the level whose mpp value is closest to the one provided by the user.
1053-
If the user-provided mpp is larger than the mpp of the closest level—indicating that the closest level has a higher resolution than requested—the image is downscaled to a resolution that matches the user-provided mpp.
1054-
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.
1055+
If the user-provided mpp is larger than the mpp of the closest level,
1056+
the image is downscaled to a resolution that matches the user-provided mpp.
1057+
Otherwise, if the closest level's resolution is not sufficient to meet the user's requested resolution,
1058+
the next lower level (which has a higher resolution) is chosen.
10551059
The image from this level is then down-scaled to achieve a resolution at the user-provided mpp value.
10561060
10571061
Args:
@@ -1123,7 +1127,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
11231127
target_res_x = int(np.round(closest_lvl_dim[0] * ds_factor_x))
11241128
target_res_y = int(np.round(closest_lvl_dim[1] * ds_factor_y))
11251129

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

11291133
wsi_arr = np.array(closest_lvl_wsi)
@@ -1305,7 +1309,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
13051309
"""
13061310
Returns the representation of the whole slide image at a given micro-per-pixel (mpp) resolution.
13071311
The optional tolerance parameters are considered at the level whose mpp value is closest to the one provided by the user.
1308-
If the user-provided mpp is larger than the mpp of the closest level—indicating that the closest level has a higher resolution than requested—the image is downscaled to a resolution that matches the user-provided mpp.
1312+
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.
13091313
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.
13101314
The image from this level is then down-scaled to achieve a resolution at the user-provided mpp value.
13111315
@@ -1319,8 +1323,8 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
13191323

13201324
pil_image, _ = optional_import("PIL", name="Image")
13211325
user_mpp_x, user_mpp_y = mpp
1322-
mpp_list = [self.get_mpp(wsi, lvl) for lvl in range(len(wsi.pages))] # QuPath show 4 levels in the pyramid, but len(wsi.pages) is 1?
1323-
closest_lvl = self._find_closest_level("mpp", mpp, mpp_list, 0, 5)
1326+
mpp_list = [self.get_mpp(wsi, lvl) for lvl in range(len(wsi.pages))]
1327+
closest_lvl = self._find_closest_level("mpp", mpp, mpp_list, 0, 5)
13241328
# -> Should not throw ValueError, instead just return the closest value; how to select tolerances?
13251329

13261330
mpp_closest_lvl = mpp_list[closest_lvl]
@@ -1358,7 +1362,6 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
13581362
ds_factor_x = mpp_closest_lvl_x / user_mpp_x
13591363
ds_factor_y = mpp_closest_lvl_y / user_mpp_y
13601364

1361-
# closest_lvl_wsi = wsi.read_region((0, 0), level=closest_lvl, size=closest_lvl_dim, num_workers=self.num_workers)
13621365
closest_lvl_wsi = pil_image.fromarray(wsi.pages[closest_lvl].asarray()) # Might be suboptimal
13631366

13641367
target_res_x = int(np.round(closest_lvl_dim[0] * ds_factor_x))
@@ -1378,7 +1381,7 @@ def get_img_at_mpp(self, wsi, mpp: tuple, atol: float = 0.00, rtol: float = 0.05
13781381

13791382
closest_lvl_dim = lvl_dims[closest_lvl]
13801383
closest_lvl_dim = (closest_lvl_dim[1], closest_lvl_dim[0])
1381-
# closest_lvl_wsi = wsi.read_region((0, 0), level=closest_lvl, size=closest_lvl_dim, num_workers=self.num_workers)
1384+
13821385
closest_lvl_wsi = pil_image.fromarray(wsi.pages[closest_lvl].asarray()) # Might be suboptimal
13831386

13841387
target_res_x = int(np.round(closest_lvl_dim[0] * ds_factor_x))

0 commit comments

Comments
 (0)