# How to call the "Islands" function of the Segment Editor from a python script with "Keep selected island"

**URL:** <https://discourse.slicer.org/t/how-to-call-the-islands-function-of-the-segment-editor-from-a-python-script-with-keep-selected-island/14763>\
**Category:** Development\
**Created:** [November 25, 2020, 9:13am UTC](https://discourse.slicer.org/t/how-to-call-the-islands-function-of-the-segment-editor-from-a-python-script-with-keep-selected-island/14763 "2020-11-25T09:13:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [November 25, 2020, 9:13am UTC](https://discourse.slicer.org/t/how-to-call-the-islands-function-of-the-segment-editor-from-a-python-script-with-keep-selected-island/14763/1 "2020-11-25T09:13:46Z")

</div>

Hi,

I am developing an semiautomated script for CT lung masking.  
After thresholding and cutting out segments of the trachea and left main bronchus (works), I normally use the “Islands” function of the Segment Editor next with the parameter “Keep selected island”.

However, something like this

```
    ...
    segmentEditorWidget.setActiveEffectByName("Islands")
    effect = segmentEditorWidget.activeEffect()
    effect.setParameter("Operation","KEEP_SELECTED_ISLAND")
    effect.setParameter("MinimumSize","1000")
    effect.self().onApply()
    ...

```

does not work from my script.  
How can I pass coordinates / selected slice view / mouse click ?  
Would be glad if someone had an idea or a code snippet.

Thanks, best regards  
rudolf

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [November 25, 2020, 12:12pm UTC](https://discourse.slicer.org/t/how-to-call-the-islands-function-of-the-segment-editor-from-a-python-script-with-keep-selected-island/14763/2 "2020-11-25T12:12:57Z")

</div>

> [@rbumm](#):
>
> ```auto
> ...
> 
> def rasToDisplay(self, r, a, s):
> displayCoords = [0, 0, 0, 1]
> 
> # get the slice node
> lm = slicer.app.layoutManager()
> sliceWidget = lm.sliceWidget('Red')
> sliceLogic = sliceWidget.sliceLogic()
> sliceNode = sliceLogic.GetSliceNode()
> 
> xyToRASMatrix = sliceNode.GetXYToRAS()
> rasToXyMatrix = vtk.vtkMatrix4x4()
> rasToXyMatrix.Invert(xyToRASMatrix, rasToXyMatrix)
> 
> worldCoords = [r, a, s, 1.0]
> rasToXyMatrix.MultiplyPoint(worldCoords, displayCoords)
> 
> return (int(displayCoords[0]), int(displayCoords[1]))
> 
> ... 
> def how_to_call_keep_selected_islands():
> ...
> lm = slicer.app.layoutManager()
> sliceWidget = lm.sliceWidget('Red')
> segmentEditorWidget.setActiveEffectByName("Islands")
> effect = segmentEditorWidget.activeEffect()
> effect.setParameter("Operation","KEEP_SELECTED_ISLAND")
> slicer.util.clickAndDrag(sliceWidget, start=self.rasToDisplay(25.3, 5.8, sliceOffset), end=self.rasToDisplay(25.3, 5.8, sliceOffset), steps=1)
> ...
> 
> ```

Found the solution myself.  
Enjoy.

---

<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:** [November 25, 2020, 2:47pm UTC](https://discourse.slicer.org/t/how-to-call-the-islands-function-of-the-segment-editor-from-a-python-script-with-keep-selected-island/14763/3 "2020-11-25T14:47:06Z")

</div>

Thanks for sharing. You could make your code simpler and more robust by slightly changing the Islands effect so that it can take a 3D coordinate directly (not just an interaction event that you need to simulate via the GUI). The effect is a simple Python script, which you can freely modify locally (you could inject the 3D coordinate [here](https://github.com/Slicer/Slicer/blob/c7937deffdfb347f1a8144c5781689ba4fe5b537/Modules/Loadable/Segmentations/EditorEffects/Python/SegmentEditorIslandsEffect.py#L308)). If the modification works well for you then you can send us a pull request so that we can integrate that change into Slicer core.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [October 2, 2021, 1:16pm UTC](https://discourse.slicer.org/t/how-to-call-the-islands-function-of-the-segment-editor-from-a-python-script-with-keep-selected-island/14763/4 "2021-10-02T13:16:36Z")

</div>

> [@rbumm](#):
>
> ```auto
> ...
> 
> def rasToDisplay(self, r, a, s):
> displayCoords = [0, 0, 0, 1]
> 
> # get the slice node
> lm = slicer.app.layoutManager()
> sliceWidget = lm.sliceWidget('Red')
> sliceLogic = sliceWidget.sliceLogic()
> sliceNode = sliceLogic.GetSliceNode()
> 
> xyToRASMatrix = sliceNode.GetXYToRAS()
> rasToXyMatrix = vtk.vtkMatrix4x4()
> rasToXyMatrix.Invert(xyToRASMatrix, rasToXyMatrix)
> 
> worldCoords = [r, a, s, 1.0]
> rasToXyMatrix.MultiplyPoint(worldCoords, displayCoords)
> 
> return (int(displayCoords[0]), int(displayCoords[1]))
> 
> ... 
> def how_to_call_keep_selected_islands():
> ...
> lm = slicer.app.layoutManager()
> sliceWidget = lm.sliceWidget('Red')
> segmentEditorWidget.setActiveEffectByName("Islands")
> effect = segmentEditorWidget.activeEffect()
> effect.setParameter("Operation","KEEP_SELECTED_ISLAND")
> slicer.util.clickAndDrag(sliceWidget, start=self.rasToDisplay(25.3, 5.8, sliceOffset), end=self.rasToDisplay(25.3, 5.8, sliceOffset), steps=1)
> ...
> 
> ```

@rbumm 我想知道这个代码只是在一个slice上选点,可是这个slice如何确定呢…我想说的是三维空间的第三个点怎么确定呢?

> I want to know that this code just selects a point on a slice, but how to determine this slice…What I want to konw is how to determine the third point in the three-dimensional space?

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [October 2, 2021, 1:28pm UTC](https://discourse.slicer.org/t/how-to-call-the-islands-function-of-the-segment-editor-from-a-python-script-with-keep-selected-island/14763/5 "2021-10-02T13:28:26Z")

</div>

@lassoan 没有更简洁的方法,直接输入the 3D coordinate来获取island?

> There is no more concise way that inject the 3D coordinate to get the island…e.g. effiect.setparameter()
