@@ -1204,3 +1204,75 @@ def test_changing_layer_bounding_box():
12041204    new_data  =  mag .read (new_bbox_size )
12051205    assert  new_data .shape  ==  (1 , 255 , 255 , 10 )
12061206    assert  np .array_equal (original_data [:, 10 :, 10 :, :], new_data )
1207+ 
1208+ 
1209+ def  test_view_offsets ():
1210+     delete_dir ("./testoutput/wk_offset_tests" )
1211+ 
1212+     ds  =  WKDataset .create ("./testoutput/wk_offset_tests" , scale = (1 , 1 , 1 ))
1213+     mag  =  ds .add_layer ("color" , "color" ).add_mag ("1" )
1214+ 
1215+     # The dataset is new -> no data has been written. 
1216+     # Therefore, the size of the bounding box in the properties.json is (0, 0, 0) 
1217+ 
1218+     # Creating this view works because the size is set to (0, 0, 0) 
1219+     # However, in practice such a view would not make sense because 'is_bounded' is set to 'True' 
1220+     wk_view  =  ds .get_view ("color" , "1" , size = (0 , 0 , 0 ), is_bounded = True )
1221+     assert  wk_view .global_offset  ==  tuple ((0 , 0 , 0 ))
1222+     assert  wk_view .size  ==  tuple ((0 , 0 , 0 ))
1223+ 
1224+     try :
1225+         # Creating this view does not work because the size (16, 16, 16) would exceed the boundingbox from the properties.json 
1226+         ds .get_view ("color" , "1" , size = (16 , 16 , 16 ), is_bounded = True )
1227+         raise  Exception (expected_error_msg )
1228+     except  AssertionError :
1229+         pass 
1230+ 
1231+     # This works because 'is_bounded' is set to 'False' 
1232+     # Therefore, the bounding box of the view can be larger than the bounding box from the properties.json 
1233+     wk_view  =  ds .get_view ("color" , "1" , size = (16 , 16 , 16 ), is_bounded = False )
1234+     assert  wk_view .global_offset  ==  tuple ((0 , 0 , 0 ))
1235+     assert  wk_view .size  ==  tuple ((16 , 16 , 16 ))
1236+ 
1237+     np .random .seed (1234 )
1238+     write_data  =  (np .random .rand (100 , 200 , 300 ) *  255 ).astype (np .uint8 )
1239+     mag .write (write_data , offset = (10 , 20 , 30 ))
1240+ 
1241+     # The bounding box of the dataset was updated according to the written data 
1242+     # Therefore, creating a view with a size of (16, 16, 16) is now allowed 
1243+     wk_view  =  ds .get_view ("color" , "1" , size = (16 , 16 , 16 ), is_bounded = True )
1244+     assert  wk_view .global_offset  ==  tuple ((10 , 20 , 30 ))
1245+     assert  wk_view .size  ==  tuple ((16 , 16 , 16 ))
1246+ 
1247+     try :
1248+         # Creating this view does not work because the offset (0, 0, 0) would be outside of the boundingbox from the properties.json 
1249+         ds .get_view ("color" , "1" , size = (16 , 16 , 16 ), offset = (0 , 0 , 0 ), is_bounded = True )
1250+         raise  Exception (expected_error_msg )
1251+     except  AssertionError :
1252+         pass 
1253+ 
1254+     # Creating this view works, even though the offset (0, 0, 0) is outside of the boundingbox from the properties.json, because 'is_bounded' is set to 'False' 
1255+     wk_view  =  ds .get_view (
1256+         "color" , "1" , size = (16 , 16 , 16 ), offset = (0 , 0 , 0 ), is_bounded = False 
1257+     )
1258+     assert  wk_view .global_offset  ==  tuple ((0 , 0 , 0 ))
1259+     assert  wk_view .size  ==  tuple ((16 , 16 , 16 ))
1260+ 
1261+     # Creating this view works because the bounding box of the view is inside the bounding box from the properties.json 
1262+     wk_view  =  ds .get_view (
1263+         "color" , "1" , size = (16 , 16 , 16 ), offset = (20 , 30 , 40 ), is_bounded = True 
1264+     )
1265+     assert  wk_view .global_offset  ==  tuple ((20 , 30 , 40 ))
1266+     assert  wk_view .size  ==  tuple ((16 , 16 , 16 ))
1267+ 
1268+     # Creating this subview works because the subview is completely inside the 'wk_view' 
1269+     sub_view  =  wk_view .get_view (size = (8 , 8 , 8 ), relative_offset = (8 , 8 , 8 ))
1270+     assert  sub_view .global_offset  ==  tuple ((28 , 38 , 48 ))
1271+     assert  sub_view .size  ==  tuple ((8 , 8 , 8 ))
1272+ 
1273+     try :
1274+         # Creating this subview does not work because it is not completely inside the 'wk_view' 
1275+         wk_view .get_view (size = (10 , 10 , 10 ), relative_offset = (8 , 8 , 8 ))
1276+         raise  Exception (expected_error_msg )
1277+     except  AssertionError :
1278+         pass 
0 commit comments