import JupyterNotebooksLib as slicernb
# Set image viewer size to 50% (fill half of a cell)
slicernb.AppWindow.setWindowSize(scale=0.5)
# Hide patient information from slice view
slicernb.showSliceViewAnnotations(False)
# Clear scene
slicer.mrmlScene.Clear(False)
# Load a Slicer sample data set
import SampleData
volume = SampleData.SampleDataLogic().downloadMRBrainTumor1()
print("Volume size: {0}".format(volume.GetImageData().GetDimensions()))
Volume size: (256, 256, 112)
# Show slice view
slicernb.ViewDisplay("OneUpRedSlice")
# Lightbox view
slicernb.ViewLightboxDisplay("Red", columns=5, rows=3, rangeShrink=[45,50])
# Enable 3D volume rendering
slicernb.showVolumeRendering(volume)
# Show slice and 3D views
slicernb.ViewDisplay("FourUp") # You can also try: FourUp, OneUpRedSlice, OneUp3D
# Slice view display
from ipywidgets import interact
@interact(position=(0,100))
def update(position=50):
return slicernb.ViewSliceDisplay('Red', positionPercent=position)
interactive(children=(IntSlider(value=50, description='position'), Output()), _dom_classes=('widget-interact',…
# Enable volume cropping
displayNode = slicer.modules.volumerendering.logic().GetFirstVolumeRenderingDisplayNode(volume)
displayNode.SetCroppingEnabled(True)
roiNode = displayNode.GetROINode()
# 3D view display
slicernb.reset3DView()
from ipywidgets import interact
@interact(roll=(-90.0,90.0,5), pitch=(-90.0,90.0,5), yaw=(-180.0,180.0,5), cropx=(0,70,5), cropy=(0,120,5), cropz=(0, 80, 5))
def update(roll=0, pitch=0, yaw=0, cropx=70, cropy=120, cropz=80):
roiNode.SetRadiusXYZ([cropx, cropy, cropz])
return slicernb.View3DDisplay(0, orientation=[roll, pitch, yaw])
interactive(children=(FloatSlider(value=0.0, description='roll', max=90.0, min=-90.0, step=5.0), FloatSlider(v…
# Slice widgets
from ipywidgets import HBox
display(HBox([slicernb.ViewSliceWidget('Red'), slicernb.ViewSliceWidget('Yellow'), slicernb.ViewSliceWidget('Green')]))
HBox(children=(ViewSliceWidget(children=(FloatSlider(value=0.0, description='Offset', max=78.40000000000002, m…
# 3D widget
display(HBox([slicernb.ViewSliceBaseWidget('Red', width="40%"), slicernb.View3DWidget(0, width="40%")]))
HBox(children=(ViewSliceBaseWidget(value=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xaf\x00\x00\x00\xc1…
# Adjust maximum rate of Slicer's Jupyter kernel consuming Jupyter messages.
# Lower values make the notebook more responsive but too low values may make the Slicer application
# slow to respond.
slicer.modules.jupyterkernel.setPollIntervalSec(0.001)
# 3D view
slicernb.AppWindow.setWindowSize(scale=0.5)
live3d = slicernb.ViewInteractiveWidget('1')
live3d.trackMouseMove = True
display(live3d)
ViewInteractiveWidget(height=193, width=175)
# Slice view (use arrow keys to move between slices, right-click-and-drag to zoom in/out)
liveRedSlice = slicernb.ViewInteractiveWidget('R')
liveRedSlice.trackMouseMove = True
display(liveRedSlice)
ViewInteractiveWidget(height=193, width=175)
slicernb.showVolumeRendering(volume, show=True)
app = slicernb.AppWindow()
# "404 : Not Found" error is displayed if Jupyter desktop server is not configured
app
# Show full application GUI
app.setContents("full")
slicer.util.selectModule("VolumeRendering")
Remote application view in separate browser tab: click here
import numpy as np
markupPoints = np.array([
[ 69.12484176, -8.73226641, -2.175 ],
[ 56.3258959 , 32.61817407, -2.175 ],
[ 46.15237483, 57.88788769, -2.175 ],
[ 34.02739799, 74.35843751, -2.175 ],
[ 3.93193405, 85.64423649, -2.175 ],
[-45.51061385, 62.53521954, -2.175 ],
[-61.63318381, -10.01634531, -2.175 ],
[-51.97287678, -54.02084408, -2.175 ],
[-18.1022449 , -87.94210015, -2.175 ],
[ 32.95255999, -83.64274816, -2.175 ],
[ 58.6231426 , -59.59987177, -2.175 ],
[ 67.88479492, -35.27503826, -2.175 ]])
closedCurve = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsClosedCurveNode")
slicer.util.updateMarkupsControlPointsFromArray(closedCurve, markupPoints)
closedCurve # slicernb.displayable(obj) is called automatially for cell outputs
label | position.R | position.A | position.S | selected | visible | description | |
---|---|---|---|---|---|---|---|
0 | MarkupsClosedCurve-1 | 69.124842 | -8.732266 | -2.175 | True | True | |
1 | MarkupsClosedCurve-2 | 56.325896 | 32.618174 | -2.175 | True | True | |
2 | MarkupsClosedCurve-3 | 46.152375 | 57.887888 | -2.175 | True | True | |
3 | MarkupsClosedCurve-4 | 34.027398 | 74.358438 | -2.175 | True | True | |
4 | MarkupsClosedCurve-5 | 3.931934 | 85.644236 | -2.175 | True | True | |
5 | MarkupsClosedCurve-6 | -45.510614 | 62.535220 | -2.175 | True | True | |
6 | MarkupsClosedCurve-7 | -61.633184 | -10.016345 | -2.175 | True | True | |
7 | MarkupsClosedCurve-8 | -51.972877 | -54.020844 | -2.175 | True | True | |
8 | MarkupsClosedCurve-9 | -18.102245 | -87.942100 | -2.175 | True | True | |
9 | MarkupsClosedCurve-10 | 32.952560 | -83.642748 | -2.175 | True | True | |
10 | MarkupsClosedCurve-11 | 58.623143 | -59.599872 | -2.175 | True | True | |
11 | MarkupsClosedCurve-12 | 67.884795 | -35.275038 | -2.175 | True | True |
closedCurve.SetNthControlPointSelected(3, False)
closedCurve.GetDisplayNode().SetSelectedColor(0,0,1)
closedCurve.GetDisplayNode().SetGlyphScale(5)
closedCurve.GetDisplayNode().UseGlyphScaleOn()
slicer.util.setSliceViewerLayers(fit=True)
slicer.modules.markups.logic().JumpSlicesToNthPointInMarkup(closedCurve.GetID(), 1)
app.setWindowSize(scale=1.0)
app.setContents("viewers")
slicernb.ViewDisplay('OneUpRedSlice', center=False)
An experimental approach is used for displaying model nodes. They are rendered outside the views in the layout, therefore only this node is visible.
# Simple static display
modelNode=slicer.modules.models.logic().AddModel(slicernb.localPath("data/ProstateMeanShape.stl"))
modelNode
# Interactive view rotation
from ipywidgets import interact
@interact(roll=(-90.0,90.0,5), pitch=(-90.0,90.0,5), yaw=(-90.0,90.0,5))
def update(roll=0, pitch=0, yaw=0):
return slicernb.ModelDisplay(modelNode, orientation=[roll, pitch, yaw], imageSize=[200,200])
interactive(children=(FloatSlider(value=0.0, description='roll', max=90.0, min=-90.0, step=5.0), FloatSlider(v…
try:
import matplotlib
except ModuleNotFoundError:
pip_install('matplotlib')
import matplotlib
matplotlib.use('Agg')
# Get a volume from SampleData and compute its histogram
import numpy as np
histogram = np.histogram(arrayFromVolume(volume), bins=50)
# Show a plot using matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(histogram[1][1:], histogram[0].astype(float))
ax.grid(True)
ax.set_ylim((0, 4e5))
slicernb.MatplotlibDisplay(plt)
Collecting matplotlib Downloading matplotlib-3.2.1-cp36-cp36m-manylinux1_x86_64.whl (12.4 MB) Collecting kiwisolver>=1.0.1 Downloading kiwisolver-1.2.0-cp36-cp36m-manylinux1_x86_64.whl (88 kB) Requirement already satisfied: python-dateutil>=2.1 in /home/sliceruser/Slicer-4.11.0-2020-05-14-linux-amd64/lib/Python/lib/python3.6/site-packages (from matplotlib) (2.8.1) Collecting cycler>=0.10 Downloading cycler-0.10.0-py2.py3-none-any.whl (6.5 kB) Requirement already satisfied: numpy>=1.11 in /home/sliceruser/Slicer-4.11.0-2020-05-14-linux-amd64/lib/Python/lib/python3.6/site-packages (from matplotlib) (1.18.1) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /home/sliceruser/Slicer-4.11.0-2020-05-14-linux-amd64/lib/Python/lib/python3.6/site-packages (from matplotlib) (2.4.6) Requirement already satisfied: six>=1.5 in /home/sliceruser/Slicer-4.11.0-2020-05-14-linux-amd64/lib/Python/lib/python3.6/site-packages (from python-dateutil>=2.1->matplotlib) (1.14.0) Installing collected packages: kiwisolver, cycler, matplotlib Successfully installed cycler-0.10.0 kiwisolver-1.2.0 matplotlib-3.2.1 generated new fontManager
# Select a model file to upload (e.g., obj file)
# This currently does not work - see https://github.com/jupyter-xeus/xeus-python/issues/272
uploader = slicernb.FileUploadWidget()
uploader
# This cell will work after file upload (above cell) is fixed
#model=slicer.modules.models.logic().AddModel(uploader.path)
#model
# Show download link
outputFile = "exported.stl"
slicer.util.saveNode(modelNode, slicernb.localPath(outputFile))
from IPython.display import FileLink
FileLink(outputFile)