# Unexpected rigid transformation when called from python

**URL:** https://discourse.slicer.org/t/unexpected-rigid-transformation-when-called-from-python/31168
**Category:** Support
**Tags:** registration, python
**Created:** [August 16, 2023, 9:53am UTC](https://discourse.slicer.org/t/unexpected-rigid-transformation-when-called-from-python/31168 "2023-08-16T09:53:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![stephan13](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@stephan13](https://discourse.slicer.org/u/stephan13)
#### Post date: [August 16, 2023, 9:53am UTC](https://discourse.slicer.org/t/unexpected-rigid-transformation-when-called-from-python/31168/1 "2023-08-16T09:53:31Z")

</div>

Hi there,

if I rigidly register two volumes within Slicer using the Elastix module, everything is as expected. However, when called from Python, I encountered unexpected behavior. The code worked using Slicer 4.11 and Elastix 5.0.1, the issues appeared using Slicer 5.2.2 and Elastix 5.1.0:

1. transform matrix is not shown in transform module  
expected:  
 ![expected_transform](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/4/6442947c012de2c76789cc30a57c37574f8a2dd3.png)  
got:  
 ![got_transform](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/3/13aea87916a7751bbf3d789dd3dee6fdcd6b6957.png)

2. When exporting the resulting transformation to file, the .tfm file is gigantic with \>700MB, normally it’s just the transformation matrix and a few KB in size.

Here’s the relevant python code:

```auto
 elastixLogic = Elastix.ElastixLogic()
 # create result transform
 slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTransformNode', result_transform_name)
 # register
 elastixLogic.registerVolumes(get_node_by_name(slicer, fixed_name),
                           get_node_by_name(slicer, moving_name),
                           parameterFilenames=[path_params_rigid],
                           outputTransformNode=get_node_by_name(slicer,
                                                                result_transform_name))

```

get\_node\_by\_name() just calls slicer.util.getNode()

I checked the rigid parameter file, it correctly uses a local copy of Parameters\_Rigid.txt from the  
https://github.com/lassoan/SlicerElastix/blob/master/Elastix/Resources/RegistrationParameters/Parameters\_Rigid.txt repo, which is consistent with the file in AppData (path see below).

As a reference also the Elastix output when called manually via the Elastix module UI within Slicer:

```auto
which elastix: C:\Users\steph\AppData\Local\NA-MIC\Slicer 5.2.2\NA-MIC\Extensions-31382\SlicerElastix\lib\Slicer-5.2\elastix.exe
  elastix version: 5.1.0
  Git revision SHA: d652938573e5f193955908eba225a854b31ce36a
  Git revision date: Thu Jan 12 14:20:18 2023 +0100
  Build date: Apr 25 2023 06:37:51
  Compiler: Visual C++ version 193532217.1
  Memory address size: 64-bit
  CMake version: 3.22.1
  ITK version: 5.3.0

Command-line arguments:
  -f C:/Users/steph/AppData/Local/Temp/Slicer/Elastix/20230816_103312_020\input\fixed.mha 
-m C:/Users/steph/AppData/Local/Temp/Slicer/Elastix/20230816_103312_020\input\moving.mha 
-out C:/Users/steph/AppData/Local/Temp/Slicer/Elastix/20230816_103312_020\result-transform 
-p "C:\Users\steph\AppData\Local\NA-MIC\Slicer 5.2.2\NA-MIC\Extensions-31382\SlicerElastix\lib\Slicer-5.2\qt-scripted-modules\Resources\RegistrationParameters\Parameters_Rigid.txt"

```

What could be the issue here? Note that the python generated transformation is still fine, I can apply and correctly transform volumes with it.

Thanks!

---

<div class="post-metadata">

### Author: ![stephan13](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@stephan13](https://discourse.slicer.org/u/stephan13)
#### Post date: [September 27, 2023, 6:30am UTC](https://discourse.slicer.org/t/unexpected-rigid-transformation-when-called-from-python/31168/2 "2023-09-27T06:30:05Z")

</div>

any update on this post?

---

<div class="post-metadata">

### Author: ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)
#### Post date: [September 27, 2023, 6:37pm UTC](https://discourse.slicer.org/t/unexpected-rigid-transformation-when-called-from-python/31168/3 "2023-09-27T18:37:17Z")

</div>

I know that the Elastix module code changed since 5.0.1 because some of my module code which was using it broke in 5.2.2 (I haven’t figured out how to fix it yet, so I just know there was a breaking change). As a simple first attempt, I would suggest that you try forcing the result transform to be linear and see if that works:

```auto
slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLinearTransformNode', result_transform_name)

```

Perhaps the Elastix module is now detecting the type of the transform node, and if it is a general transform node it returns a displacement field transform rather than the previously expected linear matrix. If I recall correctly, the displacement field transform is the default output of Elastix registration, and there was some code in the Elastix module which tried to detect the special case where a linear transformation matrix could be returned instead and converted it. Perhaps that check is now only run if the output transformation node type seems to call for it.

---

<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: [September 28, 2023, 3:03am UTC](https://discourse.slicer.org/t/unexpected-rigid-transformation-when-called-from-python/31168/4 "2023-09-28T03:03:24Z")

</div>

> [@stephan13](#):
>
> ```auto
> elastixLogic = Elastix.ElastixLogic()
> # create result transform
> slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTransformNode', result_transform_name)
> # register
> elastixLogic.registerVolumes(get_node_by_name(slicer, fixed_name),
> get_node_by_name(slicer, moving_name),
> parameterFilenames=[path_params_rigid],
> outputTransformNode=get_node_by_name(slicer,
> result_transform_name))
> 
> ```

By default, displacement field is requested as registration output. Probably this choice was kept for backward compatibility (for a good while Elastix was not able to write ITK transforms and so it was safer and simpler to get a displacement field).

If you want to get a linear transform then you can add the `forceDisplacementFieldOutputTransform=False` argument to the `registerVolumes` call.

---

<div class="post-metadata">

### Author: ![stephan13](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@stephan13](https://discourse.slicer.org/u/stephan13)
#### Post date: [October 12, 2023, 1:34pm UTC](https://discourse.slicer.org/t/unexpected-rigid-transformation-when-called-from-python/31168/5 "2023-10-12T13:34:59Z")

</div>

Perfect thanks! Adding the flag solved the issue 👌
