Databases Reference
In-Depth Information
Likewise, if we were to create a native NumPy array with our type it would get “eaten”
and the extra axes tacked on to the main array's shape:
>>> a = np . zeros (( 100 ,), dtype = dt )
>>> a . dtype
dtype('float32')
>>> a . shape
(100, 2, 2)
So what's the array type good for? Generally it's best used as an element of a compound
type. For example, if we had an experiment that reported an integer timestamp along
with the output from a 2×2 light sensor, one choice for a data type would be:
>>> dt_timestamp = np . dtype ( 'uint64' )
>>> dt_sensor = np . dtype ( '(2,2)f' )
>>> dt = np . dtype ([ ( 'time' , dt_timestamp ), ( 'sensor' , dt_sensor ) ])
Creating a dataset with this compound type, it's easy to store and retrieve individual
outputs from the experiment:
>>> import time
>>> dset = f . create_dataset ( 'mydata' , ( 100 ,), dtype = dt )
>>> dset [ "time" , 0 ] = time . time ()
>>> dset [ "sensor" , 0 ] = (( 1 , 2 ), ( 3 , 4 ))
>>> out = dset [ 0 ]
>>> out
(1368217143, [[1.0, 2.0], [3.0, 4.0]])
>>> out [ "sensor" ]
array([[ 1., 2.],
[ 3., 4.]], dtype=float32)
When your data contains “packets” of values like this, it's generally better to use the
array type than, say, add extra dimensions to the dataset. Not only does it make access
easier, but it's semantically more meaningful.
Opaque Types
It's rare, but some data simply can't be represented in any of the NumPy forms (for
example, disk images or other binary data that isn't numeric). There's a mechanism for
dealing with this in HDF5, which you should consider a last resort for data that needs
to be stored, bit for bit, in the file.
The NumPy void " V " type is used to store such “opaque” data. Like the string type " S "
this is a fixed-width flexible type. For example, to store opaque fields 200 bytes long in
NumPy:
>>> dt = np . dtype ( 'V200' )
>>> a = np . zeros (( 10 ,), dtype = dt ) # 10 elements each 200 bytes long
When you provide such a dtype to create_dataset , the underlying dataset is created
with the HDF5 opaque datatype:
Search WWH ::




Custom Search