annotate tests/test_load_features.py @ 34:115774aff442 branch-tests

music bounds not centered but aligned with start of segment to match the way features are computed
author Maria Panteli
date Thu, 14 Sep 2017 10:16:59 +0100
parents 0f3eba42b425
children c4428589b82b
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@34 22 #half_win_sec = 4.0 # assume 8-second window
Maria@34 23 win_sec = 8
Maria@34 24 music_bounds_true = np.arange(np.round(sr * (np.float(bounds[-1, 1]) - win_sec)), dtype=int)
Maria@3 25 assert np.array_equal(music_bounds, music_bounds_true)
Maria@3 26
Maria@3 27
Maria@3 28 def test_get_music_idx_from_bounds_short_segment():
Maria@3 29 # anything less than half window size is not processed
Maria@34 30 bounds = np.array([['0', '7.9', 'm']])
m@6 31 sr = feat_loader.framessr2
Maria@3 32 music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
Maria@3 33 music_bounds_true = np.array([])
Maria@3 34 assert np.array_equal(music_bounds, music_bounds_true)
Maria@3 35
Maria@3 36
m@6 37 def test_get_music_idx_from_bounds_single_frame():
Maria@34 38 bounds = np.array([['0', '8.1', 'm']])
Maria@34 39 sr = feat_loader.framessr2
m@6 40 music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
m@6 41 music_bounds_true = np.array([0])
m@6 42 assert np.array_equal(music_bounds, music_bounds_true)
m@6 43
m@6 44
Maria@3 45 def test_get_music_idx_from_bounds_mix_segments():
m@2 46 bounds = np.array([['0', '10.5', 'm'],
Maria@3 47 ['10.5', '3.0', 's'],
Maria@3 48 ['13.5', '5.0', 'm']])
m@2 49 sr = feat_loader.framessr2
m@2 50 music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
Maria@34 51 #half_win_sec = 4.0 # assume 8-second window
Maria@34 52 win_sec = 8.0 # assume 8-second window
Maria@34 53 music_bounds_true = np.concatenate([np.arange(np.round(sr * (10.5 - win_sec)), dtype=int),
Maria@34 54 np.arange(np.round(sr * 13.5),
Maria@34 55 np.round(sr * (18.5 - win_sec)), dtype=int)])
m@2 56 assert np.array_equal(music_bounds, music_bounds_true)
m@6 57
m@6 58
m@6 59 def test_get_music_idx_from_bounds_overlap_segments():
m@6 60 bounds = np.array([['0', '10.5', 'm'],
m@6 61 ['9.5', '3.0', 's'],
m@6 62 ['11.5', '5.0', 'm']])
m@6 63 sr = feat_loader.framessr2
m@6 64 music_bounds = feat_loader.get_music_idx_from_bounds(bounds, sr=sr)
m@6 65 half_win_sec = 4.0 # assume 8-second window
Maria@34 66 win_sec = 8.0 # assume 8-second window
Maria@34 67 music_bounds_true = np.concatenate([np.arange(np.round(sr * (10.5 - win_sec)), dtype=int),
Maria@34 68 np.arange(np.round(sr * 11.5),
Maria@34 69 np.round(sr * (16.5 - win_sec)), dtype=int)])
m@6 70 assert np.array_equal(music_bounds, music_bounds_true)
m@6 71
m@6 72
m@8 73 def test_average_local_frames():
m@8 74 frames = np.array([[0, 0.5, 1], [1, 1, 1]])
m@8 75 aveframes = feat_loader.average_local_frames(frames)
m@8 76 aveframes_true = np.array([[0.5], [1]])
m@8 77 assert np.array_equal(aveframes, aveframes_true)
m@8 78
m@8 79
m@8 80 def test_average_local_frames_multiple_frames():
m@8 81 frames1 = np.concatenate([np.repeat(0.5, feat_loader.hop2), np.repeat(0, feat_loader.win2)])
m@8 82 frames2 = np.concatenate([np.repeat(1.5, feat_loader.hop2), np.repeat(1, feat_loader.win2)])
m@8 83 frames = np.vstack([frames1, frames2])
m@8 84 aveframes = feat_loader.average_local_frames(frames)
m@8 85 aveframes_true = np.array([[0.5, 0], [1.5, 1]])
m@8 86 # test only the second frame which contains values 0 or values 1 for all 8-second frame entries
m@8 87 assert np.array_equal(aveframes[:, 1], aveframes_true[:, 1])
m@8 88
m@8 89
m@8 90 def test_average_local_frames_std():
m@8 91 frames1 = np.concatenate([np.repeat(0.5, feat_loader.hop2), np.repeat(0, feat_loader.win2)])
m@8 92 frames2 = np.concatenate([np.repeat(1.5, feat_loader.hop2), np.repeat(1, feat_loader.win2)])
m@8 93 frames = np.vstack([frames1, frames2])
m@8 94 aveframes = feat_loader.average_local_frames(frames, getstd=True)
m@8 95 aveframes_true = np.array([[0.5, 0], [1.5, 1], [0.1, 0], [0.1, 0]])
m@8 96 # test only the second frame which contains values 0 or values 1 for all 8-second frame entries
m@8 97 assert np.array_equal(aveframes[:, 1], aveframes_true[:, 1])