Terms Module#
soundevent.terms
#
Terms module.
This module provides tools for creating and managing standardized terms.
In the soundevent ecosystem, metadata is stored in Tag
objects, which are pairs of a Term
and a value
. The
Term
provides a standardized definition that gives
context and meaning to the value
. For example, the
Term
scientific_name
gives meaning to the value
"Turdus migratorius"
. Using standardized terms makes data understandable,
shareable, and interoperable.
This module provides three main features:
- Pre-defined Terms: A collection of standard terms for common concepts
in bioacoustics (e.g.,
scientific_name
,f1_score
). - Global API: A set of functions
(
find_term
,add_term
,add_term
, etc. ) for managing terms in a global registry.add_terms_from_file
is a convenience function that loads terms from a file and then registers them. TermRegistry
Class: The underlying class for creating and managing custom term collections.
Examples:
>>> from soundevent.data import Tag, Term
>>> from soundevent.terms import scientific_name, add_term, find_term
>>>
>>> # A list of tags that might be attached to a sound event
>>> tags = []
>>>
>>> # Use a pre-defined term to create a Tag
>>> species_tag = Tag(term=scientific_name, value="Turdus migratorius")
>>> tags.append(species_tag)
>>>
>>> print(f"{tags[0].term.label}: {tags[0].value}")
Scientific Taxon Name: Turdus migratorius
>>>
>>> # Create and use a custom term for a new Tag
>>> add_term(
... Term(
... name="custom:quality",
... label="Quality",
... definition="The quality of the recording, from 1 (poor) to 5 (excellent).",
... )
... )
>>> quality_term = find_term(q="quality")[0]
>>> quality_tag = Tag(term=quality_term, value="4")
>>> tags.append(quality_tag)
>>>
>>> print(f"{tags[1].term.label}: {tags[1].value}")
Quality: 4
Classes:
Name | Description |
---|---|
TermRegistry |
A mutable mapping for managing, storing, and retrieving |
TermSet |
A collection of terms and their optional mappings. |
Functions:
Name | Description |
---|---|
add_term |
Register a term. |
find_term |
Find terms by substring match. |
get_term |
Retrieve a term by its key. |
get_term_by |
Retrieve one term by an exact match on a single attribute. |
has_term |
Check if a term exists in the registry. |
remove_term |
Remove a term from the registry by its key. |
add_terms_from_file |
Load terms from a file and add them to a registry. |
get_global_term_registry |
Return the current global term registry. |
set_global_term_registry |
Set a new global term registry. |
Classes#
TermRegistry(terms=None)
#
Bases: MutableMapping[str, Term]
A mutable mapping for managing, storing, and retrieving Term
objects.
Provides dictionary-like access (getting, setting, deleting by key) along
with specialized methods for finding terms based on their attributes.
It serves as a central point to manage and access standardized Term
objects within a project.
Attributes:
Name | Type | Description |
---|---|---|
_terms |
Dict[str, Term]
|
The internal dictionary holding the registered terms. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
terms
|
Optional[Dict[str, Term]]
|
A dictionary of initial terms {key: term} to populate the registry. Defaults to an empty registry. |
None
|
Methods:
Name | Description |
---|---|
add_term |
Register a term, optionally defaulting the key to |
find |
Find terms by substring match; returns multiple terms. |
get |
Retrieve a term by key, returning a default if not found. |
get_by |
Retrieve one term by an exact match on a single attribute. |
remove |
Remove a term from the registry by its key. |
Functions#
add_term(term, key=None, force=False)
#
Register a term, optionally defaulting the key to term.name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
term
|
Term
|
The |
required |
key
|
Optional[str]
|
The key to use for registration. If None, |
None
|
force
|
bool
|
If True, allows overriding an existing term with the same key.
If False, raises |
False
|
Raises:
Type | Description |
---|---|
TermOverrideError
|
If |
find(label=None, name=None, uri=None, definition=None, q=None, ignore_case=True)
#
Find terms by substring match; returns multiple terms.
If q
is provided, it searches label
, name
, uri
, and
definition
for a match (OR logic).
If q
is not provided, it searches using the specific fields,
requiring all provided fields to match (AND logic).
If no arguments are given, all terms are returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label
|
Optional[str]
|
Substring to search for in labels. |
None
|
name
|
Optional[str]
|
Substring to search for in names. |
None
|
uri
|
Optional[str]
|
Substring to search for in URIs. |
None
|
definition
|
Optional[str]
|
Substring to search for in definitions. |
None
|
q
|
Optional[str]
|
General query string (searches all fields, OR logic). |
None
|
ignore_case
|
bool
|
Perform case-insensitive search. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
list[Term]
|
A list of matching |
Raises:
Type | Description |
---|---|
ValueError
|
If |
get(key, default=None)
#
Retrieve a term by key, returning a default if not found.
Mimics dict.get()
. Returns None
by default if the key is not
found, or a specified default value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key of the term to retrieve. |
required |
default
|
Any
|
The value to return if the key is not found. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Optional[Term]
|
The |
Raises:
Type | Description |
---|---|
ValueError
|
If |
get_by(label=None, name=None, uri=None)
#
Retrieve one term by an exact match on a single attribute.
Requires exactly one search criterion and expects exactly one match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label
|
Optional[str]
|
The exact label to match. |
None
|
name
|
Optional[str]
|
The exact name to match. |
None
|
uri
|
Optional[str]
|
The exact URI to match. |
None
|
Returns:
Type | Description |
---|---|
Term
|
The single |
Raises:
Type | Description |
---|---|
ValueError
|
If zero or more than one criterion is provided. |
TermNotFoundError
|
If no term matches the criterion. |
MultipleTermsFoundError
|
If more than one term matches the criterion. |
TermSet
#
Functions#
add_term(term, key=None, term_registry=None, force=False)
#
Register a term.
By default, the key is derived from term.name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
term
|
Term
|
The |
required |
key
|
Optional[str]
|
The key to use for registration. If None, |
None
|
term_registry
|
Optional[TermRegistry]
|
If provided, the term is added to this registry instead of the global one. |
None
|
force
|
bool
|
If True, overwrite any existing term with the same key. |
False
|
Raises:
Type | Description |
---|---|
KeyError
|
If |
find_term(label=None, name=None, uri=None, definition=None, q=None, ignore_case=True, term_registry=None)
#
Find terms by substring match.
If q
is provided, it searches label
, name
, uri
, and
definition
for a match (OR logic).
If q
is not provided, it searches using the specific fields,
requiring all provided fields to match (AND logic).
If no arguments are given, all terms are returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label
|
Optional[str]
|
Substring to search for in labels. |
None
|
name
|
Optional[str]
|
Substring to search for in names. |
None
|
uri
|
Optional[str]
|
Substring to search for in URIs. |
None
|
definition
|
Optional[str]
|
Substring to search for in definitions. |
None
|
q
|
Optional[str]
|
General query string (searches all fields, OR logic). |
None
|
ignore_case
|
bool
|
Perform case-insensitive search. |
True
|
term_registry
|
Optional[TermRegistry]
|
If provided, the search is performed on this registry instead of the global one. |
None
|
Returns:
Type | Description |
---|---|
List[Term]
|
A list of matching |
get_term(key, default=None, term_registry=None)
#
Retrieve a term by its key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key of the term to retrieve. |
required |
default
|
Optional[Term]
|
The value to return if the key is not found. |
None
|
term_registry
|
Optional[TermRegistry]
|
If provided, the term is retrieved from this registry instead of the global one. |
None
|
Returns:
Type | Description |
---|---|
Optional[Term]
|
The |
get_term_by(label=None, name=None, uri=None, term_registry=None)
#
Retrieve one term by an exact match on a single attribute.
Requires exactly one search criterion and expects exactly one match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
label
|
Optional[str]
|
The exact label to match. |
None
|
name
|
Optional[str]
|
The exact name to match. |
None
|
uri
|
Optional[str]
|
The exact URI to match. |
None
|
term_registry
|
Optional[TermRegistry]
|
If provided, the search is performed on this registry instead of the global one. |
None
|
Returns:
Type | Description |
---|---|
Term
|
The single |
Raises:
Type | Description |
---|---|
ValueError
|
If zero or more than one criterion is provided. |
TermNotFoundError
|
If no term matches the criterion. |
MultipleTermsFoundError
|
If more than one term matches the criterion. |
has_term(key, term_registry=None)
#
Check if a term exists in the registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key of the term to check. |
required |
term_registry
|
Optional[TermRegistry]
|
If provided, the check is performed on this registry instead of the global one. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if the term exists, False otherwise. |
remove_term(key, term_registry=None)
#
Remove a term from the registry by its key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key of the term to remove. |
required |
term_registry
|
Optional[TermRegistry]
|
If provided, the term is removed from this registry instead of the global one. |
None
|
Raises:
Type | Description |
---|---|
KeyError
|
If no term is found with the given key. |
add_terms_from_file(path, term_registry=None, format=None, override_existing=False, ignore_overrides=True, ignore_missing_key=True)
#
Load terms from a file and add them to a registry.
This function provides options to handle cases where a term being loaded already exists in the registry, or when a mapping refers to a non-existent term.
The format can be specified explicitly. If not, it will be inferred from the file extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathLike
|
The path to the file. |
required |
term_registry
|
Optional[TermRegistry]
|
The registry to add the terms to. If None, the global registry is used. |
None
|
format
|
Optional[TermFormat]
|
The format of the file. If None, it will be inferred from the file extension. |
None
|
override_existing
|
bool
|
If True, existing terms with the same name will be overwritten. Defaults to False. |
False
|
ignore_overrides
|
bool
|
If True, and |
True
|
ignore_missing_key
|
bool
|
If True, any alias in the mapping that refers to a non-existent
term will be skipped. If False, a |
True
|
Raises:
Type | Description |
---|---|
TermOverrideError
|
If |
TermNotFoundError
|
If |
Notes
See [soundevent.terms.io][] for detailed information on the supported JSON and CSV file structures.
get_global_term_registry()
#
Return the current global term registry.
Returns:
Type | Description |
---|---|
TermRegistry
|
The active global |
set_global_term_registry(term_registry)
#
Set a new global term registry.
This function replaces the existing global registry with a new one. All subsequent calls to functions in this module will operate on the new registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
term_registry
|
TermRegistry
|
The new |
required |
Term Library#
soundevent.terms.library
#
Modules:
Name | Description |
---|---|
devices |
|
geography |
|
metrics |
|
roi |
|
taxonomy |
|
Attributes: