vak.common.annotation.audio_filename_from_path#

vak.common.annotation.audio_filename_from_path(path: str | Path, audio_ext: str | None = None) str[source]#

Find the name of an audio file within a filename by removing extensions until finding an audio extension, then return the name of that audio file without the extension (i.e., the “stem”).

Removes extensions from a filename recursively, by calling os.path.splitext, until the extension is an audio file format handled by vak. Then return the stem, that is, the part that precedes the extension. Used to match audio, spectrogram, and annotation files by their stems.

Stops after finding audio extensions so that it does not remove “extensions” that are actually other parts of a filename, e.g. a time or data separated by periods.

Examples

>>> audio_filename_from_path('gy6or6_baseline_230312_0808.138.cbin.not.mat')
'gy6or6_baseline_230312_0808.138'
>>> audio_filename_from_path('Bird0/spectrograms/0.wav.npz')
'0'
>>> audio_filename_from_path('Bird0/Wave/0.wav')
'0'
Parameters:
  • path (str, Path) – Path to a file that contains an audio filename in its name.

  • audio_ext (str) – Extension corresponding to format of audio file. Must be one of vak.constants.VALID_AUDIO_FORMATS. Default is None, in which case the function looks removes extensions until it finds any valid audio format extension.

Returns:

stem – Part of filename that precedes audio extension.

Return type:

str