File: special.rst

package info (click to toggle)
h5py 3.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,828 kB
  • sloc: python: 12,212; ansic: 578; makefile: 433; sh: 33
file content (278 lines) | stat: -rw-r--r-- 9,825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
.. currentmodule:: h5py
.. _special_types:

Special types
=============

HDF5 supports a few types which have no direct NumPy equivalent.  Among the
most useful and widely used are *variable-length* (VL) types, and enumerated
types.  As of version 2.3, h5py fully supports HDF5 enums and VL types.

How special types are represented
---------------------------------

Since there is no direct NumPy dtype for enums or references (and, in NumPy 1.x, for
variable-length strings), h5py extends the dtype system slightly to let HDF5 know how
to store these types.  Each type is represented by a native NumPy dtype, with a
small amount of metadata attached.  NumPy routines ignore the metadata, but
h5py can use it to determine how to store the data.

The metadata h5py attaches to dtypes is not part of the public API,
so it may change between versions.
Use the functions described below to create and check for these types.

Variable-length strings in NumPy 1.x
------------------------------------

.. seealso:: :ref:`strings`

.. note::
   Starting from h5py 3.14 + NumPy 2.0, you can use native NumPy variable-width
   strings, a.k.a. NpyStrings or StringDType. See :ref:`npystrings`.

In HDF5, data in VL format is stored as arbitrary-length vectors of a base
type.  In particular, strings are stored C-style in null-terminated buffers.
NumPy 1.x has no native mechanism to support this.  Unfortunately, this is the
de facto standard for representing strings in the HDF5 C API, and in many
HDF5 applications.

Thankfully, NumPy has a generic pointer type in the form of the "object" ("O")
dtype.  In h5py, variable-length strings are mapped to object arrays.  A
small amount of metadata attached to an "O" dtype tells h5py that its contents
should be converted to VL strings when stored in the file.

Existing VL strings can be read and written to with no additional effort;
Python strings and fixed-length NumPy strings can be auto-converted to VL
data and stored.

Here's an example showing how to create a VL array of strings::

    >>> f = h5py.File('foo.hdf5')
    >>> dt = h5py.string_dtype(encoding='utf-8')
    >>> ds = f.create_dataset('VLDS', (100,100), dtype=dt)
    >>> ds.dtype.kind
    'O'
    >>> h5py.check_string_dtype(ds.dtype)
    string_info(encoding='utf-8', length=None)

.. function:: string_dtype(encoding='utf-8', length=None)

   Make a numpy dtype for HDF5 strings

   :param encoding: ``'utf-8'`` or ``'ascii'``.
   :param length: ``None`` for variable-length, or an integer for fixed-length
                  string data, giving the length in bytes.

.. function:: check_string_dtype(dt)

   Check if ``dt`` is a string dtype.
   Returns a *string_info* object if it is, or ``None`` if not.

.. class:: string_info

   A named tuple type holding string encoding and length.

   .. attribute:: encoding

      The character encoding associated with the string dtype,
      which can be ``'utf-8'`` or ``'ascii'``.

   .. attribute:: length

      For fixed-length string dtypes, the length in bytes.
      ``None`` for variable-length strings.

.. _vlen:

Arbitrary vlen data
-------------------

Starting with h5py 2.3, variable-length types are not restricted to strings.
For example, you can create a "ragged" array of integers::

    >>> dt = h5py.vlen_dtype(np.dtype('int32'))
    >>> dset = f.create_dataset('vlen_int', (100,), dtype=dt)
    >>> dset[0] = [1,2,3]
    >>> dset[1] = [1,2,3,4,5]

Single elements are read as NumPy arrays::

    >>> dset[0]
    array([1, 2, 3], dtype=int32)

Multidimensional selections produce an object array whose members are integer
arrays::

    >>> dset[0:2]
    array([array([1, 2, 3], dtype=int32), array([1, 2, 3, 4, 5], dtype=int32)], dtype=object)

.. note::

   NumPy doesn't support ragged arrays, and the 'arrays of arrays' h5py uses
   as a workaround are not as convenient or efficient as regular NumPy arrays.
   If you're deciding how to store data, consider whether there's a sensible
   way to do it without a variable-length type.

.. function:: vlen_dtype(basetype)

   Make a numpy dtype for an HDF5 variable-length datatype.

   :param basetype: The dtype of each element in the array.

.. function:: check_vlen_dtype(dt)

   Check if ``dt`` is a variable-length dtype.
   Returns the base type if it is, or ``None`` if not.

.. _complex_dtypes:

Complex numbers
---------------

By default, h5py creates datasets & attributes for complex data with a compound
datatype, which is compatible with HDF5 1.x. This default will probably change
in a future major version of h5py, so you can also specify the compatible
datatype explicitly::

    >>> f = h5py.File('foo.hdf5','w')
    >>> complex_arr = np.arange(100, dtype='c8')
    # Create a dataset with h5py's default (compatible) complex datatype
    >>> f["complex"] = complex_arr
    # Explicitly use the compatible datatype
    >>> ds = f.create_dataset("complex2", (100,), dtype=h5py.complex_compat_dtype('c8'))
    >>> ds[:] = complex_arr  # Write with NumPy complex data

From HDF5 2.0, there is a native datatype for complex numbers. h5py doesn't yet
create new datasets or attributes with this datatype by default, but you can do
so explicitly::

    >>> f = h5py.File('foo.hdf5','w')
    >>> ds = f.create_dataset("complex", (100,), dtype=h5py.h5t.COMPLEX_IEEE_F32LE)
    # Read & write with numpy complex data
    >>> ds[:] = np.arange(100, dtype='c8')

The native & compatible formats both store the data in the same format, as do
NumPy's complex dtypes. So it's cheap to 'convert' between them, as only the
metadata is affected.

.. function:: complex_compat_dtype(complex_dtype, names=('r', 'i'))

   Create a backward-compatible structured dtype for storing complex numbers.

   Pass in a numpy complex dtype specification, e.g. ``'<c8'``, to control size
   and endianness. You can also override the field names for the resulting
   compound dtype; these should match the :doc:`globally configured <config>`
   names, so that the HDF5 datatype can be recognised as complex data.

   .. versionadded:: 3.16

Enumerated types
----------------

HDF5 has the concept of an *enumerated type*, which is an integer datatype
with a restriction to certain named values.  Since NumPy has no such datatype,
HDF5 ENUM types are read and written as integers.

Here's an example of creating an enumerated type::

    >>> dt = h5py.enum_dtype({"RED": 0, "GREEN": 1, "BLUE": 42}, basetype='i')
    >>> h5py.check_enum_dtype(dt)
    {'BLUE': 42, 'GREEN': 1, 'RED': 0}
    >>> f = h5py.File('foo.hdf5','w')
    >>> ds = f.create_dataset("EnumDS", (100,100), dtype=dt)
    >>> ds.dtype.kind
    'i'
    >>> ds[0,:] = 42
    >>> ds[0,0]
    42
    >>> ds[1,0]
    0

.. function:: enum_dtype(values_dict, basetype=np.uint8)

   Create a NumPy representation of an HDF5 enumerated type

   :param values_dict: Mapping of string names to integer values.
   :param basetype: An appropriate integer base dtype large enough to hold the
                    possible options.

.. function:: check_enum_dtype(dt)

   Check if ``dt`` represents an enumerated type.
   Returns the values dict if it is, or ``None`` if not.

Object and region references
----------------------------

References have their :ref:`own section <refs>`.

.. _opaque_dtypes:

Storing other types as opaque data
----------------------------------

.. versionadded:: 3.0

Numpy datetime64 and timedelta64 dtypes have no equivalent in HDF5 (the HDF5
time type is broken and deprecated). h5py allows you to store such data with
an HDF5 opaque type; it can be read back correctly by h5py, but won't be
interoperable with other tools.

Here's an example of storing and reading a datetime array::

    >>> arr = np.array([np.datetime64('2019-09-22T17:38:30')])
    >>> f['data'] = arr.astype(h5py.opaque_dtype(arr.dtype))
    >>> print(f['data'][:])
    ['2019-09-22T17:38:30']

.. function:: opaque_dtype(dt)

   Return a dtype like the input, tagged to be stored as HDF5 opaque type.

.. function:: check_opaque_dtype(dt)

   Return True if the dtype given is tagged to be stored as HDF5 opaque data.

.. note::

   With some exceptions, you can use :func:`opaque_dtype` with any numpy
   dtype. While this may seem like a convenient way to get arbitrary data into
   HDF5, remember that it's not a standard format. It's better to fit your
   data into HDF5's native structures, or use a file format better suited to
   your data.

Older API
---------

Before h5py 2.10, a single pair of functions was used to create and check for
all of these special dtypes. These are still available for backwards
compatibility, but are deprecated in favour of the functions listed above.

.. function:: special_dtype(**kwds)

    Create a NumPy dtype object containing type hints.  Only one keyword
    may be specified.

    :param vlen: Base type for HDF5 variable-length datatype.

    :param enum: 2-tuple ``(basetype, values_dict)``.  ``basetype`` must be
                 an integer dtype; ``values_dict`` is a dictionary mapping
                 string names to integer values.

    :param ref:  Provide class ``h5py.Reference`` or ``h5py.RegionReference``
                 to create a type representing object or region references
                 respectively.

.. function:: check_dtype(**kwds)

    Determine if the given dtype object is a special type.  Example::

        >>> out = h5py.check_dtype(vlen=mydtype)
        >>> if out is not None:
        ...     print("Vlen of type %s" % out)
        str

    :param vlen:    Check for an HDF5 variable-length type; returns base class
    :param enum:    Check for an enumerated type; returns 2-tuple ``(basetype, values_dict)``.
    :param ref:     Check for an HDF5 object or region reference; returns
                    either ``h5py.Reference`` or ``h5py.RegionReference``.