1818import numpy as np
1919import torch
2020
21- from monai .data import ITKWriter
21+ from monai .data import ITKWriter , MetaTensor
2222from monai .utils import optional_import
2323
2424itk , has_itk = optional_import ("itk" )
@@ -45,7 +45,7 @@ def test_rgb(self):
4545 with tempfile .TemporaryDirectory () as tempdir :
4646 fname = os .path .join (tempdir , "testing.png" )
4747 writer = ITKWriter (output_dtype = np .uint8 )
48- writer .set_data_array (np .arange (48 ).reshape (3 , 4 , 4 ), channel_dim = 0 )
48+ writer .set_data_array (torch .arange (48 ).reshape (3 , 4 , 4 ), channel_dim = 0 )
4949 writer .set_metadata ({"spatial_shape" : (5 , 5 )})
5050 writer .write (fname )
5151
@@ -64,6 +64,13 @@ def test_no_channel(self):
6464 np .testing .assert_allclose (output .shape , (4 , 4 , 3 ))
6565 np .testing .assert_allclose (output [1 , 1 ], (5 , 21 , 37 ))
6666
67+ def test_metatensor_preserved (self ):
68+ data = MetaTensor (np .arange (48 ).reshape (3 , 4 , 4 , 1 ), meta = {"test_key" : "test_value" })
69+ writer = ITKWriter ()
70+ writer .set_data_array (data , channel_dim = - 1 , squeeze_end_dims = True )
71+ self .assertIsInstance (writer .data_obj , MetaTensor )
72+ self .assertEqual (writer .data_obj .meta .get ("test_key" ), "test_value" )
73+
6774
6875if __name__ == "__main__" :
6976 unittest .main ()
0 commit comments