Skip to content

Adding __str__ to MetaTensor#4843

Merged
wyli merged 9 commits intoProject-MONAI:devfrom
ericspod:metatensor_str
Aug 10, 2022
Merged

Adding __str__ to MetaTensor#4843
wyli merged 9 commits intoProject-MONAI:devfrom
ericspod:metatensor_str

Conversation

@ericspod
Copy link
Copy Markdown
Member

@ericspod ericspod commented Aug 5, 2022

Signed-off-by: Eric Kerfoot [email protected]

Fixes #4840 .

Description

Add __str__ to MetaTensor to keep similar behavior with Pytorch tensors.

Status

Ready

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.
  • 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.

@ericspod ericspod requested review from Nic-Ma and rijobro August 5, 2022 15:47
Copy link
Copy Markdown
Contributor

@rijobro rijobro left a comment

Choose a reason for hiding this comment

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

Do you think you could make the relevant change in the tutorial? Otherwise, all good. Thanks!

@ericspod
Copy link
Copy Markdown
Member Author

ericspod commented Aug 5, 2022

I can get to the notebook. Which one is it? The colab link doesn't say where it is in the repo.

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Aug 5, 2022

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Aug 5, 2022

/build

@wyli wyli enabled auto-merge (squash) August 5, 2022 21:40
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Aug 5, 2022

/build

@ericspod
Copy link
Copy Markdown
Member Author

ericspod commented Aug 8, 2022

The way tensors are printed was changed in Pytorch 1.12 which could be used to print "MetaTensor" instead of "tensor" but would need to be taken into account in the tests.

@wyli wyli disabled auto-merge August 9, 2022 19:11
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Aug 9, 2022

with this PR, print(metatensor) will output exactly the same string as print(tensor)...we lose all the meta info in the string, without this PR, print(metatensor) is verbose, any solution in the middle? cc @Nic-Ma @rijobro

@Nic-Ma
Copy link
Copy Markdown
Contributor

Nic-Ma commented Aug 10, 2022

Hi @ericspod @wyli ,

Actually, I still don't quite understand the target goal of this PR, usually the output of __str__ is for human reading, just in the format of a regular string, why not add some meta information in it?

Thanks.

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Aug 10, 2022

Hi @ericspod @wyli ,

Actually, I still don't quite understand the target goal of this PR, usually the output of __str__ is for human reading, just in the format of a regular string, why not add some meta information in it?

Thanks.

Hi Nic, the issue is clearly described in #4840, the pr is trying to address 4840.

@Nic-Ma
Copy link
Copy Markdown
Contributor

Nic-Ma commented Aug 10, 2022

Hi @wyli ,

I checked the original ticket this morning, that's I don't quite understand...
Especially: "The design intent was to not require users to have to deal with MetaTensors, it's meant to be opt-in, but this change has a visible difference the unexpecting user would have to dig into to solve."
I think we want to highlight MetaTensor in MONAI release and let users to use it, why hide the meta information in the print?

Thanks.

@ericspod
Copy link
Copy Markdown
Member Author

The issue is that a lot of code will have print statements using tensor directly, with normal Pytorch tensors this isn't a problem but with MetaTensor it's way too verbose. It'll break some people's code I image who rely on output being in a specific format. It's just too surprising to use code that does known things in Pytorch then all of a sudden with MONAI produces a huge output with information that isn't likely relevant at the time. Users will be confused about where the metadata stuff came from, we want people to not have to deal with MetaTensor if they don't want but this forces them to do just that.

@Nic-Ma
Copy link
Copy Markdown
Contributor

Nic-Ma commented Aug 10, 2022

Hi @ericspod ,

OK, I see your point.
Could you please help add clear doc-string to the __str__ function and __repr__ function to let users or developers know the usage?

Thanks.

Signed-off-by: Eric Kerfoot <[email protected]>
@ericspod
Copy link
Copy Markdown
Member Author

Hi @ericspod ,

OK, I see your point. Could you please help add clear doc-string to the __str__ function and __repr__ function to let users or developers know the usage?

Thanks.

Done.

Copy link
Copy Markdown
Contributor

@Nic-Ma Nic-Ma left a comment

Choose a reason for hiding this comment

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

Thanks for the quick update.
Looks good to me.

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Aug 10, 2022

/build

@wyli wyli enabled auto-merge (squash) August 10, 2022 15:30
@wyli wyli merged commit b087b11 into Project-MONAI:dev Aug 10, 2022
@ericspod ericspod deleted the metatensor_str branch August 10, 2022 16:09
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.

MetaTensor Needs __str__ Method

4 participants