Newer
Older
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## HDF5 Datasets\n",
"\n",
"In the following we show some examples on how to create Datasets in HDF5 (whith h5py) and update values\n",
"\n",
"### First dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import h5py\n",
"\n",
"from timeit import timeit # To measure execution time\n",
"import numpy as np # this is the main python numerical library\n",
"\n",
"f = h5py.File(\"testdata.hdf5\",'w')\n",
"\n",
"# We create a test 2-d array filled with 1s and with 10 rows and 6 columns\n",
"data = np.ones((10, 6))\n",
"\n",
"f[\"dataset_one\"] = data\n",
"\n",
"# We now retrieve the dataset from file\n",
"dset = f[\"dataset_one\"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#The following instructions show some dataset metadata\n",
"print(dset)\n",
"print(dset.dtype)\n",
"print(dset.shape)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dataset slicing\n",
"\n",
"Datasets provide analogous slicing operations as numpy arrays (with h5py). But these selections are translated by h5py to portion of the dataset and then HDF5 read the data form \"disk\". Slicing into a dataset object returns a NumpPy array.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"out = dset[...]\n",
"\n",
"print(out)\n",
"type(out)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dset[1:5, 1] = 0.0\n",
"dset[...]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# random 2d distribution\n",
"data = np.random.rand(15, 10)*2 - 1\n",
"\n",
"dset = f.create_dataset('random', data=data)\n",
"\n",
"# print the first 5 even rows and the first two columns\n",
"out = dset[0:10:2, :2]\n",
"print(out)\n",
"\n",
"# clipping to zero all negative values\n",
"dset[data<0] = 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Resizable datasets\n",
"\n",
"If we don't know in advance the dataset size and we need to append new data several times, we have to create a resizable dataset, then we have to append data in a scalable manner"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dset = f.create_dataset('dataset_two', (1,1000), dtype=np.float32, \n",
" maxshape=(None, 1000))\n",
"\n",
"a = np.ones((1000,1000))\n",
"\n",
"num_rows = dset.shape[0]\n",
"dset.resize((num_rows+a.shape[0], 1000))\n",
"\n",
"for row in a:\n",
" dset[num_rows,:] = row\n",
" num_rows +=1\n",
"\n",
"print(dset[1000,:20])\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Groups\n",
"\n",
"We can directly create nested groups with a single instruction. For instance to create the group 'nisp_frame', then the subgroup 'detectors' and at last its child group 'det11', we can use the instruction below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grp = f.create_group('nisp_frame/detectors/det11')\n",
"grp['sci_image'] = np.zeros((2040,2040))\n",
"\n",
"print(grp.name) # the group name property\n",
"print(grp.parent) # the parent group property\n",
"print(grp.file) # the file property\n",
"print(grp) # prints some group information. It has one member, the dataset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attributes\n",
"\n",
"Attributes can be defined inside a group or in a dataset. Both have the **.attrs** property to access an attribute or define new attributes. With h5py, the attribute type is inferred from the passed value, but it is also possible to explicitly assign a type."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grp = f['nisp_frame']\n",
"grp.attrs['telescope'] = 'Euclid'\n",
"grp.attrs['instrument'] = 'NISP'\n",
"grp.attrs['pointing'] = np.array([8.48223045516, -20.4610801911, 64.8793517547])\n",
"grp.attrs.create('detector_id', '11', dtype=\"|S2\")\n",
"\n",
"print(grp.attrs['pointing'])\n",
"print(grp.attrs['detector_id'])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f.close()\n",
"!h5ls -vlr testdata.hdf5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tables (compound types)\n",
"\n",
"Tables can be stored as datasets where the elements (rows) have the same compound type. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f = h5py.File(\"testdata.hdf5\")\n",
"dt = np.dtype([('source_id', np.uint32), ('ra', np.float32), ('dec', np.float32), ('magnitude', np.float64)])\n",
"\n",
"grp = f.create_group('source_catalog/det11')\n",
"dset = grp.create_dataset('star_catalog', (100,), dtype=dt)\n",
"\n",
"dset['source_id', 0] = 1\n",
"print(dset['source_id', 'ra', :20])\n",
"print(dset[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References and Region references\n",
"\n",
"In the following instruction we create a reference from the detector 11 scientific image to the corresponding star catalog, which is stored in the same file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sci_image = f['/nisp_frame/detectors/det11/sci_image']\n",
"sci_image.attrs['star_catalog'] = dset.ref\n",
"cat_ref = sci_image.attrs['star_catalog']\n",
"\n",
"print(cat_ref)\n",
"dset = f[cat_ref]\n",
"print(dset[0])\n",
"dt = h5py.special_dtype(ref=h5py.Reference)\n",
"# the above data type dt can be used to create a dataset of references or just an attribute"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"roi = sci_image.regionref[15:20, 36:78]\n",
"sci_image[roi]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Chuncking"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dset = f.create_dataset('chunked', (10,2048,2048), dtype=np.uint16, chunks=(1,64,64))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dset = f.require_dataset('auto_chunked', (2048,2048), dtype=np.float32, compression=\"gzip\")\n",
"print(dset.compression)\n",
"print(dset.compression_opts)\n",
"print(dset.chunks)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}