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

6522 update __format__ impl of MetaTensor #6523

Merged
merged 2 commits into from
May 17, 2023
Merged
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
6 changes: 6 additions & 0 deletions monai/data/meta_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,12 @@ def __str__(self):
"""
return f"meta{str(self.as_tensor())}"

def __format__(self, format_spec):
"""
returns the output of pytorch tensor's ``__format__`` method.
"""
return self.as_tensor().__format__(format_spec)
wyli marked this conversation as resolved.
Show resolved Hide resolved

def print_verbose(self) -> None:
"""Verbose print with meta data."""
print(self)
Expand Down
1 change: 1 addition & 0 deletions tests/test_meta_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ def test_str(self):
t = MetaTensor([1.0], affine=torch.tensor(1), meta={"fname": "filename"})
self.assertEqual(str(t), "metatensor([1.])")
self.assertEqual(t.__repr__(), "metatensor([1.])")
self.assertEqual(f"{t[0]:.2f}", "1.00")

def test_shape(self):
s = MetaTensor([1])
Expand Down