File size: 5,301 Bytes
11a414e | 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | """Assay Engine: Form-Factor Baseline Matching, BIS Calculation & Dynamic Price Adjustments."""
from __future__ import annotations
import json
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
from .model_client import Detection
@dataclass
class AssayedBoard:
form_factor: str
board_integrity_score: float # BIS (0.0 to 100.0)
base_price: float
adjusted_price: float
price_floor: float
detected_counts: dict[str, int]
expected_counts: dict[str, int]
fraud_desoldered_pads: int
factory_unpopulated_pads: int
occluded_regions: int
status_band: str # "pristine" (>=85), "partial" (50-84), "severe" (<50)
discrepancy_status: str = "pass" # "pass", "advisory_flag", "critical_fraud_alert"
PROFILES_FILE = Path(__file__).parent / "board_profiles.json"
def load_board_profiles() -> dict:
if PROFILES_FILE.exists():
with open(PROFILES_FILE, "r", encoding="utf-8") as f:
return json.load(f)
return {}
def calculate_board_integrity_score(
detections: list[Detection],
form_factor: str = "desktop_atx",
profiles: dict | None = None,
) -> tuple[float, dict[str, int], dict[str, int], int, int, int]:
"""Calculate Board Integrity Score (BIS 0-100) using form factor baselines."""
if profiles is None:
profiles = load_board_profiles()
profile = profiles.get(form_factor) or profiles.get("default") or {
"price_floor": 0.35,
"expected_components": {"cpu_socket": 1, "gold_finger": 1, "ic_chip": 3, "tantalum_capacitor": 4},
"weights": {"cpu_socket": 0.40, "gold_finger": 0.25, "ic_chip": 0.20, "tantalum_capacitor": 0.15},
}
expected = profile.get("expected_components", {})
weights = profile.get("weights", {})
detected_counts: dict[str, int] = {}
fraud_pads = 0
factory_pads = 0
occluded = 0
for d in detections:
c = d.class_name
if c == "forcibly_desoldered_pad":
fraud_pads += 1
elif c == "factory_unpopulated_pad":
factory_pads += 1
elif c == "occluded_region":
occluded += 1
else:
detected_counts[c] = detected_counts.get(c, 0) + 1
# Weighted component presence ratio
weighted_score = 0.0
total_weight = sum(weights.values()) or 1.0
for cls_name, exp_num in expected.items():
if exp_num > 0:
det_num = detected_counts.get(cls_name, 0)
ratio = min(1.0, det_num / float(exp_num))
else:
ratio = 1.0 # Class not expected on this form factor (e.g. CPU socket on GPU)
w = weights.get(cls_name, 0.0)
weighted_score += w * ratio
# Normalize to 100
bis_raw = (weighted_score / total_weight) * 100.0
# Penalize for forcibly desoldered pads (-25 points per fraud pad)
fraud_penalty = fraud_pads * 25.0
final_bis = max(0.0, min(100.0, bis_raw - fraud_penalty))
return final_bis, detected_counts, expected, fraud_pads, factory_pads, occluded
def calculate_adjusted_scrap_price(
bis: float,
base_price: float,
form_factor: str = "desktop_atx",
profiles: dict | None = None,
) -> tuple[float, float]:
"""Calculate dynamic scrap purchase price based on category scrap grade floor:
Adjusted Price = Base Price * (Floor + (1 - Floor) * (BIS / 100))
"""
if profiles is None:
profiles = load_board_profiles()
profile = profiles.get(form_factor) or profiles.get("default") or {"price_floor": 0.35}
price_floor = float(profile.get("price_floor", 0.35))
adjusted_price = base_price * (price_floor + (1.0 - price_floor) * (bis / 100.0))
return round(adjusted_price, 2), price_floor
def assess_bis_discrepancy(collector_bis: float, recycler_bis: float) -> str:
"""Assess mid-chain cannibalization fraud discrepancy bands.
< 12% BIS Drop -> pass (normal capture variance)
12-25% BIS Drop -> advisory_flag
> 25% BIS Drop -> critical_fraud_alert
"""
bis_drop = collector_bis - recycler_bis
if bis_drop < 12.0:
return "pass"
elif bis_drop <= 25.0:
return "advisory_flag"
else:
return "critical_fraud_alert"
def assay_pcb(
detections: list[Detection],
base_price: float = 100.0,
form_factor: str = "desktop_atx",
profiles: dict | None = None,
) -> AssayedBoard:
"""Complete Board Integrity Assay workflow."""
bis, det_counts, exp_counts, fraud_pads, factory_pads, occluded = calculate_board_integrity_score(
detections, form_factor, profiles
)
adj_price, floor = calculate_adjusted_scrap_price(bis, base_price, form_factor, profiles)
if bis >= 85.0:
status_band = "pristine"
elif bis >= 50.0:
status_band = "partial"
else:
status_band = "severe"
return AssayedBoard(
form_factor=form_factor,
board_integrity_score=round(bis, 1),
base_price=base_price,
adjusted_price=adj_price,
price_floor=floor,
detected_counts=det_counts,
expected_counts=exp_counts,
fraud_desoldered_pads=fraud_pads,
factory_unpopulated_pads=factory_pads,
occluded_regions=occluded,
status_band=status_band,
discrepancy_status="pass",
)
|