Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion monai/transforms/io/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def __call__(self, filename: Sequence[PathLike] | PathLike, reader: ImageReader
img_array, meta_data, self.simple_keys, pattern=self.pattern, sep=self.sep
)
if self.ensure_channel_first:
img = EnsureChannelFirst()(img)
img = EnsureChannelFirst()(img, meta_dict=meta_data)
if self.image_only:
return img
return img, img.meta if isinstance(img, MetaTensor) else meta_data
Expand Down
10 changes: 10 additions & 0 deletions tests/transforms/test_load_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,16 @@ def test_correct(self, input_param, expected_shape, track_meta):
self.assertNotIsInstance(r, MetaTensor)
self.assertFalse(hasattr(r, "affine"))

def test_track_meta_false_ensure_channel_first(self):
try:
set_track_meta(False)
r = LoadImage(image_only=True, ensure_channel_first=True)(self.test_data)
self.assertTupleEqual(r.shape, (1, 128, 128, 128))
self.assertIsInstance(r, torch.Tensor)
self.assertNotIsInstance(r, MetaTensor)
finally:
set_track_meta(True)
Comment on lines +500 to +508
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Restore the previous track_meta state.

set_track_meta(True) hard-codes the cleanup state and can clobber whatever the suite had before this test ran.

♻️ Proposed fix
-from monai.data.meta_obj import set_track_meta
+from monai.data.meta_obj import get_track_meta, set_track_meta
@@
     def test_track_meta_false_ensure_channel_first(self):
+        prev_track_meta = get_track_meta()
         try:
             set_track_meta(False)
             r = LoadImage(image_only=True, ensure_channel_first=True)(self.test_data)
             self.assertTupleEqual(r.shape, (1, 128, 128, 128))
             self.assertIsInstance(r, torch.Tensor)
             self.assertNotIsInstance(r, MetaTensor)
         finally:
-            set_track_meta(True)
+            set_track_meta(prev_track_meta)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_track_meta_false_ensure_channel_first(self):
try:
set_track_meta(False)
r = LoadImage(image_only=True, ensure_channel_first=True)(self.test_data)
self.assertTupleEqual(r.shape, (1, 128, 128, 128))
self.assertIsInstance(r, torch.Tensor)
self.assertNotIsInstance(r, MetaTensor)
finally:
set_track_meta(True)
def test_track_meta_false_ensure_channel_first(self):
prev_track_meta = get_track_meta()
try:
set_track_meta(False)
r = LoadImage(image_only=True, ensure_channel_first=True)(self.test_data)
self.assertTupleEqual(r.shape, (1, 128, 128, 128))
self.assertIsInstance(r, torch.Tensor)
self.assertNotIsInstance(r, MetaTensor)
finally:
set_track_meta(prev_track_meta)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/transforms/test_load_image.py` around lines 500 - 508, The test
hard-codes set_track_meta(True) in the finally block which can clobber the
suite's prior state; change the test
(test_track_meta_false_ensure_channel_first) to capture the current state at the
start (e.g., prev = get_track_meta()), call set_track_meta(False) for the test,
and then in the finally block restore the original with set_track_meta(prev)
instead of set_track_meta(True); keep using LoadImage(...) and the same
assertions and ensure get_track_meta/set_track_meta symbols are used to locate
the change.



if __name__ == "__main__":
unittest.main()
Loading