Skip to content

Commit 52f2f6f

Browse files
MathijsdeBoerjak0bw
authored andcommitted
Project-MONAI#6213 Prepend "meta" to MetaTensor.__repr__ and MetaTensor.__str__ for easier identification (Project-MONAI#6214)
Discussed in Project-MONAI#6213 ### Description Prepends `"meta"` to the `MetaTensor.__repr__` and `MetaTensor.__str__` output so printing a MetaTensor does not look the exact same as a regular `torch.Tensor`. I don't expect this change to cause any breaks, with me running the risk of invoking [xkcd 1172](https://xkcd.com/1172/). --- 1 failure in `./runtests.sh -f -u --net --coverage`: ```text ====================================================================== FAIL: test_values (tests.test_tciadataset.TestTciaDataset) ---------------------------------------------------------------------- Traceback (most recent call last): File "D:\PythonProjects\MONAI-SaveImageFormatting\tests\test_tciadataset.py", line 72, in test_values self.assertTrue( AssertionError: False is not true ``` ### 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). - [x] New tests added to cover the changes. - [x] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [x] In-line docstrings updated. --------- Signed-off-by: Mathijs de Boer <m.deboer-41@umcutrecht.nl>
1 parent 5a66787 commit 52f2f6f

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

monai/data/meta_tensor.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,17 +569,19 @@ def ensure_torch_and_prune_meta(
569569

570570
def __repr__(self):
571571
"""
572-
Prints a representation of the tensor identical to ``torch.Tensor.__repr__``.
572+
Prints a representation of the tensor.
573+
Prepends "meta" to ``torch.Tensor.__repr__``.
573574
Use ``print_verbose`` for associated metadata.
574575
"""
575-
return self.as_tensor().__repr__()
576+
return f"meta{self.as_tensor().__repr__()}"
576577

577578
def __str__(self):
578579
"""
579-
Prints a representation of the tensor identical to ``torch.Tensor.__str__``.
580+
Prints a representation of the tensor.
581+
Prepends "meta" to ``torch.Tensor.__str__``.
580582
Use ``print_verbose`` for associated metadata.
581583
"""
582-
return str(self.as_tensor())
584+
return f"meta{str(self.as_tensor())}"
583585

584586
def print_verbose(self) -> None:
585587
"""Verbose print with meta data."""

tests/test_meta_tensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ def test_decollate(self, dtype):
430430

431431
def test_str(self):
432432
t = MetaTensor([1.0], affine=torch.tensor(1), meta={"fname": "filename"})
433-
self.assertEqual(str(t), "tensor([1.])")
434-
self.assertEqual(t.__repr__(), "tensor([1.])")
433+
self.assertEqual(str(t), "metatensor([1.])")
434+
self.assertEqual(t.__repr__(), "metatensor([1.])")
435435

436436
def test_shape(self):
437437
s = MetaTensor([1])

0 commit comments

Comments
 (0)