# Python Script for segment growing and Logical operations

**URL:** <https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004>\
**Category:** Development\
**Created:** [January 29, 2020, 5:51pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004 "2020-01-29T17:51:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![manjula](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/manjula/32/80981_2.png) [@manjula](https://discourse.slicer.org/u/manjula)\
**Post date:** [January 29, 2020, 5:51pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/1 "2020-01-29T17:51:06Z")

</div>

Hi all,

I want to write a python code to grow a segment margin. (need to overlap and also grow in to a specified intensity only)

Then i want to subtract the newly grown segment from the original.

How do i do that ?

I tried to look for simpleITK documentation at

[https://itk.org/SimpleITKDoxygen120/html/index.html](https://itk.org/SimpleITKDoxygen120/html/index.html) but it is not working.

It would be great if some

Thank you

---

<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:** [January 29, 2020, 5:56pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/2 "2020-01-29T17:56:01Z")

</div>

These examples should help you getting started: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How\_to\_run\_segment\_editor\_effects\_from\_a\_script](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_run_segment_editor_effects_from_a_script)

---

<div class="post-metadata">

**Author:** ![manjula](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/manjula/32/80981_2.png) [@manjula](https://discourse.slicer.org/u/manjula)\
**Post date:** [January 29, 2020, 5:58pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/3 "2020-01-29T17:58:49Z")

</div>

Thank you Prof Lasso,

I looked at them also but i could not find how to grow and subtract segments.

Is there any documentation for simpleITK like a handbook available ?

i looked at [https://itk.org/SimpleITKDoxygen120/html/index.html](https://itk.org/SimpleITKDoxygen120/html/index.html) but the link is not working.

---

<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:** [January 29, 2020, 6:31pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/4 "2020-01-29T18:31:35Z")

</div>

> [@manjula](#):
>
> I looked at them also but i could not find how to grow and subtract segments.

There are no examples for using all the effects, but you can start from those examples and just change the effect name and input parameters if you want to use different effects.

You can post questions about SimpleITK to the [ITK forum](https://discourse.itk.org/).

---

<div class="post-metadata">

**Author:** ![manjula](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/manjula/32/80981_2.png) [@manjula](https://discourse.slicer.org/u/manjula)\
**Post date:** [January 29, 2020, 6:32pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/5 "2020-01-29T18:32:44Z")

</div>

Thank Prof Lasso. I will try to work with this information.

---

<div class="post-metadata">

**Author:** ![Juicy](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Juicy](https://discourse.slicer.org/u/Juicy)\
**Post date:** [January 29, 2020, 10:43pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/6 "2020-01-29T22:43:14Z")

</div>

I have a question slightly related to this. I am referring to the code in [this link](https://gist.github.com/lassoan/1673b25d8e7913cbc245b4f09ed853f9).

To set the active effect and change some parameters this following code is used:

```
# Thresholding
segmentEditorWidget.setActiveEffectByName("Threshold")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumThreshold","35")
effect.setParameter("MaximumThreshold","695")
effect.self().onApply()

```

I see the setParameter() method takes a string input (to select the parameter) and a value input (to define the value of that parameter). Is there any documentation anywhere that lists what the names of all the parameters are for a given active effect? I have been just trying to guess what the parameters are called but not having much luck.

---

<div class="post-metadata">

**Author:** ![Juicy](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Juicy](https://discourse.slicer.org/u/Juicy)\
**Post date:** [January 29, 2020, 11:09pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/7 "2020-01-29T23:09:49Z")

</div>

I think I answered my own question.

Found this link: [https://discourse.slicer.org/t/scissors-segment-editor-effects-from-a-script/8348/2?u=juicy](https://discourse.slicer.org/t/scissors-segment-editor-effects-from-a-script/8348/2)

I found the slicer segment editor effect code in the Slicer core code [here](https://github.com/Slicer/Slicer/tree/master/Modules/Loadable/Segmentations/EditorEffects)

I should be able to find the parameter names which I need by sifting through the code.

---

<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:** [January 29, 2020, 11:36pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/8 "2020-01-29T23:36:59Z")

</div>

It would be nice to extract the parameter names from the code and present it in a developer documentation page instead of requiring developers to mine it from the source code.

However, for this we would need much tighter integration between source code and documentation. Currently they are stored in two separate systems (svn and wiki), but soon both will be managed in git. When the transition completes, in a few weeks or worst case months, we can start working on improving the developer documentation with such details.

---

<div class="post-metadata">

**Author:** ![manjula](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/manjula/32/80981_2.png) [@manjula](https://discourse.slicer.org/u/manjula)\
**Post date:** [January 30, 2020, 7:50am UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/9 "2020-01-30T07:50:23Z")

</div>

Thank you @Juicy and @lassoan

This has been super helpful for me as i try to work on the code. What i was trying to ask for was what @Juicy asked even though i failed to put it in that way, instead of having to come back here and asking is there any documentation related to 3DSlicer python interactor that gives functions/methods and what inputs they take etc… so we can easily go through and write the code…I find good documents on C++ but i don’t know it.

---

<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:** [January 30, 2020, 3:17pm UTC](https://discourse.slicer.org/t/python-script-for-segment-growing-and-logical-operations/10004/11 "2020-01-30T15:17:01Z")

</div>

The [PerkLab Scripting and module development tutorial](https://www.slicer.org/wiki/Documentation/Nightly/Training#PerkLab.27s_Slicer_bootcamp_training_materials) contains links to API documentation sites and example of how to use Python `help` command to get Python-style manual for a specific command or class in the console.

I see how argument type information specified in C++ may look alien to Python developers, but it is just a small syntax difference that can be “translated” by a number of simple rules. I give a few examples here how a mapping could look like and if you find it useful then let me know and I’ll consider creating a more comprehensive documentation page:

| C++ | Python | | | |
| --- | --- | --- | --- | --- |
| nullptr | None | | | |
| Q(some-class-name)\* | qt.Q(some-class-name) | QWidget\* | qt.QWidget | |
| Qt::(some-type) | qt.Qt.(some-type) | Qt::WindowFlags | qt.Qt.WindowFlags | |
| qMRML(some-class-name)\* | slicer.qMRML(some-class-name) | qMRMLTableWidget\* | slicer.qMRMLTableWidget | |

Syntax differences:

- type before the method name specifies what type of data the method returns; `void` means that the method does not return any data
- const before an argument type means that the called method will not change that input argument
