Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/hest/trident_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,39 @@ def wsi_factory(
mpp: Optional[float] = None,
reader_type: Optional[str] = None,
) -> WSI:

try:
from cucim import CuImage as _CuImage # type: ignore
except ImportError:
_CuImage = None

if isinstance(img, WSI):
return _ensure_initialized(img)

if isinstance(img, np.ndarray):
return _array_to_wsi(img, mpp=mpp)

if _CuImage is not None and isinstance(img, _CuImage):
path_like = (
getattr(img, "path", None)
or getattr(img, "filename", None)
or getattr(img, "_path", None)
or getattr(img, "_filename", None)
)
if path_like is None:
raise ValueError(
"CuImage object does not expose a usable source path; "
"please pass a file path (str/Path) to wsi_factory."
)
# CuCIM does not always expose MPP reliably in metadata, so forward it when available.
if mpp is not None:
loaded = trident_load_wsi(
str(path_like), reader_type=reader_type or "cucim", mpp=float(mpp)
)
else:
loaded = trident_load_wsi(str(path_like), reader_type=reader_type or "cucim")
return _ensure_initialized(loaded)

if isinstance(img, (str, Path)):
try:
loaded = trident_load_wsi(str(img), reader_type=reader_type)
Expand All @@ -80,6 +109,7 @@ def wsi_factory(
else:
raise exc
return _ensure_initialized(loaded)

if hasattr(img, "read_region") and hasattr(img, "dimensions"):
width, height = img.dimensions
arr = np.array(img.read_region((0, 0), 0, (width, height)).convert("RGB"))
Expand All @@ -105,7 +135,7 @@ def segment_tissue_deep(
num_workers: int = 8,
weights_dir: Optional[str] = None,
holes_are_tissue: bool = True,
verbose: bool=True
verbose: bool = True,
):
_ = (pixel_size, patch_size_um, model_name, auto_download, weights_dir)
effective_pxl_size = 2.0 if fast_mode and target_pxl_size == 1 else float(target_pxl_size)
Expand Down
Loading