Skip to content

Commit

Permalink
pyflakes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Lostanlen committed Nov 21, 2018
1 parent ddf9df8 commit 1615f09
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
23 changes: 12 additions & 11 deletions birdvoxdetect/cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import print_function
import os
import sys
from birdvoxdetect.birdvoxdetect_exceptions import BirdVoxDetectError
import birdvoxdetect
from argparse import ArgumentParser, RawDescriptionHelpFormatter,\
ArgumentTypeError
from collections import Iterable
import os
from six import string_types
import sys

import birdvoxdetect
from birdvoxdetect.birdvoxdetect_exceptions import BirdVoxDetectError


def positive_float(value):
Expand Down Expand Up @@ -83,7 +84,7 @@ def parse_args(args):

parser.add_argument('--export-clips', '-c', action='store_true',
help='Export detected events as audio clips in WAV format.')

parser.add_argument('--export-likelihood', '-l', action='store_true',
help='Export the time series of event likelihood of events in HDF5 format.')

Expand All @@ -108,24 +109,24 @@ def parse_args(args):

parser.add_argument('--quiet', '-q', action='store_true',
help='Suppress all non-error messages to stdout.')

parser.add_argument('--verbose', '-v', action='store_true',
help='Print timestamps of detected events.')

args = parser.parse_args(args)

if args.quiet and args.verbose:
raise BirdVoxDetectError(
'Command-line flags --quiet (-q) and --verbose (-v) '
'are mutually exclusive.')

if args.clip_duration is None:
args.clip_duration = 1.0
elif not args.export_clips:
raise BirdVoxDetectError(
'The --export-clips (-c) flag should be present '
'if the --clip-duration (-d) flag is present.')

return args


Expand All @@ -135,11 +136,11 @@ def main():
deep learning model (Lostanlen et al. 2019).
"""
args = parse_args(sys.argv[1:])

if args.inputs[0] == "-V":
print(birdvoxdetect.version.version)
return


run(args.inputs,
output_dir=args.output_dir,
Expand Down
42 changes: 24 additions & 18 deletions birdvoxdetect/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import os
import scipy.signal
import soundfile as sf
import traceback

from birdvoxdetect.birdvoxdetect_exceptions import BirdVoxDetectError


def pad_audio(audio, padding_length):
Expand All @@ -20,59 +23,62 @@ def process_file(filepath,
frame_rate=20.0,
clip_duration=1.0,
logger_level=20):

# Check for existence of the file.
if not os.path.exists(filepath):
raise BirdVoxDetectError('File "{}" could not be found.'.format(filepath))

raise BirdVoxDetectError(
'File "{}" could not be found.'.format(filepath))

# Try loading the file as NumPy array.
try:
audio, sr = sf.read(filepath)
except Exception:
raise OpenL3Error('Could not open file "{}":\n{}'.format(filepath, traceback.format_exc()))

raise BirdVoxDetectError(
'Could not open file "{}":\n{}'.format(filepath,
traceback.format_exc()))

# Compute likelihood curve.
likelihood = get_likelihood(audio, sr, frame_rate=frame_rate)

# Find peaks.

# Export timestamps.
timestamps_path = get_output_path(
filepath, suffix + "_timestamps.csv", output_dir=output_dir)

# Export likelihood curve.
if export_likelihood:
likelihood_path = get_output_path(
filepath, suffix + "_likelihood.hdf5", output_dir=output_dir)

# Export clips.
if export_clips:
clips_dir = get_output_path(
filepath, suffix + "_clips", output_dir=output_dir)


def get_likelihood(audio, sr, frame_rate):
# Load settings.
pcen_settings = get_pcen_settings()

# Check audio array dimension
if audio.ndim > 2:
raise OpenL3Error('Audio array can only be be 1D or 2D')
elif audio.ndim == 2:
# Downmix if multichannel
audio = np.mean(audio, axis=1)

# Resample to 22,050 kHz
if not sr == pcen_settings["sr"]:
audio = librosa.resample(audio, sr, pcen_settings["sr"])

# Pad.
padding_length = int(np.round(0.5 * sr / frame_rate))
audio = pad_audio(audio, padding_length)

# Load settings.
pcen_settings = get_pcen_settings()


# Compute Short-Term Fourier Transform (STFT).
stft = librosa.stft(
chunk_waveform,
audio,
n_fft=pcen_settings["n_fft"],
win_length=pcen_settings["win_length"],
hop_length=pcen_settings["hop_length"],
Expand Down Expand Up @@ -114,10 +120,10 @@ def get_likelihood(audio, sr, frame_rate):
audio_duration = len(audio) / pcen_settings["sr"]
likelihood_x = np.arange(0.0, audio_duration, 1.0/frame_rate)
likelihood_y = median_likelihood[likelihood_x]

# Return.
return likelihood_y


def get_output_path(filepath, suffix, output_dir=None):
"""
Expand Down

0 comments on commit 1615f09

Please sign in to comment.