# Prevent transformix running in Slicer Elastix

**URL:** <https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902>\
**Category:** Support\
**Created:** [August 6, 2019, 9:09pm UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902 "2019-08-06T21:09:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prashant\_Pandey](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@Prashant\_Pandey](https://discourse.slicer.org/u/Prashant_Pandey)\
**Post date:** [August 6, 2019, 9:09pm UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/1 "2019-08-06T21:09:10Z")

</div>

Using Slicer 4.10.2

I am writing a runtime sensitive registration module which makes use of the slicer elastix module. As you can imagine elastix registration takes some time to run (about ~2 minutes for the data and parameters I am using). However transformix also runs after every run of elastix, and this takes an additional 30 seconds before the process is finished. How can I prevent transformix from running? I did not specify an output volume in Elastix, and my parameter file also specifies not to write an output image `(WriteResultImage "false")`.

---

<div class="post-metadata">

**Author:** ![Prashant\_Pandey](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@Prashant\_Pandey](https://discourse.slicer.org/u/Prashant_Pandey)\
**Post date:** [August 6, 2019, 9:49pm UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/2 "2019-08-06T21:49:53Z")

</div>

_Maybe_ this is where the issue is:

> <https://github.com/lassoan/SlicerElastix/blob/9ded00135fd8a3d5da3ecedd3a68320e40d74d38/Elastix/Elastix.py#L584-L594>

Since transformix’s purpose is to generate the output transformed volume, perhaps it should only be called when outputVolumeNode exists instead of being called every time?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 6, 2019, 10:10pm UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/3 "2019-08-06T22:10:13Z")

</div>

Yes, that makes sense - would you be able to add that option and provide a pull request?

---

<div class="post-metadata">

**Author:** ![Prashant\_Pandey](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@Prashant\_Pandey](https://discourse.slicer.org/u/Prashant_Pandey)\
**Post date:** [August 6, 2019, 11:21pm UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/4 "2019-08-06T23:21:15Z")

</div>

Sure, I’ll do that by this week!

---

<div class="post-metadata">

**Author:** ![Prashant\_Pandey](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@Prashant\_Pandey](https://discourse.slicer.org/u/Prashant_Pandey)\
**Post date:** [August 7, 2019, 3:52am UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/5 "2019-08-07T03:52:03Z")

</div>

Here is the pull request: [https://github.com/lassoan/SlicerElastix/pull/19](https://github.com/lassoan/SlicerElastix/pull/19)

---

<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:** [August 7, 2019, 4:18am UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/6 "2019-08-07T04:18:29Z")

</div>

Transformix is always necessary now because we need to generate a resampled moving volume and/or a displacement field. Both are provided by Transformix.

We could avoid the need to run Transformix if we could read the proprietary Elastix transformation file format. I’ve started implementing a reader for linear transforms, but did not finish it. I include it below - it would be great if you could finish it and integrate it into SlicerElastix module. We should probably implement bspline reader, too.

```python
# Example input:
transformStr="(TransformParameters 0.022507 0.013835 0.013726 7.760838 4.879223 -0.014589)"

transformParams = transformStr.strip("()").split(" ")
transformParams.pop(0)
transformParams = [float(x) for x in transformParams]
(roll, pitch, yaw) = np.array(transformParams)[0:3]
computeZYX=False

[rx,ry,rz]=transformParams[0:3]
[tx,ty,tz]=transformParams[3:6]

from math import sin, cos
rotX = np.array([[1.0, 0.0, 0.0], [0.0, cos(rx), -sin(rx)], [0.0, sin(rx), cos(rx)]])
rotY = np.array([[cos(ry), 0.0, sin(ry)], [0.0, 1.0, 0.0], [-sin(ry), 0.0, cos(ry)]])
rotZ = np.array([[cos(rz), -sin(rz), 0.0], [sin(rz), cos(rz), 0.0], [0.0, 0.0, 1.0]])

if computeZYX:
    # Aply the rotation first around Y then X then Z
    fixedToMovingDirection = np.dot(np.dot(rotZ, rotY), rotX)
else:
    # Like VTK transformation order
    fixedToMovingDirection = np.dot(np.dot(rotZ, rotX), rotY)

fixedToMoving = np.eye(4)
fixedToMoving[0:3,0:3] = fixedToMovingDirection
fixedToMoving[0:3,3] = transformParams[3:6]

# Create Slicer linear transform ("to parent" direction, in RAS)
ras2lps = np.array([[-1,0,0,0],[0,-1,0,0],[0,0,1,0],[0,0,0,1]])
movingToFixed = np.dot(np.dot(ras2lps, np.linalg.inv(fixedToMoving)), ras2lps)
slicer.util.updateTransformMatrixFromArray(getNode('Transform_1'),movingToFixed)
```

---

<div class="post-metadata">

**Author:** ![Prashant\_Pandey](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@Prashant\_Pandey](https://discourse.slicer.org/u/Prashant_Pandey)\
**Post date:** [August 7, 2019, 4:34am UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/7 "2019-08-07T04:34:11Z")

</div>

I should have made clear that, in my pull request, transformix is still run but with limited parameters so that only the deformation field is written and not a new output image (unless required by the user). This saves a little of time overall.

I will take a look at your code for the linear transform conversion to see if i can contribute!

---

<div class="post-metadata">

**Author:** ![Prashant\_Pandey](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@Prashant\_Pandey](https://discourse.slicer.org/u/Prashant_Pandey)\
**Post date:** [August 7, 2019, 6:20pm UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/8 "2019-08-07T18:20:59Z")

</div>

Have you confirmed that this is the correct transformation from the Euler angles to a linear transform? If so, is the only thing left to read the transform text file and write the transform to Slicer?

---

<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:** [August 7, 2019, 7:54pm UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/9 "2019-08-07T19:54:46Z")

</div>

Yes, the computation should be correct and the remaining task is to iterate through the transform files and apply this parser on them.

---

<div class="post-metadata">

**Author:** ![Prashant\_Pandey](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@Prashant\_Pandey](https://discourse.slicer.org/u/Prashant_Pandey)\
**Post date:** [August 8, 2019, 1:23am UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/10 "2019-08-08T01:23:05Z")

</div>

Here is the pull request for parsing linear transformations: [https://github.com/lassoan/SlicerElastix/pull/21](https://github.com/lassoan/SlicerElastix/pull/21)

I included support for translation only registrations too.

---

<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:** [August 10, 2019, 12:49pm UTC](https://discourse.slicer.org/t/prevent-transformix-running-in-slicer-elastix/7902/11 "2019-08-10T12:49:04Z")

</div>

Thank you. I’ve merged this and added reading of bspline transforms, too.
