Skip to content

enh(engines/utils): use tuple/dict comprehensions in PrepareBatchExtraInput#8831

Draft
Zeesejo wants to merge 7 commits intoProject-MONAI:devfrom
Zeesejo:enh/engines-utils-comprehensions
Draft

enh(engines/utils): use tuple/dict comprehensions in PrepareBatchExtraInput#8831
Zeesejo wants to merge 7 commits intoProject-MONAI:devfrom
Zeesejo:enh/engines-utils-comprehensions

Conversation

@Zeesejo
Copy link
Copy Markdown
Contributor

@Zeesejo Zeesejo commented Apr 17, 2026

Fixes #8806

Description

This implements the suggestion made by @ericspod in the review of #8747.

In PrepareBatchExtraInput.__call__ (monai/engines/utils.py), replace the imperative list.append loop and dict.update loop with a direct tuple generator expression and dict comprehension, respectively. Also adds explicit type annotations (args_: tuple and kwargs_: dict) for clarity.

Before:

args_ = []
kwargs_ = {}
...
if isinstance(self.extra_keys, (str, list, tuple)):
    for k in ensure_tuple(self.extra_keys):
        args_.append(_get_data(k))
elif isinstance(self.extra_keys, dict):
    for k, v in self.extra_keys.items():
        kwargs_.update({k: _get_data(v)})
return ..., tuple(args_), kwargs_

After:

args_: tuple = ()
kwargs_: dict = {}
...
if isinstance(self.extra_keys, (str, list, tuple)):
    args_ = tuple(_get_data(k) for k in ensure_tuple(self.extra_keys))
elif isinstance(self.extra_keys, dict):
    kwargs_ = {k: _get_data(v) for k, v in self.extra_keys.items()}
return ..., args_, kwargs_

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.

Zeesejo and others added 7 commits April 11, 2026 20:11
Fixes Project-MONAI#8820 - input_amplitude was incorrectly computed from `target` and target_amplitude from `input`. Corrected to match semantic meaning and standard forward(input, target) convention.

Signed-off-by: Zeeshan Modi <92383127+Zeesejo@users.noreply.github.com>
Fixes Project-MONAI#8822 - The forward() docstring examples used `print(1-SSIMLoss()(x,y))`, but SSIMLoss already computes 1-ssim internally. The `1-` prefix made examples return ssim (not loss), misleading users into training with inverted loss.

Signed-off-by: Zeeshan Modi <92383127+Zeesejo@users.noreply.github.com>
…aInput

Replace the imperative list-append and dict-update loops in
PrepareBatchExtraInput.__call__ with direct tuple generator and dict
comprehension expressions. Also add explicit type annotations for
args_ and kwargs_.

Suggested by @ericspod in Project-MONAI#8747. Closes Project-MONAI#8806.

Signed-off-by: Zeeshan Modi <92383127+Zeesejo@users.noreply.github.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 17, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2ffe0a84-9cdd-49d8-8bdc-8810a7d98ad2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

[EHNANCEMENT] Better Performance For engines/utils.py

2 participants