Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

- `ByDimension.to_affine()`, if all of its subtransforms can be converted to affine

## 0.9.2 - 2026-10-05

### Fixed
Expand Down
24 changes: 24 additions & 0 deletions src/transformnd/transforms/by_dimension.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from collections.abc import Iterable
from typing import Self
from array_api_compat import array_namespace
import numpy as np

from .affine import Affine
from .simple import Identity

from ..base import Transform
Expand Down Expand Up @@ -164,6 +166,28 @@ def invert(self) -> Transform[ArrayT] | None:
subtransforms=inverted_transforms,
)

def to_affine(self) -> Affine[ArrayT] | None:
"""Combine the subtransforms' affines, if all subtransforms can be converted.

Each subtransform's linear map and translation are placed in the rows of its output axes,
and the columns of its input axes.

Returns
-------
Affine[ArrayT] | None
The affine transformation, if all subtransforms can be converted to affine.
None otherwise.
"""
m = np.zeros((self.ndims.target + 1, self.ndims.source + 1))
m[-1, -1] = 1
for t in self.subtransforms:
sub_affine = t.transform.to_affine()
if sub_affine is None:
return None
m[np.ix_(t.output_axes, t.input_axes)] = sub_affine.matrix[:-1, :-1]
m[t.output_axes, -1] = sub_affine.matrix[:-1, -1]
return Affine(m)

def is_identity(self) -> bool:
for t in self.subtransforms:
if t.input_axes != t.output_axes or not t.transform.is_identity():
Expand Down
54 changes: 53 additions & 1 deletion tests/transforms/test_by_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import pytest

from transformnd.transforms.by_dimension import SubTransform
from transformnd.transforms import ByDimension, Scale, MapAxis
from transformnd.transforms import ByDimension, Scale, MapAxis, Translate
from transformnd.base import TransformSequence

from ..common import NullTransform


def test_2d_scale():
factor = [1.5]
Expand Down Expand Up @@ -113,3 +115,53 @@ def test_cross_axes_transform():
by_dim = ByDimension(subtransforms=[t_1, t_2])
coords_byDim = by_dim.apply(coords.copy())
assert np.array_equal(coords_byDim, coords_transformed)


@pytest.mark.parametrize(
"subtransforms",
[
# scale, with identity filled in
[SubTransform(Scale([1.5]), [1], [1])],
# map axis and scale
[
SubTransform(MapAxis([1, 0]), [0, 1], [0, 1]),
SubTransform(Scale([1.5]), [2], [2]),
],
# sequence with translation, to swapped axes
[
SubTransform(
TransformSequence([MapAxis([1, 0]), Translate([3.0, -4.0])]),
[0, 2],
[2, 0],
),
SubTransform(Scale([2.0]), [1], [1]),
],
# crossed axes
[
SubTransform(Scale([2.0]), [0], [1]),
SubTransform(Translate([5.0]), [1], [2]),
SubTransform(Scale([3.0]), [2], [0]),
],
],
)
def test_to_affine(subtransforms):
by_dim = ByDimension(subtransforms=subtransforms, fill_identity=3)
coords = np.random.default_rng(0).normal(size=(10, 3))
affine = by_dim.to_affine()
assert affine is not None
assert affine.apply(coords.copy()) == pytest.approx(by_dim.apply(coords.copy()))


def test_to_affine_in_sequence():
by_dim = ByDimension([SubTransform(Translate([2.0]), [0], [0])], fill_identity=2)
affine = TransformSequence([by_dim, Scale([2.0, 3.0])]).to_affine()
assert affine is not None
assert affine.apply(np.array([[1.0, 1.0]])) == pytest.approx(np.array([[6.0, 3.0]]))


def test_to_affine_not_affineable():
by_dim = ByDimension(
[SubTransform(NullTransform(1, affineable=False), [0], [0])],
fill_identity=2,
)
assert by_dim.to_affine() is None
Loading