Skip to content

Commit

Permalink
#6213 Prepend "meta" to MetaTensor.__repr__ and `MetaTensor.__str…
Browse files Browse the repository at this point in the history
…__` for easier identification (#6214)

Discussed in #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 <[email protected]>
  • Loading branch information
MathijsdeBoer authored Mar 21, 2023
1 parent 0a45777 commit 554b172
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions monai/data/meta_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,17 +569,19 @@ def ensure_torch_and_prune_meta(

def __repr__(self):
"""
Prints a representation of the tensor identical to ``torch.Tensor.__repr__``.
Prints a representation of the tensor.
Prepends "meta" to ``torch.Tensor.__repr__``.
Use ``print_verbose`` for associated metadata.
"""
return self.as_tensor().__repr__()
return f"meta{self.as_tensor().__repr__()}"

def __str__(self):
"""
Prints a representation of the tensor identical to ``torch.Tensor.__str__``.
Prints a representation of the tensor.
Prepends "meta" to ``torch.Tensor.__str__``.
Use ``print_verbose`` for associated metadata.
"""
return str(self.as_tensor())
return f"meta{str(self.as_tensor())}"

def print_verbose(self) -> None:
"""Verbose print with meta data."""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_meta_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ def test_decollate(self, dtype):

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

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

0 comments on commit 554b172

Please sign in to comment.