File size: 5,231 Bytes
15d1f1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
"""Final fail-closed reliability gate for v5.2-rc5 layout evidence."""

from __future__ import annotations

import copy
from pathlib import Path
from typing import Any, Mapping

try:
    from . import eval_geometric052_rc5 as RC5
except ImportError:  # direct evaluator import
    import eval_geometric052_rc5 as RC5


METRIC_VERSION = "v5.2-rc5f"
SCHEMA_VERSION = "edp-geometric-evaluator-v5.2-rc5f"
ENGINEERING_THRESHOLDS = dict(RC5.ENGINEERING_THRESHOLDS)
PHYSICAL_SUPPORTED_SEMANTICS = RC5.PHYSICAL_SUPPORTED_SEMANTICS


def _annotate_reliability(role_offsets: Mapping[str, Any]) -> list[str]:
    unreliable: list[str] = []
    for role, raw_detail in role_offsets.items():
        if not isinstance(raw_detail, dict):
            unreliable.append(str(role))
            continue
        detail = raw_detail
        try:
            support = int(detail.get("support_count", 0) or 0)
            predicted = int(detail.get("predicted_anchor_count", 0) or 0)
            target = int(detail.get("target_anchor_count", 0) or 0)
        except (TypeError, ValueError):
            support, predicted, target = 0, 0, 0
        families = detail.get("matched_by_family", {})
        circular = bool(
            isinstance(families, Mapping)
            and (
                int(families.get("circle", 0) or 0) > 0
                or int(families.get("arc", 0) or 0) > 0
            )
        )
        fallback = bool(detail.get("fallback_used"))
        if support >= 2:
            reliable = True
            mode = "multi_matched_primitive_pairs"
        elif support == 1 and (circular or (predicted == 1 and target == 1)):
            reliable = True
            mode = (
                "single_matched_circular_center"
                if circular
                else "single_unique_primitive_anchor"
            )
        elif support == 0 and fallback and predicted >= 2 and target >= 2:
            reliable = True
            mode = "fallback_parameter_stable_view_anchor"
        else:
            reliable = False
            mode = "insufficient_layout_evidence"
            unreliable.append(str(role))
        detail["reliable"] = reliable
        detail["support_mode"] = mode
    return unreliable


def score_dxf_pair(
    pred: str | Path,
    gt: str | Path,
    sidecar: Mapping[str, Any] | None = None,
    category: str | None = None,
    **kwargs: Any,
) -> dict[str, Any]:
    result = RC5.score_dxf_pair(
        pred, gt, sidecar=sidecar, category=category, **kwargs
    )
    result["schema_version"] = SCHEMA_VERSION
    result["metric_version"] = METRIC_VERSION
    result.setdefault("metric_definition", {})["layout_role_reliability_policy"] = {
        "support_ge_2": "reliable",
        "support_eq_1": "reliable_only_for_circular_or_unique_single_anchor",
        "support_eq_0": "reliable_only_for_fallback_with_pred_and_gt_inventory_ge_2",
        "failure_policy": "active_layout_score_zero",
    }

    layout = result.get("components", {}).get("layout", {})
    details = layout.get("details", {}) if isinstance(layout, Mapping) else {}
    role_offsets = details.get("role_offsets", {}) if isinstance(details, Mapping) else {}
    unreliable = (
        _annotate_reliability(role_offsets)
        if isinstance(role_offsets, Mapping)
        else []
    )
    if unreliable:
        preserved = copy.deepcopy(dict(details))
        preserved.update(
            {
                "role_offsets": copy.deepcopy(role_offsets),
                "unreliable_roles": unreliable,
                "prior_layout_score": layout.get("score"),
                "prior_layout_reason": layout.get("reason"),
                "core_component_fail_closed": True,
            }
        )
        replacement = RC5.RC3.BASE._component(
            0.0, True, "insufficient_layout_evidence", **preserved
        )
        result["components"]["layout"] = replacement
        prompt_components = result["online_reward_prompt_masked"]["components"]
        prompt_components["layout"] = copy.deepcopy(replacement)
        atomic = result.get("diagnostics", {}).get("atomic_geometry", {})
        result["verified_reward_full_gt"] = RC5.RC3._scoped_hierarchical_reward(
            result["components"], atomic, scope="offline_full_gt_executed_dxf"
        )
        prompt_reward = RC5.RC3._scoped_hierarchical_reward(
            prompt_components, atomic, scope="prompt_masked_actual_geometry"
        )
        result["online_reward_prompt_masked"] = {
            **prompt_reward,
            "components": prompt_components,
        }
        result["benchmark"] = RC5.RC3._benchmark(result["components"])
        result["engineering_success"] = result["benchmark"]["engineering_success"]
    elif isinstance(role_offsets, Mapping) and role_offsets:
        result["online_reward_prompt_masked"]["components"]["layout"] = copy.deepcopy(
            layout
        )

    release = result.get("diagnostics", {}).get("release_status")
    if isinstance(release, dict):
        release["candidate"] = METRIC_VERSION
    return result


__all__ = [
    "ENGINEERING_THRESHOLDS",
    "METRIC_VERSION",
    "PHYSICAL_SUPPORTED_SEMANTICS",
    "SCHEMA_VERSION",
    "score_dxf_pair",
]