{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "5768eedc-f8be-442d-b93c-4377f6a923bc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-input" ] }, "outputs": [], "source": "" }, { "cell_type": "markdown", "id": "bd120f126071e47c", "metadata": {}, "source": [ "# Tiles in `wsidata`\n", "\n", "Tiles in wsidata is stored as a `GeoDataFrame` with an associated `TileSpec` object.\n", "\n", "To extract tiles from a non-existing magnification level, the tiling operation in `wsidata` is recorded with following information:\n", "\n", "- User specifies the tile size and magnification level.\n", "- The actual tile size and magnification level are used to create the tiles.\n", "- The tile size at the level 0.\n", "\n", "Noted that `wsidata` cannot create tiles, it can only record the information for generating tiles. Please use LazySlide to create tiles." ] }, { "cell_type": "code", "execution_count": null, "id": "44be3d819edfcf81", "metadata": {}, "outputs": [], "source": [ "from huggingface_hub import hf_hub_download\n", "\n", "s = hf_hub_download(\"RendeiroLab/LazySlide-data\", \"sample.svs\", repo_type=\"dataset\")" ] }, { "cell_type": "code", "execution_count": null, "id": "5cbd0d5ea5df7e59", "metadata": { "ExecuteTime": { "end_time": "2025-03-15T10:30:10.026711Z", "start_time": "2025-03-15T10:30:09.828212Z" } }, "outputs": [], "source": [ "from wsidata import TileSpec, open_wsi\n", "\n", "wsi = open_wsi(s)" ] }, { "cell_type": "code", "execution_count": null, "id": "9d730457bec9c405", "metadata": { "ExecuteTime": { "end_time": "2025-03-15T10:28:56.364064Z", "start_time": "2025-03-15T10:28:56.359520Z" } }, "outputs": [], "source": [ "wsi.properties" ] }, { "cell_type": "markdown", "id": "b44082cf-c4cc-4919-850f-b4392f6a26b5", "metadata": {}, "source": [ "Here we have a wsi with only one level with mpp=0.5\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "id": "2b12afbe09ff3524", "metadata": { "ExecuteTime": { "end_time": "2025-03-15T10:32:04.097263Z", "start_time": "2025-03-15T10:32:04.094399Z" } }, "outputs": [], "source": [ "TileSpec.from_wsidata(wsi, 100)" ] }, { "cell_type": "markdown", "id": "7193229098adaee3", "metadata": {}, "source": [ "However, if we request a tile size of 100x100 at mpp=1, but mpp=1 doesn't exist in the original image.\n", "The tile operation will be performed at level 0 with a tile size of 200x200 and rescaled to 100x100." ] }, { "cell_type": "code", "execution_count": null, "id": "46d30d5ae9855bad", "metadata": { "ExecuteTime": { "end_time": "2025-03-15T10:30:44.003204Z", "start_time": "2025-03-15T10:30:44.000219Z" } }, "outputs": [], "source": [ "TileSpec.from_wsidata(wsi, 100, mpp=1)" ] }, { "cell_type": "markdown", "id": "57f2ff1d-6785-4708-864e-ddffb80d622b", "metadata": {}, "source": [ "Of course, tiles with overlapping is also supported" ] }, { "cell_type": "code", "execution_count": null, "id": "131db596377edbdd", "metadata": {}, "outputs": [], "source": [ "TileSpec.from_wsidata(wsi, 100, stride_px=50)" ] }, { "cell_type": "code", "execution_count": null, "id": "b3440d59-b388-4547-a0b1-53d9f34bd7dc", "metadata": {}, "outputs": [], "source": [ "TileSpec.from_wsidata(wsi, 100, overlap=0.1)" ] }, { "cell_type": "markdown", "id": "cf86c92f4c8b8eda", "metadata": {}, "source": "If you want to create a new tile table manually, please remember to synchronize the `TileSpec` with it.\n" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": [ "import numpy as np\n", "\n", "from wsidata.io import add_tiles, sync_tile_spec\n", "\n", "add_tiles(\n", " wsi,\n", " \"tiles\",\n", " np.random.randint(0, 255, (100, 2), dtype=np.int32),\n", " tile_spec=TileSpec.from_wsidata(wsi, 25),\n", " tissue_ids=np.random.randint(0, 2, 100),\n", ")\n", "\n", "wsi[\"new_tiles\"] = wsi[\"tiles\"].sample(10)\n", "sync_tile_spec(wsi, \"tiles\", \"new_tiles\")" ], "id": "b413a546eacb4b5d" } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.8" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }