Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Create analysis_runtime.py
Browse files- analysis_runtime.py +48 -0
analysis_runtime.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# analysis_runtime.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
from typing import Dict, Any, List, Optional
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
class ExecContext:
|
| 7 |
+
def __init__(self, frames: Dict[str,pd.DataFrame]):
|
| 8 |
+
self.frames = frames # name -> DataFrame
|
| 9 |
+
|
| 10 |
+
def _find_frame_with_columns(ctx: ExecContext, cols: List[str]) -> Optional[pd.DataFrame]:
|
| 11 |
+
for df in ctx.frames.values():
|
| 12 |
+
if all(c in df.columns for c in cols):
|
| 13 |
+
return df
|
| 14 |
+
return None
|
| 15 |
+
|
| 16 |
+
def op_summary_table(ctx: ExecContext, metrics: List[str], by: List[str], aggs: List[str]=["median"]) -> Optional[pd.DataFrame]:
|
| 17 |
+
df = _find_frame_with_columns(ctx, metrics + by)
|
| 18 |
+
if df is None: return None
|
| 19 |
+
agg_spec = {m: aggs for m in metrics}
|
| 20 |
+
out = df.groupby(by, dropna=False).agg(agg_spec)
|
| 21 |
+
out.columns = [f"{m}_{a}" for m,a in out.columns]
|
| 22 |
+
return out.reset_index()
|
| 23 |
+
|
| 24 |
+
def op_rank_top_n(ctx: ExecContext, metric: str, by: str, n: int=5, agg: str="median", ascending=False) -> Optional[pd.DataFrame]:
|
| 25 |
+
df = _find_frame_with_columns(ctx, [metric, by])
|
| 26 |
+
if df is None: return None
|
| 27 |
+
g = df.groupby(by, dropna=False)[metric].agg(agg).sort_values(ascending=ascending).head(n)
|
| 28 |
+
return g.reset_index().rename(columns={metric:f"{metric}_{agg}"})
|
| 29 |
+
|
| 30 |
+
def op_delta_over_time(ctx: ExecContext, metric: str, by: List[str], time_col: str, t0: str, t1: str, agg: str="median") -> Optional[pd.DataFrame]:
|
| 31 |
+
df = _find_frame_with_columns(ctx, [metric, time_col] + by)
|
| 32 |
+
if df is None: return None
|
| 33 |
+
a = df[df[time_col]==t0].groupby(by, dropna=False)[metric].agg(agg)
|
| 34 |
+
b = df[df[time_col]==t1].groupby(by, dropna=False)[metric].agg(agg)
|
| 35 |
+
out = (b - a).reset_index()
|
| 36 |
+
out = out.rename(columns={0:f"{metric}_delta_{agg}_{t0}_to_{t1}"})
|
| 37 |
+
return out
|
| 38 |
+
|
| 39 |
+
def op_capacity_calc(clients_per_day: float, teams: int, days: int) -> pd.DataFrame:
|
| 40 |
+
total = float(clients_per_day) * float(teams) * float(days)
|
| 41 |
+
return pd.DataFrame([{"clients_per_day": clients_per_day, "teams": teams, "days": days, "capacity_total": total}])
|
| 42 |
+
|
| 43 |
+
def op_cost_total(startup_per_client: float, ongoing_per_client: float, volume: int) -> pd.DataFrame:
|
| 44 |
+
total = float(startup_per_client + ongoing_per_client) * float(volume)
|
| 45 |
+
return pd.DataFrame([{
|
| 46 |
+
"startup_per_client": startup_per_client, "ongoing_per_client": ongoing_per_client,
|
| 47 |
+
"volume": volume, "total_cost": total
|
| 48 |
+
}])
|