rosacastillo's picture
Fixing x axis order
16d3c27
raw
history blame
2.28 kB
import pandas as pd
import gradio as gr
metric_choices = [
"mech calls",
"collateral amount",
"earnings",
"net earnings",
"ROI",
]
default_metric = "ROI"
HEIGHT = 600
WIDTH = 1000
def plot_trade_details(metric_name: str, trades_df: pd.DataFrame) -> gr.LinePlot:
"""Plots the trade details for the given trade detail."""
column_name = metric_name
if metric_name == "mech calls":
metric_name = "mech_calls"
column_name = "num_mech_calls"
elif metric_name == "ROI":
column_name = "roi"
# this is to filter out the data before 2023-09-01
trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
trades_filtered = (
trades_filtered.groupby("month_year_week")[column_name]
.quantile([0.25, 0.5, 0.75])
.unstack()
)
trades_filtered.columns = trades_filtered.columns.astype(str)
trades_filtered.reset_index(inplace=True)
trades_filtered.columns = [
"month_year_week",
"25th_percentile",
"50th_percentile",
"75th_percentile",
]
# reformat the data as percentile, date, value
trades_filtered = trades_filtered.melt(
id_vars=["month_year_week"], var_name="percentile", value_name=metric_name
)
return gr.LinePlot(
value=trades_filtered,
x="month_year_week",
y=metric_name,
color="percentile",
show_label=True,
interactive=True,
show_actions_button=True,
tooltip=["month_year_week", "percentile", metric_name],
height=HEIGHT,
width=WIDTH,
)
def plot_average_roi_per_market_by_week(trades_df: pd.DataFrame) -> gr.LinePlot:
mean_roi_per_market_by_week = (
trades_df.groupby(["market_creator", "month_year_week"])["roi"]
.mean()
.reset_index()
)
mean_roi_per_market_by_week.rename(columns={"roi": "mean_roi"}, inplace=True)
return gr.LinePlot(
value=mean_roi_per_market_by_week,
x="month_year_week",
y="ROI",
color="market_creator",
show_label=True,
interactive=True,
show_actions_button=True,
tooltip=["month_year_week", "market_creator", "mean_roi"],
height=HEIGHT,
width=WIDTH,
)