1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
|
import io
import os
import numpy as np
import pytest
import yaml
from numpy.random import random
from numpy.testing import assert_array_equal
import asdf
from asdf import constants, generic_io
from asdf._block import io as bio
from asdf.exceptions import AsdfBlockIndexWarning
RNG = np.random.default_rng(6)
def test_external_block(tmp_path):
tmp_path = str(tmp_path)
my_array = RNG.normal(size=(8, 8))
tree = {"my_array": my_array}
ff = asdf.AsdfFile(tree)
ff.set_array_storage(my_array, "external")
assert ff.get_array_storage(my_array) == "external"
ff.write_to(os.path.join(tmp_path, "test.asdf"))
assert "test0000.asdf" in os.listdir(tmp_path)
def test_external_block_url():
uri = "asdf://foo"
my_array = RNG.normal(size=(8, 8))
tree = {"my_array": my_array}
asdf.get_config().all_array_storage = "external"
# this should not raise a ValueError since uri is provided
asdf.AsdfFile(tree, uri=uri)
def test_external_block_non_url():
my_array = RNG.normal(size=(8, 8))
tree = {"my_array": my_array}
ff = asdf.AsdfFile(tree)
ff.set_array_storage(my_array, "external")
assert ff.get_array_storage(my_array) == "external"
buff = io.BytesIO()
with pytest.raises(ValueError, match=r"Can't write external blocks, since URI of main file is unknown."):
ff.write_to(buff)
def test_invalid_array_storage():
my_array = RNG.normal(size=(8, 8))
tree = {"my_array": my_array}
ff = asdf.AsdfFile(tree)
with pytest.raises(ValueError, match=r"array_storage must be one of.*"):
ff.set_array_storage(my_array, "foo")
def test_transfer_array_sources(tmp_path):
tmp_path = str(tmp_path)
my_array = RNG.normal(size=(8, 8))
tree = {"my_array": my_array}
ff = asdf.AsdfFile(tree)
ff.write_to(os.path.join(tmp_path, "test.asdf"))
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert_array_equal(my_array, ff.tree["my_array"])
ff.write_to(os.path.join(tmp_path, "test2.asdf"))
# write_to should have no effect on getting the original data
assert_array_equal(my_array, ff.tree["my_array"])
assert ff._fd is None
def test_write_to_same(tmp_path):
tmp_path = str(tmp_path)
my_array = RNG.normal(size=(8, 8))
tree = {"my_array": my_array}
ff = asdf.AsdfFile(tree)
ff.write_to(os.path.join(tmp_path, "test.asdf"))
with asdf.open(os.path.join(tmp_path, "test.asdf"), mode="rw") as ff:
assert_array_equal(my_array, ff.tree["my_array"])
ff.tree["extra"] = [0] * 1000
ff.write_to(os.path.join(tmp_path, "test2.asdf"))
with asdf.open(os.path.join(tmp_path, "test2.asdf"), mode="rw") as ff:
assert_array_equal(my_array, ff.tree["my_array"])
def test_pad_blocks(tmp_path):
tmp_path = str(tmp_path)
# This is the case where the new tree can't fit in the available space
my_array = np.ones((8, 8)) * 1
my_array2 = np.ones((42, 5)) * 2
tree = {"my_array": my_array, "my_array2": my_array2}
ff = asdf.AsdfFile(tree)
ff.write_to(os.path.join(tmp_path, "test.asdf"), pad_blocks=True)
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert_array_equal(ff.tree["my_array"], my_array)
assert_array_equal(ff.tree["my_array2"], my_array2)
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_expand_tree(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
testpath = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
my_array = np.arange(64) * 1
my_array2 = np.arange(64) * 2
tree = {"arrays": [my_array, my_array2, np.arange(3)]}
ff = asdf.AsdfFile(tree)
ff.set_array_storage(tree["arrays"][2], "inline")
ff.write_to(testpath, pad_blocks=True)
with asdf.open(testpath, lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
assert len(list(ff._blocks.blocks)) == 2
assert_array_equal(ff.tree["arrays"][0], my_array)
ff.tree["extra"] = [0] * 6000
ff.update()
with asdf.open(testpath) as ff:
assert ff.get_array_storage(ff.tree["arrays"][2]) == "inline"
assert_array_equal(ff.tree["arrays"][0], my_array)
assert_array_equal(ff.tree["arrays"][1], my_array2)
# Now, we expand the header only by a little bit
ff = asdf.AsdfFile(tree)
ff.set_array_storage(tree["arrays"][2], "inline")
ff.write_to(os.path.join(tmp_path, "test2.asdf"), pad_blocks=True)
with asdf.open(os.path.join(tmp_path, "test2.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
ff.tree["extra"] = [0] * 2
ff.update()
with asdf.open(os.path.join(tmp_path, "test2.asdf")) as ff:
assert ff.get_array_storage(ff.tree["arrays"][2]) == "inline"
assert_array_equal(ff.tree["arrays"][0], my_array)
assert_array_equal(ff.tree["arrays"][1], my_array2)
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_all_external(tmp_path, lazy_load, memmap):
fn = tmp_path / "test.asdf"
my_array = np.arange(64) * 1
my_array2 = np.arange(64) * 2
tree = {"arrays": [my_array, my_array2]}
af = asdf.AsdfFile(tree)
af.write_to(fn)
with asdf.config.config_context() as cfg:
cfg.array_inline_threshold = 10
cfg.all_array_storage = "external"
with asdf.open(fn, lazy_load=lazy_load, memmap=memmap, mode="rw") as af:
af.update()
assert "test0000.asdf" in os.listdir(tmp_path)
assert "test0001.asdf" in os.listdir(tmp_path)
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_some_external(tmp_path, lazy_load, memmap):
fn = tmp_path / "test.asdf"
my_array = np.arange(64) * 1
my_array2 = np.arange(64) * 2
tree = {"arrays": [my_array, my_array2]}
af = asdf.AsdfFile(tree)
af.write_to(fn)
with asdf.open(fn, lazy_load=lazy_load, memmap=memmap, mode="rw") as af:
af.set_array_storage(af["arrays"][0], "external")
af.update()
assert "test0000.asdf" in os.listdir(tmp_path)
assert "test0001.asdf" not in os.listdir(tmp_path)
def _get_update_tree():
return {"arrays": [np.arange(64) * 1, np.arange(64) * 2, np.arange(64) * 3]}
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_delete_first_array(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
tree = _get_update_tree()
ff = asdf.AsdfFile(tree)
ff.write_to(path, pad_blocks=True)
original_size = os.stat(path).st_size
with asdf.open(os.path.join(tmp_path, "test.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
del ff.tree["arrays"][0]
ff.update()
assert os.stat(path).st_size <= original_size
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert_array_equal(ff.tree["arrays"][0], tree["arrays"][1])
assert_array_equal(ff.tree["arrays"][1], tree["arrays"][2])
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_delete_last_array(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
tree = _get_update_tree()
ff = asdf.AsdfFile(tree)
ff.write_to(path, pad_blocks=True)
original_size = os.stat(path).st_size
with asdf.open(os.path.join(tmp_path, "test.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
del ff.tree["arrays"][-1]
ff.update()
assert os.stat(path).st_size <= original_size
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert_array_equal(ff.tree["arrays"][0], tree["arrays"][0])
assert_array_equal(ff.tree["arrays"][1], tree["arrays"][1])
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_delete_middle_array(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
tree = _get_update_tree()
ff = asdf.AsdfFile(tree)
ff.write_to(path, pad_blocks=True)
original_size = os.stat(path).st_size
with asdf.open(os.path.join(tmp_path, "test.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
del ff.tree["arrays"][1]
ff.update()
assert len(ff._blocks.blocks) == 2
assert os.stat(path).st_size <= original_size
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert len(ff.tree["arrays"]) == 2
assert_array_equal(ff.tree["arrays"][0], tree["arrays"][0])
assert_array_equal(ff.tree["arrays"][1], tree["arrays"][2])
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_replace_first_array(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
tree = _get_update_tree()
ff = asdf.AsdfFile(tree)
ff.write_to(path, pad_blocks=True)
original_size = os.stat(path).st_size
with asdf.open(os.path.join(tmp_path, "test.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
ff.tree["arrays"][0] = np.arange(32)
ff.update()
assert os.stat(path).st_size <= original_size
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert_array_equal(ff.tree["arrays"][0], np.arange(32))
assert_array_equal(ff.tree["arrays"][1], tree["arrays"][1])
assert_array_equal(ff.tree["arrays"][2], tree["arrays"][2])
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_replace_last_array(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
tree = _get_update_tree()
ff = asdf.AsdfFile(tree)
ff.write_to(path, pad_blocks=True)
original_size = os.stat(path).st_size
with asdf.open(os.path.join(tmp_path, "test.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
ff.tree["arrays"][2] = np.arange(32)
ff.update()
assert os.stat(path).st_size <= original_size
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert_array_equal(ff.tree["arrays"][0], tree["arrays"][0])
assert_array_equal(ff.tree["arrays"][1], tree["arrays"][1])
assert_array_equal(ff.tree["arrays"][2], np.arange(32))
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_replace_middle_array(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
tree = _get_update_tree()
ff = asdf.AsdfFile(tree)
ff.write_to(path, pad_blocks=True)
original_size = os.stat(path).st_size
with asdf.open(os.path.join(tmp_path, "test.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
ff.tree["arrays"][1] = np.arange(32)
ff.update()
assert os.stat(path).st_size <= original_size
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert_array_equal(ff.tree["arrays"][0], tree["arrays"][0])
assert_array_equal(ff.tree["arrays"][1], np.arange(32))
assert_array_equal(ff.tree["arrays"][2], tree["arrays"][2])
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_add_array(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
tree = _get_update_tree()
ff = asdf.AsdfFile(tree)
ff.write_to(path, pad_blocks=True)
with asdf.open(os.path.join(tmp_path, "test.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
ff.tree["arrays"].append(np.arange(32))
ff.update()
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert_array_equal(ff.tree["arrays"][0], tree["arrays"][0])
assert_array_equal(ff.tree["arrays"][1], tree["arrays"][1])
assert_array_equal(ff.tree["arrays"][2], tree["arrays"][2])
assert_array_equal(ff.tree["arrays"][3], np.arange(32))
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_add_array_at_end(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
tree = _get_update_tree()
ff = asdf.AsdfFile(tree)
ff.write_to(path, pad_blocks=True)
original_size = os.stat(path).st_size
with asdf.open(os.path.join(tmp_path, "test.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
ff.tree["arrays"].append(np.arange(65536, dtype="<i8"))
ff.update()
assert len(ff._blocks.blocks) == 4
assert os.stat(path).st_size >= original_size
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
assert_array_equal(ff.tree["arrays"][0], tree["arrays"][0])
assert_array_equal(ff.tree["arrays"][1], tree["arrays"][1])
assert_array_equal(ff.tree["arrays"][2], tree["arrays"][2])
assert_array_equal(ff.tree["arrays"][3], np.arange(65536, dtype="<i8"))
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_replace_all_arrays(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
testpath = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
my_array = np.ones((64, 64)) * 1
tree = {
"my_array": my_array,
}
ff = asdf.AsdfFile(tree)
ff.write_to(testpath, pad_blocks=True)
with asdf.open(os.path.join(tmp_path, "test.asdf"), lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
assert_array_equal(ff.tree["my_array"], np.ones((64, 64)) * 1)
ff.tree["my_array"] = np.ones((64, 64)) * 2
ff.update()
with asdf.open(testpath) as ff:
assert_array_equal(ff.tree["my_array"], np.ones((64, 64)) * 2)
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_array_in_place(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
testpath = os.path.join(tmp_path, "test.asdf")
# This is the case where the new tree can't fit in the available space
my_array = np.ones((64, 64)) * 1
tree = {
"my_array": my_array,
}
ff = asdf.AsdfFile(tree)
ff.write_to(testpath, pad_blocks=True)
with asdf.open(testpath, lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
array = np.asarray(ff.tree["my_array"])
array *= 2
ff.update()
with asdf.open(testpath) as ff:
assert_array_equal(ff.tree["my_array"], np.ones((64, 64)) * 2)
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_update_compressed_blocks(tmp_path, lazy_load, memmap):
"""
This test was originally constructed to test an issue where
a failed update left a corrupt file. The issue that resulted in
the failed update (a compressed block growing in size) was fixed
so this is no longer a good test for a failed update.
See: https://github.com/asdf-format/asdf/issues/1520
However, the test does serve to make sure that updating the
contents of compressed blocks in a way that causes them to grow
in size on disk does not result in a failed update.
"""
fn = tmp_path / "test.asdf"
n_arrays = 10
array_size = 10000
# make a tree with many arrays that will compress well
af = asdf.AsdfFile()
for i in range(n_arrays):
af[i] = np.zeros(array_size, dtype="uint8") + i
af.set_array_compression(af[i], "zlib")
af.write_to(fn)
with asdf.open(fn, lazy_load=lazy_load, memmap=memmap, mode="rw") as af:
# now make the data are difficult to compress
for i in range(n_arrays):
assert np.all(af[i] == i)
af[i][:] = np.random.randint(255, size=array_size)
af[i][0] = i + 1
af.update()
with asdf.open(fn, mode="r") as af:
for i in range(n_arrays):
assert af[i][0] == i + 1
def test_init_from_asdffile(tmp_path):
tmp_path = str(tmp_path)
my_array = RNG.normal(size=(8, 8))
tree = {"my_array": my_array}
ff = asdf.AsdfFile(tree)
ff2 = asdf.AsdfFile(ff)
assert ff.tree["my_array"] is ff2.tree["my_array"]
assert_array_equal(ff.tree["my_array"], ff2.tree["my_array"])
ff2.tree["my_array"] = None
assert_array_equal(ff.tree["my_array"], my_array)
ff.write_to(os.path.join(tmp_path, "test.asdf"))
with asdf.open(os.path.join(tmp_path, "test.asdf")) as ff:
ff2 = asdf.AsdfFile(ff)
# assert ff.tree["my_array"] is not ff2.tree["my_array"]
assert_array_equal(ff.tree["my_array"], ff2.tree["my_array"])
ff2.tree["my_array"] = None
assert_array_equal(ff.tree["my_array"], my_array)
def test_seek_until_on_block_boundary():
# Create content where the first block begins on a
# file-reading-block boundary.
content = b"""#ASDF 1.0.0
%YAML 1.1
%TAG ! tag:stsci.edu:asdf/
--- !core/asdf-1.0.0
foo : bar
...
"""
content += b"\0" * (io.DEFAULT_BUFFER_SIZE - 2) + constants.BLOCK_MAGIC + b"\0\x30" + b"\0" * 48
buff = io.BytesIO(content)
ff = asdf.open(buff)
assert len(ff._blocks.blocks) == 1
buff.seek(0)
fd = generic_io.InputStream(buff, "r")
ff = asdf.open(fd)
assert len(ff._blocks.blocks) == 1
def test_checksum(tmp_path):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
my_array = np.arange(0, 64, dtype="<i8").reshape((8, 8))
tree = {"my_array": my_array}
ff = asdf.AsdfFile(tree)
ff.write_to(path)
with asdf.open(path, validate_checksums=True) as ff:
assert ff._blocks.blocks[0].header["checksum"] == b"\xcaM\\\xb8t_L|\x00\n+\x01\xf1\xcfP1"
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_checksum_update(tmp_path, lazy_load, memmap):
tmp_path = str(tmp_path)
path = os.path.join(tmp_path, "test.asdf")
my_array = np.arange(0, 64, dtype="<i8").reshape((8, 8))
tree = {"my_array": my_array}
ff = asdf.AsdfFile(tree)
ff.write_to(path)
with asdf.open(path, lazy_load=lazy_load, memmap=memmap, mode="rw") as ff:
ff.tree["my_array"][7, 7] = 0.0
# update() should update the checksum, even if the data itself
# is memmapped and isn't expressly re-written.
ff.update()
with asdf.open(path, validate_checksums=True) as ff:
assert ff._blocks.blocks[0].header["checksum"] == b"T\xaf~[\x90\x8a\x88^\xc2B\x96D,N\xadL"
def test_block_index():
buff = io.BytesIO()
arrays = []
for i in range(100):
arrays.append(np.ones((8, 8)) * i)
tree = {"arrays": arrays}
ff = asdf.AsdfFile(tree)
ff.write_to(buff)
buff.seek(0)
with asdf.open(buff) as ff2:
assert len(ff2._blocks.blocks) == 100
assert ff2._blocks.blocks[0].loaded
for i in range(2, 99):
assert not ff2._blocks.blocks[i].loaded
assert ff2._blocks.blocks[99].loaded
# Force the loading of one array
ff2.tree["arrays"][50] * 2
for i in range(2, 99):
if i == 50:
assert ff2._blocks.blocks[i].loaded
else:
assert not ff2._blocks.blocks[i].loaded
def test_large_block_index():
"""
This test is designed to test reading of a block index that is
larger than a single file system block, which is why we create
io.DEFAULT_BUFFER_SIZE / 4 arrays, and assuming each entry has more
than one digit in its address, we're guaranteed to have an index
larger than a filesystem block.
"""
# TODO: It would be nice to find a way to make this test faster. The
# real bottleneck here is the enormous YAML section.
buff = io.BytesIO()
narrays = int(io.DEFAULT_BUFFER_SIZE / 4)
arrays = []
for i in range(narrays):
arrays.append(np.array([i], np.uint16))
tree = {"arrays": arrays}
ff = asdf.AsdfFile(tree)
# Since we're testing with small arrays, force all arrays to be stored
# in internal blocks rather than letting some of them be automatically put
# inline.
ff.write_to(buff, all_array_storage="internal")
buff.seek(0)
with asdf.open(buff) as ff2:
assert ff2._blocks.blocks[0].loaded
assert len(ff2._blocks.blocks) == narrays
def test_no_block_index():
buff = io.BytesIO()
arrays = []
for i in range(10):
arrays.append(np.ones((8, 8)) * i)
tree = {"arrays": arrays}
ff = asdf.AsdfFile(tree)
ff.write_to(buff, include_block_index=False)
assert constants.INDEX_HEADER not in buff.getvalue()
def test_junk_after_index():
buff = io.BytesIO()
arrays = []
for i in range(10):
arrays.append(np.ones((8, 8)) * i)
tree = {"arrays": arrays}
ff = asdf.AsdfFile(tree)
ff.write_to(buff)
buff.write(b"JUNK")
buff.seek(0)
# This has junk after the block index, so it
# should fall back to reading serially
with pytest.warns(AsdfBlockIndexWarning, match="Failed to read block index"):
with asdf.open(buff) as ff:
assert ff._blocks.blocks[1].loaded
def test_short_file_find_block_index():
# This tests searching for a block index in a file that looks like
# it might have an index, in the last filesystem block or so, but
# ultimately proves to not have an index.
buff = io.BytesIO()
ff = asdf.AsdfFile({"arr": np.ndarray([1]), "arr2": np.ndarray([2])})
# Since we're testing with small arrays, force all arrays to be stored
# in internal blocks rather than letting some of them be automatically put
# inline.
ff.write_to(buff, include_block_index=False, all_array_storage="internal")
buff.write(b"#ASDF BLOCK INDEX\n")
buff.write(b"0" * (io.DEFAULT_BUFFER_SIZE * 4))
buff.seek(0)
with pytest.warns(AsdfBlockIndexWarning, match="Failed to read block index"):
with asdf.open(buff) as ff:
assert len(ff._blocks.blocks) == 2
assert ff._blocks.blocks[1].loaded
def test_invalid_block_index_values():
# This adds a value in the block index that points to something
# past the end of the file. In that case, we should just reject
# the index altogether.
buff = generic_io.get_file(io.BytesIO(), mode="w")
arrays = []
for i in range(10):
arrays.append(np.ones((8, 8)) * i)
tree = {"arrays": arrays}
ff = asdf.AsdfFile(tree)
ff.write_to(buff, include_block_index=True)
buff.seek(0)
offset = bio.find_block_index(buff)
buff.seek(offset)
block_index = bio.read_block_index(buff)
block_index.append(123456789)
buff.seek(offset)
bio.write_block_index(buff, block_index)
buff.seek(0)
with pytest.warns(AsdfBlockIndexWarning, match="Invalid block index contents"):
with asdf.open(buff) as ff:
assert len(ff._blocks.blocks) == 10
assert ff._blocks.blocks[1].loaded
@pytest.mark.parametrize("block_index_index", [0, -1])
def test_invalid_block_index_offset(block_index_index):
"""
This adds a value in the block index that points to something
that isn't a block
"""
buff = io.BytesIO()
arrays = []
for i in range(10):
arrays.append(np.ones((8, 8)) * i)
tree = {"arrays": arrays}
ff = asdf.AsdfFile(tree)
ff.write_to(buff)
# now overwrite the block index with the first entry
# incorrectly pointing to a non-block offset
buff.seek(0)
bs = buff.read()
block_index_header_start = bs.index(constants.INDEX_HEADER)
block_index_start = block_index_header_start + len(constants.INDEX_HEADER)
block_index = yaml.load(bs[block_index_start:], yaml.SafeLoader)
block_index[block_index_index] -= 4
buff.seek(block_index_start)
yaml.dump(
block_index,
stream=buff,
explicit_start=True,
explicit_end=True,
version=asdf.versioning._YAML_VERSION,
allow_unicode=True,
encoding="utf-8",
)
buff.seek(0)
with pytest.warns(AsdfBlockIndexWarning, match="Invalid block index contents"):
with asdf.open(buff) as ff:
assert len(ff._blocks.blocks) == 10
for i, a in enumerate(arrays):
assert ff._blocks.blocks[i].loaded
assert_array_equal(ff["arrays"][i], a)
def test_unordered_block_index():
"""
This creates a block index that isn't in increasing order
"""
buff = generic_io.get_file(io.BytesIO(), mode="w")
arrays = []
for i in range(10):
arrays.append(np.ones((8, 8)) * i)
tree = {"arrays": arrays}
ff = asdf.AsdfFile(tree)
ff.write_to(buff, include_block_index=True)
buff.seek(0)
offset = bio.find_block_index(buff)
buff.seek(offset)
block_index = bio.read_block_index(buff)
buff.seek(offset)
bio.write_block_index(buff, block_index[::-1])
buff.seek(0)
buff.seek(0)
with pytest.warns(AsdfBlockIndexWarning, match="Failed to read block index"):
with asdf.open(buff) as ff:
assert len(ff._blocks.blocks) == 10
assert ff._blocks.blocks[1].loaded
@pytest.fixture(scope="module")
def filename_with_array(tmp_path_factory):
fn = tmp_path_factory.mktemp("data") / "filename_with_array.asdf"
tree = {"array": np.random.random((20, 20))}
ff = asdf.AsdfFile(tree)
ff.write_to(fn)
return fn
@pytest.mark.parametrize(
"open_kwargs,should_memmap",
[
({}, False),
({"lazy_load": True, "memmap": True}, True),
({"lazy_load": False, "memmap": True}, True),
({"lazy_load": True, "memmap": False}, False),
({"lazy_load": False, "memmap": False}, False),
],
)
def test_open_no_memmap(filename_with_array, open_kwargs, should_memmap):
"""
Test that asdf.open does not (or does) return memmaps for arrays
depending on a number of arguments including:
default (no kwargs)
memmap
"""
with asdf.open(filename_with_array, **open_kwargs) as af:
array = af.tree["array"]
if should_memmap:
assert isinstance(array.base, np.memmap)
else:
assert not isinstance(array.base, np.memmap)
def test_add_block_before_fully_loaded(tmp_path):
"""
This test covers a subtle case where a block is added
to a file before all pre-existing internal blocks have
been located. If the BlockManager isn't careful to
locate them all first, the new block will take the index
of an existing block and views over that index will
point to the wrong data.
See https://github.com/asdf-format/asdf/issues/999
"""
file_path1 = tmp_path / "test1.asdf"
file_path2 = tmp_path / "test2.asdf"
arr0 = random(10)
arr1 = random(10)
arr2 = random(10)
with asdf.AsdfFile() as af:
af["arr0"] = None
af["arr1"] = arr1
af["arr2"] = arr2
af.write_to(file_path1, include_block_index=False)
with asdf.open(file_path1) as af:
af["arr0"] = arr0
af.write_to(file_path2)
with asdf.open(file_path2) as af:
assert_array_equal(af["arr0"], arr0)
assert_array_equal(af["arr1"], arr1)
assert_array_equal(af["arr2"], arr2)
@pytest.mark.parametrize("all_array_storage", ["internal", "external", "inline"])
@pytest.mark.parametrize("all_array_compression", [None, "", "zlib", "bzp2", "lz4", "input"])
@pytest.mark.parametrize("compression_kwargs", [None, {}])
def test_write_to_update_storage_options(tmp_path, all_array_storage, all_array_compression, compression_kwargs):
if all_array_compression == "bzp2" and compression_kwargs is not None:
compression_kwargs = {"compresslevel": 1}
def assert_result(ff):
if all_array_storage == "external":
assert "test0000.asdf" in os.listdir(tmp_path)
else:
assert "test0000.asdf" not in os.listdir(tmp_path)
if all_array_storage == "internal":
assert len(ff._blocks.blocks) == 1
else:
assert len(ff._blocks.blocks) == 0
if all_array_storage == "internal":
target_compression = all_array_compression or None
if target_compression == "input":
target_compression = None
assert ff.get_array_compression(ff["array"]) == target_compression
arr1 = np.ones((8, 8))
tree = {"array": arr1}
fn = tmp_path / "test.asdf"
ff1 = asdf.AsdfFile(tree)
# first check write_to
ff1.write_to(
fn,
all_array_storage=all_array_storage,
all_array_compression=all_array_compression,
compression_kwargs=compression_kwargs,
)
# then reuse the file to check update
arr2 = np.ones((8, 8)) * 42
with asdf.open(fn, mode="rw") as ff2:
assert_result(ff2)
np.testing.assert_array_equal(arr1, ff2["array"])
ff2["array"] = arr2
ff2.update(
all_array_storage=all_array_storage,
all_array_compression=all_array_compression,
compression_kwargs=compression_kwargs,
)
assert_result(ff2)
with asdf.open(fn) as ff3:
assert_result(ff3)
np.testing.assert_array_equal(arr2, ff3["array"])
@pytest.mark.parametrize("lazy_load", [True, False])
@pytest.mark.parametrize("memmap", [True, False])
def test_remove_blocks(tmp_path, lazy_load, memmap):
fn1 = tmp_path / "test.asdf"
fn2 = tmp_path / "test2.asdf"
tree = {"a": np.zeros(3), "b": np.ones(10)}
tree["c"] = tree["b"][:5]
for key in tree:
af = asdf.AsdfFile(tree)
af.write_to(fn1)
with asdf.open(fn1, lazy_load=lazy_load, memmap=memmap, mode="rw") as af:
assert len(af._blocks.blocks) == 2
af[key] = None
af.write_to(fn2)
with asdf.open(fn1, lazy_load=lazy_load, memmap=memmap, mode="rw") as af:
assert len(af._blocks.blocks) == 2
af[key] = None
af.update()
for fn in (fn1, fn2):
with asdf.open(fn) as af:
if key == "a":
assert len(af._blocks.blocks) == 1
else:
assert len(af._blocks.blocks) == 2
for key2 in tree:
if key == key2:
continue
np.testing.assert_array_equal(af[key2], tree[key2])
def test_open_memmap_from_closed_file(tmp_path):
fn = tmp_path / "test.asdf"
arr = np.zeros(100)
arr2 = np.ones(100)
tree = {"base": arr, "view": arr[:50], "base2": arr2}
af = asdf.AsdfFile(tree)
af.write_to(fn)
with asdf.open(fn, lazy_load=True, memmap=False) as af:
# load the base so we can test if accessing the view after the
# file is closed will trigger an error
af["base"][:]
view = af["view"]
base2 = af["base2"]
msg = r"ASDF file has already been closed. Can not get the data."
with pytest.raises(OSError, match=msg):
view[:]
with pytest.raises(OSError, match=msg):
base2[:]
@pytest.mark.parametrize("default_array_save_base", [True, False])
@pytest.mark.parametrize("save_base", [True, False, None])
def test_views_save_base(tmp_path, default_array_save_base, save_base):
fn = tmp_path / "test.asdf"
arr = np.zeros(100, dtype="uint8")
tree = {"v": arr[:10]}
with asdf.config_context() as cfg:
cfg.default_array_save_base = default_array_save_base
af = asdf.AsdfFile(tree)
if save_base is not None:
af.set_array_save_base(af["v"], save_base)
af.write_to(fn)
with asdf.open(fn, memmap=True) as af:
base = af["v"].base
if save_base or (save_base is None and default_array_save_base):
assert len(base) == 100
else:
assert len(base) == 10
|