Initial commit: 基于旋律的音乐哼唱检索系统 (Query by Humming)
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
"""评测模块测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from qbh.config import DEFAULT
|
||||
from qbh.evaluate import (
|
||||
DIFFICULTY_PRESETS,
|
||||
add_noise,
|
||||
evaluate,
|
||||
make_query_specs,
|
||||
perturb_melody,
|
||||
synth_query_audio,
|
||||
)
|
||||
from qbh.melody import Melody
|
||||
from qbh.search import SearchEngine
|
||||
|
||||
|
||||
def _melody(song_id="m", n=24) -> Melody:
|
||||
rng = np.random.default_rng(hash(song_id) % 2**31)
|
||||
midis = 60 + np.cumsum(rng.integers(-2, 3, n)).astype(float)
|
||||
midis = np.clip(midis, 55, 80)
|
||||
starts = np.arange(n) * 0.5
|
||||
return Melody(midis, starts, np.full(n, 0.45), song_id, song_id)
|
||||
|
||||
|
||||
def test_make_query_specs_respects_difficulty():
|
||||
melodies = [_melody(f"m{i}") for i in range(3)]
|
||||
easy = make_query_specs(melodies, n_per_song=2, difficulty="easy", seed=1)
|
||||
hard = make_query_specs(melodies, n_per_song=2, difficulty="hard", seed=1)
|
||||
|
||||
assert len(easy) == len(hard) == 6
|
||||
assert easy[0].snr_db > hard[0].snr_db
|
||||
for spec in easy + hard:
|
||||
assert 4 <= spec.n_notes <= 24
|
||||
assert spec.start_idx >= 0
|
||||
|
||||
|
||||
def test_query_specs_are_reproducible():
|
||||
melodies = [_melody("m")]
|
||||
a = make_query_specs(melodies, n_per_song=3, seed=123)
|
||||
b = make_query_specs(melodies, n_per_song=3, seed=123)
|
||||
assert [s.to_dict() for s in a] == [s.to_dict() for s in b]
|
||||
|
||||
|
||||
def test_perturb_melody_applies_transposition_and_tempo():
|
||||
source = _melody()
|
||||
spec = make_query_specs([source], n_per_song=1, seed=5)[0]
|
||||
query = perturb_melody(source, spec, DEFAULT)
|
||||
|
||||
# 音高整体平移(允许抖动带来的偏差)
|
||||
offset = float(np.median(query.midis) - np.median(source.midis))
|
||||
assert abs(offset - spec.shift) < 1.5
|
||||
# 时长被速度缩放
|
||||
ratio = float(np.median(query.durs) / np.median(source.durs))
|
||||
assert abs(ratio - spec.tempo) < 0.3
|
||||
|
||||
|
||||
def test_perturb_melody_changes_note_count():
|
||||
source = _melody(n=40)
|
||||
spec = make_query_specs([source], n_per_song=1, difficulty="hard", seed=9)[0]
|
||||
query = perturb_melody(source, spec, DEFAULT)
|
||||
assert query.n_notes >= 3
|
||||
|
||||
|
||||
def test_add_noise_achieves_rough_snr():
|
||||
rng = np.random.default_rng(0)
|
||||
t = np.arange(16000) / 16000
|
||||
signal = (0.5 * np.sin(2 * np.pi * 440 * t)).astype(np.float32)
|
||||
for target in (20.0, 10.0):
|
||||
noisy = add_noise(signal, target, rng)
|
||||
noise = noisy - signal
|
||||
measured = 10 * np.log10(np.mean(signal**2) / np.mean(noise**2))
|
||||
assert abs(measured - target) < 1.0
|
||||
|
||||
|
||||
def test_synth_query_audio_has_signal():
|
||||
source = _melody()
|
||||
spec = make_query_specs([source], n_per_song=1, seed=3)[0]
|
||||
query = perturb_melody(source, spec, DEFAULT)
|
||||
audio = synth_query_audio(query, spec, DEFAULT)
|
||||
assert audio.size > 0
|
||||
assert float(np.abs(audio).max()) > 0.1
|
||||
|
||||
|
||||
def test_evaluate_returns_sane_metrics(tiny_library):
|
||||
engine = SearchEngine(tiny_library, DEFAULT)
|
||||
specs = make_query_specs(tiny_library.melodies(), n_per_song=2, difficulty="easy", seed=4)
|
||||
report = evaluate(engine, specs, label="test", cfg=DEFAULT)
|
||||
|
||||
assert report.n_queries == len(specs)
|
||||
assert 0.0 <= report.top1 <= 1.0
|
||||
assert report.top3 >= report.top1
|
||||
assert 0.0 <= report.mrr <= 1.0
|
||||
assert report.p95_latency_ms >= report.p50_latency_ms
|
||||
assert len(report.details) == report.n_queries
|
||||
payload = report.to_dict()
|
||||
assert payload["n_songs"] == 3
|
||||
assert "config" in payload
|
||||
|
||||
|
||||
def test_evaluate_empty_specs():
|
||||
class _Empty:
|
||||
class index:
|
||||
n_songs = 0
|
||||
|
||||
class library:
|
||||
@staticmethod
|
||||
def melodies():
|
||||
return []
|
||||
|
||||
report = evaluate(_Empty(), [], label="empty", cfg=DEFAULT)
|
||||
assert report.n_queries == 0
|
||||
assert report.top1 == 0.0
|
||||
|
||||
|
||||
def test_report_save_and_summary(tiny_library, tmp_path):
|
||||
engine = SearchEngine(tiny_library, DEFAULT)
|
||||
specs = make_query_specs(tiny_library.melodies(), n_per_song=1, seed=6)
|
||||
report = evaluate(engine, specs, label="save", cfg=DEFAULT)
|
||||
path = tmp_path / "report.json"
|
||||
report.save(path)
|
||||
assert path.exists()
|
||||
assert "Top-1" in report.summary_line()
|
||||
|
||||
|
||||
def test_difficulty_presets_have_expected_keys():
|
||||
for name, preset in DIFFICULTY_PRESETS.items():
|
||||
for key in ("fragment_low", "fragment_high", "drop", "jitter", "snr_db"):
|
||||
assert key in preset, (name, key)
|
||||
Reference in New Issue
Block a user