view tests/test_load_features.py @ 8:0f3eba42b425 branch-tests

added notebooks and utils
author Maria Panteli <m.x.panteli@gmail.com>
date Mon, 11 Sep 2017 18:23:14 +0100
parents a35bd818d8e9
children 115774aff442
line wrap: on
line source
# -*- coding: utf-8 -*-
"""
Created on Fri Sep  1 19:11:52 2017

@author: mariapanteli
"""

import pytest

import numpy as np

import scripts.load_features as load_features

feat_loader = load_features.FeatureLoader(win2sec=8)


def test_get_music_idx_from_bounds():
    bounds = np.array([['0', '10.5', 'm']])
    sr = feat_loader.framessr2            
    music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
    # upper bound minus half window size
    half_win_sec = 4.0  # assume 8-second window
    music_bounds_true = np.arange(np.round(sr * (np.float(bounds[-1, 1]) - half_win_sec)), dtype=int)
    assert np.array_equal(music_bounds, music_bounds_true)
    
    
def test_get_music_idx_from_bounds_short_segment():
    # anything less than half window size is not processed
    bounds = np.array([['0', '3.8', 'm']])
    sr = feat_loader.framessr2
    music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
    music_bounds_true = np.array([])
    assert np.array_equal(music_bounds, music_bounds_true)


def test_get_music_idx_from_bounds_single_frame():
    bounds = np.array([['0', '4.3', 'm']])
    sr = feat_loader.framessr2         
    music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
    music_bounds_true = np.array([0])
    assert np.array_equal(music_bounds, music_bounds_true)


def test_get_music_idx_from_bounds_mix_segments():
    bounds = np.array([['0', '10.5', 'm'], 
              ['10.5', '3.0', 's'],
              ['13.5', '5.0', 'm']])
    sr = feat_loader.framessr2
    music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
    half_win_sec = 4.0  # assume 8-second window
    music_bounds_true = np.concatenate([np.arange(np.round(sr * (10.5 - half_win_sec)), dtype=int),
                                        np.arange(np.round(sr * (13.5 - half_win_sec)), 
                                            np.round(sr * (18.5 - half_win_sec)), dtype=int)])
    assert np.array_equal(music_bounds, music_bounds_true)
    

def test_get_music_idx_from_bounds_overlap_segments():
    bounds = np.array([['0', '10.5', 'm'], 
              ['9.5', '3.0', 's'],
              ['11.5', '5.0', 'm']])
    sr = feat_loader.framessr2
    music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
    half_win_sec = 4.0  # assume 8-second window
    music_bounds_true = np.concatenate([np.arange(np.round(sr * (10.5 - half_win_sec)), dtype=int),
                                        np.arange(np.round(sr * (11.5 - half_win_sec)), 
                                            np.round(sr * (16.5 - half_win_sec)), dtype=int)])
    assert np.array_equal(music_bounds, music_bounds_true)


def test_average_local_frames():
    frames = np.array([[0, 0.5, 1], [1, 1, 1]])
    aveframes = feat_loader.average_local_frames(frames)
    aveframes_true = np.array([[0.5], [1]])
    assert np.array_equal(aveframes, aveframes_true)


def test_average_local_frames_multiple_frames():
    frames1 = np.concatenate([np.repeat(0.5, feat_loader.hop2), np.repeat(0, feat_loader.win2)])
    frames2 = np.concatenate([np.repeat(1.5, feat_loader.hop2), np.repeat(1, feat_loader.win2)])
    frames = np.vstack([frames1, frames2])
    aveframes = feat_loader.average_local_frames(frames)
    aveframes_true = np.array([[0.5, 0], [1.5, 1]])
    # test only the second frame which contains values 0 or values 1 for all 8-second frame entries
    assert np.array_equal(aveframes[:, 1], aveframes_true[:, 1])


def test_average_local_frames_std():
    frames1 = np.concatenate([np.repeat(0.5, feat_loader.hop2), np.repeat(0, feat_loader.win2)])
    frames2 = np.concatenate([np.repeat(1.5, feat_loader.hop2), np.repeat(1, feat_loader.win2)])
    frames = np.vstack([frames1, frames2])
    aveframes = feat_loader.average_local_frames(frames, getstd=True)
    aveframes_true = np.array([[0.5, 0], [1.5, 1], [0.1, 0], [0.1, 0]])
    # test only the second frame which contains values 0 or values 1 for all 8-second frame entries
    assert np.array_equal(aveframes[:, 1], aveframes_true[:, 1])