C++ API¶
Note
To include the whole library use
#include <h5features.h>
.The library is then available under the
h5features
namespace.
Contents
h5features::exception¶
-
class
h5features
::
exception
: public std::runtime_error¶ Defines an exception type to report errors from the h5features library.
h5features::version¶
-
enum
h5features
::
version
¶ The different h5features format versions.
This is not the version of the h5features library but the available versions of the underlying file format.
Values:
-
enumerator
v1_0
¶
-
enumerator
v1_1
¶
-
enumerator
v1_2
¶
-
enumerator
v2_0
¶
-
enumerator
-
constexpr version
h5features
::
current_version
= version::v2_0¶ Returns the current (latest) version.
-
version
h5features
::
read_version
(const hdf5::Group &group)¶ Look for a “version” attribute in the
group
and return its value.- Exceptions
h5features::exception
: If the “version” attribute is not found or is not valid.
h5features::item¶
-
class
h5features
::
item
¶ Handles the features of a single item (e.g. a speech signal)
An item is made of 4 components:
Its name,
the features (
h5features::features
) consist a suite of vectors of constant dimension,the timestamps (
h5features::times
) associates a start and stop time to each features vector,optionnal properties (
h5features::properties
) attached to the features, e.g. algorithm parameters or other attributes.This class interfaces such an item between user code and an h5features file, supporting read and write operations.
Constructors
-
item
(const std::string &name, const h5features::features &features, const h5features::times ×, const h5features::properties &properties = {}, bool check = true)¶ Instanciates an item from a
name
,features
,times
and optionalproperties
- See
- Parameters
name
: The name of the itemfeatures
: The item’s featurestimes
: The item’s timestampsproperties
: The item’s properties, if specifiedcheck
: When true, ensures the item is valid
- Exceptions
h5features::exception
: Ifcheck
is true and the item is not valid
-
item
(const std::string &name, h5features::features &&features, h5features::times &×, h5features::properties &&properties, bool check = true)¶ Instanciates an item from a
name
,features
,times
and optionalproperties
- See
- Parameters
name
: The name of the itemfeatures
: The item’s featurestimes
: The item’s timestampsproperties
: The item’s properties, if specifiedcheck
: When true, ensures the item is valid
- Exceptions
h5features::exception
: Ifcheck
is true and the item is not valid
Public Functions
-
~item
() = default¶ Destructor.
-
std::size_t
dim
() const¶ Returns the dimension of the features.
-
std::size_t
size
() const noexcept¶ Returns the number of vectors in the features.
-
std::string
name
() const noexcept¶ Returns the item’s name.
-
bool
has_properties
() const noexcept¶ Returns true if the item has attached properties, false otherwise.
-
const h5features::properties &
properties
() const noexcept¶ Returns the item’s properties.
-
void
validate
(bool deep = false) const¶ Ensures the item has a valid state.
An item is valid if an only if:
It’s name is not empty
It contains at least one features vector
The features and timestamps have the same size
- See
- Parameters
deep
: When true ensure that the features and timestamps are valid as well by calling item.features().validate() and item.times().validate().
- Exceptions
h5features::exception
: If the item is not valid
h5features::writer¶
-
class
h5features
::
writer
¶ The writer class dumps
h5features::item
instances to a HDF5 file.A writer is non-copyable and is attached to an HDF5 file and a group within this file. Each item will then be written as a sub-group in this group.
Public Functions
-
~writer
() = default¶ Destructor.
-
writer
(const std::string &filename, const std::string &group = "features", bool overwrite = false, bool compress = true, h5features::version version = h5features::current_version)¶ Instanciates a writer.
- Parameters
filename
: The HDF5 file to write ongroup
: The group in the file to write onoverwrite
: If true erase the file content if it is already existing. If false it will append new items to the existing group.compress
: When true, compress the dataversion
: The version of the file to write. Version 1.0 is not available to write, only read.
- Exceptions
h5features::exception
: Whenoverwrite
is true, if thegroup
already exists in the file and the version is not supported. Or if the requestedversion
is not supported.
-
void
write
(const h5features::item &item)¶ Writes a
h5features::item
to disk.- Parameters
item
: The item to write
- Exceptions
h5features::exception
: If the item name is already an existing object in the group or if the write operation failed.
-
template<class
Iterator
>
voidwrite
(Iterator &&first, Iterator &&last)¶ Writes a sequence of
h5features::item
to disk.- Parameters
first
: The begin iterator on the items to writelast
: The end iterator on the items to write
- Exceptions
h5features::exception
: If one item is already an existing object in the group or if the write operation failed.
-
std::string
filename
() const¶ Returns the HDF5 file name.
-
std::string
groupname
() const¶ Returns the HDF5 group name.
-
h5features::reader¶
-
class
h5features
::
reader
¶ The reader class reads
h5features::item
from a HDF5 file.A reader is non-copyable and is attached to an HDF5 file and a group within this file.
Public Functions
-
~reader
() = default¶ Destructor.
-
reader
(const std::string &filename, const std::string &group)¶ Instanciates a reader.
- Parameters
filename
: The HDF5 file to read fromgroup
: The group with the file to read items from
- Exceptions
h5features::exception
: If the file cannot be opened or if the group does not exist in the file.
-
std::string
filename
() const¶ Returns the name of the file being read.
-
std::string
groupname
() const¶ Returns the name of the group being read in the file.
-
std::vector<std::string>
items
() const¶ Returns the name of stored items.
The items can be retrieved with
read_all
orread_item
-
std::vector<h5features::item>
read_all
(bool ignore_properties = false) const¶ Returns all the items stored in the file.
- Parameters
ignore_properties
: When true, do not read the item’sproperties
- Exceptions
h5features::exception
: If the read operation failed.
-
h5features::item
read_item
(const std::string &name, bool ignore_properties = false) const¶ Reads and returns a
h5features::item
instance.- Parameters
name
: The name of the item to readignore_properties
: When true, do not read the item’sproperties
- Exceptions
h5features::exception
: If thename
group does not exist or if the read operation failed.
-
h5features::item
read_item
(const std::string &name, double start, double stop, bool ignore_properties = false) const¶ Partial Read of a
h5features::item
The method allows to partially load the content of an item, by specifiying the time interval
[start, stop]
to load.- Parameters
name
: The name of the item to readstart
: The start time to read the item fromstop
: The stop time to read the item fromignore_properties
: When true, do not read the item’s properties
- Exceptions
h5features::exception
: If thename
group does not exist or if the read operation failed.
-
h5features::times¶
-
class
h5features
::
times
¶ The times class handles the timestamps of an item.
The times are expressed in second as float64 values.
Each features frame in an item is associated to a timestamp. It can have one of the two following formats:
times::format::simple
where the timestamp is a single scalar and is interpreted as the center of the frame’s time windowtimes::format::interval
where the timestamp is a pair(tstart, tstop)
interpreted as the begin and end of the frame’s time window.
Constructors
-
times
(const std::vector<double> &data, format times_format, bool validate = true)¶ Constructs a
times
instance from raw data.The times are expressed in seconds as float64 values.
If
times_format == times::format::simple
, the timestamps have a single dimension and each timstamp is interpreted as the center time of its associated frame. In that casedata[i]
is the center time of the framei
.If
times_format == times::format::interval
there is two timestamps per frame, interpreted as the(tstart, tstop)
times for each frame. Heredata[2*i]
anddata[2*i+1]
are the start and stop time of the framei
respectively.
- See
- Parameters
data
: The timestamps datatimes_format
: The actual format of the datavalidate
: When true, ensures the timestamps are valid
- Exceptions
h5features::exception
: Whenvalidate
is true, if the timestamps are not valid.
-
times
(std::vector<double> &&data, format times_format, bool validate = true)¶ Constructs a
times
instance from raw data.The times are expressed in seconds as float64 values.
If
times_format == times::format::simple
, the timestamps have a single dimension and each timstamp is interpreted as the center time of its associated frame. In that casedata[i]
is the center time of the framei
.If
times_format == times::format::interval
there is two timestamps per frame, interpreted as the(tstart, tstop)
times for each frame. Heredata[2*i]
anddata[2*i+1]
are the start and stop time of the framei
respectively.
- See
- Parameters
data
: The timestamps datatimes_format
: The actual format of the datavalidate
: When true, ensures the timestamps are valid
- Exceptions
h5features::exception
: Whenvalidate
is true, if the timestamps are not valid.
Public Types
Public Functions
-
~times
() = default¶ Destructor.
-
times
(const std::vector<double> &start, const std::vector<double> &stop, bool validate = true)¶ Contructs a
times
instance from start and stop timestamps.The built
times
instance has thetimes::format::interval
format- See
- Parameters
start
: The start timestamps of each framestop
: The stop timestamps of each framevalidate
: When true, ensures the timestamps are valid
- Exceptions
h5features::exception
: Ifstart.size() != stop.size()
h5features::exception
: Whenvalidate
is true, if the timestamps are not valid.
-
bool
operator==
(const times &other) const noexcept¶ Returns true if the two
times
instances are equal.
-
bool
operator!=
(const times &other) const noexcept¶ Returns true if the two
times
instances are not equal.
-
std::size_t
size
() const noexcept¶ Returns the size (number of frames) of the timestamps.
-
std::size_t
dim
() const noexcept¶ Returns the timestamps dimension (1 or 2)
-
const std::vector<double> &
data
() const noexcept¶ Returns the timestamps raw data.
-
double
start
() const¶ Returns the first timestamp.
- Exceptions
h5features::exception
: If times is empty
-
double
stop
() const¶ Returns the last timestamp.
- Exceptions
h5features::exception
: If times is empty
-
void
validate
() const¶ Throws a
h5features::exception
if the timestamps are not valid.The timestamps are valid if and only if:
size() != 0
sorted in increasing order
when
times::format::interval
, we have \(tstart_i \leq tstop_i\) for each frame \(i\)
- Exceptions
h5features::exception
: If the timestamps are not valid
-
std::pair<std::size_t, std::size_t>
get_indices
(double start, double stop) const¶ Returns the indices
[i, j]
of the closest timestamps to[start, stop]
This method is used by
h5features::item
for partial reads of features.- See
- Parameters
start
: The start time to get the index fromstop
: The stop time to get the index from
- Exceptions
h5features::exception
: Ifstart
is greater thanstop
h5features::exception
: If there is not valid indices for the requested interval
-
times
select
(const std::size_t &start, const std::size_t &stop) const¶ Returns a subset of those timestamps from
start
tostop
- Parameters
start
: The first index of the returned timesstop
: The last index of the returned times
- Exceptions
h5features::exception
: Ifstart > stop
orstop > size()
h5features::features¶
-
class
h5features
::
features
¶ The features class stores the features vectors of a
h5features::item
Each item’s timestamp is associated to a features vector of constant dimension. Features are stored as float64 values.
Constructors
-
features
(const std::vector<double> &data, std::size_t dim, bool check = true)¶ Constructs a
features
instance from features vectors.- See
- Parameters
data
: A vector ofsize * dim
scalars, withsize
frames ofdim
scalars eachdim
: The dimension of each framecheck
: When true, ensures the features are valid
- Exceptions
h5features::exception
: Ifcheck
is true and the features are not valid
-
features
(std::vector<double> &&data, std::size_t dim, bool check = true)¶ Constructs a
features
instance from features vectors.- See
- Parameters
data
: A vector ofsize * dim
scalars, withsize
frames ofdim
scalars eachdim
: The dimension of each framecheck
: When true, ensures the features are valid
- Exceptions
h5features::exception
: Ifcheck
is true and the features are not valid
Public Functions
-
~features
() = default¶ Destructor.
-
bool
operator==
(const features &other) const noexcept¶ Returns true if the two
features
instances are equal.
-
bool
operator!=
(const features &other) const noexcept¶ Returns true if the two
features
instances are not equal.
-
std::size_t
dim
() const¶ Returns the dimension of a features vector.
-
std::size_t
size
() const noexcept¶ Returns the number of features vectors.
-
void
validate
() const¶ Throws a
h5features::exception
if the features are not valid.The features are valid if and only if:
it is non empty (at least one features vector)
all the vectors have the same dimension
- Exceptions
h5features::exception
: If the features are not valid
-
h5features::properties¶
-
class
h5features
::
properties
¶ Handles the properties attached to a
h5features::item
Properties are a set of
(name, value)
pairs. It can be used to store attributes attached to features, such as generation parameters.A
h5features::properties
instance is created empty.A
(name, value)
pair is added to it withset()
and accessed withget()
.The
name
must be astd::string
.The
value
type must be one specified byproperties::value_type
.
Public Types
-
using
value_type
= boost::variant<boost::recursive_wrapper<properties>, bool, int, double, std::string, std::vector<int>, std::vector<double>, std::vector<std::string>>¶ The types a property value can take.
Its value can be a scalar (
bool
,int
,double
orstd::string
), a vector (ofint
,double
orstd::string
) or anotherh5features::properties
.
Public Functions
-
properties
() = default¶ Constructor.
-
~properties
() = default¶ Destructor.
-
properties
(const properties&) = default¶ Copy constructor.
-
properties
(properties&&) = default¶ Move constructor.
-
properties &
operator=
(const properties&) = default¶ Copy operator.
-
properties &
operator=
(properties&&) = default¶ Move operator.
-
bool
operator==
(const properties &other) const¶ Returns
true
if the two properties are equal.
-
bool
operator!=
(const properties &other) const¶ Returns
true
if the two properties are not equal.
-
std::size_t
size
() const¶ Returns the number of
(name, value)
pairs stored.
-
std::set<std::string>
names
() const¶ Returns the names of the stored properties.
-
bool
contains
(const std::string &name) const¶ Returns true if a value attached to
name
is stored, false otherwise.
-
void
erase
(const std::string &name)¶ Deletes the given
name
from the properties if present.
-
template<class
T
>
voidset
(const std::string &name, const T &value)¶ Adds a new
(name, value)
pair in the properties.The added
value
must of one of the types specified byproperties::value_type
.- Parameters
name
: The name of the propertyvalue
: The value of the property
- Exceptions
h5features::exception
: If a property with the specifiedname
already exist
-
template<class
T
>
Tget
(const std::string &name) const¶ Returns the value of a property given its
name
and typeT
The returned
value
must of one of the types specified byvalue_type
.- Parameters
name
: The name of the property to read.
- Exceptions
h5features::exception
: If the property with the specifiedname
does not exist or if the specified typeT
does not match the property value.
-
auto
begin
() const¶ Returns an iterator on the beginning of the stored properties.
-
auto
end
() const¶ Returns an iterator at the end of the stored properties.