Skip to content

Commit

Permalink
6522 update __format__ impl of MetaTensor (#6523)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
wyli and pre-commit-ci[bot] authored May 17, 2023
1 parent e084885 commit 8e725aa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
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)

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

0 comments on commit 8e725aa

Please sign in to comment.