Skip to content

Fix resampling bug and add tests - #52

Merged
cclaess merged 1 commit into
mainfrom
dev
Jul 17, 2026
Merged

Fix resampling bug and add tests#52
cclaess merged 1 commit into
mainfrom
dev

Conversation

@cclaess

@cclaess cclaess commented Jul 17, 2026

Copy link
Copy Markdown
Owner

This pull request improves the resampling logic in spectre.io to correctly handle changes in the MONAI library and adds comprehensive tests to cover resampling and the HuggingFace transformers integration. The main focus is on fixing a regression where an outdated argument was passed to Spacing, ensuring correct affine handling, and adding tests to prevent future regressions.

Resampling logic fixes and improvements:

  • Updated the resample function in src/spectre/io.py to use MetaTensor for passing affine information, as the latest MONAI expects affine data on the tensor rather than as a function argument. This fixes a TypeError caused by passing an unsupported affine argument to Spacing. [1] [2]

Test coverage:

  • Added tests/test_io_resample.py to provide thorough coverage of CT scan resampling, including shape/spacing checks, error handling, and integration with windowing and cropping. This ensures the resampling path is robust and correct, especially after the recent API change in MONAI.
  • Added tests/test_hf_modeling.py to test the HuggingFace transformers wrapper around the SpectreImageFeatureExtractor, verifying call contracts, output types, error handling for unsupported kwargs, and the correct exposure of model attributes.

Copilot AI review requested due to automatic review settings July 17, 2026 15:35
@cclaess
cclaess merged commit eb3553d into main Jul 17, 2026
1 check passed

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 updates spectre.io resampling to be compatible with newer MONAI behavior (affine carried on a MetaTensor rather than passed to Spacing) and adds tests to prevent regressions in both CT resampling and the HuggingFace transformers wrapper.

Changes:

  • Fix resample() to pass affine via monai.data.MetaTensor instead of the removed Spacing(..., affine=...) argument.
  • Add end-to-end tests for NIfTI load/resample and the load_and_window(..., spacing=...) path.
  • Add tests covering the transformers wrapper call contract (ignored kwargs, refused output flags, return types).

Reviewed changes

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

File Description
src/spectre/io.py Updates resampling implementation to use MetaTensor affine propagation for MONAI compatibility.
tests/test_io_resample.py Adds regression + integration tests for CT load/window/resample behavior.
tests/test_hf_modeling.py Adds tests for the HuggingFace wrapper’s forward contract and error handling.

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

Comment thread src/spectre/io.py
Comment on lines +164 to 168
volume = MetaTensor(x, affine=torch.as_tensor(meta.affine, dtype=torch.float64))
spacer = Spacing(pixdim=tuple(float(s) for s in spacing), mode=mode)
resampled = spacer(x, affine=torch.as_tensor(meta.affine, dtype=torch.float64))
resampled = spacer(volume)
affine = getattr(resampled, "affine", meta.affine)
resampled = resampled.as_tensor() if hasattr(resampled, "as_tensor") else torch.as_tensor(resampled)
Comment thread tests/test_io_resample.py
Comment on lines +43 to +45
assert new_meta.spacing == (0.5, 0.5, 1.0)
# Finer spacing on two axes -> more voxels; coarser depth spacing (1.0 < 1.5) -> more too.
assert resampled.shape[1] > volume.shape[1]
Comment thread tests/test_hf_modeling.py
Comment on lines +26 to +30
spec = importlib.util.spec_from_file_location(name, ROOT / "hf_export" / f"{name}.py")
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
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