Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6136 6146 update the default writer flag #6147

Merged
merged 13 commits into from
Mar 16, 2023
15 changes: 10 additions & 5 deletions monai/data/image_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ class ITKWriter(ImageWriter):
output_dtype: DtypeLike = None
channel_dim: int | None

def __init__(self, output_dtype: DtypeLike = np.float32, affine_lps_to_ras: bool = True, **kwargs):
def __init__(self, output_dtype: DtypeLike = np.float32, affine_lps_to_ras: bool | None = None, **kwargs):
wyli marked this conversation as resolved.
Show resolved Hide resolved
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
"""
Args:
output_dtype: output data type.
affine_lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to ``True``.
affine_lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to ``None``.
Set to ``True`` to be consistent with ``NibabelWriter``,
otherwise the affine matrix is assumed already in the ITK convention.
kwargs: keyword arguments passed to ``ImageWriter``.
Expand Down Expand Up @@ -478,7 +478,7 @@ def create_backend_obj(
channel_dim: int | None = 0,
affine: NdarrayOrTensor | None = None,
dtype: DtypeLike = np.float32,
affine_lps_to_ras: bool = True,
affine_lps_to_ras: bool | None = None,
**kwargs,
):
"""
Expand All @@ -489,16 +489,21 @@ def create_backend_obj(
channel_dim: channel dimension of the data array. This is used to create a Vector Image if it is not ``None``.
affine: affine matrix of the data array. This is used to compute `spacing`, `direction` and `origin`.
dtype: output data type.
affine_lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to ``True``.
affine_lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to ``None``.
Set to ``True`` to be consistent with ``NibabelWriter``,
otherwise the affine matrix is assumed already in the ITK convention.
kwargs: keyword arguments. Current `itk.GetImageFromArray` will read ``ttype`` from this dictionary.

see also:

- https://github.com/InsightSoftwareConsortium/ITK/blob/v5.2.1/Wrapping/Generators/Python/itk/support/extras.py#L389

"""
if isinstance(data_array, MetaTensor) and data_array.meta.get(MetaKeys.SPACE, SpaceKeys.LPS) != SpaceKeys.LPS:
if (
isinstance(data_array, MetaTensor)
and affine_lps_to_ras is None
and data_array.meta.get(MetaKeys.SPACE, SpaceKeys.LPS) != SpaceKeys.LPS
):
affine_lps_to_ras = False # do the converting from LPS to RAS only if the space type is currently LPS.
data_array = super().create_backend_obj(data_array)
_is_vec = channel_dim is not None
Expand Down
1 change: 1 addition & 0 deletions monai/transforms/io/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ def set_options(self, init_kwargs=None, data_kwargs=None, meta_kwargs=None, writ
self.meta_kwargs.update(meta_kwargs)
if write_kwargs is not None:
self.write_kwargs.update(write_kwargs)
return self

def __call__(self, img: torch.Tensor | np.ndarray, meta_data: dict | None = None):
"""
Expand Down
1 change: 1 addition & 0 deletions monai/transforms/io/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def __init__(

def set_options(self, init_kwargs=None, data_kwargs=None, meta_kwargs=None, write_kwargs=None):
self.saver.set_options(init_kwargs, data_kwargs, meta_kwargs, write_kwargs)
return self

def __call__(self, data):
d = dict(data)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_image_rw.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def nrrd_rw(self, test_data, reader, writer, dtype, resample=True):
filepath = f"testfile_{ndim}d"
saver = SaveImage(
output_dir=self.test_dir, output_ext=output_ext, resample=resample, separate_folder=False, writer=writer
)
).set_options(init_kwargs={"affine_lps_to_ras": True})
test_data = MetaTensor(
p(test_data), meta={"filename_or_obj": f"{filepath}{output_ext}", "spatial_shape": test_data.shape}
)
Expand Down