From 1aaa19c69f4c4a303f44189562371196a41f4c49 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 3 Nov 2021 07:20:54 +0800 Subject: [PATCH 1/3] [DLMED] enhance rescale Signed-off-by: Nic Ma --- monai/transforms/utils.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index bb514104ae..d51612b703 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -156,9 +156,16 @@ def rescale_array( ) -> NdarrayOrTensor: """ Rescale the values of numpy array `arr` to be from `minv` to `maxv`. + + Args: + arr: input array to rescale. + minv: minimum value of target rescaled array. + maxv: maxmum value of target rescaled array. + dtype: data type of output, if None, same as input array. + """ - if dtype is not None: - arr, *_ = convert_data_type(arr, dtype=dtype) + dtype_ = dtype or arr.dtype + arr, *_ = convert_data_type(arr, dtype=dtype_) mina = arr.min() maxa = arr.max() @@ -166,7 +173,9 @@ def rescale_array( return arr * minv norm = (arr - mina) / (maxa - mina) # normalize the array first - return (norm * (maxv - minv)) + minv # rescale by minv and maxv, which is the normalized array by default + out = (norm * (maxv - minv)) + minv # rescale by minv and maxv, which is the normalized array by default + out, *_ = convert_data_type(out, dtype=dtype_) + return out def rescale_instance_array( From 44b755dbbba7fcde6ccc6718ee833ba0bfdf87bf Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 3 Nov 2021 07:58:38 +0800 Subject: [PATCH 2/3] [DLMED] update according to comments Signed-off-by: Nic Ma --- monai/transforms/intensity/array.py | 14 +++++++------- monai/transforms/utils.py | 10 ++++------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index 8d7ebfbfe0..6fa69e4a8f 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -436,14 +436,14 @@ def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor: if self.minv is not None and self.maxv is not None: if self.channel_wise: out = [rescale_array(d, self.minv, self.maxv, dtype=self.dtype) for d in img] - return torch.stack(out) if isinstance(img, torch.Tensor) else np.stack(out) # type: ignore - return rescale_array(img, self.minv, self.maxv, dtype=self.dtype) - if self.factor is not None: + ret = torch.stack(out) if isinstance(img, torch.Tensor) else np.stack(out) # type: ignore + ret = rescale_array(img, self.minv, self.maxv, dtype=self.dtype) + elif self.factor is not None: ret = img * (1 + self.factor) - if self.dtype is not None: - ret, *_ = convert_data_type(ret, dtype=self.dtype or img.dtype) - return ret - raise ValueError("Incompatible values: minv=None or maxv=None and factor=None.") + else: + raise ValueError("Incompatible values: minv=None or maxv=None and factor=None.") + ret, *_ = convert_data_type(ret, dtype=self.dtype or img.dtype) + return ret class RandScaleIntensity(RandomizableTransform): diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index d51612b703..118c2ad35b 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -161,11 +161,11 @@ def rescale_array( arr: input array to rescale. minv: minimum value of target rescaled array. maxv: maxmum value of target rescaled array. - dtype: data type of output, if None, same as input array. + dtype: if not None, convert input array to dtype before computation. """ - dtype_ = dtype or arr.dtype - arr, *_ = convert_data_type(arr, dtype=dtype_) + if dtype is not None: + arr, *_ = convert_data_type(arr, dtype=dtype) mina = arr.min() maxa = arr.max() @@ -173,9 +173,7 @@ def rescale_array( return arr * minv norm = (arr - mina) / (maxa - mina) # normalize the array first - out = (norm * (maxv - minv)) + minv # rescale by minv and maxv, which is the normalized array by default - out, *_ = convert_data_type(out, dtype=dtype_) - return out + return (norm * (maxv - minv)) + minv # rescale by minv and maxv, which is the normalized array by default def rescale_instance_array( From bb2a025cce579f6c4e7c79197049b4e12799759f Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 3 Nov 2021 18:38:36 +0800 Subject: [PATCH 3/3] [DLMED] fix typo Signed-off-by: Nic Ma --- monai/transforms/intensity/array.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index 6fa69e4a8f..f24b184ec8 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -437,11 +437,11 @@ def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor: if self.channel_wise: out = [rescale_array(d, self.minv, self.maxv, dtype=self.dtype) for d in img] ret = torch.stack(out) if isinstance(img, torch.Tensor) else np.stack(out) # type: ignore - ret = rescale_array(img, self.minv, self.maxv, dtype=self.dtype) - elif self.factor is not None: - ret = img * (1 + self.factor) + else: + ret = rescale_array(img, self.minv, self.maxv, dtype=self.dtype) else: - raise ValueError("Incompatible values: minv=None or maxv=None and factor=None.") + ret = (img * (1 + self.factor)) if self.factor is not None else img + ret, *_ = convert_data_type(ret, dtype=self.dtype or img.dtype) return ret