Skip to content

Commit 8e725aa

Browse files
6522 update __format__ impl of MetaTensor (#6523)
Fixes #6522 ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] 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: Wenqi Li <wenqil@nvidia.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e084885 commit 8e725aa

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

monai/data/meta_tensor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,12 @@ def __str__(self):
590590
"""
591591
return f"meta{str(self.as_tensor())}"
592592

593+
def __format__(self, format_spec):
594+
"""
595+
returns the output of pytorch tensor's ``__format__`` method.
596+
"""
597+
return self.as_tensor().__format__(format_spec)
598+
593599
def print_verbose(self) -> None:
594600
"""Verbose print with meta data."""
595601
print(self)

tests/test_meta_tensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ def test_str(self):
433433
t = MetaTensor([1.0], affine=torch.tensor(1), meta={"fname": "filename"})
434434
self.assertEqual(str(t), "metatensor([1.])")
435435
self.assertEqual(t.__repr__(), "metatensor([1.])")
436+
self.assertEqual(f"{t[0]:.2f}", "1.00")
436437

437438
def test_shape(self):
438439
s = MetaTensor([1])

0 commit comments

Comments
 (0)