Functional implementation of flip. This function operates eagerly or lazily according to ``lazy`` (default ``False``). Args: img: data to be changed, assuming `img` is channel-first. sp_axes: spatial axes along which to flip over. If None, will flip over
(img, sp_axes, lazy, transform_info)
| 273 | |
| 274 | |
| 275 | def flip(img, sp_axes, lazy, transform_info): |
| 276 | """ |
| 277 | Functional implementation of flip. |
| 278 | This function operates eagerly or lazily according to |
| 279 | ``lazy`` (default ``False``). |
| 280 | |
| 281 | Args: |
| 282 | img: data to be changed, assuming `img` is channel-first. |
| 283 | sp_axes: spatial axes along which to flip over. |
| 284 | If None, will flip over all of the axes of the input array. |
| 285 | If axis is negative it counts from the last to the first axis. |
| 286 | If axis is a tuple of ints, flipping is performed on all of the axes |
| 287 | specified in the tuple. |
| 288 | lazy: a flag that indicates whether the operation should be performed lazily or not |
| 289 | transform_info: a dictionary with the relevant information pertaining to an applied transform. |
| 290 | """ |
| 291 | sp_size = img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:] |
| 292 | sp_size = convert_to_numpy(sp_size, wrap_sequence=True).tolist() |
| 293 | extra_info = {"axes": sp_axes} # track the spatial axes |
| 294 | axes = monai.transforms.utils.map_spatial_axes(img.ndim, sp_axes) # use the axes with channel dim |
| 295 | rank = img.peek_pending_rank() if isinstance(img, MetaTensor) else torch.tensor(3.0, dtype=torch.double) |
| 296 | # axes include the channel dim |
| 297 | xform = torch.eye(int(rank) + 1, dtype=torch.double) |
| 298 | for axis in axes: |
| 299 | sp = axis - 1 |
| 300 | xform[sp, sp], xform[sp, -1] = xform[sp, sp] * -1, sp_size[sp] - 1 |
| 301 | meta_info = TraceableTransform.track_transform_meta( |
| 302 | img, sp_size=sp_size, affine=xform, extra_info=extra_info, transform_info=transform_info, lazy=lazy |
| 303 | ) |
| 304 | out = _maybe_new_metatensor(img) |
| 305 | if lazy: |
| 306 | return out.copy_meta_from(meta_info) if isinstance(out, MetaTensor) else meta_info |
| 307 | out = torch.flip(out, axes) |
| 308 | return out.copy_meta_from(meta_info) if isinstance(out, MetaTensor) else out |
| 309 | |
| 310 | |
| 311 | def resize( |
searching dependent graphs…