diff --git a/batdetect2/api.py b/batdetect2/api.py index da2d106..86bf55b 100644 --- a/batdetect2/api.py +++ b/batdetect2/api.py @@ -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. diff --git a/batdetect2/types.py b/batdetect2/types.py index 57a60b4..3f22862 100644 --- a/batdetect2/types.py +++ b/batdetect2/types.py @@ -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.""" diff --git a/batdetect2/utils/audio_utils.py b/batdetect2/utils/audio_utils.py index a2cfe44..b89cdca 100644 --- a/batdetect2/utils/audio_utils.py +++ b/batdetect2/utils/audio_utils.py @@ -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, diff --git a/batdetect2/utils/detector_utils.py b/batdetect2/utils/detector_utils.py index 93155b1..f96c5d7 100644 --- a/batdetect2/utils/detector_utils.py +++ b/batdetect2/utils/detector_utils.py @@ -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.