Skip to content

Commit c9302e4

Browse files
authored
Improve WSIReader (#3241)
* Add level argument to __init__ for WSIReader Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com> * Change reader_lib to backend Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com> * Update some comments Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com> * Update docstring Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com>
1 parent 02efc60 commit c9302e4

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

monai/data/image_reader.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -667,22 +667,25 @@ def _get_spatial_shape(self, img):
667667

668668
class WSIReader(ImageReader):
669669
"""
670-
Read whole slide imaging and extract patches.
670+
Read whole slide images and extract patches.
671671
672672
Args:
673-
reader_lib: backend library to load the images, available options: "OpenSlide" or "cuCIM".
673+
backend: backend library to load the images, available options: "OpenSlide" or "cuCIM".
674+
level: the whole slide image level at which the image is extracted. (default=0)
675+
Note that this is overridden by the level argument in `get_data`.
674676
675677
"""
676678

677-
def __init__(self, reader_lib: str = "OpenSlide"):
679+
def __init__(self, backend: str = "OpenSlide", level: int = 0):
678680
super().__init__()
679-
self.reader_lib = reader_lib.lower()
680-
if self.reader_lib == "openslide":
681+
self.backend = backend.lower()
682+
if self.backend == "openslide":
681683
self.wsi_reader, *_ = optional_import("openslide", name="OpenSlide")
682-
elif self.reader_lib == "cucim":
684+
elif self.backend == "cucim":
683685
self.wsi_reader, *_ = optional_import("cucim", name="CuImage")
684686
else:
685-
raise ValueError('`reader_lib` should be either "cuCIM" or "OpenSlide"')
687+
raise ValueError('`backend` should be either "cuCIM" or "OpenSlide"')
688+
self.level = level
686689

687690
def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
688691
"""
@@ -696,19 +699,21 @@ def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
696699

697700
def read(self, data: Union[Sequence[str], str, np.ndarray], **kwargs):
698701
"""
699-
Read image data from specified file or files.
700-
Note that the returned object is CuImage or list of CuImage objects.
702+
Read image data from given file or list of files.
701703
702704
Args:
703705
data: file name or a list of file names to read.
704706
707+
Returns:
708+
image object or list of image objects
709+
705710
"""
706711
img_: List = []
707712

708713
filenames: Sequence[str] = ensure_tuple(data)
709714
for name in filenames:
710715
img = self.wsi_reader(name)
711-
if self.reader_lib == "openslide":
716+
if self.backend == "openslide":
712717
img.shape = (img.dimensions[1], img.dimensions[0], 3)
713718
img_.append(img)
714719

@@ -719,7 +724,7 @@ def get_data(
719724
img,
720725
location: Tuple[int, int] = (0, 0),
721726
size: Optional[Tuple[int, int]] = None,
722-
level: int = 0,
727+
level: Optional[int] = None,
723728
dtype: DtypeLike = np.uint8,
724729
grid_shape: Tuple[int, int] = (1, 1),
725730
patch_size: Optional[Union[int, Tuple[int, int]]] = None,
@@ -738,9 +743,11 @@ def get_data(
738743
grid_shape: (row, columns) tuple define a grid to extract patches on that
739744
patch_size: (height, width) the size of extracted patches at the given level
740745
"""
746+
if level is None:
747+
level = self.level
741748

742-
if self.reader_lib == "openslide" and size is None:
743-
# the maximum size is set to WxH
749+
if self.backend == "openslide" and size is None:
750+
# the maximum size is set to WxH at the specified level
744751
size = (img.shape[0] // (2 ** level) - location[0], img.shape[1] // (2 ** level) - location[1])
745752

746753
region = self._extract_region(img, location=location, size=size, level=level, dtype=dtype)
@@ -780,7 +787,7 @@ def _extract_region(
780787

781788
def convert_to_rgb_array(self, raw_region, dtype: DtypeLike = np.uint8):
782789
"""Convert to RGB mode and numpy array"""
783-
if self.reader_lib == "openslide":
790+
if self.backend == "openslide":
784791
# convert to RGB
785792
raw_region = raw_region.convert("RGB")
786793
# convert to numpy

0 commit comments

Comments
 (0)