<commit_before>#!/usr/bin/env python
# CREATED:2013-03-08 15:25:18 by <NAME> <<EMAIL>>
#  unit tests for librosa core (__init__.py)
#
# Run me as follows:
#   cd tests/
#   nosetests -v

import librosa
import os, glob
import numpy, scipy.io

from nose.tools import nottest

#-- utilities --#
def files(pattern):
    test_files = glob.glob(pattern)
    test_files.sort()
    return test_files

def load(infile):
    DATA = scipy.io.loadmat(infile, chars_as_strings=True)
    return DATA
#--           --#

#-- Tests     --#
def test_hz_to_mel():
    def __test_to_mel(infile):
        DATA    = load(infile)
        z       = librosa.hz_to_mel(DATA['f'], DATA['htk'])

        assert numpy.allclose(z, DATA['result'])
    
    for infile in files('data/hz_to_mel-*.mat'):
        yield (__test_to_mel, infile)

    pass

def test_mel_to_hz():

    def __test_to_hz(infile):
        DATA    = load(infile)
        z       = librosa.mel_to_hz(DATA['f'], DATA['htk'])

        assert numpy.allclose(z, DATA['result'])
    
    for infile in files('data/mel_to_hz-*.mat'):
        yield (__test_to_hz, infile)

    pass

def test_hz_to_octs():
    def __test_to_octs(infile):
        DATA    = load(infile)
        z       = librosa.hz_to_octs(DATA['f'])

        assert numpy.allclose(z, DATA['result'])

    for infile in files('data/hz_to_octs-*.mat'):
        yield (__test_to_octs, infile)

    pass

def test_load():
    # Note: this does not test resampling.
    # That is a separate unit test.

    def __test(infile):
        DATA    = load(infile)
        (y, sr) = librosa.load(DATA['wavfile'][0], target_sr=None, mono=DATA['mono'])

        # Verify that the sample rate is correct
        assert sr == DATA['sr']

        assert numpy.allclose(y, DATA['y'])

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

@nottest
def test_resample():

    def __test(infile):
        DATA    = load(infile)
        
        # load the wav file
        (y_in, sr_in) = librosa.load(DATA['wavfile'][0], target_sr=None, mono=True)

        # Resample it to the target rate
        y_out = librosa.resample(y_in, DATA['sr_in'], DATA['sr_out'])

        # Are we the same length?
        if len(y_out) == len(DATA['y_out']):
            # Is the data close?
            assert numpy.allclose(y_out, DATA['y_out'])
        elif len(y_out) == len(DATA['y_out']) - 1:
            assert (numpy.allclose(y_out, DATA['y_out'][:-1,0]) or
                    numpy.allclose(y_out, DATA['y_out'][1:,0]))
        elif len(y_out) == len(DATA['y_out']) + 1:
            assert (numpy.allclose(y_out[1:], DATA['y_out']) or
                    numpy.allclose(y_out[:-2], DATA['y_out']))
        else:
            assert False
        pass


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

def test_stft():

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

        # Load the file
        (y, sr) = librosa.load(DATA['wavfile'][0], target_sr=None, mono=True)

        # Compute the STFT
        D       = librosa.stft(y, sr,   n_fft       =   DATA['nfft'][0].astype(int),
                                        hann_w      =   DATA['hann_w'][0].astype(int),
                                        hop_length  =   DATA['hop_length'][0].astype(int))

        assert  numpy.allclose(D, DATA['D'])   


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

def test_istft():
    def __test(infile):
        DATA    = load(infile)
<commit_msg>added documentation to core test suite<commit_after>#!/usr/bin/env python
# CREATED:2013-03-08 15:25:18 by <NAME> <<EMAIL>>
#  unit tests for librosa core (__init__.py)
#
# Run me as follows:
#   cd tests/
#   nosetests -v
#
# This test suite verifies that librosa core routines match (numerically) the output
# of various DPWE matlab implementations on a broad range of input parameters.
#
# All test data is generated by the Matlab script "makeTestData.m".
# Each test loads in a .mat file which contains the input and desired output for a given
# function.  The test then runs the librosa implementation and verifies the results
# against the desired output, typically via numpy.allclose().
#
# CAVEATS:
#   The DPWE versions of STFT/ISTFT use a custom Hann window implementation, which does
#   not quite numerically match either the Matlab or scipy.signal implementations.
#   These tests should pass if [i]stft.m are modified to use Matlab's builtin hann().
#
#   Currently, not all tests are exhaustive in parameter space.  This is typically due
#   restricted functionality of the librosa implementations.  Similarly, there is no
#   fuzz-testing here, so behavior on invalid inputs is not yet well-defined.
#

import librosa
import os, glob
import numpy, scipy.io

from nose.tools import nottest

#-- utilities --#
def files(pattern):
    test_files = glob.glob(pattern)
    test_files.sort()
    return test_files

def load(infile):
    DATA = scipy.io.loadmat(infile, chars_as_strings=True)
    return DATA
#--           --#

#-- Tests     --#
def test_hz_to_mel():
    def __test_to_mel(infile):
        DATA    = load(infile)
        z       = librosa.hz_to_mel(DATA['f'], DATA['htk'])

        assert numpy.allclose(z, DATA['result'])
    
    for infile in files('data/hz_to_mel-*.mat'):
        yield (__test_to_mel, infile)

    pass

def test_mel_to_hz():

    def __test_to_hz(infile):
        DATA    = load(infile)
        z       = librosa.mel_to_hz(DATA['f'], DATA['htk'])

        assert numpy.allclose(z, DATA['result'])
    
    for infile in files('data/mel_to_hz-*.mat'):
        yield (__test_to_hz, infile)

    pass

def test_hz_to_octs():
    def __test_to_octs(infile):
        DATA    = load(infile)
        z       = librosa.hz_to_octs(DATA['f'])

        assert numpy.allclose(z, DATA['result'])

    for infile in files('data/hz_to_octs-*.mat'):
        yield (__test_to_octs, infile)

    pass

def test_load():
    # Note: this does not test resampling.
    # That is a separate unit test.

    def __test(infile):
        DATA    = load(infile)
        (y, sr) = librosa.load(DATA['wavfile'][0], target_sr=None, mono=DATA['mono'])

        # Verify that the sample rate is correct
        assert sr == DATA['sr']

        assert numpy.allclose(y, DATA['y'])

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

@nottest
def test_resample():

    def __test(infile):
        DATA    = load(infile)
        
        # load the wav file
        (y_in, sr_in) = librosa.load(DATA['wavfile'][0], target_sr=None, mono=True)

        # Resample it to the target rate
        y_out = librosa.resample(y_in, DATA['sr_in'], DATA['sr_out'])

        # Are we the same length?
        if len(y_out) == len(DATA['y_out']):
            # Is the data close?
            assert numpy.allclose(y_out, DATA['y_out'])
        elif len(y_out) == len(DATA['y_out']) - 1:
            assert (numpy.allclose(y_out, DATA['y_out'][:-1,0]) or
                    numpy.allclose(y_out, DATA['y_out'][1:,0]))
        elif len(y_out) == len(DATA['y_out']) + 1:
            assert (numpy.allclose(y_out[1:], DATA['y_out']) or
                    numpy.allclose(y_out[:-2], DATA['y_out']))
        else:
            assert False
        pass

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

def test_stft():

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

        # Load the file
        (y, sr) = librosa.load(DATA['wavfile'][0], target_sr=None, mono=True)

        # Compute the STFT
        D       = librosa.stft(y, sr,   n_fft       =   DATA['nfft'][0].astype(int),
                                        hann_w      =   DATA['hann_w'][0].astype(int),
                                        hop_length  =   DATA['hop_length'][0].astype(int))

        assert  numpy.allclose(D, DATA['D'])   


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

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

{'fraction_non_alphanumeric': 0.13166687291176835, 'fraction_numerical': 0.00692983541640886, 'mean_word_length': 2.3341584158415842, 'pattern_counts': {'":': 0, '<': 9, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6781698', 'n_tokens_mistral': 2846, 'n_tokens_neox': 2760, 'n_words': 682}