Open a WSI file#

In this notebook, we will learn

  1. how to open a WSI file.

  2. read its metadata and inspect its content.

Let’s first download an example whole slide image.

from huggingface_hub import hf_hub_download

s = hf_hub_download("RendeiroLab/LazySlide-data", "sample.svs", repo_type="dataset")
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.

Let’s open the WSI file.

from wsidata import open_wsi

wsi = open_wsi(s)
wsi
WSI: /home/docs/.cache/huggingface/hub/datasets--RendeiroLab--LazySlide-data/snapshots/d469afd4a763ad366861e8c49d4cf424bfad902c/sample.svs
Reader: openslide
Dimensions: 2967×2220 (h×w), 1 Pyramid
Pixel physical size: 0.50 MPP (20X)
SpatialData object
with coordinate systems:

If we inspect the WSIData object, we can see that it contains several information. If you are in Jupyter environment, you will see a thumbnail of the WSI.

The first line shows you the disk path to your WSI file. The reader tells you which reader was used to open the file. There are also additional information about the slide. The rest are output from SpatialData object, which we used to record the relevant analysis results of the WSI file.

WSI Properties#

The properties of the WSI file can be accessed through the properties attribute.

wsi.properties

Slide Properties

FieldValue
shape[2967, 2220]
n_level1
level_shape[[2967, 2220]]
level_downsample[1.0]
mpp0.499
magnification20.0
bounds[0, 0, 2220, 2967]

If you want to access one of the value, you can use attributes.

f"The mpp of the slide is {wsi.properties.mpp}"
'The mpp of the slide is 0.499'

What does the WSI looks like?#

wsi.thumbnail
../_images/ec1aeb9a2ac65aed03e23220cfe3b09c1896511f067feaef2b3ed5c494d4984f.png

Access the WSI region#

You can access a region of the WSI file by specifying the x-y coordination and the size of the region. Here we access a region of size 250x210 at (1000, 1000) at level 0.

# This will return a numpy array in uint8 format
region = wsi.read_region(1000, 1000, 210, 250, level=0)

Let’s visualize the region.

from PIL import Image

Image.fromarray(region)
../_images/ac7e36f48021e79a385064785050a9fe7b3cf93a50f15326dc02e04ca6de73b7.png

Save the wsidata on disk#

The WSIData object is inherited from SpatialData object. You can save it on disk as a zarr file. However, you don’t need to specify the path, by default, it will be saved in the same directory as the WSI file.

wsi.wsi_store
PosixPath('/home/docs/.cache/huggingface/hub/datasets--RendeiroLab--LazySlide-data/snapshots/d469afd4a763ad366861e8c49d4cf424bfad902c/sample.zarr')

Here, the zarr storage will have the same name as the slide file

# To save it on disk
wsi.write()
# To save it on disk with a specific name
# wsi.write("data/sample.zarr")