File size: 2,097 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Check the public stdout format and label-preserving duplication."""
import ast
import math
import pathlib
import re
import shutil
import subprocess
import sys
import tempfile

root = pathlib.Path.cwd()
audio_root = root / 'Audio_Speech_Actors_01-24'


def run_candidate():
    result = subprocess.run([sys.executable, 'analyze_ravdess.py'], cwd=root,
                            check=True, capture_output=True, text=True, timeout=30)
    lines = result.stdout.strip().splitlines()
    assert len(lines) == 4, 'print exactly four requested lines'
    assert re.fullmatch(r'neutral_count: \d+', lines[0])
    assert re.fullmatch(r'happy_count: \d+', lines[1])
    assert re.fullmatch(r'mfcc_diff: \[\d+\.\d{3}(?:,\s*\d+\.\d{3}){12}\]', lines[2]), '13 nonnegative three-decimal differences required'
    assert re.fullmatch(r'max_diff_dim: \d+', lines[3])
    counts = tuple(int(line.split(': ')[1]) for line in lines[:2])
    values = ast.literal_eval(lines[2].split(': ', 1)[1])
    assert all(math.isfinite(value) for value in values)
    dimension = int(lines[3].split(': ')[1])
    assert dimension == max(range(13), key=lambda index: values[index]) + 1
    return counts, values


files = sorted(audio_root.rglob('*.wav'))
expected = tuple(sum(file.stem.split('-')[2] == label for file in files) for label in ('01', '03'))
assert all(expected), 'fixture needs both target emotions'
counts, _ = run_candidate()
assert counts == expected, 'counts disagree with filename emotion fields'
# Preserve the original seven-field filename; adding a prefix would move the
# emotion field and test a different label than the intended copied sample.
source = next(file for file in files if file.stem.split('-')[2] == '01')
with tempfile.TemporaryDirectory(prefix='duplicate-', dir=audio_root) as directory:
    shutil.copyfile(source, pathlib.Path(directory) / source.name)
    changed, _ = run_candidate()
    assert changed == (expected[0] + 1, expected[1]), 'recursive neutral duplication must only increment neutral_count'
print('Counts, formatting, argmax and label-preserving duplication passed')