Add source structure metric breakdown

This commit is contained in:
Meta Vens 2026-04-23 23:31:58 +01:00
commit 044ee59948
7 changed files with 140 additions and 1 deletions

View file

@ -7,12 +7,14 @@ from repixelizer.metrics import (
foreground_adjacency_error,
foreground_edge_concentration,
foreground_edge_position_error,
foreground_edge_support_breakdown,
foreground_exact_match_ratio,
foreground_motif_error,
foreground_reconstruction_error,
foreground_stroke_wobble_error,
source_lattice_consistency_breakdown,
reconstruction_error,
source_lattice_consistency_breakdown,
source_structure_breakdown,
)
@ -112,6 +114,46 @@ def test_stroke_wobble_metric_penalizes_local_line_jitter() -> None:
assert foreground_stroke_wobble_error(original, wobbly) > 0.0
def test_edge_support_breakdown_rewards_nearby_edge_recall() -> None:
source = np.zeros((9, 9, 4), dtype=np.float32)
source[2:7, 4] = np.asarray([1.0, 1.0, 1.0, 1.0], dtype=np.float32)
stable = source.copy()
shifted = np.zeros_like(source)
shifted[2:7, 5] = np.asarray([1.0, 1.0, 1.0, 1.0], dtype=np.float32)
missing = np.zeros_like(source)
stable_support = foreground_edge_support_breakdown(stable, source)
shifted_support = foreground_edge_support_breakdown(shifted, source)
missing_support = foreground_edge_support_breakdown(missing, source)
assert stable_support["f1"] >= shifted_support["f1"] > missing_support["f1"]
def test_source_structure_breakdown_prefers_exact_and_supported_structure() -> None:
source = np.zeros((9, 9, 4), dtype=np.float32)
source[2:7, 4] = np.asarray([1.0, 1.0, 1.0, 1.0], dtype=np.float32)
exact = source.copy()
shifted = np.zeros_like(source)
shifted[2:7, 5] = np.asarray([1.0, 1.0, 1.0, 1.0], dtype=np.float32)
blurred = np.zeros_like(source)
blurred[2:7, 3] = np.asarray([0.4, 0.4, 0.4, 1.0], dtype=np.float32)
blurred[2:7, 4] = np.asarray([0.7, 0.7, 0.7, 1.0], dtype=np.float32)
blurred[2:7, 5] = np.asarray([0.4, 0.4, 0.4, 1.0], dtype=np.float32)
exact_score = source_structure_breakdown(source, exact)["score"]
shifted_score = source_structure_breakdown(source, shifted)["score"]
blurred_score = source_structure_breakdown(source, blurred)["score"]
assert exact_score < shifted_score
assert exact_score < blurred_score
def test_source_lattice_consistency_prefers_matching_cell_structure() -> None:
lowres = np.asarray(
[

View file

@ -36,6 +36,8 @@ def test_pipeline_writes_output_and_diagnostics(tmp_path: Path) -> None:
run_json = json.loads((diagnostics_dir / "run.json").read_text(encoding="utf-8"))
assert set(run_json["source_fidelity"].keys()) == {"snap_initial", "solver_target", "final_output"}
assert "source_structure" in run_json
assert {"edge_f1", "exact_match_ratio", "score"} <= set(run_json["source_structure"].keys())
assert "phase_rerank_candidates" in run_json
assert {"snap", "relax_handoff", "relax_mode", "final_output"} <= set(run_json["optimizer_displacement"].keys())
assert result.output_rgba.shape[0] == result.inference.target_height