Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import h5py\n",
  10. "import numpy as np"
  11. ]
  12. },
  13. {
  14. "cell_type": "code",
  15. "execution_count": 2,
  16. "metadata": {},
  17. "outputs": [
  18. {
  19. "data": {
  20. "text/plain": [
  21. "'2.9.0'"
  22. ]
  23. },
  24. "execution_count": 2,
  25. "metadata": {},
  26. "output_type": "execute_result"
  27. }
  28. ],
  29. "source": [
  30. "h5py.__version__"
  31. ]
  32. },
  33. {
  34. "cell_type": "code",
  35. "execution_count": 3,
  36. "metadata": {},
  37. "outputs": [
  38. {
  39. "data": {
  40. "text/plain": [
  41. "'1.10.4'"
  42. ]
  43. },
  44. "execution_count": 3,
  45. "metadata": {},
  46. "output_type": "execute_result"
  47. }
  48. ],
  49. "source": [
  50. "h5py.version.hdf5_version"
  51. ]
  52. },
  53. {
  54. "cell_type": "markdown",
  55. "metadata": {},
  56. "source": [
  57. "Make two source files with regular datasets, and make the second one read-only:"
  58. ]
  59. },
  60. {
  61. "cell_type": "code",
  62. "execution_count": 4,
  63. "metadata": {},
  64. "outputs": [],
  65. "source": [
  66. "with h5py.File('src1.h5', 'w') as f:\n",
  67. " f['data'] = np.arange(0, 10)"
  68. ]
  69. },
  70. {
  71. "cell_type": "code",
  72. "execution_count": 5,
  73. "metadata": {},
  74. "outputs": [],
  75. "source": [
  76. "!rm -rf src2.h5"
  77. ]
  78. },
  79. {
  80. "cell_type": "code",
  81. "execution_count": 6,
  82. "metadata": {},
  83. "outputs": [],
  84. "source": [
  85. "with h5py.File('src2.h5', 'w') as f:\n",
  86. " f['data'] = np.arange(10, 20)"
  87. ]
  88. },
  89. {
  90. "cell_type": "code",
  91. "execution_count": 7,
  92. "metadata": {},
  93. "outputs": [],
  94. "source": [
  95. "!chmod -w src2.h5"
  96. ]
  97. },
  98. {
  99. "cell_type": "markdown",
  100. "metadata": {},
  101. "source": [
  102. "Assemble a virtual dataset in a third file:"
  103. ]
  104. },
  105. {
  106. "cell_type": "code",
  107. "execution_count": 8,
  108. "metadata": {},
  109. "outputs": [],
  110. "source": [
  111. "layout = h5py.VirtualLayout((2, 10), dtype=np.int64)\n",
  112. "vsrc1 = h5py.VirtualSource(\"src1.h5\", \"data\", (10,), dtype=np.int64)\n",
  113. "vsrc2 = h5py.VirtualSource(\"src2.h5\", \"data\", (10,), dtype=np.int64)\n",
  114. "layout[0] = vsrc1\n",
  115. "layout[1] = vsrc2"
  116. ]
  117. },
  118. {
  119. "cell_type": "code",
  120. "execution_count": 9,
  121. "metadata": {},
  122. "outputs": [],
  123. "source": [
  124. "with h5py.File('virt.h5', 'w') as f:\n",
  125. " f.create_virtual_dataset('data', layout)"
  126. ]
  127. },
  128. {
  129. "cell_type": "markdown",
  130. "metadata": {},
  131. "source": [
  132. "If we open the third file read-only, we can see all its data:"
  133. ]
  134. },
  135. {
  136. "cell_type": "code",
  137. "execution_count": 10,
  138. "metadata": {},
  139. "outputs": [
  140. {
  141. "data": {
  142. "text/plain": [
  143. "array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
  144. " [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])"
  145. ]
  146. },
  147. "execution_count": 10,
  148. "metadata": {},
  149. "output_type": "execute_result"
  150. }
  151. ],
  152. "source": [
  153. "with h5py.File('virt.h5', 'r') as f:\n",
  154. " arr = f['data'][:]\n",
  155. "arr"
  156. ]
  157. },
  158. {
  159. "cell_type": "markdown",
  160. "metadata": {},
  161. "source": [
  162. "But if we open it for read+write, the data from `src2.h5` (read-only) is not accessible."
  163. ]
  164. },
  165. {
  166. "cell_type": "code",
  167. "execution_count": 11,
  168. "metadata": {},
  169. "outputs": [
  170. {
  171. "data": {
  172. "text/plain": [
  173. "array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
  174. " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])"
  175. ]
  176. },
  177. "execution_count": 11,
  178. "metadata": {},
  179. "output_type": "execute_result"
  180. }
  181. ],
  182. "source": [
  183. "with h5py.File('virt.h5', 'r+') as f:\n",
  184. " arr = f['data'][:]\n",
  185. "arr"
  186. ]
  187. }
  188. ],
  189. "metadata": {
  190. "kernelspec": {
  191. "display_name": "Python 3",
  192. "language": "python",
  193. "name": "python3"
  194. },
  195. "language_info": {
  196. "codemirror_mode": {
  197. "name": "ipython",
  198. "version": 3
  199. },
  200. "file_extension": ".py",
  201. "mimetype": "text/x-python",
  202. "name": "python",
  203. "nbconvert_exporter": "python",
  204. "pygments_lexer": "ipython3",
  205. "version": "3.7.2"
  206. }
  207. },
  208. "nbformat": 4,
  209. "nbformat_minor": 2
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement