{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Install dependencies for this example\n", "# Note: This does not include itkwidgets, itself\n", "import sys\n", "!{sys.executable} -m pip install itk-io requests tqdm" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import requests\n", "from tqdm import tqdm_notebook as tqdm\n", "import os\n", "\n", "import numpy as np\n", "import itk\n", "\n", "from itkwidgets import view\n", "import itkwidgets" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def download_data(url, filename):\n", " if not os.path.exists(filename):\n", " chunk_size = 32 * 1024\n", " r = requests.get(url, stream=True)\n", " total_size = int(r.headers.get('content-length', 0))\n", " pbar = tqdm(unit=\"B\", unit_scale=True, total=int(total_size))\n", " with open(filename, 'wb') as f:\n", " for chunk in r.iter_content(chunk_size=chunk_size): \n", " if chunk: # filter out keep-alive new chunks\n", " pbar.update(len(chunk))\n", " f.write(chunk)\n", " \n", "# Download example data\n", "# Source: https://cvlab.epfl.ch/data/em\n", "#\n", "# 1024x768x165 unsigned char, 124MB file\n", "large_url = 'https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/training.tif'\n", "large_filename = 'large_hippocampus.tif'\n", "\n", "# 2048x1536x1065 unsigned char, 3.2GB file\n", "very_large_url = 'https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/volumedata.tif'\n", "very_large_filename = 'very_large_hippocampus.tif'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "This function will be removed in tqdm==5.0.0\n", "Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "85d1c08fc473420a909fdd37d8c69d50", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(FloatProgress(value=0.0, max=129919357.0), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "download_data(large_url, large_filename)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 450 ms, sys: 43.5 ms, total: 493 ms\n", "Wall time: 583 ms\n" ] } ], "source": [ "%time large_image = itk.imread(large_filename)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.58 s, sys: 256 ms, total: 1.84 s\n", "Wall time: 3.55 s\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "742a3a1ce6d4454fae3a569696ac0a86", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Viewer(annotations=False, cmap='gray_Matlab', geometries=[], gradient_opacity=0.4, point_sets=[], rendered_ima…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%time view(large_image, select_roi=True, gradient_opacity=0.4, shadow=False, cmap=itkwidgets.cm.gray, annotations=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, pass `select_roi=True` when creating the view, or click the crop button in the widget user interface. Adjust the location of the region of interest (roi) by clicking and dragging the corner of the cropping box.\n", "\n", "Large images are decimated for visualization; higher resolutions are presented as the roi shrinks." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 343 ms, sys: 123 ms, total: 465 ms\n", "Wall time: 162 ms\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d18cdea23679414996f9ccc6af2bedcb", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Viewer(annotations=False, cmap='gray_Matlab', geometries=[], gradient_opacity=0.4, mode='z', point_sets=[], re…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%time view(large_image, select_roi=False, mode='z', gradient_opacity=0.4, shadow=False, cmap=itkwidgets.cm.gray, annotations=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, set `select_roi` to `False` and the `mode` to `x`, `y`, or `z`. When zooming, the region of interest will shrink around the viewed area." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "This function will be removed in tqdm==5.0.0\n", "Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "20546e228b5d4b63b12f02b602e4982a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(FloatProgress(value=0.0, max=3353674357.0), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "download_data(very_large_url, very_large_filename)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%time very_large_image = itk.imread(very_large_filename)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%time view(very_large_image, gradient_opacity=0.4, shadow=False, cmap=itkwidgets.cm.gray, annotations=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To change the resolution of the rendered image, use the `size_limit_3d` keyword." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "size_limit_3d = [128, 128, 128]\n", "%time view(very_large_image, size_limit_3d=size_limit_3d, gradient_opacity=0.3, shadow=False, cmap=itkwidgets.cm.gray, annotations=False)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }