Skip to content

Commit 6487157

Browse files
fepegarCopilot
andcommitted
Address second round of review comments
- Fix docstring: dict transforms detected via isinstance(MapTransform), not by 'keys' attribute - Fix type annotation: Mapping[str, object] instead of dict[str, object] - Call subject.update_attributes() after adding new keys so attribute access works (e.g., subject.t1_copy) - Restrict new tensor wrapping to 4D only (ScalarImage requirement) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a3f9824 commit 6487157

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/torchio/transforms/monai_adapter.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class MonaiAdapter(Transform):
3333
affine matrix embedded, so spatial transforms (e.g., cropping, resizing)
3434
correctly propagate affine changes.
3535
36-
**Dictionary transforms** (with a ``keys`` attribute) operate on the
37-
full subject dictionary — only the keys specified in the MONAI transform
38-
are modified.
36+
**Dictionary transforms** (subclasses of MONAI's ``MapTransform``)
37+
operate on the full subject dictionary — only the keys specified in
38+
the MONAI transform are modified.
3939
40-
**Array transforms** (without a ``keys`` attribute) are applied to each
40+
**Array transforms** (all other callables) are applied to each
4141
image in the subject individually, respecting the ``include`` and
4242
``exclude`` parameters inherited from
4343
[`Transform`][torchio.transforms.Transform].
@@ -247,7 +247,7 @@ def _subject_to_monai_dict(
247247

248248
def _update_subject_from_monai_dict(
249249
subject: Subject,
250-
monai_dict: dict[str, object],
250+
monai_dict: Mapping[str, object],
251251
monai: ModuleType,
252252
) -> None:
253253
"""Update a Subject in-place from the MONAI transform output.
@@ -271,7 +271,7 @@ def _update_subject_from_monai_dict(
271271
# New key or non-image key from MONAI output
272272
if isinstance(value, torch.Tensor):
273273
tensor = _unwrap_tensor(value, monai)
274-
if 2 <= tensor.ndim <= 5:
274+
if tensor.ndim == 4:
275275
new_affine = _extract_affine(value, monai)
276276
if new_affine is None:
277277
new_affine = np.eye(4)
@@ -283,3 +283,4 @@ def _update_subject_from_monai_dict(
283283
subject[key] = tensor
284284
else:
285285
subject[key] = value
286+
subject.update_attributes()

tests/transforms/test_monai_adapter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ def test_new_dict_key_wrapped_as_image(self):
324324
transformed = transform(subject)
325325
assert 't1_copy' in transformed
326326
assert isinstance(transformed['t1_copy'], tio.ScalarImage)
327+
# New keys should be accessible via attribute syntax
328+
assert isinstance(transformed.t1_copy, tio.ScalarImage)
327329

328330
def test_multi_sample_transform_raises(self):
329331
"""MONAI transforms returning list[dict] should raise a clear error."""

0 commit comments

Comments
 (0)