code-HFD_lzk / tests /test_hfd_third_party_data.py
Tinghong-Ye22's picture
Add files using upload-large-folder tool
db172f6 verified
Raw History Blame Contribute Delete
5.62 kB
from __future__ import annotations
import numpy as np
import pytest
from scripts.data.build_hfd_third_party_sessions import (
_difficulty_counts,
_difficulty_plan,
_placement_for_profile,
_preview_bounds,
_profile_counts,
_profile_plan,
_target_level_db,
_timeline_content_sha256,
)
from scripts.data.hfd_third_party_common import (
OVERLAP_SAMPLES,
STRIDE_SAMPLES,
WINDOW_SAMPLES,
expected_window_count,
gain_for_db,
measured_db,
reconstruct_overlapping_windows,
semantic_filter_reason,
signal_to_residual_db_on_active_signal,
)
def test_stride_one_windows_reconstruct_one_continuous_waveform() -> None:
waveform = np.linspace(
-0.25,
0.25,
WINDOW_SAMPLES + 3 * STRIDE_SAMPLES,
dtype=np.float32,
)
windows = [
waveform[start : start + WINDOW_SAMPLES]
for start in range(0, 3 * STRIDE_SAMPLES + 1, STRIDE_SAMPLES)
]
rebuilt = reconstruct_overlapping_windows(windows)
np.testing.assert_array_equal(rebuilt, waveform)
assert expected_window_count(rebuilt.size) == len(windows)
assert OVERLAP_SAMPLES == 3_200
def test_reconstruction_rejects_a_discontinuous_overlap() -> None:
first = np.zeros(WINDOW_SAMPLES, dtype=np.float32)
second = np.zeros(WINDOW_SAMPLES, dtype=np.float32)
second[0] = 0.1
with pytest.raises(ValueError, match="overlap"):
reconstruct_overlapping_windows([first, second])
def test_gain_contract_reaches_requested_db_ratio() -> None:
reference_rms = 0.08
interference_rms = 0.2
target_db = 7.0
gain = gain_for_db(reference_rms, interference_rms, target_db)
assert measured_db(reference_rms, interference_rms * gain) == pytest.approx(
target_db, abs=1e-10
)
def test_semantic_filter_is_conservative_about_motion_commands() -> None:
commands = {"raise your right hand", "walk forward"}
assert semantic_filter_reason(
"The historian described how people walk home", commands
) == "command_words:walk"
assert semantic_filter_reason(
"The lantern remained beside the quiet window", commands
) is None
assert semantic_filter_reason("walk forward", commands) == "too_few_words"
def test_pilot_profile_allocation_is_exact_and_deterministic() -> None:
assert _profile_counts(96) == {
"clean_reference": 12,
"ambient_noise": 12,
"white_or_colored_noise": 12,
"foreground_non_command": 30,
"babble": 30,
}
assert _profile_counts(32) == {
"clean_reference": 4,
"ambient_noise": 4,
"white_or_colored_noise": 4,
"foreground_non_command": 10,
"babble": 10,
}
first = _profile_plan(32, 26091702, "validation")
second = _profile_plan(32, 26091702, "validation")
assert first == second
assert len(first) == 32
assert "invalid_audio_stream" not in first
def test_difficulty_and_overlap_plans_match_v2_contract() -> None:
assert _difficulty_counts(30) == {"easy": 15, "medium": 11, "hard": 4}
assert _difficulty_counts(10) == {"easy": 5, "medium": 4, "hard": 1}
first = _difficulty_plan(30, 26091702, "train", "babble")
second = _difficulty_plan(30, 26091702, "train", "babble")
assert first == second
assert len(first) == 30
assert sum(
_placement_for_profile("foreground_non_command", index)
== "partial_command_overlap"
for index in range(30)
) == 18
assert sum(
_placement_for_profile("babble", index) == "partial_command_overlap"
for index in range(10)
) == 6
assert _target_level_db("babble", "hard", "train") == 0.0
assert _target_level_db("babble", "hard", "validation") == -1.0
def test_non_overlap_preview_keeps_command_and_later_interference() -> None:
target_start = int(6.6 * 16_000)
target_end = int(8.28 * 16_000)
interference_start = int(14.8 * 16_000)
interference_end = int(18.27 * 16_000)
start, end = _preview_bounds(
sample_count=int(20.2 * 16_000),
preview_bucket="non_overlap",
target_events=[
{
"start_sample": target_start,
"end_sample": target_end,
"turn_index": 1,
}
],
interference_events=[
{
"start_sample": interference_start,
"end_sample": interference_end,
"placement": "during_motion",
}
],
)
assert start <= target_start < target_end <= end
assert start <= interference_start < interference_end <= end
assert end - start > 12 * 16_000
def test_total_signal_to_residual_ratio_uses_all_background() -> None:
signal = np.full(16_000, 0.2, dtype=np.float32)
residual = np.full(16_000, 0.1, dtype=np.float32)
mixture = signal + residual
ratio = signal_to_residual_db_on_active_signal(signal, mixture)
assert ratio == pytest.approx(20.0 * np.log10(2.0), abs=1e-6)
assert signal_to_residual_db_on_active_signal(
signal * 0.5, mixture * 0.5
) == pytest.approx(ratio, abs=1e-6)
def test_timeline_hash_is_key_order_independent_and_content_sensitive() -> None:
left = {
"states": np.arange(12, dtype=np.float32).reshape(3, 4),
"mask": np.asarray([True, False, True]),
}
right = {"mask": left["mask"].copy(), "states": left["states"].copy()}
assert _timeline_content_sha256(left) == _timeline_content_sha256(right)
right["states"][1, 2] += 1.0
assert _timeline_content_sha256(left) != _timeline_content_sha256(right)