create a shearing matrix Args: spatial_dims: spatial rank coefs: shearing factors, a tuple of 2 floats for 2D, a tuple of 6 floats for 3D). Individual single-axis shear matrices are composed (multiplied) in coefficient order so that the result is a p
(
spatial_dims: int,
coefs: Sequence[float] | float,
device: torch.device | None = None,
backend=TransformBackends.NUMPY,
)
| 945 | |
| 946 | |
| 947 | def create_shear( |
| 948 | spatial_dims: int, |
| 949 | coefs: Sequence[float] | float, |
| 950 | device: torch.device | None = None, |
| 951 | backend=TransformBackends.NUMPY, |
| 952 | ) -> NdarrayOrTensor: |
| 953 | """ |
| 954 | create a shearing matrix |
| 955 | |
| 956 | Args: |
| 957 | spatial_dims: spatial rank |
| 958 | coefs: shearing factors, a tuple of 2 floats for 2D, a tuple of 6 floats for 3D). |
| 959 | Individual single-axis shear matrices are composed (multiplied) in |
| 960 | coefficient order so that the result is a proper shear with determinant 1. |
| 961 | For 2D with coefs ``(Sx, Sy)`` the composed matrix is:: |
| 962 | |
| 963 | [ |
| 964 | [1.0, Sx, 0.0], |
| 965 | [Sy, 1.0 + Sx*Sy, 0.0], |
| 966 | [0.0, 0.0, 1.0], |
| 967 | ] |
| 968 | |
| 969 | device: device to compute and store the output (when the backend is "torch"). |
| 970 | backend: APIs to use, ``numpy`` or ``torch``. |
| 971 | |
| 972 | Raises: |
| 973 | NotImplementedError: When ``spatial_dims`` is not one of [2, 3]. |
| 974 | |
| 975 | """ |
| 976 | _backend = look_up_option(backend, TransformBackends) |
| 977 | if _backend == TransformBackends.NUMPY: |
| 978 | return _create_shear(spatial_dims=spatial_dims, coefs=coefs, eye_func=np.eye) |
| 979 | if _backend == TransformBackends.TORCH: |
| 980 | return _create_shear( |
| 981 | spatial_dims=spatial_dims, coefs=coefs, eye_func=lambda rank: torch.eye(rank, device=device) |
| 982 | ) |
| 983 | raise ValueError(f"backend {backend} is not supported") |
| 984 | |
| 985 | |
| 986 | def _create_shear(spatial_dims: int, coefs: Sequence[float] | float, eye_func=np.eye) -> NdarrayOrTensor: |
searching dependent graphs…