Skip to content

Restore statistical padding modes - #1499

Merged
fepegar merged 9 commits into
mainfrom
fepegar/restore-statistical-padding-modes
Jul 20, 2026
Merged

Restore statistical padding modes#1499
fepegar merged 9 commits into
mainfrom
fepegar/restore-statistical-padding-modes

Conversation

@fepegar

@fepegar fepegar commented Jul 20, 2026

Copy link
Copy Markdown
Member

[Generated by a coding agent]


Summary

  • restore mean, median, and minimum padding modes in Pad and CropOrPad
  • preserve whole-volume, per-subject statistics across eager, batched, and lazy paths
  • retain dtype, device, gradient, affine, and integer truncation behavior

Testing

  • uv run --group test pytest tests/test_pad.py tests/test_crop_or_pad.py -q
  • mise run quality

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c079ff0-a235-45de-b35d-154ca4812d34
Copilot AI review requested due to automatic review settings July 20, 2026 14:37
@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/6v6d5j42573u4d5u6q4l/

Built from 79e565c

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c079ff0-a235-45de-b35d-154ca4812d34

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 restores statistical padding modes (mean, median, minimum) for Pad and CropOrPad, ensuring consistent whole-volume/per-subject statistics across eager, batched, and lazy execution paths while preserving existing dtype/device/affine behavior.

Changes:

  • Adds validation and implementation for statistical padding modes in Pad, including integer-truncation warnings for mean/median.
  • Reuses the same padding logic in CropOrPad (eager + lazy paths) to keep behavior consistent.
  • Adds targeted test coverage for invalid modes, statistics correctness, batching, lazy backend behavior, and truncation warnings.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
tests/test_pad.py Adds tests for statistical modes, batching behavior, integer truncation warnings, and differentiability.
tests/test_crop_or_pad.py Adds tests for statistical padding in tensor and lazy-backend paths.
src/torchio/transforms/spatial/pad.py Introduces validated PaddingMode, statistical value computation, and a shared _pad_tensor implementation.
src/torchio/transforms/spatial/crop_or_pad.py Switches eager/lazy padding to use the shared pad helper and validates padding mode.

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

Comment thread src/torchio/transforms/spatial/pad.py Outdated
Comment thread src/torchio/transforms/spatial/crop_or_pad.py Outdated
Comment thread src/torchio/transforms/spatial/crop_or_pad.py Outdated
@fepegar
fepegar force-pushed the fepegar/restore-statistical-padding-modes branch from b98068e to 20699a4 Compare July 20, 2026 16:17
fepegar and others added 4 commits July 20, 2026 17:20
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c079ff0-a235-45de-b35d-154ca4812d34
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c079ff0-a235-45de-b35d-154ca4812d34
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c079ff0-a235-45de-b35d-154ca4812d34
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c079ff0-a235-45de-b35d-154ca4812d34

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 7 out of 7 changed files in this pull request and generated 1 comment.

Comment thread src/torchio/transforms/spatial/_padding.py Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c079ff0-a235-45de-b35d-154ca4812d34

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 7 out of 7 changed files in this pull request and generated 2 comments.

Comment thread src/torchio/transforms/spatial/ensure_shape_multiple.py
Comment thread src/torchio/transforms/spatial/_padding.py Outdated
fepegar and others added 2 commits July 20, 2026 17:44
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c079ff0-a235-45de-b35d-154ca4812d34
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c079ff0-a235-45de-b35d-154ca4812d34

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 11 out of 11 changed files in this pull request and generated 1 comment.

Comment on lines +33 to +38
if not 0 <= q <= 1:
msg = f"Only values 0 <= q <= 1 are supported, but got {q!r}"
raise ValueError(msg)
index = q * (values.numel() - 1)
lower = math.floor(index)
lower_value = torch.kthvalue(values, lower + 1).values
@fepegar
fepegar merged commit 85a81dc into main Jul 20, 2026
31 checks passed
@fepegar
fepegar deleted the fepegar/restore-statistical-padding-modes branch July 20, 2026 16:51
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