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

Update ['pixdim'] after Spacing transform in meta dict. #8269

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions monai/data/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,8 @@ def get_data(self, img) -> tuple[np.ndarray, dict]:

for i, filename in zip(ensure_tuple(img), self.filenames):
header = self._get_meta_dict(i)
if MetaKeys.PIXDIM in header:
header[MetaKeys.ORIGINAL_PIXDIM] = np.array(header[MetaKeys.PIXDIM], copy=True)
header[MetaKeys.AFFINE] = self._get_affine(i)
header[MetaKeys.ORIGINAL_AFFINE] = self._get_affine(i)
header["as_closest_canonical"] = self.as_closest_canonical
Expand Down
5 changes: 4 additions & 1 deletion monai/transforms/inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from monai import transforms
from monai.data.meta_obj import MetaObj, get_track_meta
from monai.data.meta_tensor import MetaTensor
from monai.data.utils import to_affine_nd
from monai.data.utils import affine_to_spacing, to_affine_nd
from monai.transforms.traits import InvertibleTrait
from monai.transforms.transform import Transform
from monai.utils import (
Expand Down Expand Up @@ -197,6 +197,9 @@ def track_transform_meta(
else:
raise
out_obj.meta[MetaKeys.AFFINE] = convert_to_tensor(affine, device=torch.device("cpu"), dtype=torch.float64)
if MetaKeys.PIXDIM in out_obj.meta:
spacing = affine_to_spacing(out_obj.meta[MetaKeys.AFFINE])
out_obj.meta[MetaKeys.PIXDIM][1 : 1 + len(spacing)] = spacing

if not (get_track_meta() and transform_info and transform_info.get(TraceKeys.TRACING)):
if isinstance(data, Mapping):
Expand Down
7 changes: 7 additions & 0 deletions monai/transforms/spatial/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
import numpy as np
import torch

import monai.transforms as transforms
from monai.config import DtypeLike, KeysCollection, SequenceStr
from monai.config.type_definitions import NdarrayOrTensor
from monai.data.box_utils import BoxMode, StandardMode
from monai.data.meta_obj import get_track_meta
from monai.data.meta_tensor import MetaTensor
from monai.data.utils import is_supported_format
from monai.networks.layers.simplelayers import GaussianFilter
from monai.transforms.croppad.array import CenterSpatialCrop
from monai.transforms.inverse import InvertibleTransform
Expand Down Expand Up @@ -520,6 +522,11 @@ def __call__(self, data: Mapping[Hashable, torch.Tensor], lazy: bool | None = No
output_spatial_shape=output_shape_k if should_match else None,
lazy=lazy_,
)
if isinstance(d[key], MetaTensor) and f"{key}_meta_dict" in d:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may need to be more generalized. There are options in LoadImaged to change the default naming convention between a key and the meta_dict for that key. meta_keys and meta_key_postfix can change this behaviour. I don't know how well changing those from their defaults is supported, however.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atbenmurray Thank you for your suggestion. We plan to implement checking all dictionary keys that start with {key}_ to support custom settings of meta_keys and meta_key_postfix. This ensures that no matter how users configure the naming conventions in LoadImaged, we can correctly synchronize metadata from the MetaTensor to the corresponding meta dictionary.

During implementation, we discovered that sync_meta_info() doesn't properly synchronize meta dictionaries with custom postfixes, and it creates a new key using the default postfix.

For example, if the original key is 'image' and the postfix is changed to 'dict', there would be two keys: 'image' and 'image_dict'. However, after executing sync_meta_info(), a third key 'image_meta_dict' is created, using the default postfix.

In our implementation, we've worked around this by directly updating the existing meta dictionaries, which avoids the creation of additional keys. Would you consider this behavior something that should be addressed in a separate issue, or is our current approach sufficient? We'd appreciate your thoughts on this.

cc @slicepaste @IamTingTing

if "filename_or_obj" in d[key].meta and is_supported_format(
d[key].meta["filename_or_obj"], ["nii", "nii.gz"]
):
d = transforms.sync_meta_info(key, d)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask why we need this sync here seems it already been done in the MapTransform?

list_d[idx] = transforms.sync_meta_info(k, dict_i, t=not isinstance(self, transforms.InvertD))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your question. In MapTransform, it only synchronizes MetaTensor, but since {key}_meta_dict is not a MetaTensor format, it won't be updated automatically. That's why we added this line of code to ensure the information in "{key}_meta_dict gets synchronized as well.

if output_shape_k is None:
output_shape_k = d[key].peek_pending_shape() if isinstance(d[key], MetaTensor) else d[key].shape[1:]
return d
Expand Down
2 changes: 2 additions & 0 deletions monai/utils/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ class MetaKeys(StrEnum):
Typical keys for MetaObj.meta
"""

PIXDIM = "pixdim" # MetaTensor.pixdim
ORIGINAL_PIXDIM = "original_pixdim" # the pixdim after image loading before any data processing
AFFINE = "affine" # MetaTensor.affine
ORIGINAL_AFFINE = "original_affine" # the affine after image loading before any data processing
SPATIAL_SHAPE = "spatial_shape" # optional key for the length in each spatial dimension
Expand Down
Loading