-
Notifications
You must be signed in to change notification settings - Fork 260
Description
Is there an existing issue for this?
- I have searched the existing issues
Problem summary
Describe the problem
TorchIO spatial transforms (e.g., Crop, Pad, CropOrPad) reconstruct images internally using type(image)(tensor=..., affine=..., type=..., path=...).
If a user-defined subclass of torchio.Image has a different __init__ signature (e.g., requires extra arguments like history), these transforms raise errors and fail.
This issue affects all transforms that reconstruct images, not just Crop.
It prevents users from extending TorchIO Image to store metadata or other extra attributes inside images.
Proposed solution
Introduce an optional hook or method (new_like) that allows subclasses to return a new instance when transforms need to copy/crop/resize the image. Transforms would call this hook if it exists, otherwise fall back to the standard constructor.
Code for reproduction
python
import torchio as tio
import torch
class HistoryScalarImage(tio.ScalarImage):
def __init__(self, tensor, affine, history, **kwargs):
super().__init__(tensor=tensor, affine=affine, **kwargs)
self.history = history
img = HistoryScalarImage(torch.rand(1,10,10,10), affine=torch.eye(4), history=[])
subject = tio.Subject(image=img)
transform = tio.Crop(cropping=2)Actual outcome
transform(subject) # raises TypeError
Error messages
TypeErrorExpected outcome
Transforms should either:
- Support arbitrary
Imagesubclasses by providing a clone / factory hook (new_like()or similar), or - Clearly document that only vanilla TorchIO
Imagesubclasses are supported.
System info
Platform: Linux-6.8.0-85-generic-x86_64-with-glibc2.39
TorchIO: 0.21.0
PyTorch: 2.1.2
SimpleITK: 2.5.2 (ITK 5.4)
NumPy: 1.26.4
Python: 3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:36:39) [GCC 12.3.0]