Package: hdf5storage
OverviewThis Python package provides high level utilities to read/write a
variety of Python types to/from HDF5 (Heirarchal Data Format) formatted
files. This package also provides support for MATLAB MAT v7.3 formatted
files, which are just HDF5 files with a different extension and some
extra meta-data.All of this is done without pickling data. Pickling is bad for security
because it allows arbitrary code to be executed in the interpreter. One
wants to be able to read possibly HDF5 and MAT files from untrusted
sources, so pickling is avoided in this package.The package’s documetation is found athttp://pythonhosted.org/hdf5storage/The package’s source code is found athttps://github.com/frejanordsiek/hdf5storageThe package is licensed under a 2-clause BSD license
(https://github.com/frejanordsiek/hdf5storage/blob/master/COPYING.txt).InstallationDependenciesThis package only supports Python >= 2.6.This package requires the numpy and h5py (>= 2.1) packages to run. Note
that full functionality requires h5py >= 2.3. An optional dependency is
the scipy package.Installing by pipThis package is onPyPI.
To install hdf5storage using pip, run the command:pip install hdf5storageInstalling from SourceTo install hdf5storage from source, download the package and then
install the dependenciespip install -r requirements.txtThen to install the package, run the command with Pythonpython setup.py installRunning TestsFor testing, the package nose (>= 1.0) is required as well as unittest2
on Python 2.6. There are some tests that require Matlab and scipy to be
installed and be in the executable path. Not having them means that
those tests cannot be run (they will be skipped) but all the other
tests will run. To install all testing dependencies, other than scipy,
runpip install -r requirements_tests.txt.To run the testspython setup.py nosetestsBuilding DocumentationThe documentation additionally requires sphinx (>= 1.7). The
documentation dependencies can be installed bypip install -r requirements_doc.txtTo build the documentationpython setup.py build_sphinxPython 2This package was designed and written for Python 3, with Python 2.7 and
2.6 support added later. This does mean that a few things are a little
clunky in Python 2. Examples include requiringunicodekeys for
dictionaries, theintandlongtypes both being mapped to the
Python 3inttype, etc. The storage format’s metadata looks more
familiar from a Python 3 standpoint as well.The documentation is written in terms of Python 3 syntax and types
primarily. Important Python 2 information beyond direct translations of
syntax and types will be pointed out.Hierarchal Data Format 5 (HDF5)HDF5 files (seehttp://www.hdfgroup.org/HDF5/) are a commonly used file
format for exchange of numerical data. It has built in support for a
large variety of number formats (un/signed integers, floating point
numbers, strings, etc.) as scalars and arrays, enums and compound types.
It also handles differences in data representation on different hardware
platforms (endianness, different floating point formats, etc.). As can
be imagined from the name, data is represented in an HDF5 file in a
hierarchal form modelling a Unix filesystem (Datasets are equivalent to
files, Groups are equivalent to directories, and links are supported).This package interfaces HDF5 files using the h5py package
(http://www.h5py.org/) as opposed to the PyTables package
(http://www.pytables.org/).MATLAB MAT v7.3 file supportMATLAB (http://www.mathworks.com/) MAT files version 7.3 and later are
HDF5 files with a different file extension (.mat) and a very
specific set of meta-data and storage conventions. This package provides
read and write support for a limited set of Python and MATLAB types.SciPy (http://scipy.org/) has functions to read and write the older MAT
file formats. This package has functions modeled after thescipy.io.savematandscipy.io.loadmatfunctions, that have the
same names and similar arguments. The dispatch to the SciPy versions if
the MAT file format is not an HDF5 based one.Supported TypesThe supported Python and MATLAB types are given in the tables below.
The tables assume that one has imported collections and numpy as:import collections as cl
import numpy as npThe table gives which Python types can be read and written, the first
version of this package to support it, the numpy type it gets
converted to for storage (if type information is not written, that
will be what it is read back as) the MATLAB class it becomes if
targetting a MAT file, and the first version of this package to
support writing it so MATlAB can read it.PythonMATLABTypeVersionConverted toClassVersionbool0.1np.bool_ or np.uint8logical0.1[1]None0.1np.float64([])[]0.1int[2][3]0.1np.int64[2]int640.1long[3][4]0.1np.int64int640.1float0.1np.float64double0.1complex0.1np.complex128double0.1str0.1np.uint32/16char0.1[5]bytes0.1np.bytes_ or np.uint16char0.1[6]bytearray0.1np.bytes_ or np.uint16char0.1[6]list0.1np.object_cell0.1tuple0.1np.object_cell0.1set0.1np.object_cell0.1frozenset0.1np.object_cell0.1cl.deque0.1np.object_cell0.1dict0.1struct0.1[7]np.bool_0.1logical0.1np.void0.1np.uint80.1uint80.1np.uint160.1uint160.1np.uint320.1uint320.1np.uint640.1uint640.1np.uint80.1int80.1np.int160.1int160.1np.int320.1int320.1np.int640.1int640.1np.float16[8]0.1np.float320.1single0.1np.float640.1double0.1np.complex640.1single0.1np.complex1280.1double0.1np.str_0.1np.uint32/16char/uint320.1[5]np.bytes_0.1np.bytes_ or np.uint16char0.1[6]np.object_0.1cell0.1np.ndarray0.1[9][10][9][10]0.1[9][11]np.matrix0.1[9][9]0.1[9]np.chararray0.1[9][9]0.1[9]np.recarray0.1structured np.ndarray[9][10]0.1[9][1]Depends on the selected options. Alwaysnp.uint8when doing
MATLAB compatiblity, or if the option is explicitly set.[2](1,2)In Python 2.x, it may be read back as alongif it can’t fit
in the size of anint.[3](1,2)Must be small enough to fit into annp.int64.[4]Type found only in Python 2.x. Python 2.x’slongandintare unified into a singleinttype in Python 3.x. Read as anintin Python 3.x.[5](1,2)Depends on the selected options and whether it can be converted
to UTF-16 without using doublets. If the option is explicity set
(or implicitly when doing MATLAB compatibility) and it can be
converted to UTF-16 without losing any characters that can’t be
represented in UTF-16 or using UTF-16 doublets (MATLAB doesn’t
support them), then it is written asnp.uint16in UTF-16
encoding. Otherwise, it is stored atnp.uint32in UTF-32
encoding.[6](1,2,3)Depends on the selected options. If the option is explicitly set
(or implicitly when doing MATLAB compatibility), it will be
stored asnp.uint16in UTF-16 encoding unless it has
non-ASCII characters in which case aNotImplementedErroris
thrown). Otherwise, it is just written asnp.bytes_.[7]All keys must bestrin Python 3 orunicodein Python 2.
They cannot have null characters ('\x00') or forward slashes
('/') in them.[8]np.float16are not supported for h5py versions before2.2.[9](1,2,3,4,5,6,7,8,9,10,11)Container types are only supported if their underlying dtype is
supported. Data conversions are done based on its dtype.[10](1,2,3)Structurednp.ndarrays (have fields in their dtypes) can be
written as an HDF5 COMPOUND type or as an HDF5 Group with
Datasets holding its fields (either the values directly, or as
an HDF5 Reference array to the values for the different elements
of the data). Can only be written as an HDF5 COMPOUND type if
none of its field are of dtype'object'. Field names cannot
have null characters ('\x00') and, when writing as an HDF5
GROUP, forward slashes ('/') in them.[11]Structurednp.ndarrays with no elements, when written like a
structure, will not be read back with the right dtypes for their
fields (will all become ‘object’).This table gives the MATLAB classes that can be read from a MAT file,
the first version of this package that can read them, and the Python
type they are read as.MATLAB ClassVersionPython Typelogical0.1np.bool_single0.1np.float32 or np.complex64[12]double0.1np.float64 or np.complex128[12]uint80.1np.uint8uint160.1np.uint16uint320.1np.uint32uint640.1np.uint64int80.1np.int8int160.1np.int16int320.1np.int32int640.1np.int64char0.1np.str_struct0.1structured np.ndarraycell0.1np.object_canonical empty0.1np.float64([])[12](1,2)Depends on whether there is a complex part or not.File IncompatibilitiesThe storage of emptynumpy.ndarray(or objects that would be stored like
one) when theOptions.store_shape_for_empty(implicitly set when Matlab
compatibility is enabled) is incompatible with both Matlab and the main
branch of this package after 2021-07-11 due to a bug (Issue #114) that cannot
be fixed without breaking compatibility in the 0.1.x series and thus
will not be fixed (it is however fixed in the main branch after 2021-07-11)
since such a fix would mean the version could not be of the form 0.1.x.The incompatibility is caused by storing the array shape in the Dataset after
reversing the dimension order instead of before, meaning that the array is
read with its dimensions reversed from what is expected if read by Matlab
or the main branch after 2021-07-11.Versions0.1.19. Bugfix release.Issue #122 and #124. Replaced use of deprecatednumpy.asscalarfunctions with thenumpy.ndarray.itemmethod.Issue #123. Forced the use of English month and day of the week names
in the HDF5 header for MATLAB compatibility.Issue #125. Fixed accidental collection ofpkg_resources.parse_versionfrom setuptools as a Marshaller now
that it is a class.0.1.18. Performance improving release.Pull Request #111 from Daniel Hrisca. Many repeated calls to the__getitem__methods of objects were turned into single calls.Further reducionts in__getitem__calls in the spirit of PR #111.0.1.17. Bugfix and deprecation workaround release that fixed the following.Issue #109. Fixed the fix Issue #102 for 32-bit platforms (previous
fix was segfaulting).Moved to usingpkg_resources.parse_versionfromsetuptoolswithdistutils.versionclasses as a fallback instead of just the
later to prepare for the removal ofdistutils(PEP 632) and
prevent warnings on Python versions where it is marked as deprecated.Issue #110. Changed all uses of thetostringmethod on numpy types
to usingtobytesif available, withtostringas the fallback
for old versions of numpy where it is not.0.1.16. Bugfix release that fixed the following bugs.Issue #81 and #82.h5py.Filewill require the mode to be
passed explicitly in the future. All calls without passing it were
fixed to pass it.Issue #102. Added support for h5py 3.0 and 3.1.Issue #73. Fixed bug where a missing variable inloadmatwould
cause the function to think that the file is a pre v7.3 format MAT
file fall back toscipy.io.loadmatwhich won’t work since the file
is a v7.3 format MAT file.Fixed formatting issues in the docstrings and the documentation that
prevented the documentation from building.0.1.15. Bugfix release that fixed the following bugs.Issue #68. Fixed bug wherestrandnumpy.unicode_strings (but not ndarrays of them) were saved inuint32format regardless of the value ofOptions.convert_numpy_bytes_to_utf16.Issue #70. Updatedsetup.pyandrequirements.txtto specify
the maximum versions of numpy and h5py that can be used for specific
python versions (avoid version with dropped support).Issue #71. Fixed bug where the'python_fields'attribute wouldn’t
always be written when doing python metadata for data written in
a struct-like fashion. The bug caused the field order to not be
preserved when writing and reading.Fixed an assertion in the tests to handle field re-ordering when
no metadata is used for structured dtypes that only worked on
older versions of numpy.Issue #72. Fixed bug where python collections filled with ndarrays
that all have the same shape were converted to multi-dimensional
object ndarrays instead of a 1D object ndarray of the elements.0.1.14. Bugfix release that also added a couple features.Issue #45. Fixed syntax errors in unicode strings for Python
3.0 to 3.2.Issues #44 and #47. Fixed bugs in testing of conversion and
storage of string types.Issue #46. Fixed raising ofRuntimeWarningsin tests due
to signalling NaNs.Added requirements files for building documentation and
running tests.Made it so that Matlab compatability tests are skipped if
Matlab is not found, instead of raising errors.0.1.13. Bugfix release fixing the following bug.Issue #36. Fixed bugs in writingintandlongto HDF5
and their tests on 32 bit systems.0.1.12. Bugfix release fixing the following bugs. In addition, copyright years were also updated and notices put in the Matlab files used for testing.Issue #32. Fixed transposing before reshapingnp.ndarraywhen reading from HDF5 files where python metadata was stored
but not Matlab metadata.Issue #33. Fixed the loss of the number of characters when
reading empty numpy string arrays.Issue #34. Fixed a conversion error whennp.chararrayare
written with Matlab metadata.0.1.11. Bugfix release fixing the following.Issue #30. Fixedloadmatnot opening files in read mode.0.1.10. Minor feature/performance fix release doing the following.Issue #29. Addedwritesandreadsfunctions to write
and read more than one piece of data at a time and madesavematandloadmatuse them to increase performance.
Previously, the HDF5 file was being opened and closed for
each piece of data, which impacted performance, especially
for large files.0.1.9. Bugfix and minor feature release doing the following.Issue #23. Fixed bug where a structurednp.ndarraywith
a field name of'O'could never be written as an
HDF5 COMPOUND Dataset (falsely thought a field’s dtype was
object).Issue #6. Added optional data compression and the storage of
data checksums. Controlled by several new options.0.1.8. Bugfix release fixing the following two bugs.Issue #21. Fixed bug where the'MATLAB_class'Attribute is
not set when writingdicttypes when writing MATLAB
metadata.Issue #22. Fixed bug where null characters ('\x00') and
forward slashes ('/') were allowed indictkeys and the
field names of structurednp.ndarray(except that forward
slashes are allowed when thestructured_numpy_ndarray_as_structis not set as is the
case when thematlab_compatibleoption is set). These
cause problems for theh5pypackage and the HDF5 library.NotImplementedErroris now thrown in these cases.0.1.7. Bugfix release with an added compatibility option and some added test code. Did the following.Fixed an issue reading variables larger than 2 GB in MATLAB
MAT v7.3 files when no explicit variable names to read are
given tohdf5storage.loadmat. Fix also reduces memory
consumption and processing time a little bit by removing an
unneeded memory copy.Optionsnow will accept any additional keyword arguments it
doesn’t support, ignoring them, to be API compatible with future
package versions with added options.Added tests for reading data that has been compressed or had
other HDF5 filters applied.0.1.6. Bugfix release fixing a bug with determining the maximum size of a Python 2.xinton a 32-bit system.0.1.5. Bugfix release fixing the following bug.Fixed bug where anintcould be stored that is too big to
fit into anintwhen read back in Python 2.x. When it is
too big, it is converted to along.Fixed a bug where anintorlongthat is too big to
big to fit into annp.int64raised the wrong exception.Fixed bug where fields names for structurednp.ndarraywith
non-ASCII characters (assumed to be UTF-8 encoded in
Python 2.x) can’t be read or written properly.Fixed bug wherenp.bytes_with non-ASCII characters can
were converted incorrectly to UTF-16 when that option is set
(set implicitly when doing MATLAB compatibility). Now, it throws
aNotImplementedError.0.1.4. Bugfix release fixing the following bugs. Thanks goes tomrdominofor writing the bug fixes.Fixed bug wheredtypeis used as a keyword parameter ofnp.ndarray.astypewhen it is a positional argument.Fixed error caused byh5py.__version__being absent on
Ubuntu 12.04.0.1.3. Bugfix release fixing the following bug.Fixed broken ability to correctly read and write empty
structurednp.ndarray(has fields).0.1.2. Bugfix release fixing the following bugs.Removed mistaken support fornp.float16for h5py versions
before2.2since that was when support for it was
introduced.Structurednp.ndarraywhere one or more fields is of the'object'dtype can now be written without an error when
thestructured_numpy_ndarray_as_structoption is not set.
They are written as an HDF5 Group, as if the option was set.Support for the'MATLAB_fields'Attribute for data types
that are structures in MATLAB has been added for when the
version of the h5py package being used is2.3or greater.
Support is still missing for earlier versions (this package
requires a minimum version of2.1).The check for non-unicode string keys (strin Python 3 andunicodein Python 2) in the typedictis done right
before any changes are made to the HDF5 file instead of in the
middle so that no changes are applied if an invalid key is
present.HDF5 userblock set with the proper metadata for MATLAB support
right at the beginning of when data is being written to an HDF5
file instead of at the end, meaning the writing can crash and
the file will still be a valid MATLAB file.0.1.1. Bugfix release fixing the following bugs.stris now written likenumpy.str_instead ofnumpy.bytes_.Complex numbers where the real or imaginary part arenanbut the other part are not are now read correctly as opposed
to setting both parts tonan.Fixed bugs in string conversions on Python 2 resulting fromstr.decode()andunicode.encode()not taking the same
keyword arguments as in Python 3.MATLAB structure arrays can now be read without producing an
error on Python 2.numpy.str_now written asnumpy.uint16on Python 2 if
theconvert_numpy_str_to_utf16option is set and the
conversion can be done without using UTF-16 doublets, instead
of always writing them asnumpy.uint32.0.1. Initial version.