Scheme¶
- class audformat.Scheme(dtype=None, *, labels=None, minimum=None, maximum=None, description=None, meta=None)[source]¶
A scheme defines valid values of an annotation.
Allowed values for
dtype
are:'bool'
,'int'
,'float'
,'object'
,'str'
,'time'
, and'date'
(seeaudformat.define.DataType
). Values can be restricted to a set of labels provided by a list, dictionary or a table ID of aaudformat.MiscTable
, where the values of the index are used as labels. A continuous range can be limited by a minimum and maximum value.- Parameters
- Raises
BadValueError – if an invalid
dtype
is passedValueError – if
labels
are not passed as string, list, or dictionaryValueError – if
labels
is a table ID, butdtype
is not specifiedValueError – if
labels
are not of same data typeValueError –
dtype
does not match type oflabels
iflabels
is a list or dictionaryValueError – when assigning a scheme, that contains a table ID as
labels
, to a database, but the corresponding misc table is not part of the database, or the given table ID is not a misc table, or its index is multi-dimensional, or its index contains duplicates, ordtype
does not match type of labels from misc table, ordtype
is set tobool
, or the misc table has a column that is already assigned to a scheme with labels from another misc table
Examples
>>> Scheme() {dtype: str} >>> Scheme(labels=["a", "b", "c"]) dtype: str labels: [a, b, c] >>> Scheme(define.DataType.INTEGER) {dtype: int} >>> Scheme("float", minimum=0, maximum=1) {dtype: float, minimum: 0, maximum: 1} >>> # Use index of misc table as labels >>> import audformat >>> db = audformat.Database("mydb") >>> db["speaker"] = audformat.MiscTable(pd.Index(["spk1", "spk2"], name="speaker")) >>> Scheme("str", labels="speaker") {dtype: str, labels: speaker}
__contains__()¶
dtype¶
- Scheme.dtype¶
Data type.
Possible return values are given by
audformat.define.DataType
.
dump()¶
from_dict()¶
labels_as_list¶
- Scheme.labels_as_list¶
Scheme labels as list.
If scheme does not define labels an empty list is returned.
- Returns
list of labels
replace_labels()¶
- Scheme.replace_labels(labels)[source]¶
Replace labels.
If scheme is part of a
audformat.Database
the dtype of allaudformat.Column
objects that reference the scheme will be updated. Removed labels are set toNaN
.- Parameters
- Raises
ValueError – if scheme does not define labels
ValueError – if dtype of new labels does not match dtype of scheme
ValueError – if
labels
is a misc table ID and the scheme is already assigned to a database, but the corresponding misc table is not part of the database, or the given table ID is not a misc table, or its index is multi-dimensional, or its index contains duplicates, or the misc table has a column that is already assigned to a scheme with labels from another misc table
Examples
>>> speaker = Scheme( ... labels={ ... 0: {"gender": "female"}, ... 1: {"gender": "male"}, ... } ... ) >>> speaker dtype: int labels: 0: {gender: female} 1: {gender: male} >>> speaker.replace_labels( ... { ... 1: {"gender": "male", "age": 33}, ... 2: {"gender": "female", "age": 44}, ... } ... ) >>> speaker dtype: int labels: 1: {gender: male, age: 33} 2: {gender: female, age: 44}
to_dict()¶
to_pandas_dtype()¶
- Scheme.to_pandas_dtype()[source]¶
Convert data type to
pandas
data type.If
labels
is notNone
,pandas.CategoricalDtype
is returned. Otherwise the following rules are applied:str
->str
int
->Int64
(to allow NaN)float
->float
time
->timedelta64[ns]
date
->datetime64[ns]
- Return type
- Returns
pandas
data type
uses_table¶
- Scheme.uses_table¶
Scheme has labels stored in a misc table.
If property is
True
the attributelabels
is set to an ID of aaudformat.MiscTable
where the actual label values are stored.- Returns
True
if scheme has labels stored in a misc table