@@ -820,7 +820,10 @@ def get_data(
820820 """
821821 # Verify inputs
822822 if level is None :
823- level = self ._check_level (img , level )
823+ level = self .level
824+ max_level = self ._get_max_level (img )
825+ if level > max_level :
826+ raise ValueError (f"The maximum level of this image is { max_level } while level={ level } is requested)!" )
824827
825828 # Extract a region or the entire image
826829 region = self ._extract_region (img , location = location , size = size , level = level , dtype = dtype )
@@ -844,21 +847,19 @@ def get_data(
844847
845848 return patches , metadata
846849
847- def _check_level (self , img , level ):
848- level = self .level
850+ def _get_max_level (self , img_obj ):
851+ """
852+ Return the maximum number of levels in the whole slide image
853+ Args:
854+ img: the whole slide image object
849855
850- level_count = 0
856+ """
851857 if self .backend == "openslide" :
852- level_count = img .level_count
853- elif self .backend == "cucim" :
854- level_count = img .resolutions ["level_count" ]
855- elif self .backend == "tifffile" :
856- level_count = len (img .pages )
857-
858- if level > level_count - 1 :
859- raise ValueError (f"The maximum level of this image is { level_count - 1 } while level={ level } is requested)!" )
860-
861- return level
858+ return img_obj .level_count - 1
859+ if self .backend == "cucim" :
860+ return img_obj .resolutions ["level_count" ] - 1
861+ if self .backend == "tifffile" :
862+ return len (img_obj .pages ) - 1
862863
863864 def _get_image_size (self , img , size , level , location ):
864865 """
0 commit comments