diff --git a/src/scripts/historical_cost_analysis.py b/src/scripts/historical_cost_analysis.py index 4bf66af..d8b27bd 100644 --- a/src/scripts/historical_cost_analysis.py +++ b/src/scripts/historical_cost_analysis.py @@ -24,25 +24,28 @@ def _build_summary_row(df: pd.DataFrame, usage_key: str, file_count: int) -> dic caching_cost = usage_df.loc[is_cache, "total_cost"].sum() non_cache_df = usage_df.loc[~is_cache].copy() + median_cost_per_contract = 0.0 if non_cache_df.empty: adjusted_total_cost = 0.0 else: by_file = non_cache_df.groupby("file_name", dropna=False)["total_cost"].sum() outlier_file = by_file.idxmax() - adjusted_total_cost = non_cache_df.loc[ - non_cache_df["file_name"] != outlier_file, "total_cost" - ].sum() + filtered_by_file = by_file.drop(index=outlier_file) + adjusted_total_cost = float(filtered_by_file.sum()) + if not filtered_by_file.empty: + median_cost_per_contract = float(filtered_by_file.median()) return { "Batch": os.path.basename(usage_key), "caching_cost": round(float(caching_cost), 6), "total_cost": round(float(adjusted_total_cost), 6), "file_count": int(file_count), - "cost_per_contract": ( + "mean_cost_per_contract": ( round(float(adjusted_total_cost) / float(file_count), 6) if file_count else 0.0 ), + "median_cost_per_contract": round(float(median_cost_per_contract), 6), } @@ -119,12 +122,19 @@ def run_historical_cost_analysis( "caching_cost", "total_cost", "file_count", - "cost_per_contract", + "mean_cost_per_contract", + "median_cost_per_contract", ], ) if summary_df.empty: summary_df = pd.DataFrame( - columns=["caching_cost", "total_cost", "file_count", "cost_per_contract"] + columns=[ + "caching_cost", + "total_cost", + "file_count", + "mean_cost_per_contract", + "median_cost_per_contract", + ] ) summary_df.index.name = "Batch" else: