Functional implementation of affine. This function operates eagerly or lazily according to ``lazy`` (default ``False``). Args: img: data to be changed, assuming `img` is channel-first. affine: the affine transformation to be applied, it can be a 3x3 or 4x4 matrix. T
(
img, affine, grid, resampler, sp_size, mode, padding_mode, do_resampling, image_only, lazy, transform_info
)
| 588 | |
| 589 | |
| 590 | def affine_func( |
| 591 | img, affine, grid, resampler, sp_size, mode, padding_mode, do_resampling, image_only, lazy, transform_info |
| 592 | ): |
| 593 | """ |
| 594 | Functional implementation of affine. |
| 595 | This function operates eagerly or lazily according to |
| 596 | ``lazy`` (default ``False``). |
| 597 | |
| 598 | Args: |
| 599 | img: data to be changed, assuming `img` is channel-first. |
| 600 | affine: the affine transformation to be applied, it can be a 3x3 or 4x4 matrix. This should be defined |
| 601 | for the voxel space spatial centers (``float(size - 1)/2``). |
| 602 | grid: used in non-lazy mode to pre-compute the grid to do the resampling. |
| 603 | resampler: the resampler function, see also: :py:class:`monai.transforms.Resample`. |
| 604 | sp_size: output image spatial size. |
| 605 | mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers). |
| 606 | Interpolation mode to calculate output values. |
| 607 | See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html |
| 608 | When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used |
| 609 | and the value represents the order of the spline interpolation. |
| 610 | See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html |
| 611 | padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``} |
| 612 | Padding mode for outside grid values. |
| 613 | See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html |
| 614 | When `mode` is an integer, using numpy/cupy backends, this argument accepts |
| 615 | {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}. |
| 616 | See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html |
| 617 | do_resampling: whether to do the resampling, this is a flag for the use case of updating metadata but |
| 618 | skipping the actual (potentially heavy) resampling operation. |
| 619 | image_only: if True return only the image volume, otherwise return (image, affine). |
| 620 | lazy: a flag that indicates whether the operation should be performed lazily or not |
| 621 | transform_info: a dictionary with the relevant information pertaining to an applied transform. |
| 622 | |
| 623 | """ |
| 624 | |
| 625 | # resampler should carry the align_corners and type info |
| 626 | img_size = img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:] |
| 627 | rank = img.peek_pending_rank() if isinstance(img, MetaTensor) else torch.tensor(3.0, dtype=torch.double) |
| 628 | extra_info = { |
| 629 | "affine": affine, |
| 630 | "mode": mode, |
| 631 | "padding_mode": padding_mode, |
| 632 | "do_resampling": do_resampling, |
| 633 | "align_corners": resampler.align_corners, |
| 634 | } |
| 635 | affine = monai.transforms.Affine.compute_w_affine(rank, affine, img_size, sp_size) |
| 636 | meta_info = TraceableTransform.track_transform_meta( |
| 637 | img, |
| 638 | sp_size=sp_size, |
| 639 | affine=affine, |
| 640 | extra_info=extra_info, |
| 641 | orig_size=img_size, |
| 642 | transform_info=transform_info, |
| 643 | lazy=lazy, |
| 644 | ) |
| 645 | if lazy: |
| 646 | out = _maybe_new_metatensor(img) |
| 647 | out = out.copy_meta_from(meta_info) if isinstance(out, MetaTensor) else meta_info |
no test coverage detected
searching dependent graphs…