annotate tests/test_load_features.py @ 6:a35bd818d8e9 branch-tests

notebook to test music segments
author Maria Panteli <m.x.panteli@gmail.com>
date Mon, 11 Sep 2017 14:22:17 +0100
parents 230a0cf17de0
children 0f3eba42b425
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