write

ketos.data_handling.database_interface.write(x, table, table_annot=None, id=None)[source]

Write waveform or spectrogram and annotations to HDF5 tables.

Note: If the id argument is not specified, the row number will will be used as a unique identifier for the spectrogram.

When a list of audio objects is provided, only the instance attributes (filename, offset, label, annotations, etc.) of the first object is written to the table.

Args:
x: instance of audio.waveform.Waveform,

audio.spectrogram.MagSpectrogram, audio.spectrogram.PowerSpectrogram, audio.spectrogram.MelSpectrogram, audio.spectrogram.CQTSpectrogram, numpy.ndarray The audio object to be stored in the table. It is also possible to specify a list of audio objects. The number of objects must match the number of data columns in the table.

table: tables.Table

Table in which the audio data will be stored. (described by table_description()).

table_annot: tables.Table

Table in which the annotations will be stored. (described by table_description_weak_annot() or table_description_strong_annot()).

id: int

Audio object unique identifier. Optional.

Returns:

None.

Examples:
>>> import tables
>>> from ketos.data_handling.database_interface import open_file, create_table, table_description, table_description_annot, write
>>> from ketos.audio.spectrogram import MagSpectrogram
>>> from ketos.audio.waveform import Waveform
>>>
>>> # Create an Waveform object from a .wav file
>>> audio = Waveform.from_wav('ketos/tests/assets/2min.wav')
>>> # Use that signal to create a spectrogram
>>> spec = MagSpectrogram.from_waveform(audio, window=0.2, step=0.05)
>>> # Add a single annotation
>>> spec.annotate(label=1, start=0., end=2.)
>>>
>>> # Open a connection to a new HDF5 database file
>>> h5file = open_file("ketos/tests/assets/tmp/database2.h5", 'w')
>>> # Create table descriptions for storing the spectrogram data
>>> descr_data = table_description(spec)
>>> descr_annot = table_description_annot()
>>> # Create tables
>>> tbl_data = create_table(h5file, "/group1/", "table_data", descr_data) 
>>> tbl_annot = create_table(h5file, "/group1/", "table_annot", descr_annot) 
>>> # Write spectrogram and its annotation to the tables
>>> write(spec, tbl_data, tbl_annot)
>>> # flush memory to ensure data is put in the tables
>>> tbl_data.flush()
>>> tbl_annot.flush()
>>>
>>> # Check that the spectrogram data have been saved 
>>> tbl_data.nrows
1
>>> tbl_annot.nrows
1
>>> # Check annotation data
>>> tbl_annot[0]['label']
1
>>> tbl_annot[0]['start']
0.0
>>> tbl_annot[0]['end']
2.0
>>> # Check audio source data
>>> tbl_data[0]['filename'].decode()
'2min.wav'
>>> h5file.close()