Skip to content

refactor: one kernels module instead of five copies - #61

Open
Hendrik-code wants to merge 1 commit into
hm/fix-bucket-and-rngfrom
hm/dedup-kernels
Open

refactor: one kernels module instead of five copies#61
Hendrik-code wants to merge 1 commit into
hm/fix-bucket-and-rngfrom
hm/dedup-kernels

Conversation

@Hendrik-code

Copy link
Copy Markdown
Collaborator

Stacked on #60. First of the three deduplication PRs; no new behaviour, but see the caveat below — it is not bit-for-bit everywhere.

What was duplicated

Four independent 3-D Gaussian implementations, three bias fields and two copies of the Laplace/Scharr constant tables, across five modules:

gpu/contrast.py gpu/fromSeg.py gpu/domain_transfer.py synthseg/functional.py cpu/contrast.py
Gaussian kernel
Bias field ✓ (polynomial)
Laplace / Scharr

Each carried some version of a comment saying it was kept local so its module would stay self-contained. The synthseg and domain_transfer bias fields were line-for-line identical.

That divergence is the point. It is what let two of the copies go wrong unnoticed — the uncentred Gaussian (#58) and the malformed 2-D Scharr x-kernel (#58) — and why each had to be fixed on its own rather than in one place. smauglab/transforms/kernels.py is that one place now.

Not bit-for-bit, deliberately

Two blur call sites change slightly:

  • Radius is ceil(3*sigma). domain_transfer and fromSeg used round(3*sigma), which is never wider, so their kernels may now be one tap larger.
  • Padding is reflect everywhere. domain_transfer used replicate; fromSeg relied on conv3d's implicit zero padding, which pulls the volume border towards 0. That is the one difference here that was wrong rather than merely different.

Everything else is the same arithmetic as the copy it replaces.

Interfaces are unchanged

The local wrappers stay where their callers expect them — _gaussian_blur_3d in fromSeg.py still clamps to [0, 1], _random_bias_field3d in domain_transfer.py still returns a bare [D, H, W] volume rather than [B, C, D, H, W]. This is a change of implementation, not of interface.

Testing

New unit_tests/test_kernels.py (12 tests) covers the merged behaviour and, more usefully, the equivalences that make this one implementation rather than a sixth copy:

  • test_synthseg_and_domain_transfer_now_share_one_implementation — same seed, same field
  • test_the_kernels_are_shared_by_both_backends — the CPU transform hands out the shared tables
  • test_a_constant_volume_survives_the_blur — the reflect-padding property that the zero-padded copy failed

test_kernel_correctness.py from #58 follows the Gaussian to its new home; its 1-D assertions move onto the sigma-derived gaussian_kernel1d, since the fixed-size helper is now internal to gaussian_kernel3d.

Net: −209 / +56 across the five modules, plus the 265-line shared module.

Full suite: 82 passed, 169 subtests. ruff and mypy clean.

Four independent 3D Gaussian implementations, three bias fields and two copies of
the Laplace/Scharr constant tables lived in five modules: gpu/contrast.py,
gpu/fromSeg.py, gpu/domain_transfer.py, synthseg/functional.py, cpu/contrast.py.
Each carried some version of a comment saying it was kept local so its module
would stay self-contained.

They were not equivalent, and that is what let two of them go wrong unnoticed --
the uncentred Gaussian and the malformed 2D Scharr x-kernel, both corrected
earlier in this series. Each had to be fixed on its own because there was no
shared implementation to fix instead. smauglab/transforms/kernels.py is that
implementation now.

The consolidation is not bit-for-bit at the two blur call sites, deliberately:

* Radius is ceil(3*sigma). domain_transfer and fromSeg used round(3*sigma), which
  is never wider, so their kernels may be one tap larger.
* Padding is reflect everywhere. domain_transfer used replicate; fromSeg relied on
  conv3d's implicit zero padding, which pulls the volume border towards zero. That
  is the one difference here that was wrong rather than merely different.

Everything else is the same arithmetic as the copy it replaces: the dense
Gaussian, both derivative tables, and the bias field, whose synthseg and
domain_transfer copies were already line-for-line identical.

The local wrappers stay where callers expect them -- _gaussian_blur_3d in
fromSeg.py still clamps to [0, 1], _random_bias_field3d in domain_transfer.py
still returns a bare volume rather than [B, C, D, H, W] -- so this is a change of
implementation, not of interface.

unit_tests/test_kernels.py covers the merged behaviour and the equivalences that
make it one implementation: that synthseg and domain_transfer now produce the same
bias field from the same seed, and that the CPU transform hands out the shared
tables. test_kernel_correctness.py follows the Gaussian to its new home.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings August 20, 2026 12:08

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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