Welcome to SimpleITK Jupyter Notebooks

Newcomers to Jupyter Notebooks:

  1. We use two types of cells, code and markdown.
  2. To run a code cell, select it (mouse or arrow key so that it is highlighted) and then press shift+enter which also moves focus to the next cell or ctrl+enter which doesn't.
  3. Closing the browser window does not close the Jupyter server. To close the server, go to the terminal where you ran it and press ctrl+c twice.

For additional details see the Jupyter Notebook Quick Start Guide.

SimpleITK Environment Setup

Check that SimpleITK and auxiliary program(s) are correctly installed in your environment, and that you have the SimpleITK version which you expect (requires network connectivity).

You can optionally download all of the data used in the notebooks in advance. This step is only necessary if you expect to run the notebooks without network connectivity.

The following cell checks that all expected packages are installed.

In [1]:
from __future__ import print_function
import importlib
from distutils.version import LooseVersion

# check that all packages are installed (see requirements.txt file)
required_packages = {'jupyter', 
                     'numpy',
                     'matplotlib',
                     'ipywidgets',
                     'scipy',
                     'pandas',
                     'SimpleITK'
                    }

problem_packages = list()
# Iterate over the required packages: If the package is not installed
# ignore the exception. 
for package in required_packages:
    try:
        p = importlib.import_module(package)        
    except ImportError:
        problem_packages.append(package)
    
if len(problem_packages) is 0:
    print('All is well.')
else:
    print('The following packages are required but not installed: ' \
          + ', '.join(problem_packages))
All is well.
In [2]:
import SimpleITK as sitk

%run update_path_to_download_script
from downloaddata import fetch_data, fetch_data_all

from ipywidgets import interact

print(sitk.Version())
SimpleITK Version: 1.2.4 (ITK 4.13)
Compiled: Nov 12 2019 23:05:07

We expect that you have an external image viewer installed. The default viewer is Fiji. If you have another viewer (i.e. ITK-SNAP or 3D Slicer) you will need to set an environment variable to point to it. This can be done from within a notebook as shown below.

In [3]:
# Uncomment the line below to change the default external viewer to your viewer of choice and test that it works.
#%env SITK_SHOW_COMMAND /Applications/ITK-SNAP.app/Contents/MacOS/ITK-SNAP 

# Retrieve an image from the network, read it and display using the external viewer. 
# The show method will also set the display window's title and by setting debugOn to True, 
# will also print information with respect to the command it is attempting to invoke.
# NOTE: The debug information is printed to the terminal from which you launched the notebook
#       server.
sitk.Show(sitk.ReadImage(fetch_data("SimpleITK.jpg")), "SimpleITK Logo", debugOn=True)
Fetching SimpleITK.jpg

Now we check that the ipywidgets will display correctly. When you run the following cell you should see a slider.

If you don't see a slider please shutdown the Jupyter server, at the command line prompt press Control-c twice, and then run the following command:

jupyter nbextension enable --py --sys-prefix widgetsnbextension

In [4]:
interact(lambda x: x, x=(0,10));

Download all of the data in advance if you expect to be working offline (may take a couple of minutes).

In [ ]:
fetch_data_all(os.path.join('..','Data'), os.path.join('..','Data','manifest.json'))
Fetching cthead1.png
Fetching VM1111Shrink-RGB.png
Downloaded 184076 of 184076 bytes (100.00%)
Fetching 2th_cthead1.png
Downloaded 3095 of 3095 bytes (100.00%)
Fetching nac-hncma-atlas2013-Slicer4Version/Data/A1_grayT1.nrrd
Fetching nac-hncma-atlas2013-Slicer4Version/Data/A1_grayT2.nrrd
Fetching nac-hncma-atlas2013-Slicer4Version/Data/hncma-atlas.nrrd
Downloaded 466196 of 466196 bytes (100.00%)
Fetching B1.tiff
Downloaded 50196 of 50196 bytes (100.00%)
Fetching B2.tiff
Downloaded 50196 of 50196 bytes (100.00%)
Fetching B1_fixed_B2_moving0GenericAffine.mat
Downloaded 137 of 137 bytes (100.00%)
Fetching B1_fixed_B2_movingComposite.h5
Downloaded 816984 of 816984 bytes (100.00%)
Fetching coins.png
Downloaded 583024 of 583024 bytes (100.00%)
Fetching a_vm1108.png
Downloaded 408256 of 408256 bytes (100.00%)
Fetching vm_head_rgb.mha
Fetching vm_head_rgb.mha
Downloaded 75612160 of 1040255141 bytes (7.27%)
In [ ]: