Skip to content

Commit fc1156a

Browse files
slicepasteeinsyang723IamTingTing
committed
fix the issue of the naming rule of key_meta_dict
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. Signed-off-by: Wei_Chuan, Chiang <[email protected]> Co-authored-by: einsyang723 <[email protected]> Co-authored-by: IamTingTing <[email protected]>
1 parent 9e51576 commit fc1156a

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

monai/transforms/spatial/dictionary.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import numpy as np
2525
import torch
2626

27-
import monai.transforms as transforms
2827
from monai.config import DtypeLike, KeysCollection, SequenceStr
2928
from monai.config.type_definitions import NdarrayOrTensor
3029
from monai.data.box_utils import BoxMode, StandardMode
@@ -522,11 +521,13 @@ def __call__(self, data: Mapping[Hashable, torch.Tensor], lazy: bool | None = No
522521
output_spatial_shape=output_shape_k if should_match else None,
523522
lazy=lazy_,
524523
)
525-
if isinstance(d[key], MetaTensor) and f"{key}_meta_dict" in d:
526-
if "filename_or_obj" in d[key].meta and is_supported_format(
527-
d[key].meta["filename_or_obj"], ["nii", "nii.gz"]
528-
):
529-
d = transforms.sync_meta_info(key, d)
524+
if isinstance(d[key], MetaTensor):
525+
meta_keys = [k for k in d.keys() if k is not None and k.startswith(f"{key}_")]
526+
for meta_key in meta_keys:
527+
if "filename_or_obj" in d[key].meta and is_supported_format(
528+
d[key].meta["filename_or_obj"], ["nii", "nii.gz"]
529+
):
530+
d[meta_key].update(d[key].meta)
530531
if output_shape_k is None:
531532
output_shape_k = d[key].peek_pending_shape() if isinstance(d[key], MetaTensor) else d[key].shape[1:]
532533
return d

0 commit comments

Comments
 (0)