Rajan Sharma
Create unit_validator.py
4b415b1 verified
import math
import json
def _in_range(val, min_v, max_v):
return (val is None) or (min_v <= val <= max_v)
def assert_valid(data: dict, global_policy_path: str):
with open(global_policy_path, "r", encoding="utf-8") as f:
policy = json.load(f)
ranges = policy["numeric_ranges"]
# Very lightweight checks; the output must contain numeric deltas in the text strings.
# Here, we simply ensure required numeric fields in cost/capacity are non-negative,
# and clinical sections contain unit tokens.
cost = data.get("cost", {})
for k in ["startup_per_client", "ongoing_per_client", "total_per_client", "total_for_n"]:
v = cost.get(k)
if v is None or v < 0:
raise ValueError(f"UnitValidator: cost.{k} must be >= 0")
cap = data.get("capacity", {})
if cap.get("total_clients", 0) < 0:
raise ValueError("UnitValidator: capacity.total_clients must be >= 0")
# Check presence of unit tokens in clinical text
clin = data.get("clinical_benefits", {})
di = (clin.get("diabetes_group") or "").lower()
ndi = (clin.get("non_diabetes_group") or "").lower()
must_tokens = ["%", "mmhg", "kg/m²"]
if not any(tok in di for tok in must_tokens):
raise ValueError("UnitValidator: diabetes_group must include units (%, mmHg, kg/m² where applicable).")
if not any(tok in ndi for tok in must_tokens):
raise ValueError("UnitValidator: non_diabetes_group must include units (%, mmHg, kg/m² where applicable).")