Load acquired data from .h5 file
Difficulty Level:
Tags open☁load☁h5

For storing large amounts of data a .h5 (hierarchical data format) file defines an interesting approach. This is one of the predefined OpenSignals output files formats. It will be explained how to load/transpose the data inside .h5 file to a Python list, that can easily be manipulated in the processing operations.


1 - Needed packages importation

In [1]:
# Package used for loading data from the input h5 file
from h5py import File

# Package intended to work with arrays
from numpy import array

# biosignalsnotebooks python package
import biosignalsnotebooks as bsnb

2 - Creation of a h5py object from the file named "ecg_sample.h5"

In [2]:
file_folder = "/signal_samples"
file_name = "ecg_sample.h5"
file_path = file_folder + "/" + file_name

h5_object = File(file_path)

3 - Inspection of .h5 file internal structure/groups (in this case the mac address list of the used devices for acquiring data)

In [3]:
# Keys list (.h5 hierarchy ground level)
list(h5_object.keys())
Out[3]:
['00:07:80:3B:46:61']

4 - Access to the second hierarchy level through group key "00:07:80:3B:46:61"

In [4]:
h5_group = h5_object.get('00:07:80:3B:46:61')
print ("Second hierarchy level: " + str(list(h5_group)))
Second hierarchy level: ['digital', 'events', 'plugin', 'raw', 'support']

5 - Identification of h5_group metadata attributes

In [5]:
print ("Metadata of h5_group: \n" + str(list(h5_group.attrs.keys())))
Metadata of h5_group: 
['channels', 'comments', 'date', 'device', 'device connection', 'device name', 'digital IO', 'duration', 'firmware version', 'forcePlatform values', 'keywords', 'macaddress', 'mode', 'nsamples', 'resolution', 'sampling rate', 'sync interval', 'time']

6 - Storage of acquisition sampling rate by accessing h5_group metadata (attributes)

In [6]:
sampling_rate = h5_group.attrs.get("sampling rate")
print ("Sampling Rate: " + str(sampling_rate))
Sampling Rate: 200

7 - Access to the third level of data through group key "00:07:80:3B:46:61" and sub-group key "raw"

In [7]:
h5_sub_group = h5_group.get("raw")
print("Third hierarchy level: " + str(list(h5_sub_group)))
Third hierarchy level: ['channel_1', 'nSeq']

8 - Transposition of "channel_1" dataset to a Python list (the units are mV). This sub-group corresponds to the data acquired at channel 1

In [8]:
h5_data = h5_sub_group.get("channel_1")

# Conversion of a nested list to a flatten list by list-comprehension
# The following line is equivalent to:
# for sublist in h5_data:
#    for item in sublist:
#        flat_list.append(item)
data_list = [item for sublist in h5_data for item in sublist]
time = bsnb.generate_time(data_list, sampling_rate)

# Signal data samples values and graphical representation.
print (array([item for sublist in h5_data for item in sublist]))
bsnb.plot([time], [data_list], x_axis_label="Time (s)", y_axis_label="Raw Data")
[32452 32394 32448 ... 33120 33164 33192]

This procedure can be automatically done by load function of biosignalsnotebooks package

With the described steps the user acquire the capability of exploring h5 files, navigating through his hierarchy and accessing data and metadata.

In order to access this information through a graphical user interface, there are interesting applications such as HDFView .

In the following animation the file hierarchy is presented together with the signal samples in channel 1 and the file metadata/attributes.

In [9]:
%%javascript
document.getElementById("video_1").play()

It is amazing how a great amount of information can be organized in such an elegant way !

With the current Jupyter Notebook you can easily access and work with data inside .h5 files and understand a little bit the internal "hierarchy" of this file format !

We hope that you have enjoyed this guide. biosignalsnotebooks is an environment in continuous expansion, so don"t stop your journey and learn more with the remaining Notebooks !

In [10]:
from biosignalsnotebooks.__notebook_support__ import css_style_apply
css_style_apply()
.................... CSS Style Applied to Jupyter Notebook .........................
Out[10]: