Help with Accessing Module Logic in Python Scripted Modules

Hi folks,
I’m just getting started with slicer and python and I’m hoping to get some help regarding how to access the underlying logic of Slicer modules from within a python script? (i.e. the Python interactor or the python code of a scripted module)

Specifically, I’ve got a scripted module I made using the Extension Wizard (after working through these two tutorials on python and python modules). In the ‘run’ section of my scripted module, I’d like to call the Simple Filters module to use the ConnectedThresholdImageFilter and then call the Model Maker module to turn that segmentation into a model.

I’ve found this example showing the use of the Model Maker module via python, but I’m hoping to have an understanding that’s more generalizable to other modules (i.e. are all modules accessed via “slicer.cli.run( …)”? How would I figure out which parameters need to be passed to the module if I didn’t know them in advance?).

Thanks in advance for your help!

Hi Justin,

You can get the parameters of CLI modules as described in the wiki.

I think you can have access to the logic of Slicer modules like this:
>>> slicer.vtkSlicerTransformLogic()
(vtkCommonCorePython.vtkSlicerTransformLogic)0x138f47738
>>> slicer.modules.transforms.logic()
(vtkCommonCorePython.vtkSlicerTransformLogic)0x138f476d0

I don’t think you need to use the Simple Filters module if you’re doing Python. You can just use SimpleITK in your code.

Hi Fernando,
Thanks so much for your help! I’ve been able to sort out the details of using the model maker CLI module to go from label map to model as well as using SimpleITK to perform the segmentation.

My only remaining hiccup is that in trying to connect the two, the filter in SimpleITK returns a scalar volume node while the model maker module requires a label-map to behave as intended (when I pass it a volume node it still runs but it appears to be trying to make a model out of the entire volume rather than the segmented area only). I can see the Volumes module can do this with the ‘Convert to Label Map’ button but I’m unsure how to call that button from a python script (unlike module maker, the Volumes appears to be a loadable module rather than a CLI one).

Try looking in slicer.modules.volumes.logic()

It’s what happens when you click on the button anyway: https://github.com/Slicer/Slicer/blob/eefeac9286f48552e8418a11f412b2823a09b407/Modules/Loadable/Volumes/qSlicerVolumesModuleWidget.cxx#L138

I searched “Convert to label map” in the 3D Slicer repository and found what I was looking for in the first result.

Instead of maintaining a labelmap for editing and slice display and model(s) for 3D display and converting between manually, I would strongly recommend to use a segmentation node. With just 4 lines of Python code, you can create segmentation node from a labelmap, and after that the segmentation node takes care of all visualization and conversion:
https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Create_a_segmentation_from_a_labelmap_volume_and_display_in_3D

Using segmentation has many advantages compared to using labelmaps and models, including:

  • you have only one node, which can be displayed in slice and 3D views: no need for “model making” etc.
  • you can directly edit the segmentation using the new Segment editor module (which is far more capable than the old Editor module)
  • when labelmap changes, all views in slice and 3D views are updated automatically
  • there are more visualization options for segmentation than for labelmaps and models (see in the Segmentations module)
  • segments may overlap

Fernando, thanks again for your help! I’m also pretty new to github so I hadn’t realized you could search an entire repository. I’ve now got the pipeline of segmentation and model making working as intended.

Andras, I already had the model making process sorted out so I won’t bother re-factoring this time around but I looked at your suggested approach and I agree it seems much simpler. I’ll be sure to keep that in mind for future.

1 Like