Skip to content

Integrate subject mapping and exact replay - #1503

Open
fepegar wants to merge 6 commits into
fepegar/batch-redesign-historyfrom
fepegar/batch-redesign-integrations
Open

Integrate subject mapping and exact replay#1503
fepegar wants to merge 6 commits into
fepegar/batch-redesign-historyfrom
fepegar/batch-redesign-integrations

Conversation

@fepegar

@fepegar fepegar commented Jul 20, 2026

Copy link
Copy Markdown
Member

[Generated by a coding agent]


What

Integrate subject-wise mapping, exact parameter replay, and MONAI/Cornucopia adapters with the simplified batch model.

Why

Non-vectorizable operations need a supported per-subject path, and exact replay must retain the normal copy/wrap/history lifecycle. Adapters previously duplicated unbatch/rebatch code and silently ignored unsupported outputs.

How

  • add SubjectsBatch.map_subjects(copy=...)
  • add Transform.apply_with_params() through the shared execution path
  • validate transient batched params without persisting private keys
  • reject replay for transforms without a compatible exact kernel
  • migrate MONAI/Cornucopia adapters to mapping with explicit result validation
  • standardize strict p=0 behavior, including lazy CropOrPad

Validation

  • full test suite
  • Ruff format/lint and ty
  • documentation tests and build
  • full prek hook suite, including Xenon

Replacement stack

  1. #1500 — padding type prerequisite
  2. #1501 — batch factories, prototypes, and schema
  3. #1502 — exact per-element batch history
  4. This PR — mapping, replay, and adapters
  5. #1504 — documentation and benchmarks

Copilot AI review requested due to automatic review settings July 20, 2026 21:20
@github-actions

github-actions Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

📖 Docs Preview

Preview of the documentation for this PR:

🔗 https://smokeshow.helpmanual.io/6j3t43153s2y2a1g066r/

Built from 1c8b785

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends TorchIO’s batching/transform infrastructure to support exact parameter replay and per-subject mapping within batches, and updates adapter implementations to use the new batch mapping lifecycle while tightening probability semantics (p=0 is always a no-op).

Changes:

  • Add Transform.apply_with_params() and refactor Transform.forward() through a shared execution path that supports exact replay without sampling.
  • Add SubjectsBatch.map_subjects() to apply per-subject callbacks (with optional tensor cloning) and rebuild batches with schema validation.
  • Update MONAI/Cornucopia adapters and select transforms to reject exact replay where it’s incompatible, and standardize strict p=0 behavior (including lazy CropOrPad).

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/test_transforms_base.py Adds coverage for apply_with_params() replay, history behavior, and batched-param validation.
tests/test_monai_adapter.py Adds adapter tests for metadata updates, history/annotation preservation, strict p=0, and in-place behavior.
tests/test_crop_or_pad.py Updates probability test to ensure p=0 is a strict no-op even when RNG draws 0.
tests/test_cornucopia_adapter.py Adds adapter tests for history/annotations, strict p=0, result validation, and in-place behavior.
tests/test_batch.py Adds coverage for SubjectsBatch.map_subjects() schema, history retention, and copy semantics.
src/torchio/transforms/transform.py Introduces apply_with_params(), shared _execute() lifecycle, and batched-param validation helpers.
src/torchio/data/batch.py Implements SubjectsBatch.map_subjects() built on unbatch() + rebuild with schema checks.
src/torchio/transforms/monai_adapter.py Refactors adapter to use map_subjects() and validates dict-transform outputs/metadata updates.
src/torchio/transforms/cornucopia_adapter.py Refactors adapter to use map_subjects() and validates result arity/types.
src/torchio/transforms/spatial/crop_or_pad.py Makes lazy probability check strict (>=) and disables apply_with_params().
src/torchio/transforms/spatial/ensure_shape_multiple.py Disables apply_with_params() for this spatial transform.
src/torchio/transforms/compose.py Disables apply_with_params() for composition/transforms with incompatible replay semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@fepegar
fepegar force-pushed the fepegar/batch-redesign-history branch from f992093 to c49206f Compare July 20, 2026 21:33
@fepegar
fepegar force-pushed the fepegar/batch-redesign-integrations branch from bde5bcd to dc67be7 Compare July 20, 2026 21:34
@fepegar
fepegar requested a review from Copilot July 20, 2026 21:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/torchio/transforms/transform.py:480

  • In _build_history_traces, _batched_keys being present implicitly means batched bookkeeping is active, but _batch_size is only read via params.get(...). If a custom transform returns _batched_keys without a valid _batch_size, the current error becomes Parameter batch size None does not match input batch size ..., which is misleading and doesn’t catch non-int/bool values. Consider validating _batch_size here (similar to _get_expected_batch_size) to produce a clear error for transform authors.
        expected_size = params.get("_batch_size")
        if expected_size != batch_size:
            msg = (
                f"Parameter batch size {expected_size} does not match"
                f" input batch size {batch_size}"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

@fepegar
fepegar force-pushed the fepegar/batch-redesign-history branch from 9d4a1f0 to a2250fc Compare July 20, 2026 21:53
@fepegar
fepegar force-pushed the fepegar/batch-redesign-integrations branch from 4ff3671 to 1936a5a Compare July 20, 2026 21:59
@fepegar
fepegar requested a review from Copilot July 20, 2026 21:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

@fepegar
fepegar force-pushed the fepegar/batch-redesign-history branch from a2250fc to 61c539c Compare July 20, 2026 22:06
@fepegar
fepegar force-pushed the fepegar/batch-redesign-integrations branch from 1936a5a to 813693a Compare July 20, 2026 22:08
@fepegar
fepegar requested a review from Copilot July 20, 2026 22:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

@fepegar
fepegar force-pushed the fepegar/batch-redesign-history branch from 61c539c to fa4ffb3 Compare July 20, 2026 22:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

@fepegar
fepegar force-pushed the fepegar/batch-redesign-integrations branch from 11d47fe to c5f8d49 Compare July 20, 2026 22:58
@fepegar
fepegar force-pushed the fepegar/batch-redesign-history branch from 9823221 to 80f7b1a Compare July 20, 2026 22:58
@fepegar
fepegar requested a review from Copilot July 20, 2026 22:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

@fepegar
fepegar force-pushed the fepegar/batch-redesign-integrations branch from c5f8d49 to 77a7350 Compare July 20, 2026 23:10
@fepegar
fepegar requested a review from Copilot July 20, 2026 23:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Comment thread src/torchio/transforms/monai_adapter.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Comment thread src/torchio/transforms/transform.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Comment thread src/torchio/transforms/transform.py Outdated
fepegar added 6 commits July 21, 2026 00:40
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: baf5f11a-f67c-44dc-804e-6be849fa9160
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: baf5f11a-f67c-44dc-804e-6be849fa9160
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: baf5f11a-f67c-44dc-804e-6be849fa9160
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: baf5f11a-f67c-44dc-804e-6be849fa9160
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: baf5f11a-f67c-44dc-804e-6be849fa9160
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: baf5f11a-f67c-44dc-804e-6be849fa9160
@fepegar
fepegar force-pushed the fepegar/batch-redesign-integrations branch from 99ae9a8 to 1c8b785 Compare July 20, 2026 23:43
@fepegar
fepegar requested a review from Copilot July 20, 2026 23:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

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.

2 participants