<commit_before>#!/usr/bin/env python
#
# Run me as follows:
# cd tests/
# nosetests

import glob
import json
import librosa
from nose.tools import nottest, eq_, raises, assert_equals
import numpy.testing as npt
import os

# Msaf imports
import msaf.featextract
from msaf.input_output import FileStruct

# Global vars
audio_file = os.path.join("fixtures", "chirp.mp3")
sr = msaf.Anal.sample_rate
audio, fs = librosa.load(audio_file, sr=sr)
y_harmonic, y_percussive = librosa.effects.hpss(audio)


def test_compute_beats():
    beats_idx, beats_times = msaf.featextract.compute_beats(y_percussive)
    assert len(beats_idx) > 0
    assert len(beats_idx) == len(beats_times)


def test_compute_features():
    mfcc, hpcp, tonnetz, cqt = msaf.featextract.compute_features(audio, y_harmonic)
    assert mfcc.shape[1] == msaf.Anal.mfcc_coeff
    assert hpcp.shape[1] == 12
    assert tonnetz.shape[1] == 6
    assert_equals(mfcc.shape[0], hpcp.shape[0])
    assert_equals(hpcp.shape[0], tonnetz.shape[0])


def test_save_features():
    # Read audio and compute features
    tmp_file = "temp.json"
    features = msaf.featextract.compute_features_for_audio_file(audio_file)
    msaf.featextract.save_features(tmp_file, features)

    # Check that new file exists
    assert os.path.isfile(tmp_file)

    # Check that the json file is actually readable
    with open(tmp_file, "r") as f:
        features = json.load(f)
    npt.assert_almost_equal(features["analysis"]["dur"], len(audio) / float(sr),
                            decimal=1)

    # Clean up
    os.remove(tmp_file)


def test_compute_beat_sync_features():
    # Compute features
    features = msaf.featextract.compute_features_for_audio_file(audio_file)

    # Beat track
    beats_idx, beats_times = msaf.featextract.compute_beats(y_percussive,
                                                            sr=sr)

    # Compute beat sync feats
    bs_mfcc, bs_hpcp, bs_tonnetz, bs_cqt = \
        msaf.featextract.compute_beat_sync_features(features, beats_idx)
    assert_equals(bs_mfcc.shape[0],  len(beats_idx) - 1)
    assert_equals(bs_mfcc.shape[0], bs_hpcp.shape[0])
    assert_equals(bs_hpcp.shape[0], bs_tonnetz.shape[0])


def test_compute_features_for_audio_file():
    features = msaf.featextract.compute_features_for_audio_file(audio_file)
    keys = ["mfcc", "hpcp", "tonnetz", "cqt", "beats_idx", "beats", "bs_mfcc",
            "bs_hpcp", "bs_tonnetz", "bs_cqt", "anal"]<commit_msg>Fixed unit test: now we get same number of beat-sync feats than beats<commit_after>#!/usr/bin/env python
#
# Run me as follows:
# cd tests/
# nosetests

import glob
import json
import librosa
from nose.tools import nottest, eq_, raises, assert_equals
import numpy.testing as npt
import os

# Msaf imports
import msaf.featextract
from msaf.input_output import FileStruct

# Global vars
audio_file = os.path.join("fixtures", "chirp.mp3")
sr = msaf.Anal.sample_rate
audio, fs = librosa.load(audio_file, sr=sr)
y_harmonic, y_percussive = librosa.effects.hpss(audio)


def test_compute_beats():
    beats_idx, beats_times = msaf.featextract.compute_beats(y_percussive)
    assert len(beats_idx) > 0
    assert len(beats_idx) == len(beats_times)


def test_compute_features():
    mfcc, hpcp, tonnetz, cqt = msaf.featextract.compute_features(audio, y_harmonic)
    assert mfcc.shape[1] == msaf.Anal.mfcc_coeff
    assert hpcp.shape[1] == 12
    assert tonnetz.shape[1] == 6
    assert_equals(mfcc.shape[0], hpcp.shape[0])
    assert_equals(hpcp.shape[0], tonnetz.shape[0])


def test_save_features():
    # Read audio and compute features
    tmp_file = "temp.json"
    features = msaf.featextract.compute_features_for_audio_file(audio_file)
    msaf.featextract.save_features(tmp_file, features)

    # Check that new file exists
    assert os.path.isfile(tmp_file)

    # Check that the json file is actually readable
    with open(tmp_file, "r") as f:
        features = json.load(f)
    npt.assert_almost_equal(features["analysis"]["dur"], len(audio) / float(sr),
                            decimal=1)

    # Clean up
    os.remove(tmp_file)


def test_compute_beat_sync_features():
    # Compute features
    features = msaf.featextract.compute_features_for_audio_file(audio_file)

    # Beat track
    beats_idx, beats_times = msaf.featextract.compute_beats(y_percussive,
                                                            sr=sr)

    # Compute beat sync feats
    bs_mfcc, bs_hpcp, bs_tonnetz, bs_cqt = \
        msaf.featextract.compute_beat_sync_features(features, beats_idx)
    assert_equals(bs_mfcc.shape[0],  len(beats_idx))
    assert_equals(bs_mfcc.shape[0], bs_hpcp.shape[0])
    assert_equals(bs_hpcp.shape[0], bs_tonnetz.shape[0])


def test_compute_features_for_audio_file():
    features = msaf.featextract.compute_features_for_audio_file(audio_file)
    keys = ["mfcc", "hpcp", "tonnetz", "cqt", "beats_idx", "beats", "bs_mfcc",
            "bs_hpcp", "bs_tonnetz", "bs_cqt", "anal"]
{'fraction_non_alphanumeric': 0.10938759065269943, 'fraction_numerical': 0.007453666398066076, 'mean_word_length': 3.751196172248804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6312765', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1799, 'n_words': 348}