Skip to content

Commit 4d4c7f6

Browse files
authored
enh(engines/utils): use tuple/dict comprehensions in PrepareBatchExtraInput
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>
1 parent ea4b1c3 commit 4d4c7f6

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

monai/engines/utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ def __call__(
219219
`kwargs` supports other args for `Tensor.to()` API.
220220
"""
221221
image, label = default_prepare_batch(batchdata, device, non_blocking, **kwargs)
222-
args_ = []
223-
kwargs_ = {}
222+
args_: tuple = ()
223+
kwargs_: dict = {}
224224

225225
def _get_data(key: str) -> torch.Tensor:
226226
data = batchdata[key]
@@ -231,13 +231,13 @@ def _get_data(key: str) -> torch.Tensor:
231231
return data
232232

233233
if isinstance(self.extra_keys, (str, list, tuple)):
234-
for k in ensure_tuple(self.extra_keys):
235-
args_.append(_get_data(k))
234+
args_ = tuple(_get_data(k) for k in ensure_tuple(self.extra_keys))
235+
236236
elif isinstance(self.extra_keys, dict):
237-
for k, v in self.extra_keys.items():
238-
kwargs_.update({k: _get_data(v)})
237+
kwargs_ = {k: _get_data(v) for k, v in self.extra_keys.items()}
238+
239239

240-
return cast(torch.Tensor, image), cast(torch.Tensor, label), tuple(args_), kwargs_
240+
return cast(torch.Tensor, image), cast(torch.Tensor, label), args_, kwargs_
241241

242242

243243
class DiffusionPrepareBatch(PrepareBatch):

0 commit comments

Comments
 (0)