Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement