File size: 4,228 Bytes
3950f25 de6972c 1b7dc53 9e988e1 1b7dc53 cdbc522 3950f25 1b7dc53 3950f25 22e9be8 ab87186 a702e0c ab87186 cdbc522 3950f25 d0991fd 4ea95e9 d0991fd cdbc522 a702e0c 1e9fc2b cdbc522 795d0fe cdbc522 3950f25 9e988e1 3950f25 de6972c 3950f25 2003311 3950f25 a702e0c 3950f25 ab87186 |
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 |
import pandas as pd
import gradio as gr
import matplotlib.pyplot as plt
import seaborn as sns
from seaborn import FacetGrid
import plotly.express as px
import plotly.graph_objs as go
HEIGHT = 600
WIDTH = 1000
def plot_daily_invalid_trades_plotly(invalid_trades: pd.DataFrame):
fig = px.histogram(invalid_trades, x="creation_date")
return gr.Plot(value=fig)
def plot_daily_dist_invalid_trades(invalid_trades: pd.DataFrame):
"""Function to paint the distribution of daily invalid trades, no matter which market"""
sns.set_theme(palette="viridis")
plt.figure(figsize=(25, 10))
plot2 = sns.histplot(data=invalid_trades, x="creation_date", kde=True)
plt.xlabel("Creation date")
plt.ylabel("Daily number of invalid trades")
plt.xticks(rotation=45, ha="right")
daily_trades_fig = plot2.get_figure()
return gr.Plot(value=daily_trades_fig)
def plot_daily_nr_invalid_markets(invalid_trades: pd.DataFrame):
"""Function to paint the number of invalid markets over time"""
daily_invalid_markets = (
invalid_trades.groupby("creation_date")
.agg(trades_count=("title", "count"), nr_markets=("title", "nunique"))
.reset_index()
)
daily_invalid_markets["creation_date"] = daily_invalid_markets[
"creation_date"
].astype(str)
daily_invalid_markets.columns = daily_invalid_markets.columns.astype(str)
return gr.LinePlot(
value=daily_invalid_markets,
x="creation_date",
y="nr_markets",
y_title="nr_markets",
interactive=True,
show_actions_button=True,
tooltip=["creation_date", "nr_markets", "trades_count"],
height=HEIGHT,
width=WIDTH,
)
def plotly_daily_nr_invalid_markets(invalid_trades: pd.DataFrame) -> gr.Plot:
daily_invalid_markets = (
invalid_trades.groupby("creation_date")
.agg(trades_count=("title", "count"), nr_markets=("title", "nunique"))
.reset_index()
)
# Create the Plotly figure
fig = go.Figure()
# Add the line trace
fig.add_trace(
go.Scatter(
x=daily_invalid_markets["creation_date"],
y=daily_invalid_markets["nr_markets"],
mode="lines+markers",
name="Number of Markets",
hovertemplate="<b>Date:</b> %{x}<br>"
+ "<b>Number of Markets:</b> %{y}<br>"
+ "<b>Trades Count:</b> %{text}<br>",
text=daily_invalid_markets["trades_count"], # Used in the tooltip
)
)
# Customize the layout
fig.update_layout(
title="Daily Invalid Markets",
xaxis_title="Market Creation Date",
yaxis_title="Number of Markets",
xaxis=dict(
tickangle=-45, # Rotate x-axis labels by -45 degrees
tickfont=dict(size=10), # Adjust font size if needed
),
width=1000, # Adjusted for better fit on laptop screens
height=600, # Adjusted for better fit on laptop screens
hovermode="closest", # Improve tooltip behavior
# template="plotly_white", # Optional: set a cleaner background
)
return gr.Plot(
value=fig,
)
def plot_ratio_invalid_trades_per_market(invalid_trades: pd.DataFrame):
"""Function to paint the number of invalid trades that the same market accummulates"""
cat = invalid_trades["title"]
codes, uniques = pd.factorize(cat)
# add the IDs as a new column to the original dataframe
invalid_trades["title_id"] = codes
plot: FacetGrid = sns.displot(invalid_trades, x="title_id")
plt.xlabel("market id")
plt.ylabel("Total number of invalid trades by market")
plt.title("Distribution of invalid trades per market")
return gr.Plot(value=plot.figure)
def plot_top_invalid_markets(invalid_trades: pd.DataFrame):
"""Function to paint the top markets with the highest number of invalid trades"""
top_invalid_markets: pd.DataFrame = (
invalid_trades.title.value_counts().reset_index()
)
print(top_invalid_markets.head(5))
top_invalid_markets = top_invalid_markets.head(5)
top_invalid_markets.rename(columns={"count": "nr_invalid_trades"}, inplace=True)
return gr.DataFrame(top_invalid_markets)
|