Skip to content

Randomize k-space split axis in Motion (#81)#1485

Open
niravpatidar37 wants to merge 3 commits into
TorchIO-project:mainfrom
niravpatidar37:81-randomize-motion-kspace-axis
Open

Randomize k-space split axis in Motion (#81)#1485
niravpatidar37 wants to merge 3 commits into
TorchIO-project:mainfrom
niravpatidar37:81-randomize-motion-kspace-axis

Conversation

@niravpatidar37

@niravpatidar37 niravpatidar37 commented Jun 22, 2026

Copy link
Copy Markdown

Summary

Closes #81.

The Motion transform splits k-space into num_transforms + 1 segments and fills each from a differently moved copy of the image. Previously the split was hardcoded to the first spatial axis, even though the class docstring already documented it as being "along a random axis".

This PR randomizes the split axis, following the pattern already used by Ghosting:

  • New axes=(0, 1, 2) constructor option. One axis is chosen at random per application.
  • Pass axes=(0,) to keep the previous fixed-axis behavior.
  • The chosen axis is recorded in the transform params ("axis"), so it is reproducible and works per-instance when the transform is applied to a batch.

Implementation notes

  • The per-axis split is implemented with a vectorized per-element k-space mask (_segment_mask), so each element in a batch can use its own axis without a Python loop over elements.
  • The degenerate-segment guard was generalized to _check_segment_sizes, which validates the size of whichever axis is actually used (error message wording unchanged).

Tests

  • Updated the degenerate-segment test to use a volume that is too small on every axis, so it stays deterministic under a random axis, plus a pinned-axis variant.
  • Added TestMotionAxes: axes validation, axis recorded in params, the split axis is genuinely randomized across applications, and pinned-axis reproducibility.
  • tests/test_motion.py, tests/test_ghosting.py, tests/test_vectorization.py and tests/test_per_instance.py all pass; the vectorization suite confirms the per-element axis handling matches element-by-element application.

niravpatidar37 and others added 3 commits June 21, 2026 21:56
The Motion transform hardcoded the k-space segmentation to the first
spatial axis. Add an `axes=(0, 1, 2)` option (mirroring `Ghosting`) and
sample the split axis per application, recorded in the transform params
so it is reproducible and per-instance when batched. Pass `axes=(0,)` to
keep the previous fixed-axis behavior.

The per-axis split is implemented with a vectorized per-element k-space
mask, so each batch element can use its own axis. This also makes the
implementation match the class docstring, which already documented the
split as being "along a random axis".

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

Randomize k-space filling axis in RandomMotion

1 participant