feat(eval): implement evaluation metrics (timing MAE, pitch accuracy, WER) and runner
This commit is contained in:
81
tests/test_metrics.py
Normal file
81
tests/test_metrics.py
Normal file
@ -0,0 +1,81 @@
|
||||
"""Unit tests for evaluation metrics (timing, pitch, WER)."""
|
||||
|
||||
import pytest
|
||||
|
||||
from songsplit.eval.metrics import evaluate_vocals, normalize_text_for_wer
|
||||
from songsplit.export.ultrastar import UltraStarNote, UltraStarSong
|
||||
from songsplit.schema.vocals import VocalLine, VocalsDocument, VocalSyllable
|
||||
|
||||
|
||||
def test_normalize_text_for_wer():
|
||||
raw = "Hallo, Welt! Wie geht's?"
|
||||
norm = normalize_text_for_wer(raw)
|
||||
assert norm == "hallo welt wie gehts"
|
||||
|
||||
|
||||
def test_evaluate_vocals_perfect_match():
|
||||
# Ground truth: 2 syllables
|
||||
ref_song = UltraStarSong(
|
||||
headers={"BPM": "480", "GAP": "1000"},
|
||||
lines=[
|
||||
[
|
||||
UltraStarNote(":", 0, 4, 0, "Hal", start_sec=1.0, end_sec=1.5, midi=60.0),
|
||||
UltraStarNote(":", 4, 4, 2, "lo", start_sec=1.5, end_sec=2.0, midi=62.0),
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
pred_doc = VocalsDocument(
|
||||
lines=[
|
||||
VocalLine(
|
||||
start=1.0,
|
||||
end=2.0,
|
||||
syllables=[
|
||||
VocalSyllable(text="Hal", start=1.0, end=1.5, midi=60.0),
|
||||
VocalSyllable(text="lo", start=1.5, end=2.0, midi=62.0),
|
||||
],
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
metrics = evaluate_vocals(pred_doc, ref_song)
|
||||
assert metrics["mae_timing_ms"] == pytest.approx(0.0)
|
||||
assert metrics["median_timing_ms"] == pytest.approx(0.0)
|
||||
assert metrics["fraction_within_100ms"] == pytest.approx(1.0)
|
||||
assert metrics["strict_pitch_accuracy"] == pytest.approx(1.0)
|
||||
assert metrics["octave_equiv_pitch_accuracy"] == pytest.approx(1.0)
|
||||
assert metrics["wer"] == pytest.approx(0.0)
|
||||
|
||||
|
||||
def test_evaluate_vocals_octave_error_and_timing_offset():
|
||||
ref_song = UltraStarSong(
|
||||
headers={"BPM": "480", "GAP": "1000"},
|
||||
lines=[
|
||||
[
|
||||
# Note at 60.0, start 1.0
|
||||
UltraStarNote(":", 0, 4, 0, "Test", start_sec=1.0, end_sec=1.5, midi=60.0),
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
# Prediction is offset by 50ms and has octave error (72.0 vs 60.0)
|
||||
pred_doc = VocalsDocument(
|
||||
lines=[
|
||||
VocalLine(
|
||||
start=1.05,
|
||||
end=1.55,
|
||||
syllables=[
|
||||
VocalSyllable(text="Test", start=1.05, end=1.55, midi=72.0),
|
||||
],
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
metrics = evaluate_vocals(pred_doc, ref_song)
|
||||
assert metrics["mae_timing_ms"] == pytest.approx(50.0, abs=1.0)
|
||||
assert metrics["fraction_within_100ms"] == pytest.approx(1.0)
|
||||
# Strict pitch should fail (72 != 60)
|
||||
assert metrics["strict_pitch_accuracy"] == pytest.approx(0.0)
|
||||
# Octave equivalent pitch should succeed (72 % 12 == 60 % 12)
|
||||
assert metrics["octave_equiv_pitch_accuracy"] == pytest.approx(1.0)
|
||||
assert metrics["wer"] == pytest.approx(0.0)
|
||||
Reference in New Issue
Block a user