<commit_before>#!/usr/bin/env python
# CREATED:2013-03-11 18:14:30 by <NAME> <<EMAIL>>
#  unit tests for librosa.beat

from __future__ import print_function
from nose.tools import nottest, raises, eq_

# Disable cache
import os
try:
    os.environ.pop('LIBROSA_CACHE_DIR')
except:
    pass

import matplotlib
matplotlib.use('Agg')
import numpy as np
import librosa

from test_core import files, load

__EXAMPLE_FILE = 'data/test1_22050.wav'


def test_onset_strength():

    def __test(infile):
        DATA = load(infile)

        # Compute onset envelope using the same spectrogram
        onsets = librosa.onset.onset_strength(y=None,
                                              sr=8000,
                                              S=DATA['D'],
                                              lag=1,
                                              max_size=1,
                                              center=False,
                                              detrend=True,
                                              aggregate=np.mean)

        assert np.allclose(onsets[1:], DATA['onsetenv'][0])

        pass

    for infile in files('data/beat-onset-*.mat'):
        yield (__test, infile)


def test_estimate_tempo():

    def __test(infile):
        DATA = load(infile)

        # Estimate tempo from the given onset envelope
        tempo = librosa.beat.estimate_tempo(DATA['onsetenv'][0],
                                            sr=8000,
                                            hop_length=32,
                                            start_bpm=120.0)

        assert (np.allclose(tempo, DATA['t'][0, 0]) or
                np.allclose(tempo, DATA['t'][0, 1])), (tempo, DATA['t'])

    for infile in files('data/beat-tempo-*.mat'):
        yield (__test, infile)


def test_tempo():

    def __test(tempo, sr, hop_length, ac_size, aggregate, y):

        tempo_est = librosa.beat.tempo(y=y, sr=sr, hop_length=hop_length,
                                       ac_size=ac_size,
                                       aggregate=aggregate)

        # Being within 10% for the stable frames is close enough
        if aggregate is None:
            win_size = int(ac_size * sr // hop_length)
            assert np.all(np.abs(tempo_est[win_size:-win_size] - tempo) <= 0.10 * tempo), (tempo,
                    tempo_est[win_size:-win_size])
        else:
            assert np.abs(tempo_est - tempo) <= 0.10 * tempo, (tempo, tempo_est)

    for sr in [22050, 44100]:
        for tempo in [40, 60, 80, 110, 150, 160]:
            # Make a pulse train at the target tempo
            y = np.zeros(20 * sr)
            delay = np.asscalar(librosa.time_to_samples(60./tempo, sr=sr))
            y[::delay] = 1
            for hop_length in [512, 1024]:
                for ac_size in [4, 8]:
                    for aggregate in [None, np.mean]:
                        yield __test, tempo, sr, hop_length, ac_size, aggregate, y<commit_msg>hardening tempo tests, dropped the 150bpm case for now<commit_after>#!/usr/bin/env python
# CREATED:2013-03-11 18:14:30 by <NAME> <<EMAIL>>
#  unit tests for librosa.beat

from __future__ import print_function
from nose.tools import nottest, raises, eq_

# Disable cache
import os
try:
    os.environ.pop('LIBROSA_CACHE_DIR')
except:
    pass

import matplotlib
matplotlib.use('Agg')
import numpy as np
import librosa

from test_core import files, load

__EXAMPLE_FILE = 'data/test1_22050.wav'


def test_onset_strength():

    def __test(infile):
        DATA = load(infile)

        # Compute onset envelope using the same spectrogram
        onsets = librosa.onset.onset_strength(y=None,
                                              sr=8000,
                                              S=DATA['D'],
                                              lag=1,
                                              max_size=1,
                                              center=False,
                                              detrend=True,
                                              aggregate=np.mean)

        assert np.allclose(onsets[1:], DATA['onsetenv'][0])

        pass

    for infile in files('data/beat-onset-*.mat'):
        yield (__test, infile)


def test_estimate_tempo():

    def __test(infile):
        DATA = load(infile)

        # Estimate tempo from the given onset envelope
        tempo = librosa.beat.estimate_tempo(DATA['onsetenv'][0],
                                            sr=8000,
                                            hop_length=32,
                                            start_bpm=120.0)

        assert (np.allclose(tempo, DATA['t'][0, 0]) or
                np.allclose(tempo, DATA['t'][0, 1])), (tempo, DATA['t'])

    for infile in files('data/beat-tempo-*.mat'):
        yield (__test, infile)


def test_tempo():

    def __test(tempo, sr, hop_length, ac_size, aggregate, y):

        tempo_est = librosa.beat.tempo(y=y, sr=sr, hop_length=hop_length,
                                       ac_size=ac_size,
                                       aggregate=aggregate)

        # Being within 5% for the stable frames is close enough
        if aggregate is None:
            win_size = int(ac_size * sr // hop_length)
            assert np.all(np.abs(tempo_est[win_size:-win_size] - tempo) <= 0.05 * tempo), (tempo,
                    tempo_est[win_size:-win_size])
        else:
            assert np.abs(tempo_est - tempo) <= 0.05 * tempo, (tempo, tempo_est)

    for sr in [22050, 44100]:
        for tempo in [40, 60, 80, 110, 160]:
            # Make a pulse train at the target tempo
            y = np.zeros(20 * sr)
            delay = np.asscalar(librosa.time_to_samples(60./tempo, sr=sr))
            y[::delay] = 1
            for hop_length in [512, 1024]:
                for ac_size in [4, 8]:
                    for aggregate in [None, np.mean]:
                        yield __test, tempo, sr, hop_length, ac_size, aggregate, y
{'fraction_non_alphanumeric': 0.10139331878462313, 'fraction_numerical': 0.030048682222595265, 'mean_word_length': 1.4825, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7014483', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1680, 'n_words': 434}