This 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 at http://pythonhosted.org/hdf5storage/
The package's source code is found at https://github.com/frejanordsiek/hdf5storage
The package is licensed under a 2-clause BSD license (https://github.com/frejanordsiek/hdf5storage/blob/master/COPYING.txt).
This package only supports Python >= 2.7. Python 2.6 support was dropped in version 0.2.
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.
This package is on PyPI. To install hdf5storage using pip, run the command:
pip install hdf5storage
To install hdf5storage from source, download the package and then install the dependencies :
pip install -r requirements.txt
Then to install the package, run the command with Python :
python setup.py install
For testing, the package nose (>= 1.0) is additionally required. There are some tests that require Matlab and scipy to be installed and be in the executable path. In addition, there are some tests that require Julia with the MAT package. 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, Julia, Matlab run :
pip install -r requirements_tests.txt.
To run the tests :
python setup.py nosetests
The documentation additionally requires sphinx (>= 1.3). The documentation dependencies can be installed by :
pip install -r requirements_doc.txt
To build the documentation :
python setup.py build_sphinx
This package was designed and written for Python 3, and then backported to Python 2.x. This does mean that a few things are a little clunky in Python 2. For example, Python 2 int
and long
types are both mapped to the Python 3 int
type. 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.
HDF5 files (see http://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 (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 the scipy.io.savemat
and scipy.io.loadmat
functions, 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.
The 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 np
The 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.
+----------------+---------+-------------------------+-------------+---------+-------------------+ | Python | MATLAB | Notes | +----------------+---------+-------------------------+-------------+---------+-------------------+ | Type | Version | Converted to | Class | Version | | +================+=========+=========================+=============+=========+===================+ | bool | 0.1 | np.bool_ or np.uint8 | logical | 0.1 |1 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | None | 0.1 | np.float64([])
| []
| 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | int | 0.1 | np.int64 or np.bytes_ | int64 | 0.1 |23 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | long | 0.1 | np.int64 or np.bytes_ | int64 | 0.1 |45 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | float | 0.1 | np.float64 | double | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | complex | 0.1 | np.complex128 | double | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | str | 0.1 | np.uint32/16 | char | 0.1 |6 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | bytes | 0.1 | np.bytes_ or np.uint16 | char | 0.1 |7 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | bytearray | 0.1 | np.bytes_ or np.uint16 | char | 0.1 |8 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | list | 0.1 | np.object_ | cell | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | tuple | 0.1 | np.object_ | cell | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | set | 0.1 | np.object_ | cell | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | frozenset | 0.1 | np.object_ | cell | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | cl.deque | 0.1 | np.object_ | cell | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | dict | 0.1 | | struct | 0.1 |9 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | cl.OrderedDict | 0.2 | | struct | 0.2 |10 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.bool_ | 0.1 | | logical | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.void | 0.1 | | | | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.uint8 | 0.1 | | uint8 | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.uint16 | 0.1 | | uint16 | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.uint32 | 0.1 | | uint32 | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.uint64 | 0.1 | | uint64 | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.uint8 | 0.1 | | int8 | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.int16 | 0.1 | | int16 | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.int32 | 0.1 | | int32 | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.int64 | 0.1 | | int64 | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.float16 | 0.1 | | | |11 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.float32 | 0.1 | | single | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.float64 | 0.1 | | double | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.complex64 | 0.1 | | single | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.complex128 | 0.1 | | double | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.str_ | 0.1 | np.uint32/16 | char/uint32 | 0.1 |12 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.bytes_ | 0.1 | np.bytes_ or np.uint16 | char | 0.1 |13 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.object_ | 0.1 | | cell | 0.1 | | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.ndarray | 0.1 | see notes | see notes | 0.1 |141516 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.matrix | 0.1 | see notes | see notes | 0.1 |17 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.chararray | 0.1 | see notes | see notes | 0.1 |18 | +----------------+---------+-------------------------+-------------+---------+-------------------+ | np.recarray | 0.1 | structured np.ndarray | see notes | 0.1 |1920 | +----------------+---------+-------------------------+-------------+---------+-------------------+
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 Class | Version | Python Type |
---|---|---|
logical | 0.1 | np.bool_ |
single | 0.1 | np.float32 or np.complex6421 |
double | 0.1 | np.float64 or np.complex12822 |
uint8 | 0.1 | np.uint8 |
uint16 | 0.1 | np.uint16 |
uint32 | 0.1 | np.uint32 |
uint64 | 0.1 | np.uint64 |
int8 | 0.1 | np.int8 |
int16 | 0.1 | np.int16 |
int32 | 0.1 | np.int32 |
int64 | 0.1 | np.int64 |
char | 0.1 | np.str_ |
struct | 0.1 | structured np.ndarray |
cell | 0.1 | np.object_ |
canonical empty | 0.1 | np.float64([]) |
- 0.2. Feature release adding/changing the following, including some API breaking changes.
- Issue #50. Python 2.6 support dropped. The
pkgutil.find_loader
function is required, and it is not present in Python 2.6. - Issue #27. Added of paths with null characters and slashes. It is used for the field names of structured numpy ndarrays as well as the keys of
dict
like objects when writing their values to individual Datasets. - Issue #49. Changed marshaller types and their handling code to support marshallers that handle types in modules that may not be available or should not be imported until needed. If the the required modules are not available, an approximate version of the data is read using the
read_approximate
method of the marshaller instead of theread
method. The required modules, if available, can either be imported immediately upon the creation of theMarshallerCollection
or they can be imported only when the marshaller is needed for actual use (lazy loading). - Issue #52. Added the usage of a default
MarshallerColllection
which is used whenever creating a newOptions
without aMarshallerCollection
specified. The default can be obtained usingget_default_MarshallerCollection
and a new default can be generated usingmake_new_default_MarshallerCollection
. This is useful if one wants to override the default lazy loading behavior. - Issue #42. read and write functions moved from the
lowlevel
andMarshallers
modules to theutilities
module and thelowlevel
module renamed toexceptions
since that is all that remains in it. - Ability to write Python 3.x
int
and Python 2.xlong
that are too large to fit intonp.int64
. Doing so no longer raises an exception. - Ability to write
np.bytes_
with non-ASCII characters in them. Doing so no longer raises an exception. - Issue #24 and #25. Added support for writing
dict
like objects with keys that are not allstr
without null and'/'
characters. Two new options,'dict_like_keys_name'
and'dict_like_values_name'
control how they are stored if the keys are not string like, can't be converted to Python 3.xstr
or Python 2.xunicode
, or have null or'/'
characters. - Issue #38. Added support for
cl.OrderedDict
. It was added to theMarshallers.PythonDictMarshaller
. - Issue #40. Made it so that tests use tempfiles instead of using hardcoded filenames in the local directory.
- Issue #41. Added tests using the Julia MAT package to check interop with Matlab v7.3 MAT files.
- Issue #39. Documentation now uses the napoleon extension in Sphinx >= 1.3 as a replacement for numpydoc package.
- Issue #50. Python 2.6 support dropped. The
- 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 of
RuntimeWarnings
in 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 writing
int
andlong
to HDF5 and their tests on 32 bit systems.
- Issue #36. Fixed bugs in writing
- 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 reshaping
np.ndarray
when 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 when
np.chararray
are written with Matlab metadata.
- Issue #32. Fixed transposing before reshaping
- 0.1.11. Bugfix release fixing the following.
- Issue #30. Fixed
loadmat
not opening files in read mode.
- Issue #30. Fixed
- 0.1.10. Minor feature/performance fix release doing the following.
- Issue #29. Added
writes
andreads
functions to write and read more than one piece of data at a time and madesavemat
andloadmat
use them to increase performance. Previously, the HDF5 file was being opened and closed for each piece of data, which impacted performance, especially
- Issue #29. Added
for large files.
- 0.1.9. Bugfix and minor feature release doing the following.
- Issue #23. Fixed bug where a structured
np.ndarray
with 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.
- Issue #23. Fixed bug where a structured
- 0.1.8. Bugfix release fixing the following two bugs.
- Issue #21. Fixed bug where the
'MATLAB_class'
Attribute is not set when writingdict
types when writing MATLAB metadata. - Issue #22. Fixed bug where null characters (
'\x00'
) and forward slashes ('/'
) were allowed indict
keys and the field names of structurednp.ndarray
(except that forward slashes are allowed when thestructured_numpy_ndarray_as_struct
is not set as is the case when thematlab_compatible
option is set). These cause problems for theh5py
package and the HDF5 library.NotImplementedError
is now thrown in these cases.
- Issue #21. Fixed bug where the
- 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 to
hdf5storage.loadmat
. Fix also reduces memory consumption and processing time a little bit by removing an unneeded memory copy. Options
now 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.
- Fixed an issue reading variables larger than 2 GB in MATLAB MAT v7.3 files when no explicit variable names to read are given to
0.1.6. Bugfix release fixing a bug with determining the maximum size of a Python 2.x int
on a 32-bit system.
- 0.1.5. Bugfix release fixing the following bug.
- Fixed bug where an
int
could be stored that is too big to fit into anint
when read back in Python 2.x. When it is too big, it is converted to along
. - Fixed a bug where an
int
orlong
that is too big to
- Fixed bug where an
- big to fit into an
np.int64
raised the wrong exception. - Fixed bug where fields names for structured
np.ndarray
with non-ASCII characters (assumed to be UTF-8 encoded in Python 2.x) can't be read or written properly. - Fixed bug where
np.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
.
- Fixed bug where fields names for structured
- 0.1.4. Bugfix release fixing the following bugs. Thanks goes to mrdomino for writing the bug fixes.
- Fixed bug where
dtype
is used as a keyword parameter ofnp.ndarray.astype
when it is a positional argument. - Fixed error caused by
h5py.__version__
being absent on Ubuntu 12.04.
- Fixed bug where
- 0.1.3. Bugfix release fixing the following bug.
- Fixed broken ability to correctly read and write empty structured
np.ndarray
(has fields).
- Fixed broken ability to correctly read and write empty structured
- 0.1.2. Bugfix release fixing the following bugs.
- Removed mistaken support for
np.float16
for h5py versions before2.2
since that was when support for it was introduced. - Structured
np.ndarray
where one or more fields is of the'object'
dtype can now be written without an error when thestructured_numpy_ndarray_as_struct
option 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.3
or greater. Support is still missing for earlier versions (this package requires a minimum version of2.1
). - The check for non-unicode string keys (
str
in Python 3 andunicode
in Python 2) in the typedict
is 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.
- Removed mistaken support for
- 0.1.1. Bugfix release fixing the following bugs.
str
is now written likenumpy.str_
instead ofnumpy.bytes_
.- Complex numbers where the real or imaginary part are
nan
but 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 from
str.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.uint16
on Python 2 if theconvert_numpy_str_to_utf16
option is set and the conversion can be done without using UTF-16 doublets, instead of always writing them asnumpy.uint32
.
0.1. Initial version.
Depends on the selected options. Always
np.uint8
when doing MATLAB compatiblity, or if the option is explicitly set.↩In Python 2.x, it may be read back as a
long
if it can't fit in the size of anint
.↩Stored as a
np.int64
if it is small enough to fit. Otherwise its decimal string representation is stored as annp.bytes_
for hdf5storage >= 0.2 (error in earlier versions).↩Stored as a
np.int64
if it is small enough to fit. Otherwise its decimal string representation is stored as annp.bytes_
for hdf5storage >= 0.2 (error in earlier versions).↩Type found only in Python 2.x. Python 2.x's
long
andint
are unified into a singleint
type in Python 3.x. Read as anint
in Python 3.x.↩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 as
np.uint16
in UTF-16 encoding. Otherwise, it is stored atnp.uint32
in UTF-32 encoding.↩Depends on the selected options. If the option is explicitly set (or implicitly when doing MATLAB compatibility), it will be stored as
np.uint16
in UTF-16 encoding unless it has non-ASCII characters in which case aNotImplementedError
is thrown). Otherwise, it is just written asnp.bytes_
.↩Depends on the selected options. If the option is explicitly set (or implicitly when doing MATLAB compatibility), it will be stored as
np.uint16
in UTF-16 encoding unless it has non-ASCII characters in which case aNotImplementedError
is thrown). Otherwise, it is just written asnp.bytes_
.↩Stored either as each key-value as their own Dataset or as two Datasets, one for keys and one for values. The former is used if all keys can be converted to
str
in Python 3 orunicode
in Python 2 and they don't have null characters ('\x00'
) or forward slashes ('/'
) in them. Otherwise, the latter format is used.↩Stored either as each key-value as their own Dataset or as two Datasets, one for keys and one for values. The former is used if all keys can be converted to
str
in Python 3 orunicode
in Python 2 and they don't have null characters ('\x00'
) or forward slashes ('/'
) in them. Otherwise, the latter format is used.↩np.float16
are not supported for h5py versions before2.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 as
np.uint16
in UTF-16 encoding. Otherwise, it is stored atnp.uint32
in UTF-32 encoding.↩Depends on the selected options. If the option is explicitly set (or implicitly when doing MATLAB compatibility), it will be stored as
np.uint16
in UTF-16 encoding unless it has non-ASCII characters in which case aNotImplementedError
is thrown). Otherwise, it is just written asnp.bytes_
.↩Container types are only supported if their underlying dtype is supported. Data conversions are done based on its dtype.↩
Structured
np.ndarray
s (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.↩Structured
np.ndarray
s with no elements, when written like a structure, will not be read back with the right dtypes for their fields (will all become 'object').↩Container types are only supported if their underlying dtype is supported. Data conversions are done based on its dtype.↩
Container types are only supported if their underlying dtype is supported. Data conversions are done based on its dtype.↩
Container types are only supported if their underlying dtype is supported. Data conversions are done based on its dtype.↩
Structured
np.ndarray
s (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.↩Depends on whether there is a complex part or not.↩
Depends on whether there is a complex part or not.↩