annotate 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
rev   line source
m@2 1 # -*- coding: utf-8 -*-
m@2 2 """
m@2 3 Created on Fri Sep 1 19:11:52 2017
m@2 4
m@2 5 @author: mariapanteli
m@2 6 """
m@2 7
m@2 8 import pytest
m@2 9
m@2 10 import numpy as np
m@2 11
Maria@3 12 import scripts.load_features as load_features
m@2 13
m@2 14 feat_loader = load_features.FeatureLoader(win2sec=8)
m@2 15
Maria@3 16
m@2 17 def test_get_music_idx_from_bounds():
m@6 18 bounds = np.array([['0', '10.5', 'm']])
Maria@3 19 sr = feat_loader.framessr2
Maria@3 20 music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
Maria@3 21 # upper bound minus half window size
Maria@3 22 half_win_sec = 4.0 # assume 8-second window
Maria@3 23 music_bounds_true = np.arange(np.round(sr * (np.float(bounds[-1, 1]) - half_win_sec)), dtype=int)
Maria@3 24 assert np.array_equal(music_bounds, music_bounds_true)
Maria@3 25
Maria@3 26
Maria@3 27 def test_get_music_idx_from_bounds_short_segment():
Maria@3 28 # anything less than half window size is not processed
Maria@3 29 bounds = np.array([['0', '3.8', 'm']])
m@6 30 sr = feat_loader.framessr2
Maria@3 31 music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
Maria@3 32 music_bounds_true = np.array([])
Maria@3 33 assert np.array_equal(music_bounds, music_bounds_true)
Maria@3 34
Maria@3 35
m@6 36 def test_get_music_idx_from_bounds_single_frame():
m@6 37 bounds = np.array([['0', '4.3', 'm']])
m@6 38 sr = feat_loader.framessr2
m@6 39 music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
m@6 40 music_bounds_true = np.array([0])
m@6 41 assert np.array_equal(music_bounds, music_bounds_true)
m@6 42
m@6 43
Maria@3 44 def test_get_music_idx_from_bounds_mix_segments():
m@2 45 bounds = np.array([['0', '10.5', 'm'],
Maria@3 46 ['10.5', '3.0', 's'],
Maria@3 47 ['13.5', '5.0', 'm']])
m@2 48 sr = feat_loader.framessr2
m@2 49 music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
m@6 50 half_win_sec = 4.0 # assume 8-second window
m@6 51 music_bounds_true = np.concatenate([np.arange(np.round(sr * (10.5 - half_win_sec)), dtype=int),
m@6 52 np.arange(np.round(sr * (13.5 - half_win_sec)),
m@6 53 np.round(sr * (18.5 - half_win_sec)), dtype=int)])
m@2 54 assert np.array_equal(music_bounds, music_bounds_true)
m@6 55
m@6 56
m@6 57 def test_get_music_idx_from_bounds_overlap_segments():
m@6 58 bounds = np.array([['0', '10.5', 'm'],
m@6 59 ['9.5', '3.0', 's'],
m@6 60 ['11.5', '5.0', 'm']])
m@6 61 sr = feat_loader.framessr2
m@6 62 music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
m@6 63 half_win_sec = 4.0 # assume 8-second window
m@6 64 music_bounds_true = np.concatenate([np.arange(np.round(sr * (10.5 - half_win_sec)), dtype=int),
m@6 65 np.arange(np.round(sr * (11.5 - half_win_sec)),
m@6 66 np.round(sr * (16.5 - half_win_sec)), dtype=int)])
m@6 67 assert np.array_equal(music_bounds, music_bounds_true)
m@6 68
m@6 69
m@8 70 def test_average_local_frames():
m@8 71 frames = np.array([[0, 0.5, 1], [1, 1, 1]])
m@8 72 aveframes = feat_loader.average_local_frames(frames)
m@8 73 aveframes_true = np.array([[0.5], [1]])
m@8 74 assert np.array_equal(aveframes, aveframes_true)
m@8 75
m@8 76
m@8 77 def test_average_local_frames_multiple_frames():
m@8 78 frames1 = np.concatenate([np.repeat(0.5, feat_loader.hop2), np.repeat(0, feat_loader.win2)])
m@8 79 frames2 = np.concatenate([np.repeat(1.5, feat_loader.hop2), np.repeat(1, feat_loader.win2)])
m@8 80 frames = np.vstack([frames1, frames2])
m@8 81 aveframes = feat_loader.average_local_frames(frames)
m@8 82 aveframes_true = np.array([[0.5, 0], [1.5, 1]])
m@8 83 # test only the second frame which contains values 0 or values 1 for all 8-second frame entries
m@8 84 assert np.array_equal(aveframes[:, 1], aveframes_true[:, 1])
m@8 85
m@8 86
m@8 87 def test_average_local_frames_std():
m@8 88 frames1 = np.concatenate([np.repeat(0.5, feat_loader.hop2), np.repeat(0, feat_loader.win2)])
m@8 89 frames2 = np.concatenate([np.repeat(1.5, feat_loader.hop2), np.repeat(1, feat_loader.win2)])
m@8 90 frames = np.vstack([frames1, frames2])
m@8 91 aveframes = feat_loader.average_local_frames(frames, getstd=True)
m@8 92 aveframes_true = np.array([[0.5, 0], [1.5, 1], [0.1, 0], [0.1, 0]])
m@8 93 # test only the second frame which contains values 0 or values 1 for all 8-second frame entries
m@8 94 assert np.array_equal(aveframes[:, 1], aveframes_true[:, 1])