# Root Mean Square Surface Distance - Mauerer Distances Map. how to calculate it?

**URL:** <https://discourse.itk.org/t/root-mean-square-surface-distance-mauerer-distances-map-how-to-calculate-it/424>\
**Category:** Beginner Questions\
**Tags:** dicom, simpleitk, python\
**Created:** [November 15, 2017, 1:14pm UTC](https://discourse.itk.org/t/root-mean-square-surface-distance-mauerer-distances-map-how-to-calculate-it/424 "2017-11-15T13:14:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ral\_san](https://discourse.itk.org/user_avatar/discourse.itk.org/ral_san/32/236_2.png) [@ral\_san](https://discourse.itk.org/u/ral_san)\
**Post date:** [November 15, 2017, 1:14pm UTC](https://discourse.itk.org/t/root-mean-square-surface-distance-mauerer-distances-map-how-to-calculate-it/424/1 "2017-11-15T13:14:34Z")

</div>

Below it’s my code for trying to calculate the symmetric root mean square distance. But I am not getting the right values.  
I can’t figure out where is the mistake in my approach.

```
segmented_surface_mask = sitk.LabelContour(segmentation)
segmented_surface_ref = sitk.LabelContour(reference_segmentation)

# init signed mauerer distance as reference metrics
self.reference_distance_map = sitk.Abs(sitk.SignedMaurerDistanceMap(reference_segmentation, squaredDistance=False, useImageSpacing=True))
self.mask_distance_map = sitk.Abs(sitk.SignedMaurerDistanceMap(segmentation, squaredDistance=False, useImageSpacing=True))

# init label_1 intensity statistics filter
label_intensity_statistics_filter = sitk.LabelIntensityStatisticsImageFilter()
label_intensity_statistics_filter.Execute(segmented_surface_mask, self.reference_distance_map)
label_1 =255
self.n1 = label_intensity_statistics_filter.GetNumberOfPixels(label_1)
label_intensity_statistics_filter.Execute(segmented_surface_ref, self.mask_distance_map)
self.n2 = label_intensity_statistics_filter.GetNumberOfPixels(label_1)

 def get_rms_symmetric_dist(self):      
        '''compute the root mean square distance'''
        
        # square the Mauerer distances
        mask_distance_map_float = sitk.GetArrayFromImage(self.mask_distance_map)
        mask_distance_map_float = np.flip(mask_distance_map_float ,2)
        mask_distance_map_squared = mask_distance_map_float ** 2
        
        reference_distance_map_float = sitk.GetArrayFromImage(self.reference_distance_map)
        reference_distance_map_float = np.flip(reference_distance_map_float,2)
        reference_distance_map_squared = reference_distance_map_float ** 2
        
        # sum the distances
        mask_distance_map_sum = mask_distance_map_squared.sum()
        reference_distance_map_sum = reference_distance_map_squared.sum()
        
        return np.sqrt(1. / (self.n1 + self.n2)) * np.sqrt(mask_distance_map_sum + reference_distance_map_sum)
```

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [November 15, 2017, 6:15pm UTC](https://discourse.itk.org/t/root-mean-square-surface-distance-mauerer-distances-map-how-to-calculate-it/424/2 "2017-11-15T18:15:19Z")

</div>

@ral_san  
Looking at the get\_rms\_symmetric\_dist function:

1. Why do you flip the numpy array axis?
2. You don’t seem to take into account the surfaces, it looks like you are working with the original distance maps.

I have updated the notebook which I am guessing you started with to use symmetric surface distances, see [here](https://github.com/zivy/SimpleITK-Notebooks/blob/segEvalUpdate/Python/34_Segmentation_Evaluation.ipynb). It made the code slightly more complex, but not too bad. You should be able to base your code on that example.

Let us know if you encounter any issues.
