Calling module with slicer functions fail in Jupyter notebook

I’m currently using slicer via jupyter. I’ve written a number of functions to help me batch segment 4dct data and I’m attempting to load those functions into my notebook via a module as I’ve got multiple patients to evaluate. When I try to access the functions, however, I get "name ‘slicer’ is not defined.

When the function is defined from within my notebook, it executes without an issue. I’ve checked the python path from within the module functions and it points to all the slicer-python locations.

Is there something special I need to do to import this module which calls numerous slicer executables?

OS: MacOS, 10:15
slicer: 4.11.20201026

The slicer namespace is only available if you use the Slicer kernel. You can choose the kernel when you create the notebook or later in the notebook’s menu (Kernel → Change kernel).

Right, I do start the notebook with the slicer kernel and the slicer gui launches. I then try to import a module in which I’ve written some functions in that also use the slicer namespace.

The notebook loads the module okay. Since the functions are not explicitly defined within the notebook, but loaded, does it not have access to the slicer namespace? I’ve included a section of the module that I’m trying to load into the notebook which is running the slicer kernel.

import sys
import slicer


def setup_airway_segment_editor(segmentationNode=None, masterVolumeNode=None):
    
"""
Runs standard setup of segment editor widget and segment editor node
"""
    if segmentationNode is None:
    # Create segmentation node
        segmentationNode = slicer.vtkMRMLSegmentationNode()
        slicer.mrmlScene.AddNode(segmentationNode)
        segmentationNode.CreateDefaultDisplayNodes()
    
    segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
    segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
    slicer.mrmlScene.AddNode(segmentEditorNode)
    segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
    segmentEditorWidget.setSegmentationNode(segmentationNode)
    segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
    if masterVolumeNode:
        segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
    
    # Add airway segment
    airwaySegmentID = segmentationNode.GetSegmentation().AddEmptySegment("Airway")
    
    # Add mask segment
    maskLabelMap = slicer.util.getNode('maskLabelMap')
    slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(maskLabelMap, segmentationNode)
    maskSegmentID = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName('mask')

    return segmentEditorWidget, segmentEditorNode, segmentationNode, airwaySegmentID, maskSegmentID

You can add functions to the global() namespace or slicer namespace (by simply using the = operator). However, normally you would not want to pollute these global namespaces but instead add helper functions to your module’s namespace or into your module logic, which you can import and use from anywhere.

For example, you can reach SampleData module functions logic like this:

  1. Run function specified in a module’s namespace:
from SampleData import *

downloadSample('MRHead')
  1. Run method specified in a module logic class:
import SampleData

logic = SampleData.SampleDataLogic()
logic.downloadCTACardio()