UXInteract / task_031 /tests /validate_outputs.py
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
2.1 kB
"""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')