{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "quick start: http://docs.h5py.org/en/latest/quick.html\n", "\n", "Dimension Scales: http://docs.h5py.org/en/latest/high/dims.html" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import h5py" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "f = h5py.File('test.hdf5', mode='w') # can also be used as context manager" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.name" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "f['a'] = 12" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(f.keys())" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = f['a']\n", "data" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(12)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[...]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(data.dims)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.dtype" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.maxshape" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/a'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.name" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "data.attrs['extra info'] = 'super data'" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'extra info': 'super data'}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(data.attrs.items())" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': }" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(f.items())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dimension Scales\n", "\n", "http://docs.h5py.org/en/latest/high/dims.html" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "numpy_array = np.array([[1., 2, 3], [4, 5, 6]])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f['b'] = numpy_array\n", "f['b']" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.create_dataset('c', dtype='float32', data=numpy_array)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': ,\n", " 'b': ,\n", " 'c': }" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(f.items())" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "f['b'].dims[1].label = 'nice second dimension!'" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "f['myscale'] = [1, 2, 3, 4]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "# this seems to be only necessary for setting a label:\n", "f['c'].dims.create_scale(f['myscale'], 'my scale label')\n", "#h5py.h5ds.set_scale(f['myscale'].id, b'my scale label')" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "f['c'].dims[0].attach_scale(f['myscale'])" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['', 'nice second dimension!']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[dim.label for dim in f['b'].dims]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[<\"\" dimension 0 of HDF5 dataset at 140378890863152>,\n", " <\"\" dimension 1 of HDF5 dataset at 140378890863152>]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(f['c'].dims.keys())" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['my scale label']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(f['c'].dims[0].keys())" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f['c'].dims[0][0]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f['c'].dims[0]['my scale label']" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "f.close()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HDF5 \"test.hdf5\" {\r\n", "GROUP \"/\" {\r\n", " DATASET \"a\" {\r\n", " DATATYPE H5T_STD_I64LE\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): 12\r\n", " }\r\n", " ATTRIBUTE \"extra info\" {\r\n", " DATATYPE H5T_STRING {\r\n", " STRSIZE H5T_VARIABLE;\r\n", " STRPAD H5T_STR_NULLTERM;\r\n", " CSET H5T_CSET_UTF8;\r\n", " CTYPE H5T_C_S1;\r\n", " }\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): \"super data\"\r\n", " }\r\n", " }\r\n", " }\r\n", " DATASET \"b\" {\r\n", " DATATYPE H5T_IEEE_F64LE\r\n", " DATASPACE SIMPLE { ( 2, 3 ) / ( 2, 3 ) }\r\n", " DATA {\r\n", " (0,0): 1, 2, 3,\r\n", " (1,0): 4, 5, 6\r\n", " }\r\n", " ATTRIBUTE \"DIMENSION_LABELS\" {\r\n", " DATATYPE H5T_STRING {\r\n", " STRSIZE H5T_VARIABLE;\r\n", " STRPAD H5T_STR_NULLTERM;\r\n", " CSET H5T_CSET_ASCII;\r\n", " CTYPE H5T_C_S1;\r\n", " }\r\n", " DATASPACE SIMPLE { ( 2 ) / ( 2 ) }\r\n", " DATA {\r\n", " (0): NULL, \"nice second dimension!\"\r\n", " }\r\n", " }\r\n", " }\r\n", " DATASET \"c\" {\r\n", " DATATYPE H5T_IEEE_F32LE\r\n", " DATASPACE SIMPLE { ( 2, 3 ) / ( 2, 3 ) }\r\n", " DATA {\r\n", " (0,0): 1, 2, 3,\r\n", " (1,0): 4, 5, 6\r\n", " }\r\n", " ATTRIBUTE \"DIMENSION_LIST\" {\r\n", " DATATYPE H5T_VLEN { H5T_REFERENCE { H5T_STD_REF_OBJECT }}\r\n", " DATASPACE SIMPLE { ( 2 ) / ( 2 ) }\r\n", " DATA {\r\n", " (0): (DATASET 6344 /myscale ), ()\r\n", " }\r\n", " }\r\n", " }\r\n", " DATASET \"myscale\" {\r\n", " DATATYPE H5T_STD_I64LE\r\n", " DATASPACE SIMPLE { ( 4 ) / ( 4 ) }\r\n", " DATA {\r\n", " (0): 1, 2, 3, 4\r\n", " }\r\n", " ATTRIBUTE \"CLASS\" {\r\n", " DATATYPE H5T_STRING {\r\n", " STRSIZE 16;\r\n", " STRPAD H5T_STR_NULLTERM;\r\n", " CSET H5T_CSET_ASCII;\r\n", " CTYPE H5T_C_S1;\r\n", " }\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): \"DIMENSION_SCALE\"\r\n", " }\r\n", " }\r\n", " ATTRIBUTE \"NAME\" {\r\n", " DATATYPE H5T_STRING {\r\n", " STRSIZE 15;\r\n", " STRPAD H5T_STR_NULLTERM;\r\n", " CSET H5T_CSET_ASCII;\r\n", " CTYPE H5T_C_S1;\r\n", " }\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): \"my scale label\"\r\n", " }\r\n", " }\r\n", " ATTRIBUTE \"REFERENCE_LIST\" {\r\n", " DATATYPE H5T_COMPOUND {\r\n", " H5T_REFERENCE { H5T_STD_REF_OBJECT } \"dataset\";\r\n", " H5T_STD_I32LE \"dimension\";\r\n", " }\r\n", " DATASPACE SIMPLE { ( 1 ) / ( 1 ) }\r\n", " DATA {\r\n", " (0): {\r\n", " DATASET 1672 /c ,\r\n", " 0\r\n", " }\r\n", " }\r\n", " }\r\n", " }\r\n", "}\r\n", "}\r\n" ] } ], "source": [ "!h5dump test.hdf5" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "with h5py.File('two_scales.hdf5', mode='w') as f:\n", " f['a'] = [1, 2, 3]\n", " f['scale1'] = 1\n", " f['scale2'] = 2\n", " f['a'].dims.create_scale(f['scale1'], 'scale one')\n", " h5py.h5ds.set_scale(f['scale2'].id, b'scale two')\n", " f['a'].dims[0].attach_scale(f['scale1'])\n", " f['a'].dims[0].attach_scale(f['scale2'])" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HDF5 \"two_scales.hdf5\" {\r\n", "GROUP \"/\" {\r\n", " DATASET \"a\" {\r\n", " DATATYPE H5T_STD_I64LE\r\n", " DATASPACE SIMPLE { ( 3 ) / ( 3 ) }\r\n", " DATA {\r\n", " (0): 1, 2, 3\r\n", " }\r\n", " ATTRIBUTE \"DIMENSION_LIST\" {\r\n", " DATATYPE H5T_VLEN { H5T_REFERENCE { H5T_STD_REF_OBJECT }}\r\n", " DATASPACE SIMPLE { ( 1 ) / ( 1 ) }\r\n", " DATA {\r\n", " (0): (DATASET 1400 /scale1 , DATASET 1672 /scale2 )\r\n", " }\r\n", " }\r\n", " }\r\n", " DATASET \"scale1\" {\r\n", " DATATYPE H5T_STD_I64LE\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): 1\r\n", " }\r\n", " ATTRIBUTE \"CLASS\" {\r\n", " DATATYPE H5T_STRING {\r\n", " STRSIZE 16;\r\n", " STRPAD H5T_STR_NULLTERM;\r\n", " CSET H5T_CSET_ASCII;\r\n", " CTYPE H5T_C_S1;\r\n", " }\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): \"DIMENSION_SCALE\"\r\n", " }\r\n", " }\r\n", " ATTRIBUTE \"NAME\" {\r\n", " DATATYPE H5T_STRING {\r\n", " STRSIZE 10;\r\n", " STRPAD H5T_STR_NULLTERM;\r\n", " CSET H5T_CSET_ASCII;\r\n", " CTYPE H5T_C_S1;\r\n", " }\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): \"scale one\"\r\n", " }\r\n", " }\r\n", " ATTRIBUTE \"REFERENCE_LIST\" {\r\n", " DATATYPE H5T_COMPOUND {\r\n", " H5T_REFERENCE { H5T_STD_REF_OBJECT } \"dataset\";\r\n", " H5T_STD_I32LE \"dimension\";\r\n", " }\r\n", " DATASPACE SIMPLE { ( 1 ) / ( 1 ) }\r\n", " DATA {\r\n", " (0): {\r\n", " DATASET 800 /a ,\r\n", " 0\r\n", " }\r\n", " }\r\n", " }\r\n", " }\r\n", " DATASET \"scale2\" {\r\n", " DATATYPE H5T_STD_I64LE\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): 2\r\n", " }\r\n", " ATTRIBUTE \"CLASS\" {\r\n", " DATATYPE H5T_STRING {\r\n", " STRSIZE 16;\r\n", " STRPAD H5T_STR_NULLTERM;\r\n", " CSET H5T_CSET_ASCII;\r\n", " CTYPE H5T_C_S1;\r\n", " }\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): \"DIMENSION_SCALE\"\r\n", " }\r\n", " }\r\n", " ATTRIBUTE \"NAME\" {\r\n", " DATATYPE H5T_STRING {\r\n", " STRSIZE 10;\r\n", " STRPAD H5T_STR_NULLTERM;\r\n", " CSET H5T_CSET_ASCII;\r\n", " CTYPE H5T_C_S1;\r\n", " }\r\n", " DATASPACE SCALAR\r\n", " DATA {\r\n", " (0): \"scale two\"\r\n", " }\r\n", " }\r\n", " ATTRIBUTE \"REFERENCE_LIST\" {\r\n", " DATATYPE H5T_COMPOUND {\r\n", " H5T_REFERENCE { H5T_STD_REF_OBJECT } \"dataset\";\r\n", " H5T_STD_I32LE \"dimension\";\r\n", " }\r\n", " DATASPACE SIMPLE { ( 1 ) / ( 1 ) }\r\n", " DATA {\r\n", " (0): {\r\n", " DATASET 800 /a ,\r\n", " 0\r\n", " }\r\n", " }\r\n", " }\r\n", " }\r\n", "}\r\n", "}\r\n" ] } ], "source": [ "!h5dump two_scales.hdf5" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 4 }