# Tranformation of labelmap not working on script

**URL:** <https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499>\
**Category:** Development\
**Tags:** registration, transforms, labelmap\
**Created:** [September 30, 2022, 11:04am UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499 "2022-09-30T11:04:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![justomont](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/justomont/32/16814_2.png) [@justomont](https://discourse.slicer.org/u/justomont)\
**Post date:** [September 30, 2022, 11:04am UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499/1 "2022-09-30T11:04:33Z")

</div>

**Operating system:** MacOS BigSur 11.6  
**Slicer version:** 5.0.3  
**Expected behavior:** Transform labelmap volume node  
**Actual behavior:** Labelmap volume node is not transformed

Hello everyone,

I’m developing a script, that among other things, registers an MNI template onto the T1 brain scan of some patients for anatomical research purposes. For that, I just register the MNI template as the moving volume to the scan of each patient (fixed), which works wonderful. However, this MNI template also comes with a label map naming each region of the cortex, which I would like to transform too, so I could identify specific points in the brain. For that, I just apply the transformation matrix from the previous registration to the label map volume, like this:

```auto
labelmapVolumeNode = slicer.util.loadVolume(labelmapPath,properties={"name":"MNI_labels","labelmap":True,"center":True})
transform = slicer.util.getFirstNodeByName("Transform2MNI")
transformedLabels = slicer.mrmlScene.CopyNode(labelmapVolumeNode)
transformedLabels.SetName("transformed_MNI_labels")
transformedLabels.ApplyTransformMatrix(transform.GetMatrixTransformToParent())

```

Where MNI\_labels is the label map volume I would like to transform, and Transform2MNI is the transformation from the previous and successful registration. However, the code is not working. It creates the new “transformed\_MNI\_labels” Node but is just a replica of the original label map. What’s also interesting is that if I copy and paste the lines of code onto the python interactor, it works!

Finally, all the commands are in a simple function with no conditionals or structures of any kind that could prevent that part of the code from running. I’ve also checked that it was running adding logging info in between the lines. And as said, the label map is loaded and copied, the problem is that it is not transformed.

Am I missing something? Is there a better way to transform the label map?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [September 30, 2022, 2:26pm UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499/2 "2022-09-30T14:26:36Z")

</div>

Could it be that the transform is being applied, but the view is also being corrected so as to undo the effect visually? Sometimes I get thrown off by this. Like I’ll apply a transform to a volume and not see any effect, except the subtle effect that the slice views go from Axial/Sagittal/Coronal to “Reformat”.

(My comment applies to affine transforms only; not sure if that is what you have)

One way to see if that’s happening: you said that when you paste your snippet into the python interactor, it works. So after pasting it, try toggling the visibility of the successfully transformed label map. When it comes back into visibility do the slice views change to remove the visual effect of the transformation?

Alternatively: if you make the transformed labelmap visible at the same time that the transformed T1 image is visible, do they actually successfully line up?

---

<div class="post-metadata">

**Author:** ![justomont](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/justomont/32/16814_2.png) [@justomont](https://discourse.slicer.org/u/justomont)\
**Post date:** [October 3, 2022, 9:16am UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499/3 "2022-10-03T09:16:57Z")

</div>

Thank you so much for your response,

> [@ebrahim](#):
>
> Could it be that the transform is being applied, but the view is also being corrected so as to undo the effect visually? Sometimes I get thrown off by this. Like I’ll apply a transform to a volume and not see any effect, except the subtle effect that the slice views go from Axial/Sagittal/Coronal to “Reformat”.

The slice view is kept as Axial/Sagittal/Coronal, it doesn’t change to Reformat.

> [@ebrahim](#):
>
> (My comment applies to affine transforms only; not sure if that is what you have)

Yes! It is an affine transform

> [@ebrahim](#):
>
> One way to see if that’s happening: you said that when you paste your snippet into the python interactor, it works. So after pasting it, try toggling the visibility of the successfully transformed label map. When it comes back into visibility do the slice views change to remove the visual effect of the transformation?

No, the slice views are not changing after pasting the snippet in the python interactor either

> [@ebrahim](#):
>
> Alternatively: if you make the transformed labelmap visible at the same time that the transformed T1 image is visible, do they actually successfully line up?

They successfully line up **only after** running the snippet from the Python interactor. In the following image you can see the MNI template registered to the subject (ICBM152\_registered) and the labelmap after transforming it with the script:

![Captura de pantalla 2022-10-03 a las 10.38.57](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/7/0725ba124f2bd8ba6372412b3e0ffac1e64594d4.png)

where the transformed labelmap (transformed\_MNI\_labels) is clearly not transformed. However, the following figure shows in the same slice how the transformation is performed after running the snippet from the interactor:

![Captura de pantalla 2022-10-03 a las 10.39.18](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/e/2e0e90e2fd55d568dd2601722972d4774a9f5768.png)

Any other ideas on what could be causing this script issue?

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [October 3, 2022, 1:58pm UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499/4 "2022-10-03T13:58:59Z")

</div>

How are you running the script when it’s not by pasting into the python interactor?

I wonder if some of the objects (`transform`, `transformedLabels`, `labelmapVolumeNode`,…) are coming out as `None` and no error is being reported because the later funcitons do not complain when given `None`. Maybe you could print out those objects in the snippet to make sure they’re as expected

---

<div class="post-metadata">

**Author:** ![justomont](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/justomont/32/16814_2.png) [@justomont](https://discourse.slicer.org/u/justomont)\
**Post date:** [October 4, 2022, 9:25am UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499/5 "2022-10-04T09:25:55Z")

</div>

I’m running the script from a Scripted Module.

I already checked and that part of the script is running, the line that is apparently the issue is `transformedLabels.ApplyTransformMatrix(transform.GetMatrixTransformToParent())` as it is not applying the transform. I’ve also tried with `labelmapVolumeNode.ApplyTransform(transform.GetTransformToParent())` but it’s not working either.

All the objects are coming out as they are expected (`labelmapVolumeNode` and `transform`), with the exception of the `transformedLabels`, which is just the copy of `labelmapVolumeNode`, but it’s not `None`.

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [October 5, 2022, 2:42pm UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499/6 "2022-10-05T14:42:48Z")

</div>

What if you apply the transform using `SetAndObserveTransformNodeID` as [seen here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#apply-a-transform-to-a-transformable-node), then does it work from the scripted module?

I’m not sure I understand the difference between the `SetAndObserveTransformNodeID` and the `ApplyTransform` approach, but if the difference is that `ApplyTransform` hardens the transform and this is something you want then you can call `HardenTransform` on the segmentation node afterwards.

---

<div class="post-metadata">

**Author:** ![S\_Arbabi](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@S\_Arbabi](https://discourse.slicer.org/u/S_Arbabi)\
**Post date:** [October 10, 2022, 7:25pm UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499/7 "2022-10-10T19:25:00Z")

</div>

I also have the same question, did you find the solution @justomont ?

---

<div class="post-metadata">

**Author:** ![justomont](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/justomont/32/16814_2.png) [@justomont](https://discourse.slicer.org/u/justomont)\
**Post date:** [October 11, 2022, 10:23am UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499/8 "2022-10-11T10:23:09Z")

</div>

Since the transformation was working from the Python Interactor, I just moved the section of the script that registered the label map to a different new function (called right after finishing the registration of the MNI template) and it worked. To be honest, I don’t really understand why it is working now, but it does.

---

<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:** [October 19, 2022, 6:35am UTC](https://discourse.slicer.org/t/tranformation-of-labelmap-not-working-on-script/25499/9 "2022-10-19T06:35:21Z")

</div>

> [@justomont](#):
>
> `slicer.mrmlScene.CopyNode(labelmapVolumeNode)`

CopyNode is a very low-level function. It makes a copy only of the data node, but still keeps referencing the same display, storage, etc. nodes. If you want to create an independent copy of a node then you can use [clone feature of Subject Hierarchy](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#clone-a-node).
