# Python - Editing Feature Size Local Threshold

**URL:** <https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393>\
**Category:** Development\
**Created:** [July 6, 2020, 1:15pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393 "2020-07-06T13:15:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 6, 2020, 1:15pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/1 "2020-07-06T13:15:35Z")

</div>

In the local threshold code on GitHub, there is a function updateMRMLfromGUI:

def updateMRMLFromGUI(self):  
SegmentEditorThresholdEffect.updateMRMLFromGUI(self)

```
minimumDiameterMm = self.minimumDiameterSpinBox.value
self.scriptedEffect.setParameter(MINIMUM_DIAMETER_MM_PARAMETER_NAME, minimumDiameterMm)

featureSizeMm = self.featureSizeSpinBox.value
self.scriptedEffect.setParameter(FEATURE_SIZE_MM_PARAMETER_NAME, featureSizeMm)

segmentationAlgorithm = self.segmentationAlgorithmSelector.currentText
self.scriptedEffect.setParameter(SEGMENTATION_ALGORITHM_PARAMETER_NAME, segmentationAlgorithm)

```

How would I edit this code to set the feature size to 6.000 mm? Do I just replace featureSizeMm with 6?

Thanks!

---

<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:** [July 6, 2020, 1:33pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/2 "2020-07-06T13:33:58Z")

</div>

You don’t need to change the source code of a Segment Editor effect if you just want to change effect parameters from a Python script. Instead, you simply call `effect.setParameter` method from outside. See complete examples in the script repository: [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:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 6, 2020, 1:37pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/3 "2020-07-06T13:37:03Z")

</div>

So for local threshold effect, I would type in:  
localThresholdEffect.setFeatureSize(6)  
localThresholdEffect.setMinimumThreshold(250)  
localThresholdEffect.setMaximumThreshold(1000)

Our code just takes in a point on a vertebra and runs local threshold from that point. We have already coded the taking in a point.

---

<div class="post-metadata">

**Author:** ![vertebrae](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@vertebrae](https://discourse.slicer.org/u/vertebrae)\
**Post date:** [July 6, 2020, 3:25pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/4 "2020-07-06T15:25:07Z")

</div>

Do we also need to include values for BACKGROUND\_VALUE and LABEL\_VALUE in the apply function?

---

<div class="post-metadata">

**Author:** ![vertebrae](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@vertebrae](https://discourse.slicer.org/u/vertebrae)\
**Post date:** [July 6, 2020, 3:51pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/5 "2020-07-06T15:51:50Z")

</div>

Also SELECTED\_ISLAND\_VALUE and OUTSIDE\_THRESHOLD\_VALUE

---

<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:** [July 6, 2020, 4:27pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/6 "2020-07-06T16:27:26Z")

</div>

> [@vertebrae](#):
>
> Do we also need to include values for BACKGROUND\_VALUE and LABEL\_VALUE in the apply function?

You don’t need to do anything with BACKGROUND\_VALUE, LABEL\_VALUE, etc. - they are internal constants.

The [only parameter for `apply` method is `ijkPoints`](https://github.com/lassoan/SlicerSegmentEditorExtraEffects/blob/5b46d10fd6ca81e9b714fc92fd05f621bc041adf/SegmentEditorLocalThreshold/SegmentEditorLocalThresholdLib/SegmentEditorEffect.py#L336) and the method uses scripted effect parameters:

> <https://github.com/lassoan/SlicerSegmentEditorExtraEffects/blob/5b46d10fd6ca81e9b714fc92fd05f621bc041adf/SegmentEditorLocalThreshold/SegmentEditorLocalThresholdLib/SegmentEditorEffect.py#L173-L179>

---

<div class="post-metadata">

**Author:** ![vertebrae](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@vertebrae](https://discourse.slicer.org/u/vertebrae)\
**Post date:** [July 6, 2020, 5:38pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/7 "2020-07-06T17:38:30Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 6, 2020, 6:30pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/8 "2020-07-06T18:30:00Z")

</div>

Hello Andras!

We are in the testing stage of the entire code now. To confirm, we are running it line by line in the python interactor. The GitHub code uses an apply function(which appears to be synonymous with run the algorithm). The parameters for apply are self and ijkPoints.

Whenever we run the apply function:’

apply(ijkPoints)

It says apply() missing 1 required positional argument: ‘ijkPoints’

We have defined it already, so what do we do?

Thanks!

---

<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:** [July 6, 2020, 6:37pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/9 "2020-07-06T18:37:12Z")

</div>

You need to call apply like this: `effect.apply(ijkPoints)`

---

<div class="post-metadata">

**Author:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 6, 2020, 6:38pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/10 "2020-07-06T18:38:15Z")

</div>

It says effect is not defined when I attempted this…

---

<div class="post-metadata">

**Author:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 6, 2020, 6:42pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/11 "2020-07-06T18:42:53Z")

</div>

![Screen Shot 2020-07-06 at 2.42.25 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/7/873038c511c26b0591dacfb40e6744d16c58bf21.png)

---

<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:** [July 6, 2020, 6:49pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/12 "2020-07-06T18:49:26Z")

</div>

You need to set an active effect first.

---

<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:** [July 6, 2020, 7:00pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/14 "2020-07-06T19:00:22Z")

</div>

Call `setActiveEffectByName`, as it is done in all the [examples](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_run_segment_editor_effects_from_a_script).

---

<div class="post-metadata">

**Author:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 6, 2020, 7:15pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/15 "2020-07-06T19:15:43Z")

</div>

We set an active effect by name, but once again receive another error. It says:

Attribute Error: qSlicerSegmentEditorScriptEffect has no attribute named ‘apply’

This was after running effect.apply(ijkPoints)

We defined the function apply, what should we do?

---

<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:** [July 6, 2020, 7:41pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/16 "2020-07-06T19:41:29Z")

</div>

Indeed, in case of this effect, which is derived from C++ effect, the syntax is slightly different, as `effect` refers to the C++ effect, and you need to call `effect.self()` to access the methods added in Python. So, a complete example:

```python
segmentEditorWidget = slicer.modules.segmenteditor.widgetRepresentation().self().editor
segmentEditorWidget.setActiveEffectByName("Local Threshold")
effect = segmentEditorWidget.activeEffect()
ijkPoints = vtk.vtkPoints()
ijkPoints.InsertNextPoint(127,102,80)
effect.self().apply(ijkPoints)

```

---

<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:** [July 7, 2020, 8:01pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/17 "2020-07-07T20:01:44Z")

</div>

A post was merged into an existing topic: [Python Module Logic Class](https://discourse.slicer.org/t/python-module-logic-class/12415/8)

---

<div class="post-metadata">

**Author:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 7, 2020, 7:48pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/18 "2020-07-07T19:48:30Z")

</div>

@lassoan

I am sorry to bother you again. I copied all of the functions from the Local Threshold into the Python Interactor and created a segmentationNode around a pre-determined point. Then, I run this code:

segmentEditorWidget = slicer.modules.segmenteditor.widgetRepresentation().self().editor  
segmentEditorWidget.setActiveEffectByName(“Local Threshold”)  
effect.setParameter(“Minimum Threshold”, 265)  
effect.setParameter(“Maximum Threshold”, 1009)  
effect = segmentEditorWidget.activeEffect()  
ijkPoints = vtk.vtkPoints()  
ijkPoints.InsertNextPoint(world[0], world[1], world[2]) ##world has the coordinates of the point  
effect.self().apply(ijkPoints)

This gives me an error saying that “None Type” has no attribute self. I feel like we have the necessary elements to the code(many thanks to your GitHub resources), but I just cannot get the code to actually work when run. 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:** [July 7, 2020, 7:59pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/19 "2020-07-07T19:59:16Z")

</div>

You can only select an effect if you selected a master volume and created a segment. See the [examples](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_run_segment_editor_effects_from_a_script).

Also note that the order of operations need to be fixed (`effect = ` comes first) and that `world` coordinate is physical (RAS) coordinate and not `IJK`. You need to convert to `RAS` to `IJK` before inserting the point coordinates into `ijkPoints`.

---

<div class="post-metadata">

**Author:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 8, 2020, 1:08pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/20 "2020-07-08T13:08:46Z")

</div>

Thank you! I found out how to convert the RAS coordinates to IJK coordinates, but we are still having trouble with the effect line:

segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()  
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)  
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()  
slicer.mrmlScene.AddNode(segmentEditorNode)  
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)  
segmentEditorWidget.setSegmentationNode(segmentationNode)  
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

segmentEditorWidget = slicer.modules.segmenteditor.widgetRepresentation().self().editor  
**segmentEditorWidget.setActiveEffectByName(“Local Threshold”)**  
effect = segmentEditorWidget.activeEffect()  
effect.self().apply(fidIJK)

The setActiveEffectByName line(and lines where we setParameters after it) still have errors regarding attributes and identifiers. Do you have any idea why @lassoan?

---

<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:** [July 8, 2020, 1:11pm UTC](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393/21 "2020-07-08T13:11:38Z")

</div>

Can you send the link to your module’s source code?

[Next page](https://discourse.slicer.org/t/python-editing-feature-size-local-threshold/12393.md?page=2)
