# Help with Accessing Module Logic in Python Scripted Modules

**URL:** <https://discourse.slicer.org/t/help-with-accessing-module-logic-in-python-scripted-modules/409>\
**Category:** Development\
**Created:** [May 31, 2017, 10:08pm UTC](https://discourse.slicer.org/t/help-with-accessing-module-logic-in-python-scripted-modules/409 "2017-05-31T22:08:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_jmichael](https://avatars.discourse-cdn.com/v4/letter/_/ce7236/32.png) [@\_jmichael](https://discourse.slicer.org/u/_jmichael)\
**Post date:** [May 31, 2017, 10:08pm UTC](https://discourse.slicer.org/t/help-with-accessing-module-logic-in-python-scripted-modules/409/1 "2017-05-31T22:08:00Z")

</div>

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](https://www.slicer.org/w/images/c/c0/Slicer4_ProgrammingTutorial_Slicer4.5.pdf) [two](https://docs.google.com/presentation/d/1JXIfs0rAM7DwZAho57Jqz14MRn2BIMrjB17Uj_7Yztc/edit#slide=id.g420896289_0102) 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](http://www.na-mic.org/Wiki/index.php/AHM2012-Slicer-Python#CLI_Modules) 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!

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [May 31, 2017, 10:48pm UTC](https://discourse.slicer.org/t/help-with-accessing-module-logic-in-python-scripted-modules/409/2 "2017-05-31T22:48:01Z")

</div>

Hi Justin,

You can get the parameters of CLI modules as described in the [wiki](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Running_a_CLI_from_Python).

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.

---

<div class="post-metadata">

**Author:** ![\_jmichael](https://avatars.discourse-cdn.com/v4/letter/_/ce7236/32.png) [@\_jmichael](https://discourse.slicer.org/u/_jmichael)\
**Post date:** [June 2, 2017, 3:39am UTC](https://discourse.slicer.org/t/help-with-accessing-module-logic-in-python-scripted-modules/409/3 "2017-06-02T03:39:45Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [June 2, 2017, 8:11am UTC](https://discourse.slicer.org/t/help-with-accessing-module-logic-in-python-scripted-modules/409/4 "2017-06-02T08:11:38Z")

</div>

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](https://github.com/Slicer/Slicer/blob/eefeac9286f48552e8418a11f412b2823a09b407/Modules/Loadable/Volumes/qSlicerVolumesModuleWidget.cxx#L138)

I searched “Convert to label map” in the [3D Slicer repository](https://github.com/Slicer/Slicer) and found what I was looking for in the first result.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 2, 2017, 2:19pm UTC](https://discourse.slicer.org/t/help-with-accessing-module-logic-in-python-scripted-modules/409/5 "2017-06-02T14:19:35Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![\_jmichael](https://avatars.discourse-cdn.com/v4/letter/_/ce7236/32.png) [@\_jmichael](https://discourse.slicer.org/u/_jmichael)\
**Post date:** [June 2, 2017, 7:24pm UTC](https://discourse.slicer.org/t/help-with-accessing-module-logic-in-python-scripted-modules/409/6 "2017-06-02T19:24:02Z")

</div>

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.
