# Plastimatch and Python Interactor

**URL:** https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556
**Category:** Support
**Created:** [May 15, 2020, 1:47pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556 "2020-05-15T13:47:25Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.slicer.org/u/user1)
#### Post date: [May 15, 2020, 1:47pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/1 "2020-05-15T13:47:25Z")

</div>

Hello Community,

I have a (maybe easy) basic question to the Python Interactor in 3D Slicer/ SlicerRT. I want to know, which kind of the average Hausdorff-Distance the SlicerRT extension calculate (e.g. directed average or undirected average or undirected max average Hausdorff-distance).  
Therefore I try to calculate the HD with the Python Interactor in 3D Slicer with the commands of Plastimatch (and compare with my manual calculation), but it doesn’t work. May somebody show me, how can I use the Plastimatch-commands in the Interactor (e.g. get\_avg\_average\_hausdorff)?

Thank you very much!

---

<div class="post-metadata">

### Author: ![gcsharp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gcsharp/32/863_2.png) [@gcsharp](https://discourse.slicer.org/u/gcsharp)
#### Post date: [May 15, 2020, 2:32pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/2 "2020-05-15T14:32:57Z")

</div>

Unfortunately you can’t call plastimatch functions from python. Those functions are not python wrapped. But you can see which functions are called here:

> <https://github.com/SlicerRt/SlicerRT/blob/master/SegmentComparison/Logic/vtkSlicerSegmentComparisonModuleLogic.cxx#L471>

---

<div class="post-metadata">

### Author: ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.slicer.org/u/user1)
#### Post date: [May 15, 2020, 3:01pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/3 "2020-05-15T15:01:01Z")

</div>

Thanks for the quick answer.

So did I understood it right, that the average HD in Slicer represent the sum of all minimum distances from the **external contour** -pixels/ voxels of one contour/ segment to the pixel/ voxel of the external border of the other contour divided by the number of pixels from the **first** contour (or sum of all pixels of both contours?)? = directed average HD  
After that the same steps are repeated for the other contour and the sum of both (directed) distances divided by 2 or did Slicer take the maximum of the two directed distances? = undirected average HD

Thanks in advance!

---

<div class="post-metadata">

### Author: ![gcsharp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gcsharp/32/863_2.png) [@gcsharp](https://discourse.slicer.org/u/gcsharp)
#### Post date: [May 15, 2020, 7:45pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/4 "2020-05-15T19:45:30Z")

</div>

It works like this:

1. For each boundary point of the compare image, find the closest boundary point on the reference image. Then take the average of these distances.
2. For each boundary point of the reference image, find the closest boundary point on the compare image. Then take the average of these distances.
3. Take the average of the numbers computed in step 1 and step 2.

---

<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: [May 16, 2020, 4:26am UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/5 "2020-05-16T04:26:37Z")

</div>

> [@gcsharp](#):
>
> Unfortunately you can’t call plastimatch functions from python

All computations that Slicer can perform are accessible from Python, too.

For example, you can compute metric by calling [SegmentComparison module logic](https://github.com/SlicerRt/SlicerRT/blob/master/SegmentComparison/Logic/vtkSlicerSegmentComparisonModuleLogic.h#L48-L54) and retrieve results from the associated [vtkMRMLSegmentComparisonNode](https://github.com/SlicerRt/SlicerRT/blob/master/SegmentComparison/Logic/vtkMRMLSegmentComparisonNode.h).

---

<div class="post-metadata">

### Author: ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.slicer.org/u/user1)
#### Post date: [May 16, 2020, 8:07am UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/6 "2020-05-16T08:07:50Z")

</div>

Thanks for the information.

Unfortunately I can’t call these SegmentComparison module logic’s (“std::string ComputeHausdorffDistances(vtkMRMLSegmentComparisonNode\* parameterNode);”) in the Python-Interactor. First I included the “slicer includes”, after that I created a segmentation from a LabelMap-volume (created in segmentations-module) as you showed in a thread before: “[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)” (is the “parameterNode” == this segmentation?).

May you can give me an example how I can input the syntax for this, please?

Thank you!

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [May 17, 2020, 10:26am UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/7 "2020-05-17T10:26:39Z")

</div>

Please note the signature of the function. It takes a vtkMRMLSegmentComparisonNode, which is a parameter set node containing the parameters of a segment comparison run. Now if you look at this class

> <https://github.com/SlicerRt/SlicerRT/blob/master/SegmentComparison/Logic/vtkMRMLSegmentComparisonNode.h#L58-L176>

  
then you’ll see what inputs you can set to it and what outputs you gan get after the calculation.

---

<div class="post-metadata">

### Author: ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.slicer.org/u/user1)
#### Post date: [May 19, 2020, 12:25pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/8 "2020-05-19T12:25:38Z")

</div>

Thanks for the help! Sorry, that I have to ask again to this…

First, I get every time an error message, when I want to get my reference segmentation node (segmentation\* GetReferenceSegmentationNode();). Do I understood it right, that my “vtkMRMLSegmentationNode” = my active Segmentation (which contains the two segments for calculation of Hausdorff/ Dice…)?  
Furthermore I get the error message, that the name “SetAndObserveReferenceSegmentationNode” is not defined, when I want to set my reference segmentation node…

May you show me my mistake or are there any special packages necessary, which I have to include/ install/ import?

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [May 19, 2020, 1:07pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/9 "2020-05-19T13:07:05Z")

</div>

This function from an actual working module should help:

> <https://github.com/SlicerRt/SegmentRegistration/blob/master/ProstateMRIUSContourPropagation/ProstateMRIUSContourPropagation.py#L1085-L1126>

---

<div class="post-metadata">

### Author: ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.slicer.org/u/user1)
#### Post date: [May 19, 2020, 3:57pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/10 "2020-05-19T15:57:21Z")

</div>

OK, I think I got it, thank you very much!

I just have one more question to the segmentation node. May you explain me, what the “usSegmentationNode” or “mrSegmentationNode” is in your code:  
self.segmentComparisonNode.SetAndObserveReferenceSegmentationNode(self.usSegmentationNode)  
I thought this are the segments in the active segmentation…

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [May 20, 2020, 9:21am UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/11 "2020-05-20T09:21:29Z")

</div>

This particular module compares segments from two different segmentations, one done on ultrasound (US) and the other on MR.

---

<div class="post-metadata">

### Author: ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.slicer.org/u/user1)
#### Post date: [May 20, 2020, 9:41am UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/12 "2020-05-20T09:41:45Z")

</div>

ok, I mean what is the input of this module.  
So how I get a “usSegmentationNode” and a “mrSegmentationNode”?  
Do I need two different segmentations or is it enough to have two segments to compare in one segmentation?

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [May 20, 2020, 10:09am UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/13 "2020-05-20T10:09:33Z")

</div>

You can find information about this module here

> **[GitHub - SlicerRt/SegmentRegistration: 3D Slicer extension for segment...](https://github.com/SlicerRt/SegmentRegistration)**
>
> 3D Slicer extension for segment registration (aka fusion, contour propagation) - GitHub - SlicerRt/SegmentRegistration: 3D Slicer extension for segment registration (aka fusion, contour propagation)

There is a tutorial as well that shows you how it is used.

> [@user1](#):
>
> Do I need two different segmentations or is it enough to have two segments to compare in one segmentation?

You can compare any two segments.

---

<div class="post-metadata">

### Author: ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.slicer.org/u/user1)
#### Post date: [May 20, 2020, 12:21pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/14 "2020-05-20T12:21:19Z")

</div>

Ah ok it is this module. Is there any way to compute the Hausdorff-distances with the “Segment Comparison” - module in the python interactor as you can see here:

 ![Bildschirmfoto 2020-05-20 um 14.11.04](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/4/f4d4f3e4840ae9bd2f9933bf17f7c007d8696fce.png)

I want to calculate the Hausdorff-distances for volume and not for boundary which is the default one in SlicerRT (in my example segment it would be probably the same as boundary HD, but I want to use the volume HD for an other case). Therefore I tried to do this with the python-interactor…

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [May 20, 2020, 12:55pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/15 "2020-05-20T12:55:35Z")

</div>

You have all the information for this above. Please take a look at the settings vtkMRMLSegmentComparisonNode offers.

---

<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: [May 20, 2020, 2:12pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/16 "2020-05-20T14:12:26Z")

</div>

> [@user1](#):
>
> I want to calculate the Hausdorff-distances for volume and not for boundary

By definition, Hausdorff distance is between boundary surfaces, so you have to provide them as inputs. Even if you think that the boundary is a trivial one, such as bone surface or skin surface, you still need to specify it (for example, by creating a segment with thresholding).

---

<div class="post-metadata">

### Author: ![gcsharp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gcsharp/32/863_2.png) [@gcsharp](https://discourse.slicer.org/u/gcsharp)
#### Post date: [May 20, 2020, 4:16pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/17 "2020-05-20T16:16:31Z")

</div>

> [@lassoan](#):
>
> By definition, Hausdorff distance is between boundary surfaces

As I understand, Hausdorff distance is defined between two sets. Surface boundary is most common, but it is not the only choice.

---

<div class="post-metadata">

### Author: ![Spiros\_Karkavitsas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/spiros_karkavitsas/32/18224_2.png) [@Spiros\_Karkavitsas](https://discourse.slicer.org/u/Spiros_Karkavitsas)
#### Post date: [February 24, 2023, 2:23pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/18 "2023-02-24T14:23:43Z")

</div>

Hello everyone

I came across your discussion since I have a relative easy question to ask: I dowloaded the Plastimatch in 3D slicer and I would like to ask how I can use it to compare segmentations using the mean surface distance ?

I was trying to find a script online but I could not find any script which I can trust totally…

Thank you for your time reading my message:)

---

<div class="post-metadata">

### Author: ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)
#### Post date: [February 24, 2023, 3:18pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/19 "2023-02-24T15:18:44Z")

</div>

> [@Spiros\_Karkavitsas](#):
>
> dowloaded the Plastimatch in 3D slicer

I assume this means that you installed the SlicerRT extension. I suggest you use the Segment Comparison module and use the mean Hausdorff metric to achieve the same result.

---

<div class="post-metadata">

### Author: ![Spiros\_Karkavitsas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/spiros_karkavitsas/32/18224_2.png) [@Spiros\_Karkavitsas](https://discourse.slicer.org/u/Spiros_Karkavitsas)
#### Post date: [February 24, 2023, 3:40pm UTC](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556/20 "2023-02-24T15:40:46Z")

</div>

Hello and thanks for your answer.

As you said,yes I installed the extension in Slicer.

So regarding your answer, why the mean Hausdorff distance is the same as the mean surface distance ? The answer might be obvious but since I have not searched it yet,that is why I am asking.🙂

Στις Παρ, 24 Φεβ 2023, 16:18 ο χρήστης Csaba Pinter via 3D Slicer Community \<[notifications@slicer.discoursemail.com](mailto:notifications@slicer.discoursemail.com)\> έγραψε:

[Next page](https://discourse.slicer.org/t/plastimatch-and-python-interactor/11556.md?page=2)
