"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"
"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."
"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."
"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"
In the following we show some examples on how to create Datasets in HDF5 (whith h5py) and update values
### First dataset
%% Cell type:code id: tags:
``` python
importh5py
fromtimeitimporttimeit# To measure execution time
importnumpyasnp# this is the main python numerical library
f=h5py.File("testdata.hdf5",'w')
# We create a test 2-d array filled with 1s and with 10 rows and 6 columns
data=np.ones((10,6))
f["dataset_one"]=data
# We now retrieve the dataset from file
dset=f["dataset_one"]
```
%% Cell type:code id: tags:
``` python
#The following instructions show some dataset metadata
print(dset)
print(dset.dtype)
print(dset.shape)
```
%% Cell type:markdown id: tags:
### Dataset slicing
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.
%% Cell type:code id: tags:
``` python
out=dset[...]
print(out)
type(out)
```
%% Cell type:code id: tags:
``` python
dset[1:5,1]=0.0
dset[...]
```
%% Cell type:code id: tags:
``` python
# random 2d distribution
data=np.random.rand(15,10)*2-1
dset=f.create_dataset('random',data=data)
# print the first 5 even rows and the first two columns
out=dset[0:10:2,:2]
print(out)
# clipping to zero all negative values
dset[data<0]=0
```
%% Cell type:markdown id: tags:
### Resizable datasets
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
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 id: tags:
``` python
grp=f.create_group('nisp_frame/detectors/det11')
grp['sci_image']=np.zeros((2040,2040))
print(grp.name)# the group name property
print(grp.parent)# the parent group property
print(grp.file)# the file property
print(grp)# prints some group information. It has one member, the dataset
```
%% Cell type:markdown id: tags:
## Attributes
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.
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