Databases Reference
In-Depth Information
The point of all this is to store the “semantic” meaning of these values as close as possible
to the data itself, rather than, for example, in Appendix G of a manual that nobody reads.
There's no native concept for this in the NumPy world, so we fall back again to our
friend h5py.special_dtype . In this case, we use a different keyword, enum , and supply
both a base type and dictionary mapping names to values:
>>> mapping = { "RED" : 0 , "GREEN" : 1 , "BLUE" : 2 }
>>> dt = h5py . special_dtype ( enum = ( np . int8 , mapping ) )
Datasets you create with this type work just like regular integer datasets:
>>> dset = f . create_dataset ( 'enum' , ( 100 ,), dtype = dt )
>>> dset [ 0 ]
0
Like variable-length strings, data you read from the dataset will have the extra “special
dtype” information stripped off:
>>> dset [ 0 ] . dtype
dtype('int8')
Keep in mind that in both HDF5 and NumPy, no checking is performed to make sure
you keep to values specified in the enum. For example, if you were to assign one element
to a different value, HDF5 will happily store it:
>>> dset [ 0 ] = 100
>>> dset [ 0 ]
100
It's strictly on the honor system.
HDF5 itself doesn't like to convert between integers and enums. So if
you create an enum dataset, keep in mind that people who read your
data will have to explicitly read it as an enum. Generally this works
fine, but as always, if you're interacting with third-party code it's a good
idea to test.
Booleans
When storing Boolean (True/False) flags, people often resort to simply using integers.
In Chapter 3 , we saw that NumPy natively supports arrays of Booleans. They have their
own data type, np.bool . NumPy hides the storage type from you, but behind the scenes,
arrays of type bool are stored as single-byte integers.
There's no native HDF5 Boolean type, but like complex numbers, h5py automatically
provides one for you (in this case using an enum). The base type is np.int8 and the
mapping is {"FALSE": 0, "TRUE": 1} . Let's create a Boolean dataset:
Search WWH ::




Custom Search