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
dtypeare:'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
dtypeis passedValueError – if
labelsare not passed as string, list, or dictionaryValueError – if
labelsis a table ID, butdtypeis not specifiedValueError – if
labelsare not of same data typeValueError –
dtypedoes not match type oflabelsiflabelsis 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, ordtypedoes not match type of labels from misc table, ordtypeis 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.Databasethe dtype of allaudformat.Columnobjects 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
labelsis 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
pandasdata type.If
labelsis notNone,pandas.CategoricalDtypeis returned. Otherwise the following rules are applied:str->strint->Int64(to allow NaN)float->floattime->timedelta64[ns]date->datetime64[ns]
- Return type
- Returns
pandasdata type
uses_table¶
- Scheme.uses_table¶
Scheme has labels stored in a misc table.
If property is
Truethe attributelabelsis set to an ID of aaudformat.MiscTablewhere the actual label values are stored.- Returns
Trueif scheme has labels stored in a misc table