Skip to content

Commit 2713c2b

Browse files
slicepasteeinsyang723IamTingTingericspodKumoLiu
authored
Update ['pixdim'] after Spacing transform in meta dict. (#8269)
Fixes #6840 ### Description According to the issue, this PR addresses on the meta dictionary `data['pixdim']` of NIfTI images does not update after applying the `spacing` or `spacingd`. To align with `affine`, we update `data['pixdim']` and keep the original metainfo in `data['original_pixdim']`. Additionally, this PR also update the metainfo `key_{meta_key_postfix}['pixdim']` in NIfTI images, consistent with `spaced_data_dict['image_meta_dict']['pixdim']` in issue #6832. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [ ] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Wei_Chuan, Chiang <jamesis1356@gmail.com> Co-authored-by: einsyang723 <tina910723@gmail.com> Co-authored-by: IamTingTing <6121smile@gmail.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
1 parent 958dac7 commit 2713c2b

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

monai/data/image_reader.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,8 @@ def get_data(self, img) -> tuple[np.ndarray, dict]:
11131113

11141114
for i, filename in zip(ensure_tuple(img), self.filenames):
11151115
header = self._get_meta_dict(i)
1116+
if MetaKeys.PIXDIM in header:
1117+
header[MetaKeys.ORIGINAL_PIXDIM] = np.array(header[MetaKeys.PIXDIM], copy=True)
11161118
header[MetaKeys.AFFINE] = self._get_affine(i)
11171119
header[MetaKeys.ORIGINAL_AFFINE] = self._get_affine(i)
11181120
header["as_closest_canonical"] = self.as_closest_canonical

monai/transforms/inverse.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from monai import transforms
2323
from monai.data.meta_obj import MetaObj, get_track_meta
2424
from monai.data.meta_tensor import MetaTensor
25-
from monai.data.utils import to_affine_nd
25+
from monai.data.utils import affine_to_spacing, to_affine_nd
2626
from monai.transforms.traits import InvertibleTrait
2727
from monai.transforms.transform import Transform
2828
from monai.utils import (
@@ -224,6 +224,9 @@ def track_transform_meta(
224224
else:
225225
raise
226226
out_obj.meta[MetaKeys.AFFINE] = convert_to_tensor(affine, device=torch.device("cpu"), dtype=torch.float64)
227+
if MetaKeys.PIXDIM in out_obj.meta:
228+
spacing = affine_to_spacing(out_obj.meta[MetaKeys.AFFINE])
229+
out_obj.meta[MetaKeys.PIXDIM][1 : 1 + len(spacing)] = spacing
227230

228231
if not (get_track_meta() and transform_info and transform_info.get(TraceKeys.TRACING)):
229232
if isinstance(data, Mapping):

monai/transforms/spatial/dictionary.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from monai.data.box_utils import BoxMode, StandardMode
3030
from monai.data.meta_obj import get_track_meta
3131
from monai.data.meta_tensor import MetaTensor
32+
from monai.data.utils import is_supported_format
3233
from monai.networks.layers.simplelayers import GaussianFilter
3334
from monai.transforms.croppad.array import CenterSpatialCrop
3435
from monai.transforms.inverse import InvertibleTransform
@@ -521,6 +522,13 @@ def __call__(self, data: Mapping[Hashable, torch.Tensor], lazy: bool | None = No
521522
output_spatial_shape=output_shape_k if should_match else None,
522523
lazy=lazy_,
523524
)
525+
if isinstance(d[key], MetaTensor):
526+
meta_keys = [k for k in d.keys() if k is not None and k.startswith(f"{key}_")]
527+
for meta_key in meta_keys:
528+
if "filename_or_obj" in d[key].meta and is_supported_format(
529+
d[key].meta["filename_or_obj"], ["nii", "nii.gz"]
530+
):
531+
d[meta_key].update(d[key].meta)
524532
if output_shape_k is None:
525533
output_shape_k = d[key].peek_pending_shape() if isinstance(d[key], MetaTensor) else d[key].shape[1:]
526534
return d

monai/utils/enums.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ class MetaKeys(StrEnum):
542542
Typical keys for MetaObj.meta
543543
"""
544544

545+
PIXDIM = "pixdim" # MetaTensor.pixdim
546+
ORIGINAL_PIXDIM = "original_pixdim" # the pixdim after image loading before any data processing
545547
AFFINE = "affine" # MetaTensor.affine
546548
ORIGINAL_AFFINE = "original_affine" # the affine after image loading before any data processing
547549
SPATIAL_SHAPE = "spatial_shape" # optional key for the length in each spatial dimension

0 commit comments

Comments
 (0)