C++ API¶
Note
To include the whole library use
#include <h5features.h>
.The library is then available under the
h5features
namespace.
h5features::exception¶
-
class exception : public std::runtime_error¶
Defines an exception type to report errors from the h5features library.
h5features::version¶
-
enum class 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 v1_0¶
-
constexpr version h5features::current_version = version::v2_0¶
Returns the current (latest) version.
h5features::item¶
-
class 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,optional 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)¶
Instantiates an item from a
name
,features
,times
and optionalproperties
See also
- Parameters:
name – The name of the item
features – The item’s features
times – The item’s timestamps
properties – The item’s properties, if specified
check – When true, ensures the item is valid
- Throws:
h5features::exception – If
check
is true and the item is not valid
-
item(const std::string &name, h5features::features &&features, h5features::times &×, h5features::properties &&properties, bool check = true)¶
Public Functions
-
virtual ~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 also
See also
- Parameters:
deep – When true ensure that the features and timestamps are valid as well by calling item.features().validate() and item.times().validate().
- Throws:
h5features::exception – If the item is not valid
h5features::writer¶
-
class 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
-
virtual ~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)¶
Instantiates a writer.
- Parameters:
filename – The HDF5 file to write on
group – The group in the file to write on
overwrite – 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 data
version – The version of the file to write. Version 1.0 is not available to write, only read.
- Throws:
h5features::exception – When
overwrite
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
- Throws:
h5features::exception – If the item name is already an existing object in the group or if the write operation failed.
-
template<class Iterator>
inline void write(Iterator &&first, Iterator &&last)¶ Writes a sequence of
h5features::item
to disk.- Parameters:
first – The begin iterator on the items to write
last – The end iterator on the items to write
- Throws:
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.
-
virtual ~writer() = default¶
h5features::reader¶
-
class 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
-
virtual ~reader() = default¶
Destructor.
-
reader(const std::string &filename, const std::string &group)¶
Instantiates a reader.
- Parameters:
filename – The HDF5 file to read from
group – The group within the file to read items from
- Throws:
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
- Throws:
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 read
ignore_properties – When true, do not read the item’sproperties
- Throws:
h5features::exception – If the
name
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 specifying the time interval
[start, stop]
to load.- Parameters:
name – The name of the item to read
start – The start time to read the item from
stop – The stop time to read the item from
ignore_properties – When true, do not read the item’s properties
- Throws:
h5features::exception – If the
name
group does not exist or if the read operation failed.
-
virtual ~reader() = default¶
h5features::times¶
-
class 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 timestamp 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 also
- Parameters:
data – The timestamps data
times_format – The actual format of the data
validate – When true, ensures the timestamps are valid
- Throws:
h5features::exception – When
validate
is true, if the timestamps are not valid.
Public Types
Public Functions
-
virtual ~times() = default¶
Destructor.
-
times(const std::vector<double> &start, const std::vector<double> &stop, bool validate = true)¶
Constructs a
times
instance from start and stop timestamps.The built
times
instance has thetimes::format::interval
formatSee also
-
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.
- Throws:
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 \((t_\text{start})_i \leq (t_\text{stop})_i\) for each frame \(i\)
- Throws:
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 also
h5features::features¶
-
class 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 also
- Parameters:
data – A vector of
size * dim
scalars, withsize
frames ofdim
scalars eachdim – The dimension of each frame
check – When true, ensures the features are valid
- Throws:
h5features::exception – If
check
is true and the features are not valid
-
features(std::vector<double> &&data, std::size_t dim, bool check = true)¶
Public Functions
-
virtual ~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
- Throws:
h5features::exception – If the features are not valid
-
features(const std::vector<double> &data, std::size_t dim, bool check = true)¶
h5features::properties¶
-
class 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 = std::variant<std::shared_ptr<properties>, bool, int, double, std::string, std::vector<int>, std::vector<double>, std::vector<std::string>, std::vector<properties>>¶
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.
-
virtual ~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>
inline void set(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 property
value – The value of the property
- Throws:
h5features::exception – If a property with the specified
name
already exist
-
template<class T>
inline T get(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.
- Throws:
h5features::exception – If the property with the specified
name
does not exist or if the specified typeT
does not match the property value.
-
inline auto begin() const¶
Returns an iterator on the beginning of the stored properties.
-
inline auto end() const¶
Returns an iterator at the end of the stored properties.