From cfa73f8427069d8426ac0c8a2bc01cdcc13544eb Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Sat, 6 Jun 2020 19:02:54 +0100 Subject: [PATCH 01/10] revise nifti_writer --- monai/data/nifti_writer.py | 65 ++++++++++++++++++-------------------- tests/test_nifti_rw.py | 36 ++++++--------------- 2 files changed, 40 insertions(+), 61 deletions(-) diff --git a/monai/data/nifti_writer.py b/monai/data/nifti_writer.py index d014e0c55ff..6df52b8df59 100644 --- a/monai/data/nifti_writer.py +++ b/monai/data/nifti_writer.py @@ -11,9 +11,10 @@ import nibabel as nib import numpy as np -import scipy.ndimage +import torch -from monai.data.utils import compute_shape_offset, to_affine_nd, InterpolationCode +from monai.data.utils import compute_shape_offset, to_affine_nd +from monai.networks.layers import AffineTransform def write_nifti( @@ -23,9 +24,8 @@ def write_nifti( target_affine=None, resample: bool = True, output_shape=None, - interp_order=InterpolationCode.SPLINE3, - mode="constant", - cval=0, + interp_order: str = "bilinear", + mode: str = "border", dtype=None, ): """ @@ -67,15 +67,12 @@ def write_nifti( could not be achieved by swapping/flipping data axes. output_shape (None or tuple of ints): output image shape. this option is used when resample = True. - interp_order: the order of the spline interpolation, default is InterpolationCode.SPLINE3. - The order has to be in the range 0 - 5. - https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.affine_transform.html + interp_order (`nearest|bilinear`): the interpolation mode, default is "bilinear". + See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample this option is used when `resample = True`. - mode (`reflect|constant|nearest|mirror|wrap`): + mode (`zeros|border|reflection`): The mode parameter determines how the input array is extended beyond its boundaries. this option is used when `resample = True`. - cval (scalar): Value to fill past edges of input if mode is "constant". Default is 0.0. - this option is used when `resample = True`. dtype (np.dtype, optional): convert the image to save to this data type. """ assert isinstance(data, np.ndarray), "input data must be numpy array." @@ -88,7 +85,7 @@ def write_nifti( target_affine = affine target_affine = to_affine_nd(sr, target_affine) - if np.allclose(affine, target_affine): + if np.allclose(affine, target_affine, atol=1e-3): # no affine changes, save (data, affine) results_img = nib.Nifti1Image(data.astype(dtype), to_affine_nd(3, target_affine)) nib.save(results_img, file_name) @@ -101,38 +98,38 @@ def write_nifti( data_shape = data.shape data = nib.orientations.apply_orientation(data, ornt_transform) _affine = affine @ nib.orientations.inv_ornt_aff(ornt_transform, data_shape) - if np.allclose(_affine, target_affine) or not resample: + if np.allclose(_affine, target_affine, atol=1e-3) or not resample: results_img = nib.Nifti1Image(data.astype(dtype), to_affine_nd(3, target_affine)) nib.save(results_img, file_name) return # need resampling + affine_xform = AffineTransform( + normalized=False, mode=interp_order, padding_mode=mode, align_corners=True, reverse_indexing=True + ) transform = np.linalg.inv(_affine) @ target_affine if output_shape is None: output_shape, _ = compute_shape_offset(data.shape, _affine, target_affine) - dtype = dtype or data.dtype if data.ndim > 3: # multi channel, resampling each channel spatial_shape, channel_shape = data.shape[:3], data.shape[3:] - data_ = data.astype(dtype).reshape(list(spatial_shape) + [-1]) - data_chns = [] - for chn in range(data_.shape[-1]): - data_chns.append( - scipy.ndimage.affine_transform( - data_[..., chn], - matrix=transform, - output_shape=output_shape[:3], - order=interp_order, - mode=mode, - cval=cval, - ) - ) - data_chns_ = np.stack(data_chns, axis=-1) - data_ = data_chns_.reshape(list(data_chns_.shape[:3]) + list(channel_shape)) - else: - data_ = data.astype(dtype) - data_ = scipy.ndimage.affine_transform( - data_, matrix=transform, output_shape=output_shape[: data_.ndim], order=interp_order, mode=mode, cval=cval + data_ = data.reshape(list(spatial_shape) + [-1]) + data_ = np.moveaxis(data_, -1, 0) # channel first for pytorch + data_ = affine_xform( + torch.from_numpy((data_.astype(np.float64))[None]), + torch.from_numpy(transform.astype(np.float64)), + spatial_size=output_shape[:3], ) - results_img = nib.Nifti1Image(data_, to_affine_nd(3, target_affine)) + data_ = data_.squeeze(0).detach().cpu().numpy() + data_ = np.moveaxis(data_, 0, -1) # channel last for nifti + data_ = data_.reshape(list(data_.shape[:3]) + list(channel_shape)) + else: # single channel image, need to expand to have batch and channel + data_ = affine_xform( + torch.from_numpy((data.astype(np.float64))[None, None]), + torch.from_numpy(transform.astype(np.float64)), + spatial_size=output_shape[:3], + ) + data_ = data_.squeeze(0).squeeze(0).detach().cpu().numpy() + dtype = dtype or data.dtype + results_img = nib.Nifti1Image(data_.astype(dtype), to_affine_nd(3, target_affine)) nib.save(results_img, file_name) return diff --git a/tests/test_nifti_rw.py b/tests/test_nifti_rw.py index 2a97726bbc1..b0ba5ad103e 100644 --- a/tests/test_nifti_rw.py +++ b/tests/test_nifti_rw.py @@ -87,14 +87,20 @@ def test_consistency(self): data, _, new_affine = Orientation("ILP")(data, new_affine) if os.path.exists(test_image): os.remove(test_image) - write_nifti(data[0], test_image, new_affine, original_affine, interp_order=0, mode="reflect") + write_nifti(data[0], test_image, new_affine, original_affine, interp_order="nearest", mode="border") saved = nib.load(test_image) saved_data = saved.get_fdata() np.testing.assert_allclose(saved_data, np.arange(64).reshape(1, 8, 8), atol=1e-7) if os.path.exists(test_image): os.remove(test_image) write_nifti( - data[0], test_image, new_affine, original_affine, interp_order=0, mode="reflect", output_shape=(1, 8, 8) + data[0], + test_image, + new_affine, + original_affine, + interp_order="nearest", + mode="border", + output_shape=(1, 8, 8), ) saved = nib.load(test_image) saved_data = saved.get_fdata() @@ -102,37 +108,13 @@ def test_consistency(self): if os.path.exists(test_image): os.remove(test_image) - def test_write_1d(self): - out_dir = tempfile.mkdtemp() - image_name = os.path.join(out_dir, "test.nii.gz") - img = np.arange(5).reshape(-1) - write_nifti(img, image_name, affine=np.diag([1, 1, 1]), target_affine=np.diag([1.4, 2.0, 1])) - out = nib.load(image_name) - np.testing.assert_allclose(out.get_fdata(), [0, 1, 3, 0]) - np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1])) - - image_name = os.path.join(out_dir, "test1.nii.gz") - img = np.arange(5).reshape(-1) - write_nifti(img, image_name, affine=[[1]], target_affine=[[1.4]]) - out = nib.load(image_name) - np.testing.assert_allclose(out.get_fdata(), [0, 1, 3, 0]) - np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1])) - - image_name = os.path.join(out_dir, "test2.nii.gz") - img = np.arange(5).reshape(-1) - write_nifti(img, image_name, affine=np.diag([1.5, 1.5, 1.5]), target_affine=np.diag([1.5, 1.5, 1.5])) - out = nib.load(image_name) - np.testing.assert_allclose(out.get_fdata(), np.arange(5).reshape(-1)) - np.testing.assert_allclose(out.affine, np.diag([1.5, 1, 1, 1])) - shutil.rmtree(out_dir) - def test_write_2d(self): out_dir = tempfile.mkdtemp() image_name = os.path.join(out_dir, "test.nii.gz") img = np.arange(6).reshape((2, 3)) write_nifti(img, image_name, affine=np.diag([1]), target_affine=np.diag([1.4])) out = nib.load(image_name) - np.testing.assert_allclose(out.get_fdata(), [[0, 1, 2], [0, 0, 0]]) + np.testing.assert_allclose(out.get_fdata(), [[0, 1, 2], [3.0, 4, 5]]) np.testing.assert_allclose(out.affine, np.diag([1.4, 1, 1, 1])) image_name = os.path.join(out_dir, "test1.nii.gz") From fbddb1b230007aef8645a9cdc5d7bf1cff4a3e78 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Sat, 6 Jun 2020 19:09:41 +0100 Subject: [PATCH 02/10] revise nifti_saver --- monai/data/nifti_saver.py | 22 ++++++++-------------- monai/data/nifti_writer.py | 6 +++--- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/monai/data/nifti_saver.py b/monai/data/nifti_saver.py index 1ae81af0824..bbc2026e42f 100644 --- a/monai/data/nifti_saver.py +++ b/monai/data/nifti_saver.py @@ -31,9 +31,8 @@ def __init__( output_postfix: str = "seg", output_ext: str = ".nii.gz", resample: bool = True, - interp_order: int = 3, - mode: str = "constant", - cval: Union[int, float] = 0, + interp_order: str = "bilinear", + mode: str = "border", dtype: Optional[np.dtype] = None, ): """ @@ -41,16 +40,13 @@ def __init__( output_dir (str): output image directory. output_postfix (str): a string appended to all output file names. output_ext (str): output file extension name. - resample: whether to resample before saving the data array. - interp_order: the order of the spline interpolation, default is InterpolationCode.SPLINE3. - The order has to be in the range 0 - 5. - https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.affine_transform.html - this option is used when `resample = True`. - mode (`reflect|constant|nearest|mirror|wrap`): + resample (bool): whether to resample before saving the data array. + interp_order (`nearest|bilinear`): the interpolation mode, default is "bilinear". + See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample + This option is used when `resample = True`. + mode (`zeros|border|reflection`): The mode parameter determines how the input array is extended beyond its boundaries. - this option is used when `resample = True`. - cval (scalar): Value to fill past edges of input if mode is "constant". Default is 0.0. - this option is used when `resample = True`. + This option is used when `resample = True`. dtype (np.dtype, optional): convert the image data to save to this data type. If None, keep the original type of data. @@ -61,7 +57,6 @@ def __init__( self.resample = resample self.interp_order = interp_order self.mode = mode - self.cval = cval self.dtype = dtype self._data_index = 0 @@ -106,7 +101,6 @@ def save(self, data: Union[torch.Tensor, np.ndarray], meta_data=None): output_shape=spatial_shape, interp_order=self.interp_order, mode=self.mode, - cval=self.cval, dtype=self.dtype or data.dtype, ) diff --git a/monai/data/nifti_writer.py b/monai/data/nifti_writer.py index 6df52b8df59..048dff2d558 100644 --- a/monai/data/nifti_writer.py +++ b/monai/data/nifti_writer.py @@ -66,13 +66,13 @@ def write_nifti( resample: whether to run resampling when the target affine could not be achieved by swapping/flipping data axes. output_shape (None or tuple of ints): output image shape. - this option is used when resample = True. + This option is used when resample = True. interp_order (`nearest|bilinear`): the interpolation mode, default is "bilinear". See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample - this option is used when `resample = True`. + This option is used when `resample = True`. mode (`zeros|border|reflection`): The mode parameter determines how the input array is extended beyond its boundaries. - this option is used when `resample = True`. + This option is used when `resample = True`. dtype (np.dtype, optional): convert the image to save to this data type. """ assert isinstance(data, np.ndarray), "input data must be numpy array." From 0203de021d2d303a76d94b5a5d677ace77031e57 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Sat, 6 Jun 2020 19:29:35 +0100 Subject: [PATCH 03/10] revise SegmentationSaver --- monai/handlers/segmentation_saver.py | 46 +++++++++++++++++++++------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/monai/handlers/segmentation_saver.py b/monai/handlers/segmentation_saver.py index 3697dc743b2..9f87da5489f 100644 --- a/monai/handlers/segmentation_saver.py +++ b/monai/handlers/segmentation_saver.py @@ -28,8 +28,8 @@ def __init__( output_postfix: str = "seg", output_ext: str = ".nii.gz", resample: bool = True, - interp_order: int = 0, - mode: str = "constant", + interp_order="bilinear", + mode: str = "border", cval: Union[int, float] = 0, scale: bool = False, dtype: Optional[np.dtype] = None, @@ -42,17 +42,21 @@ def __init__( output_dir (str): output image directory. output_postfix (str): a string appended to all output file names. output_ext (str): output file extension name. - resample: whether to resample before saving the data array. - interp_order: the order of the spline interpolation, default is 0. + resample (bool): whether to resample before saving the data array. + interp_order (int or str): + The interpolation mode. This option is used when `resample = True`. + When saving NIfTI files, the options are "nearest", "bilinear". Defaults to "bilinear". + See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample + When saving PNG files, this sets the order of the spline interpolation. The order has to be in the range 0 - 5. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.affine_transform.html + mode (str): The mode parameter determines how the input array is extended beyond its boundaries. This option is used when `resample = True`. - mode (`reflect|constant|nearest|mirror|wrap`): - The mode parameter determines how the input array is extended beyond its boundaries. - This option is used when `resample = True`. + When saving NIfTI files, the options are "zeros", "border", "reflection". Default is "border". + When saving PNG files, the options are "reflect", "constant", "nearest", "mirror", "wrap". cval (scalar): Value to fill past edges of input if mode is "constant". Default is 0.0. - This option is used when `resample = True`. - scale: whether to scale data with 255 and convert to uint8 for data in range [0, 1]. + this option is used when `resample = True`. It's used for PNG format only. + scale (bool): whether to scale data with 255 and convert to uint8 for data in range [0, 1]. It's used for PNG format only. dtype (np.dtype, optional): convert the image data to save to this data type. If None, keep the original type of data. It's used for Nifti format only. @@ -67,9 +71,29 @@ def __init__( """ self.saver: Union[NiftiSaver, PNGSaver] if output_ext in (".nii.gz", ".nii"): - self.saver = NiftiSaver(output_dir, output_postfix, output_ext, resample, interp_order, mode, cval, dtype) + self.saver = NiftiSaver( + output_dir=output_dir, + output_postfix=output_postfix, + output_ext=output_ext, + resample=resample, + interp_order=interp_order, + mode=mode, + dtype=dtype, + ) elif output_ext == ".png": - self.saver = PNGSaver(output_dir, output_postfix, output_ext, resample, interp_order, mode, cval, scale) + # handling the function's default + interp_order = 0 if interp_order == "nearest" else interp_order + mode = "nearest" if mode == "border" else mode + self.saver = PNGSaver( + output_dir=output_dir, + output_postfix=output_postfix, + output_ext=output_ext, + resample=resample, + interp_order=interp_order, + mode=mode, + cval=cval, + scale=scale, + ) self.batch_transform = batch_transform self.output_transform = output_transform From ffe75775073d51eccaf13262cfb5dd5ab2142a30 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Sun, 7 Jun 2020 19:55:28 +0100 Subject: [PATCH 04/10] fixes float32->float64 --- monai/networks/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/networks/utils.py b/monai/networks/utils.py index 2b41dd6447d..385b07c89ba 100644 --- a/monai/networks/utils.py +++ b/monai/networks/utils.py @@ -96,7 +96,7 @@ def normalize_transform(shape, device=None, dtype=None, align_corners: bool = Fa norm[norm <= 0.0] = 2.0 norm = 2.0 / norm norm = torch.diag(torch.cat((norm, torch.ones((1,), dtype=torch.float64, device=device)))) - norm[:-1, -1] = 1.0 / torch.tensor(shape, dtype=torch.float32, device=device) - 1.0 + norm[:-1, -1] = 1.0 / torch.tensor(shape, dtype=torch.float64, device=device) - 1.0 norm = norm.unsqueeze(0).to(dtype=dtype) norm.requires_grad = False return norm From b4e1224b36e2d34935d3d085983110f432773a60 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Mon, 8 Jun 2020 13:36:44 +0100 Subject: [PATCH 05/10] update based on comments --- monai/data/nifti_saver.py | 3 +-- monai/data/nifti_writer.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/monai/data/nifti_saver.py b/monai/data/nifti_saver.py index bbc2026e42f..e6846cc7c77 100644 --- a/monai/data/nifti_saver.py +++ b/monai/data/nifti_saver.py @@ -46,10 +46,9 @@ def __init__( This option is used when `resample = True`. mode (`zeros|border|reflection`): The mode parameter determines how the input array is extended beyond its boundaries. - This option is used when `resample = True`. + Defaults to "border". This option is used when `resample = True`. dtype (np.dtype, optional): convert the image data to save to this data type. If None, keep the original type of data. - """ self.output_dir = output_dir self.output_postfix = output_postfix diff --git a/monai/data/nifti_writer.py b/monai/data/nifti_writer.py index 048dff2d558..f0899c3983e 100644 --- a/monai/data/nifti_writer.py +++ b/monai/data/nifti_writer.py @@ -72,7 +72,7 @@ def write_nifti( This option is used when `resample = True`. mode (`zeros|border|reflection`): The mode parameter determines how the input array is extended beyond its boundaries. - This option is used when `resample = True`. + Defaults to "border". This option is used when `resample = True`. dtype (np.dtype, optional): convert the image to save to this data type. """ assert isinstance(data, np.ndarray), "input data must be numpy array." From a7d01115671b3997875a9824d200d495948544ac Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Mon, 8 Jun 2020 13:39:27 +0100 Subject: [PATCH 06/10] remove unused import --- monai/networks/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/monai/networks/utils.py b/monai/networks/utils.py index 385b07c89ba..679d25bba52 100644 --- a/monai/networks/utils.py +++ b/monai/networks/utils.py @@ -14,7 +14,6 @@ import warnings import torch -import torch.nn.functional as f def one_hot(labels, num_classes: int, dtype: torch.dtype = torch.float): From 5763fe2dbd209830ea9a16be15703caf36c34f7b Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Mon, 8 Jun 2020 15:01:07 +0100 Subject: [PATCH 07/10] additional unit tests --- monai/data/nifti_writer.py | 6 +++- monai/data/png_saver.py | 8 ++++-- monai/data/png_writer.py | 8 +++--- monai/data/utils.py | 4 +-- monai/handlers/segmentation_saver.py | 4 +-- tests/test_handler_segmentation_saver.py | 36 ++++++++++++++++++++++-- tests/test_load_png.py | 4 +++ 7 files changed, 55 insertions(+), 15 deletions(-) diff --git a/monai/data/nifti_writer.py b/monai/data/nifti_writer.py index f0899c3983e..11a99662b2c 100644 --- a/monai/data/nifti_writer.py +++ b/monai/data/nifti_writer.py @@ -111,6 +111,8 @@ def write_nifti( if output_shape is None: output_shape, _ = compute_shape_offset(data.shape, _affine, target_affine) if data.ndim > 3: # multi channel, resampling each channel + while len(output_shape) < 3: + output_shape = list(output_shape) + [1] spatial_shape, channel_shape = data.shape[:3], data.shape[3:] data_ = data.reshape(list(spatial_shape) + [-1]) data_ = np.moveaxis(data_, -1, 0) # channel first for pytorch @@ -123,10 +125,12 @@ def write_nifti( data_ = np.moveaxis(data_, 0, -1) # channel last for nifti data_ = data_.reshape(list(data_.shape[:3]) + list(channel_shape)) else: # single channel image, need to expand to have batch and channel + while len(output_shape) < len(data.shape): + output_shape = list(output_shape) + [1] data_ = affine_xform( torch.from_numpy((data.astype(np.float64))[None, None]), torch.from_numpy(transform.astype(np.float64)), - spatial_size=output_shape[:3], + spatial_size=output_shape[: len(data.shape)], ) data_ = data_.squeeze(0).squeeze(0).detach().cpu().numpy() dtype = dtype or data.dtype diff --git a/monai/data/png_saver.py b/monai/data/png_saver.py index ba4b86aa940..463e2f2e304 100644 --- a/monai/data/png_saver.py +++ b/monai/data/png_saver.py @@ -42,12 +42,14 @@ def __init__( output_postfix (str): a string appended to all output file names. output_ext (str): output file extension name. resample: whether to resample and resize if providing spatial_shape in the metadata. - interp_order: the order of the spline interpolation, default is InterpolationCode.SPLINE3. + Defaults to True. + interp_order (int): the order of the spline interpolation, default is InterpolationCode.SPLINE3. This option is used when spatial_shape is specified and different from the data shape. - The order has to be in the range 0 - 5. - mode (`reflect|constant|nearest|mirror|wrap`): + The order has to be in the range 0 - 5. Defaults to 3. + mode (`constant|edge|symmetric|reflect|wrap`): The mode parameter determines how the input array is extended beyond its boundaries. This option is used when spatial_shape is specified and different from the data shape. + Defaults to "constant". cval (scalar): Value to fill past edges of input if mode is "constant". Default is 0.0. This option is used when spatial_shape is specified and different from the data shape. scale: whether to scale data with 255 and convert to uint8 for data in range [0, 1]. diff --git a/monai/data/png_writer.py b/monai/data/png_writer.py index 43730ccdc25..43c0f09947a 100644 --- a/monai/data/png_writer.py +++ b/monai/data/png_writer.py @@ -33,12 +33,12 @@ def write_png( data (numpy.ndarray): input data to write to file. file_name (string): expected file name that saved on disk. output_shape (None or tuple of ints): output image shape. - interp_order: the order of the spline interpolation, default is InterpolationCode.SPLINE3. - The order has to be in the range 0 - 5. + interp_order (int): the order of the spline interpolation, default is InterpolationCode.SPLINE3. + The order has to be in the range 0 - 5. Defaults to 3. this option is used when `output_shape != None`. - mode (`reflect|constant|nearest|mirror|wrap`): + mode (`constant|edge|symmetric|reflect|wrap`): The mode parameter determines how the input array is extended beyond its boundaries. - this option is used when `output_shape != None`. + This option is used when `output_shape != None`. Defaults to "constant". cval (scalar): Value to fill past edges of input if mode is "constant". Default is 0.0. this option is used when `output_shape != None`. scale: whether to scale data with 255 and convert to uint8 for data in range [0, 1]. diff --git a/monai/data/utils.py b/monai/data/utils.py index 6593605deed..b482d306bd1 100644 --- a/monai/data/utils.py +++ b/monai/data/utils.py @@ -387,12 +387,12 @@ def to_affine_nd(r, affine): """ affine_ = np.array(affine, dtype=np.float64) if affine_.ndim != 2: - raise ValueError("input affine must have two dimensions") + raise ValueError(f"input affine matrix must have two dimensions, got {affine_.ndim}.") new_affine = np.array(r, dtype=np.float64, copy=True) if new_affine.ndim == 0: sr = new_affine.astype(int) if not np.isfinite(sr) or sr < 0: - raise ValueError("r must be positive.") + raise ValueError(f"r must be positive, got {sr}.") new_affine = np.eye(sr + 1, dtype=np.float64) d = max(min(len(new_affine) - 1, len(affine_) - 1), 1) new_affine[:d, :d] = affine_[:d, :d] diff --git a/monai/handlers/segmentation_saver.py b/monai/handlers/segmentation_saver.py index 9f87da5489f..1801ee1ee34 100644 --- a/monai/handlers/segmentation_saver.py +++ b/monai/handlers/segmentation_saver.py @@ -82,8 +82,8 @@ def __init__( ) elif output_ext == ".png": # handling the function's default - interp_order = 0 if interp_order == "nearest" else interp_order - mode = "nearest" if mode == "border" else mode + interp_order = 1 if interp_order == "bilinear" else interp_order + mode = "edge" if mode == "border" else mode self.saver = PNGSaver( output_dir=output_dir, output_postfix=output_postfix, diff --git a/tests/test_handler_segmentation_saver.py b/tests/test_handler_segmentation_saver.py index f8e764bbd3b..6d112d32bb2 100644 --- a/tests/test_handler_segmentation_saver.py +++ b/tests/test_handler_segmentation_saver.py @@ -15,22 +15,23 @@ import torch from ignite.engine import Engine from parameterized import parameterized +import numpy as np from monai.handlers import SegmentationSaver -TEST_CASE_1 = [".nii.gz"] +TEST_CASE_0 = [".nii.gz"] TEST_CASE_1 = [".png"] class TestHandlerSegmentationSaver(unittest.TestCase): - @parameterized.expand([TEST_CASE_1, TEST_CASE_1]) + @parameterized.expand([TEST_CASE_0, TEST_CASE_1]) def test_saved_content(self, output_ext): default_dir = os.path.join(".", "tempdir") shutil.rmtree(default_dir, ignore_errors=True) # set up engine def _train_func(engine, batch): - return torch.zeros(8, 1, 2, 2) + return torch.randint(0, 255, (8, 1, 2, 2)).float() engine = Engine(_train_func) @@ -45,6 +46,35 @@ def _train_func(engine, batch): self.assertTrue(os.path.exists(os.path.join(default_dir, filepath))) shutil.rmtree(default_dir) + @parameterized.expand([TEST_CASE_0, TEST_CASE_1]) + def test_save_resized_content(self, output_ext): + default_dir = os.path.join(".", "tempdir") + shutil.rmtree(default_dir, ignore_errors=True) + + # set up engine + def _train_func(engine, batch): + return torch.randint(0, 255, (8, 1, 2, 2)).float() + + engine = Engine(_train_func) + + # set up testing handler + saver = SegmentationSaver(output_dir=default_dir, output_postfix="seg", output_ext=output_ext) + saver.attach(engine) + + data = [ + { + "filename_or_obj": ["testfile" + str(i) for i in range(8)], + "spatial_shape": [(28, 28)] * 8, + "affine": [np.diag(np.ones(4)) * 5] * 8, + "original_affine": [np.diag(np.ones(4)) * 1.0] * 8, + } + ] + engine.run(data, max_epochs=1) + for i in range(8): + filepath = os.path.join("testfile" + str(i), "testfile" + str(i) + "_seg" + output_ext) + self.assertTrue(os.path.exists(os.path.join(default_dir, filepath))) + shutil.rmtree(default_dir) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_load_png.py b/tests/test_load_png.py index 40c0526fcda..4f71492f803 100644 --- a/tests/test_load_png.py +++ b/tests/test_load_png.py @@ -36,6 +36,10 @@ def test_shape(self, data_shape, filenames, expected_shape, meta_shape): result = LoadPNG()(filenames) self.assertTupleEqual(result[1]["spatial_shape"], meta_shape) self.assertTupleEqual(result[0].shape, expected_shape) + if result[0].shape == test_image.shape: + np.testing.assert_allclose(result[0], test_image) + else: + np.testing.assert_allclose(result[0], np.tile(test_image, [result[0].shape[0], 1, 1])) shutil.rmtree(tempdir) From e469f434deb530224923d54ead680f092c8872fd Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Mon, 8 Jun 2020 15:13:09 +0100 Subject: [PATCH 08/10] update docstring --- monai/data/nifti_saver.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/monai/data/nifti_saver.py b/monai/data/nifti_saver.py index e6846cc7c77..2dc59e12fad 100644 --- a/monai/data/nifti_saver.py +++ b/monai/data/nifti_saver.py @@ -105,6 +105,15 @@ def save(self, data: Union[torch.Tensor, np.ndarray], meta_data=None): def save_batch(self, batch_data: Union[torch.Tensor, np.ndarray], meta_data=None): """Save a batch of data into Nifti format files. + This function assumes the NIfTI dimension notations. + Spatially it supports up to three dimensions, that is, H, HW, HWD for + 1D, 2D, 3D respectively (resampling only supports 2D and 3D). + + When saving multiple time steps or multiple channels `batch_data`, + time and/or modality axes should be appended after the batch dimensions. + For example, the shape of a batch of 2D eight-class + segmentation probabilities to be saved could be `(batch, 8, 64, 64, 1)`, + or `(batch, 8, 64, 64)`. args: batch_data (Tensor or ndarray): target batch data content that save into NIfTI format. From c9806809badcd5d309c49d5049cd9fbbf33b4b8f Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Mon, 8 Jun 2020 15:47:32 +0100 Subject: [PATCH 09/10] additional tests/tuning --- monai/data/nifti_saver.py | 28 ++++++++++------- tests/test_handler_segmentation_saver.py | 4 ++- tests/test_nifti_saver.py | 38 +++++++++++++++++++++++- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/monai/data/nifti_saver.py b/monai/data/nifti_saver.py index 2dc59e12fad..2115615b308 100644 --- a/monai/data/nifti_saver.py +++ b/monai/data/nifti_saver.py @@ -9,11 +9,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Union, Optional +from typing import Optional, Union import numpy as np import torch + from monai.data.nifti_writer import write_nifti + from .utils import create_file_basename @@ -89,6 +91,9 @@ def save(self, data: Union[torch.Tensor, np.ndarray], meta_data=None): data = data.detach().cpu().numpy() filename = create_file_basename(self.output_postfix, filename, self.output_dir) filename = f"{filename}{self.output_ext}" + # change data shape to be (channel, h, w, d) + while len(data.shape) < 4: + data = np.expand_dims(data, -1) # change data to "channel last" format and write to nifti format file data = np.moveaxis(data, 0, -1) write_nifti( @@ -104,16 +109,17 @@ def save(self, data: Union[torch.Tensor, np.ndarray], meta_data=None): ) def save_batch(self, batch_data: Union[torch.Tensor, np.ndarray], meta_data=None): - """Save a batch of data into Nifti format files. - This function assumes the NIfTI dimension notations. - Spatially it supports up to three dimensions, that is, H, HW, HWD for - 1D, 2D, 3D respectively (resampling only supports 2D and 3D). - - When saving multiple time steps or multiple channels `batch_data`, - time and/or modality axes should be appended after the batch dimensions. - For example, the shape of a batch of 2D eight-class - segmentation probabilities to be saved could be `(batch, 8, 64, 64, 1)`, - or `(batch, 8, 64, 64)`. + """ + Save a batch of data into Nifti format files. + + Spatially it supports up to three dimensions, that is, H, HW, HWD for + 1D, 2D, 3D respectively (with resampling supports for 2D and 3D only). + + When saving multiple time steps or multiple channels `batch_data`, + time and/or modality axes should be appended after the batch dimensions. + For example, the shape of a batch of 2D eight-class + segmentation probabilities to be saved could be `(batch, 8, 64, 64, 1)`. + or `(batch, 8, 64, 64)` args: batch_data (Tensor or ndarray): target batch data content that save into NIfTI format. diff --git a/tests/test_handler_segmentation_saver.py b/tests/test_handler_segmentation_saver.py index 6d112d32bb2..9fb232e891f 100644 --- a/tests/test_handler_segmentation_saver.py +++ b/tests/test_handler_segmentation_saver.py @@ -12,10 +12,12 @@ import os import shutil import unittest + +import numpy as np import torch from ignite.engine import Engine from parameterized import parameterized -import numpy as np + from monai.handlers import SegmentationSaver TEST_CASE_0 = [".nii.gz"] diff --git a/tests/test_nifti_saver.py b/tests/test_nifti_saver.py index 406dbc632e2..d8a560a6360 100644 --- a/tests/test_nifti_saver.py +++ b/tests/test_nifti_saver.py @@ -12,8 +12,9 @@ import os import shutil import unittest -import torch +import numpy as np +import torch from monai.data import NiftiSaver @@ -32,6 +33,41 @@ def test_saved_content(self): self.assertTrue(os.path.exists(os.path.join(default_dir, filepath))) shutil.rmtree(default_dir) + def test_saved_resize_content(self): + default_dir = os.path.join(".", "tempdir") + shutil.rmtree(default_dir, ignore_errors=True) + + saver = NiftiSaver(output_dir=default_dir, output_postfix="seg", output_ext=".nii.gz", dtype=np.float32) + + meta_data = { + "filename_or_obj": ["testfile" + str(i) for i in range(8)], + "affine": [np.diag(np.ones(4)) * 5] * 8, + "original_affine": [np.diag(np.ones(4)) * 1.0] * 8, + } + saver.save_batch(torch.randint(0, 255, (8, 8, 2, 2)), meta_data) + for i in range(8): + filepath = os.path.join("testfile" + str(i), "testfile" + str(i) + "_seg.nii.gz") + self.assertTrue(os.path.exists(os.path.join(default_dir, filepath))) + shutil.rmtree(default_dir) + + def test_saved_3d_resize_content(self): + default_dir = os.path.join(".", "tempdir") + shutil.rmtree(default_dir, ignore_errors=True) + + saver = NiftiSaver(output_dir=default_dir, output_postfix="seg", output_ext=".nii.gz", dtype=np.float32) + + meta_data = { + "filename_or_obj": ["testfile" + str(i) for i in range(8)], + "spatial_shape": [(10, 10, 2)] * 8, + "affine": [np.diag(np.ones(4)) * 5] * 8, + "original_affine": [np.diag(np.ones(4)) * 1.0] * 8, + } + saver.save_batch(torch.randint(0, 255, (8, 8, 1, 2, 2)), meta_data) + for i in range(8): + filepath = os.path.join("testfile" + str(i), "testfile" + str(i) + "_seg.nii.gz") + self.assertTrue(os.path.exists(os.path.join(default_dir, filepath))) + shutil.rmtree(default_dir) + if __name__ == "__main__": unittest.main() From a6c3833f3b347910a23cc7bd60067d8a6bac8ccf Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 9 Jun 2020 19:38:13 +0100 Subject: [PATCH 10/10] update based on comments --- monai/data/nifti_saver.py | 5 +++-- monai/data/nifti_writer.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/monai/data/nifti_saver.py b/monai/data/nifti_saver.py index 2115615b308..29ba23ba962 100644 --- a/monai/data/nifti_saver.py +++ b/monai/data/nifti_saver.py @@ -118,8 +118,9 @@ def save_batch(self, batch_data: Union[torch.Tensor, np.ndarray], meta_data=None When saving multiple time steps or multiple channels `batch_data`, time and/or modality axes should be appended after the batch dimensions. For example, the shape of a batch of 2D eight-class - segmentation probabilities to be saved could be `(batch, 8, 64, 64, 1)`. - or `(batch, 8, 64, 64)` + segmentation probabilities to be saved could be `(batch, 8, 64, 64)`; + in this case each item in the batch will be saved as (64, 64, 1, 8) + NIfTI file (the third dimension is reserved as a spatial dimension). args: batch_data (Tensor or ndarray): target batch data content that save into NIfTI format. diff --git a/monai/data/nifti_writer.py b/monai/data/nifti_writer.py index 11a99662b2c..9f05cd92e9b 100644 --- a/monai/data/nifti_writer.py +++ b/monai/data/nifti_writer.py @@ -54,7 +54,8 @@ def write_nifti( When saving multiple time steps or multiple channels `data`, time and/or modality axes should be appended after the first three dimensions. For example, shape of 2D eight-class segmentation probabilities to be saved - could be `(64, 64, 1, 8)`, + could be `(64, 64, 1, 8)`. Also, data in shape (64, 64, 8), (64, 64, 8, 1) + will be considered as a single-channel 3D image. Args: data (numpy.ndarray): input data to write to file.