Skip to content

CropOrPad self.bounds_parameters is set only for first image. #757

Closed
@ramonemiliani93

Description

Is there an existing issue for this?

  • I have searched the existing issues

Bug summary

When using a mask without target shape CropOrPad will compute the target shape only for the first image used in the transform. The following images will have an incorrect output shape.

Code for reproduction

import numpy as np
import torchio as tio

shape = (1, 5, 5, 5)

mask_a = np.zeros(shape)
mask_a[0, 2, 2, 2] = 1

mask_b = np.zeros(shape)
mask_b[0, 1:4, 1:4, 1:4] = 1

image_a = tio.ScalarImage(tensor=np.ones(shape), affine=np.eye(4))
mask_a = tio.LabelMap(tensor=mask_a, affine=np.eye(4))
subject_a = tio.Subject(image=image_a, mask=mask_a)

image_b = tio.ScalarImage(tensor=np.ones(shape), affine=np.eye(4))
mask_b = tio.LabelMap(tensor=mask_b, affine=np.eye(4))
subject_b = tio.Subject(image=image_b, mask=mask_b)

# Works
shape_a = tio.CropOrPad(mask_name="mask")(subject_a).image.shape
shape_b = tio.CropOrPad(mask_name="mask")(subject_b).image.shape
assert shape_a != shape_b

# Fails
crop = tio.CropOrPad(mask_name="mask")
assert crop(subject_a).image.shape != crop(subject_b).image.shape

Actual outcome

The transform outputs an image of shape (1, 1, 1, 1) for subject_a and of (1, 1, 1, 1) for subject_b.

Error messages

No response

Expected outcome

The transform should output an image of shape (1, 1, 1, 1) for subject_a and of (1, 3, 3, 3) for subject_b.

System info

Platform:   macOS-10.16-x86_64-i386-64bit
TorchIO:    0.18.62
PyTorch:    1.10.0
SimpleITK:  2.1.1 (ITK 5.2)
NumPy:      1.20.3
Python:     3.8.11 (default, Aug  6 2021, 08:56:27) 
[Clang 10.0.0 ]

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions