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 6 commits into
base: dev
Choose a base branch
from

Conversation

slicepaste
Copy link
Contributor

@slicepaste slicepaste commented Dec 23, 2024

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

  • 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.

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 Project-MONAI#6832.

Signed-off-by: Wei_Chuan, Chiang <[email protected]>
Co-authored-by: einsyang723 <[email protected]>
Co-authored-by: IamTingTing <[email protected]>
@slicepaste slicepaste changed the title Fixes #6840 Update ['pixdim'] after Spacing transform in meta dict. Dec 23, 2024
Signed-off-by: Wei_Chuan, Chiang <[email protected]>
Signed-off-by: Wei_Chuan, Chiang <[email protected]>
Signed-off-by: Wei_Chuan, Chiang <[email protected]>
Signed-off-by: Wei_Chuan, Chiang <[email protected]>
Signed-off-by: Wei_Chuan, Chiang <[email protected]>
@@ -477,6 +477,10 @@ def pixdim(self):
return [affine_to_spacing(a) for a in self.affine]
return affine_to_spacing(self.affine)

def set_pixdim(self) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense for this to be a method? If it's only going to be called in one place it's simple code could just be put there. If there's anticipation that this would be called by other things then that's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your quick reply.
Originally, we considered other files such as DICOM might use Spacing, which could involve the usage and access meta_tensor.py property. Therefore, we decided to define a method.
However, after we reevaluating the entire codebase this week, it might be better to modify data["pixdim"] directly within TraceableTransform instead.

@@ -535,6 +535,9 @@ def __call__(
dtype=dtype,
lazy=lazy_,
)
if isinstance(data_array, MetaTensor) and "pixdim" in data_array.meta:
data_array = cast(MetaTensor, data_array.clone())
Copy link
Member

Choose a reason for hiding this comment

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

Why is it necessary to clone the data array here? This is going to have a cost and I think isn't compatible with lazy resampling. Perhaps this is code that should be SpactialResample instead? @atbenmurray If you could please check if this is going to interact with laziness, thanks.

Copy link
Contributor Author

@slicepaste slicepaste Jan 9, 2025

Choose a reason for hiding this comment

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

@ericspod Thank you for your suggestion.

We initially considered using data_array.clone() based on the following issue:

# LoadImage
data = {'image', 'data1.nii'}
imgloader = LoadImaged(keys=('image'), image_only=False, ensure_channel_first=True)
input_data_dict = imgloader(data)

# Spacing
respacing = transforms.Spacingd(keys=['image', 'label'], pixdim=(1, 1, 10), mode=('bilinear'))
spaced_data_dict = respacing(input_data_dict)

Originally, if we didn't use data_array.clone() and directly modified the data, the MetaTensor in both input_data_dict and spaced_data_dict would be affected simultaneously.
This means input_data_dict would lose its original input_data_dict["pixdim"] information.

However, as suggested above, using .clone() in this way is not ideal as it introduces additional costs.
If we perform this modification within TraceableTransform.track_transform_meta() method, which is executed by SpatialResample, it might no longer be a concern.

cc @einsyang723 @IamTingTing

Copy link
Contributor

@KumoLiu KumoLiu left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Leave several comments inline.

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.

@@ -535,6 +535,9 @@ def __call__(
dtype=dtype,
lazy=lazy_,
)
if isinstance(data_array, MetaTensor) and "pixdim" in data_array.meta:
data_array = cast(MetaTensor, data_array.clone())
data_array.set_pixdim()
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that the pixel dimensions (pixdim) are only updated in the Spacing transformation. This is why I previously suggested that we should only retain the original_pixdim, as the latest pixdim can be derived from the metadata in the MetaTensor and the affine transformation.

Additionally, the use of meta_dict for logging metadata is becoming outdated. Perhaps we no longer need to maintain it? What are your thoughts on this?

import torch
from monai.transforms import Spacing

data = torch.randn(2, 1, 32, 32, 32)
trans = Spacing(pixdim=(1.5, 1.5, 1.0))
out = trans(data)
print(out.pixdim) -- > (1.5, 1.5, 1.0)

cc @ericspod @Nic-Ma

Choose a reason for hiding this comment

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

@KumoLiu Thank you for your detailed feedback.

Our original thinking was that all related information should be update, including:

data['image']['pixdim']
data['image'].pixdim
data['image_meta_dict']['pixdim']

However, after reading your response, we're a bit uncertain: are you suggesting that we don't need to update pixdim in {key}_meta_dict?
Or will there be a new way to handle {key}_meta_dict in future releases?
If possible, could you please elaborate on this? Thank you.

cc @slicepaste @IamTingTing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modify pixdim to original_pixdim in meta dict
4 participants