Data processing

In [1]:
import JupyterNotebooksLib as slicernb

# Set image viewer size to 50% (fill half of a cell)
slicernb.AppWindow.setWindowSize(scale=0.8)
slicernb.AppWindow.setContents("viewers")
# Hide patient information from slice view
slicernb.showSliceViewAnnotations(False)

# Example of installing an extension:
# slicernb.installExtensions(["SegmentEditorExtraEffects"])

Image processing

In [4]:
# Clear scene
slicer.mrmlScene.Clear(False)

# Load data
import SampleData
volume = slicer.util.loadVolume('data/MRBrainTumor1.nrrd')

# Create output node
blurredVolume = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode")
blurredVolume.CreateDefaultDisplayNodes()

# Show 3-over-3 compare layout with original and blurred volume
layoutManager = slicer.app.layoutManager()
for sliceViewName in layoutManager.sliceViewNames():
  sliceWidget = layoutManager.sliceWidget(sliceViewName)
  volumeNodeToShow = volume if sliceWidget.mrmlSliceNode().GetViewGroup() == 0  else blurredVolume
  sliceWidget.mrmlSliceCompositeNode().SetBackgroundVolumeID(volumeNodeToShow.GetID())

# Create a function for performing the filtering.
# This will be executed whenever the slider is moved and released.
def threshold(lower=-20, upper=180):
    parameters = {
        "InputVolume": volume.GetID(),
        "OutputVolume": blurredVolume.GetID(),
        #"ThresholdType" : 'Outside',
        "Lower": lower,
        "Upper": upper
        }
    slicer.cli.runSync(slicer.modules.thresholdscalarvolume, parameters=parameters, update_display=False)
    return slicernb.ViewDisplay("ThreeOverThree")

# Create slider
from ipywidgets import FloatSlider, interact
interact(threshold,
         lower=FloatSlider(150, min=-50, max=300, step=5, continuous_update=False),
         upper=FloatSlider(250, min=-50, max=300, step=5, continuous_update=False))
Out[4]:
<function threshold at 0x000001BCF6EE6730>

Create model from volume

In [5]:
# Clear scene
slicer.mrmlScene.Clear(False)

import SampleData
volume = SampleData.SampleDataLogic().downloadCTChest()

# Enable 3D volume rendering
slicernb.showVolumeRendering(volume)
display(slicernb.ViewDisplay("FourUp"))
slicernb.showVolumeRendering(volume, show=False)