| 
7 | 7 | import numpy as np  | 
8 | 8 | from shutil import rmtree, copytree  | 
9 | 9 | 
 
  | 
 | 10 | +from wkw.wkw import WKWException  | 
 | 11 | + | 
10 | 12 | from wkcuber.api.Dataset import WKDataset, TiffDataset, TiledTiffDataset  | 
11 | 13 | from os import path, makedirs  | 
12 | 14 | 
 
  | 
13 | 15 | from wkcuber.api.Layer import Layer  | 
14 | 16 | from wkcuber.api.Properties.DatasetProperties import TiffProperties, WKProperties  | 
15 | 17 | from wkcuber.api.TiffData.TiffMag import TiffReader  | 
16 | 18 | from wkcuber.api.bounding_box import BoundingBox  | 
 | 19 | +from wkcuber.compress import compress_mag_inplace  | 
17 | 20 | from wkcuber.mag import Mag  | 
18 | 21 | from wkcuber.utils import get_executor_for_args  | 
19 | 22 | 
 
  | 
@@ -1279,6 +1282,158 @@ def test_view_offsets():  | 
1279 | 1282 |         pass  | 
1280 | 1283 | 
 
  | 
1281 | 1284 | 
 
  | 
 | 1285 | +def test_writing_subset_of_compressed_data_multi_channel():  | 
 | 1286 | +    delete_dir("./testoutput/compressed_data/")  | 
 | 1287 | + | 
 | 1288 | +    # create uncompressed dataset  | 
 | 1289 | +    write_data1 = (np.random.rand(3, 20, 40, 60) * 255).astype(np.uint8)  | 
 | 1290 | +    WKDataset.create(  | 
 | 1291 | +        os.path.abspath("./testoutput/compressed_data"), scale=(1, 1, 1)  | 
 | 1292 | +    ).add_layer("color", Layer.COLOR_TYPE, num_channels=3).add_mag(  | 
 | 1293 | +        "1", block_len=8, file_len=8  | 
 | 1294 | +    ).write(  | 
 | 1295 | +        write_data1  | 
 | 1296 | +    )  | 
 | 1297 | + | 
 | 1298 | +    # compress data  | 
 | 1299 | +    compress_mag_inplace(  | 
 | 1300 | +        os.path.abspath("./testoutput/compressed_data/"),  | 
 | 1301 | +        layer_name="color",  | 
 | 1302 | +        mag=Mag("1"),  | 
 | 1303 | +    )  | 
 | 1304 | + | 
 | 1305 | +    # open compressed dataset  | 
 | 1306 | +    compressed_mag = (  | 
 | 1307 | +        WKDataset("./testoutput/compressed_data").get_layer("color").get_mag("1")  | 
 | 1308 | +    )  | 
 | 1309 | + | 
 | 1310 | +    write_data2 = (np.random.rand(3, 10, 10, 10) * 255).astype(np.uint8)  | 
 | 1311 | +    compressed_mag.write(  | 
 | 1312 | +        offset=(10, 20, 30), data=write_data2, allow_compressed_write=True  | 
 | 1313 | +    )  | 
 | 1314 | + | 
 | 1315 | +    np.array_equal(  | 
 | 1316 | +        write_data2, compressed_mag.read(offset=(10, 20, 30), size=(10, 10, 10))  | 
 | 1317 | +    )  # the new data was written  | 
 | 1318 | +    np.array_equal(  | 
 | 1319 | +        write_data1[:, :10, :20, :30],  | 
 | 1320 | +        compressed_mag.read(offset=(0, 0, 0), size=(10, 20, 30)),  | 
 | 1321 | +    )  # the old data is still there  | 
 | 1322 | + | 
 | 1323 | + | 
 | 1324 | +def test_writing_subset_of_compressed_data_single_channel():  | 
 | 1325 | +    delete_dir("./testoutput/compressed_data/")  | 
 | 1326 | + | 
 | 1327 | +    # create uncompressed dataset  | 
 | 1328 | +    write_data1 = (np.random.rand(20, 40, 60) * 255).astype(np.uint8)  | 
 | 1329 | +    WKDataset.create(  | 
 | 1330 | +        os.path.abspath("./testoutput/compressed_data"), scale=(1, 1, 1)  | 
 | 1331 | +    ).add_layer("color", Layer.COLOR_TYPE).add_mag("1", block_len=8, file_len=8).write(  | 
 | 1332 | +        write_data1  | 
 | 1333 | +    )  | 
 | 1334 | + | 
 | 1335 | +    # compress data  | 
 | 1336 | +    compress_mag_inplace(  | 
 | 1337 | +        os.path.abspath("./testoutput/compressed_data/"),  | 
 | 1338 | +        layer_name="color",  | 
 | 1339 | +        mag=Mag("1"),  | 
 | 1340 | +    )  | 
 | 1341 | + | 
 | 1342 | +    # open compressed dataset  | 
 | 1343 | +    compressed_mag = (  | 
 | 1344 | +        WKDataset("./testoutput/compressed_data").get_layer("color").get_mag("1")  | 
 | 1345 | +    )  | 
 | 1346 | + | 
 | 1347 | +    write_data2 = (np.random.rand(10, 10, 10) * 255).astype(np.uint8)  | 
 | 1348 | +    compressed_mag.write(  | 
 | 1349 | +        offset=(10, 20, 30), data=write_data2, allow_compressed_write=True  | 
 | 1350 | +    )  | 
 | 1351 | + | 
 | 1352 | +    np.array_equal(  | 
 | 1353 | +        write_data2, compressed_mag.read(offset=(10, 20, 30), size=(10, 10, 10))  | 
 | 1354 | +    )  # the new data was written  | 
 | 1355 | +    np.array_equal(  | 
 | 1356 | +        write_data1[:10, :20, :30],  | 
 | 1357 | +        compressed_mag.read(offset=(0, 0, 0), size=(10, 20, 30)),  | 
 | 1358 | +    )  # the old data is still there  | 
 | 1359 | + | 
 | 1360 | + | 
 | 1361 | +def test_writing_subset_of_compressed_data():  | 
 | 1362 | +    delete_dir("./testoutput/compressed_data/")  | 
 | 1363 | + | 
 | 1364 | +    # create uncompressed dataset  | 
 | 1365 | +    WKDataset.create(  | 
 | 1366 | +        os.path.abspath("./testoutput/compressed_data"), scale=(1, 1, 1)  | 
 | 1367 | +    ).add_layer("color", Layer.COLOR_TYPE).add_mag("1", block_len=8, file_len=8).write(  | 
 | 1368 | +        (np.random.rand(20, 40, 60) * 255).astype(np.uint8)  | 
 | 1369 | +    )  | 
 | 1370 | + | 
 | 1371 | +    # compress data  | 
 | 1372 | +    compress_mag_inplace(  | 
 | 1373 | +        os.path.abspath("./testoutput/compressed_data/"),  | 
 | 1374 | +        layer_name="color",  | 
 | 1375 | +        mag=Mag("1"),  | 
 | 1376 | +    )  | 
 | 1377 | + | 
 | 1378 | +    # open compressed dataset  | 
 | 1379 | +    compressed_mag = (  | 
 | 1380 | +        WKDataset("./testoutput/compressed_data").get_layer("color").get_mag("1")  | 
 | 1381 | +    )  | 
 | 1382 | + | 
 | 1383 | +    with pytest.raises(WKWException):  | 
 | 1384 | +        # calling 'write' with unaligned data on compressed data without setting 'allow_compressed_write=True'  | 
 | 1385 | +        compressed_mag.write(  | 
 | 1386 | +            offset=(10, 20, 30),  | 
 | 1387 | +            data=(np.random.rand(10, 10, 10) * 255).astype(np.uint8),  | 
 | 1388 | +        )  | 
 | 1389 | + | 
 | 1390 | + | 
 | 1391 | +def test_writing_subset_of_chunked_compressed_data():  | 
 | 1392 | +    delete_dir("./testoutput/compressed_data/")  | 
 | 1393 | + | 
 | 1394 | +    # create uncompressed dataset  | 
 | 1395 | +    write_data1 = (np.random.rand(100, 200, 300) * 255).astype(np.uint8)  | 
 | 1396 | +    WKDataset.create(  | 
 | 1397 | +        os.path.abspath("./testoutput/compressed_data"), scale=(1, 1, 1)  | 
 | 1398 | +    ).add_layer("color", Layer.COLOR_TYPE).add_mag("1", block_len=8, file_len=8).write(  | 
 | 1399 | +        write_data1  | 
 | 1400 | +    )  | 
 | 1401 | + | 
 | 1402 | +    # compress data  | 
 | 1403 | +    compress_mag_inplace(  | 
 | 1404 | +        os.path.abspath("./testoutput/compressed_data/"),  | 
 | 1405 | +        layer_name="color",  | 
 | 1406 | +        mag=Mag("1"),  | 
 | 1407 | +    )  | 
 | 1408 | + | 
 | 1409 | +    # open compressed dataset  | 
 | 1410 | +    compressed_view = WKDataset("./testoutput/compressed_data").get_view(  | 
 | 1411 | +        "color", "1", size=(100, 200, 300), is_bounded=True  | 
 | 1412 | +    )  | 
 | 1413 | + | 
 | 1414 | +    with pytest.raises(AssertionError):  | 
 | 1415 | +        # the aligned data (offset=(0,0,0), size=(128, 128, 128)) is NOT fully within the bounding box of the view  | 
 | 1416 | +        compressed_view.write(  | 
 | 1417 | +            relative_offset=(10, 20, 30),  | 
 | 1418 | +            data=(np.random.rand(90, 80, 70) * 255).astype(np.uint8),  | 
 | 1419 | +            allow_compressed_write=True,  | 
 | 1420 | +        )  | 
 | 1421 | + | 
 | 1422 | +    # the aligned data (offset=(0,0,0), size=(64, 64, 64)) IS fully within the bounding box of the view  | 
 | 1423 | +    write_data2 = (np.random.rand(50, 40, 30) * 255).astype(np.uint8)  | 
 | 1424 | +    compressed_view.write(  | 
 | 1425 | +        relative_offset=(10, 20, 30), data=write_data2, allow_compressed_write=True  | 
 | 1426 | +    )  | 
 | 1427 | + | 
 | 1428 | +    np.array_equal(  | 
 | 1429 | +        write_data2, compressed_view.read(offset=(10, 20, 30), size=(50, 40, 30))  | 
 | 1430 | +    )  # the new data was written  | 
 | 1431 | +    np.array_equal(  | 
 | 1432 | +        write_data1[:10, :20, :30],  | 
 | 1433 | +        compressed_view.read(offset=(0, 0, 0), size=(10, 20, 30)),  | 
 | 1434 | +    )  # the old data is still there  | 
 | 1435 | + | 
 | 1436 | + | 
1282 | 1437 | def test_add_symlink_layer():  | 
1283 | 1438 |     delete_dir("./testoutput/wk_dataset_with_symlink")  | 
1284 | 1439 |     delete_dir("./testoutput/simple_wk_dataset_copy")  | 
 | 
0 commit comments