Giter VIP home page Giter VIP logo

slicerjupyter's Introduction

SlicerJupyter

Extension for 3D Slicer that allows the application to be used from Jupyter notebook

Demo video: https://youtu.be/oZ3_cRXX2QM

Usage

Option 1. Run using Binder

You can use this option for a quick start. No installation or setup is needed, just click the link below and start using Slicer via Jupyter notebook in your web browser.

Binder

When you click on the link, Binder launches 3D Slicer with SlicerJupyter extension on their cloud servers. Binder is a free service and server resources are quite limited. Also, there is no interactive access to the graphical user interface. Therefore, this option is only recommended for testing, demos, or simple computations or visualizations.

Option 2. Run Slicer and Jupyter on your own computer

Setup

  • Install Python and Jupyter notebook
    • Install Anaconda (recommended) or any other Python distribution (see installation instructions here)
    • You can choose any Python version and any bitness (Python 3, 64-bit is recommended)
    • Adding Python to your PATH environment variable or registering as default Python is not required
  • Install 3D Slicer, start it, and install SlicerJupyter extension in its Extension Manager, restart 3D Slicer
  • Install Python packages in 3D Slicer's Python console by copy-pasting these lines:
import os
if os.name=='nt':
    # There are no official pyzmq wheels for Python-3.6 for Windows, so we have to install manually
    pip_install("https://files.pythonhosted.org/packages/94/e1/13059383d21444caa16306b48c8bf7a62331ca361d553d2119696ea67119/pyzmq-19.0.0-cp36-cp36m-win_amd64.whl")
else:
    # PIL may be corrupted on linux, reinstall from pillow
    pip_install('--upgrade pillow --force-reinstall')
pip_install("ipywidgets pandas ipyevents ipycanvas")
  • Install Slicer jupyter kernel
    • Switch to JupyterKernel module in 3D Slicer
    • Click "Copy command to clipboard" to copy the kernel installation command to the clipboard
    • Start a command prompt in the Python environment where Jupyter is installed, and paste and run the kernel installation command
    • Install Python packages for dynamic Slicer views display by running these command in the installed Python environment: python -m pip install jupyter ipywidgets pandas ipycanvas ipyevents. For Jupyter lab, run these additional commands:
conda install -c conda-forge nodejs
pip install ipywidgets ipyevents ipycanvas
jupyter labextension install @jupyter-widgets/jupyterlab-manager
jupyter labextension install @jupyter-widgets/jupyterlab-manager ipycanvas
jupyter labextension install @jupyter-widgets/jupyterlab-manager ipyevents
  • Start Jupyter notebook. For example, by runnning jupyter-notebook executable.

See video of installation steps using Anaconda here:

Using Slicer from a notebook

  • Create a new notebook, selecting Slicer 4.x kernel (for example, Slicer 4.11). Jupyter will open a new Slicer instance automatically when kernel start is requested. This Slicer instance will be automatically closed when kernel shutdown is requested.

Select Slicer kernel

  • While the kernel is starting, "Kernel starting, please wait.." message is displayed. After maximum few ten seconds Slicer kernel should start.
  • Do a quick test - show views content in the notebook:
import JupyterNotebooksLib as slicernb
slicernb.ViewDisplay()
  • Try the interactive view widget:
slicernb.ViewInteractiveWidget()
  • Hit Tab key for auto-complete
  • Hit Shift+Tab for showing documentation for a method (hit multiple times to show more details). Note: method name must be complete (you can use Tab key to complete the name) and the cursor must be inside the name or right after it (not in the parentheses). For example, type slicer.util.getNode and hit Shift+Tab.

Hit Tab key to auto-complete

Hit Shift-Tab key to inspect

Examples

You can get started by looking at example Slicer notebooks here.

For developers

Build instructions

  • Build the extension against the newly built Slicer with Qt5 and VTK9 enabled.

  • Install Jupyter

mkvirtualenv -p python3.6 jupyter_env  # Create and activare virtual environment

pip install jupyter
  • Install kernel
jupyter-kernelspec install /tmp/SlicerJupyter-build/inner-build/share/Slicer-4.11/qt-loadable-modules/JupyterKernel/Slicer-4.9/ --replace --user
  • Start notebook
workon jupyter_env
python -m jupyter notebook

Launch a kernel manually

Type this into Slicer's Python console to manually start a kernel that a notebook can connect to:

connection_file=r'C:\Users\andra\AppData\Roaming\jupyter\runtime\kernel-3100f53f-3433-40f9-8978-c72ed8f88515.json'
print('Jupyter connection file: ['+connection_file+']')
slicer.modules.jupyterkernel.startKernel(connection_file)

Path of connection_file is printed on jupyter notebook's terminal window.

Special commands

These commands must be the last commands in a cell.

  • __kernel_debug_enable(): enable detailed logging of all incoming Jupyter requests
  • __kernel_debug_disable(): enable detailed logging of all incoming Jupyter requests

slicerjupyter's People

Contributors

lassoan avatar jcfr avatar ihnorton avatar fedorov avatar pieper avatar tommydino93 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.