Commit 6092da9
authored
Less verbose meta tensor print (#5658)
### Description
We've had feedback that with MetaTensors, `x.__repr__` is overly
verbose. This is used frequently when debugging with an IDE for example.
The problem is accentuated with e.g., `x.max()`. Here, a single value is
hidden by over 50 lines of text.
Example:
```python
import monai.transforms as mt
loader = mt.LoadImage(image_only=True)
x = loader(x)
print(x.max().__repr__())
```
<details>
<summary>Output</summary>
```
tensor(2906.1714)
Metadata
sizeof_hdr: 348
extents: 0
session_error: 0
dim_info: 0
dim: [ 3 161 338 61 1 1 1 1]
intent_p1: 0.0
intent_p2: 0.0
intent_p3: 0.0
intent_code: 0
datatype: 16
bitpix: 32
slice_start: 0
pixdim: [1. 1. 1. 2.000296 0. 0. 0. 0. ]
vox_offset: 0.0
scl_slope: nan
scl_inter: nan
slice_end: 0
slice_code: 0
xyzt_units: 2
cal_max: 0.0
cal_min: 0.0
slice_duration: 0.0
toffset: 0.0
glmax: 0
glmin: 0
qform_code: 1
sform_code: 0
quatern_b: -0.4889228641986847
quatern_c: 0.5108369588851929
quatern_d: -0.4703003466129303
qoffset_x: -77.69402313232422
qoffset_y: 218.0015411376953
qoffset_z: -808.5780029296875
srow_x: [0. 0. 0. 0.]
srow_y: [0. 0. 0. 0.]
srow_z: [0. 0. 0. 0.]
affine: tensor([[ 3.5726e-02, -2.8526e-03, 1.9990e+00, -7.7694e+01],
[-9.9619e-01, 7.9544e-02, 7.1691e-02, 2.1800e+02],
[-7.9595e-02, -9.9683e-01, 1.3722e-07, -8.0858e+02],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 1.0000e+00]],
dtype=torch.float64)
original_affine: [[ 3.57263708e-02 -2.85262855e-03 1.99901100e+00 -7.76940231e+01]
[-9.96186848e-01 7.95440337e-02 7.16907652e-02 2.18001541e+02]
[-7.95951681e-02 -9.96827272e-01 1.37223043e-07 -8.08578003e+02]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
as_closest_canonical: False
spatial_shape: [161 338 61]
space: RAS
original_channel_dim: no_channel
filename_or_obj: /Users/rich/Downloads/sub-verse004_ct.nii.gz
Applied operations
[]
Is batch?: False
```
</details>
I'm proposing that the `__repr__` and `__str__` match that of
`torch.Tensor`. If users want meta data they can use `x.print_verbose()`
or `print(x.meta)`.
### 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] Breaking change (fix or new feature that would cause existing
functionality to change).
- [x] New tests added to cover the changes.
- [x] In-line docstrings updated.
- [x] Documentation updated, tested `make html` command in the `docs/`
folder.
Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>1 parent 16b13a4 commit 6092da9
2 files changed
Lines changed: 16 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
269 | 269 | | |
270 | 270 | | |
271 | 271 | | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | 272 | | |
276 | 273 | | |
277 | 274 | | |
| |||
281 | 278 | | |
282 | 279 | | |
283 | 280 | | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
| |||
532 | 532 | | |
533 | 533 | | |
534 | 534 | | |
535 | | - | |
| 535 | + | |
536 | 536 | | |
537 | | - | |
538 | | - | |
539 | | - | |
540 | | - | |
| 537 | + | |
| 538 | + | |
541 | 539 | | |
542 | | - | |
| 540 | + | |
543 | 541 | | |
544 | 542 | | |
545 | 543 | | |
546 | | - | |
| 544 | + | |
| 545 | + | |
547 | 546 | | |
548 | 547 | | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
423 | 423 | | |
424 | 424 | | |
425 | 425 | | |
| 426 | + | |
426 | 427 | | |
427 | 428 | | |
428 | 429 | | |
| |||
0 commit comments