test_file_image.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import numpy as np
  2. import h5py
  3. from h5py import h5f, h5p
  4. from .common import TestCase
  5. class TestFileImage(TestCase):
  6. def test_load_from_image(self):
  7. from binascii import a2b_base64
  8. from zlib import decompress
  9. compressed_image = 'eJzr9HBx4+WS4mIAAQ4OBhYGAQZk8B8KKjhQ+TD5BCjNCKU7oPQKJpg4I1hOAiouCDUfXV1IkKsrSPV/NACzx4AFQnMwjIKRCDxcHQNAdASUD0ulJ5hQ1ZWkFpeAaFh69KDQXkYGNohZjDA+JCUzMkIEmKHqELQAWKkAByytOoBJViAPJM7ExATWyAE0B8RgZkyAJmlYDoEAIahukJoNU6+HMTA0UOgT6oBgP38XUI6G5UMFZrzKR8EoGAUjGMDKYVgxDSsuAHcfMK8='
  10. image = decompress(a2b_base64(compressed_image))
  11. fapl = h5p.create(h5py.h5p.FILE_ACCESS)
  12. fapl.set_fapl_core()
  13. fapl.set_file_image(image)
  14. fid = h5f.open(self.mktemp().encode(), h5py.h5f.ACC_RDONLY, fapl=fapl)
  15. f = h5py.File(fid)
  16. self.assertTrue('test' in f)
  17. def test_open_from_image(self):
  18. from binascii import a2b_base64
  19. from zlib import decompress
  20. compressed_image = 'eJzr9HBx4+WS4mIAAQ4OBhYGAQZk8B8KKjhQ+TD5BCjNCKU7oPQKJpg4I1hOAiouCDUfXV1IkKsrSPV/NACzx4AFQnMwjIKRCDxcHQNAdASUD0ulJ5hQ1ZWkFpeAaFh69KDQXkYGNohZjDA+JCUzMkIEmKHqELQAWKkAByytOoBJViAPJM7ExATWyAE0B8RgZkyAJmlYDoEAIahukJoNU6+HMTA0UOgT6oBgP38XUI6G5UMFZrzKR8EoGAUjGMDKYVgxDSsuAHcfMK8='
  21. image = decompress(a2b_base64(compressed_image))
  22. fid = h5f.open_file_image(image)
  23. f = h5py.File(fid)
  24. self.assertTrue('test' in f)
  25. def test_in_memory():
  26. arr = np.arange(10)
  27. # Passing one fcpl & one fapl parameter to exercise the code splitting them:
  28. with h5py.File.in_memory(track_order=True, rdcc_nbytes=2_000_000) as f1:
  29. f1['a'] = arr
  30. f1.flush()
  31. img = f1.id.get_file_image()
  32. # Open while f1 is still open
  33. with h5py.File.in_memory(img) as f2:
  34. np.testing.assert_array_equal(f2['a'][:], arr)
  35. # Reuse image now that previous files are closed
  36. with h5py.File.in_memory(img) as f3:
  37. np.testing.assert_array_equal(f3['a'][:], arr)