ProcessWithContext¶
- class audinterface.ProcessWithContext(*, process_func=None, process_func_args=None, sampling_rate=None, resample=False, channels=None, mixdown=False, verbose=False)[source]¶
Alternate processing interface that provides signal context.
In contrast to
Process
this interface does not look at segments in isolation, but passes the complete signal together with a list of segments to the processing function. By doing so, it becomes possible to process segments in context, e.g. by taking into account surrounding signal values or other segments.- Parameters
process_func (
Optional
[Callable
[...
,Sequence
[Any
]]]) –processing function, which expects four positional arguments:
signal
sampling_rate
starts
sequence with start indicesends
sequence with end indices
and any number of additional keyword arguments (see
process_func_args
). There are the following special arguments:'idx'
,'file'
,'root'
. If expected by the function, but not specified inprocess_func_args
, they will be replaced with: a running index, the currently processed file, the root folder. The function must return a sequence of results for every segmentprocess_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing functionsampling_rate (
Optional
[int
]) – sampling rate in Hz. IfNone
it will callprocess_func
with the actual sampling rate of the signalresample (
bool
) – ifTrue
enforces given sampling rate by resamplingchannels (
Union
[int
,Sequence
[int
],None
]) – channel selection, seeaudresample.remix()
mixdown (
bool
) – apply mono mix-down on selectionverbose (
bool
) – show debug messages
- Raises
ValueError – if
resample = True
, butsampling_rate = None
Examples
>>> def running_mean(signal, sampling_rate, starts, ends): ... means_per_segment = [ ... signal[:, start:end].mean() for start, end in zip(starts, ends) ... ] ... cumsum = np.cumsum(np.pad(means_per_segment, 1)) ... return (cumsum[2:] - cumsum[:-2]) / float(2) >>> interface = ProcessWithContext(process_func=running_mean) >>> signal = np.array([1.0, 2.0, 3.0, 1.0, 2.0, 3.0]) >>> sampling_rate = 3 >>> starts = [0, sampling_rate] >>> ends = [sampling_rate, 2 * sampling_rate] >>> interface(signal, sampling_rate, starts, ends) array([2., 1.]) >>> # Apply interface on an audformat conform index of a dataframe >>> import audb >>> db = audb.load( ... "emodb", ... version="1.3.0", ... media="wav/03a01Fa.wav", ... full_path=False, ... verbose=False, ... ) >>> files = list(db.files) * 3 >>> starts = [0, 0.1, 0.2] >>> ends = [0.5, 0.6, 0.7] >>> index = audformat.segmented_index(files, starts, ends) >>> interface.process_index(index, root=db.root) file start end wav/03a01Fa.wav 0 days 00:00:00 0 days 00:00:00.500000 -0.000261 0 days 00:00:00.100000 0 days 00:00:00.600000 -0.000199 0 days 00:00:00.200000 0 days 00:00:00.700000 -0.000111 dtype: float32
__call__()¶
- ProcessWithContext.__call__(signal, sampling_rate, starts, ends)[source]¶
Apply processing to signal.
This function processes the signal without transforming the output into a
pd.Series
. Instead, it will return the raw processed signal. However, if channel selection, mixdown and/or resampling is enabled, the signal will be first remixed and resampled if the input sampling rate does not fit the expected sampling rate.- Parameters
- Return type
- Returns
Processed signal
- Raises
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
process_func_args¶
- ProcessWithContext.process_func_args¶
Additional keyword arguments to processing function.
process_index()¶
- ProcessWithContext.process_index(index, *, root=None, process_func_args=None)[source]¶
Process from a segmented index conform to audformat.
- Parameters
index (
Index
) – index with segment informationroot (
Optional
[str
]) – root folder to expand relative file pathsprocess_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing function. They will temporarily overwrite the ones stored inaudinterface.ProcessWithContext.process_func_args
- Return type
- Returns
Series with processed segments conform to audformat
- Raises
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
RuntimeError – if sequence returned by
process_func
does not match length ofindex
process_signal_from_index()¶
- ProcessWithContext.process_signal_from_index(signal, sampling_rate, index, process_func_args=None)[source]¶
Split a signal into segments and process each segment.
- Parameters
signal (
ndarray
) – signal valuessampling_rate (
int
) – sampling rate in Hzindex (
Index
) – apandas.MultiIndex
with two levels named start and end that hold start and end positions aspandas.Timedelta
objects. See alsoaudinterface.utils.signal_index()
process_func_args (
Optional
[Dict
[str
,Any
]]) – (keyword) arguments passed on to the processing function. They will temporarily overwrite the ones stored inaudinterface.ProcessWithContext.process_func_args
- Return type
- Returns
Series with processed segments conform to audformat
- Raises
RuntimeError – if sampling rates do not match
RuntimeError – if channel selection is invalid
RuntimeError – if sequence returned by
process_func
does not match length ofindex
ValueError – if index contains duplicates