Skip to content

Commit 2e1fb21

Browse files
committed
Add OMEZarr reader support based on changes in mahmoodlab#163
1 parent e8f23f7 commit 2e1fb21

4 files changed

Lines changed: 25 additions & 10 deletions

File tree

run_batch_of_slides.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def build_parser() -> argparse.ArgumentParser:
5959
help='Custom keys used to store the resolution as MPP (micron per pixel) in your list of whole-slide image.')
6060
parser.add_argument('--custom_list_of_wsis', type=str, default=None,
6161
help='Custom list of WSIs specified in a csv file.')
62-
parser.add_argument('--reader_type', type=str, choices=['openslide', 'image', 'cucim', 'sdpc'], default=None,
63-
help='Force the use of a specific WSI image reader. Options are ["openslide", "image", "cucim", "sdpc"]. Defaults to None (auto-determine which reader to use).')
62+
parser.add_argument('--reader_type', type=str, choices=['openslide', 'image', 'cucim', 'sdpc', 'omezarr'], default=None,
63+
help='Force the use of a specific WSI image reader. Options are ["openslide", "image", "cucim", "sdpc", "omezarr"]. Defaults to None (auto-determine which reader to use).')
6464
parser.add_argument("--search_nested", action="store_true",
6565
help=("If set, recursively search for whole-slide images (WSIs) within all subdirectories of "
6666
"`wsi_source`. Uses `os.walk` to include slides from nested folders. "

trident/Processor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from trident import load_wsi, WSIReaderType
1212
from trident.IO import create_lock, remove_lock, is_locked, update_log, collect_valid_slides, splitext
1313
from trident.Maintenance import deprecated
14-
from trident.wsi_objects.WSIFactory import OPENSLIDE_EXTENSIONS, PIL_EXTENSIONS, SDPC_EXTENSIONS
14+
from trident.wsi_objects.WSIFactory import OPENSLIDE_EXTENSIONS, PIL_EXTENSIONS, SDPC_EXTENSIONS, OMEZARR_EXTENSIONS
1515

1616

1717
class Processor:
@@ -74,7 +74,7 @@ def __init__(
7474
Maximum number of workers for data loading. If None, the default behavior will be used.
7575
Defaults to None.
7676
reader_type (WSIReaderType, optional):
77-
Force the image reader engine to use. Options are are ["openslide", "image", "cucim"]. Defaults to None
77+
Force the image reader engine to use. Options are are ["openslide", "image", "cucim", "sdpc", "omezarr"]. Defaults to None
7878
(auto-determine the right engine based on image extension).
7979
search_nested (bool, optional):
8080
If True, the processor will recursively search for WSIs within all subdirectories of `wsi_source`.
@@ -108,7 +108,7 @@ def __init__(
108108

109109
self.job_dir = job_dir
110110
self.wsi_source = wsi_source
111-
self.wsi_ext = wsi_ext or (list(PIL_EXTENSIONS) + list(OPENSLIDE_EXTENSIONS) + list(SDPC_EXTENSIONS))
111+
self.wsi_ext = wsi_ext or (list(PIL_EXTENSIONS) + list(OPENSLIDE_EXTENSIONS) + list(SDPC_EXTENSIONS) + list(OMEZARR_EXTENSIONS))
112112
self.skip_errors = skip_errors
113113
self.custom_mpp_keys = custom_mpp_keys
114114
self.max_workers = max_workers

trident/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from trident.wsi_objects.CuCIMWSI import CuCIMWSI
1010
from trident.wsi_objects.ImageWSI import ImageWSI
1111
from trident.wsi_objects.SDPCWSI import SDPCWSI
12+
from trident.wsi_objects.OMEZarrWSI import OMEZarrWSI
1213
from trident.wsi_objects.WSIFactory import load_wsi, WSIReaderType
1314
from trident.wsi_objects.WSIPatcher import OpenSlideWSIPatcher, WSIPatcher
1415
from trident.wsi_objects.WSIPatcherDataset import WSIPatcherDataset
@@ -28,6 +29,7 @@
2829
"ImageWSI",
2930
"CuCIMWSI",
3031
"SDPCWSI",
32+
"OMEZarrWSI",
3133
"WSIPatcher",
3234
"OpenSlideWSIPatcher",
3335
"WSIPatcherDataset",

trident/wsi_objects/WSIFactory.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
from trident.wsi_objects.ImageWSI import ImageWSI
77
from trident.wsi_objects.CuCIMWSI import CuCIMWSI
88
from trident.wsi_objects.SDPCWSI import SDPCWSI
9-
WSIReaderType = Literal['openslide', 'image', 'cucim', 'sdpc']
9+
from trident.wsi_objects.OMEZarrWSI import OMEZarrWSI
10+
WSIReaderType = Literal['openslide', 'image', 'cucim', 'sdpc', 'omezarr']
1011
OPENSLIDE_EXTENSIONS = {'.svs', '.tif', '.tiff', '.ndpi', '.vms', '.vmu', '.scn', '.mrxs'}
1112
CUCIM_EXTENSIONS = {'.svs', '.tif', '.tiff'}
1213
SDPC_EXTENSIONS = {'.sdpc'}
1314
PIL_EXTENSIONS = {'.png', '.jpg', '.jpeg'}
15+
OMEZARR_EXTENSIONS = {'.ome.zarr'}
1416

1517

1618
def load_wsi(
1719
slide_path: str,
1820
reader_type: Optional[WSIReaderType] = None,
1921
lazy_init: bool = False,
2022
**kwargs
21-
) -> Union[OpenSlideWSI, ImageWSI, CuCIMWSI, SDPCWSI]:
23+
) -> Union[OpenSlideWSI, ImageWSI, CuCIMWSI, SDPCWSI, OMEZarrWSI]:
2224
"""
2325
Load a whole-slide image (WSI) using the appropriate backend.
2426
@@ -30,7 +32,7 @@ def load_wsi(
3032
----------
3133
slide_path : str
3234
Path to the whole-slide image.
33-
reader_type : {'openslide', 'image', 'cucim', 'sdpc'}, optional
35+
reader_type : {'openslide', 'image', 'cucim', 'sdpc', 'omezarr'}, optional
3436
Manually specify the WSI reader to use. If None (default), selection
3537
is automatic based on file extension.
3638
lazy_init : bool, optional
@@ -41,7 +43,7 @@ def load_wsi(
4143
4244
Returns
4345
-------
44-
Union[OpenSlideWSI, ImageWSI, CuCIMWSI, SDPCWSI]
46+
Union[OpenSlideWSI, ImageWSI, CuCIMWSI, SDPCWSI, OMEZarrWSI]
4547
An instance of the appropriate WSI reader.
4648
4749
Raises
@@ -78,11 +80,22 @@ def load_wsi(
7880
f"Unsupported file format '{ext}' for CuCIM. "
7981
f"Supported whole-slide image formats are: {', '.join(CUCIM_EXTENSIONS)}."
8082
)
81-
83+
84+
elif reader_type == 'omezarr':
85+
if ext in OMEZARR_EXTENSIONS:
86+
return OMEZarrWSI(slide_path=slide_path, lazy_init=lazy_init, **kwargs)
87+
else:
88+
raise ValueError(
89+
f"Unsupported file format '{ext}' for Ome-Zarr. "
90+
f"Supported whole-slide image formats are: {', '.join(OMEZARR_EXTENSIONS)}."
91+
)
92+
8293
elif reader_type is None:
8394
if ext in OPENSLIDE_EXTENSIONS:
8495
return OpenSlideWSI(slide_path=slide_path, lazy_init=lazy_init, **kwargs)
8596
elif ext in SDPC_EXTENSIONS:
8697
return SDPCWSI(slide_path=slide_path, lazy_init=lazy_init, **kwargs)
98+
elif ext in OMEZARR_EXTENSIONS:
99+
return OMEZarrWSI(slide_path=slide_path, lazy_init=lazy_init, **kwargs)
87100
else:
88101
return ImageWSI(slide_path=slide_path, lazy_init=lazy_init, **kwargs)

0 commit comments

Comments
 (0)