Added AudioPath as an alias for the path definition

This commit is contained in:
Kavi 2025-05-16 15:13:08 +02:00
parent 52570738f2
commit 4282e2ae70
4 changed files with 21 additions and 21 deletions

View File

@ -99,6 +99,7 @@ consult the API documentation in the code.
import warnings
from typing import List, Optional, Tuple, BinaryIO, Any, Union
from .types import AudioPath
import numpy as np
import torch
@ -244,9 +245,7 @@ def generate_spectrogram(
def process_file(
path: Union[
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
],
path: AudioPath,
model: DetectionModel = MODEL,
config: Optional[ProcessingConfiguration] = None,
device: torch.device = DEVICE,
@ -256,9 +255,7 @@ def process_file(
Parameters
----------
path : Union[
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
]
path : AudioPath
Path to audio data.
model : DetectionModel, optional
Detection model. Uses default model if not specified.

View File

@ -1,6 +1,10 @@
"""Types used in the code base."""
from typing import List, NamedTuple, Optional, Union
from typing import List, NamedTuple, Optional, Union, Any, BinaryIO
import audioread
import os
import soundfile as sf
import numpy as np
import torch
@ -40,6 +44,9 @@ __all__ = [
"SpectrogramParameters",
]
AudioPath = Union[
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
]
class SpectrogramParameters(TypedDict):
"""Parameters for generating spectrograms."""

View File

@ -1,6 +1,8 @@
import warnings
from typing import Optional, Tuple, Union, Any, BinaryIO
from ..types import AudioPath
import librosa
import librosa.core.spectrum
import numpy as np
@ -146,9 +148,7 @@ def generate_spectrogram(
return spec, spec_for_viz
def load_audio(
path: Union[
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
],
path: AudioPath,
time_exp_fact: float,
target_samp_rate: int,
scale: bool = False,
@ -177,9 +177,7 @@ def load_audio(
return sample_rate, audio_data
def load_audio_and_samplerate(
path: Union[
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
],
path: AudioPath,
time_exp_fact: float,
target_samp_rate: int,
scale: bool = False,

View File

@ -2,6 +2,8 @@ import json
import os
from typing import Any, Iterator, List, Optional, Tuple, Union, BinaryIO
from ..types import AudioPath
import numpy as np
import pandas as pd
import torch
@ -735,9 +737,7 @@ def process_audio_array(
def process_file(
path: Union[
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
],
path: AudioPath,
model: DetectionModel,
config: ProcessingConfiguration,
device: torch.device,
@ -750,7 +750,7 @@ def process_file(
Parameters
----------
path : str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
path : AudioPath
Path to audio file.
model : torch.nn.Module
@ -760,7 +760,7 @@ def process_file(
Configuration for processing.
file_id: Optional[str],
Give the data an id. Defaults to the filename if path is a string. Otherwise
Give the data an id. Defaults to the filename if path is a string. Otherwise an md5 will be calculated from the binary data.
Returns
-------
@ -859,9 +859,7 @@ def process_file(
return results
def _generate_id(path: Union[
str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO
]) -> str:
def _generate_id(path: AudioPath) -> str:
""" Generate an id based on the path.
If the path is a str or PathLike it will parsed as the basename.