File size: 8,597 Bytes
492569d
5d68d4f
694d37e
5d68d4f
e8e5773
5d68d4f
 
 
 
492569d
5d68d4f
 
 
 
 
 
 
 
 
492569d
5d68d4f
492569d
5d68d4f
492569d
5d68d4f
 
 
492569d
 
 
 
5d68d4f
 
492569d
 
 
 
 
 
5d68d4f
 
 
 
 
 
 
 
 
 
 
e8e5773
492569d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e5773
 
5d68d4f
 
492569d
5d68d4f
492569d
 
 
5d68d4f
 
 
 
 
da99b31
5d68d4f
 
e8e5773
 
5d68d4f
 
 
 
 
 
e8e5773
 
5d68d4f
 
 
e8e5773
5d68d4f
 
 
 
492569d
 
 
5d68d4f
 
 
 
 
 
492569d
 
 
 
 
5d68d4f
 
 
 
 
 
 
 
492569d
5d68d4f
492569d
5d68d4f
 
 
 
 
 
492569d
5d68d4f
 
 
 
 
 
 
 
 
 
 
 
492569d
 
5d68d4f
 
 
492569d
5d68d4f
 
 
 
 
 
 
 
 
 
 
 
 
492569d
5d68d4f
 
492569d
5d68d4f
 
492569d
 
 
 
5d68d4f
 
 
492569d
5d68d4f
 
492569d
5d68d4f
 
492569d
 
 
 
5d68d4f
 
 
 
 
492569d
5d68d4f
492569d
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# scenario_engine.py
from __future__ import annotations
from typing import Dict, List, Any, Tuple, Optional
import re, math, json, ast
import numpy as np
import pandas as pd
from schema import ScenarioPlan, TaskPlan
from column_resolver import resolve_cols

# Allowed safe functions
_ALLOWED_FUNCS = {
    "abs": abs, "round": round, "sqrt": math.sqrt, "log": math.log, "exp": math.exp,
    "min": np.minimum, "max": np.maximum,
    "mean": np.mean, "avg": np.mean, "median": np.median, "sum": np.sum,
    "count": lambda x: np.size(x),
    "p50": lambda x: np.percentile(x, 50), "p75": lambda x: np.percentile(x, 75),
    "p90": lambda x: np.percentile(x, 90), "p95": lambda x: np.percentile(x, 95),
}

# -------- SAFE EXPRESSION PARSER --------
class _SafeExpr(ast.NodeTransformer):
    def __init__(self, allowed: set): self.allowed = allowed
    def visit_Name(self, node):
        if node.id not in self.allowed and node.id not in ("True","False","None"):
            raise ValueError(f"Unknown name: {node.id}")
        return node
    def visit_Call(self, node):
        if not isinstance(node.func, ast.Name):
            raise ValueError("Only simple calls allowed")
        if node.func.id not in _ALLOWED_FUNCS:
            raise ValueError(f"Function not allowed: {node.func.id}")
        return self.generic_visit(node)
    def generic_visit(self, node):
        allowed = (
            ast.Expression, ast.BoolOp, ast.BinOp, ast.UnaryOp, ast.Compare, ast.Call, ast.Name,
            ast.Load, ast.Constant, ast.And, ast.Or, ast.Not, ast.Add, ast.Sub, ast.Mult, ast.Div,
            ast.Mod, ast.Pow, ast.FloorDiv, ast.Eq, ast.NotEq, ast.Lt, ast.LtE, ast.Gt, ast.GtE,
            ast.USub, ast.UAdd
        )
        if not isinstance(node, allowed):
            raise ValueError(f"Unsupported syntax: {type(node).__name__}")
        return super().generic_visit(node)

def _eval_series_expr(expr: str, df: pd.DataFrame) -> pd.Series:
    names = set(df.columns) | {"True","False","None"}
    tree = ast.parse(expr, mode="eval")
    _SafeExpr(names).visit(tree)
    code = compile(tree, "<expr>", "eval")
    env = {**{k: df[k] for k in df.columns}, **_ALLOWED_FUNCS}
    return eval(code, {"__builtins__": {}}, env)

# -------- COLUMN ROLE RESOLVER --------
SEMANTIC_ROLES = {
    "facility": ["facility", "hospital", "centre", "center", "clinic", "site", "settlement", "community"],
    "zone": ["zone", "region", "area", "district"],
    "specialty": ["specialty", "service", "program", "discipline"],
    "city": ["city", "town", "village"],
    "lat": ["latitude", "lat"],
    "lon": ["longitude", "lon", "lng"],
}

def resolve_role(df: pd.DataFrame, role: str) -> Optional[str]:
    """Find the best matching column for a semantic role."""
    candidates = SEMANTIC_ROLES.get(role, [])
    lower_cols = {c.lower(): c for c in df.columns}
    for cand in candidates:
        for col_lc, col in lower_cols.items():
            if cand in col_lc:
                return col
    return None

# -------- MAIN ENGINE --------
class ScenarioEngine:
    @staticmethod
    def _as_df(v: Any) -> Optional[pd.DataFrame]:
        if isinstance(v, list):
            return pd.DataFrame(v) if v else pd.DataFrame()
        if isinstance(v, dict):
            return pd.DataFrame([v]) if all(isinstance(val, (int,float,str,bool,type(None))) for val in v.values()) else pd.DataFrame()
        if isinstance(v, pd.DataFrame):
            return v
        return None

    @staticmethod
    def execute_plan(plan: ScenarioPlan, datasets: Dict[str, Any]) -> str:
        sections: List[str] = ["# Scenario Output\n"]
        for t in plan.tasks:
            sections.append(ScenarioEngine._exec_task(t, datasets))
        return "\n".join(sections).strip()

    @staticmethod
    def _get_df(datasets: Dict[str, Any], key: Optional[str]) -> Optional[pd.DataFrame]:
        if key and key in datasets:
            v = datasets[key]
        else:
            v = next((vv for vv in datasets.values() if isinstance(vv, (list, dict, pd.DataFrame))), None)
        return ScenarioEngine._as_df(v) if v is not None else None

    @staticmethod
    def _apply_filter(df: pd.DataFrame, expr: str) -> pd.DataFrame:
        m = _eval_series_expr(expr, df)
        return df.loc[m.astype(bool)].copy()

    @staticmethod
    def _apply_derive(df: pd.DataFrame, spec: str) -> pd.DataFrame:
        parts = re.split(r'[;,]\s*', spec)
        for p in parts:
            if "=" in p:
                col, expr = p.split("=", 1)
                df[col.strip()] = _eval_series_expr(expr.strip(), df)
        return df

    @staticmethod
    def _parse_aggs(spec: Optional[str]) -> List[Tuple[str, str]]:
        if not spec: return []
        out = []
        for it in [x.strip() for x in spec.split(",") if x.strip()]:
            if it.lower() in ("count","count(*)"):
                out.append(("count","count(*)")); continue
            m = re.match(r'([a-zA-Z_][a-zA-Z0-9_]*)\(([^)]+)\)', it)
            if not m: continue
            func, arg = m.group(1).lower(), m.group(2).strip()
            out.append((f"{func}_{arg}", f"{func}({arg})"))
        return out

    @staticmethod
    def _apply_agg_call(df: pd.DataFrame, call: str):
        call = call.strip()
        if call.lower() in ("count","count(*)"): return int(len(df))
        m = re.match(r'([a-zA-Z_][a-zA-Z0-9_]*)\(([^)]+)\)', call)
        func, arg = m.group(1).lower(), m.group(2).strip()
        if arg not in df.columns: return None
        col = df[arg].dropna()
        if func in ("avg","mean"): return float(np.mean(col)) if len(col) else float("nan")
        if func == "median": return float(np.median(col)) if len(col) else float("nan")
        if func == "sum": return float(np.sum(col)) if len(col) else 0.0
        if func in ("min","max"): return float(getattr(np, func)(col)) if len(col) else float("nan")
        if func.startswith("p") and func[1:].isdigit(): return float(np.percentile(col, int(func[1:]))) if len(col) else float("nan")
        return None

    @staticmethod
    def _group_agg(df: pd.DataFrame, group_by: Optional[List[str]], agg_spec: Optional[str]) -> pd.DataFrame:
        aggs = ScenarioEngine._parse_aggs(agg_spec)
        if not aggs and not group_by: return df
        if not group_by:
            return pd.DataFrame([{k: ScenarioEngine._apply_agg_call(df, call) for k, call in aggs}])
        rows = []
        gb = df.groupby(group_by, dropna=False)
        for keys, g in gb:
            if not isinstance(keys, tuple): keys = (keys,)
            rec = {group_by[i]: keys[i] for i in range(len(group_by))}
            for out_col, call in aggs:
                rec[out_col] = ScenarioEngine._apply_agg_call(g, call)
            rows.append(rec)
        return pd.DataFrame(rows)

    # -------- RENDERERS --------
    @staticmethod
    def _render_table(df: pd.DataFrame) -> str:
        if df.empty: return "_No rows._"
        dff = df.copy()
        for c in dff.columns:
            dff[c] = dff[c].apply(lambda v: "NaN" if (isinstance(v,float) and math.isnan(v)) else f"{v:,.4g}" if isinstance(v,float) else v)
        header = "| " + " | ".join(dff.columns) + " |"
        sep = "|" + "|".join(["---"] * len(dff.columns)) + "|"
        rows = ["| " + " | ".join(map(str, r)) + " |" for r in dff.to_numpy().tolist()]
        return "\n".join([header, sep, *rows])

    @staticmethod
    def _exec_task(t: TaskPlan, datasets: Dict[str, Any]) -> str:
        section = [f"## {t.title}\n"]
        df = ScenarioEngine._get_df(datasets, t.data_key)
        if df is None or df.empty:
            section.append("_No matching data for this task._")
            return "\n".join(section)

        # Resolve semantic roles dynamically
        if t.group_by:
            t.group_by = resolve_cols(t.group_by, df.columns.tolist())

        if t.filter: df = ScenarioEngine._apply_filter(df, t.filter)
        if t.derive:
            for d in t.derive: df = ScenarioEngine._apply_derive(df, d)

        if t.group_by or t.agg:
            df = ScenarioEngine._group_agg(df, t.group_by, ", ".join(t.agg or []))

        if t.sort_by and t.sort_by in df.columns:
            df = df.sort_values(by=t.sort_by, ascending=(t.sort_dir or "desc").lower()=="asc")

        if t.top and t.top > 0:
            df = df.head(t.top)

        if t.fields:
            cols = resolve_cols(t.fields, df.columns.tolist())
            cols = [c for c in cols if c in df.columns]
            if cols: df = df[cols]

        section.append(ScenarioEngine._render_table(df))
        return "\n".join(section)