fix: resolve equipment detection decline by capturing bare equipment tags, preserving sub-train parallel equipment, and fixing PSV host line vector snapping to eliminate 26-CX-9021 false attribution

This commit is contained in:
Abhinav Gupta
2026-09-03 11:38:36 +05:30
parent 5589261c11
commit 0722877947
4 changed files with 115 additions and 23 deletions
+14 -6
View File
@@ -194,17 +194,25 @@ class CompilerAgent(BaseAgent):
compiled = []
seen_equip: dict = {} # canonical key → EquipmentItem (deduplication)
eq_tags = [t for t in texts if t["classification"] == "EQUIPMENT_TAG"]
for eq in eq_tags:
tag = eq["tag"]
# Deduplicate by canonical key (strip project prefix AND trailing unit suffix)
canon_key = re.sub(r'^\d{2,3}-', '', tag.upper())
canon_key = re.sub(r'-[A-Z][A-Z0-9]{0,3}$', '', canon_key)
base_key = re.sub(r'-[A-Z0-9]{1,4}$', '', canon_key)
# Check if this exact canonical key or an exact base prefix exists
matched_key = None
if canon_key in seen_equip:
if len(tag) > len(seen_equip[canon_key].tag):
seen_equip[canon_key].tag = tag
matched_key = canon_key
elif base_key in seen_equip and base_key != canon_key:
# Merge bare base tag e.g. HA-911 into more specific 26-HA-911-C01
matched_key = base_key
elif any(k.startswith(canon_key + "-") for k in seen_equip):
# An existing specific tag (e.g. 26-HA-911-C01) is already registered for this bare tag (HA-911)
continue
if matched_key:
if len(tag) > len(seen_equip[matched_key].tag):
seen_equip[matched_key].tag = tag
continue
coords = None
+33 -7
View File
@@ -217,7 +217,8 @@ def trace_lines_and_connections(
ix = float(iattrs.get("pos_x", 0.5)) if iattrs.get("pos_x") else 0.5
iy = float(iattrs.get("pos_y", 0.5)) if iattrs.get("pos_y") else 0.5
closest_line = _find_closest_tag(ix, iy, line_items, max_dist=0.45)
# Snap instrument directly to continuous polyline vector traces
closest_line = _find_closest_line_segment(ix, iy, traces, line_items, max_dist=0.08)
if closest_line:
rkey = (itag, closest_line, "MONITORS")
if rkey not in existing_rels:
@@ -228,7 +229,8 @@ def trace_lines_and_connections(
"rel_type": "MONITORS"
})
else:
closest_eq = _find_closest_tag(ix, iy, equipment, max_dist=0.50)
# Direct equipment nozzle attachment (tight threshold)
closest_eq = _find_closest_tag(ix, iy, equipment, max_dist=0.06)
if closest_eq:
rkey = (itag, closest_eq, "MONITORS")
if rkey not in existing_rels:
@@ -239,7 +241,7 @@ def trace_lines_and_connections(
"rel_type": "MONITORS"
})
# C. Map PSVs to Equipment or Lines
# C. Map PSVs to Host Piping Lines & Relief Destinations (Eliminates distant equipment false snapping)
for psv in psvs:
ptag = psv.get("tag")
if not ptag:
@@ -248,16 +250,40 @@ def trace_lines_and_connections(
px = float(pattrs.get("pos_x", 0.5)) if pattrs.get("pos_x") else 0.5
py = float(pattrs.get("pos_y", 0.5)) if pattrs.get("pos_y") else 0.5
closest_target = _find_closest_tag(px, py, equipment, max_dist=0.50) or _find_closest_tag(px, py, line_items, max_dist=0.45)
if closest_target:
rkey = (ptag, closest_target, "INSTALLED_ON")
# 1. PSVs are physically installed on Process Piping Lines (prioritize vector line trace)
closest_line = _find_closest_line_segment(px, py, traces, line_items, max_dist=0.08)
if closest_line:
rkey = (ptag, closest_line, "INSTALLED_ON")
if rkey not in existing_rels:
existing_rels.add(rkey)
relations.append({
"source_tag": ptag,
"target_tag": closest_target,
"target_tag": closest_line,
"rel_type": "INSTALLED_ON"
})
else:
# Direct vessel nozzle top mount (tight threshold only)
closest_eq = _find_closest_tag(px, py, equipment, max_dist=0.04)
if closest_eq:
rkey = (ptag, closest_eq, "INSTALLED_ON")
if rkey not in existing_rels:
existing_rels.add(rkey)
relations.append({
"source_tag": ptag,
"target_tag": closest_eq,
"rel_type": "INSTALLED_ON"
})
# 2. Discharge Relief Destination Header (HP Flare Header)
relief_dest = pattrs.get("relief_destination") or "HP Flare Header"
rkey_dest = (ptag, relief_dest, "RELIEVES_TO")
if rkey_dest not in existing_rels:
existing_rels.add(rkey_dest)
relations.append({
"source_tag": ptag,
"target_tag": relief_dest,
"rel_type": "RELIEVES_TO"
})
# D. Map Lines to Equipment Endpoint Nozzles (CONNECTS_TO)
for litem in line_items:
+34 -9
View File
@@ -95,22 +95,22 @@ _EQUIP_PREFIX_ALLOWLIST = {
# Compressors, blowers, turbines, motors
'K', 'KA', 'KB', 'KC', 'KT', 'CM', 'CP', 'MT', 'MG',
# Heat exchangers, coolers, heaters, furnaces
'E', 'EA', 'EB', 'HX', 'HE', 'BA',
'E', 'EA', 'EB', 'EC', 'HA', 'HB', 'HC', 'HX', 'HE', 'BA',
# Vessels, drums, tanks, columns
'V', 'VA', 'VB', 'TK', 'T', 'D', 'DA', 'C', 'R', 'DR',
'V', 'VA', 'VB', 'VC', 'TK', 'T', 'D', 'DA', 'DB', 'C', 'CA', 'CB', 'R', 'DR',
# Pumps
'P', 'PA', 'PB', 'PC', 'G', 'GA', 'PM', 'PU',
'P', 'PA', 'PB', 'PC', 'G', 'GA', 'GB', 'PM', 'PU',
# Filters, separators, strainers
'F', 'FA', 'FB', 'ST', 'CX',
'F', 'FA', 'FB', 'FC', 'FL', 'SA', 'SB', 'SC', 'SE', 'ST', 'CX',
# Skids, packages
'SK', 'PK',
'SK', 'PK', 'PKG', 'KZ', 'ME',
# General mechanical
'M', 'MA', 'MB', 'U', 'UA', 'W', 'WA',
}
# Generic 1-3 letter + number equipment — guarded with prefix allowlist
# Generic 1-3 letter + 3-5 digit equipment — guarded with prefix allowlist (supports sub-equipment e.g. HA-911-C01)
_GENERIC_EQUIP_PATTERN = re.compile(
r'\b([A-Z]{1,3}-\d{2,5}[A-Z]?(?:/[A-Z])?)\b', re.IGNORECASE
r'\b([A-Z]{1,3}-\d{3,5}[A-Z]?(?:-[A-Z0-9]{1,4})?(?:/[A-Z])?)\b', re.IGNORECASE
)
# Patterns that must NOT be classified as equipment (spec codes, sheet refs, etc.)
@@ -584,6 +584,15 @@ def classify_paddle_results(
tag = re.sub(r'\s+', '', raw_tag_clean).upper()
if not tag or len(tag) < 4:
continue
# Guard against equipment tags without size prefix (e.g. HA-911-C01, KA-902-M01, TK-101-A)
parts = tag.split('-')
has_size = bool(parts and (re.match(r'^\d', parts[0]) or '"' in parts[0] or "'" in parts[0] or 'MM' in parts[0] or 'DN' in parts[0]))
if not has_size and len(parts) >= 2:
prefix = parts[0]
if prefix in _EQUIP_PREFIX_ALLOWLIST:
continue # Route to equipment pattern search instead
flag_reason = None
tag_conf = conf
size_valid, size_flag = _validate_line_tag_size(tag)
@@ -659,10 +668,10 @@ def classify_paddle_results(
}
if code in _VALVE_FUNCTION_CODES and len(re.sub(r'\D', '', seq)) >= 3:
cat = 'VALVE_TAG'
elif code in _INSTRUMENT_CODES:
cat = 'INSTRUMENT_TAG'
elif code in _EQUIPMENT_CODES:
cat = 'EQUIPMENT_TAG'
elif code in _INSTRUMENT_CODES:
cat = 'INSTRUMENT_TAG'
elif len(seq) >= 4:
cat = 'INSTRUMENT_TAG'
else:
@@ -675,6 +684,22 @@ def classify_paddle_results(
found[full_tag] = _make_item(full_tag, cat, conf, it_copy)
item_added = True
# Bare equipment tags without project prefix (e.g. KA-902, KA-901, CX-9021, CX-9011, HA-911, TK-901, FA-9015)
for m in _GENERIC_EQUIP_PATTERN.finditer(t):
full_tag = m.group(1).upper()
code_m = re.match(r'^([A-Z]{1,3})', full_tag)
code = code_m.group(1) if code_m else ""
if (code in _EQUIP_PREFIX_ALLOWLIST
and not _EQUIP_REJECT_PATTERN.match(full_tag)
and not _SPEC_CODE_PATTERN.match(full_tag)
and not _SERVICE_CODE_PATTERN.match(full_tag)):
if full_tag not in found:
it_copy = dict(item)
if is_ref_context:
it_copy["is_reference"] = True
found[full_tag] = _make_item(full_tag, 'EQUIPMENT_TAG', conf, it_copy)
item_added = True
# Bare instrument tags — Defect 3: setpoint negative-context guard for 3-digit setpoints
for m in _BARE_INSTRUMENT_SEARCH.finditer(t):
tag = m.group(1).upper()
+34 -1
View File
@@ -223,7 +223,40 @@ def run_tests():
assert_test("27-PY-0001BA" in audit_tags and audit_tags["27-PY-0001BA"]["classification"] == "INSTRUMENT_TAG", "Extracted sibling instrument 27-PY-0001BA")
assert_test("27-PY-0001BB" in audit_tags and audit_tags["27-PY-0001BB"]["classification"] == "INSTRUMENT_TAG", "Extracted sibling instrument 27-PY-0001BB")
assert_test("26-ST-9002" in audit_tags and audit_tags["26-ST-9002"]["classification"] == "EQUIPMENT_TAG", "Recognized suction strainer '9002 S 26' as 26-ST-9002")
assert_test(rectify_ocr_typos("005BARG") == "0.005 BARG", "Restored dropped decimal point '005BARG' -> '0.005 BARG'")
print("\n" + "=" * 70)
print("7. TESTING BARE EQUIPMENT EXTRACTION & TOPOLOGICAL PSV SNAPPING")
print("=" * 70)
# A. Bare equipment extraction (without 26- prefix)
bare_eq_items = [
{"text": "KA-902", "confidence": 0.95, "attributes": {}},
{"text": "CX-9021", "confidence": 0.95, "attributes": {}},
{"text": "HA-911-C01", "confidence": 0.95, "attributes": {}},
{"text": "HA-911-C02", "confidence": 0.95, "attributes": {}},
]
classified_bare_eq = classify_paddle_results(bare_eq_items, "PID")
bare_eq_tags = {c["tag"]: c for c in classified_bare_eq}
assert_test("KA-902" in bare_eq_tags and bare_eq_tags["KA-902"]["classification"] == "EQUIPMENT_TAG", "Captured bare compressor 'KA-902' without project prefix")
assert_test("CX-9021" in bare_eq_tags and bare_eq_tags["CX-9021"]["classification"] == "EQUIPMENT_TAG", "Captured bare filter 'CX-9021' without project prefix")
# B. Parallel equipment retention in compiler
compiled_parallel_eq = ca._compile_equipment(classified_bare_eq, [])
parallel_tags = [e.tag for e in compiled_parallel_eq]
assert_test("HA-911-C01" in parallel_tags and "HA-911-C02" in parallel_tags, "Retained BOTH parallel exchanger coolers HA-911-C01 & HA-911-C02 (not collapsed)")
# C. PSV Topology (installed_on Line, NOT distant coalescing filter 26-CX-9021)
from src.utils.line_tracer import trace_lines_and_connections
topo_texts = [
{"classification": "PSV_TAG", "tag": "PSV-9027A", "attributes": {"pos_x": 0.45, "pos_y": 0.50}},
{"classification": "LINE_TAG", "tag": '10"-VF-43-9027-AS20S-00', "attributes": {"pos_x": 0.44, "pos_y": 0.50}},
{"classification": "EQUIPMENT_TAG", "tag": "26-CX-9021", "attributes": {"pos_x": 0.80, "pos_y": 0.80}}, # Distant equipment
]
topo_result = trace_lines_and_connections(None, topo_texts, [])
topo_rels = topo_result["relations"]
psv_rels = [r for r in topo_rels if r["source_tag"] == "PSV-9027A"]
psv_line_installed = any(r["target_tag"] == '10"-VF-43-9027-AS20S-00' and r["rel_type"] == "INSTALLED_ON" for r in psv_rels)
psv_cx_installed = any(r["target_tag"] == "26-CX-9021" for r in psv_rels)
assert_test(psv_line_installed, "PSV-9027A correctly mapped as installed_on host line '10\"-VF-43-9027-AS20S-00'")
assert_test(not psv_cx_installed, "PSV-9027A NOT falsely mapped to distant filter '26-CX-9021'")
print("\n" + "=" * 70)
print(f"FINAL RESULT: {total_passed}/{total_tests} Tests Passed (100% Precision Verified Across All Categories)")