From 89a72349b729d36d9dbc55e9beebe3e790b94e72 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 4 Aug 2021 11:27:14 +0800 Subject: [PATCH 1/4] [DLMED] update doc-strings of Affine transforms Signed-off-by: Nic Ma --- monai/transforms/spatial/array.py | 141 +++++++++++++++++-------- monai/transforms/spatial/dictionary.py | 75 +++++++++---- monai/transforms/utils.py | 13 ++- 3 files changed, 161 insertions(+), 68 deletions(-) diff --git a/monai/transforms/spatial/array.py b/monai/transforms/spatial/array.py index d9c10cf9c0..a562eaa50c 100644 --- a/monai/transforms/spatial/array.py +++ b/monai/transforms/spatial/array.py @@ -944,25 +944,24 @@ def __call__( class AffineGrid(Transform): """ Affine transforms on the coordinates. + More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. Args: - rotate_params: angle range in radians. rotate_params[0] with be used to generate the 1st rotation - parameter from `uniform[-rotate_params[0], rotate_params[0])`. Similarly, `rotate_params[1]` and - `rotate_params[2]` are used in 3D affine for the range of 2nd and 3rd axes. - shear_params: shear_params[0] with be used to generate the 1st shearing parameter from - `uniform[-shear_params[0], shear_params[0])`. Similarly, `shear_params[1]` to - `shear_params[N]` controls the range of the uniform distribution used to generate the 2nd to - N-th parameter. - translate_params : translate_params[0] with be used to generate the 1st shift parameter from - `uniform[-translate_params[0], translate_params[0])`. Similarly, `translate_params[1]` - to `translate_params[N]` controls the range of the uniform distribution used to generate - the 2nd to N-th parameter. - scale_params: scale_params[0] with be used to generate the 1st scaling factor from - `uniform[-scale_params[0], scale_params[0]) + 1.0`. Similarly, `scale_params[1]` to - `scale_params[N]` controls the range of the uniform distribution used to generate the 2nd to - N-th parameter. - as_tensor_output: whether to output tensor instead of numpy array. - defaults to True. + rotate_params: a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. + Defaults to no rotation. + shear_params: shearing factors for affine matrix, take a 3D affine as example:: + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. + translate_params: a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Translation is in + pixel/voxel relative to the center of the input image. Defaults to no translation. + scale_params: scale factor for every spatial dims. a tuple of 2 floats for 2D, + a tuple of 3 floats for 3D. Defaults to `1.0`(no scaling). + as_tensor_output: whether to output tensor instead of numpy array, defaults to True. device: device to store the output grid data. affine: If applied, ignore the params (`rotate_params`, etc.) and use the supplied matrix. Should be square with each side = num of image spatial @@ -1041,6 +1040,7 @@ def __call__( class RandAffineGrid(Randomizable, Transform): """ Generate randomised affine grid. + More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. """ def __init__( @@ -1054,16 +1054,26 @@ def __init__( ) -> None: """ Args: - rotate_range: angle range in radians. If element `i` is iterable, then + rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the ith dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can + for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 and nothing for the remaining dimensions. - shear_range: shear_range with format matching `rotate_range`. - translate_range: translate_range with format matching `rotate_range`. - scale_range: scaling_range with format matching `rotate_range`. A value of 1.0 is added to the result. - This allows 0 to correspond to no change (i.e., a scaling of 1). + shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select + shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix, + take a 3D affine as example:: + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly + select voxels to translate for every spatial dims. + scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select + the scale factor to translate for every spatial dims. A value of 1.0 is added to the result. + This allows 0 to correspond to no change (i.e., a scaling of 1.0). as_tensor_output: whether to output tensor instead of numpy array. defaults to True. device: device to store the output grid data. @@ -1284,6 +1294,8 @@ def __call__( class Affine(Transform): """ Transform ``img`` given the affine parameters. + More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. + """ def __init__( @@ -1305,10 +1317,18 @@ def __init__( Args: rotate_params: a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. Defaults to no rotation. - shear_params: a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. + shear_params: shearing factors for affine matrix, take a 3D affine as example:: + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. translate_params: a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Translation is in pixel/voxel relative to the center of the input image. Defaults to no translation. - scale_params: a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Defaults to no scaling. + scale_params: scale factor for every spatial dims. a tuple of 2 floats for 2D, + a tuple of 3 floats for 3D. Defaults to `1.0`(no scaling). spatial_size: output image spatial size. if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1, the transform will use the spatial size of `img`. @@ -1372,6 +1392,8 @@ def __call__( class RandAffine(RandomizableTransform): """ Random affine transform. + More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. + """ def __init__( @@ -1392,16 +1414,26 @@ def __init__( Args: prob: probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid. - rotate_range: angle range in radians. If element `i` is iterable, then + rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the ith dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can + for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 and nothing for the remaining dimensions. - shear_range: shear_range with format matching `rotate_range`. - translate_range: translate_range with format matching `rotate_range`. - scale_range: scaling_range with format matching `rotate_range`. A value of 1.0 is added to the result. - This allows 0 to correspond to no change (i.e., a scaling of 1). + shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select + shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix, + take a 3D affine as example:: + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly + select pixel/voxel to translate for every spatial dims. + scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select + the scale factor to translate for every spatial dims. A value of 1.0 is added to the result. + This allows 0 to correspond to no change (i.e., a scaling of 1.0). spatial_size: output image spatial size. if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1, the transform will use the spatial size of `img`. @@ -1530,7 +1562,8 @@ def __call__( class Rand2DElastic(RandomizableTransform): """ - Random elastic deformation and affine in 2D + Random elastic deformation and affine in 2D. + More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. """ def __init__( @@ -1555,16 +1588,24 @@ def __init__( prob: probability of returning a randomized elastic transform. defaults to 0.1, with 10% chance returns a randomized elastic transform, otherwise returns a ``spatial_size`` centered area extracted from the input image. - rotate_range: angle range in radians. If element `i` is iterable, then + rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the ith dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can + for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 and nothing for the remaining dimensions. - shear_range: shear_range with format matching `rotate_range`. - translate_range: translate_range with format matching `rotate_range`. - scale_range: scaling_range with format matching `rotate_range`. A value of 1.0 is added to the result. - This allows 0 to correspond to no change (i.e., a scaling of 1). + shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select + shearing factors(a tuple of 2 floats for 2D) for affine matrix, take a 2D affine as example:: + [ + [1.0, params[0], 0.0], + [params[1], 1.0, 0.0], + [0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly + select pixel to translate for every spatial dims. + scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select + the scale factor to translate for every spatial dims. A value of 1.0 is added to the result. + This allows 0 to correspond to no change (i.e., a scaling of 1.0). spatial_size: specifying output image spatial size [h, w]. if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1, the transform will use the spatial size of `img`. @@ -1656,7 +1697,8 @@ def __call__( class Rand3DElastic(RandomizableTransform): """ - Random elastic deformation and affine in 3D + Random elastic deformation and affine in 3D. + More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. """ def __init__( @@ -1683,16 +1725,25 @@ def __init__( prob: probability of returning a randomized elastic transform. defaults to 0.1, with 10% chance returns a randomized elastic transform, otherwise returns a ``spatial_size`` centered area extracted from the input image. - rotate_range: angle range in radians. If element `i` is iterable, then + rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the ith dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can + for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 and nothing for the remaining dimensions. - shear_range: shear_range with format matching `rotate_range`. - translate_range: translate_range with format matching `rotate_range`. - scale_range: scaling_range with format matching `rotate_range`. A value of 1.0 is added to the result. - This allows 0 to correspond to no change (i.e., a scaling of 1). + shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select + shearing factors(a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly + select voxel to translate for every spatial dims. + scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select + the scale factor to translate for every spatial dims. A value of 1.0 is added to the result. + This allows 0 to correspond to no change (i.e., a scaling of 1.0). spatial_size: specifying output image spatial size [h, w, d]. if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1, the transform will use the spatial size of `img`. diff --git a/monai/transforms/spatial/dictionary.py b/monai/transforms/spatial/dictionary.py index 0d65fdfa29..00844b35dd 100644 --- a/monai/transforms/spatial/dictionary.py +++ b/monai/transforms/spatial/dictionary.py @@ -592,10 +592,18 @@ def __init__( keys: keys of the corresponding items to be transformed. rotate_params: a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. Defaults to no rotation. - shear_params: a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. + shear_params: shearing factors for affine matrix, take a 3D affine as example:: + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. translate_params: a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Translation is in pixel/voxel relative to the center of the input image. Defaults to no translation. - scale_params: a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Defaults to no scaling. + scale_params: scale factor for every spatial dims. a tuple of 2 floats for 2D, + a tuple of 3 floats for 3D. Defaults to `1.0`(no scaling). spatial_size: output image spatial size. if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1, the transform will use the spatial size of `img`. @@ -710,16 +718,26 @@ def __init__( to `(32, 64)` if the second spatial dimension size of img is `64`. prob: probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid. - rotate_range: angle range in radians. If element `i` is iterable, then + rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the ith dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can + for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 and nothing for the remaining dimensions. - shear_range: shear_range with format matching `rotate_range`. - translate_range: translate_range with format matching `rotate_range`. - scale_range: scaling_range with format matching `rotate_range`. A value of 1.0 is added to the result. - This allows 0 to correspond to no change (i.e., a scaling of 1). + shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select + shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix, + take a 3D affine as example:: + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly + select pixel/voxel to translate for every spatial dims. + scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select + the scale factor to translate for every spatial dims. A value of 1.0 is added to the result. + This allows 0 to correspond to no change (i.e., a scaling of 1.0). mode: {``"bilinear"``, ``"nearest"``} Interpolation mode to calculate output values. Defaults to ``"bilinear"``. See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample @@ -876,16 +894,24 @@ def __init__( prob: probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid, otherwise returns a ``spatial_size`` centered area extracted from the input image. - rotate_range: angle range in radians. If element `i` is iterable, then + rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the ith dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can + for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 and nothing for the remaining dimensions. - shear_range: shear_range with format matching `rotate_range`. - translate_range: translate_range with format matching `rotate_range`. - scale_range: scaling_range with format matching `rotate_range`. A value of 1.0 is added to the result. - This allows 0 to correspond to no change (i.e., a scaling of 1). + shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select + shearing factors(a tuple of 2 floats for 2D) for affine matrix, take a 2D affine as example:: + [ + [1.0, params[0], 0.0], + [params[1], 1.0, 0.0], + [0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly + select pixel to translate for every spatial dims. + scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select + the scale factor to translate for every spatial dims. A value of 1.0 is added to the result. + This allows 0 to correspond to no change (i.e., a scaling of 1.0). mode: {``"bilinear"``, ``"nearest"``} Interpolation mode to calculate output values. Defaults to ``"bilinear"``. See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample @@ -996,16 +1022,25 @@ def __init__( prob: probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid, otherwise returns a ``spatial_size`` centered area extracted from the input image. - rotate_range: angle range in radians. If element `i` is iterable, then + rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the ith dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can + for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 and nothing for the remaining dimensions. - shear_range: shear_range with format matching `rotate_range`. - translate_range: translate_range with format matching `rotate_range`. - scale_range: scaling_range with format matching `rotate_range`. A value of 1.0 is added to the result. - This allows 0 to correspond to no change (i.e., a scaling of 1). + shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select + shearing factors(a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly + select voxel to translate for every spatial dims. + scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select + the scale factor to translate for every spatial dims. A value of 1.0 is added to the result. + This allows 0 to correspond to no change (i.e., a scaling of 1.0). mode: {``"bilinear"``, ``"nearest"``} Interpolation mode to calculate output values. Defaults to ``"bilinear"``. See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index 2da7b688cb..4f163b7d4c 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -607,7 +607,14 @@ def create_shear(spatial_dims: int, coefs: Union[Sequence[float], float]) -> np. Args: spatial_dims: spatial rank - coefs: shearing factors, defaults to 0. + coefs: shearing factors, a tuple of 2 floats for 2D, a tuple of 6 floats for 3D), + take a 3D affine as example:: + [ + [1.0, coefs[0], coefs[1], 0.0], + [coefs[2], 1.0, coefs[3], 0.0], + [coefs[4], coefs[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] Raises: NotImplementedError: When ``spatial_dims`` is not one of [2, 3]. @@ -635,7 +642,7 @@ def create_scale(spatial_dims: int, scaling_factor: Union[Sequence[float], float Args: spatial_dims: spatial rank - scaling_factor: scaling factors, defaults to 1. + scaling_factor: scaling factors for every spatial dim, defaults to 1. """ scaling_factor = ensure_tuple_size(scaling_factor, dim=spatial_dims, pad_val=1.0) return np.diag(scaling_factor[:spatial_dims] + (1.0,)) @@ -647,7 +654,7 @@ def create_translate(spatial_dims: int, shift: Union[Sequence[float], float]) -> Args: spatial_dims: spatial rank - shift: translate factors, defaults to 0. + shift: translate pixel/voxel for every spatial dim, defaults to 0. """ shift = ensure_tuple(shift) affine = np.eye(spatial_dims + 1) From bfc457fcd62b63a79e510a1cfd926b9499d790b3 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 4 Aug 2021 12:01:56 +0800 Subject: [PATCH 2/4] [DLMED] fix format issue Signed-off-by: Nic Ma --- monai/transforms/spatial/array.py | 90 +++++++++++++++----------- monai/transforms/spatial/dictionary.py | 58 ++++++++++------- monai/transforms/utils.py | 13 ++-- 3 files changed, 91 insertions(+), 70 deletions(-) diff --git a/monai/transforms/spatial/array.py b/monai/transforms/spatial/array.py index a562eaa50c..f51b31087c 100644 --- a/monai/transforms/spatial/array.py +++ b/monai/transforms/spatial/array.py @@ -950,17 +950,19 @@ class AffineGrid(Transform): rotate_params: a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. Defaults to no rotation. shear_params: shearing factors for affine matrix, take a 3D affine as example:: - [ - [1.0, params[0], params[1], 0.0], - [params[2], 1.0, params[3], 0.0], - [params[4], params[5], 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - ] - a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. + + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + + a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. translate_params: a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Translation is in pixel/voxel relative to the center of the input image. Defaults to no translation. scale_params: scale factor for every spatial dims. a tuple of 2 floats for 2D, - a tuple of 3 floats for 3D. Defaults to `1.0`(no scaling). + a tuple of 3 floats for 3D. Defaults to `1.0`. as_tensor_output: whether to output tensor instead of numpy array, defaults to True. device: device to store the output grid data. affine: If applied, ignore the params (`rotate_params`, etc.) and use the @@ -1063,12 +1065,14 @@ def __init__( shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: - [ - [1.0, params[0], params[1], 0.0], - [params[2], 1.0, params[3], 0.0], - [params[4], params[5], 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - ] + + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly select voxels to translate for every spatial dims. scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select @@ -1318,17 +1322,19 @@ def __init__( rotate_params: a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. Defaults to no rotation. shear_params: shearing factors for affine matrix, take a 3D affine as example:: - [ - [1.0, params[0], params[1], 0.0], - [params[2], 1.0, params[3], 0.0], - [params[4], params[5], 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - ] - a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. + + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + + a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. translate_params: a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Translation is in pixel/voxel relative to the center of the input image. Defaults to no translation. scale_params: scale factor for every spatial dims. a tuple of 2 floats for 2D, - a tuple of 3 floats for 3D. Defaults to `1.0`(no scaling). + a tuple of 3 floats for 3D. Defaults to `1.0`. spatial_size: output image spatial size. if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1, the transform will use the spatial size of `img`. @@ -1423,12 +1429,14 @@ def __init__( shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: - [ - [1.0, params[0], params[1], 0.0], - [params[2], 1.0, params[3], 0.0], - [params[4], params[5], 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - ] + + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly select pixel/voxel to translate for every spatial dims. scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select @@ -1596,11 +1604,13 @@ def __init__( and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D) for affine matrix, take a 2D affine as example:: - [ - [1.0, params[0], 0.0], - [params[1], 1.0, 0.0], - [0.0, 0.0, 1.0], - ] + + [ + [1.0, params[0], 0.0], + [params[1], 1.0, 0.0], + [0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly select pixel to translate for every spatial dims. scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select @@ -1733,12 +1743,14 @@ def __init__( and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: - [ - [1.0, params[0], params[1], 0.0], - [params[2], 1.0, params[3], 0.0], - [params[4], params[5], 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - ] + + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly select voxel to translate for every spatial dims. scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select diff --git a/monai/transforms/spatial/dictionary.py b/monai/transforms/spatial/dictionary.py index 00844b35dd..ef6a153a08 100644 --- a/monai/transforms/spatial/dictionary.py +++ b/monai/transforms/spatial/dictionary.py @@ -593,17 +593,19 @@ def __init__( rotate_params: a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. Defaults to no rotation. shear_params: shearing factors for affine matrix, take a 3D affine as example:: - [ - [1.0, params[0], params[1], 0.0], - [params[2], 1.0, params[3], 0.0], - [params[4], params[5], 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - ] - a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. + + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + + a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing. translate_params: a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Translation is in pixel/voxel relative to the center of the input image. Defaults to no translation. scale_params: scale factor for every spatial dims. a tuple of 2 floats for 2D, - a tuple of 3 floats for 3D. Defaults to `1.0`(no scaling). + a tuple of 3 floats for 3D. Defaults to `1.0`. spatial_size: output image spatial size. if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1, the transform will use the spatial size of `img`. @@ -727,12 +729,14 @@ def __init__( shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: - [ - [1.0, params[0], params[1], 0.0], - [params[2], 1.0, params[3], 0.0], - [params[4], params[5], 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - ] + + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly select pixel/voxel to translate for every spatial dims. scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select @@ -902,11 +906,13 @@ def __init__( and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D) for affine matrix, take a 2D affine as example:: - [ - [1.0, params[0], 0.0], - [params[1], 1.0, 0.0], - [0.0, 0.0, 1.0], - ] + + [ + [1.0, params[0], 0.0], + [params[1], 1.0, 0.0], + [0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly select pixel to translate for every spatial dims. scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select @@ -1030,12 +1036,14 @@ def __init__( and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: - [ - [1.0, params[0], params[1], 0.0], - [params[2], 1.0, params[3], 0.0], - [params[4], params[5], 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - ] + + [ + [1.0, params[0], params[1], 0.0], + [params[2], 1.0, params[3], 0.0], + [params[4], params[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + translate_range: translate range with format matching `rotate_range`, it defines the range to randomly select voxel to translate for every spatial dims. scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index 4f163b7d4c..6dd2d2539f 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -609,12 +609,13 @@ def create_shear(spatial_dims: int, coefs: Union[Sequence[float], float]) -> np. spatial_dims: spatial rank coefs: shearing factors, a tuple of 2 floats for 2D, a tuple of 6 floats for 3D), take a 3D affine as example:: - [ - [1.0, coefs[0], coefs[1], 0.0], - [coefs[2], 1.0, coefs[3], 0.0], - [coefs[4], coefs[5], 1.0, 0.0], - [0.0, 0.0, 0.0, 1.0], - ] + + [ + [1.0, coefs[0], coefs[1], 0.0], + [coefs[2], 1.0, coefs[3], 0.0], + [coefs[4], coefs[5], 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] Raises: NotImplementedError: When ``spatial_dims`` is not one of [2, 3]. From bb48abf6dfd745932aa79aebbf7b525d437e4001 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 4 Aug 2021 13:29:24 +0800 Subject: [PATCH 3/4] [DLMED] update according to comments Signed-off-by: Nic Ma --- monai/transforms/spatial/array.py | 38 +++++++++++++++----------- monai/transforms/spatial/dictionary.py | 24 ++++++++-------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/monai/transforms/spatial/array.py b/monai/transforms/spatial/array.py index f51b31087c..8ced2046c0 100644 --- a/monai/transforms/spatial/array.py +++ b/monai/transforms/spatial/array.py @@ -1058,10 +1058,10 @@ def __init__( Args: rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can - be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range - `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 - and nothing for the remaining dimensions. + for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. + This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be + in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` + for dim0 and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: @@ -1299,6 +1299,7 @@ class Affine(Transform): """ Transform ``img`` given the affine parameters. More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. + A tutorial is available: https://github.com/Project-MONAI/tutorials/blob/0.6.0/modules/transforms_demo_2d.ipynb. """ @@ -1399,6 +1400,7 @@ class RandAffine(RandomizableTransform): """ Random affine transform. More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. + A tutorial is available: https://github.com/Project-MONAI/tutorials/blob/0.6.0/modules/transforms_demo_2d.ipynb. """ @@ -1422,10 +1424,10 @@ def __init__( defaults to 0.1, with 10% chance returns a randomized grid. rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can - be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range - `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 - and nothing for the remaining dimensions. + for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. + This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be + in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` + for dim0 and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: @@ -1572,6 +1574,8 @@ class Rand2DElastic(RandomizableTransform): """ Random elastic deformation and affine in 2D. More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. + A tutorial is available: https://github.com/Project-MONAI/tutorials/blob/0.6.0/modules/transforms_demo_2d.ipynb. + """ def __init__( @@ -1598,10 +1602,10 @@ def __init__( otherwise returns a ``spatial_size`` centered area extracted from the input image. rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can - be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range - `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 - and nothing for the remaining dimensions. + for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. + This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be + in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` + for dim0 and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D) for affine matrix, take a 2D affine as example:: @@ -1709,6 +1713,8 @@ class Rand3DElastic(RandomizableTransform): """ Random elastic deformation and affine in 3D. More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. + A tutorial is available: https://github.com/Project-MONAI/tutorials/blob/0.6.0/modules/transforms_demo_2d.ipynb. + """ def __init__( @@ -1737,10 +1743,10 @@ def __init__( otherwise returns a ``spatial_size`` centered area extracted from the input image. rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can - be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range - `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 - and nothing for the remaining dimensions. + for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. + This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be + in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` + for dim0 and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: diff --git a/monai/transforms/spatial/dictionary.py b/monai/transforms/spatial/dictionary.py index ef6a153a08..a7eeceacf9 100644 --- a/monai/transforms/spatial/dictionary.py +++ b/monai/transforms/spatial/dictionary.py @@ -722,10 +722,10 @@ def __init__( defaults to 0.1, with 10% chance returns a randomized grid. rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can - be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range - `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 - and nothing for the remaining dimensions. + for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. + This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be + in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` + for dim0 and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: @@ -900,10 +900,10 @@ def __init__( otherwise returns a ``spatial_size`` centered area extracted from the input image. rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can - be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range - `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 - and nothing for the remaining dimensions. + for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. + This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be + in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` + for dim0 and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 2 floats for 2D) for affine matrix, take a 2D affine as example:: @@ -1030,10 +1030,10 @@ def __init__( otherwise returns a ``spatial_size`` centered area extracted from the input image. rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter - for the `i`th dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. This can - be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be in range - `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` for dim0 - and nothing for the remaining dimensions. + for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used. + This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be + in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]` + for dim0 and nothing for the remaining dimensions. shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select shearing factors(a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example:: From f701f6ad1ed980a8229c3918e64f8949d72f6d15 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 4 Aug 2021 17:05:47 +0800 Subject: [PATCH 4/4] [DLMED] remove coord link Signed-off-by: Nic Ma --- monai/transforms/spatial/array.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/monai/transforms/spatial/array.py b/monai/transforms/spatial/array.py index 8ced2046c0..9e78e18f85 100644 --- a/monai/transforms/spatial/array.py +++ b/monai/transforms/spatial/array.py @@ -944,7 +944,6 @@ def __call__( class AffineGrid(Transform): """ Affine transforms on the coordinates. - More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. Args: rotate_params: a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. @@ -1042,7 +1041,7 @@ def __call__( class RandAffineGrid(Randomizable, Transform): """ Generate randomised affine grid. - More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. + """ def __init__( @@ -1298,7 +1297,6 @@ def __call__( class Affine(Transform): """ Transform ``img`` given the affine parameters. - More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. A tutorial is available: https://github.com/Project-MONAI/tutorials/blob/0.6.0/modules/transforms_demo_2d.ipynb. """ @@ -1399,7 +1397,6 @@ def __call__( class RandAffine(RandomizableTransform): """ Random affine transform. - More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. A tutorial is available: https://github.com/Project-MONAI/tutorials/blob/0.6.0/modules/transforms_demo_2d.ipynb. """ @@ -1573,7 +1570,6 @@ def __call__( class Rand2DElastic(RandomizableTransform): """ Random elastic deformation and affine in 2D. - More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. A tutorial is available: https://github.com/Project-MONAI/tutorials/blob/0.6.0/modules/transforms_demo_2d.ipynb. """ @@ -1712,7 +1708,6 @@ def __call__( class Rand3DElastic(RandomizableTransform): """ Random elastic deformation and affine in 3D. - More details about affine matrix definition: https://www.slicer.org/wiki/Coordinate_systems. A tutorial is available: https://github.com/Project-MONAI/tutorials/blob/0.6.0/modules/transforms_demo_2d.ipynb. """