Download CODE/evaluation/eval/view_entity_matching_v52_rc3.py from QLU-LLM/CAD: direct link, hf CLI and curl.
- Browser
- Download file 7.41 kB
-
https://huggingface.co/datasets/QLU-LLM/CAD/resolve/main/CODE/evaluation/eval/view_entity_matching_v52_rc3.py
- Command line
-
hf download hf://datasets/QLU-LLM/CAD/CODE/evaluation/eval/view_entity_matching_v52_rc3.py
-
curl -L -o view_entity_matching_v52_rc3.py https://huggingface.co/datasets/QLU-LLM/CAD/resolve/main/CODE/evaluation/eval/view_entity_matching_v52_rc3.py
7.41 kB
| #!/usr/bin/env python3 | |
| """RC3 matching context: bounded role search, complete extra-cluster accounting. | |
| RC2 deliberately limits semantic-role assignment to the eight strongest view | |
| clusters. RC3 keeps that complexity bound, then appends every omitted cluster | |
| to ``pred_views`` as an explicit extra. Consequently the existing geometry | |
| penalty over ``alignment.extra_predicted_indices`` covers all prediction | |
| clusters rather than only the role-search candidates. | |
| Every view also receives a parameter-stable ``layout_anchor``: the coordinate- | |
| wise median of LINE midpoints and CIRCLE/ARC centres. Changing a circle/arc | |
| radius therefore cannot move the anchor used by an RC3 layout component. | |
| """ | |
| from __future__ import annotations | |
| import copy | |
| from pathlib import Path | |
| from typing import Any, Mapping, Sequence | |
| import numpy as np | |
| try: | |
| from . import view_entity_matching_v52 as RC1 | |
| from . import view_entity_matching_v52_rc2 as RC2 | |
| except ImportError: # direct evaluator import | |
| import view_entity_matching_v52 as RC1 | |
| import view_entity_matching_v52_rc2 as RC2 | |
| SCHEMA_VERSION = "view-entity-matching-v5.2-rc3" | |
| MAX_PREDICTED_VIEW_CANDIDATES = RC2.MAX_PREDICTED_VIEW_CANDIDATES | |
| ROLE_CENTER_ACCEPT_FRAC = RC2.ROLE_CENTER_ACCEPT_FRAC | |
| CANONICAL_REL_TOL = RC2.CANONICAL_REL_TOL | |
| ROBUST_SIZE_CAP_MULTIPLIER = RC2.ROBUST_SIZE_CAP_MULTIPLIER | |
| LAYOUT_ANCHOR_DEFINITION = "coordinate_median(line_midpoints,circle_centers,arc_centers)" | |
| def _layout_anchor(entities: Mapping[str, Any]) -> tuple[list[float] | None, int]: | |
| points: list[tuple[float, float]] = [] | |
| for line in entities.get("lines", []): | |
| if isinstance(line, Sequence) and len(line) >= 4: | |
| points.append( | |
| ( | |
| 0.5 * (float(line[0]) + float(line[2])), | |
| 0.5 * (float(line[1]) + float(line[3])), | |
| ) | |
| ) | |
| for family in ("circles", "arcs"): | |
| for primitive in entities.get(family, []): | |
| if isinstance(primitive, Sequence) and len(primitive) >= 2: | |
| points.append((float(primitive[0]), float(primitive[1]))) | |
| if not points: | |
| return None, 0 | |
| anchor = np.median(np.asarray(points, dtype=float), axis=0) | |
| return [float(anchor[0]), float(anchor[1])], len(points) | |
| def _decorate_view(view: Mapping[str, Any]) -> dict[str, Any]: | |
| result = copy.deepcopy(dict(view)) | |
| entities = result.get("entities") | |
| anchor, support = _layout_anchor(entities if isinstance(entities, Mapping) else {}) | |
| result["layout_anchor"] = anchor | |
| result["layout_anchor_support_count"] = int(support) | |
| result["layout_anchor_source"] = LAYOUT_ANCHOR_DEFINITION | |
| return result | |
| def _decorate_context(context: dict[str, Any]) -> dict[str, Any]: | |
| pred_all_raw = context.get("pred_views_all") | |
| pred_selected_raw = context.get("pred_views") | |
| gt_raw = context.get("gt_by_role") | |
| pred_all = ( | |
| [_decorate_view(view) for view in pred_all_raw] | |
| if isinstance(pred_all_raw, Sequence) | |
| else [] | |
| ) | |
| pred_selected = ( | |
| [_decorate_view(view) for view in pred_selected_raw] | |
| if isinstance(pred_selected_raw, Sequence) | |
| else [] | |
| ) | |
| gt_by_role = ( | |
| {str(role): _decorate_view(view) for role, view in gt_raw.items()} | |
| if isinstance(gt_raw, Mapping) | |
| else {} | |
| ) | |
| alignment = context.get("alignment") | |
| if not isinstance(alignment, dict): | |
| context["pred_views_all"] = pred_all | |
| context["pred_views"] = pred_selected | |
| context["gt_by_role"] = gt_by_role | |
| context["layout_anchors"] = { | |
| "definition": LAYOUT_ANCHOR_DEFINITION, | |
| "predicted": [], | |
| "target": { | |
| role: view.get("layout_anchor") for role, view in gt_by_role.items() | |
| }, | |
| } | |
| return context | |
| selected_original = list(alignment.get("selected_original_indices", [])) | |
| omitted_original = list(alignment.get("omitted_original_indices", [])) | |
| # RC2's pred_views are ordered by ranking, whereas pred_views_all retains | |
| # original cluster order. Keep the selected prefix untouched so every | |
| # existing role_to_pred index remains valid. | |
| appended: list[dict[str, int]] = [] | |
| for raw_original in omitted_original: | |
| try: | |
| original = int(raw_original) | |
| except (TypeError, ValueError): | |
| continue | |
| if not 0 <= original < len(pred_all): | |
| continue | |
| predicted_index = len(pred_selected) | |
| pred_selected.append(copy.deepcopy(pred_all[original])) | |
| appended.append( | |
| {"original_index": original, "predicted_index": predicted_index} | |
| ) | |
| existing_extra = { | |
| int(index) | |
| for index in alignment.get("extra_predicted_indices", []) | |
| if isinstance(index, int) or str(index).lstrip("-").isdigit() | |
| } | |
| appended_extra = {item["predicted_index"] for item in appended} | |
| alignment["extra_predicted_indices"] = sorted(existing_extra | appended_extra) | |
| alignment["omitted_appended_indices"] = appended | |
| alignment["role_assignment_candidate_count"] = len(selected_original) | |
| alignment["role_assignment_candidate_limit"] = MAX_PREDICTED_VIEW_CANDIDATES | |
| alignment["all_predicted_cluster_count"] = len(pred_all) | |
| alignment["all_extra_clusters_accounted_for"] = bool( | |
| len(pred_selected) == len(pred_all) | |
| and len(appended) == len(omitted_original) | |
| ) | |
| alignment["role_assignment_uses_appended_clusters"] = False | |
| context["schema_version"] = SCHEMA_VERSION | |
| context["alignment"] = alignment | |
| context["pred_views_all"] = pred_all | |
| context["pred_views"] = pred_selected | |
| context["gt_by_role"] = gt_by_role | |
| reverse_roles = { | |
| int(index): str(role) | |
| for role, index in alignment.get("role_to_pred", {}).items() | |
| if isinstance(index, int) | |
| } | |
| context["layout_anchors"] = { | |
| "definition": LAYOUT_ANCHOR_DEFINITION, | |
| "parameter_invariance": ["circle_radius", "arc_radius", "arc_angles"], | |
| "predicted": [ | |
| { | |
| "predicted_index": index, | |
| "role": reverse_roles.get(index), | |
| "is_extra": index in set(alignment["extra_predicted_indices"]), | |
| "anchor": view.get("layout_anchor"), | |
| "support_count": view.get("layout_anchor_support_count", 0), | |
| } | |
| for index, view in enumerate(pred_selected) | |
| ], | |
| "target": { | |
| role: { | |
| "anchor": view.get("layout_anchor"), | |
| "support_count": view.get("layout_anchor_support_count", 0), | |
| } | |
| for role, view in gt_by_role.items() | |
| }, | |
| } | |
| return context | |
| def build_pair_context( | |
| pred: str | Path, | |
| gt: str | Path, | |
| *, | |
| sidecar: Mapping[str, Any] | None, | |
| ) -> dict[str, Any]: | |
| """Return the RC2 context with all clusters and stable layout anchors.""" | |
| context = RC2.build_pair_context(pred, gt, sidecar=sidecar) | |
| return _decorate_context(context) | |
| match_view_primitives = RC1.match_view_primitives | |
| role_local_transform = RC1.role_local_transform | |
| __all__ = [ | |
| "CANONICAL_REL_TOL", | |
| "LAYOUT_ANCHOR_DEFINITION", | |
| "MAX_PREDICTED_VIEW_CANDIDATES", | |
| "ROBUST_SIZE_CAP_MULTIPLIER", | |
| "ROLE_CENTER_ACCEPT_FRAC", | |
| "SCHEMA_VERSION", | |
| "build_pair_context", | |
| "match_view_primitives", | |
| "role_local_transform", | |
| ] | |