Skip to content

Instantly share code, notes, and snippets.

@takluyver
Created April 9, 2019 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takluyver/577a0edf2fe67ea3590d8d5bced39c56 to your computer and use it in GitHub Desktop.
Save takluyver/577a0edf2fe67ea3590d8d5bced39c56 to your computer and use it in GitHub Desktop.
HDF5 virtual dataset access issue
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import h5py\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2.9.0'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h5py.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.10.4'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h5py.version.hdf5_version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make two source files with regular datasets, and make the second one read-only:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"with h5py.File('src1.h5', 'w') as f:\n",
" f['data'] = np.arange(0, 10)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"!rm -rf src2.h5"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"with h5py.File('src2.h5', 'w') as f:\n",
" f['data'] = np.arange(10, 20)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"!chmod -w src2.h5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assemble a virtual dataset in a third file:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"layout = h5py.VirtualLayout((2, 10), dtype=np.int64)\n",
"vsrc1 = h5py.VirtualSource(\"src1.h5\", \"data\", (10,), dtype=np.int64)\n",
"vsrc2 = h5py.VirtualSource(\"src2.h5\", \"data\", (10,), dtype=np.int64)\n",
"layout[0] = vsrc1\n",
"layout[1] = vsrc2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"with h5py.File('virt.h5', 'w') as f:\n",
" f.create_virtual_dataset('data', layout)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we open the third file read-only, we can see all its data:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"with h5py.File('virt.h5', 'r') as f:\n",
" arr = f['data'][:]\n",
"arr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But if we open it for read+write, the data from `src2.h5` (read-only) is not accessible."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"with h5py.File('virt.h5', 'r+') as f:\n",
" arr = f['data'][:]\n",
"arr"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment