# Extracting Exact Metric Value in SimpleITK Python

**URL:** https://discourse.itk.org/t/extracting-exact-metric-value-in-simpleitk-python/2423
**Category:** Beginner Questions
**Tags:** python, simpleitk
**Created:** [November 19, 2019, 3:43pm UTC](https://discourse.itk.org/t/extracting-exact-metric-value-in-simpleitk-python/2423 "2019-11-19T15:43:19Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jhess](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@jhess](https://discourse.itk.org/u/jhess)
#### Post date: [November 19, 2019, 3:43pm UTC](https://discourse.itk.org/t/extracting-exact-metric-value-in-simpleitk-python/2423/1 "2019-11-19T15:43:19Z")

</div>

Hi there,

I am using Elastix in command line to register images using Mattes mutual information metric, and at the end of the registration process, I am recording the exact metric value between the fixed and moving image, a functionality introduced in Elastix 4.9.0. I am attempting to compute the same value with simpleITK in python, and I am not able to reproduce the result that I am getting through elastix. I would appreciate any help on the topic.

Here is my python code and attached is the transform parameter file used in Elastix:

import simpleitk as sitk  
pixelType = sitk.sitkFloat64  
fixedImage = sitk.ReadImage(’/path/fixed\_image.nii’, pixelType)  
movingImage = sitk.ReadImage(’/path/moving\_image.nii’, pixelType)  
Reg = sitk.ImageRegistrationMethod()  
Reg.SetOptimizerAsGradientDescentLineSearch(learningRate=1.0,numberOfIterations=1,convergenceMinimumValue=1e-5,convergenceWindowSize=5)  
Reg.SetMetricSamplingPercentage(100.0)  
Reg.SetInitialTransform(sitk.Transform(2,sitk.sitkIdentity))  
Reg.SetMetricAsMattesMutualInformation(numberOfHistogramBins = 32)

Reg.MetricEvaluate(fixedImage,movingImage)

[Affine\_Double.txt](https://discourse.itk.org/uploads/short-url/ucy8fVjmUQTfeNzcn7PhbFtYpht.txt) (1.8 KB)

Josh

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 19, 2019, 3:54pm UTC](https://discourse.itk.org/t/extracting-exact-metric-value-in-simpleitk-python/2423/2 "2019-11-19T15:54:04Z")

</div>

`NumberOfSpatialSamples 8000` is a far cry from 100% sampling. There are a lot of other parameters in that file, and you are explicitly setting only a subset. And how is the metric value different?

@Niels_Dekker might have more advice.

---

<div class="post-metadata">

### Author: ![jhess](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@jhess](https://discourse.itk.org/u/jhess)
#### Post date: [November 19, 2019, 4:01pm UTC](https://discourse.itk.org/t/extracting-exact-metric-value-in-simpleitk-python/2423/3 "2019-11-19T16:01:28Z")

</div>

Thank you for the quick response. The exact metric value recorded in the resulting elastix.log is -0.595666 between the fused moving image and fixed image, and the computed value is -0.59268 in python. Those are close, but not quite the same, and I am afraid to move forward without being able to recapitulate the elastix results. 8000 spatial samples is certainly different than 100% sampling, but I was under the impression that computing the exact metric in elastix was performed by sampling all locations, which is why I chose 100% with simpleITK.

Thanks for the help!

---

<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 19, 2019, 4:13pm UTC](https://discourse.itk.org/t/extracting-exact-metric-value-in-simpleitk-python/2423/4 "2019-11-19T16:13:13Z")

</div>

Hi @jhess,

Some additional things to be aware of:

1. Setting the random sampling to 100% doesn’t sample all voxels. This is just a way to specify the number of samples (repeated samples will occur).
2. The random sampling in ITK does not take the actual voxel center, there is a jitter factor inside each voxel, so each time the sample value from the same voxel will be slightly different.
3. To use the actual voxel values for 100% of the voxels don’t sample

```auto
Reg.SetMetricSamplingStrategy(Reg.NONE)

```

---

<div class="post-metadata">

### Author: ![jhess](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@jhess](https://discourse.itk.org/u/jhess)
#### Post date: [November 19, 2019, 4:41pm UTC](https://discourse.itk.org/t/extracting-exact-metric-value-in-simpleitk-python/2423/5 "2019-11-19T16:41:21Z")

</div>

Hi @zivy and @dzenanz,

Using the advice given (setting Reg.SetMetricSamplingStrategy(Reg.NONE)), I am still receiving a metric value of -0.59268. Happy to hear any other suggestions on how to reproduce the elastix exact metric.

Thanks again.

Josh

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [November 19, 2019, 4:52pm UTC](https://discourse.itk.org/t/extracting-exact-metric-value-in-simpleitk-python/2423/6 "2019-11-19T16:52:23Z")

</div>

Perhaps there is Gaussian smoothing going on in Elastix?

---

<div class="post-metadata">

### Author: ![jhess](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@jhess](https://discourse.itk.org/u/jhess)
#### Post date: [November 19, 2019, 5:57pm UTC](https://discourse.itk.org/t/extracting-exact-metric-value-in-simpleitk-python/2423/7 "2019-11-19T17:57:47Z")

</div>

Hi @blowekamp,

I think you may be correct. In elastix the default smoothing schedule for the multi-resolution registration approach was used, and it seems that the default sigma value for the final resolution is 1. It would and need to be 0 for a fair comparison to the simpleITK implementation.

Josh
