Skip to content

Less verbose meta tensor print#5658

Merged
wyli merged 3 commits intoProject-MONAI:devfrom
rijobro:meta_tensor_verbose
Dec 5, 2022
Merged

Less verbose meta tensor print#5658
wyli merged 3 commits intoProject-MONAI:devfrom
rijobro:meta_tensor_verbose

Conversation

@rijobro
Copy link
Copy Markdown
Contributor

@rijobro rijobro commented Dec 5, 2022

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:

import monai.transforms as mt
loader = mt.LoadImage(image_only=True)
x = loader(x)
print(x.max().__repr__())
Output
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

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

  • 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.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

Signed-off-by: Richard Brown <[email protected]>
@rijobro rijobro requested review from Nic-Ma, ericspod and wyli December 5, 2022 16:31
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Dec 5, 2022

/build

Copy link
Copy Markdown
Contributor

@wyli wyli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR's description has received ❤️ s

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Dec 5, 2022

/build

@wyli wyli enabled auto-merge (squash) December 5, 2022 19:38
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Dec 5, 2022

/build

@wyli wyli merged commit 6092da9 into Project-MONAI:dev Dec 5, 2022
@rijobro rijobro deleted the meta_tensor_verbose branch December 6, 2022 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants