<commit_before>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# CREATED:2015-02-14 22:51:01 by <NAME> <<EMAIL>>
'''Unit tests for display module'''


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

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

import librosa
import numpy as np

import seaborn as sns

sns.set(style='white')

from nose.tools import nottest, raises, eq_
from mpl_ic import image_comparison

# MPL 1.5 broke things pretty bad.  We'll skip image tests for now.
from nose.plugins.skip import SkipTest

@nottest
def get_spec(y, sr):

    C = librosa.cqt(y, sr=sr)
    return librosa.stft(y), C, sr


__EXAMPLE_FILE = 'data/test1_22050.wav'
y, sr = librosa.load(__EXAMPLE_FILE)
S, C, sr = get_spec(y, sr)
S_abs = np.abs(S)
S_signed = np.abs(S) - np.median(np.abs(S))
S_bin = S_signed > 0


@image_comparison(baseline_images=['complex'], extensions=['png'])
def test_complex_input():
    plt.figure()
    librosa.display.specshow(S)


@image_comparison(baseline_images=['abs'], extensions=['png'])
def test_abs_input():
    plt.figure()
    librosa.display.specshow(S_abs)


@image_comparison(baseline_images=['cqt_note'], extensions=['png'])
def test_cqt_note():
    plt.figure()
    librosa.display.specshow(C, y_axis='cqt_note')


@image_comparison(baseline_images=['cqt_hz'], extensions=['png'])
def test_cqt_hz():
    plt.figure()
    librosa.display.specshow(C, y_axis='cqt_hz')


@image_comparison(baseline_images=['tempo'], extensions=['png'])
def test_tempo():
    T = librosa.feature.tempogram(y=y, sr=sr)

    plt.figure()
    librosa.display.specshow(T, y_axis='tempo')


@image_comparison(baseline_images=['tonnetz'], extensions=['png'])
def test_tonnetz():
    plt.figure()
    chroma = librosa.feature.chroma_cqt(C=C)
    ton = librosa.feature.tonnetz(chroma=chroma)
    librosa.display.specshow(ton, y_axis='tonnetz')


@image_comparison(baseline_images=['chroma'], extensions=['png'])
def test_chroma():
    plt.figure()
    plt.subplot(3, 1, 1)
    chr1 = librosa.feature.chroma_stft(S=S_abs**2, sr=sr)
    librosa.display.specshow(chr1, y_axis='chroma')

    plt.subplot(3, 1, 2)
    chr2 = librosa.feature.chroma_stft(S=S_abs**2, sr=sr, n_chroma=2*12)
    librosa.display.specshow(chr2, y_axis='chroma', bins_per_octave=2*12)

    plt.subplot(3, 1, 3)
    chr3 = librosa.feature.chroma_stft(S=S_abs**2, sr=sr, n_chroma=3*12)
    librosa.display.specshow(chr3, y_axis='chroma', bins_per_octave=3*12)


@image_comparison(baseline_images=['double_chroma'], extensions=['png'])
def test_double_chroma():
    plt.figure()

    chr1 = librosa.feature.chroma_stft(S=S_abs**2, sr=sr)
    chr1 = np.vstack((chr1, chr1))
    librosa.display.specshow(chr1, y_axis='chroma', bins_per_octave=12)


@image_comparison(baseline_images=['x_mel'], extensions=['png'])
def test_x_mel():
    plt.figure()

    M = librosa.feature.melspectrogram(S=S_abs**2)
    librosa.display.specshow(M, y_axis='mel')


@image_comparison(baseline_images=['y_mel'], extensions=['png'])
def test_y_mel():
    plt.figure()

    M = librosa.feature.melspectrogram(S=S_abs**2)
    librosa.display.specshow(M.T, x_axis='mel')


@image_comparison(baseline_images=['y_mel_bounded'], extensions=['png'])
def test_y_mel_bounded():
    plt.figure()

    fmin, fmax = 110, 880
    M = librosa.feature.melspectrogram(S=S_abs**2, fmin=fmin, fmax=fmax)
    librosa.display.specshow(M, y_axis='mel', fmin=fmin, fmax=fmax)


@image_comparison(baseline_images=['x_none_y_linear'], extensions=['png'])
def test_xaxis_none_yaxis_linear():
    plt.figure()
    plt.subplot(3, 1, 1)
    librosa.display.specshow(S_abs, x_axis='linear')

    plt.subplot(3, 1, 2)
    librosa.display.specshow(S_signed, x_axis='linear')

    plt.subplot(3, 1, 3)
    librosa.display.specshow(S_bin, x_axis='linear')


@image_comparison(baseline_images=['x_none_y_log'], extensions=['png'])
def test_xaxis_none_yaxis_log():
    plt.figure()

    plt.subplot(3, 1, 1)
    librosa.display.specshow(S_abs, y_axis='log')

    plt.subplot(3, 1, 2)
    librosa.display.specshow(S_signed, y_axis='log')

    plt.subplot(3, 1, 3)
    librosa.display.specshow(S_bin, y_axis='log')


@image_comparison(baseline_images=['x_none_y_log_khz'], extensions=['png'])
def test_xaxis_none_yaxis_log_khz():
    plt.figure()
    librosa.display.specshow(S_abs, y_axis='log', freq_fmt='kHz')
    plt.tight_layout()


@image_comparison(baseline_images=['x_none_y_log_mhz'], extensions=['png'])
def test_xaxis_none_yaxis_log_mhz():
    plt.figure()
    librosa.display.specshow(S_abs, y_axis='log', freq_fmt='mHz')
    plt.tight_layout()

@image_comparison(baseline_images=['x_none_y_log_megahz'], extensions=['png'])
def test_xaxis_none_yaxis_log_megahz():
    plt.figure()
    librosa.display.specshow(S_abs, y_axis='log', freq_fmt='MHz')
    plt.tight_layout()


@image_comparison(baseline_images=['x_none_y_log_ghz'], extensions=['png'])
def test_xaxis_none_yaxis_log_ghz():
    plt.figure()
    librosa.display.specshow(S_abs, y_axis='log', freq_fmt='GHz')
    plt.tight_layout()


@image_comparison(baseline_images=['x_linear_y_none'], extensions=['png'])
def test_xaxis_linear_yaxis_none():
    plt.figure()

    plt.subplot(3, 1, 1)
    librosa.display.specshow(S_abs.T, x_axis='linear')

    plt.subplot(3, 1, 2)
    librosa.display.specshow(S_signed.T, x_axis='linear')

    plt.subplot(3, 1, 3)
    librosa.display.specshow(S_bin.T, x_axis='linear')


@image_comparison(baseline_images=['x_log_y_none'], extensions=['png'])
def test_xaxis_log_yaxis_none():

    plt.figure()

    plt.subplot(3, 1, 1)
    librosa.display.specshow(S_abs.T, x_axis='log')

    plt.subplot(3, 1, 2)
    librosa.display.specshow(S_signed.T, x_axis='log')

    plt.subplot(3, 1, 3)
    librosa.display.specshow(S_bin.T, x_axis='log')


@image_comparison(baseline_images=['x_time_y_none'], extensions=['png'])<commit_msg>Added a failure test for bad frequency scaling<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# CREATED:2015-02-14 22:51:01 by <NAME> <<EMAIL>>
'''Unit tests for display module'''


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

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

import librosa
import numpy as np

import seaborn as sns

sns.set(style='white')

from nose.tools import nottest, raises, eq_
from mpl_ic import image_comparison

# MPL 1.5 broke things pretty bad.  We'll skip image tests for now.
from nose.plugins.skip import SkipTest

@nottest
def get_spec(y, sr):

    C = librosa.cqt(y, sr=sr)
    return librosa.stft(y), C, sr


__EXAMPLE_FILE = 'data/test1_22050.wav'
y, sr = librosa.load(__EXAMPLE_FILE)
S, C, sr = get_spec(y, sr)
S_abs = np.abs(S)
S_signed = np.abs(S) - np.median(np.abs(S))
S_bin = S_signed > 0


@image_comparison(baseline_images=['complex'], extensions=['png'])
def test_complex_input():
    plt.figure()
    librosa.display.specshow(S)


@image_comparison(baseline_images=['abs'], extensions=['png'])
def test_abs_input():
    plt.figure()
    librosa.display.specshow(S_abs)


@image_comparison(baseline_images=['cqt_note'], extensions=['png'])
def test_cqt_note():
    plt.figure()
    librosa.display.specshow(C, y_axis='cqt_note')


@image_comparison(baseline_images=['cqt_hz'], extensions=['png'])
def test_cqt_hz():
    plt.figure()
    librosa.display.specshow(C, y_axis='cqt_hz')


@image_comparison(baseline_images=['tempo'], extensions=['png'])
def test_tempo():
    T = librosa.feature.tempogram(y=y, sr=sr)

    plt.figure()
    librosa.display.specshow(T, y_axis='tempo')


@image_comparison(baseline_images=['tonnetz'], extensions=['png'])
def test_tonnetz():
    plt.figure()
    chroma = librosa.feature.chroma_cqt(C=C)
    ton = librosa.feature.tonnetz(chroma=chroma)
    librosa.display.specshow(ton, y_axis='tonnetz')


@image_comparison(baseline_images=['chroma'], extensions=['png'])
def test_chroma():
    plt.figure()
    plt.subplot(3, 1, 1)
    chr1 = librosa.feature.chroma_stft(S=S_abs**2, sr=sr)
    librosa.display.specshow(chr1, y_axis='chroma')

    plt.subplot(3, 1, 2)
    chr2 = librosa.feature.chroma_stft(S=S_abs**2, sr=sr, n_chroma=2*12)
    librosa.display.specshow(chr2, y_axis='chroma', bins_per_octave=2*12)

    plt.subplot(3, 1, 3)
    chr3 = librosa.feature.chroma_stft(S=S_abs**2, sr=sr, n_chroma=3*12)
    librosa.display.specshow(chr3, y_axis='chroma', bins_per_octave=3*12)


@image_comparison(baseline_images=['double_chroma'], extensions=['png'])
def test_double_chroma():
    plt.figure()

    chr1 = librosa.feature.chroma_stft(S=S_abs**2, sr=sr)
    chr1 = np.vstack((chr1, chr1))
    librosa.display.specshow(chr1, y_axis='chroma', bins_per_octave=12)


@image_comparison(baseline_images=['x_mel'], extensions=['png'])
def test_x_mel():
    plt.figure()

    M = librosa.feature.melspectrogram(S=S_abs**2)
    librosa.display.specshow(M, y_axis='mel')


@image_comparison(baseline_images=['y_mel'], extensions=['png'])
def test_y_mel():
    plt.figure()

    M = librosa.feature.melspectrogram(S=S_abs**2)
    librosa.display.specshow(M.T, x_axis='mel')


@image_comparison(baseline_images=['y_mel_bounded'], extensions=['png'])
def test_y_mel_bounded():
    plt.figure()

    fmin, fmax = 110, 880
    M = librosa.feature.melspectrogram(S=S_abs**2, fmin=fmin, fmax=fmax)
    librosa.display.specshow(M, y_axis='mel', fmin=fmin, fmax=fmax)


@image_comparison(baseline_images=['x_none_y_linear'], extensions=['png'])
def test_xaxis_none_yaxis_linear():
    plt.figure()
    plt.subplot(3, 1, 1)
    librosa.display.specshow(S_abs, x_axis='linear')

    plt.subplot(3, 1, 2)
    librosa.display.specshow(S_signed, x_axis='linear')

    plt.subplot(3, 1, 3)
    librosa.display.specshow(S_bin, x_axis='linear')


@image_comparison(baseline_images=['x_none_y_log'], extensions=['png'])
def test_xaxis_none_yaxis_log():
    plt.figure()

    plt.subplot(3, 1, 1)
    librosa.display.specshow(S_abs, y_axis='log')

    plt.subplot(3, 1, 2)
    librosa.display.specshow(S_signed, y_axis='log')

    plt.subplot(3, 1, 3)
    librosa.display.specshow(S_bin, y_axis='log')


@image_comparison(baseline_images=['x_none_y_log_khz'], extensions=['png'])
def test_xaxis_none_yaxis_log_khz():
    plt.figure()
    librosa.display.specshow(S_abs, y_axis='log', freq_fmt='kHz')
    plt.tight_layout()


@image_comparison(baseline_images=['x_none_y_log_mhz'], extensions=['png'])
def test_xaxis_none_yaxis_log_mhz():
    plt.figure()
    librosa.display.specshow(S_abs, y_axis='log', freq_fmt='mHz')
    plt.tight_layout()

@image_comparison(baseline_images=['x_none_y_log_megahz'], extensions=['png'])
def test_xaxis_none_yaxis_log_megahz():
    plt.figure()
    librosa.display.specshow(S_abs, y_axis='log', freq_fmt='MHz')
    plt.tight_layout()


@image_comparison(baseline_images=['x_none_y_log_ghz'], extensions=['png'])
def test_xaxis_none_yaxis_log_ghz():
    plt.figure()
    librosa.display.specshow(S_abs, y_axis='log', freq_fmt='GHz')
    plt.tight_layout()

@raises(librosa.ParameterError)
def test_xaxis_none_yaxis_log_badscale():
    plt.figure()
    librosa.display.specshow(S_abs, y_axis='log', freq_fmt='no-scale')
    plt.tight_layout()

@image_comparison(baseline_images=['x_linear_y_none'], extensions=['png'])
def test_xaxis_linear_yaxis_none():
    plt.figure()

    plt.subplot(3, 1, 1)
    librosa.display.specshow(S_abs.T, x_axis='linear')

    plt.subplot(3, 1, 2)
    librosa.display.specshow(S_signed.T, x_axis='linear')

    plt.subplot(3, 1, 3)
    librosa.display.specshow(S_bin.T, x_axis='linear')


@image_comparison(baseline_images=['x_log_y_none'], extensions=['png'])
def test_xaxis_log_yaxis_none():

    plt.figure()

    plt.subplot(3, 1, 1)
    librosa.display.specshow(S_abs.T, x_axis='log')

    plt.subplot(3, 1, 2)
    librosa.display.specshow(S_signed.T, x_axis='log')

    plt.subplot(3, 1, 3)
    librosa.display.specshow(S_bin.T, x_axis='log')


@image_comparison(baseline_images=['x_time_y_none'], extensions=['png'])
{'fraction_non_alphanumeric': 0.16132780082987552, 'fraction_numerical': 0.017759336099585063, 'mean_word_length': 5.382944915254237, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '2933838', 'n_tokens_mistral': 5170, 'n_tokens_neox': 4801, 'n_words': 700}