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 thread
ericspod marked this conversation as resolved.
Comment on lines +501 to +508
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
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)
_previous_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(_previous_meta)

This ensures the previous state is restored which could be False. This isn't thread-safe by the way but every other usage of these functions isn't either so we will have to accept this for now until that's fixed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is unlikely to be an issue in the tests honestly but we should do this anyway just in case.



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