cyberosa
updating notebook with errors handling. Cleaning
5b81650
raw
history blame
3.17 kB
import pandas as pd
import gradio as gr
from typing import List
HEIGHT = 600
WIDTH = 1000
def get_error_data(tools_df: pd.DataFrame, inc_tools: List[str]) -> pd.DataFrame:
"""Gets the error data for the given tools and calculates the error percentage."""
tools_inc = tools_df[tools_df["tool"].isin(inc_tools)]
error = (
tools_inc.groupby(["tool", "request_month_year_week", "error"])
.size()
.unstack()
.fillna(0)
.reset_index()
)
error["error_perc"] = (error[1] / (error[0] + error[1])) * 100
error["total_requests"] = error[0] + error[1]
return error
def get_error_data_overall(error_df: pd.DataFrame) -> pd.DataFrame:
"""Gets the error data for the given tools and calculates the error percentage."""
error_total = (
error_df.groupby("request_month_year_week")
.agg({"total_requests": "sum", 1: "sum", 0: "sum"})
.reset_index()
)
error_total["error_perc"] = (error_total[1] / error_total["total_requests"]) * 100
error_total.columns = error_total.columns.astype(str)
error_total["error_perc"] = error_total["error_perc"].apply(lambda x: round(x, 4))
return error_total
def plot_error_data(error_all_df: pd.DataFrame) -> gr.BarPlot:
"""Plots the error data for the given tools and calculates the error percentage."""
return gr.BarPlot(
value=error_all_df,
x="request_month_year_week",
y="error_perc",
title="Error Percentage",
x_title="Week",
y_title="Error Percentage",
show_label=True,
interactive=True,
show_actions_button=True,
tooltip=["request_month_year_week", "error_perc"],
height=HEIGHT,
width=WIDTH,
)
def plot_tool_error_data(error_df: pd.DataFrame, tool: str) -> gr.BarPlot:
"""Plots the error data for the given tool."""
error_tool = error_df[error_df["tool"] == tool]
error_tool.columns = error_tool.columns.astype(str)
error_tool["error_perc"] = error_tool["error_perc"].apply(lambda x: round(x, 4))
return gr.BarPlot(
title="Error Percentage",
x_title="Week",
y_title="Error Percentage",
show_label=True,
interactive=True,
show_actions_button=True,
tooltip=["request_month_year_week", "error_perc"],
value=error_tool,
x="request_month_year_week",
y="error_perc",
height=HEIGHT,
width=WIDTH,
)
def plot_week_error_data(error_df: pd.DataFrame, week: str) -> gr.BarPlot:
"""Plots the error data for the given week."""
error_week = error_df[error_df["request_month_year_week"] == week]
error_week.columns = error_week.columns.astype(str)
error_week["error_perc"] = error_week["error_perc"].apply(lambda x: round(x, 4))
return gr.BarPlot(
value=error_week,
x="tool",
y="error_perc",
title="Error Percentage",
x_title="Tool",
y_title="Error Percentage",
show_label=True,
interactive=True,
show_actions_button=True,
tooltip=["tool", "error_perc"],
height=HEIGHT,
width=WIDTH,
)