Databases Reference
In-Depth Information
>>>
out
=
ref_dset
[
0
]
>>>
out
<HDF5 object reference (null)>
Like a null pointer in C, this reference doesn't point to anything. Like trying to deref‐
erence against the wrong file, dereferencing a null reference just results in
ValueError
:
>>>
f
[
out
]
ValueError: Invalid HDF5 object reference
There's a simple way to check for a null reference, without having to catch exceptions.
The truth value of a reference indicates whether or not it's null:
>>>
bool
(
out
)
False
>>>
bool
(
grp1
.
ref
)
True
Keep in mind that a value of
True
doesn't
mean that the reference
actually resolves to something, just that it isn't null. If, for example, you
create a reference and then delete the object, the reference will evalu‐
ate as
True
but you will still get
ValueError
when you attempt to
dereference it.
Region References
Region references are one of the coolest features of HDF5. These let you store a reference
to
part
of a dataset. For example, you might want to store a region of interest (ROI) on
photographs stored in an HDF5 file, so that during later analysis you don't have to
process the whole thing.
Creating Region References and Reading
You can think of region references as effectively
storing your slicing arguments
for dataset
access. Here's an example: looking at the dataset created in the previous example, we
notice a property named
regionref
:
>>>
dset
.
name
u'/mydata3'
>>>
dset
.
shape
(100,)
>>>
dset
.
regionref
<h5py._hl.dataset._RegionProxy at 0x459d6d0>
This is a little proxy object which we can use to store our selections. You create a new
region reference by applying the standard NumPy slicing syntax to this object: