Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 36 additions & 33 deletions audinterface/core/feature.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import annotations

from collections.abc import Callable
from collections.abc import Sequence
import errno
import inspect
import os
import typing

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -246,25 +249,25 @@ class Feature:

def __init__(
self,
feature_names: typing.Union[str, typing.Sequence[str]],
feature_names: str | Sequence[str],
*,
name: str = None,
params: typing.Dict = None,
process_func: typing.Callable[..., typing.Any] = None,
process_func_args: typing.Dict[str, typing.Any] = None,
name: str | None = None,
params: dict | None = None,
process_func: Callable[..., object] | None = None,
process_func_args: dict[str, object] | None = None,
process_func_is_mono: bool = False,
process_func_applies_sliding_window: bool = False,
sampling_rate: int = None,
sampling_rate: int | None = None,
resample: bool = False,
channels: typing.Union[int, typing.Sequence[int]] = 0,
channels: int | Sequence[int] = 0,
mixdown: bool = False,
win_dur: Timestamp = None,
hop_dur: Timestamp = None,
min_signal_dur: Timestamp = None,
max_signal_dur: Timestamp = None,
segment: Segment = None,
win_dur: Timestamp | None = None,
hop_dur: Timestamp | None = None,
min_signal_dur: Timestamp | None = None,
max_signal_dur: Timestamp | None = None,
segment: Segment | None = None,
keep_nat: bool = False,
num_workers: typing.Optional[int] = 1,
num_workers: int | None = 1,
multiprocessing: bool = False,
verbose: bool = False,
):
Expand Down Expand Up @@ -359,10 +362,10 @@ def process_file(
self,
file: str,
*,
start: Timestamp = None,
end: Timestamp = None,
root: str = None,
process_func_args: typing.Dict[str, typing.Any] = None,
start: Timestamp | None = None,
end: Timestamp | None = None,
root: str | None = None,
process_func_args: dict[str, object] | None = None,
) -> pd.DataFrame:
r"""Extract features from an audio file.

Expand Down Expand Up @@ -399,12 +402,12 @@ def process_file(

def process_files(
self,
files: typing.Sequence[str],
files: Sequence[str],
*,
starts: Timestamps = None,
ends: Timestamps = None,
root: str = None,
process_func_args: typing.Dict[str, typing.Any] = None,
starts: Timestamps | None = None,
ends: Timestamps | None = None,
root: str | None = None,
process_func_args: dict[str, object] | None = None,
) -> pd.DataFrame:
r"""Extract features for a list of files.

Expand Down Expand Up @@ -449,7 +452,7 @@ def process_folder(
*,
filetype: str = "wav",
include_root: bool = True,
process_func_args: typing.Dict[str, typing.Any] = None,
process_func_args: dict[str, object] | None = None,
) -> pd.DataFrame:
r"""Extract features from files in a folder.

Expand Down Expand Up @@ -500,9 +503,9 @@ def process_index(
index: pd.Index,
*,
preserve_index: bool = False,
root: str = None,
cache_root: str = None,
process_func_args: typing.Dict[str, typing.Any] = None,
root: str | None = None,
cache_root: str | None = None,
process_func_args: dict[str, object] | None = None,
) -> pd.DataFrame:
r"""Extract features from an index conform to audformat_.

Expand Down Expand Up @@ -572,10 +575,10 @@ def process_signal(
signal: np.ndarray,
sampling_rate: int,
*,
file: str = None,
start: Timestamp = None,
end: Timestamp = None,
process_func_args: typing.Dict[str, typing.Any] = None,
file: str | None = None,
start: Timestamp | None = None,
end: Timestamp | None = None,
process_func_args: dict[str, object] | None = None,
) -> pd.DataFrame:
r"""Extract features for an audio signal.

Expand Down Expand Up @@ -627,7 +630,7 @@ def process_signal_from_index(
signal: np.ndarray,
sampling_rate: int,
index: pd.MultiIndex,
process_func_args: typing.Dict[str, typing.Any] = None,
process_func_args: dict[str, object] | None = None,
) -> pd.DataFrame:
r"""Split a signal into segments and extract features for each segment.

Expand Down Expand Up @@ -676,7 +679,7 @@ def to_numpy(
"""
return frame.values.T.reshape(self.num_channels, self.num_features, -1)

def _reshape_3d(self, features: typing.Union[np.ndarray, pd.Series]):
def _reshape_3d(self, features: np.ndarray | pd.Series):
r"""Reshape to [n_channels, n_features, n_frames]."""
features = np.array(features)
features = np.atleast_1d(features)
Expand Down
115 changes: 59 additions & 56 deletions audinterface/core/process.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

from collections.abc import Callable
from collections.abc import Sequence
import errno
import inspect
import itertools
import os
import typing

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -155,20 +158,20 @@ class Process:
def __init__(
self,
*,
process_func: typing.Callable[..., typing.Any] = None,
process_func_args: typing.Dict[str, typing.Any] = None,
process_func: Callable[..., object] | None = None,
process_func_args: dict[str, object] | None = None,
process_func_is_mono: bool = False,
sampling_rate: int = None,
sampling_rate: int | None = None,
resample: bool = False,
channels: typing.Union[int, typing.Sequence[int]] = None,
channels: int | Sequence[int] | None = None,
mixdown: bool = False,
win_dur: Timestamp = None,
hop_dur: Timestamp = None,
min_signal_dur: Timestamp = None,
max_signal_dur: Timestamp = None,
segment: Segment = None,
win_dur: Timestamp | None = None,
hop_dur: Timestamp | None = None,
min_signal_dur: Timestamp | None = None,
max_signal_dur: Timestamp | None = None,
segment: Segment | None = None,
keep_nat: bool = False,
num_workers: typing.Optional[int] = 1,
num_workers: int | None = 1,
multiprocessing: bool = False,
verbose: bool = False,
):
Expand Down Expand Up @@ -241,15 +244,15 @@ def _process_file(
file: str,
*,
idx: int = 0,
root: str = None,
start: pd.Timedelta = None,
end: pd.Timedelta = None,
process_func_args: typing.Dict[str, typing.Any] = None,
) -> typing.Tuple[
typing.List[typing.Any],
typing.List[str],
typing.List[pd.Timedelta],
typing.List[pd.Timedelta],
root: str | None = None,
start: pd.Timedelta | None = None,
end: pd.Timedelta | None = None,
process_func_args: dict[str, object] | None = None,
) -> tuple[
list[object],
list[str],
list[pd.Timedelta],
list[pd.Timedelta],
]:
if start is not None:
start = utils.to_timedelta(start, self.sampling_rate)
Expand Down Expand Up @@ -300,10 +303,10 @@ def process_file(
self,
file: str,
*,
start: Timestamp = None,
end: Timestamp = None,
root: str = None,
process_func_args: typing.Dict[str, typing.Any] = None,
start: Timestamp | None = None,
end: Timestamp | None = None,
root: str | None = None,
process_func_args: dict[str, object] | None = None,
) -> pd.Series:
r"""Process the content of an audio file.

Expand Down Expand Up @@ -358,12 +361,12 @@ def process_file(

def process_files(
self,
files: typing.Sequence[str],
files: Sequence[str],
*,
starts: Timestamps = None,
ends: Timestamps = None,
root: str = None,
process_func_args: typing.Dict[str, typing.Any] = None,
starts: Timestamps | None = None,
ends: Timestamps | None = None,
root: str | None = None,
process_func_args: dict[str, object] | None = None,
) -> pd.Series:
r"""Process a list of files.

Expand Down Expand Up @@ -460,7 +463,7 @@ def process_folder(
*,
filetype: str = "wav",
include_root: bool = True,
process_func_args: typing.Dict[str, typing.Any] = None,
process_func_args: dict[str, object] | None = None,
) -> pd.Series:
r"""Process files in a folder.

Expand Down Expand Up @@ -512,8 +515,8 @@ def process_folder(
def _process_index_wo_segment(
self,
index: pd.Index,
root: typing.Optional[str],
process_func_args: typing.Dict[str, typing.Any] = None,
root: str | None,
process_func_args: dict[str, object] | None = None,
) -> pd.Series:
r"""Like process_index, but does not apply segmentation."""
if index.empty:
Expand Down Expand Up @@ -558,9 +561,9 @@ def process_index(
index: pd.Index,
*,
preserve_index: bool = False,
root: str = None,
cache_root: str = None,
process_func_args: typing.Dict[str, typing.Any] = None,
root: str | None = None,
cache_root: str | None = None,
process_func_args: dict[str, object] | None = None,
) -> pd.Series:
r"""Process from an index conform to audformat_.

Expand Down Expand Up @@ -638,16 +641,16 @@ def _process_signal(
sampling_rate: int,
*,
idx: int = 0,
root: str = None,
file: str = None,
start: pd.Timedelta = None,
end: pd.Timedelta = None,
process_func_args: typing.Dict[str, typing.Any] = None,
) -> typing.Tuple[
typing.List[typing.Any],
typing.List[str],
typing.List[pd.Timedelta],
typing.List[pd.Timedelta],
root: str | None = None,
file: str | None = None,
start: pd.Timedelta | None = None,
end: pd.Timedelta | None = None,
process_func_args: dict[str, object] | None = None,
) -> tuple[
list[object],
list[str],
list[pd.Timedelta],
list[pd.Timedelta],
]:
signal = np.atleast_2d(signal)

Expand Down Expand Up @@ -718,10 +721,10 @@ def process_signal(
signal: np.ndarray,
sampling_rate: int,
*,
file: str = None,
start: Timestamp = None,
end: Timestamp = None,
process_func_args: typing.Dict[str, typing.Any] = None,
file: str | None = None,
start: Timestamp | None = None,
end: Timestamp | None = None,
process_func_args: dict[str, object] | None = None,
) -> pd.Series:
r"""Process audio signal and return result.

Expand Down Expand Up @@ -799,7 +802,7 @@ def _process_signal_from_index_wo_segment(
signal: np.ndarray,
sampling_rate: int,
index: pd.Index,
process_func_args: typing.Dict[str, typing.Any] = None,
process_func_args: dict[str, object] | None = None,
) -> pd.Series:
r"""Like process_signal_from_index, but does not apply segmentation."""
if index.empty:
Expand Down Expand Up @@ -865,7 +868,7 @@ def process_signal_from_index(
signal: np.ndarray,
sampling_rate: int,
index: pd.Index,
process_func_args: typing.Dict[str, typing.Any] = None,
process_func_args: dict[str, object] | None = None,
) -> pd.Series:
r"""Split a signal into segments and process each segment.

Expand Down Expand Up @@ -919,10 +922,10 @@ def _call(
sampling_rate: int,
*,
idx: int = 0,
root: str = None,
file: str = None,
process_func_args: typing.Dict[str, typing.Any] = None,
) -> typing.Any:
root: str | None = None,
file: str | None = None,
process_func_args: dict[str, object] | None = None,
) -> object:
Comment thread
hagenw marked this conversation as resolved.
r"""Call processing function, possibly pass special args."""
signal, sampling_rate = utils.preprocess_signal(
signal,
Expand Down Expand Up @@ -980,7 +983,7 @@ def __call__(
self,
signal: np.ndarray,
sampling_rate: int,
) -> typing.Any:
) -> object:
r"""Apply processing to signal.

This function processes the signal **without** transforming the output
Expand Down
Loading
Loading