Skip to content

Type classes for lazy resampling #5418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 28, 2022
Merged

Type classes for lazy resampling #5418

merged 12 commits into from
Oct 28, 2022

Conversation

atbenmurray
Copy link
Contributor

@atbenmurray atbenmurray commented Oct 27, 2022

Signed-off-by: Ben Murray [email protected]

Fixes # .

Description

A few sentences describing the changes proposed in this pull request.

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.

Copy link
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.

Thanks, it looks good to me... cc @ericspod @Nic-Ma @rijobro if you don't have any other concerns I'll merge this one today

@Nic-Ma
Copy link
Contributor

Nic-Ma commented Oct 27, 2022

Hi @wyli @atbenmurray ,

I see you 2 experts submitted several PRs today, I was busy with other tasks, will try to review all of them tomorrow.
Please keep it open for 1 more day.

Thanks.

@atbenmurray atbenmurray mentioned this pull request Oct 27, 2022
33 tasks
@atbenmurray atbenmurray marked this pull request as ready for review October 27, 2022 18:40
Copy link
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.

May I know why use the class name with "XXXType"? LazyTransform may be enough? Like other classes in the codebase.

Thanks.

@atbenmurray
Copy link
Contributor Author

atbenmurray commented Oct 28, 2022

May I know why use the class name with "XXXType"? LazyTransform may be enough? Like other classes in the codebase.

Thanks.
Hi @Nic-Ma,
LazyTransformType is a pure abstract class that can be used by anyone whose transform is capable of lazy operation. It defines a bit of signature that should be adhered to but no implementation. If we then decide that there is common functionality that we want to put into our own transforms, we can have a LazyTransform that inherits from LazyTransformType that also provides that functionality.
Outside of python, it is generally considered good practice to provide a pure type and implementation that inherits from it. It helps with issues like dependency management, and also allows people to wrap existing types more simply. I don't know why people don't do this in python in general; our lives would be much easier if pytorch provided a Tensor interface that their Tensor implementation inherits, for example.

Initially, I was going to call it ILazyTransform as this is a convention in other languages, but @ericspod requested that I rename it. I could see any of the following working:

  1. ILazyTransform -> LazyTransform
  2. LazyTransformType -> LazyTransform
  3. LazyTransform -> LazyTransformBase

I could do 3. but then we would be changing RandomizableTransform -> RandomizableTransformBase and I think that would cause problems, so I went with 2.

@wyli
Copy link
Contributor

wyli commented Oct 28, 2022

I don't know why people don't do this in python in general; our lives would be much easier if pytorch provided a Tensor interface that their Tensor implementation inherits, for example.

numpy and pytorch defines the __array_function__ protocol instead of defining abstract classes which is in my opinion more flexible duck typing pattern...

@atbenmurray
Copy link
Contributor Author

atbenmurray commented Oct 28, 2022

I don't know why people don't do this in python in general; our lives would be much easier if pytorch provided a Tensor interface that their Tensor implementation inherits, for example.

numpy and pytorch defines the __array_function__ protocol instead of defining abstract classes which is in my opinion more flexible duck typing pattern...

This kind of dispatch method is certainly a useful way of solving issues with operator overloading, but it doesn't solve every problem with subtyping ndarrays or tensors. For example, I would have preferred to solve the multi sampling issue by having metatensor keep an internal data tensor that could be shared between multiple metatensor instances, which would be possible with a TensorType interface to inherit from but not possible as metatensor is a tensor, rather than just appearing to be a tensor (this is IMO a shortcoming of pytorch that they don't provide a pure type signature).

I agree that a pure duck typing approach is quite pythonic, but the big libraries use isinstance a lot to validate their inputs which means that you can't get away with a class that walks and quacks like a tensor but isn't of Tensor type. I think people are generally more used to that than they are that checking whether a method has certain attributes or not.

@Nic-Ma
Copy link
Contributor

Nic-Ma commented Oct 28, 2022

Hi @ericspod ,

May I know your opinion about this naming question?
I prefer to any method that is consistent with our existing classes in the codebase as we are already 1.0..

Thanks in advance.

atbenmurray and others added 2 commits October 28, 2022 17:06
@wyli
Copy link
Contributor

wyli commented Oct 28, 2022

/black

@wyli
Copy link
Contributor

wyli commented Oct 28, 2022

/build

@wyli wyli enabled auto-merge (squash) October 28, 2022 17:34
@atbenmurray
Copy link
Contributor Author

It is saying the blossom-ci is failing, but when I check it, is says that it succeeded, but I suspect part of it is not running

@wyli
Copy link
Contributor

wyli commented Oct 28, 2022

It is saying the blossom-ci is failing, but when I check it, is says that it succeeded, but I suspect part of it is not running

I guess torch 1.13 released a few hours ago changed some of the typing. probably unrelated to this PR...

Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Contributor

wyli commented Oct 28, 2022

/build

@wyli wyli disabled auto-merge October 28, 2022 19:43
@wyli wyli enabled auto-merge (squash) October 28, 2022 19:43
@wyli wyli merged commit 8b1f0c3 into dev Oct 28, 2022
@wyli wyli deleted the lr_basetypes branch October 28, 2022 20:28
@atbenmurray
Copy link
Contributor Author

Nice!

KumoLiu pushed a commit that referenced this pull request Nov 2, 2022
Signed-off-by: Ben Murray <[email protected]>

Fixes # .

### Description

A few sentences describing the changes proposed in this pull request.

### 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).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [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.
- [x] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: Ben Murray <[email protected]>
Signed-off-by: KumoLiu <[email protected]>
yiheng-wang-nv pushed a commit to yiheng-wang-nv/MONAI that referenced this pull request Nov 2, 2022
Signed-off-by: Ben Murray <[email protected]>

Fixes # .

### Description

A few sentences describing the changes proposed in this pull request.

### 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).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [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.
- [x] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: Ben Murray <[email protected]>
Signed-off-by: Yiheng Wang <[email protected]>
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.

3 participants