Skip to content

fix affine transformations: argument order, inverse, axes - #291

Open
CuiweiG wants to merge 4 commits into
HelenaLC:develfrom
CuiweiG:fix-289-affine
Open

CuiweiG wants to merge 4 commits into
HelenaLC:develfrom
CuiweiG:fix-289-affine

Conversation

@CuiweiG

@CuiweiG CuiweiG commented Nov 10, 2026

Copy link
Copy Markdown

Fixes #289.

The first three commits are the ordering from the issue and the two points from my comment there. The tests include the three I listed in that comment, and crop() on blobs_image through an affine over (c, y, x).

  • .trans_f() now passes the matrix to ST_Affine() row by row, then the offsets.
  • rev = TRUE inverts the matrix instead of negating it. A singular matrix gives an error.
  • A new .ct_affine() picks the rows and columns of a stored matrix by the transformation's output and input axes, and stops when axes and data don't agree. transform() uses it, and .box2rev() uses it to get the (x, y) part, without an image's channel axis. addCT() now counts the element's axes. It used dim(x), which has length 2 for any point or shape element, so a transformation added to a 3D one got (x, y) axes, and .ct_affine() would have rejected it.

The fourth commit is extra: the identity shortcut in .trans_f() compared an affine matrix with 1 throughout, so a matrix of ones was treated as the identity, and the identity itself was not. It is separate so it can be left out.

Not changed: a sequence with an affine step still fails as on devel, an image or label still has no affine() method for transform() to dispatch to, and circle radii are only rescaled by scale().

devtools::check() on the Bioconductor devel Docker image (R 4.6.1) gives 0 errors, 0 warnings and the same 4 notes as devel, and lintr is clean on the changed files.

On #289 I only offered the tests, so if you'd rather fix this your own way I can cut the PR down to them.

@Artur-man

Copy link
Copy Markdown
Collaborator

Thanks for the PR(s) @CuiweiG, I am busy with some other projects now, but will check after BioC release if can.

`.trans_f()` spliced the matrix into `ST_Affine()` column by column, so
`rbind(c(a, b, xoff), c(d, e, yoff))` arrived as `(a, d, b, e, xoff,
yoff)` where DuckDB expects `(a, b, d, e, xoff, yoff)`: the linear part
was transposed and rotations ran the wrong way. Translations and
diagonal scalings, the cases tested so far, are the same either way.
With `rev=TRUE`, `.trans_f()` negated the affine matrix, which is not
its inverse, not even for a pure translation. `crop()` on images and
labels gets here through `.box2rev()`, which maps the query box back
with `transform(z, 1, rev=TRUE)`. The linear part is now inverted and
the offsets follow from it; a singular matrix gives an error.
The rows of a stored affine matrix follow the transformation's output
axes, its columns the input axes. `transform()` applied the matrix as
stored, and `.box2rev()` relabelled the axes of an array's
transformation as (x, y) without reordering it. For `blobs_labels`,
whose affine maps (y, x) to (x, y), `crop()` through that coordinate
system sent the query box to the wrong place.

`.ct_affine()` now picks rows and columns by these axes, leaves out
axes not needed (e.g., the channel of an image) and fails when data and
axes don't agree. `addCT()` takes the number of axes from `axes(x)`;
`dim(x)` has length 2 for any point or shape element, so a 3D one got
(x, y) axes.
The identity shortcut in `.trans_f()` compared an affine matrix against
1 throughout, so a matrix of ones was returned untransformed, while the
actual identity was not recognised.
@CuiweiG

CuiweiG commented Nov 16, 2026

Copy link
Copy Markdown
Author

I've rebased this PR and #292 onto devel without changing any code. The only conflict here was in inst/NEWS. #292 rebased cleanly, but its NEWS entry ended up under 0.99.47. Both entries are now under 'changes in devel', with this PR's at the end and #292's at the top, so merging either PR won't cause a conflict in the other.

Before the rebase, both PRs failed the same two checks, for reasons unrelated to these changes. The Windows job of check-SpatialData.Plot fails the same way on the devel branch: SpatialData.plot has no Windows snapshot for its overlays test, so testthat adds a new one, which counts as a failure on CI. On Ubuntu, the Linux snapshot still matches. The touchstone comment check fails because the benchmarks are skipped for PRs from non-collaborators, so there's no artifact for it to download.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Affine trans. matrix element ordering for ST_Affine

2 participants