From dac287ad4444dd9d74daed712f186335e26077b9 Mon Sep 17 00:00:00 2001 From: Han123su Date: Fri, 20 Sep 2024 01:42:59 +0800 Subject: [PATCH 1/4] Modify Signed-off-by: Han123su --- monai/transforms/utils.py | 2 +- monai/utils/type_conversion.py | 7 +++++++ tests/test_rand_weighted_crop.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index e32bf6fc48..b8c3a0f408 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -579,7 +579,7 @@ def weighted_patch_samples( if not v[-1] or not isfinite(v[-1]) or v[-1] < 0: # uniform sampling idx = r_state.randint(0, len(v), size=n_samples) else: - r, *_ = convert_to_dst_type(r_state.random(n_samples), v) + r, *_ = convert_to_dst_type(r_state.random(n_samples), v, structure_only=True) idx = searchsorted(v, r * v[-1], right=True) # type: ignore idx, *_ = convert_to_dst_type(idx, v, dtype=torch.int) # type: ignore # compensate 'valid' mode diff --git a/monai/utils/type_conversion.py b/monai/utils/type_conversion.py index e4f97fc4a6..1d20663bb5 100644 --- a/monai/utils/type_conversion.py +++ b/monai/utils/type_conversion.py @@ -339,6 +339,7 @@ def convert_to_dst_type( wrap_sequence: bool = False, device: None | str | torch.device = None, safe: bool = False, + structure_only: bool = False, ) -> tuple[NdarrayTensor, type, torch.device | None]: """ Convert source data to the same data type and device as the destination data. @@ -355,12 +356,18 @@ def convert_to_dst_type( device: target device to put the converted Tensor data. If unspecified, `dst.device` will be used if possible. safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`. E.g., `[256, -12]` -> `[array(0), array(244)]`. If `True`, then `[256, -12]` -> `[array(255), array(0)]`. + structure_only: if `True`, only convert the data structure without changing the data type. default to `False`. See Also: :func:`convert_data_type` """ device = dst.device if device is None and isinstance(dst, torch.Tensor) else device + + # If structure_only is True, keep the dtype of src without changing its data type. + if structure_only: + dtype = getattr(src, "dtype", dtype) # Use the data type of src + if dtype is None: dtype = getattr(dst, "dtype", None) # sequence has no dtype diff --git a/tests/test_rand_weighted_crop.py b/tests/test_rand_weighted_crop.py index 47a8f3bfa2..f844ddf081 100644 --- a/tests/test_rand_weighted_crop.py +++ b/tests/test_rand_weighted_crop.py @@ -90,6 +90,21 @@ def get_data(ndim): [[63, 37], [31, 43], [66, 20]], ] ) + im = SEG1_2D + weight_map = np.zeros_like(im, dtype=np.int32) + weight_map[0, 30, 20] = 3 + weight_map[0, 45, 44] = 1 + weight_map[0, 60, 50] = 2 + TESTS.append( + [ + "int w 2d", + dict(spatial_size=(10, 12), num_samples=3), + p(im), + q(weight_map), + (1, 10, 12), + [[60, 50], [30, 20], [45, 44]], + ] + ) im = SEG1_3D weight = np.zeros_like(im) weight[0, 5, 30, 17] = 1.1 @@ -149,6 +164,21 @@ def get_data(ndim): [[32, 24, 40], [32, 24, 40], [32, 24, 40]], ] ) + im = SEG1_3D + weight_map = np.zeros_like(im, dtype=np.int32) + weight_map[0, 6, 22, 19] = 4 + weight_map[0, 8, 40, 31] = 2 + weight_map[0, 13, 20, 24] = 3 + TESTS.append( + [ + "int w 3d", + dict(spatial_size=(8, 10, 12), num_samples=3), + p(im), + q(weight_map), + (1, 8, 10, 12), + [[13, 20, 24], [6, 22, 19], [8, 40, 31]], + ] + ) class TestRandWeightedCrop(CropTest): From ec4bfa4e9595dc6a34a982f7632f74c999527a26 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Sep 2024 18:09:35 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/utils/type_conversion.py | 2 +- tests/test_rand_weighted_crop.py | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/monai/utils/type_conversion.py b/monai/utils/type_conversion.py index ebc92740d0..1a62a353a2 100644 --- a/monai/utils/type_conversion.py +++ b/monai/utils/type_conversion.py @@ -347,7 +347,7 @@ def convert_to_dst_type( wrap_sequence: bool = False, device: None | str | torch.device = None, safe: bool = False, - structure_only: bool = False, + structure_only: bool = False, ) -> tuple[NdarrayTensor, type, torch.device | None]: """ Convert source data to the same data type and device as the destination data. diff --git a/tests/test_rand_weighted_crop.py b/tests/test_rand_weighted_crop.py index f844ddf081..f509065a56 100644 --- a/tests/test_rand_weighted_crop.py +++ b/tests/test_rand_weighted_crop.py @@ -91,18 +91,18 @@ def get_data(ndim): ] ) im = SEG1_2D - weight_map = np.zeros_like(im, dtype=np.int32) + weight_map = np.zeros_like(im, dtype=np.int32) weight_map[0, 30, 20] = 3 weight_map[0, 45, 44] = 1 weight_map[0, 60, 50] = 2 TESTS.append( [ - "int w 2d", - dict(spatial_size=(10, 12), num_samples=3), - p(im), - q(weight_map), - (1, 10, 12), - [[60, 50], [30, 20], [45, 44]], + "int w 2d", + dict(spatial_size=(10, 12), num_samples=3), + p(im), + q(weight_map), + (1, 10, 12), + [[60, 50], [30, 20], [45, 44]], ] ) im = SEG1_3D @@ -165,18 +165,18 @@ def get_data(ndim): ] ) im = SEG1_3D - weight_map = np.zeros_like(im, dtype=np.int32) + weight_map = np.zeros_like(im, dtype=np.int32) weight_map[0, 6, 22, 19] = 4 weight_map[0, 8, 40, 31] = 2 weight_map[0, 13, 20, 24] = 3 TESTS.append( [ - "int w 3d", - dict(spatial_size=(8, 10, 12), num_samples=3), - p(im), - q(weight_map), - (1, 8, 10, 12), - [[13, 20, 24], [6, 22, 19], [8, 40, 31]], + "int w 3d", + dict(spatial_size=(8, 10, 12), num_samples=3), + p(im), + q(weight_map), + (1, 8, 10, 12), + [[13, 20, 24], [6, 22, 19], [8, 40, 31]], ] ) From e79f1782b41d03d9456eb42c4e66627801e532f9 Mon Sep 17 00:00:00 2001 From: Han123su Date: Fri, 20 Sep 2024 05:23:10 +0800 Subject: [PATCH 3/4] Modify to simple Signed-off-by: Han123su --- monai/transforms/utils.py | 3 ++- monai/utils/type_conversion.py | 6 ------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index 3f7038c5cb..e7e1616e13 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -582,7 +582,8 @@ def weighted_patch_samples( if not v[-1] or not isfinite(v[-1]) or v[-1] < 0: # uniform sampling idx = r_state.randint(0, len(v), size=n_samples) else: - r, *_ = convert_to_dst_type(r_state.random(n_samples), v, structure_only=True) + r_samples = r_state.random(n_samples) + r, *_ = convert_to_dst_type(r_samples, v, dtype=r_samples.dtype) idx = searchsorted(v, r * v[-1], right=True) # type: ignore idx, *_ = convert_to_dst_type(idx, v, dtype=torch.int) # type: ignore # compensate 'valid' mode diff --git a/monai/utils/type_conversion.py b/monai/utils/type_conversion.py index ebc92740d0..7a2838f289 100644 --- a/monai/utils/type_conversion.py +++ b/monai/utils/type_conversion.py @@ -347,7 +347,6 @@ def convert_to_dst_type( wrap_sequence: bool = False, device: None | str | torch.device = None, safe: bool = False, - structure_only: bool = False, ) -> tuple[NdarrayTensor, type, torch.device | None]: """ Convert source data to the same data type and device as the destination data. @@ -364,7 +363,6 @@ def convert_to_dst_type( device: target device to put the converted Tensor data. If unspecified, `dst.device` will be used if possible. safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`. E.g., `[256, -12]` -> `[array(0), array(244)]`. If `True`, then `[256, -12]` -> `[array(255), array(0)]`. - structure_only: if `True`, only convert the data structure without changing the data type. default to `False`. See Also: :func:`convert_data_type` @@ -372,10 +370,6 @@ def convert_to_dst_type( device = dst.device if device is None and isinstance(dst, torch.Tensor) else device - # If structure_only is True, keep the dtype of src without changing its data type. - if structure_only: - dtype = getattr(src, "dtype", dtype) # Use the data type of src - if dtype is None: dtype = getattr(dst, "dtype", None) # sequence has no dtype From a6b1ae1af12a5fbde3bb3ec2cc0591b888fe5a28 Mon Sep 17 00:00:00 2001 From: Han123su <107395380+Han123su@users.noreply.github.com> Date: Fri, 20 Sep 2024 11:47:40 +0800 Subject: [PATCH 4/4] Update type_conversion.py Signed-off-by: Han123su <107395380+Han123su@users.noreply.github.com> --- monai/utils/type_conversion.py | 1 - 1 file changed, 1 deletion(-) diff --git a/monai/utils/type_conversion.py b/monai/utils/type_conversion.py index 7a2838f289..420e935b33 100644 --- a/monai/utils/type_conversion.py +++ b/monai/utils/type_conversion.py @@ -369,7 +369,6 @@ def convert_to_dst_type( """ device = dst.device if device is None and isinstance(dst, torch.Tensor) else device - if dtype is None: dtype = getattr(dst, "dtype", None) # sequence has no dtype