Repository: Project-MONAI/MONAI
File: monai/apps/deepgrow/transforms.py (Python)

Code before:
                box_start = meta_dict[self.start_coord_key]
                box_end = meta_dict[self.end_coord_key]

                spatial_dims = min(len(box_start), len(image.shape[1:]))
                slices = tuple(
                    [slice(None)] + [slice(s, e) for s, e in zip(box_start[:spatial_dims], box_end[:spatial_dims])]
                )
                result[slices] = image
            else:
                result = image

            # Undo Spacing
            if self.restore_spacing:
                current_size = result.shape[1:]
                # change spatial_shape from HWD to DHW
                spatial_shape = list(np.roll(meta_dict["spatial_shape"], 1))
                spatial_size = spatial_shape[-len(current_size) :]

                if np.any(np.not_equal(current_size, spatial_size)):
                    resizer = Resize(spatial_size=spatial_size, mode=mode)
                    result = resizer(result, mode=mode, align_corners=align_corners)  # type: ignore

            # Undo Slicing
            slice_idx = meta_dict.get("slice_idx")
            final_result: NdarrayOrTensor
            if self.restore_slicing == False:
                final_result = result
            elif slice_idx is None or self.slice_only:
                final_result = result if len(result.shape) <= 3 else result[0]
            else:
                slice_idx = meta_dict["slice_idx"][0]
                final_result = np.zeros(tuple(spatial_shape))
                final_result[slice_idx] = result
            d[key] = final_result

            meta_key = meta_key or f"{key}_{self.meta_key_postfix}"
            meta = d.get(meta_key)
            if meta is None:
                meta = dict()
                d[meta_key] = meta
            meta["slice_idx"] = slice_idx
            meta["affine"] = meta_dict["original_affine"]
        return d


class Fetch2DSliced(MapTransform):
    """
    Fetch one slice in case of a 3D volume.

    The volume only contains spatial coordinates.


Review comment (suggestion):
```suggestion
            if not self.restore_slicing:  # do nothing if restore slicing isn't requested
```

Code after:
                if np.any(np.not_equal(current_shape, cropped_shape)):
                    resizer = Resize(spatial_size=cropped_shape[1:], mode=mode)
                    image = resizer(image, mode=mode, align_corners=align_corners)

            # Undo Crop
            if self.restore_cropping:
                original_shape = meta_dict[self.original_shape_key]
                result = np.zeros(original_shape, dtype=np.float32)
                box_start = meta_dict[self.start_coord_key]
                box_end = meta_dict[self.end_coord_key]

                spatial_dims = min(len(box_start), len(image.shape[1:]))
                slices = tuple(
                    [slice(None)] + [slice(s, e) for s, e in zip(box_start[:spatial_dims], box_end[:spatial_dims])]
                )
                result[slices] = image
            else:
                result = image

            # Undo Spacing
            if self.restore_spacing:
                current_size = result.shape[1:]
                # change spatial_shape from HWD to DHW
                spatial_shape = list(np.roll(meta_dict["spatial_shape"], 1))
                spatial_size = spatial_shape[-len(current_size) :]

                if np.any(np.not_equal(current_size, spatial_size)):
                    resizer = Resize(spatial_size=spatial_size, mode=mode)
                    result = resizer(result, mode=mode, align_corners=align_corners)  # type: ignore

            # Undo Slicing
            slice_idx = meta_dict.get("slice_idx")
            final_result: NdarrayOrTensor
            if not self.restore_slicing:  # do nothing if restore slicing isn't requested
                final_result = result
            elif slice_idx is None or self.slice_only:
                final_result = result if len(result.shape) <= 3 else result[0]
            else:
                slice_idx = meta_dict["slice_idx"][0]
                final_result = np.zeros(tuple(spatial_shape))
                final_result[slice_idx] = result
            d[key] = final_result

            meta_key = meta_key or f"{key}_{self.meta_key_postfix}"
            meta = d.get(meta_key)
            if meta is None:
                meta = dict()
                d[meta_key] = meta
            meta["slice_idx"] = slice_idx
            meta["affine"] = meta_dict["original_affine"]
        return d