fix(eval): correct UltraStar tick duration, word boundary WER, and pitch metrics

This commit is contained in:
2026-11-03 20:37:04 +01:00
parent 58ff3fe59a
commit cb3047ddae
3 changed files with 204 additions and 39 deletions

View File

@ -1,8 +1,13 @@
"""Unit tests for evaluation metrics (timing, pitch, WER)."""
"""Unit tests for evaluation metrics (timing, pitch, pitch class, octave offset, WER)."""
import pytest
from unmix.eval.metrics import evaluate_vocals, normalize_text_for_wer
from unmix.eval.metrics import (
evaluate_vocals,
extract_text_from_reference,
extract_text_from_vocals,
normalize_text_for_wer,
)
from unmix.export.ultrastar import UltraStarNote, UltraStarSong
from unmix.schema.vocals import VocalLine, VocalsDocument, VocalSyllable
@ -13,14 +18,50 @@ def test_normalize_text_for_wer():
assert norm == "hallo welt wie gehts"
def test_extract_words_and_wer_computation():
# UltraStar notes format: word boundaries are marked by space after word
ref_song = UltraStarSong(
headers={"BPM": "120", "GAP": "1000"},
lines=[
[
UltraStarNote(":", 0, 4, 0, "We ", start_sec=1.0, end_sec=1.5, midi=60.0),
UltraStarNote(":", 4, 4, 2, "need ", start_sec=1.5, end_sec=2.0, midi=62.0),
UltraStarNote(":", 8, 4, 4, "love", start_sec=2.0, end_sec=2.5, midi=64.0),
]
],
)
pred_doc = VocalsDocument(
lines=[
VocalLine(
start=1.0,
end=2.5,
syllables=[
VocalSyllable(text="We", start=1.0, end=1.5, midi=60.0, word_end=True),
VocalSyllable(text="need", start=1.5, end=2.0, midi=62.0, word_end=True),
VocalSyllable(text="love", start=2.0, end=2.5, midi=64.0, word_end=True),
],
)
]
)
ref_text = extract_text_from_reference(ref_song)
pred_text = extract_text_from_vocals(pred_doc)
assert ref_text == "We need love"
assert pred_text == "We need love"
metrics = evaluate_vocals(pred_doc, ref_song)
assert metrics["wer"] == 0.0
def test_evaluate_vocals_perfect_match():
# Ground truth: 2 syllables
ref_song = UltraStarSong(
headers={"BPM": "480", "GAP": "1000"},
headers={"BPM": "120", "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),
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),
]
],
)
@ -31,8 +72,8 @@ def test_evaluate_vocals_perfect_match():
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),
VocalSyllable(text="Hal-", start=1.0, end=1.5, midi=60.0),
VocalSyllable(text="lo ", start=1.5, end=2.0, midi=62.0, word_end=True),
],
)
]
@ -41,41 +82,55 @@ def test_evaluate_vocals_perfect_match():
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_50ms"] == pytest.approx(1.0)
assert metrics["fraction_within_100ms"] == pytest.approx(1.0)
assert metrics["fraction_within_200ms"] == pytest.approx(1.0)
assert metrics["duration_mae_ms"] == pytest.approx(0.0)
assert metrics["strict_pitch_accuracy"] == pytest.approx(1.0)
assert metrics["pitch_class_accuracy"] == pytest.approx(1.0)
assert metrics["octave_equiv_pitch_accuracy"] == pytest.approx(1.0)
assert metrics["median_octave_offset"] == pytest.approx(0.0)
assert metrics["line_start_mae_ms"] == pytest.approx(0.0)
assert metrics["line_end_mae_ms"] == pytest.approx(0.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"},
headers={"BPM": "120", "GAP": "1000"},
lines=[
[
# Note at 60.0, start 1.0
# Note at 60.0 (C4), start 1.0, duration 0.5s
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)
# Prediction is offset by 70ms, has octave error (-1 octave: 48.2 vs 60.0), duration 0.6s
pred_doc = VocalsDocument(
lines=[
VocalLine(
start=1.05,
end=1.55,
start=1.07,
end=1.67,
syllables=[
VocalSyllable(text="Test", start=1.05, end=1.55, midi=72.0),
VocalSyllable(text="Test", start=1.07, end=1.67, midi=48.2, word_end=True),
],
)
]
)
metrics = evaluate_vocals(pred_doc, ref_song)
assert metrics["mae_timing_ms"] == pytest.approx(50.0, abs=1.0)
assert metrics["mae_timing_ms"] == pytest.approx(70.0, abs=1.0)
assert metrics["fraction_within_50ms"] == pytest.approx(0.0)
assert metrics["fraction_within_100ms"] == pytest.approx(1.0)
# Strict pitch should fail (72 != 60)
assert metrics["fraction_within_200ms"] == pytest.approx(1.0)
assert metrics["duration_mae_ms"] == pytest.approx(100.0, abs=1.0)
# Strict pitch should fail (48 != 60)
assert metrics["strict_pitch_accuracy"] == pytest.approx(0.0)
# Octave equivalent pitch should succeed (72 % 12 == 60 % 12)
# Pitch class should succeed (48 % 12 == 60 % 12 == 0)
assert metrics["pitch_class_accuracy"] == pytest.approx(1.0)
assert metrics["octave_equiv_pitch_accuracy"] == pytest.approx(1.0)
# Median octave offset should be -1.0
assert metrics["median_octave_offset"] == pytest.approx(-1.0)
assert metrics["wer"] == pytest.approx(0.0)