# Time average of sequence of transforms

**URL:** <https://discourse.slicer.org/t/time-average-of-sequence-of-transforms/31927>\
**Category:** Support\
**Tags:** registration, transforms, sequences\
**Created:** [September 27, 2023, 8:58am UTC](https://discourse.slicer.org/t/time-average-of-sequence-of-transforms/31927 "2023-09-27T08:58:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zimogre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zimogre/32/10062_2.png) [@Zimogre](https://discourse.slicer.org/u/Zimogre)\
**Post date:** [September 27, 2023, 8:58am UTC](https://discourse.slicer.org/t/time-average-of-sequence-of-transforms/31927/1 "2023-09-27T08:58:00Z")

</div>

Hi all,

I am recently working with a time series of volumes. First, I obtained a sequence (Sequence module) of volumes and then registered them using the SequenceRegistration module obtaining the corresponding Transforms and displacement fields. I would like to obtain the time-averaged transformation. Is there a way in Slicer or also in Python to perform the time average operation over the sequence of transformations?

Also, can I apply mathematical operations (such as invention or composition) to these transforms?  
Thanks a lot!

---

<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, 2:46am UTC](https://discourse.slicer.org/t/time-average-of-sequence-of-transforms/31927/2 "2023-09-28T02:46:24Z")

</div>

You can write a small Python code snippet that iterates through the sequence for each time point it exports the transform into a displacement field vector volume, and you copy the voxels of the volume into a numpy array. In the end you can concatenate the arrays and use numpy to compute statistics along any axis (e.g., compute mean along time axis).

---

<div class="post-metadata">

**Author:** ![Zimogre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zimogre/32/10062_2.png) [@Zimogre](https://discourse.slicer.org/u/Zimogre)\
**Post date:** [September 28, 2023, 2:30pm UTC](https://discourse.slicer.org/t/time-average-of-sequence-of-transforms/31927/3 "2023-09-28T14:30:39Z")

</div>

Thanks for the suggestion! Once the time-averaged displacement field is loaded back in Slcier, how can I apply it on a Volume? Can I convert a Displacement Field into a Transform?  
P.S. is there any example of a snippet code like the one you mentioned?

---

<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:39pm UTC](https://discourse.slicer.org/t/time-average-of-sequence-of-transforms/31927/4 "2023-09-28T15:39:47Z")

</div>

> [@Zimogre](#):
>
> Can I convert a Displacement Field into a Transform?

You can load a displacement field image file (e.g., in a nrrd file) as a transform. Or you can manually create a grid transform and set the displacement image into it.

---

<div class="post-metadata">

**Author:** ![Zimogre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zimogre/32/10062_2.png) [@Zimogre](https://discourse.slicer.org/u/Zimogre)\
**Post date:** [October 2, 2023, 3:17pm UTC](https://discourse.slicer.org/t/time-average-of-sequence-of-transforms/31927/5 "2023-10-02T15:17:31Z")

</div>

> [@lassoan](#):
>
> write a small Python code snippet that iterates through the sequence for each time point it exports the transform into a displacement field vector volume, and you copy the voxels of the volume into a numpy array.

I converted all the Transform nodes into Displacement Fields with:  
displacement\_field\_volume =slicer.modules.transforms.logic().CreateDisplacementVolumeFromTransform(transformNode, referenceVolumeNode, False)

Then I tried to extract the corresponding array in this way:  
displacement\_field\_volume\_node = slicer.util.getNode(displacement\_field\_volume.GetID())  
array\_displacement\_field = slicer.util.arrayFromVolume(displacement\_field\_volume\_node)

But slicer gives the error:  
File “/opt/Slicer-4.11.20210226-linux-amd64/bin/Python/slicer/util.py”, line 1443, in arrayFromVolume  
narray = vtk.util.numpy\_support.vtk\_to\_numpy(vimage.GetPointData().GetScalars()).reshape(nshape)  
ValueError: cannot reshape array of size 1720320 into shape (28,160,128)

It seems that the arrayFromVolume tries to reshape all the 3 channels of the displacement fields together, do you know how to solve this?
