"""Unit tests for evaluation metrics (timing, pitch, pitch class, octave offset, WER).""" import pytest from unmix.eval.metrics import ( evaluate_vocals, extract_text_from_reference, extract_text_from_vocals, generate_training_diff, normalize_text_for_wer, ) from unmix.export.ultrastar import UltraStarNote, UltraStarSong from unmix.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_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": "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), ] ], ) 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, word_end=True), ], ) ] ) 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": "120", "GAP": "1000"}, lines=[ [ # 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 70ms, has octave error (-1 octave: 48.2 vs 60.0), duration 0.6s pred_doc = VocalsDocument( lines=[ VocalLine( start=1.07, end=1.67, syllables=[ 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(70.0, abs=1.0) assert metrics["fraction_within_50ms"] == pytest.approx(0.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(100.0, abs=1.0) # Strict pitch should fail (48 != 60) assert metrics["strict_pitch_accuracy"] == pytest.approx(0.0) # 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) def test_generate_training_diff(): ref_song = UltraStarSong( headers={"BPM": "120", "GAP": "1000"}, lines=[ [ UltraStarNote(":", 0, 4, 0, "First ", start_sec=1.0, end_sec=1.5, midi=60.0), UltraStarNote(":", 4, 4, 2, "Second ", start_sec=1.5, end_sec=2.0, midi=62.0), ] ], ) pred_doc = VocalsDocument( lines=[ VocalLine( start=1.02, end=1.52, syllables=[ VocalSyllable(text="First", start=1.02, end=1.52, midi=60.0, word_end=True), VocalSyllable(text="Extra", start=5.0, end=5.5, midi=65.0, word_end=True), ], ) ] ) diff = generate_training_diff(pred_doc, ref_song) assert "summary" in diff assert "systematic_biases" in diff assert "matched_notes" in diff assert "unmatched_reference_notes" in diff assert "unmatched_predictions" in diff # Matched note checks assert len(diff["matched_notes"]) == 1 m0 = diff["matched_notes"][0] assert m0["ref_text"] == "First" assert m0["pred_text"] == "First" assert m0["timing_error_ms"] == pytest.approx(20.0, abs=1.0) assert m0["pitch_error_semitones"] == pytest.approx(0.0) # Unmatched reference note: "Second" was missed by AI assert len(diff["unmatched_reference_notes"]) == 1 assert diff["unmatched_reference_notes"][0]["text"] == "Second" # Unmatched prediction: "Extra" was hallucinated / extra assert len(diff["unmatched_predictions"]) == 1 assert diff["unmatched_predictions"][0]["text"] == "Extra" # Systematic biases biases = diff["systematic_biases"] assert biases["matched_notes_count"] == 1 assert biases["unmatched_ref_notes_count"] == 1 assert biases["unmatched_predictions_count"] == 1 assert biases["mean_timing_bias_ms"] == pytest.approx(20.0, abs=1.0)