spatialdata-plot icon indicating copy to clipboard operation
spatialdata-plot copied to clipboard

Transformations with flipped y-axis

Open Sonja-Stockhaus opened this issue 1 year ago • 3 comments

Sth I encountered while working on #337: when using datashader, transformations of points and shapes need to be applied before rendering. I think the flipping of the y-axis leads to an inconsistency when applying Affine transformations (might also be for other transformations, that's just where I noticed it).

Say we are applying an affine transformation to our blobs polygons, namely a rotation 90° to the right (around 0/0), and then render:

from spatialdata.transformations import Affine
from spatialdata.datasets import blobs
from spatialdata.transformations._utils import _set_transformations

rotation = Affine(
    [
        [0, -1, 0],
        [1, 0, 0],
        [0, 0, 1],
    ],
    input_axes=("x", "y"),
    output_axes=("x", "y"),
)

blob = blobs()
_set_transformations(blob["blobs_polygons"], {"global": rotation})
blob.pl.render_shapes("blobs_polygons", method="matplotlib", outline_alpha=1.0).pl.show()

If I get this correctly, a point can be transformed by performing matrix multiplication with the affine matrix of the transformation, such as np.array([1, 1]) @ np.array([[0, -1], [1, 0]]) which gives [1, -1], therefore rotation by 90° to the right around the point 0/0.

The original image of the blobs polygons without any transformation looks like this: Image

And the code above (using matplotlib) gives us Image Which looks like a rotation to the right around 0/0. However, my question here is that when you flip the y-axis, wouldn't any rotation to the right look like a rotation to the left instead?

My current solution for datashader simply performs the matrix multiplication of all points of the polygons with the transformation matrix. This is all in a "normal", not flipped coordinate system, so when you flip the axes, you would get something like this: Image (Looks like a rotation to the left, because the y-axis is flipped).

@LucaMarconato since you're the master of transformations, could you have a look and tell me what you think about it? Is this intended behavior since it is more intuitive for the user? If yes: do you know an easy way how to get the affine matrix to do the transformation in the y-axis-not-flipped scenario (especially thinking about sequences of transformations...) If no: the matplotlib version would be "incorrect" and I think get_extent() as well, since it gives us exactly the values that we see in the matplotlib plot (and that's a problem, because in the very end of basic.show(), the axis limits are adapted according to the extent - I only got the datashader plot above by manually overwriting the extent).

(cc: @timtreis)

Sonja-Stockhaus avatar Nov 05 '24 20:11 Sonja-Stockhaus

@LucaMarconato any thoughts on this? Our current best guess is "do what matplotlib is doing" so we don't change all plots created to date with the library 😅

timtreis avatar Nov 27 '24 17:11 timtreis

Sorry I started working on this but then things got into my way. I'll get back to this soon.

LucaMarconato avatar Nov 27 '24 17:11 LucaMarconato

@LucaMarconato I have an approach right now in #378 that works for Identity, Scale and Affine and all sorts of sequences of the three. The idea is to first reflect all points along the x-axis by multiplying with [[1, 0], [0, -1]], then apply the transformation(s) (multiply with the affine matrix - for a Sequence we need to reverse the order of the transformations first) and flip back by once again multiplying with our flip matrix.

This gives the same results as matplotlib. The only problem is that the same approach doesn't work for MapAxis and Translation, so I will probably just try to write a method that checks for the type of each transformation and treats it accordingly (e.g. don't flip before MapAxis).

If you find the time it would just be interesting to know what you think about the idea that a rotation to the right should be done to the left on a flipped y-axis, aka the matplotlib result is "incorrect"?

Sonja-Stockhaus avatar Nov 27 '24 18:11 Sonja-Stockhaus