{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "7QWi2mZTNM0q"
},
"source": [
"# Loading the SPIDER dataset from HuggingFace\n",
"\n",
"This tutorial will walk you through the steps to download and use the SPIDER dataset."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "L9f_x4IO9pka"
},
"source": [
"### Table of Contents\n",
"\n",
"1. [Installing Dependencies](#dependencies)\n",
"2. [Loading Demo Dataset](#demo_config)\n",
"3. [Visualizing an Example Image](#visualizing_image)\n",
"4. [Resizing Images](#resizing)\n",
"5. [Loading Original Images](#original_images)\n",
"6. [Extracting Metadata](#metadata)\n",
"7. [Filtering Scan Types](#filter_scan_type)\n",
"8. [Loading Full Dataset](#loading_full_dataset)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Wt9vqFfcQT8G"
},
"source": [
"### Installing Dependencies "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ejAzA9RlNSVv"
},
"source": [
"First, install the necessary dependencies:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "cRMYzjBvo66Q",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e1432cce-dda3-4905-dcd3-9781a2bbdfc8"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m510.5/510.5 kB\u001b[0m \u001b[31m5.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m116.3/116.3 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m194.1/194.1 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m134.8/134.8 kB\u001b[0m \u001b[31m8.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m52.7/52.7 MB\u001b[0m \u001b[31m25.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
],
"source": [
"!pip install datasets -q\n",
"!pip install scikit-image -q\n",
"!pip install SimpleITK -q"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LOBqCjn3MDm_",
"outputId": "a1ade988-ccb4-40ea-9038-faa6792325d5"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"datasets: 2.18.0\n",
"scikit-image: 0.19.3\n",
"SimpleITK: 2.3.1\n"
]
}
],
"source": [
"import datasets\n",
"import skimage\n",
"import SimpleITK as sitk\n",
"\n",
"print(f'datasets: {datasets.__version__}')\n",
"print(f'scikit-image: {skimage.__version__}')\n",
"print(f'SimpleITK: {sitk.__version__}')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Liq722klQal4"
},
"source": [
"### Loading the Dataset with Demo Configuration "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zHCVOF9xOZFX"
},
"source": [
"Next, use the `load_dataset` function from the `datasets` library to download the data directly from the [Zenodo](https://zenodo.org/records/10159290) repository.\n",
"\n",
"Select the `demo` configuration to verify that the function works as intended. The `demo` configuration downloads all of the original `.mha` image and mask files from Zenodo, but only processes the first 10 examples to reduce computation time. The downloaded `.mha` image files will be saved to cache on your local system (which you can set with the `cache_dir` parameter -- see the HuggingFace [docs](https://huggingface.co/docs/datasets/v2.18.0/en/package_reference/loading_methods#datasets.load_dataset)).\n",
"\n",
"Note that in future versions of the `load_dataset` function, you will have to\n",
"explicitly pass `trust_remote_code=True` for the code to run. You can review\n",
"the source code in the HuggingFace repository [here](https://huggingface.co/datasets/cdoswald/SPIDER/blob/main/SPIDER.py)."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 441,
"referenced_widgets": [
"621343e4760c43f0ab55d3ba8415a43f",
"250923e24b104d80bae010903ae7886e",
"c94edf1892dc4a75926b4fcdc72538ec",
"e1a59a81e3f142d5bb979ab9fd60e25d",
"815b0613252547c5ba4bfb5d43f6bf5c",
"48c41139f5494157988be8c1a846c8bd",
"5fae5e920e3440e4b65313d2d4c0b421",
"723499d614344423a087580bdc64c6c4",
"051bb482f41b40d9af6a9b21090080b5",
"b82b605406584f3492f217b34f75dd47",
"23423b6a9ff84610b1110e2106fc00a8",
"f02765b1804545b4b4acec881578d49a",
"5f4a20ed7ff44afd90e95e2e3b31fdd2",
"8be26f5759fa4ad9b5dfcf7051d1c1f5",
"87d3bdcde951426aba06009e8bdd510f",
"bbea68cb4dab4f5195c41021a34c571f",
"acfc87ff0d7c4cbcaa4cfa91516144d2",
"a791255e8ee04ba4b14f111e0ed7719d",
"7b6807645f6e48ca9d2cc46a8ff3e611",
"267b26ee9864470b9917568305f63541",
"d1c0f001e1d644b09e049faeb84c3f53",
"6e2d23576f3f4df6ba5b254f8045901f",
"3d58235d864545a6b8d18af6dc467add",
"4ea0a5051a7240fe8f6bfd61ac88b656",
"a2d0a6d375b74f06a9f5d12403b88816",
"410df446d3754fd8b5531c85c6809470",
"650474a3bd254c4286cc6d537bcfd62e",
"af10fca1f7b1483eb2ef8bf1179c6cae",
"15534a65241646518315ad67cc97c08e",
"3d9988ce8c114b8b878bad95cc06a458",
"9a73f08d0245457397e817cbf247f779",
"b946c44b275640eca910a4a208e4562e",
"db70ea21aa944583ad4a363de82bb2d5",
"cd207b34f4d148a688d3deda6571c80d",
"c04510a1418744f5a18447f87c675204",
"0edd7b2cb75446a1a0c96d3e8fbe431c",
"63a0a86d2fab478f8ed520289313ab90",
"aaa88cf1e23d4d328749281cf82682f6",
"9fcfc169527041d79c8bdc660413d6e0",
"79d4513c94ff44aba803333304c36a69",
"d60f493361b642a4bbcc3b9b54443010",
"1567b4c0cafe47a9a7510584558a913b",
"d7a5387e195d47e29576d4c6d29dcb2b",
"36580c8feaed4fe085e8aaa5191d9388",
"225f0f74a40c4105b16feee76e0f118c",
"0272e5ff7cf84580a7b88e25779024ed",
"eb6c72632aa84bb29c1db28ddb226af4",
"69a63fa0fecc470fab787a11b17982ab",
"86103848746442788d56fdd06e48c798",
"60951750f0d94eed8ab8ac3677a694d3",
"2c40b2fba8cd4a88aa8d4ffe9e6ee22e",
"820211f82fc046b4b647c64fc181affb",
"85736668956245eaabe456c91ff298c9",
"ccfe041d29c948fa9c9ba28de22fa205",
"dfb6ab1c1532469aaafc24549e73fa76",
"f598b908c9344a2d826a526b283699d3",
"7b3aaf29306b4050b5db89962401439e",
"cc8e2735483c4d2abb5412ba18b5c2a9",
"57ec61b6d54e4779ab007c165a47db05",
"cf576920a45c419bbe5b18d9eb7b8a0f",
"32423c538fe84a1abc8f78b7d3a2590e",
"ed43c1c77b364862856c9dcd0b841389",
"b3e982dab99d4ae68d970d76641fb994",
"0e3214c3ecdc44a4be5c1fd2bf41af3f",
"41e3206455614968af6bfdc6933b46b9",
"aaee835fd5534b0b8ae59b02822a2964",
"528ea2f5936240158bda15a9b8f746cf",
"fbef287ed7aa4ec98ec51ad7a141b5c6",
"6ad4cbf87ea54292ad66eb036885d75b",
"e07de8941e974c4b94b6566c91cac95b",
"a8f7426d733a46378d6472cae8c6675d",
"49c5a91d6e69463ab1b70275bf874f04",
"5cd82c15589f46e1964b2769266f9b51",
"ecce67a0cecd445c8a5af2138404978c",
"006b58baedc04f8d9ded64efd69e577d",
"eadefca20d5548d097e56f7db73eb830",
"1dc698cb39404b29a05ab69b2aeaa9a2",
"4391604f31854468b3edd9a0a113ee49",
"1984044ab86c472a8b2e6eb975a3b793",
"6d4944cce17340198b7aea42fba085f8",
"d146bd18851c4c1eaf90051fa9e5965c",
"8eaa07b051a8434fb8f42ae1ab04eaac",
"4522219e9220461cb530960a24fdd9d6",
"090afa3384734f1ab40a6578cc780c92",
"c4f68c215dfe4165971806aa64e845a7",
"6c3d708200ea4d89a6e1895d551d932d",
"e5a03ceac7f7493e8baa1508c2ba102c",
"9344b1f91aaa48e7b31a23dca3c0b39e",
"95a4bbe5b62a46fe82775be9d3cff3b0",
"deb4ed22013e4f4583c1805f7d2da24f",
"3e2e261dc5f641ddb84c864e0e6d97da",
"eb00e7ad026242b38b952db8096ec026",
"6f0185f0b64d45baace00fd0755e71f5",
"5f2f4a9cd5914a52b5f6bd154365771b",
"6fa9005edc854dc6b3c48afc8b49bd3d",
"cdf035c3e4154435a8daa39f5e48e8c6",
"1aeb813172f84cd285d329eb072ca699",
"48dc7a64bfa74727bb24bfff56d23a15",
"c73511bc40c64286940563fe06c77740",
"627ca8e891ab4fb6be5893c6dffbe6a0",
"76583c6c2c0f4de3883a339667831cb1",
"df5fd312a6ef40649ffedba0fb1573cc",
"bd7e885e3e26492bbdb8077e9f320392",
"8e1f792cb9824d3496cafefc9d248c10",
"14fc73fbc9dd41248da4c1289f0c8d6c",
"a99ace5b8ffc407fb64693f2ee07d59e",
"c1d7ecb478894f04b3aab0197f9eb58b",
"4139fd0e5894424795628262968fa272",
"76bdff969cd04ad0a036dc8a637a683f",
"fa22c564cdee48a3911ca65243d8b074"
]
},
"id": "QOHzp7bRoqV4",
"outputId": "2bdc567d-6ee6-4ad2-cec7-5997bbfb1880"
},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:88: UserWarning: \n",
"The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
"To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
"You will be able to reuse this secret in all of your notebooks.\n",
"Please note that authentication is recommended but still optional to access public models or datasets.\n",
" warnings.warn(\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Downloading builder script: 0%| | 0.00/20.5k [00:00, ?B/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "621343e4760c43f0ab55d3ba8415a43f"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Downloading readme: 0%| | 0.00/6.23k [00:00, ?B/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "f02765b1804545b4b4acec881578d49a"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Downloading data: 0%| | 0.00/3.70G [00:00, ?B/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "3d58235d864545a6b8d18af6dc467add"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Downloading data: 0%| | 0.00/58.2M [00:00, ?B/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "cd207b34f4d148a688d3deda6571c80d"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Downloading data: 0.00B [00:00, ?B/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "225f0f74a40c4105b16feee76e0f118c"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Downloading data: 0.00B [00:00, ?B/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "f598b908c9344a2d826a526b283699d3"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Downloading data: 0%| | 0.00/1.20k [00:00, ?B/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "528ea2f5936240158bda15a9b8f746cf"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Generating train split: 0 examples [00:00, ? examples/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "4391604f31854468b3edd9a0a113ee49"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Generating validation split: 0 examples [00:00, ? examples/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "95a4bbe5b62a46fe82775be9d3cff3b0"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Generating test split: 0 examples [00:00, ? examples/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "627ca8e891ab4fb6be5893c6dffbe6a0"
}
},
"metadata": {}
}
],
"source": [
"from datasets import load_dataset\n",
"\n",
"dataset = load_dataset(\"cdoswald/SPIDER\", name=\"demo\", trust_remote_code=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "W0ODUGtORTZ6"
},
"source": [
"Notice that the dataset is split into train, validation, and test subsets, and each example has 8 features: `patient_id`, `scan_type`, `image`, `mask`, `image_path`, `mask_path`, `metadata`, and `rad_gradings`. Only the first 10 examples were processed for this `demo` configuration."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2G6WIkFZb9Up",
"outputId": "702c9809-5398-4584-8ae8-5e35b2b2700c"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['patient_id', 'scan_type', 'image', 'mask', 'image_path', 'mask_path', 'metadata', 'rad_gradings'],\n",
" num_rows: 10\n",
" })\n",
" validation: Dataset({\n",
" features: ['patient_id', 'scan_type', 'image', 'mask', 'image_path', 'mask_path', 'metadata', 'rad_gradings'],\n",
" num_rows: 10\n",
" })\n",
" test: Dataset({\n",
" features: ['patient_id', 'scan_type', 'image', 'mask', 'image_path', 'mask_path', 'metadata', 'rad_gradings'],\n",
" num_rows: 10\n",
" })\n",
"})"
]
},
"metadata": {},
"execution_count": 4
}
],
"source": [
"dataset"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HLX45XUvUDKs"
},
"source": [
"### Visualizing an Example Image "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "34CeKbtlSH3m"
},
"source": [
"We can view the features for a specific example by first selecting the data subset (e.g., \"train\") and then indexing a particular observation:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aTPm0k0vSSDB",
"outputId": "a18bb90d-17fb-4ec2-c133-f578f056dc84"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Patient ID: 184\n",
"Scan type: t1\n"
]
}
],
"source": [
"example = dataset['train'][0]\n",
"print(f'Patient ID: {example[\"patient_id\"]}\\nScan type: {example[\"scan_type\"]}')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Y4hgVOivSwf4"
},
"source": [
"By default, the `image` and `mask` attributes will each be 3D volumetric arrays of size `(512, 512, 30)` -- in other words, 30 stacked 512 x 512 grayscale images (note that the channel dimension indicates depth rather than RGB values).\n",
"\n",
"We can select a few specific depths to display as 2D images using matplotlib's `imshow` function:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 356
},
"id": "Nq0jUm-UazpL",
"outputId": "acb70db5-f568-4aaf-e0c5-ca4473d0039b"
},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"