<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 --with-coverage --cover-package=librosa
#

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

import librosa
import glob
import numpy as np
import scipy.io
import six

import matplotlib
matplotlib.use('Agg')
from nose.tools import nottest, eq_, raises


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


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


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],
                             sr=None,
                             mono=DATA['mono'])

        # Verify that the sample rate is correct
        eq_(sr, DATA['sr'])

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

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

def test_segment_load():
    """
    Test loading a segment. Check size accuracy
    """

    sample_len = 2003
    fs = 44100
    test_file = 'data/test1_44100.wav'
    y, sr = librosa.load(test_file, sr=None, mono=False, offset=0., duration=sample_len/float(fs))
    assert y.shape[1] == sample_len

    y2, sr = librosa.load(test_file, sr=None, mono=False)
    assert np.max(np.abs(y-y2[:,:sample_len]))<1e-4

    sample_offset = 2048
    y, sr = librosa.load(test_file, sr=None, mono=False, offset=sample_offset/float(fs), duration=1.0)
    assert y.shape[1] == fs

    y2, sr = librosa.load(test_file, sr=None, mono=False)
    assert np.max(np.abs(y-y2[:,sample_offset:sample_offset+fs]))<1e-4


def test_resample():

    def __test(y, sr_in, sr_out, res_type, fix):

        y2 = librosa.resample(y, sr_in, sr_out,
                              res_type=res_type,
                              fix=fix)

        # First, check that the audio is valid
        librosa.util.valid_audio(y2, mono=True)

        # If it's a no-op, make sure the signal is untouched
        if sr_out == sr_in:
            assert np.allclose(y, y2)

        # Check buffer contiguity
        assert y2.flags['C_CONTIGUOUS']

        # Check that we're within one sample of the target length
        target_length = len(y) * sr_out // sr_in
        assert np.abs(len(y2) - target_length) <= 1

    for infile in ['data/test1_44100.wav',
                   'data/test1_22050.wav',
                   'data/test2_8000.wav']:<commit_msg>changed segment_load unit tests to use np.allclose<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 --with-coverage --cover-package=librosa
#

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

import librosa
import glob
import numpy as np
import scipy.io
import six

import matplotlib
matplotlib.use('Agg')
from nose.tools import nottest, eq_, raises


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


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


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],
                             sr=None,
                             mono=DATA['mono'])

        # Verify that the sample rate is correct
        eq_(sr, DATA['sr'])

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

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

def test_segment_load():

    sample_len = 2003
    fs = 44100
    test_file = 'data/test1_44100.wav'
    y, sr = librosa.load(test_file, sr=None, mono=False,
                         offset=0., duration=sample_len/float(fs))

    eq_(y.shape[-1], sample_len)

    y2, sr = librosa.load(test_file, sr=None, mono=False)
    assert np.allclose(y, y2[:, :sample_len])

    sample_offset = 2048
    y, sr = librosa.load(test_file, sr=None, mono=False,
                         offset=sample_offset/float(fs), duration=1.0)

    eq_(y.shape[-1], fs)

    y2, sr = librosa.load(test_file, sr=None, mono=False)
    assert np.allclose(y, y2[:, sample_offset:sample_offset+fs])


def test_resample():

    def __test(y, sr_in, sr_out, res_type, fix):

        y2 = librosa.resample(y, sr_in, sr_out,
                              res_type=res_type,
                              fix=fix)

        # First, check that the audio is valid
        librosa.util.valid_audio(y2, mono=True)

        # If it's a no-op, make sure the signal is untouched
        if sr_out == sr_in:
            assert np.allclose(y, y2)

        # Check buffer contiguity
        assert y2.flags['C_CONTIGUOUS']

        # Check that we're within one sample of the target length
        target_length = len(y) * sr_out // sr_in
        assert np.abs(len(y2) - target_length) <= 1

    for infile in ['data/test1_44100.wav',
                   'data/test1_22050.wav',
                   'data/test2_8000.wav']:
{'fraction_non_alphanumeric': 0.11898224418817499, 'fraction_numerical': 0.024894746476295075, 'mean_word_length': 2.41927409261577, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7417670', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1871, 'n_words': 462}