[python-hdf5storage] 119/152: Moved the code to read an h5py.Reference array and make a numpy.object_ array from it to a function.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:40 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag 0.1
in repository python-hdf5storage.

commit 40c4b6803055fbb68105bddaa30f5cff35f113e8
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Fri Feb 14 14:26:20 2014 -0500

    Moved the code to read an h5py.Reference array and make a numpy.object_ array from it to a function.
---
 doc/source/hdf5storage.Marshallers.rst |  7 ++++
 hdf5storage/Marshallers.py             | 65 +++++++++++++++++++++++++---------
 2 files changed, 56 insertions(+), 16 deletions(-)

diff --git a/doc/source/hdf5storage.Marshallers.rst b/doc/source/hdf5storage.Marshallers.rst
index c984250..b52c44a 100644
--- a/doc/source/hdf5storage.Marshallers.rst
+++ b/doc/source/hdf5storage.Marshallers.rst
@@ -8,6 +8,7 @@ hdf5storage.Marshallers
 .. autosummary::
 
    write_object_array
+   read_object_array
    TypeMarshaller
    NumpyScalarArrayMarshaller
    PythonScalarMarshaller
@@ -24,6 +25,12 @@ write_object_array
 .. autofunction:: write_object_array
 
 
+read_object_array
+------------------
+
+.. autofunction:: read_object_array
+
+
 TypeMarshaller
 --------------
 
diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index 8bf442e..79c8d07 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -70,6 +70,7 @@ def write_object_array(f, data, options):
 
     See Also
     --------
+    read_object_array
     hdf5storage.Options.group_for_references
     h5py.Reference
 
@@ -139,6 +140,53 @@ def write_object_array(f, data, options):
     return data_refs.astype(dtype=ref_dtype).copy()
 
 
+def read_object_array(f, data, options):
+    """ Reads an array of objects recursively.
+
+    Read the elements of the given HDF5 Reference array recursively
+    in the and constructs a ``numpy.object_`` array from its elements,
+    which is returned.
+
+    Parameters
+    ----------
+    f : h5py.File
+        The HDF5 file handle that is open.
+    data : numpy.ndarray of h5py.Reference
+        The array of HDF5 References to read and make an object array
+        from.
+    options : hdf5storage.core.Options
+        hdf5storage options object.
+
+    Raises
+    ------
+    NotImplementedError
+        If reading the object from file is currently not supported.
+
+    Returns
+    -------
+    numpy.ndarray of numpy.object_
+        The Python object array containing the items pointed to by
+        `data`.
+
+    See Also
+    --------
+    write_object_array
+    hdf5storage.Options.group_for_references
+    h5py.Reference
+
+    """
+    # Go through all the elements of data and read them using their
+    # references, and the putting the output in new object array.
+    data_derefed = np.zeros(shape=data.shape, dtype='object')
+    for index, x in np.ndenumerate(data):
+        try:
+            data_derefed[index] = read_data(f, f[x].parent, \
+                posixpath.basename(f[x].name), options)
+        except:
+            raise
+    return data_derefed
+
+
 class TypeMarshaller(object):
     """ Base class for marshallers of Python types.
 
@@ -786,22 +834,7 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
         # array that is its replicate, but with the objects they are
         # pointing to in their elements instead of just the references.
         if h5py.check_dtype(ref=grp[name].dtype) is not None:
-            data_derefed = data.copy().astype(np.dtype('object'))
-
-            # Go through all the elements of data and read them using
-            # their references, and the putting the output in
-            # data_derefed. If they can't be read, None is put in.
-
-            for index, x in np.ndenumerate(data):
-                data_derefed[index] = None
-                try:
-                    data_derefed[index] = read_data(f, f[x].parent, \
-                        posixpath.basename(f[x].name), options)
-                except:
-                    raise
-
-            # Now all that needs to be done is copy back to data.
-            data = data_derefed.copy()
+            data = read_object_array(f, data, options)
 
         # If metadata is present, that can be used to do convert to the
         # desired/closest Python data types. If none is present, or not

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-hdf5storage.git



More information about the debian-science-commits mailing list