# Assessing average displacement resulting from a linear transform

**URL:** <https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318>\
**Category:** SlicerDMRI\
**Created:** [November 27, 2019, 10:00pm UTC](https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318 "2019-11-27T22:00:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DTI](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DTI](https://discourse.slicer.org/u/DTI)\
**Post date:** [November 27, 2019, 10:00pm UTC](https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318/1 "2019-11-27T22:00:17Z")

</div>

Hi,

Is there a way in Slicer to calculate the mean DTI distortion (e.g. as mean transform needed to align DTI to T2w, like the root mean square deviation).

I found this:

[https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5118068/#R34](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5118068/#R34) -\>

## From the paper:

"We also calculated the root mean square (RMS) deviation for each subject, which combines the six rigid body parameters into a single estimator of mean displacement [Jenkinson, 1999; Reuter et al., 2015]:

RMS = sqrt{r∗r ∕ 5 + tr[(M−I)T(M − I)] + tTt},

## where t is the translation vector, M is (here) the 3×3 rotation matrix, I is the identity matrix, tr[] is the trace operator, and r is the approximate spherical radius of the brain (here, estimated from the data to be 65 mm). The rigid body parameters and RMS values of the navigator logs were also examined."

I have a slicer transform file (.h5) for each of my subjects and wonder if there is a way to calculate the RMS (or a similar measure) from them.

Thanks, Lorenz

---

<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 27, 2019, 11:49pm UTC](https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318/2 "2019-11-27T23:49:04Z")

</div>

This formula in [Taylor2016](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5118068/) incorrect (since it would give non-zero value for an identity matrix), don’t use this:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/6/c66a189e5f6abc8d86b64fa56828a0de4a5f1ee7.png)

The formula in [Reuter2015](https://www.ncbi.nlm.nih.gov/pubmed/25498430/) (the paper that Taylor2016 cites) looks correct:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/d/7df1c65efed6e4c23504f8eb03e2b8c73d951f31.png)

You can get the value in Slicer by copy-pasting the following code into the Python console:

```python
transformNode = getNode('MyLinearTransform')
r = 15

import numpy as np
import math
transformMatrix = slicer.util.arrayFromTransformMatrix(transformNode)
t = transformMatrix[0:3,3]
M = transformMatrix[0:3,0:3]
RMS = math.sqrt(r * r / 5.0 * np.trace( (M-np.eye(3)).T @ (M-np.eye(3)) ) + np.dot(t,t))
print(RMS)

```

---

<div class="post-metadata">

**Author:** ![DTI](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DTI](https://discourse.slicer.org/u/DTI)\
**Post date:** [November 28, 2019, 6:16am UTC](https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318/3 "2019-11-28T06:16:39Z")

</div>

Thank you very much! This is very helpful!

I tried copy-pasted the code and filled in the name of my transform. I get the following error. How can I fix this?

```auto
>>> transformNode = getNode('003_transform')
>>> r = 15
>>> import numpy as np
>>> import math
>>> transformMatrix = slicer.util.arrayFromTransformMatrix(transformNode)
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'arrayFromTransformMatrix'

```

Thanks again,  
Lorenz

---

<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 28, 2019, 12:27pm UTC](https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318/4 "2019-11-28T12:27:27Z")

</div>

This script works in recent Slicer Preview Releases. If you need to use latest Slicer Stable Release (4.10.2) then you can copy the slicer.util.arrayFromTransformMatrix method from [here](https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/util.py).

---

<div class="post-metadata">

**Author:** ![DTI](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DTI](https://discourse.slicer.org/u/DTI)\
**Post date:** [November 28, 2019, 2:00pm UTC](https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318/5 "2019-11-28T14:00:46Z")

</div>

It used it in the newest nightly release and it worked. Thank you very much again! I appreciate it a lot. - Lorenz

---

<div class="post-metadata">

**Author:** ![DTI](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DTI](https://discourse.slicer.org/u/DTI)\
**Post date:** [March 24, 2020, 7:58pm UTC](https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318/6 "2020-03-24T19:58:20Z")

</div>

Hi, one last question regarding the formula as I am writing this up: what does T stand for? Havent found a definition in those papers. Thanks, Lorenz

---

<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:** [March 24, 2020, 8:14pm UTC](https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318/7 "2020-03-24T20:14:34Z")

</div>

`matrix.T` it means transpose of `matrix`.

---

<div class="post-metadata">

**Author:** ![DTI](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DTI](https://discourse.slicer.org/u/DTI)\
**Post date:** [April 1, 2020, 1:01pm UTC](https://discourse.slicer.org/t/assessing-average-displacement-resulting-from-a-linear-transform/9318/8 "2020-04-01T13:01:17Z")

</div>

Perfect. Thank you very much!
