lazyslide.tl.virtual_stain

Contents

lazyslide.tl.virtual_stain#

virtual_stain(wsi, model='rosie', image_key=None, tile_key='tiles', device=None, amp=None, autocast_dtype=None, batch_size=32, num_workers=0, pbar=None, *, prefetch_factor=None, compile=None, compile_kws=None, postprocess=False)#

Translate the H&E images to multiplexed images.

A new multi-channel image will be created and stored in the WSIData object. The marker name is recorded in the image channel names.

What gets produced depends on the kind of model, not on its name:

  • A tile prediction model predicts one value per column for each tile. The result is a coarse image with one pixel per tile, holding the predicted values as float32. With postprocess=True, ROSIE’s output is contrast-stretched per channel into uint8 for display instead.

  • A marker map model, such as GigaTIME, predicts a value for every pixel. Tiles are stitched into one image, blended where they overlap.

  • A virtual stain model predicts one or more RGB stains for every pixel. Each stain is stored as its own RGB image.

Parameters:
wsiWSIData

The whole-slide image data to work on.

modelstr or VirtualStainModelProtocol or MarkerMapModelProtocol or TilePredictionModelProtocol, default: “rosie”

The virtual staining model to use: a model registry key (see Models), or an instance of a virtual stain, marker map or tile prediction model.

image_keystr, default: None

The key to store the new image. For a virtual stain model this is a prefix, and each stain is stored under '{image_key}_{stain}'.

tile_keystr, default: “tiles”

The key for the tile table.

devicestr, optional

Which device to use for inference. If None, the default device is used.

ampbool, optional

Whether to use automatic mixed precision.

autocast_dtypetorch.dtype, optional

The dtype for automatic mixed precision.

batch_sizeint, default: 32

The batch size for inference.

num_workersint, default: 0

The number of workers for data loading.

pbarbool, optional

If the progress bar should be shown. If None, uses settings.pbar.

prefetch_factorint, optional

The number of batches loaded in advance by each worker. Only used when num_workers > 0.

compilebool, optional

Whether to compile the model with torch.compile(). Compilation is best-effort and is silently skipped for models that do not support it.

compile_kwsdict, optional

Keyword arguments passed to torch.compile().

postprocessbool, default: False

Contrast-stretch ROSIE’s output into uint8 for display, like --postprocess_image in the ROSIE codebase: per channel, values up to the 90th percentile become 0 and the 99.9th percentile becomes 255. Off by default, as upstream: use the raw values for quantitative analysis. Only ROSIE has this post-processing.

Returns:
None

The virtual stain image is added to the images slot of the WSIData object under the key '{model_name}_prediction', or one image per stain for a virtual stain model.

Examples

>>> import lazyslide as zs
>>> wsi = zs.datasets.sample()
>>> zs.tl.virtual_stain(wsi)
>>> wsi.images["rosie_prediction"]