# Weighted average over temporal dimension

**URL:** <https://discourse.itk.org/t/weighted-average-over-temporal-dimension/3636>\
**Category:** Algorithms\
**Tags:** simpleitk\
**Created:** [November 19, 2020, 5:20pm UTC](https://discourse.itk.org/t/weighted-average-over-temporal-dimension/3636 "2020-11-19T17:20:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonasteuwen](https://discourse.itk.org/user_avatar/discourse.itk.org/jonasteuwen/32/1335_2.png) [@jonasteuwen](https://discourse.itk.org/u/jonasteuwen)\
**Post date:** [November 19, 2020, 5:20pm UTC](https://discourse.itk.org/t/weighted-average-over-temporal-dimension/3636/1 "2020-11-19T17:20:32Z")

</div>

Hello all. I have a bunch of 3D scans of the same subject over time joined in a 4D scan using (by `JoinSeries`) and I have a vector with the phase weights. To create a 3D approximation of this 4D object you might want to multiply with the (normalized) phase weights and then take the sum over that axis.

Is there a one-liner for this in (Simple)ITK or should I resort to turning the vector into an image with the same dimensions, multiply and sum over the axis? The ProjectionImageFilter’s do not seem to support weights.

As an followup question, I’d like to know if there is somehow a way to assign these non-uniform “temporal spacings” in ITK? Currently I use SetMetaData where I set the phase as an additional key, but having non-uniform spacings would be convenient. I see that basically the xarray implementation between ITK \<-\> xarray basically sets a position _per slice_. Is there such ITK support?

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 19, 2020, 6:03pm UTC](https://discourse.itk.org/t/weighted-average-over-temporal-dimension/3636/2 "2020-11-19T18:03:09Z")

</div>

> [@jonasteuwen](#):
>
> Is there a one-liner for this in (Simple)ITK or should I resort to turning the vector into an image with the same dimensions, multiply and sum over the axis? The ProjectionImageFilter’s do not seem to support weights.

Your could try something like:

```auto
sitk.NaryAdd([img4d[:,:,:,i]*weights[i] for i in range(img4d.GetSize()[3])] ) 

```

If you use a for loop with the inplace `*=` it may be a little more efficeint.

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [November 19, 2020, 6:36pm UTC](https://discourse.itk.org/t/weighted-average-over-temporal-dimension/3636/3 "2020-11-19T18:36:11Z")

</div>

Hello @jonasteuwen,

With respect to non-uniform temporal spacings, that isn’t supported in the form of a 4D ITK image. In ITK the steps along an axis are always assumed to have uniform spacing. This works in most 3D+time settings such as standard video.

To have non uniform spacing along the temporal axis you’d have to treat the volumetric video as a list/array of 3D images so that you can associate a variable timestamp with each step.

---

<div class="post-metadata">

**Author:** ![jonasteuwen](https://discourse.itk.org/user_avatar/discourse.itk.org/jonasteuwen/32/1335_2.png) [@jonasteuwen](https://discourse.itk.org/u/jonasteuwen)\
**Post date:** [November 19, 2020, 6:39pm UTC](https://discourse.itk.org/t/weighted-average-over-temporal-dimension/3636/4 "2020-11-19T18:39:44Z")

</div>

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 19, 2020, 7:37pm UTC](https://discourse.itk.org/t/weighted-average-over-temporal-dimension/3636/5 "2020-11-19T19:37:06Z")

</div>

The Cast image filter does not support 4D images. It looks like the error message could use improvement.

---

<div class="post-metadata">

**Author:** ![jonasteuwen](https://discourse.itk.org/user_avatar/discourse.itk.org/jonasteuwen/32/1335_2.png) [@jonasteuwen](https://discourse.itk.org/u/jonasteuwen)\
**Post date:** [November 19, 2020, 8:22pm UTC](https://discourse.itk.org/t/weighted-average-over-temporal-dimension/3636/6 "2020-11-19T20:22:58Z")

</div>

Thanks. While I can how to resolve that for the above problem, there are more general places where this might be relevant. I’ll open another post!
