@@ -764,6 +764,20 @@ def test_affine_44(self):
764764 with pytest .raises (ValueError ):
765765 IC (data , np .diag ([2 , 3 , 4 ]))
766766
767+ def test_dtype_init_arg (self ):
768+ # data_dtype can be set by argument in absence of header
769+ img_klass = self .image_class
770+ arr = np .arange (24 , dtype = np .int16 ).reshape ((2 , 3 , 4 ))
771+ aff = np .eye (4 )
772+ for dtype in self .supported_np_types :
773+ img = img_klass (arr , aff , dtype = dtype )
774+ assert img .get_data_dtype () == dtype
775+ # It can also override the header dtype
776+ hdr = img .header
777+ for dtype in self .supported_np_types :
778+ img = img_klass (arr , aff , hdr , dtype = dtype )
779+ assert img .get_data_dtype () == dtype
780+
767781 def test_offset_to_zero (self ):
768782 # Check offset is always set to zero when creating images
769783 img_klass = self .image_class
@@ -878,6 +892,21 @@ def test_no_finite_values(self):
878892 img_back = self .image_class .from_file_map (fm )
879893 assert_array_equal (img_back .dataobj , 0 )
880894
895+ def test_dtype_to_filename_arg (self ):
896+ # data_dtype can be set by argument in absence of header
897+ img_klass = self .image_class
898+ arr = np .arange (24 , dtype = np .int16 ).reshape ((2 , 3 , 4 ))
899+ aff = np .eye (4 )
900+ img = img_klass (arr , aff )
901+ fname = 'test' + img_klass .files_types [0 ][1 ]
902+ with InTemporaryDirectory ():
903+ for dtype in self .supported_np_types :
904+ img .to_filename (fname , dtype = dtype )
905+ new_img = img_klass .from_filename (fname )
906+ assert new_img .get_data_dtype () == dtype
907+ # data_type is reset after write
908+ assert img .get_data_dtype () == np .int16
909+
881910
882911def test_unsupported ():
883912 # analyze does not support uint32
0 commit comments