foley.eval#

Tier-1 retrieval evaluation — metrics, a frozen golden set, and the nDCG gate.

This subpackage answers “did we retrieve the right sound?” with TREC-style ranking metrics (nDCG@10 / Recall@k / mAP@10 / MRR@10) over a frozen golden set, and ships the PR gate: a pytest check that blocks a change whose nDCG@10 regresses by more than 0.02 on the Ring-0 golden set. Every part is pure numpy/stdlib and deterministic — no ranx/numba, no CLAP, no downloads — so the gate runs on every index/embedder/prompt PR in foley’s CI.

Tier-2 fit-judging (#10b) — “does this clip match the intent?” — lives alongside in fit (the judge-based fit harness), reliability (Krippendorff’s α + judge-vs-human calibration), fidelity (set-level FAD/KAD generation fidelity) and aesthetics (per-clip Audiobox Aesthetics scores). Tier-2 is nightly/pre-release + cost-gated — NOT a per-PR gate — and never touches the retrieval ranking. The tasteful-weave eval (#45), taste, reports the restraint / licence / placement / loudness of a whole sound design over a passage set, with a pluggable designer (foley.score by default). All numpy imports are function-local so import foley stays dol-only.

Functions

ndcg_at_k(qrels_q, run_q[, k])

Normalized DCG at k with linear gains (0.0 when no graded answer).

recall_at_k(qrels_q, run_q[, k, rel_lvl])

Fraction of ALL relevant docs (grade ≥ rel_lvl) retrieved in the top-k.

precision_at_k(qrels_q, run_q[, k, rel_lvl])

Fraction of the top-k that is relevant (denominator is literal k).

average_precision_at_k(qrels_q, run_q[, k, ...])

Average precision (trec_eval map: divide by TOTAL relevant, not min(R,k)).

mrr_at_k(qrels_q, run_q[, k, rel_lvl])

Reciprocal rank of the first relevant doc in the top-k (0.0 if none).

mean_over_queries(values)

Macro-average of per-query metric values (0.0 for an empty list).

build_run(candidates)

Turn an ordered search() result into a run row with distinct scores.

evaluate_run(run, qrels, *[, k, metrics, ...])

Score a run against qrels, returning a RetrievalReport.

load_golden([path])

Load and validate the frozen golden set from path (JSON list).

to_qrels(golden)

Flatten the golden set into TREC qrels: {query_id: {clip_id: grade}}.

build_eval_library(*[, embedder, manifest_path])

Build the ephemeral Ring-0 eval library (stem ids + injected caption vectors).

run_ring0_retrieval_eval(*[, k, ...])

Score the golden set's queries against the Ring-0 library — the gate input.

load_baseline([path])

Load the committed baseline dict from path.

write_baseline(report, *[, path, metric, ...])

Write a fresh baseline from report (the --update-baseline action).

is_stale(baseline, *, seed_path, manifest_path)

True if the baseline's fixture stamps no longer match the fixtures on disk.

write_fit_baseline(report, *[, path, level, ...])

Write a fresh Tier-2 fit baseline from a FitReport.

check_fit_baseline(report, baseline, *[, ...])

Gate report against a committed fit baseline: (passed, message).

run_fit_eval(*[, golden, golden_path, ...])

Run the Tier-2 fit eval over a stratified golden sample → a FitReport.

fit_precision(matches, relevants)

Fraction of judge-accepted candidates that are actually gold-relevant.

fit_recall(matches, relevants)

TP / (TP + FN) — of the gold-relevant candidates, how many the judge confirmed.

fit_f1(matches, relevants)

The harmonic mean of fit_precision() and fit_recall().

stratified_sample(units, *, strata_keys, ...)

Deterministic seeded stratified draw over units — the cost gate.

krippendorff_alpha(reliability_data, *[, ...])

Krippendorff's α over (raters × units) ratings (nan = missing).

percent_agreement(reliability_data)

Raw pairwise percent agreement, reported ALONGSIDE α (report 08 §2.3).

reliability_band(alpha)

Map an α (or κ) to Krippendorff's benchmark band.

calibrate_judge_vs_human(human_grades, ...)

Compute judge-vs-human agreement and the promotion verdict (report 08 §2.3).

render_panel_html(tasks, *[, title])

One self-contained HTML page for the blind tasks (see the module docstring).

frechet_distance(x_ref, x_gen)

Fréchet distance between two embedding sets (the FAD formula).

kernel_audio_distance(x_ref, x_gen, *[, ...])

Kernel Audio Distance — the unbiased RBF-MMD² between two embedding sets.

generation_fidelity(ref_wavs, gen_wavs, *, ...)

Embed two wav sets through an injected embedder and compute FAD or KAD.

aesthetics_verdict(audio_bytes, *, scorer)

Score audio_bytes with scorer and compare with its floors; never raises.

pam_verdict(audio_bytes, *, scorer)

Score audio_bytes with scorer and compare with its floor; never raises.

run_taste_eval([passages, designer, ...])

Run designer over passages and report restraint / licence / placement / loudness.

score_designer(*[, library, intended_use, ...])

A designer that is foley.score on a passage's segments (and audio, when it has some).

load_passages([path])

Load a passage set: a JSON list of {id, segments, audio?}.

Classes

RetrievalReport([per_query, mean, ranks, k])

Per-query + mean Tier-1 metrics over a golden set (JSON-friendly).

GoldenItem(id, context, expected_events, ...)

One frozen (context → expected sounds) judgment (report 08 §1.3).

HashingBowEmbedder(*[, dim])

Deterministic hashing bag-of-words text embedder (L2-normalized).

FitReport([per_item, fit_precision, ...])

Tier-2 fit metrics over a stratified golden sample (JSON-friendly, mirrors RetrievalReport).

AlphaResult(alpha, level, n_units, n_raters, ...)

A judge-vs-human calibration record (surfaced as FitReport.calibration).

FidelityStamp(embedding, toolkit, version, ...)

Mandatory provenance for a FAD/KAD score — it is meaningless without it.

AestheticsScore(axes, model, version[, toolkit])

One clip's Audiobox Aesthetics scores, stamped with the model and toolkit version.

AudioboxAesthetics(*[, checkpoint, ...])

Per-clip Audiobox Aesthetics scores computed in a separate interpreter.

FadtkFidelity(*[, model, models, workers])

Set-level FAD computed by fadtk in a separate interpreter.

KadtkFidelity(*[, bandwidth, device, ...])

Set-level KAD computed by kadtk in a separate interpreter.

Pam(*[, checkpoint, prompts, batch_size, ...])

Per-clip PAM scores computed by msclap in a separate interpreter.

PamScore(score, segments, model, version[, ...])

One clip's PAM score, its per-window scores, and the model and toolkit version.

FidelityResult(metric, value, stamp[, params])

A stamped set-level fidelity score (attached to FitReport.fidelity).

Passage(id, segments[, audio])

One narration passage: its segments (what foley.score takes) and optional audio.

PassageTaste(passage_id[, restraint, ...])

The four measurements for one passage (JSON-safe via to_dict()).

TasteReport(designer, passages[, thresholds])

The tasteful-weave eval over a passage set: per passage + aggregated, plus thresholds.

TasteThresholds([sounds_per_sentence, ...])

The eval's thresholds in one place (defaults: the module constants).

class foley.eval.AestheticsScore(axes, model, version, toolkit='audiobox_aesthetics')[source]#

Bases: object

One clip’s Audiobox Aesthetics scores, stamped with the model and toolkit version.

to_dict()[source]#

The axes and the stamp as one flat JSON-safe dict.

Return type:

dict

class foley.eval.AlphaResult(alpha, level, n_units, n_raters, percent_agreement, band, promoted)[source]#

Bases: object

A judge-vs-human calibration record (surfaced as FitReport.calibration).

Codifies the promotion rule in one legible object: a model judge is promoted to unattended use only once its agreement with the human raters reaches the ALPHA_RELIABLE band.

class foley.eval.AudioboxAesthetics(*, checkpoint=None, batch_size=10, min_seconds=0.025, floors=None, timeout=300.0, **kwargs)[source]#

Bases: ExternalToolkit

Per-clip Audiobox Aesthetics scores computed in a separate interpreter.

:param those of ExternalToolkit (python: :param : :param env: :param timeout: :param runner): :param plus: :type checkpoint: Optional[str] :param checkpoint: A local checkpoint.pt (default: the Hugging Face weights,

downloaded once into the toolkit’s HF_HOME; pass env={"HF_HOME": ...} to choose where).

Parameters:
  • batch_size (int) – Clips a forward pass.

  • min_seconds (float) – The shortest clip scored (default AES_MIN_SECONDS).

  • timeout (Optional[float]) – Seconds before a run is abandoned (default AES_TIMEOUT_S; None waits forever).

  • floors (Optional[Mapping[str, float]]) – Axis → the lowest acceptable score, read by aesthetics_verdict() (default none: the verdict only records).

check_clip(clip, *, sr, name)[source]#

Refuse a clip shorter than min_seconds, or with a NaN or infinite sample.

Return type:

None

failure_hints: Mapping[str, str] = {"No module named 'requests'": "audiobox_aesthetics imports requests without declaring it. Install: uv venv ~/venvs/aes --python 3.11 && uv pip install --python ~/venvs/aes/bin/python audiobox_aesthetics requests; then AudioboxAesthetics(python='~/venvs/aes/bin/python') or set FOLEY_AES_PYTHON"}#

A substring of a failed run’s output → what to do about it.

isolation_reason: str = 'it brings its own torch and torchaudio, which foley does not depend on'#

Why the toolkit cannot live in foley’s environment (said when no interpreter).

class foley.eval.FadtkFidelity(*, model=None, models=None, workers=1, **kwargs)[source]#

Bases: ExternalFidelity

Set-level FAD computed by fadtk in a separate interpreter.

:param those of ExternalFidelity; model: :param is a key of FADTK_MODELS: :type is a key of FADTK_MODELS: default DEFAULT_FADTK_MODEL

frames_needed(dim)[source]#

More frames than dimensions, or the covariance is singular and FAD meaningless.

Return type:

tuple[int, str]

class foley.eval.FidelityResult(metric, value, stamp, params=dict())[source]#

Bases: object

A stamped set-level fidelity score (attached to FitReport.fidelity).

params holds the settings the value depends on beyond the stamp, e.g. kadtk’s bandwidth and scale: score a second generator against the same reference with the same bandwidth for the two values to be comparable.

class foley.eval.FidelityStamp(embedding, toolkit, version, n_ref, n_gen)[source]#

Bases: object

Mandatory provenance for a FAD/KAD score — it is meaningless without it.

FAD/KAD are not comparable across embeddings/toolkits/versions, so a bare number is uninterpretable; this stamp makes the basis of comparison legible.

class foley.eval.FitReport(per_item=dict(), fit_precision=0.0, fit_recall=0.0, fit_f1=0.0, fit_score=0.0, auto_accept_rate=0.0, n_accepted=0, n_confirmed=0, strata=dict(), calibration=None, fidelity=dict(), judge_model='', embedder_model_id='', seed=0, k=10, schema_version=1)[source]#

Bases: object

Tier-2 fit metrics over a stratified golden sample (JSON-friendly, mirrors RetrievalReport).

The fit numbers are co-emitted with the judge-vs-human calibration (an AlphaResult dict) so the §5 human-calibration guardrail is legible: a fit_precision is only trusted where α certifies the judge. fidelity holds stamped FAD/KAD results for release-level backend comparison. Both default empty on the pure-fake path (no human labels, no generated-set wavs).

format_regression_diff(baseline)[source]#

A human diff of fit_precision vs a committed baseline (trend reporting).

The committed fit-baseline stamp is deferred until real judges produce stable numbers; this ships the trend path only.

Return type:

str

gate(min_fit_precision)[source]#

The Tier-2 gate — a NO-OP returning True unless a floor is supplied.

Structural guarantee that Tier-2 is never a silent per-PR gate: CI passes no floor, so this is always True; only the nightly/pre-release runner sets one.

Return type:

bool

class foley.eval.GoldenItem(id, context, expected_events, answer_clip_ids, grade, negatives=list(), labeler='llm+human', schema_version=1)[source]#

Bases: object

One frozen (context → expected sounds) judgment (report 08 §1.3).

id#

Unique item id (e.g. gld_0001).

Type:

str

context#

The narrative paragraph (the future SELECT input).

Type:

str

expected_events#

One or more {query, ucs_catid, layer, diegetic, salience, ...} dicts; query is the string fed to search.

Type:

list

answer_clip_ids#

{ucs_catid: [clip_id, ...]} — the acceptable clips.

Type:

dict

grade#

{clip_id: grade} (2 ideal / 1 acceptable / 0 wrong).

Type:

dict

negatives#

Free-text distractors (unused by scoring; documentation).

Type:

list

labeler#

Provenance of the labels (llm+human etc.).

Type:

str

schema_version#

The GoldenItem schema version.

Type:

int

class foley.eval.HashingBowEmbedder(*, dim=64)[source]#

Bases: object

Deterministic hashing bag-of-words text embedder (L2-normalized).

Two texts sharing a token share a dimension, so cosine similarity tracks lexical overlap — plausible and reproducible (a stable hashlib hash, never the salted builtin hash). Suitable only for the eval gate / offline retrieval regression; real retrieval uses CLAP.

embed_text(text)[source]#

Embed text (or a list of texts) -> a (n, dim) float32 array.

class foley.eval.KadtkFidelity(*, bandwidth=None, device='cpu', warn_frames=100, clip_rule=None, **kwargs)[source]#

Bases: ExternalFidelity

Set-level KAD computed by kadtk in a separate interpreter.

:param those of ExternalFidelity: :param plus: :type bandwidth: Optional[float] :param bandwidth: The RBF bandwidth (default: kadtk’s, the median pairwise distance

of the generated set’s embedding frames; the one used is in params).

Parameters:
  • device (str) – Where kadtk computes the kernel sums ('cpu', 'cuda', 'mps').

  • warn_frames (Optional[int]) – Warn below this many frames a set (default KADTK_WARN_FRAMES; None never warns).

  • clip_rule (Optional[ClipRule]) – What each clip must be (default: the model’s in KADTK_CLIP_RULES, else any clip).

model is a key of KADTK_MODELS (default DEFAULT_KADTK_MODEL).

check_clip(clip, *, sr, name)[source]#

Refuse a clip clip_rule says kadtk would crash on, before it runs.

Return type:

None

failure_hints: Mapping[str, str] = {"No module named '_tkinter'": "kadtk imports turtle, which needs Tk; build the venv on a Python that has it. Install: uv venv ~/venvs/kadtk --python 3.11 --managed-python && uv pip install --python ~/venvs/kadtk/bin/python kadtk torchaudio==2.5.1; then KadtkFidelity(python='~/venvs/kadtk/bin/python') or set FOLEY_KADTK_PYTHON", '_aoti_torch_abi_version': "torchaudio does not match torch; pin torchaudio to torch's version. Install: uv venv ~/venvs/kadtk --python 3.11 --managed-python && uv pip install --python ~/venvs/kadtk/bin/python kadtk torchaudio==2.5.1; then KadtkFidelity(python='~/venvs/kadtk/bin/python') or set FOLEY_KADTK_PYTHON", 'maximum size for tensor at dimension': "kadtk pools this model's embedding per 1 s and crashes on a clip (or, for encodec-emb-48k, a last segment) shorter than that; use longer clips, or give the model a clip_rule"}#

A substring of a failed run’s output → what to do about it.

frames_needed(dim)[source]#

Two frames: the unbiased MMD² averages over pairs of distinct frames.

Return type:

tuple[int, str]

options()[source]#

kadtk’s bandwidth and device.

Return type:

dict

class foley.eval.Pam(*, checkpoint=None, prompts=('the sound is clear and clean.', 'the sound is noisy and with artifacts.'), batch_size=10, device='cpu', floor=None, **kwargs)[source]#

Bases: ExternalToolkit

Per-clip PAM scores computed by msclap in a separate interpreter.

:param those of ExternalToolkit (python: :param : :param default $FOLEY_PAM_PYTHON; env: :param e.g. {"HF_HOME": ...} to choose where: :param the weights go; timeout; runner): :param plus: :type checkpoint: Optional[str] :param checkpoint: A local CLAP_weights_2023.pth (default: downloaded once into the

interpreter’s HF_HOME).

Parameters:
  • prompts (Sequence[str]) – The prompts, the clean one first (default PAM_PROMPTS; others give a PAM-style score, not PAM’s).

  • batch_size (int) – Windows a forward pass.

  • device (str) – Where the model runs ('cpu', 'cuda', 'mps').

  • floor (Optional[float]) – The lowest acceptable score in [0, 1], read by pam_verdict() (a clip below it is flagged low, never quarantined). Default none: the verdict only records (no floor is calibrated for SFX).

check_clip(clip, *, sr, name)[source]#

Refuse an empty clip, or one with a NaN or infinite sample.

A short clip is scored: PAM tiles it to one 7 s window.

Return type:

None

isolation_reason: str = 'it pins numpy<2 and brings its own torch'#

Why the toolkit cannot live in foley’s environment (said when no interpreter).

class foley.eval.PamScore(score, segments, model, version, toolkit='msclap')[source]#

Bases: object

One clip’s PAM score, its per-window scores, and the model and toolkit version.

to_dict()[source]#

The score, the window scores and the stamp as one JSON-safe dict.

Return type:

dict

class foley.eval.Passage(id, segments, audio=None)[source]#

Bases: object

One narration passage: its segments (what foley.score takes) and optional audio.

audio (a path, bytes, or a 48 kHz ndarray) makes the default designer weave, which is what the loudness metric needs.

property text: str#

The passage as one transcript.

class foley.eval.PassageTaste(passage_id, restraint=dict(), licence=list(), placement=list(), alignment=None, loudness=None, error=None)[source]#

Bases: object

The four measurements for one passage (JSON-safe via to_dict()).

property aligned: bool#

Whether the word timings came from a real alignment (not the fake’s estimates).

"cached" timings (carried on the timeline from an earlier weave) count as aligned: the eval cannot tell what produced them, so a cache of estimates passes.

property licence_clean_share: Optional[float]#

Share of placed sounds the licence gate keeps (None when nothing was placed).

property placement_ok_share: Optional[float]#

Share of placed sounds within the placement tolerance.

to_dict()[source]#

Plain, JSON-safe dict (with the derived shares).

Return type:

dict

class foley.eval.RetrievalReport(per_query=dict(), mean=dict(), ranks=dict(), k=10)[source]#

Bases: object

Per-query + mean Tier-1 metrics over a golden set (JSON-friendly).

format_regression_diff(baseline)[source]#

A human diff for a failing gate: mean vs baseline + the worst queries.

Return type:

str

class foley.eval.TasteReport(designer, passages, thresholds=TasteThresholds())[source]#

Bases: object

The tasteful-weave eval over a passage set: per passage + aggregated, plus thresholds.

summary()[source]#

The aggregate numbers (pooled over sounds, or over passages for the band/loudness).

Return type:

dict

to_dict()[source]#

Plain, JSON-safe dict: designer, thresholds, summary, per-passage rows.

Return type:

dict

to_markdown()[source]#

A short markdown rendering: the aggregate table, then one row per passage.

Return type:

str

class foley.eval.TasteThresholds(sounds_per_sentence=(0.1, 1.0), licence_clean_target=1.0, placement_tolerance_s=0.25, loudness_tolerance_lu=1.0)[source]#

Bases: object

The eval’s thresholds in one place (defaults: the module constants).

to_dict()[source]#

Plain, JSON-safe dict.

Return type:

dict

foley.eval.aesthetics_verdict(audio_bytes, *, scorer)[source]#

Score audio_bytes with scorer and compare with its floors; never raises.

Returns:

status (scored | ok | low | unchecked), and the four axes (None for a non-finite one), model, toolkit and version when scored, plus floors and the axes below them when the scorer has floors; or a short reason (_reason(): no paths, no program output) when not scored: no interpreter, a clip too short, a timeout, a crash, a scorer answering anything but one well-formed score.

Return type:

dict

>>> import numpy as np
>>> from foley.audio import encode
>>> clip = encode(np.zeros(1600), 16000)
>>> aesthetics_verdict(clip, scorer=lambda wavs, *, sr: [])["reason"]
'ValueError: the scorer returned 0 scores for 1 clip'
foley.eval.average_precision_at_k(qrels_q, run_q, k=10, *, rel_lvl=1)[source]#

Average precision (trec_eval map: divide by TOTAL relevant, not min(R,k)).

Return type:

float

foley.eval.build_eval_library(*, embedder=None, manifest_path=PosixPath('/home/runner/work/foley/foley/foley/data/golden/corpus.json'))[source]#

Build the ephemeral Ring-0 eval library (stem ids + injected caption vectors).

Each manifest clip becomes a SoundRecord with id ring0:<stem> and a vector = embedder.embed_text(caption + tags) — so both the vector and BM25 legs carry the caption bag-of-words and the answer clip lands at integer rank 1 (deterministic, cross-platform).

Parameters:
  • embedder – A text embedder (default: HashingBowEmbedder).

  • manifest_path – The Ring-0 manifest.json (default: the bundled fixture).

Returns:

A populated in-memory SoundLibrary.

foley.eval.build_run(candidates)[source]#

Turn an ordered search() result into a run row with distinct scores.

Assigns each candidate a strictly-descending integer score (len - rank) in the order returned, so the run carries **no score ties** and _ranked_ids() reproduces the search ranking exactly — the determinism the gate relies on. The doc id is candidate.sound.id.

Parameters:

candidates – An ordered list[Candidate] from foley.index.library.SoundLibrary.search() (best first).

Return type:

dict[str, float]

Returns:

{clip_id: score} with distinct descending scores.

foley.eval.calibrate_judge_vs_human(human_grades, judge_grades, *, level='ordinal')[source]#

Compute judge-vs-human agreement and the promotion verdict (report 08 §2.3).

Stacks the human rating row(s) and the model-judge row into one reliability matrix and returns an AlphaResult; promoted is True iff α reaches the reliable band — the model judge may then be trusted unattended on that slice.

Parameters:
  • human_grades – A 1-D per-unit human grade sequence, or a 2-D (humans × units) matrix (nan = missing).

  • judge_grades – The model judge’s 1-D per-unit grades (nan = missing).

  • level (str) – The measurement level (default 'ordinal').

Return type:

AlphaResult

Returns:

An AlphaResult.

foley.eval.check_fit_baseline(report, baseline, *, level='judge', sample=None, seed_path=None, manifest_path=None)[source]#

Gate report against a committed fit baseline: (passed, message).

Passes when fit_precision >= value - tolerance. A report measured under a different judge, embedder, rung, sample, seed or k than the baseline’s run is not comparable and fails with a message saying so (re-stamp with foley eval-fit --update-baseline), never a silent pass. So does a golden set that no longer matches the baseline’s stamp, when seed_path and manifest_path are given.

Return type:

tuple[bool, str]

foley.eval.evaluate_run(run, qrels, *, k=10, metrics=('ndcg@10', 'recall@10', 'map@10', 'mrr@10'), rel_lvl=1)[source]#

Score a run against qrels, returning a RetrievalReport.

Only query ids present in qrels are scored (a run row without a ground-truth judgment is ignored). ranks records, per query, the 1-based rank of the highest-graded answer (for the failure diff).

Return type:

RetrievalReport

foley.eval.fit_f1(matches, relevants)[source]#

The harmonic mean of fit_precision() and fit_recall().

Return type:

float

foley.eval.fit_precision(matches, relevants)[source]#

Fraction of judge-accepted candidates that are actually gold-relevant.

TP / (TP + FP) where a candidate is a positive iff the fit-judge said match and gold-relevant iff its golden grade ≥ 1. This measures the JUDGE’s auto-accept purity (a low value ⇒ the judge waves through wrong clips) — NOT the ranker. Pure, closed-form, and the decisive correctness oracle (the Ring-0 + fake-judge plumbing makes the end-to-end value tautological, so the metric math is tested in isolation).

Parameters:
  • matches (Sequence[bool]) – Per-candidate fit-judge match booleans.

  • relevants (Sequence[bool]) – Per-candidate gold-relevance booleans (grade ≥ 1).

Return type:

float

Returns:

The fit precision in [0, 1] (0.0 when nothing was accepted).

foley.eval.fit_recall(matches, relevants)[source]#

TP / (TP + FN) — of the gold-relevant candidates, how many the judge confirmed.

Return type:

float

foley.eval.frechet_distance(x_ref, x_gen)[source]#

Fréchet distance between two embedding sets (the FAD formula).

‖μ_r − μ_g‖² + Tr(Σ_r + Σ_g − 2(Σ_r Σ_g)^½) — lower = the generated set’s embedding distribution is closer to the reference. Feed PANNs Wavegram-Logmel embeddings for “FAD-P”, or CLAP for a domain-matched FAD.

Parameters:
  • x_ref – Reference embeddings (n_ref, d).

  • x_gen – Generated embeddings (n_gen, d).

Return type:

float

Returns:

The FAD as a float (≥ 0 up to numerical round-off; ~0 for identical distributions).

foley.eval.generation_fidelity(ref_wavs, gen_wavs, *, embedder, sr=48000, metric='fad', toolkit='foley-numpy', version='1')[source]#

Embed two wav sets through an injected embedder and compute FAD or KAD.

The real fidelity path is a zero-call-site-change embedder swap: a fake / hashing embedder in CI, PANNs / CLAP (foley[fit]) in prod. The embedder’s model_id is stamped into the result.

Parameters:
  • ref_wavs – An iterable of reference waveforms (1-D arrays).

  • gen_wavs – An iterable of generated waveforms (1-D arrays).

  • embedder – An object with embed_audio(wav, sr) -> vector and model_id.

  • sr (int) – The sample rate passed to embed_audio.

  • metric (str) – 'fad' (default) or 'kad'.

  • toolkit (str) – Provenance label for the stamp (default 'foley-numpy').

  • version (str) – Provenance version for the stamp.

Return type:

FidelityResult

Returns:

A FidelityResult carrying the score and its FidelityStamp.

foley.eval.is_stale(baseline, *, seed_path, manifest_path)[source]#

True if the baseline’s fixture stamps no longer match the fixtures on disk.

Return type:

bool

foley.eval.kernel_audio_distance(x_ref, x_gen, *, bandwidth=None)[source]#

Kernel Audio Distance — the unbiased RBF-MMD² between two embedding sets.

Distribution-free and small-sample-convergent; preferred over FAD for the small reference/generated sets of early-stage eval. The diagonal (same-sample) kernel terms are dropped (the unbiased estimator), so it is centred on 0 for two sets from one distribution and is then negative about half the time; small sets make that spread wide, so a value near 0 means “indistinguishable”, not “identical”.

Parameters:
  • x_ref – Reference embeddings (n_ref, d).

  • x_gen – Generated embeddings (n_gen, d).

  • bandwidth (Optional[float]) – RBF bandwidth σ (default: the median heuristic over the pooled set).

Return type:

float

Returns:

The unbiased MMD² as a float.

foley.eval.krippendorff_alpha(reliability_data, *, level='ordinal', value_domain=None)[source]#

Krippendorff’s α over (raters × units) ratings (nan = missing).

α = 1 − D_o/D_e — 1 is perfect agreement, ~0 is chance, negative is systematic disagreement. Ordinal is the default (foley’s 0–3 grades); missing ratings are tolerated (units with < 2 ratings are dropped, no imputation). Pure numpy.

Parameters:
  • reliability_data – A 2-D array-like (raters × units); nan = missing.

  • level (str) – 'ordinal' (default) | 'nominal' | 'interval'.

  • value_domain – Optional explicit value set (for a label unobserved by some rater).

Return type:

float

Returns:

α as a bare float (1.0 when there is no expected disagreement, D_e = 0).

foley.eval.load_baseline(path=PosixPath('/home/runner/work/foley/foley/foley/data/golden/baseline.json'))[source]#

Load the committed baseline dict from path.

Return type:

dict

foley.eval.load_golden(path=PosixPath('/home/runner/work/foley/foley/foley/data/golden/seed.json'))[source]#

Load and validate the frozen golden set from path (JSON list).

Return type:

list[GoldenItem]

foley.eval.load_passages(path=PosixPath('/home/runner/work/foley/foley/foley/data/taste/passages.json'))[source]#

Load a passage set: a JSON list of {id, segments, audio?}.

A relative audio path resolves against the JSON file’s folder.

Return type:

list[Passage]

foley.eval.mean_over_queries(values)[source]#

Macro-average of per-query metric values (0.0 for an empty list).

Return type:

float

foley.eval.mrr_at_k(qrels_q, run_q, k=10, *, rel_lvl=1)[source]#

Reciprocal rank of the first relevant doc in the top-k (0.0 if none).

Return type:

float

foley.eval.ndcg_at_k(qrels_q, run_q, k=10)[source]#

Normalized DCG at k with linear gains (0.0 when no graded answer).

Return type:

float

foley.eval.pam_verdict(audio_bytes, *, scorer)[source]#

Score audio_bytes with scorer and compare with its floor; never raises.

Returns:

status (scored | ok | low | unchecked), and pam (None when not finite), its window segments, model, toolkit and version when scored, plus floor when the scorer has one; or a short reason (no paths, no program output) when not scored.

Return type:

dict

>>> import numpy as np
>>> from foley.audio import encode
>>> clip = encode(np.zeros(1600), 16000)
>>> pam_verdict(clip, scorer=lambda wavs, *, sr: [])["reason"]
'ValueError: the scorer returned 0 scores for 1 clip'
>>> one = PamScore(score=0.68, segments=(0.68,), model="m", version="v")
>>> pam_verdict(clip, scorer=lambda wavs, *, sr: [one])["status"]
'scored'
foley.eval.percent_agreement(reliability_data)[source]#

Raw pairwise percent agreement, reported ALONGSIDE α (report 08 §2.3).

On foley’s skewed ‘most candidates irrelevant’ label distribution a chance-corrected α can look pessimistic, so the uncorrected agreement is co-reported for context.

Parameters:

reliability_data – A 2-D array-like (raters × units); nan = missing.

Return type:

float

Returns:

The fraction of same-unit rater pairs that agree (0.0 if no pairs).

foley.eval.precision_at_k(qrels_q, run_q, k=10, *, rel_lvl=1)[source]#

Fraction of the top-k that is relevant (denominator is literal k).

Return type:

float

foley.eval.recall_at_k(qrels_q, run_q, k=10, *, rel_lvl=1)[source]#

Fraction of ALL relevant docs (grade ≥ rel_lvl) retrieved in the top-k.

Return type:

float

foley.eval.reliability_band(alpha)[source]#

Map an α (or κ) to Krippendorff’s benchmark band.

Parameters:

alpha (float) – A reliability coefficient in (-∞, 1].

Return type:

str

Returns:

'reliable' (α ≥ 0.8), 'tentative' (0.667 ≤ α < 0.8), else 'revise-rubric'.

foley.eval.render_panel_html(tasks, *, title='Listening panel')[source]#

One self-contained HTML page for the blind tasks (see the module docstring).

The tasks are embedded as JSON, so the page needs nothing but the audio files their clip paths name. Save the result next to the audio and open or host it.

Return type:

str

foley.eval.run_fit_eval(*, golden=None, golden_path=PosixPath('/home/runner/work/foley/foley/foley/data/golden/seed.json'), sample=None, strata_keys=('family', 'diegetic'), fit_judge=None, embedder=None, level=VerifyLevel.judge, seed=0, k=10, llm=None)[source]#

Run the Tier-2 fit eval over a stratified golden sample → a FitReport.

Composes the Tier-1 golden builders + the #7 SELECT loop, applies the authoritative fit-judge as an independent audit, and aggregates fit-precision/recall/F1 + fit-score

  • auto-accept-rate, with a per-stratum breakdown. Report-only unless min_fit_precision is supplied.

Parameters:
  • golden – A pre-loaded golden list (default: load golden_path).

  • golden_path – The golden JSON (default: the bundled Ring-0 seed set).

  • sample (Optional[int]) – The stratified sample cap (default: the whole set — the cost gate).

  • strata_keys – The stratification axes (default ('family', 'diegetic')).

  • fit_judge – The injected authoritative Judge (default: foley.agent.verify._default_fit_judge() for llm — the hermetic fake unless llm / $FOLEY_LLM opts in).

  • embedder – The Ring-0 text/​audio embedder (default: HashingBowEmbedder).

  • level (str | VerifyLevel) – The verify rung the fit-judge audits at — 'listen' or 'judge' (default VerifyLevel.judge). 'clap' is rejected: it never invokes the fit-judge (it only re-runs the retrieval clap gate), so it cannot measure fit.

  • seed (int) – The sampling RNG seed.

  • k (int) – The retrieval shortlist depth per event.

Return type:

FitReport

Returns:

A FitReport. Gating is the caller’s job via FitReport.gate().

Raises:

ValueError – If level is 'clap' (fit judging requires a listen/judge rung).

foley.eval.run_ring0_retrieval_eval(*, k=10, golden_path=PosixPath('/home/runner/work/foley/foley/foley/data/golden/seed.json'), embedder=None)[source]#

Score the golden set’s queries against the Ring-0 library — the gate input.

Runs every golden expected_events[].query through the real SoundLibrary.search() path (vector ⊕ BM25 ⊕ RRF) and evaluates the resulting runs against the golden qrels.

Parameters:
  • k (int) – Retrieval cutoff (and the metric @k).

  • golden_path – The golden set JSON.

  • embedder – The eval embedder (default: HashingBowEmbedder).

Return type:

RetrievalReport

Returns:

A RetrievalReport (per-query + mean nDCG@10 / recall / mAP / MRR).

foley.eval.run_taste_eval(passages=None, *, designer=None, library=None, intended_use=None, thresholds=TasteThresholds(sounds_per_sentence=(0.1, 1.0), licence_clean_target=1.0, placement_tolerance_s=0.25, loudness_tolerance_lu=1.0))[source]#

Run designer over passages and report restraint / licence / placement / loudness.

Report-only: nothing here fails a build. A passage whose designer raises is recorded with its error and left out of the aggregates; a malformed passage set is refused up front (ValueError naming every bad passage), and a metric bug raises.

Parameters:
  • passages (Union[None, str, Path, Iterable]) – The passage set: None (the shipped set), a JSON path, or an iterable of Passage / {id, segments, audio?} dicts.

  • designer (Optional[Callable[[Passage], ScoreResult]]) – passage -> ScoreResult (default: score_designer() over library).

  • library – The library the designer draws from, used for the licence check. Default: eval_library() with the default designer, else foley’s default library.

  • intended_use – The rights intent (default: commercial publishing, as foley.score).

  • thresholds (TasteThresholds) – A TasteThresholds (default: the module constants); echoed in the report.

Return type:

TasteReport

Returns:

A TasteReport (to_dict() for JSON, to_markdown() for people).

foley.eval.score_designer(*, library=None, intended_use=None, llm='fake', generate=False, aligner=None, **score_kwargs)[source]#

A designer that is foley.score on a passage’s segments (and audio, when it has some).

Defaults are the offline, deterministic ones: the fake LLM, no generation fallback (Budget(allow_generate=False, max_usd=0)) and the fake aligner (even with foley[align] installed), so nothing is fetched, generated or paid and runs repeat. generate=True, llm=None ($FOLEY_LLM) and a real aligner give the production behaviour.

Return type:

Callable[[Passage], ScoreResult]

foley.eval.stratified_sample(units, *, strata_keys, sample, seed)[source]#

Deterministic seeded stratified draw over units — the cost gate.

Round-robins across strata (so an easy family cannot dominate a capped sample); sample=None keeps the full set. Each unit is (GoldenItem, event_index, event_dict).

Parameters:
  • units (list) – The full (item, ev_idx, ev_dict) unit list.

  • strata_keys – The stratification axes (e.g. ('family', 'diegetic')).

  • sample (Optional[int]) – The total draw size, or None for all.

  • seed (int) – The RNG seed (reproducibility).

Return type:

list

foley.eval.to_qrels(golden)[source]#

Flatten the golden set into TREC qrels: {query_id: {clip_id: grade}}.

One qrels row per (item, event) — query_id = f"{item.id}::{event_idx}" — future-proofing multi-event items (seed items are single-event today). Each clip in answer_clip_ids carries its grade.

Return type:

dict[str, dict[str, int]]

foley.eval.write_baseline(report, *, path=PosixPath('/home/runner/work/foley/foley/foley/data/golden/baseline.json'), metric='ndcg@10', tolerance=0.02, seed_path, manifest_path, embedder_model_id='foley-eval/hashing-bow-v1', dim=64, rrf_k=60, updated_at, n_items, revision='gld-v1')[source]#

Write a fresh baseline from report (the --update-baseline action).

Records the mean metric value plus sha256 stamps of the seed + manifest so a later fixture edit is detected. updated_at is passed in (not read from the clock) so the caller controls reproducibility. revision labels the golden-set generation (bumped when the frozen set is regrown).

Return type:

dict

Returns:

The baseline dict that was written.

foley.eval.write_fit_baseline(report, *, path=PosixPath('/home/runner/work/foley/foley/foley/data/golden/fit_baseline.json'), level='judge', sample=None, tolerance=0.02, seed_path, manifest_path, updated_at, n_items, revision='gld-v1')[source]#

Write a fresh Tier-2 fit baseline from a FitReport.

The sibling of write_baseline(): the committed number is fit_precision with the same fixture stamps (so a golden-set edit is detected) plus the run it was measured under (judge, embedder, rung, sample, seed, k). The committed file is stamped on the hermetic path (the deterministic fake judge, no network); re-stamp it with a real judge once its numbers are stable, which records that judge in run.

Return type:

dict

Modules

bradley_terry

Bradley-Terry ranking from pairwise preferences (2AFC human panels; #49).

panel

Human-panel infrastructure: blind tasks, a listening page, responses, and the calibration feed (#49).

aesthetics

Per-clip generation quality from Meta's Audiobox Aesthetics, run in its own interpreter (#262).

baseline

The committed nDCG baseline — the PR gate's SSOT + staleness stamps.

clip_checks

The per-clip scorers foley.generate can run on each stored clip: one table (#277).

embedder

A deterministic, numpy-only text embedder for the retrieval eval gate.

external_fidelity

Set-level fidelity from a reference toolkit run in its own interpreter (#48, #262).

external_toolkit

A reference toolkit run in its own interpreter: the part every adapter shares (#48, #262).

fadtk

Reference FAD from Microsoft's fadtk, run in its own interpreter (#48).

fidelity

Set-level generation-fidelity — Fréchet Audio Distance (FAD) + Kernel Audio Distance (KAD).

fit

Tier-2 fit evaluation — "does the accepted clip actually match the intent?" (report 08 §2/§5).

golden

The frozen golden set + the deterministic Ring-0 retrieval harness.

kadtk

Reference KAD from kadtk, run in its own interpreter (#262).

pam

Per-clip, reference-free audio quality from PAM, run in an msclap interpreter (#262).

panel_page

A self-contained listening page for a human panel (#49): open it in a browser, rate, download.

prompt_match

Calibrate the prompt-match threshold of a text-audio embedder on a labelled set (#111).

reliability

Inter-rater reliability — Krippendorff's α + the judge-vs-human calibration guardrail.

retrieval

Tier-1 retrieval metrics + run assembly — pure stdlib, CI-safe, ranx-exact.

taste

The "tasteful weave" eval: does a sound designer produce a restrained, clean, well-placed result?

toolkit_driver

The script foley.eval.external_toolkit.ExternalToolkit runs inside a toolkit's interpreter.