# Buggy code
def apply_transform(self, sample: Subject) -> dict:
    sample.check_consistent_shape()
    params = self.get_params(
        self.scales,
        self.degrees,
        self.translation,
        self.isotropic,
    )
    scaling_params, rotation_params, translation_params = params
    for image in self.get_images(sample):
        if image[TYPE] != INTENSITY:
            interpolation = Interpolation.NEAREST
        else:
            interpolation = self.interpolation

        if image.is_2d():
            scaling_params[0] = 1
            rotation_params[-2:] = 0

        if self.use_image_center:
            center = image.get_center(lps=True)
        else:
            center = None

        transformed_tensors = []
        for tensor in image[DATA]:
            transformed_tensor = self.apply_affine_transform(
                tensor,
                image[AFFINE],
                scaling_params.tolist(),
                rotation_params.tolist(),
                translation_params.tolist(),
                interpolation,
                center_lps=center,
            )
            transformed_tensors.append(transformed_tensor)
        image[DATA] = torch.stack(transformed_tensors)
    random_parameters_dict = {
        "scaling": scaling_params,
        "rotation": rotation_params,
        "translation": translation_params,
    }
    sample.add_transform(self, random_parameters_dict)
    return sample


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def apply_transform(self, sample: Subject) -> dict:
    sample.check_consistent_spatial_shape()
    params = self.get_params(
        self.scales,
        self.degrees,
        self.translation,
        self.isotropic,
    )
    scaling_params, rotation_params, translation_params = params
    for image in self.get_images(sample):
        if image[TYPE] != INTENSITY:
            interpolation = Interpolation.NEAREST
        else:
            interpolation = self.interpolation

        if image.is_2d():
            scaling_params[0] = 1
            rotation_params[-2:] = 0

        if self.use_image_center:
            center = image.get_center(lps=True)
        else:
            center = None

        transformed_tensors = []
        for tensor in image[DATA]:
            transformed_tensor = self.apply_affine_transform(
                tensor,
                image[AFFINE],
                scaling_params.tolist(),
                rotation_params.tolist(),
                translation_params.tolist(),
                interpolation,
                center_lps=center,
            )
            transformed_tensors.append(transformed_tensor)
        image[DATA] = torch.stack(transformed_tensors)
    random_parameters_dict = {
        "scaling": scaling_params,
        "rotation": rotation_params,
        "translation": translation_params,
    }
    sample.add_transform(self, random_parameters_dict)
    return sample