# Spatialdata.aggregate, image and segmentation\_mask have different shape, but aligned in 'global'

**URL:** <https://discourse.scverse.org/t/spatialdata-aggregate-image-and-segmentation-mask-have-different-shape-but-aligned-in-global/3650>\
**Category:** spatialdata\
**Created:** [May 14, 2025, 12:39am UTC](https://discourse.scverse.org/t/spatialdata-aggregate-image-and-segmentation-mask-have-different-shape-but-aligned-in-global/3650 "2025-05-14T00:39:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kare9758](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@kare9758](https://discourse.scverse.org/u/kare9758)\
**Post date:** [May 14, 2025, 12:39am UTC](https://discourse.scverse.org/t/spatialdata-aggregate-image-and-segmentation-mask-have-different-shape-but-aligned-in-global/3650/1 "2025-05-14T00:39:21Z")

</div>

Thanks for the great package!

I have an image and a cell mask, I want to quantify the mean intensity of the signal measured in the image within each cell, using the cell mask. The image is a lot smaller, with size (3,912,1488), while the cell mark is a dataTree with varying sizes, the smallest is (2129,3198).

I have a global coordinate system where there two modalities are aligned, but when I use spatialdata.aggregate, (1) it does not accept the “cell\_labels” mask, as it is a DataTree rather than a DataArray, (2) when I supply a specific element in the DataTree, it says the mask and image do not have the same size. Seems like aggregate() was not able to leverage the alignment information stored in the ‘global’ coordinate system.

I still have the option of resizing my image to the same shape as the cell mask, but just wanted to check to see if you have any comments or suggestions.

Thank you very much.

Best regards,  
Michelle

 ![Picture1](https://canada1.discourse-cdn.com/flex035/uploads/forum11/original/2X/8/8db272a4b9fb97fbd7f8bbce5ee6e5ef4f0c1947.jpeg)

---

<div class="post-metadata">

**Author:** ![wjylrm](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@wjylrm](https://discourse.scverse.org/u/wjylrm)\
**Post date:** [May 26, 2025, 5:32pm UTC](https://discourse.scverse.org/t/spatialdata-aggregate-image-and-segmentation-mask-have-different-shape-but-aligned-in-global/3650/2 "2025-05-26T17:32:14Z")

</div>

I have the same question. Even after resizing, the aggregate step threw the out of memory error. I am wondering if we need to crop and stitch the images. Any suggestions or comments are appreciated

---

<div class="post-metadata">

**Author:** ![LucaMarconato](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/lucamarconato/32/661_2.png) [@LucaMarconato](https://discourse.scverse.org/u/LucaMarconato)\
**Post date:** [November 10, 2025, 12:52am UTC](https://discourse.scverse.org/t/spatialdata-aggregate-image-and-segmentation-mask-have-different-shape-but-aligned-in-global/3650/3 "2025-11-10T00:52:21Z")

</div>

Hi @kare9758, you can perform the task by calling first `spatialdata.rasterize()`, which will allow you to obtain a DataArray from a DataTree, with the desired resolution and aware of coordinate transformation. After this you will be able to call `aggregate()`. We could make `aggregate()` do this automatically in the future (or a PR for this is welcome).

---

<div class="post-metadata">

**Author:** ![LucaMarconato](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/lucamarconato/32/661_2.png) [@LucaMarconato](https://discourse.scverse.org/u/LucaMarconato)\
**Post date:** [November 10, 2025, 12:53am UTC](https://discourse.scverse.org/t/spatialdata-aggregate-image-and-segmentation-mask-have-different-shape-but-aligned-in-global/3650/4 "2025-11-10T00:53:34Z")

</div>

> [@wjylrm](#):
>
> I have the same question. Even after resizing, the aggregate step threw the out of memory error. I am wondering if we need to crop and stitch the images. Any suggestions or comments are appreciated

If the problem is still open please open an issue in `spatialdata` by posting a “copy-pastable” code to allow to us to reproduce the issue. The code should generate mock data and not use external data (unless public and downloaded as part of the script). Thank you.

---

<div class="post-metadata">

**Author:** ![wjylrm](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@wjylrm](https://discourse.scverse.org/u/wjylrm)\
**Post date:** [November 21, 2025, 6:46pm UTC](https://discourse.scverse.org/t/spatialdata-aggregate-image-and-segmentation-mask-have-different-shape-but-aligned-in-global/3650/5 "2025-11-21T18:46:50Z")

</div>

Thanks. It seems like this is basically a non rigid image registration problem with the Xenium image and IF image being different resolution. I am wondering if there are any existing tools for this in the field.
