0

Is there a way to store and subsequently read attributes in an hdf file in order of creation?

I have an ordered list of tuples (string, value) in python which I want to add to an hdf group as attributes. I visit my list in order and the attributes end up sorted lexicographically (I assume) in the hdf file.

import h5py
a = [('attr1', -1.0), ('attr2', 8), ('attr3', 4000), ('attr4', 1000),\
     ('attr5', -1.0), ('attr6', 8), ('attr7', 4000), ('attr8', 1000),\
     ('attr9', -1.0), ('attr10', 8), ('attr11', 4000), ('attr12', 1000)]
with h5py.File('test.h5', 'a') as output_stream:
    for k, v in a:
        output_stream.attrs[k] = v

The h5dump of this is the one below and it doesn't seem to hold any time data on when it was created.

HDF5 "test.h5" {
GROUP "/" {
   ATTRIBUTE "attr1" {
      DATATYPE  H5T_IEEE_F64LE
      DATASPACE  SCALAR
      DATA {
      (0): -1
      }
   }
   ATTRIBUTE "attr10" {
      DATATYPE  H5T_STD_I32LE
      DATASPACE  SCALAR
      DATA {
      (0): 8
      }
   }
   ATTRIBUTE "attr11" {
      DATATYPE  H5T_STD_I32LE
      DATASPACE  SCALAR
      DATA {
      (0): 4000
      }
   }
   ATTRIBUTE "attr12" {
      DATATYPE  H5T_STD_I32LE
      DATASPACE  SCALAR
      DATA {
      (0): 1000
      }
   }
   ATTRIBUTE "attr2" {
      DATATYPE  H5T_STD_I32LE
      DATASPACE  SCALAR
      DATA {
      (0): 8
      }
   }
  ... (ATTRIBUTE 3 to 8)
   ATTRIBUTE "attr9" {
      DATATYPE  H5T_IEEE_F64LE
      DATASPACE  SCALAR
      DATA {
      (0): -1
      }
   }
}
}

I found in some hdf documentation that HDF5 is supposed to support iteration and lookup of attributes by creation order by using H5Pset_attr_creation_order but I don't know how to do this with h5py.

https://support.hdfgroup.org/HDF5/doc/ADGuide/WhatsNew180.html

Satrapes
  • 183
  • 1
  • 18
  • I don't think that this is supported. Also note that your method will is very inefficient in runtime and needed space on disk if you put only one value in a dataset. Have a look at https://stackoverflow.com/a/33249726/4045774 – max9111 Jul 26 '17 at 21:53
  • This is an attribute not a dataset with a single value and anyway this is only needed once in my code so it doesn't matter. – Satrapes Jul 26 '17 at 22:31
  • I haven't used `h5dump` much, but my impression is that it displays all stored information. The display appears to order the attributes in lexical order, where as your list has them in numeric order on the last characters. Maybe you can assign names with that order in mind. – hpaulj Jul 27 '17 at 00:26
  • `dataset` creation does include a `track_times` parameter, but I haven't found any evidence yet of its action. `h5dump` has a `-q` parameter, but again no evidence of it working. – hpaulj Jul 27 '17 at 00:27
  • I just used this kludge for now (adding a number in front so that I get the correct order) but I am feeling ambitious that you could actually make it read the attributes in order by using the low level h5py api. I just don't have enough time to find it now. – Satrapes Jul 27 '17 at 00:56

0 Answers0