Args: img: shape must be (num_channels, H, W[, D]). distort_steps: This argument is a list of tuples, where each tuple contains the distort steps of the corresponding dimensions (in the order of H, W[, D]). The length of each tuple equals to `num_cell
(
self,
img: torch.Tensor,
distort_steps: Sequence[Sequence] | None = None,
mode: str | None = None,
padding_mode: str | None = None,
)
| 3028 | self.device = device |
| 3029 | |
| 3030 | def __call__( |
| 3031 | self, |
| 3032 | img: torch.Tensor, |
| 3033 | distort_steps: Sequence[Sequence] | None = None, |
| 3034 | mode: str | None = None, |
| 3035 | padding_mode: str | None = None, |
| 3036 | ) -> torch.Tensor: |
| 3037 | """ |
| 3038 | Args: |
| 3039 | img: shape must be (num_channels, H, W[, D]). |
| 3040 | distort_steps: This argument is a list of tuples, where each tuple contains the distort steps of the |
| 3041 | corresponding dimensions (in the order of H, W[, D]). The length of each tuple equals to `num_cells + 1`. |
| 3042 | Each value in the tuple represents the distort step of the related cell. |
| 3043 | mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers). |
| 3044 | Interpolation mode to calculate output values. Defaults to ``self.mode``. |
| 3045 | See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html |
| 3046 | When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used |
| 3047 | and the value represents the order of the spline interpolation. |
| 3048 | See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html |
| 3049 | padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``} |
| 3050 | Padding mode for outside grid values. Defaults to ``self.padding_mode``. |
| 3051 | See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html |
| 3052 | When `mode` is an integer, using numpy/cupy backends, this argument accepts |
| 3053 | {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}. |
| 3054 | See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html |
| 3055 | |
| 3056 | """ |
| 3057 | distort_steps = self.distort_steps if distort_steps is None else distort_steps |
| 3058 | if len(img.shape) != len(distort_steps) + 1: |
| 3059 | raise ValueError("the spatial size of `img` does not match with the length of `distort_steps`") |
| 3060 | |
| 3061 | all_ranges = [] |
| 3062 | num_cells = ensure_tuple_rep(self.num_cells, len(img.shape) - 1) |
| 3063 | if isinstance(img, MetaTensor) and img.pending_operations: |
| 3064 | warnings.warn("MetaTensor img has pending operations, transform may return incorrect results.") |
| 3065 | for dim_idx, dim_size in enumerate(img.shape[1:]): |
| 3066 | dim_distort_steps = distort_steps[dim_idx] |
| 3067 | ranges = torch.zeros(dim_size, dtype=torch.float32) |
| 3068 | cell_size = dim_size // num_cells[dim_idx] |
| 3069 | prev = 0 |
| 3070 | for idx in range(num_cells[dim_idx] + 1): |
| 3071 | start = int(idx * cell_size) |
| 3072 | end = start + cell_size |
| 3073 | if end > dim_size: |
| 3074 | end = dim_size |
| 3075 | cur = dim_size |
| 3076 | else: |
| 3077 | cur = prev + cell_size * dim_distort_steps[idx] |
| 3078 | ranges[start:end] = torch.linspace(prev, cur, end - start) |
| 3079 | prev = cur |
| 3080 | ranges = ranges - (dim_size - 1.0) / 2.0 |
| 3081 | all_ranges.append(ranges) |
| 3082 | |
| 3083 | coords = meshgrid_ij(*all_ranges) |
| 3084 | grid = torch.stack([*coords, torch.ones_like(coords[0])]) |
| 3085 | |
| 3086 | return self.resampler(img, grid=grid, mode=mode, padding_mode=padding_mode) |
| 3087 |
nothing calls this directly
no test coverage detected