forked from ChelseaKR/olive-bark-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_eval.py
More file actions
56 lines (42 loc) · 1.95 KB
/
Copy pathtest_eval.py
File metadata and controls
56 lines (42 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Eval: detection accuracy against a labeled synthetic session.
A synthetic session with known loud regions stands in for the labeled recording the
roadmap calls for (deterministic, no hardware, no stored audio). We assert the monitor
recovers the right number of events and that each lines up with a labeled region.
"""
from __future__ import annotations
from monitor.capture import LoudRegion, synthetic_session
from monitor.config import Config
from monitor.service import run_pipeline
# Labeled ground truth: two distinct loud spans in an otherwise quiet 20 s session.
LABELS = [
LoudRegion(start_s=2.0, end_s=5.0, amplitude=0.3),
LoudRegion(start_s=10.0, end_s=13.0, amplitude=0.4),
]
SESSION_SECONDS = 20.0
def _detect(threshold=-35.0, min_duration=0.4, debounce=1.0):
config = Config(threshold_dbfs=threshold, min_duration_s=min_duration, debounce_s=debounce)
source = synthetic_session(
SESSION_SECONDS, LABELS, sample_rate=config.sample_rate, frame_size=config.frame_size
)
return list(run_pipeline(source, config))
def test_recovers_the_labeled_event_count():
events = _detect()
assert len(events) == len(LABELS)
def test_detected_events_align_with_labels():
events = sorted(_detect(), key=lambda e: e.start)
for ev, label in zip(events, LABELS):
# Detection should start within one frame of the label and not overrun it much.
assert abs(ev.start - label.start_s) <= 0.2, (ev.start, label.start_s)
assert abs(ev.end - label.end_s) <= 0.2, (ev.end, label.end_s)
assert ev.peak_level >= -35.0
def test_no_false_positives_in_quiet_session():
quiet = list(
run_pipeline(
synthetic_session(SESSION_SECONDS, [], sample_rate=16000, frame_size=1600),
Config(),
)
)
assert quiet == []
def test_threshold_too_high_detects_nothing():
# If the threshold is above the loud level, no events should be reported.
assert _detect(threshold=0.0) == []