comparison tests/test_PitchBihist.py @ 36:3b67cd634b9a branch-tests

tests for pitch bihist, before refactoring
author Maria Panteli
date Thu, 14 Sep 2017 14:16:29 +0100
parents
children 2cc444441f42
comparison
equal deleted inserted replaced
35:c4428589b82b 36:3b67cd634b9a
1 # -*- coding: utf-8 -*-
2 """
3 Created on Fri Sep 1 19:11:52 2017
4
5 @author: mariapanteli
6 """
7
8 import pytest
9
10 import numpy as np
11
12 import scripts.PitchBihist as PitchBihist
13
14
15 pbi = PitchBihist.PitchBihist()
16
17
18 def test_hz_to_cents():
19 freq_Hz = np.array([32.703, 65.406, 55, 110])
20 freq_cents = pbi.hz_to_cents(freq_Hz)
21 freq_cents_true = np.array([0, 1200, 900, 2100])
22 assert np.array_equal(freq_cents, freq_cents_true)
23
24
25 def test_wrap_to_octave():
26 cents = np.array([900, 2100, 1200])
27 octave_cents = pbi.wrap_to_octave(cents)
28 octave_cents_true = np.array([900, 900, 0])
29 assert np.array_equal(octave_cents, octave_cents_true)
30
31
32 def test_get_melody_from_file():
33 melodia_file = 'data/sample_dataset/Melodia/mel_1_2_1.csv'
34 melody = pbi.get_melody_from_file(melodia_file)
35 assert len(melody) < 12. * pbi.chromasr
36
37
38 def test_get_melody_matrix():
39 melody = 440 * np.ones(1000)
40 melody_matrix = pbi.get_melody_matrix(melody)
41 n_frames = melody_matrix.shape[1]
42 assert np.array_equal(melody_matrix[45, :], np.ones(n_frames))
43
44
45 def test_bihist_from_melodia():
46 melodia_file = 'data/sample_dataset/Melodia/mel_1_2_1.csv'
47 bihist = pbi.bihist_from_melodia(melodia_file, secondframedecomp=False)
48 assert bihist.shape == (60, 60)
49
50
51 def test_bihist_from_melodia_n_frames():
52 melodia_file = 'data/sample_dataset/Melodia/mel_1_2_1.csv'
53 bihist = pbi.bihist_from_melodia(melodia_file, secondframedecomp=True)
54 dur_sec = 11.5 # duration of first file in metadata.csv is > 11 seconds
55 n_frames_true = np.round((dur_sec - pbi.win2sec) * 2) # for .5 sec hop size
56 assert bihist.shape[1] == n_frames_true
57
58
59 def test_bihistogram():
60 melody = 440 * np.ones(1000)
61 melody_matrix = pbi.get_melody_matrix(melody)
62 bihist = pbi.bihistogram(melody_matrix, align=False)
63 assert np.array_equal(bihist, np.zeros((60, 60)))
64
65
66 def test_bihistogram_values():
67 melody = np.concatenate([440 * np.ones(500), 32.703 * np.ones(500)])
68 melody_matrix = pbi.get_melody_matrix(melody)
69 # melody transitions from A to C (bin 45/60 to bin 0/60)
70 bihist = pbi.bihistogram(melody_matrix, align=False)
71 # expect only element [45, 0] to be non-zero
72 assert bihist[45, 0] > 0 and (np.sum(bihist) - bihist[45, 0]) == 0
73
74
75