@@ -830,23 +830,37 @@ def _compose_affine_from_linear_and_translation(
830830 return Affine (matrix , input_axes = input_axes , output_axes = output_axes )
831831
832832
833- def _decompose_transformation (transformation : BaseTransformation , input_axes : tuple [ValidAxis_t , ...]) -> Sequence :
833+ def _decompose_transformation (
834+ transformation : BaseTransformation , input_axes : tuple [ValidAxis_t , ...], simple_decomposition : bool = True
835+ ) -> Sequence :
834836 """
835837 Decompose a given 2D transformation into a sequence of predetermined types of transformations.
836838
837839 Parameters
838840 ----------
839841 transformation
840- The transformation to decompose. It is assumed to be a type that can be represented as a single affine
842+ The transformation to decompose. It is assumed to be of a type that can be represented as a single affine
841843 transformation. It should leave the input axes unmodified, and it should not transform the c channel, if this
842844 is present.
845+ input_axes
846+ The axes of the data the transformation is to be applied to
847+ simple_decomposition
848+ If true, decomposes a transformation into it's linear part (affine without translation) and translation part,
849+ otherwise decomposes it into a sequence of reflection, rotation, shear, scale, translation.
843850
844851 Returns
845852 -------
846853 sequence
847854 Returns a sequence of transformations (class :class:`~spatialdata.transformations.Sequence`) which operates only
848- on the spatial part (no c channel). The output sequence will contain 5 transformations in the following order
849- (the first is applied first):
855+ on the spatial part (no c channel). The output sequence will contain either 2 either 5 transformations in the
856+ following order (the first is applied first).
857+ Case `simple_decomposition = True`.
858+
859+ 1. Linear part (affine): linear part of the affine transformation, represented as a
860+ :class:`~spatialdata.transformations.Affine` transformation.
861+ 2. Translation. Represented as a :class:`~spatialdata.transformations.Translation` transformation.
862+
863+ Case `simple_decomposition = False`.
850864
851865 1. Reflection. Represented as :class:`~spatialdata.transformations.Scale` transformation with elements in
852866 {1, -1}.
@@ -855,15 +869,12 @@ def _decompose_transformation(transformation: BaseTransformation, input_axes: tu
855869 Please look at the source code of this function if you need to recover the angle theta.
856870 3. Shear. Represented as an :class:`~spatialdata.transformations.Affine` transformation which in its matrix
857871 form presents itself as an homogeneous affine matrix with no translation part. The matrix is upper
858- triangular with diagonal elements in {-1, 1} .
859- 3 . Scale. Represented as a :class:`~spatialdata.transformations.Scale` transformation with positive
872+ triangular with diagonal elements all equal to 1 .
873+ 4 . Scale. Represented as a :class:`~spatialdata.transformations.Scale` transformation with positive
860874 elements.
861- 4 . Translation. Represented as a :class:`~spatialdata.transformations.Translation` transformation.
875+ 5 . Translation. Represented as a :class:`~spatialdata.transformations.Translation` transformation.
862876
863- Note that some of these transformations may be identity transformations if applicable.
864-
865- input_axes
866- The axes of the input coordinate system of the transformation.
877+ Note that some of these transformations may be identity transformations.
867878 """
868879 output_axes = _get_current_output_axes (transformation = transformation , input_axes = input_axes )
869880 if input_axes != output_axes :
@@ -890,43 +901,62 @@ def _decompose_transformation(transformation: BaseTransformation, input_axes: tu
890901 translation_part = m [:- 1 , - 1 ]
891902 linear_part = m [:- 1 , :- 1 ]
892903
893- ##
894- # qr factorization
895- a = linear_part
896- r , q = scipy .linalg .rq (a )
897-
898- theta = np .arctan2 (q [1 , 0 ], q [0 , 0 ])
899- rotation_matrix = np .array ([[np .cos (theta ), - np .sin (theta )], [np .sin (theta ), np .cos (theta )]])
900-
901- scale_matrix = np .diag (np .abs (np .diag (r )))
902- shear_matrix = np .linalg .inv (scale_matrix ) @ r
903- assert np .allclose (scale_matrix @ shear_matrix , r )
904-
905- qq = rotation_matrix .T @ q
906- # check that qq is a diagonal matrix with diagonal values in {-1, 1}
907- assert np .allclose (np .diag (qq ) ** 2 , np .ones (qq .shape [0 ]))
908- assert np .isclose (np .sum (np .abs (qq .ravel ())), qq .shape [0 ])
909- assert np .allclose (rotation_matrix @ qq , q )
910-
911- aa = scale_matrix @ shear_matrix @ rotation_matrix @ qq
912- assert np .allclose (a , aa )
913-
914- scale = Scale (np .diag (scale_matrix ), axes = axes )
915- shear = _compose_affine_from_linear_and_translation (
916- linear = shear_matrix ,
917- translation = np .zeros (shear_matrix .shape [0 ]),
918- input_axes = axes ,
919- output_axes = axes ,
920- )
921- rotation = _compose_affine_from_linear_and_translation (
922- linear = rotation_matrix ,
923- translation = np .zeros (rotation_matrix .shape [0 ]),
924- input_axes = axes ,
925- output_axes = axes ,
926- )
927- inversion = Scale (np .diag (qq ), axes = axes )
928- translation = Translation (translation_part , axes = axes )
929- sequence = Sequence ([inversion , rotation , shear , scale , translation ])
904+ if simple_decomposition :
905+ translation = Translation (translation_part , axes = axes )
906+ linear = _compose_affine_from_linear_and_translation (
907+ linear = linear_part ,
908+ translation = np .zeros (linear_part .shape [0 ]),
909+ input_axes = axes ,
910+ output_axes = axes ,
911+ )
912+ sequence = Sequence ([linear , translation ])
913+ else :
914+ # qr factorization
915+ a = linear_part
916+ r , q = scipy .linalg .rq (a )
917+
918+ theta = np .arctan2 (q [1 , 0 ], q [0 , 0 ])
919+ rotation_matrix = np .array ([[np .cos (theta ), - np .sin (theta )], [np .sin (theta ), np .cos (theta )]])
920+
921+ scale_matrix = np .diag (np .abs (np .diag (r )))
922+ shear_matrix = np .linalg .inv (scale_matrix ) @ r
923+ assert np .allclose (scale_matrix @ shear_matrix , r )
924+ d = np .diag (np .diag (shear_matrix ))
925+
926+ qq = rotation_matrix .T @ q
927+ # check that qq is a diagonal matrix with diagonal values in {-1, 1}
928+ assert np .allclose (np .diag (qq ) ** 2 , np .ones (qq .shape [0 ]))
929+ assert np .isclose (np .sum (np .abs (qq .ravel ())), qq .shape [0 ])
930+ assert np .allclose (rotation_matrix @ qq , q )
931+
932+ adjusted_shear_matrix = shear_matrix @ d
933+ adjusted_rotation_matrix = d @ rotation_matrix @ d
934+ assert np .allclose (
935+ adjusted_rotation_matrix @ adjusted_rotation_matrix .T , np .eye (adjusted_rotation_matrix .shape [0 ])
936+ )
937+ adjusted_qq = d @ qq
938+
939+ aaa = scale_matrix @ shear_matrix @ d @ d @ rotation_matrix @ d @ d @ qq
940+ assert np .allclose (a , aaa )
941+ aa = scale_matrix @ adjusted_shear_matrix @ adjusted_rotation_matrix @ adjusted_qq
942+ assert np .allclose (a , aa )
943+
944+ scale = Scale (np .diag (scale_matrix ), axes = axes )
945+ shear = _compose_affine_from_linear_and_translation (
946+ linear = adjusted_shear_matrix ,
947+ translation = np .zeros (shear_matrix .shape [0 ]),
948+ input_axes = axes ,
949+ output_axes = axes ,
950+ )
951+ rotation = _compose_affine_from_linear_and_translation (
952+ linear = adjusted_rotation_matrix ,
953+ translation = np .zeros (rotation_matrix .shape [0 ]),
954+ input_axes = axes ,
955+ output_axes = axes ,
956+ )
957+ inversion = Scale (np .diag (adjusted_qq ), axes = axes )
958+ translation = Translation (translation_part , axes = axes )
959+ sequence = Sequence ([inversion , rotation , shear , scale , translation ])
930960 check_m = sequence .to_affine_matrix (input_axes = input_axes , output_axes = input_axes )
931961 assert np .allclose (check_m , matrix )
932962 return sequence
0 commit comments