@@ -127,7 +127,7 @@ def set_dtype(self, dtype):
127127 self .dtype = np .dtype (dtype )
128128
129129 def set_device (self , device ):
130- if device is None or isinstance (device , ( torch .device , str ) ):
130+ if device is None or isinstance (device , torch .device | str ):
131131 self .device = device
132132 else :
133133 raise ValueError (f"`device` must be `torch.device`, `str` or `None` but { type (device )} is given." )
@@ -358,7 +358,7 @@ def get_data(
358358 metadata_list : list = []
359359
360360 # CuImage object is iterable, so ensure_tuple won't work on single object
361- if not isinstance (wsi , ( list , tuple ) ):
361+ if not isinstance (wsi , list | tuple ):
362362 wsi = (wsi ,)
363363 for each_wsi in ensure_tuple (wsi ):
364364 # get the valid level based on resolution info
@@ -835,7 +835,9 @@ def get_mpp(self, wsi, level: int) -> tuple[float, float]:
835835
836836 raise ValueError ("`mpp` cannot be obtained for this file. Please use `level` instead." )
837837
838- def get_wsi_at_mpp (self , wsi , mpp : float | tuple [float , float ], atol : float = 0.00 , rtol : float = 0.05 ) -> Any :
838+ def get_wsi_at_mpp (
839+ self , wsi , mpp : float | tuple [float , float ], atol : float = 0.00 , rtol : float = 0.05
840+ ) -> np .ndarray :
839841 """
840842 Returns the representation of the whole slide image at a given micro-per-pixel (mpp) resolution.
841843 The optional tolerance parameters are considered at the level whose mpp value is closest to the one provided by the user.
@@ -852,7 +854,7 @@ def get_wsi_at_mpp(self, wsi, mpp: float | tuple[float, float], atol: float = 0.
852854 rtol: the acceptable relative tolerance for resolution in micro per pixel.
853855
854856 Returns:
855- Cupy array containing the whole slide image at the requested MPP resolution.
857+ Numpy array containing the whole slide image at the requested MPP resolution.
856858
857859 """
858860 cp , _ = optional_import ("cupy" )
@@ -1423,7 +1425,8 @@ def get_wsi_at_mpp(
14231425 if within_tolerance :
14241426 # If the image at the desired mpp resolution is within tolerances, return the image at closest_level.
14251427 # TiffFile does not expose `read_region`; read the whole page instead (consistent with `_get_patch`).
1426- closest_lvl_wsi = wsi .pages [closest_lvl ].asarray ()
1428+ pil_image , _ = optional_import ("PIL" , name = "Image" )
1429+ closest_lvl_wsi = pil_image .fromarray (wsi .pages [closest_lvl ].asarray ())
14271430
14281431 elif closest_level_is_bigger :
14291432 # Otherwise, select the level closest to the desired mpp with a higher resolution and downsample it.
@@ -1437,6 +1440,9 @@ def get_wsi_at_mpp(
14371440 closest_lvl = closest_lvl - 1
14381441 closest_lvl_wsi = self ._resize_to_mpp_res (wsi , closest_lvl , mpp_list , mpp )
14391442
1443+ # Convert to specified mode to normalize dtype
1444+ closest_lvl_wsi = closest_lvl_wsi .convert (self .mode )
1445+
14401446 wsi_arr = np .array (closest_lvl_wsi )
14411447
14421448 # Ensure channel dimension exists for grayscale
@@ -1561,4 +1567,4 @@ def _resize_to_mpp_res(self, wsi, closest_lvl, mpp_list, user_mpp: tuple):
15611567 # PIL `resize` takes (W, H); use the same row/col order as OpenSlide.
15621568 closest_lvl_wsi = closest_lvl_wsi .resize ((target_res_y , target_res_x ), pil_image .BILINEAR )
15631569
1564- return closest_lvl_wsi
1570+ return closest_lvl_wsi
0 commit comments