@@ -128,6 +128,44 @@ def assign_metadata(val):
128128 pytest .raises (TypeError , assign_metadata , 'not-a-meta' )
129129
130130
131+ @pytest .mark .parametrize ('label' , data_type_codes .value_set ('label' ))
132+ def test_image_typing (label ):
133+ dtype = data_type_codes .dtype [label ]
134+ if dtype == np .void :
135+ return
136+ arr = 127 * rng .random (
137+ 20 ,
138+ )
139+ try :
140+ cast = arr .astype (label )
141+ except TypeError :
142+ return
143+ darr = GiftiDataArray (cast , datatype = label )
144+ img = GiftiImage (darrays = [darr ])
145+
146+ # Force-write always works
147+ force_rt = img .from_bytes (img .to_bytes (mode = 'force' ))
148+ assert np .array_equal (cast , force_rt .darrays [0 ].data )
149+
150+ # Compatibility mode does its best
151+ if np .issubdtype (dtype , np .integer ) or np .issubdtype (dtype , np .floating ):
152+ compat_rt = img .from_bytes (img .to_bytes (mode = 'compat' ))
153+ compat_darr = compat_rt .darrays [0 ].data
154+ assert np .allclose (cast , compat_darr )
155+ assert compat_darr .dtype in ('uint8' , 'int32' , 'float32' )
156+ else :
157+ with pytest .raises (ValueError ):
158+ img .to_bytes (mode = 'compat' )
159+
160+ # Strict mode either works or fails
161+ if label in ('uint8' , 'int32' , 'float32' ):
162+ strict_rt = img .from_bytes (img .to_bytes (mode = 'strict' ))
163+ assert np .array_equal (cast , strict_rt .darrays [0 ].data )
164+ else :
165+ with pytest .raises (ValueError ):
166+ img .to_bytes (mode = 'strict' )
167+
168+
131169def test_dataarray_empty ():
132170 # Test default initialization of DataArray
133171 null_da = GiftiDataArray ()
0 commit comments