Welcome to SimpleITK Jupyter Notebooks¶
Newcomers to Jupyter Notebooks:¶
- We use two types of cells, code and markdown.
- 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.
- 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 project documentation on Jupyter Notebook or JupyterLab.
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.
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",
"numba",
"multiprocess",
"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) == 0:
print("All is well.")
else:
print(
"The following packages are required but not installed: "
+ ", ".join(problem_packages)
)
All is well.
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: 2.4.0 (ITK 5.4) Compiled: Aug 13 2024 14:19:28
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.
# 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
Debug: In /Users/runner/work/SimpleITK/SimpleITK/Code/IO/src/sitkImageViewer.cxx, line 624: ExecuteCommand: 'open' '-a' '/Applications/Fiji.app' '-n' '--args' '-eval' 'open("/tmp/SimpleITKLogo-34964-0.mha"); rename("SimpleITK Logo");' Debug: In /Users/runner/work/SimpleITK/SimpleITK/Code/IO/src/sitkImageViewer.cxx, line 664: Normal process exit. exitValue = 0 Debug: In /Users/runner/work/SimpleITK/SimpleITK/Code/IO/src/sitkImageViewer.cxx, line 707: Done. Deleting process.
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 notebook 5.2 or earlier):
jupyter nbextension enable --py --sys-prefix widgetsnbextension
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).
fetch_data_all(os.path.join("..", "Data"), os.path.join("..", "Data", "manifest.json"))
Fetching cthead1.png Fetching VM1111Shrink-RGB.png Fetching 2th_cthead1.png 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 Fetching B1.tiff Fetching B2.tiff Fetching B1_fixed_B2_moving0GenericAffine.mat Fetching B1_fixed_B2_movingComposite.h5 Fetching coins.png Fetching a_vm1108.png Fetching vm_head_rgb.mha
Fetching vm_head_mri.mha Fetching training_001_ct.mha Fetching training_001_mr_T1.mha Fetching ct_T1.standard Fetching CIRS057A_MR_CT_DICOM/readme.txt Fetching SimpleITK.jpg Fetching spherical_fiducials.mha Fetching POPI/meta/00-P.mhd Fetching POPI/meta/10-P.mhd Fetching POPI/meta/20-P.mhd Fetching POPI/meta/30-P.mhd Fetching POPI/meta/40-P.mhd Fetching POPI/meta/50-P.mhd Fetching POPI/meta/60-P.mhd Fetching POPI/meta/70-P.mhd Fetching POPI/meta/80-P.mhd Fetching POPI/meta/90-P.mhd Fetching POPI/landmarks/00-Landmarks.pts Fetching POPI/landmarks/10-Landmarks.pts Fetching POPI/landmarks/20-Landmarks.pts Fetching POPI/landmarks/30-Landmarks.pts Fetching POPI/landmarks/40-Landmarks.pts Fetching POPI/landmarks/50-Landmarks.pts Fetching POPI/landmarks/60-Landmarks.pts Fetching POPI/landmarks/70-Landmarks.pts Fetching POPI/landmarks/80-Landmarks.pts Fetching POPI/landmarks/90-Landmarks.pts Fetching POPI/masks/00-air-body-lungs.mhd Fetching POPI/masks/10-air-body-lungs.mhd Fetching POPI/masks/20-air-body-lungs.mhd Fetching POPI/masks/30-air-body-lungs.mhd Fetching POPI/masks/40-air-body-lungs.mhd Fetching POPI/masks/50-air-body-lungs.mhd Fetching POPI/masks/60-air-body-lungs.mhd Fetching POPI/masks/70-air-body-lungs.mhd Fetching POPI/masks/80-air-body-lungs.mhd Fetching POPI/masks/90-air-body-lungs.mhd Fetching liverTumorSegmentations/Patient01Homo.mha Fetching liverTumorSegmentations/Patient01Homo_Rad01.mha Fetching liverTumorSegmentations/Patient01Homo_Rad02.mha Fetching liverTumorSegmentations/Patient01Homo_Rad03.mha Fetching Control.tif Fetching head_mr_oriented.mha Fetching fib_sem_bacillus_subtilis.mha
Fetching mr_slice_atlas/readme.txt Fetching photo.dcm Fetching leg_panorama/readme.txt Fetching microscopy_colocalization.nrrd Fetching cxr.dcm