Research / TorchIO v2 pre-release, Spatial transform

In the TorchIO v2 pre-release, the option that sets the order of two image transforms was inverted.

ReproducedFix submitted, not yet merged
Result
In the v2 pre-release, the option that sets the order of two image transforms was inverted, and the default matched neither order. The stable release, 1.2.1, is not affected. A fix is submitted for review, not yet merged.
Boundary
The v2 pre-release only (2.0.0a1 and 2.0.0a2), synthetic volumes, and target grids aligned with the image axes.
Who did what
Investigated and fix submitted by OMEGA (pull request #1536). AI agents in OMEGA wrote the patch and tests and ran the checks; Warren chose the scope and approved the submission. Issue #1491 was reported by another user.
Public evidence

The question

Does TorchIO’s combined spatial transform apply its two steps in the order its option says?

TorchIO prepares and augments 3D medical images for training deep-learning models. Its Spatial transform combines an affine step (rotation, scaling and shift) with an elastic deformation in one resampling, and the order matters: the other order gives a different image. A training pipeline would run normally with the wrong order, and nothing in the output would flag it.

Reported or claimedaffine_first=True applies the affine transform first, then the elastic deformation.
Independent checkAgainst the two transforms applied one after the other, affine_first=False matched affine-first, and the default matched neither order closely.

Reproduced: A reported behaviour was reproduced from a minimal, public input. Fix submitted, not yet merged: A fix was submitted to the maintainers with its evidence. It is not merged or released.

What was tested

  1. Compared the combined transform with the same two transforms applied one after the other, in each order, on a synthetic 3D volume.
  2. Repeated the comparison when resampling to a different target grid, against a reference written independently in numpy and scipy.
  3. Checked the released v1 code, which the issue had been filed against.
  4. Added four test cases, then broke the fix deliberately in three ways to check that the tests notice.

What happened

  • On the v2 code, affine_first=False reproduced affine-then-elastic (mean difference 0.00026). The default, affine_first=True, was 0.0049 from one order and 0.0142 from the other.
  • With the fix, each setting matches its requested order: mean differences of 0.00026 and 0.00041, where the two orders differ from each other by 0.0154.
  • The released v1 already used the correct order, and the swap proposed in the issue would have broken it, so the change targets v2 only.
  • The four new test cases fail on the v2 code and pass with the fix. The full suite passes: 1,476 tests, 8 skipped.

Challenge

The objections that would most easily undo the conclusion, and what answered each.

Is the swap proposed in the issue the right fix?
Not for the released version: v1 was already correct, and the swap would have broken it.
Could the new tests pass on the broken code?
No. All four fail on the v2 code, and each of the three deliberate breakages is caught.

What survived

Did not survive

  • In the v2 pre-release, affine_first selects the order of the two steps.Inverted, and the default matched neither order.

Survived

  • The released v1 applies the documented order.Each setting matched its own order (mean difference 0.00036 and 0.00034, against 0.011 for the other order).

Not established, or not tested

  • Which grid the deformation should span when resampling to a different target is not documented. The fix keeps the output grid and leaves the choice to the maintainers.
  • Target grids that are rotated relative to the image were not tested.

Limits

Synthetic volumes on macOS. No clinical images were used, and no effect on any trained model, diagnosis or patient was measured or is claimed. Applies to the v2 pre-release, not the stable release.

Reproduce or inspect

Source
repowazdogz-droid/torchio/tree/1491-fix-affine-first-order
Revision
c9848543
Command
python -m pytest -q tests/test_spatial.py
Expect
115 passed

Run from a TorchIO checkout of the pull request branch with its test dependencies installed. Re-run by OMEGA at this revision on 18 October 2026 (macOS): 115 passed.

Public record

Have a question like this?