|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "2181bf6a-68c4-4869-a1bb-4225b9b7d53f", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Import necessary modules" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": null, |
| 14 | + "id": "5e21c83d-cb4d-4c5b-a255-4c2834abd93c", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import pathlib\n", |
| 19 | + "\n", |
| 20 | + "import ipywidgets as widgets\n", |
| 21 | + "from IPython.display import display\n", |
| 22 | + "\n", |
| 23 | + "import ipyniivue\n", |
| 24 | + "from ipyniivue import NiiVue, download_dataset" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "id": "dfc76d81-1331-428a-a64b-5087dbbcf8be", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "# Download Formats" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": null, |
| 38 | + "id": "e70f5a26-4a37-4887-a3c0-327818181289", |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "DATA_FOLDER = pathlib.Path(ipyniivue.__file__).parent / \"images\"\n", |
| 43 | + "\n", |
| 44 | + "local_imgs = [\n", |
| 45 | + " \"FLAIR.nrrd\",\n", |
| 46 | + " \"cactus.nii.gz\",\n", |
| 47 | + " \"DoG.png\",\n", |
| 48 | + " \"anat_final.FT+tlrc.HEAD\",\n", |
| 49 | + " \"mha.mha\",\n", |
| 50 | + " \"template.mif.gz\",\n", |
| 51 | + " \"trix/fa.mif\",\n", |
| 52 | + " \"dsistudio.src.gz\",\n", |
| 53 | + " \"dsistudio.fib.gz\",\n", |
| 54 | + " \"wm.mgz\",\n", |
| 55 | + "]\n", |
| 56 | + "\n", |
| 57 | + "files_to_download_local = local_imgs.copy()\n", |
| 58 | + "for img in local_imgs:\n", |
| 59 | + " if img.endswith(\".HEAD\"):\n", |
| 60 | + " # Also need to download the paired .BRIK file\n", |
| 61 | + " brik_file = img.replace(\".HEAD\", \".BRIK\")\n", |
| 62 | + " files_to_download_local.append(brik_file)\n", |
| 63 | + "\n", |
| 64 | + "download_dataset(\n", |
| 65 | + " api_url=\"https://niivue.com/demos/images/\",\n", |
| 66 | + " dest_folder=DATA_FOLDER,\n", |
| 67 | + " files=files_to_download_local,\n", |
| 68 | + ")" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "id": "af6729b4-2f0e-4a76-b493-eba46cbcdf14", |
| 74 | + "metadata": {}, |
| 75 | + "source": [ |
| 76 | + "# Download Modalities" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "id": "6e1f8e7f-3eed-40d6-aa8e-828ec08a31ec", |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "modalities_imgs = [\n", |
| 87 | + " \"chris_MRA\",\n", |
| 88 | + " \"chris_PD\",\n", |
| 89 | + " \"chris_t1\",\n", |
| 90 | + " \"chris_t2\",\n", |
| 91 | + " \"CT_Abdo\",\n", |
| 92 | + " \"CT_AVM\",\n", |
| 93 | + " \"CT_Electrodes\",\n", |
| 94 | + " \"CT_Philips\",\n", |
| 95 | + " \"CT_pitch\",\n", |
| 96 | + " \"fmri_pitch\",\n", |
| 97 | + " \"Iguana\",\n", |
| 98 | + " \"mni152\",\n", |
| 99 | + " \"MR_Gd\",\n", |
| 100 | + " \"pcasl\",\n", |
| 101 | + " \"spm152\",\n", |
| 102 | + " \"spmMotor\",\n", |
| 103 | + " \"visiblehuman\",\n", |
| 104 | + " \"rgb_bmp.jpg\",\n", |
| 105 | + " \"gray_bmp.png\",\n", |
| 106 | + " \"HCD1464653.qsdr.fz\",\n", |
| 107 | + "]\n", |
| 108 | + "\n", |
| 109 | + "# Add the appropriate extensions\n", |
| 110 | + "files_to_download_modalities = []\n", |
| 111 | + "for img in modalities_imgs:\n", |
| 112 | + " if (\n", |
| 113 | + " not img.endswith(\".png\")\n", |
| 114 | + " and not img.endswith(\".jpg\")\n", |
| 115 | + " and not img.endswith(\".fz\")\n", |
| 116 | + " ):\n", |
| 117 | + " img_with_ext = img + \".nii.gz\"\n", |
| 118 | + " else:\n", |
| 119 | + " img_with_ext = img\n", |
| 120 | + " files_to_download_modalities.append(img_with_ext)\n", |
| 121 | + "\n", |
| 122 | + "download_dataset(\n", |
| 123 | + " api_url=\"https://niivue.github.io/niivue-demo-images/\",\n", |
| 124 | + " dest_folder=DATA_FOLDER,\n", |
| 125 | + " files=files_to_download_modalities,\n", |
| 126 | + ")" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "markdown", |
| 131 | + "id": "d17023b6-94dc-4a38-846a-719fcfaa7009", |
| 132 | + "metadata": {}, |
| 133 | + "source": [ |
| 134 | + "# Setup NiiVue Instance" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "id": "14021983-a5fc-400b-b58c-1b9d0284d68b", |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [], |
| 143 | + "source": [ |
| 144 | + "nv = NiiVue(\n", |
| 145 | + " back_color=(0.7, 0.7, 0.9, 1),\n", |
| 146 | + ")\n", |
| 147 | + "nv.opts.is_colorbar = True\n", |
| 148 | + "nv.set_slice_type(\"RENDER\")\n", |
| 149 | + "nv.set_clip_plane(0.35, 270, 0)\n", |
| 150 | + "\n", |
| 151 | + "initial_volume_path = DATA_FOLDER / \"mni152.nii.gz\"\n", |
| 152 | + "nv.load_volumes(\n", |
| 153 | + " [\n", |
| 154 | + " {\n", |
| 155 | + " \"path\": initial_volume_path,\n", |
| 156 | + " \"colormap\": \"gray\",\n", |
| 157 | + " \"opacity\": 1,\n", |
| 158 | + " \"visible\": True,\n", |
| 159 | + " }\n", |
| 160 | + " ]\n", |
| 161 | + ")" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "id": "ba2edb7f-fa9f-4194-aad5-c74cfb0fffdb", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + "# Store image paths" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": null, |
| 175 | + "id": "38e81fe1-0afc-4391-bc7b-b07e72739171", |
| 176 | + "metadata": {}, |
| 177 | + "outputs": [], |
| 178 | + "source": [ |
| 179 | + "img_paths = {}\n", |
| 180 | + "\n", |
| 181 | + "# Formats\n", |
| 182 | + "for img in local_imgs:\n", |
| 183 | + " img_path = DATA_FOLDER / img\n", |
| 184 | + " img_paths[img] = img_path\n", |
| 185 | + " if img.endswith(\".HEAD\"):\n", |
| 186 | + " # Also map the paired .BRIK file\n", |
| 187 | + " brik_file = img.replace(\".HEAD\", \".BRIK\")\n", |
| 188 | + " brik_path = DATA_FOLDER / brik_file\n", |
| 189 | + " img_paths[img + \"_paired\"] = brik_path\n", |
| 190 | + "\n", |
| 191 | + "# Modalities\n", |
| 192 | + "for img in modalities_imgs:\n", |
| 193 | + " if (\n", |
| 194 | + " not img.endswith(\".png\")\n", |
| 195 | + " and not img.endswith(\".jpg\")\n", |
| 196 | + " and not img.endswith(\".fz\")\n", |
| 197 | + " ):\n", |
| 198 | + " img_with_ext = img + \".nii.gz\"\n", |
| 199 | + " else:\n", |
| 200 | + " img_with_ext = img\n", |
| 201 | + " img_path = DATA_FOLDER / img_with_ext\n", |
| 202 | + " img_paths[img] = img_path" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "markdown", |
| 207 | + "id": "bc147316-9433-4e13-ac92-6e1dbc9143dc", |
| 208 | + "metadata": {}, |
| 209 | + "source": [ |
| 210 | + "# Dropdowns" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "code", |
| 215 | + "execution_count": null, |
| 216 | + "id": "4c85aff2-40d0-439b-a581-32b0bfab9316", |
| 217 | + "metadata": {}, |
| 218 | + "outputs": [], |
| 219 | + "source": [ |
| 220 | + "# Dropdown for Formats\n", |
| 221 | + "formats_dropdown = widgets.Dropdown(\n", |
| 222 | + " options=[(img, img) for img in local_imgs],\n", |
| 223 | + " description=\"Formats:\",\n", |
| 224 | + " value=None,\n", |
| 225 | + ")\n", |
| 226 | + "\n", |
| 227 | + "# Dropdown for Modalities\n", |
| 228 | + "modalities_dropdown = widgets.Dropdown(\n", |
| 229 | + " options=[(img, img) for img in modalities_imgs],\n", |
| 230 | + " description=\"Modalities:\",\n", |
| 231 | + " value=None,\n", |
| 232 | + ")\n", |
| 233 | + "\n", |
| 234 | + "# Dropdown for Colormaps\n", |
| 235 | + "colormaps = sorted(nv.colormaps())\n", |
| 236 | + "colormap_dropdown = widgets.Dropdown(\n", |
| 237 | + " options=colormaps,\n", |
| 238 | + " description=\"Colormap:\",\n", |
| 239 | + " value=\"gray\",\n", |
| 240 | + ")" |
| 241 | + ] |
| 242 | + }, |
| 243 | + { |
| 244 | + "cell_type": "markdown", |
| 245 | + "id": "d96f552b-dc63-4fd6-ace8-653e1ef453fb", |
| 246 | + "metadata": {}, |
| 247 | + "source": [ |
| 248 | + "# Event handlers" |
| 249 | + ] |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "code", |
| 253 | + "execution_count": null, |
| 254 | + "id": "bd2d2635-4faf-403e-b2d5-494422841601", |
| 255 | + "metadata": {}, |
| 256 | + "outputs": [], |
| 257 | + "source": [ |
| 258 | + "def on_format_change(change):\n", |
| 259 | + " \"\"\"Handle selection in Formats Dropdown.\"\"\"\n", |
| 260 | + " selected_img = change[\"new\"]\n", |
| 261 | + " if not selected_img:\n", |
| 262 | + " return\n", |
| 263 | + " img_path = img_paths.get(selected_img)\n", |
| 264 | + " if not img_path.exists():\n", |
| 265 | + " print(f\"Image {img_path} not found.\")\n", |
| 266 | + " return\n", |
| 267 | + " volumeObj = {\n", |
| 268 | + " \"path\": img_path,\n", |
| 269 | + " \"colormap\": \"gray\",\n", |
| 270 | + " \"opacity\": 1,\n", |
| 271 | + " \"visible\": True,\n", |
| 272 | + " }\n", |
| 273 | + " if selected_img.endswith(\".HEAD\"):\n", |
| 274 | + " paired_img_path = img_paths.get(selected_img + \"_paired\")\n", |
| 275 | + " if not paired_img_path or not paired_img_path.exists():\n", |
| 276 | + " print(f\"Paired image {paired_img_path} not found.\")\n", |
| 277 | + " return\n", |
| 278 | + " volumeObj[\"paired_img_path\"] = paired_img_path\n", |
| 279 | + " print(\"Loading format image:\", img_path.name)\n", |
| 280 | + " nv.load_volumes([volumeObj])\n", |
| 281 | + " # Reset Modalities Dropdown\n", |
| 282 | + " modalities_dropdown.unobserve(on_modality_change, names=\"value\")\n", |
| 283 | + " modalities_dropdown.value = None\n", |
| 284 | + " modalities_dropdown.observe(on_modality_change, names=\"value\")\n", |
| 285 | + " # Update colormap\n", |
| 286 | + " if nv.volumes:\n", |
| 287 | + " colormap_dropdown.value = nv.volumes[0].colormap\n", |
| 288 | + "\n", |
| 289 | + "\n", |
| 290 | + "def on_modality_change(change):\n", |
| 291 | + " \"\"\"Handle selection in Modalities Dropdown.\"\"\"\n", |
| 292 | + " selected_img = change[\"new\"]\n", |
| 293 | + " if not selected_img:\n", |
| 294 | + " return\n", |
| 295 | + " img_path = img_paths.get(selected_img)\n", |
| 296 | + " if not img_path.exists():\n", |
| 297 | + " print(f\"Image {img_path} not found.\")\n", |
| 298 | + " return\n", |
| 299 | + " volumeObj = {\n", |
| 300 | + " \"path\": img_path,\n", |
| 301 | + " \"colormap\": \"gray\",\n", |
| 302 | + " \"opacity\": 1,\n", |
| 303 | + " \"visible\": True,\n", |
| 304 | + " }\n", |
| 305 | + " print(\"Loading modality image:\", img_path.name)\n", |
| 306 | + " nv.load_volumes([volumeObj])\n", |
| 307 | + " # Reset Formats Dropdown\n", |
| 308 | + " formats_dropdown.unobserve(on_format_change, names=\"value\")\n", |
| 309 | + " formats_dropdown.value = None\n", |
| 310 | + " formats_dropdown.observe(on_format_change, names=\"value\")\n", |
| 311 | + " # Update colormap\n", |
| 312 | + " if nv.volumes:\n", |
| 313 | + " colormap_dropdown.value = nv.volumes[0].colormap\n", |
| 314 | + "\n", |
| 315 | + "\n", |
| 316 | + "def on_colormap_change(change):\n", |
| 317 | + " \"\"\"Handle selection in Colormap Dropdown.\"\"\"\n", |
| 318 | + " selected_cmap = change[\"new\"]\n", |
| 319 | + " if nv.volumes:\n", |
| 320 | + " nv.set_colormap(nv.volumes[0].id, selected_cmap)" |
| 321 | + ] |
| 322 | + }, |
| 323 | + { |
| 324 | + "cell_type": "markdown", |
| 325 | + "id": "70b2b439-11c0-411b-9378-5df96be084ad", |
| 326 | + "metadata": {}, |
| 327 | + "source": [ |
| 328 | + "# Observe the dropdowns" |
| 329 | + ] |
| 330 | + }, |
| 331 | + { |
| 332 | + "cell_type": "code", |
| 333 | + "execution_count": null, |
| 334 | + "id": "d9ee4730-6b3c-45a6-a664-a873e667fe67", |
| 335 | + "metadata": {}, |
| 336 | + "outputs": [], |
| 337 | + "source": [ |
| 338 | + "formats_dropdown.observe(on_format_change, names=\"value\")\n", |
| 339 | + "modalities_dropdown.observe(on_modality_change, names=\"value\")\n", |
| 340 | + "colormap_dropdown.observe(on_colormap_change, names=\"value\")" |
| 341 | + ] |
| 342 | + }, |
| 343 | + { |
| 344 | + "cell_type": "markdown", |
| 345 | + "id": "416fe1dd-4179-4d58-afbd-2799ea4eba03", |
| 346 | + "metadata": {}, |
| 347 | + "source": [ |
| 348 | + "# Display all" |
| 349 | + ] |
| 350 | + }, |
| 351 | + { |
| 352 | + "cell_type": "code", |
| 353 | + "execution_count": null, |
| 354 | + "id": "0b107556-61a3-4581-ac77-e2c21a26883b", |
| 355 | + "metadata": {}, |
| 356 | + "outputs": [], |
| 357 | + "source": [ |
| 358 | + "controls = widgets.VBox(\n", |
| 359 | + " [\n", |
| 360 | + " formats_dropdown,\n", |
| 361 | + " modalities_dropdown,\n", |
| 362 | + " colormap_dropdown,\n", |
| 363 | + " ]\n", |
| 364 | + ")\n", |
| 365 | + "\n", |
| 366 | + "display(controls, nv)" |
| 367 | + ] |
| 368 | + } |
| 369 | + ], |
| 370 | + "metadata": {}, |
| 371 | + "nbformat": 4, |
| 372 | + "nbformat_minor": 5 |
| 373 | +} |
0 commit comments