scverse / scverse/spatialdata

Transformation inconsistency between on-disk and in-memory usage

Open
#736 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

method: transforms 🔃 needs: triage priority: medium
Dominant language
Python
Stars
394
Forks
95
Avg merge
4d 3h
Merged PRs (30d)
7

Description

Hi @LucaMarconato,

I discovered this weird edge case behavior which can lead to errors in the inverse transformations, but it only happens when we write the spatialdata object on disk.

TL;DR: when we save a 2D transformation on-disk, it is then read as a 3D transformation even if we don't want to (if a "z" column exists).
Then, if we want to assign this transformation (supposedly 2D) to 2D shapes, then when writing the data again, the transformation will keep a 3D output but a 2D input. This doesn't happen in-memory because the transformation remains 2D.

Reproducing the issue:

import spatialdata
from spatialdata.transformations import Affine, set_transformation, get_transformation

sdata = spatialdata.datasets.blobs()

# I'm adding a dummy z-column
sdata["blobs_points"]["z"] = 1

# creating a dummy transformation (note that I'm not using z!)
affine = Affine([[0.2, 0, 100], [0, 0.2, 600], [0, 0, 1]], ["x", "y"], ["x", "y"])
set_transformation(sdata["blobs_points"], affine, "micron")

# now, I'm saving my sdata on disk for later use
sdata.write("test.zarr", overwrite=True)
sdata = spatialdata.read_zarr("test.zarr") # later on, I'm loading it back

# I'm loading back my sdata object, and I want to assign the same transformation to my circles
# Note that, since I saved sdata on disk, the affine transformation now contains the z-axis...
set_transformation(sdata["blobs_circles"], get_transformation(sdata["blobs_points"], get_all=True), set_all=True)

# saving again the object, for later use
sdata.write("test2.zarr")
sdata = spatialdata.read_zarr("test2.zarr") # later on, I'm loading it back

Now, the cells transformation is weird:

>>> sdata["blobs_circles"].attrs

{'transform': {'global': Identity ,
  'micron': Affine (x, y -> z, x, y)
      [0. 0. 0.]
      [  0.2   0.  100. ]
      [0.e+00 2.e-01 6.e+02]
      [0. 0. 1.]}}

For instance, it can't be inversed, and this will create an error:

sdata["blobs_circles"].attrs["transform"]["micron"].inverse()

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Affine serialization through write(), read_zarr(), set_transformation(), and get_transformation(), using the reproduction in the issue. Compare the in-memory and reloaded transformation dimensions, then verify the repeated write/read case and inverse() behavior. Done means a 2D transformation remains 2D after persistence and can be assigned and inverted consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.