优化流程
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _score(expected: dict[str, Any], actual: dict[str, Any]) -> float | None:
|
||||
scores: list[float] = []
|
||||
for key in ("center_mm", "start_mm", "end_mm", "normal", "axis_direction"):
|
||||
if key in expected:
|
||||
left, right = expected[key], actual.get(key)
|
||||
if not isinstance(right, (list, tuple)) or len(left) != len(right): return None
|
||||
delta = math.sqrt(sum((float(a) - float(b)) ** 2 for a, b in zip(left, right)))
|
||||
scores.append(max(0.0, 1.0 - delta / 0.05))
|
||||
for key in ("radius_mm", "plane_offset_mm"):
|
||||
if key in expected:
|
||||
try: delta = abs(float(expected[key]) - float(actual[key]))
|
||||
except Exception: return None
|
||||
scores.append(max(0.0, 1.0 - delta / 0.05))
|
||||
return sum(scores) / len(scores) if scores else 0.0
|
||||
|
||||
|
||||
def bind_selector(kind: str, owner_feature_id: str, geometry: dict[str, Any], records: list[dict[str, Any]], *, minimum_score: float = 0.8) -> dict[str, Any]:
|
||||
candidates = []
|
||||
for record in records:
|
||||
owners = record.get("owner_feature_ids") or [record.get("feature_id")]
|
||||
if record.get("kind") != kind or owner_feature_id not in owners: continue
|
||||
score = _score(geometry, record.get("geometry") or {})
|
||||
if score is not None and score >= minimum_score: candidates.append((score, record))
|
||||
candidates.sort(key=lambda item: (-item[0], str(item[1].get("record_id"))))
|
||||
if not candidates: raise ValueError("selector_not_found")
|
||||
if len(candidates) > 1 and abs(candidates[0][0] - candidates[1][0]) <= 1e-9: raise ValueError("selector_ambiguous")
|
||||
score, record = candidates[0]
|
||||
return {"kind": kind, "owner_feature_id": owner_feature_id, "stable_id": record["record_id"], "snapshot_id": record["record_id"], "source": "cadfs_featurescript", "confidence": round(score, 6), "geometry": record.get("geometry") or geometry}
|
||||
Reference in New Issue
Block a user