@@ -1119,6 +1119,54 @@ def test_write_scaling(self):
11191119 with np .errstate (invalid = 'ignore' ):
11201120 self ._check_write_scaling (slope , inter , e_slope , e_inter )
11211121
1122+ def test_dynamic_dtype_aliases (self ):
1123+ for in_dt , mn , mx , alias , effective_dt in [
1124+ (np .int64 , 0 , 255 , 'compat' , np .uint8 ),
1125+ (np .int64 , 0 , 256 , 'compat' , np .int16 ),
1126+ (np .int64 , - 1 , 255 , 'compat' , np .int16 ),
1127+ (np .int64 , 0 , 32768 , 'compat' , np .int32 ),
1128+ (np .int64 , 0 , 4294967296 , 'compat' , None ),
1129+ (np .float64 , 0 , 1e30 , 'compat' , np .float32 ),
1130+ (np .float64 , 0 , 1e40 , 'compat' , None ),
1131+ (np .int64 , 0 , 255 , 'smallest' , np .uint8 ),
1132+ (np .int64 , 0 , 256 , 'smallest' , np .int16 ),
1133+ (np .int64 , - 1 , 255 , 'smallest' , np .int16 ),
1134+ (np .int64 , 0 , 32768 , 'smallest' , np .int32 ),
1135+ (np .int64 , 0 , 4294967296 , 'smallest' , None ),
1136+ (np .float32 , 0 , 1 , 'smallest' , None ),
1137+ (np .float64 , 0 , 1 , 'smallest' , None )
1138+ ]:
1139+ arr = np .arange (24 , dtype = in_dt ).reshape ((2 , 3 , 4 ))
1140+ arr [0 , 0 , :2 ] = [mn , mx ]
1141+ img = self .image_class (arr , np .eye (4 ), dtype = alias )
1142+ # Stored as alias
1143+ assert img .get_data_dtype () == alias
1144+ if effective_dt is None :
1145+ with pytest .raises (ValueError ):
1146+ img .get_data_dtype (finalize = True )
1147+ continue
1148+ # Finalizing sets and clears the alias
1149+ assert img .get_data_dtype (finalize = True ) == effective_dt
1150+ assert img .get_data_dtype () == effective_dt
1151+ # Re-set to alias
1152+ img .set_data_dtype (alias )
1153+ assert img .get_data_dtype () == alias
1154+ img_rt = bytesio_round_trip (img )
1155+ assert img_rt .get_data_dtype () == effective_dt
1156+ # Seralizing does not finalize the source image
1157+ assert img .get_data_dtype () == alias
1158+
1159+ def test_static_dtype_aliases (self ):
1160+ for alias , effective_dt in [
1161+ ("mask" , np .uint8 ),
1162+ ]:
1163+ for orig_dt in ('u1' , 'i8' , 'f4' ):
1164+ arr = np .arange (24 , dtype = orig_dt ).reshape ((2 , 3 , 4 ))
1165+ img = self .image_class (arr , np .eye (4 ), dtype = alias )
1166+ assert img .get_data_dtype () == effective_dt
1167+ img_rt = bytesio_round_trip (img )
1168+ assert img_rt .get_data_dtype () == effective_dt
1169+
11221170
11231171class TestNifti1Image (TestNifti1Pair ):
11241172 # Run analyze-flavor spatialimage tests
0 commit comments