Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Create unit_validator.py
Browse files- validators/unit_validator.py +33 -0
validators/unit_validator.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
def _in_range(val, min_v, max_v):
|
| 5 |
+
return (val is None) or (min_v <= val <= max_v)
|
| 6 |
+
|
| 7 |
+
def assert_valid(data: dict, global_policy_path: str):
|
| 8 |
+
with open(global_policy_path, "r", encoding="utf-8") as f:
|
| 9 |
+
policy = json.load(f)
|
| 10 |
+
|
| 11 |
+
ranges = policy["numeric_ranges"]
|
| 12 |
+
# Very lightweight checks; the output must contain numeric deltas in the text strings.
|
| 13 |
+
# Here, we simply ensure required numeric fields in cost/capacity are non-negative,
|
| 14 |
+
# and clinical sections contain unit tokens.
|
| 15 |
+
cost = data.get("cost", {})
|
| 16 |
+
for k in ["startup_per_client", "ongoing_per_client", "total_per_client", "total_for_n"]:
|
| 17 |
+
v = cost.get(k)
|
| 18 |
+
if v is None or v < 0:
|
| 19 |
+
raise ValueError(f"UnitValidator: cost.{k} must be >= 0")
|
| 20 |
+
|
| 21 |
+
cap = data.get("capacity", {})
|
| 22 |
+
if cap.get("total_clients", 0) < 0:
|
| 23 |
+
raise ValueError("UnitValidator: capacity.total_clients must be >= 0")
|
| 24 |
+
|
| 25 |
+
# Check presence of unit tokens in clinical text
|
| 26 |
+
clin = data.get("clinical_benefits", {})
|
| 27 |
+
di = (clin.get("diabetes_group") or "").lower()
|
| 28 |
+
ndi = (clin.get("non_diabetes_group") or "").lower()
|
| 29 |
+
must_tokens = ["%", "mmhg", "kg/m²"]
|
| 30 |
+
if not any(tok in di for tok in must_tokens):
|
| 31 |
+
raise ValueError("UnitValidator: diabetes_group must include units (%, mmHg, kg/m² where applicable).")
|
| 32 |
+
if not any(tok in ndi for tok in must_tokens):
|
| 33 |
+
raise ValueError("UnitValidator: non_diabetes_group must include units (%, mmHg, kg/m² where applicable).")
|