Tiles in wsidata#
Tiles in wsidata is stored as a GeoDataFrame with an associated TileSpec object.
To extract tiles from a non-existing magnification level, the tiling operation in wsidata is recorded with following information:
User specifies the tile size and magnification level.
The actual tile size and magnification level are used to create the tiles.
The tile size at the level 0.
Noted that wsidata cannot create tiles, it can only record the information for generating tiles. Please use LazySlide to create tiles.
from huggingface_hub import hf_hub_download
s = hf_hub_download("RendeiroLab/LazySlide-data", "sample.svs", repo_type="dataset")
from wsidata import TileSpec, open_wsi
wsi = open_wsi(s)
wsi.properties
Slide Properties
| Field | Value |
|---|---|
| shape | [2967, 2220] |
| n_level | 1 |
| level_shape | [[2967, 2220]] |
| level_downsample | [1.0] |
| mpp | 0.499 |
| magnification | 20.0 |
| bounds | [0, 0, 2220, 2967] |
Here we have a wsi with only one level with mpp=0.5
If we want to request a tile size of 100x100 at level 0, the tile operation will be performed at level 0 with a tile size of 100x100.
TileSpec.from_wsidata(wsi, 100)
Tile 1
Tile 2
Tile 3
Tile size: 100×100 (h×w)
Stride: 100×100 (0×0 overlap)
Operation size: 100×100, level=0
Base size: 100×100, level=0
Target tissue: 'None'
However, if we request a tile size of 100x100 at mpp=1, but mpp=1 doesn’t exist in the original image. The tile operation will be performed at level 0 with a tile size of 200x200 and rescaled to 100x100.
TileSpec.from_wsidata(wsi, 100, mpp=1)
Tile 1
Tile 2
Tile 3
Tile size: 100×100 (h×w)
Stride: 100×100 (0×0 overlap)
Operation size: 200×200, level=0
Base size: 200×200, level=0
Target tissue: 'None'
Of course, tiles with overlapping is also supported
TileSpec.from_wsidata(wsi, 100, stride_px=50)
Tile 1
Tile 2
Tile 3
Tile size: 100×100 (h×w)
Stride: 50×50 (50×50 overlap)
Operation size: 100×100, level=0
Base size: 100×100, level=0
Target tissue: 'None'
TileSpec.from_wsidata(wsi, 100, overlap=0.1)
Tile 1
Tile 2
Tile 3
Tile size: 100×100 (h×w)
Stride: 90×90 (10×10 overlap)
Operation size: 100×100, level=0
Base size: 100×100, level=0
Target tissue: 'None'
If you want to create a new tile table manually, please remember to synchronize the TileSpec with it.
import numpy as np
from wsidata.io import add_tiles, sync_tile_spec
add_tiles(
wsi,
"tiles",
np.random.randint(0, 255, (100, 2), dtype=np.int32),
tile_spec=TileSpec.from_wsidata(wsi, 25),
tissue_ids=np.random.randint(0, 2, 100),
)
wsi["new_tiles"] = wsi["tiles"].sample(10)
sync_tile_spec(wsi, "tiles", "new_tiles")