Skip to content
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

Instantiation mode "partial" to "callable". Return the _target_ component as-is when in _mode_="callable" and no kwargs are specified #7413

Merged
merged 6 commits into from
Feb 6, 2024

Conversation

ibro45
Copy link
Contributor

@ibro45 ibro45 commented Jan 24, 2024

Description

A _target_ component with _mode_="partial" will still be wrapped in functools.partial even when no kwargs are passed: functool.partial(component). In such cases, the component can just be returned as-is.

If you agree with this, I will add tests for it. Thank you!

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.

@KumoLiu
Copy link
Contributor

KumoLiu commented Jan 24, 2024

Hi @ibro45, thanks for your PR, could you please mention the case when it will be an issue? Perhaps a small snippets of code. Thanks.

@ibro45
Copy link
Contributor Author

ibro45 commented Jan 25, 2024

I was trying to achieve this:

    inferer:
        _target_: monai.inferers.PatchInferer
        batch_size: 8
        splitter:
            _target_: monai.inferers.SlidingWindowSplitter
            patch_size: [96, 96, 96]
            overlap: 0.5
        merger_cls:
            _target_: monai.inferers.AvgMerger  # I had a custom merger here that I would have to import if I wanted to use $ 
            _mode_: partial

but it would fail to instantiate because the PatchInferer complained that the merger_cls is not a subclass of Merger, but a functools.partial instead.

I know this could be solved with $, but it may require additional importing which _target_ does not need. It simply seems like a neater option to use _target_ there.

Thank you!

@KumoLiu
Copy link
Contributor

KumoLiu commented Jan 25, 2024

but it would fail to instantiate because the PatchInferer complained that the merger_cls is not a subclass of Merger, but a functools.partial instead.

I get the point now, so instead of return the component directly when no kwargs, I would suggest add a mode here or make it clear in the docstring.

MONAI/monai/utils/module.py

Lines 235 to 239 in 6c9e49e

__mode: the operating mode for invoking the (callable) ``component`` represented by ``__path``:
- ``"default"``: returns ``component(**kwargs)``
- ``"partial"``: returns ``functools.partial(component, **kwargs)``
- ``"debug"``: returns ``pdb.runcall(component, **kwargs)``

What do you think?
cc @ericspod @Nic-Ma

@ericspod
Copy link
Member

but it would fail to instantiate because the PatchInferer complained that the merger_cls is not a subclass of Merger, but a functools.partial instead.

I get the point now, so instead of return the component directly when no kwargs, I would suggest add a mode here or make it clear in the docstring.

I'd suggest a mode name change too. Perhaps "partial" should become "callable" or something to indicate that you could get a partial object back or just whatever the callable thing was if you gave no arguments.

@ibro45
Copy link
Contributor Author

ibro45 commented Jan 28, 2024

I think renaming "partial" to "callable" works with appropriate docstrings.

I may be overcomplicating it here, but what about changing the mode names as follows:

  • "default" -> "invoke" mode - Executes a function or instantiates a class immediately, returning the output or instance.

  • "partial" -> "callable" mode - Directly returns the function or class for future invoking. If kwargs are provided, it creates a functools.partial combining these kwargs with the function or class, enabling delayed invocation with pre-set parameters.

  • "debug" stays as-is.

@ericspod
Copy link
Member

I think renaming "partial" to "callable" works with appropriate docstrings.

I may be overcomplicating it here, but what about changing the mode names as follows:

* `"default"` -> `"invoke"` mode - Executes a function or instantiates a class immediately, returning the output or instance.

* `"partial"` -> `"callable"` mode -  Directly returns the function or class for future invoking. If kwargs are provided, it creates a `functools.partial` combining these kwargs with the function or class, enabling delayed invocation with pre-set parameters.

* `"debug"` stays as-is.

I would leave "default" alone but otherwise this sounds sensible. @KumoLiu thoughts?

@KumoLiu
Copy link
Contributor

KumoLiu commented Jan 31, 2024

I would leave "default" alone but otherwise this sounds sensible. @KumoLiu thoughts?

Agreed.

@ericspod
Copy link
Member

ericspod commented Feb 2, 2024

I would leave "default" alone but otherwise this sounds sensible. @KumoLiu thoughts?

Agreed.

Let's fix the minor flake issues but otherwise it looks good.

@KumoLiu
Copy link
Contributor

KumoLiu commented Feb 5, 2024

Hi @ibro45, could you please fix the flake8 issue and DCO issue, then I will help merge this one. Thanks.
https://github.com/Project-MONAI/MONAI/actions/runs/7635293460/job/20800473351?pr=7413#step:7:387

Signed-off-by: Ibrahim Hadzic <ibrahimhadzic45@gmail.com>
DCO Remediation Commit for Ibrahim Hadzic <ibrahimhadzic45@gmail.com>

I, Ibrahim Hadzic <ibrahimhadzic45@gmail.com>, hereby add my Signed-off-by to this commit: ba27569
I, Ibrahim Hadzic <ibrahimhadzic45@gmail.com>, hereby add my Signed-off-by to this commit: bcc5bde

Signed-off-by: Ibrahim Hadzic <ibrahimhadzic45@gmail.com>
@ibro45 ibro45 changed the title Return the _target_ component as-is when in _mode_="partial" and no kwargs are specified Instantiation mode "partial" to "callable". Return the _target_ component as-is when in _mode_="callable" and no kwargs are specified Feb 5, 2024
@ibro45
Copy link
Contributor Author

ibro45 commented Feb 6, 2024

@KumoLiu should be good now, also renamed "partial" to "callable"

@KumoLiu
Copy link
Contributor

KumoLiu commented Feb 6, 2024

/build

@KumoLiu KumoLiu merged commit ec2cc83 into Project-MONAI:dev Feb 6, 2024
28 checks passed
juampatronics pushed a commit to juampatronics/MONAI that referenced this pull request Mar 25, 2024
… component as-is when in `_mode_="callable"` and no kwargs are specified (Project-MONAI#7413)

### Description

A `_target_` component with `_mode_="partial"` will still be wrapped in
`functools.partial` even when no kwargs are passed:
`functool.partial(component)`. In such cases, the component can just be
returned as-is.

If you agree with this, I will add tests for it. Thank you!

### 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).
- [ ] 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.

---------

Signed-off-by: Ibrahim Hadzic <ibrahimhadzic45@gmail.com>
Signed-off-by: Juan Pablo de la Cruz Gutiérrez <juampatronics@gmail.com>
Yu0610 pushed a commit to Yu0610/MONAI that referenced this pull request Apr 11, 2024
… component as-is when in `_mode_="callable"` and no kwargs are specified (Project-MONAI#7413)

### Description

A `_target_` component with `_mode_="partial"` will still be wrapped in
`functools.partial` even when no kwargs are passed:
`functool.partial(component)`. In such cases, the component can just be
returned as-is.

If you agree with this, I will add tests for it. Thank you!

### 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).
- [ ] 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.

---------

Signed-off-by: Ibrahim Hadzic <ibrahimhadzic45@gmail.com>
Signed-off-by: Yu0610 <612410030@alum.ccu.edu.tw>
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