rosacastillo commited on
Commit
6012e5c
·
1 Parent(s): af19e8a

tools accuracy updates

Browse files
Files changed (4) hide show
  1. app.py +19 -72
  2. tabs/metrics.py +19 -2
  3. tabs/tool_accuracy.py +50 -0
  4. tabs/tool_win.py +91 -0
app.py CHANGED
@@ -20,23 +20,29 @@ from tabs.trades import (
20
  )
21
 
22
  from tabs.metrics import (
23
- metric_choices,
24
- default_metric,
 
 
25
  plot_trade_details,
26
  plot2_trade_details,
27
  plot_trade_metrics,
28
  WIDTH,
29
  HEIGHT,
 
30
  )
31
 
32
  from tabs.tool_win import (
33
  get_tool_winning_rate,
 
34
  get_overall_winning_rate,
35
  plot_tool_winnings_overall,
36
  plot_tool_winnings_by_tool,
37
  )
38
 
39
  from tabs.tool_accuracy import (
 
 
40
  compute_weighted_accuracy,
41
  plot_tools_accuracy_graph,
42
  plot_tools_weighted_accuracy_graph,
@@ -193,68 +199,7 @@ with demo:
193
  )
194
 
195
  with gr.Tabs():
196
- with gr.TabItem("🔥Trades Dashboard v1"):
197
- with gr.Row():
198
- gr.Markdown("# Trend of weekly trades")
199
- with gr.Row():
200
- with gr.Column(min_width=380):
201
- qs_trades_by_week = plot_trades_per_market_by_week(
202
- trades_df=trades_by_market, market_type="quickstart"
203
- )
204
- with gr.Column(min_width=380):
205
- pearl_trades_by_week = plot_trades_per_market_by_week(
206
- trades_df=trades_by_market, market_type="pearl"
207
- )
208
- with gr.Column(min_width=380):
209
- all_trades_by_week = plot_trades_per_market_by_week(
210
- trades_df=trades_by_market, market_type="all"
211
- )
212
-
213
- with gr.Row():
214
- gr.Markdown("# Percentage of winning trades per week")
215
- with gr.Row():
216
- with gr.Column(min_width=380):
217
- qs_wtrades_by_week = plot_winning_trades_per_market_by_week(
218
- trades_df=winning_trades_by_market, market_type="quickstart"
219
- )
220
- with gr.Column(min_width=380):
221
- pearl_wtrades_by_week = plot_winning_trades_per_market_by_week(
222
- trades_df=winning_trades_by_market, market_type="pearl"
223
- )
224
- with gr.Column(min_width=380):
225
- all_wtrades_by_week = plot_winning_trades_per_market_by_week(
226
- trades_df=winning_trades_by_market, market_type="all"
227
- )
228
-
229
- with gr.Row():
230
- gr.Markdown("# ⚖️ Trading metrics")
231
- with gr.Row():
232
- trade_details_selector = gr.Dropdown(
233
- label="Select a trade metric",
234
- choices=metric_choices,
235
- value=default_metric,
236
- )
237
- with gr.Row():
238
- trade_details_plot = plot_trade_metrics(
239
- metric_name=default_metric,
240
- trades_df=trades_df,
241
- )
242
-
243
- def update_trade_details(trade_detail):
244
- return plot_trade_metrics(metric_name=trade_detail, trades_df=trades_df)
245
-
246
- trade_details_selector.change(
247
- update_trade_details,
248
- inputs=trade_details_selector,
249
- outputs=trade_details_plot,
250
- )
251
-
252
- with gr.Row():
253
- trade_details_selector
254
- with gr.Row():
255
- trade_details_plot
256
-
257
- with gr.TabItem("🔥Trades Dashboard v2"):
258
  with gr.Row():
259
  gr.Markdown("# Trend of weekly trades")
260
  with gr.Row():
@@ -274,12 +219,12 @@ with demo:
274
  with gr.Row():
275
  trade_details_selector = gr.Dropdown(
276
  label="Select a trade metric",
277
- choices=metric_choices,
278
- value=default_metric,
279
  )
280
  with gr.Row():
281
  trade_details_plot = plot_trade_metrics(
282
- metric_name=default_metric,
283
  trades_df=trades_df,
284
  height=400,
285
  width=1400,
@@ -311,13 +256,15 @@ with demo:
311
  with gr.Row():
312
  winning_selector = gr.Dropdown(
313
  label="Select the tool metric",
314
- choices=["losses", "wins", "total_request", "win_perc"],
315
- value="win_perc",
316
  )
317
 
318
  with gr.Row():
 
319
  winning_plot = plot_tool_winnings_overall(
320
- wins_df=winning_rate_overall_df, winning_selector="win_perc"
 
321
  )
322
 
323
  def update_tool_winnings_overall_plot(winning_selector):
@@ -371,7 +318,7 @@ with demo:
371
  )
372
 
373
  with gr.Row():
374
- _ = plot_tools_accuracy_graph(tools_accuracy_info)
375
 
376
  with gr.Row():
377
  gr.Markdown("# Weighted accuracy ranking per tool")
@@ -384,7 +331,7 @@ with demo:
384
  "The data used for this metric is from the past two months. This metric is computed using both the tool accuracy and the volume of requests received by the tool. The minimum value of this custom metric is 0 and the maximum value is 1. The higher the better is the tool."
385
  )
386
  with gr.Row():
387
- _ = plot_tools_weighted_accuracy_graph(tools_accuracy_info)
388
 
389
  with gr.TabItem("⛔ Invalid Markets Dashboard"):
390
  with gr.Row():
 
20
  )
21
 
22
  from tabs.metrics import (
23
+ trade_metric_choices,
24
+ tool_metric_choices,
25
+ default_trade_metric,
26
+ default_tool_metric,
27
  plot_trade_details,
28
  plot2_trade_details,
29
  plot_trade_metrics,
30
  WIDTH,
31
  HEIGHT,
32
+ plot_tool_metrics,
33
  )
34
 
35
  from tabs.tool_win import (
36
  get_tool_winning_rate,
37
+ get_tool_winning_rate_by_market,
38
  get_overall_winning_rate,
39
  plot_tool_winnings_overall,
40
  plot_tool_winnings_by_tool,
41
  )
42
 
43
  from tabs.tool_accuracy import (
44
+ plot_tools_weighted_accuracy_rotated_graph,
45
+ plot_tools_accuracy_rotated_graph,
46
  compute_weighted_accuracy,
47
  plot_tools_accuracy_graph,
48
  plot_tools_weighted_accuracy_graph,
 
199
  )
200
 
201
  with gr.Tabs():
202
+ with gr.TabItem("🔥Trades Dashboard"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  with gr.Row():
204
  gr.Markdown("# Trend of weekly trades")
205
  with gr.Row():
 
219
  with gr.Row():
220
  trade_details_selector = gr.Dropdown(
221
  label="Select a trade metric",
222
+ choices=trade_metric_choices,
223
+ value=default_trade_metric,
224
  )
225
  with gr.Row():
226
  trade_details_plot = plot_trade_metrics(
227
+ metric_name=default_trade_metric,
228
  trades_df=trades_df,
229
  height=400,
230
  width=1400,
 
256
  with gr.Row():
257
  winning_selector = gr.Dropdown(
258
  label="Select the tool metric",
259
+ choices=tool_metric_choices,
260
+ value=default_tool_metric,
261
  )
262
 
263
  with gr.Row():
264
+ # plot_tool_metrics
265
  winning_plot = plot_tool_winnings_overall(
266
+ wins_df=winning_rate_overall_df,
267
+ winning_selector=default_tool_metric,
268
  )
269
 
270
  def update_tool_winnings_overall_plot(winning_selector):
 
318
  )
319
 
320
  with gr.Row():
321
+ _ = plot_tools_accuracy_rotated_graph(tools_accuracy_info)
322
 
323
  with gr.Row():
324
  gr.Markdown("# Weighted accuracy ranking per tool")
 
331
  "The data used for this metric is from the past two months. This metric is computed using both the tool accuracy and the volume of requests received by the tool. The minimum value of this custom metric is 0 and the maximum value is 1. The higher the better is the tool."
332
  )
333
  with gr.Row():
334
+ _ = plot_tools_weighted_accuracy_rotated_graph(tools_accuracy_info)
335
 
336
  with gr.TabItem("⛔ Invalid Markets Dashboard"):
337
  with gr.Row():
tabs/metrics.py CHANGED
@@ -2,7 +2,7 @@ import pandas as pd
2
  import gradio as gr
3
  import plotly.express as px
4
 
5
- metric_choices = [
6
  "mech calls",
7
  "collateral amount",
8
  "earnings",
@@ -10,7 +10,10 @@ metric_choices = [
10
  "ROI",
11
  ]
12
 
13
- default_metric = "ROI"
 
 
 
14
 
15
  HEIGHT = 600
16
  WIDTH = 1000
@@ -219,3 +222,17 @@ def plot_average_roi_per_market_by_week(trades_df: pd.DataFrame) -> gr.LinePlot:
219
  height=HEIGHT,
220
  width=WIDTH,
221
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  import plotly.express as px
4
 
5
+ trade_metric_choices = [
6
  "mech calls",
7
  "collateral amount",
8
  "earnings",
 
10
  "ROI",
11
  ]
12
 
13
+ tool_metric_choices = ["losses", "wins", "total_request", "win_perc"]
14
+
15
+ default_trade_metric = "ROI"
16
+ default_tool_metric = "win_perc"
17
 
18
  HEIGHT = 600
19
  WIDTH = 1000
 
222
  height=HEIGHT,
223
  width=WIDTH,
224
  )
225
+
226
+
227
+ def plot_tool_metrics(wins_df: pd.DataFrame, winning_selector: str) -> gr.Plot:
228
+ print("under construction")
229
+ if winning_selector == "losses":
230
+ yaxis_title = "Nr of mech calls per trade"
231
+ elif winning_selector == "win_perc":
232
+ column_name = "roi"
233
+ yaxis_title = "ROI (net profit/cost)"
234
+ elif winning_selector == "wins":
235
+ yaxis_title = "Collateral amount per trade (xDAI)"
236
+ else: # "total_request"
237
+ column_name = metric_name
238
+ yaxis_title = "Gross profit per trade (xDAI)"
tabs/tool_accuracy.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
  from typing import Tuple
 
6
 
7
  VOLUME_FACTOR_REGULARIZATION = 0.5
8
  UNSCALED_WEIGHTED_ACCURACY_INTERVAL = (-0.5, 100.5)
@@ -23,6 +24,9 @@ tools_palette = {
23
  "prediction-request-rag": "chocolate",
24
  }
25
 
 
 
 
26
 
27
  def scale_value(
28
  value: float,
@@ -79,6 +83,29 @@ def plot_tools_accuracy_graph(tools_accuracy_info: pd.DataFrame):
79
  return gr.Plot(value=plot.get_figure())
80
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  def plot_tools_weighted_accuracy_graph(tools_accuracy_info: pd.DataFrame):
83
  tools_accuracy_info = tools_accuracy_info.sort_values(
84
  by="weighted_accuracy", ascending=False
@@ -98,3 +125,26 @@ def plot_tools_weighted_accuracy_graph(tools_accuracy_info: pd.DataFrame):
98
  plt.ylabel("tool", fontsize=20)
99
  plt.tick_params(axis="y", labelsize=12)
100
  return gr.Plot(value=plot.get_figure())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
  from typing import Tuple
6
+ import plotly.express as px
7
 
8
  VOLUME_FACTOR_REGULARIZATION = 0.5
9
  UNSCALED_WEIGHTED_ACCURACY_INTERVAL = (-0.5, 100.5)
 
24
  "prediction-request-rag": "chocolate",
25
  }
26
 
27
+ HEIGHT = 400
28
+ WIDTH = 1100
29
+
30
 
31
  def scale_value(
32
  value: float,
 
83
  return gr.Plot(value=plot.get_figure())
84
 
85
 
86
+ def plot_tools_accuracy_rotated_graph(tools_accuracy_info: pd.DataFrame):
87
+ tools_accuracy_info = tools_accuracy_info.sort_values(
88
+ by="tool_accuracy", ascending=False
89
+ )
90
+ fig = px.bar(
91
+ tools_accuracy_info,
92
+ x="tool",
93
+ y="tool_accuracy",
94
+ color="tool",
95
+ color_discrete_map=tools_palette,
96
+ )
97
+ fig.update_layout(
98
+ xaxis_title="Tool",
99
+ yaxis_title="Mech tool_accuracy (%)",
100
+ )
101
+ fig.update_layout(width=WIDTH, height=HEIGHT)
102
+ # fig.update_xaxes(tickangle=45)
103
+ fig.update_xaxes(showticklabels=False)
104
+ return gr.Plot(
105
+ value=fig,
106
+ )
107
+
108
+
109
  def plot_tools_weighted_accuracy_graph(tools_accuracy_info: pd.DataFrame):
110
  tools_accuracy_info = tools_accuracy_info.sort_values(
111
  by="weighted_accuracy", ascending=False
 
125
  plt.ylabel("tool", fontsize=20)
126
  plt.tick_params(axis="y", labelsize=12)
127
  return gr.Plot(value=plot.get_figure())
128
+
129
+
130
+ def plot_tools_weighted_accuracy_rotated_graph(tools_accuracy_info: pd.DataFrame):
131
+ tools_accuracy_info = tools_accuracy_info.sort_values(
132
+ by="weighted_accuracy", ascending=False
133
+ )
134
+ fig = px.bar(
135
+ tools_accuracy_info,
136
+ x="tool",
137
+ y="weighted_accuracy",
138
+ color="tool",
139
+ color_discrete_map=tools_palette,
140
+ )
141
+ fig.update_layout(
142
+ xaxis_title="Tool",
143
+ yaxis_title="Weighted accuracy metric",
144
+ )
145
+ fig.update_layout(width=WIDTH, height=HEIGHT)
146
+ # fig.update_xaxes(tickangle=45)
147
+ fig.update_xaxes(showticklabels=False)
148
+ return gr.Plot(
149
+ value=fig,
150
+ )
tabs/tool_win.py CHANGED
@@ -38,6 +38,40 @@ def get_tool_winning_rate(tools_df: pd.DataFrame, inc_tools: List[str]) -> pd.Da
38
  return wins
39
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def get_overall_winning_rate(wins_df: pd.DataFrame) -> pd.DataFrame:
42
  """Gets the overall winning rate data for the given tools and calculates the winning percentage."""
43
  overall_wins = (
@@ -49,6 +83,28 @@ def get_overall_winning_rate(wins_df: pd.DataFrame) -> pd.DataFrame:
49
  return overall_wins
50
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  def plot_tool_winnings_overall(
53
  wins_df: pd.DataFrame, winning_selector: str = "win_perc"
54
  ) -> gr.BarPlot:
@@ -69,6 +125,41 @@ def plot_tool_winnings_overall(
69
  )
70
 
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  def plot_tool_winnings_by_tool(wins_df: pd.DataFrame, tool: str) -> gr.BarPlot:
73
  """Plots the winning rate data for the given tool."""
74
  return gr.BarPlot(
 
38
  return wins
39
 
40
 
41
+ def get_tool_winning_rate_by_market(
42
+ tools_df: pd.DataFrame, inc_tools: List[str]
43
+ ) -> pd.DataFrame:
44
+ """Gets the tool winning rate data for the given tools by market and calculates the winning percentage."""
45
+ tools_inc = tools_df[tools_df["tool"].isin(inc_tools)]
46
+ tools_non_error = tools_inc[tools_inc["error"] != 1]
47
+ tools_non_error.loc[:, "currentAnswer"] = tools_non_error["currentAnswer"].replace(
48
+ {"no": "No", "yes": "Yes"}
49
+ )
50
+ tools_non_error = tools_non_error[
51
+ tools_non_error["currentAnswer"].isin(["Yes", "No"])
52
+ ]
53
+ tools_non_error = tools_non_error[tools_non_error["vote"].isin(["Yes", "No"])]
54
+ tools_non_error["win"] = (
55
+ tools_non_error["currentAnswer"] == tools_non_error["vote"]
56
+ ).astype(int)
57
+ tools_non_error.columns = tools_non_error.columns.astype(str)
58
+ wins = (
59
+ tools_non_error.groupby(
60
+ ["tool", "request_month_year_week", "market_creator", "win"], sort=False
61
+ )
62
+ .size()
63
+ .unstack()
64
+ .fillna(0)
65
+ )
66
+ wins["win_perc"] = (wins[1] / (wins[0] + wins[1])) * 100
67
+ wins.reset_index(inplace=True)
68
+ wins["total_request"] = wins[0] + wins[1]
69
+ wins.columns = wins.columns.astype(str)
70
+ # Convert request_month_year_week to string and explicitly set type for Altair
71
+ wins["request_month_year_week"] = wins["request_month_year_week"].astype(str)
72
+ return wins
73
+
74
+
75
  def get_overall_winning_rate(wins_df: pd.DataFrame) -> pd.DataFrame:
76
  """Gets the overall winning rate data for the given tools and calculates the winning percentage."""
77
  overall_wins = (
 
83
  return overall_wins
84
 
85
 
86
+ def get_overall_winning_rate(wins_df: pd.DataFrame) -> pd.DataFrame:
87
+ """Gets the overall winning rate data for the given tools and calculates the winning percentage."""
88
+ overall_wins = (
89
+ wins_df.groupby("request_month_year_week")
90
+ .agg({"0": "sum", "1": "sum", "win_perc": "mean", "total_request": "sum"})
91
+ .rename(columns={"0": "losses", "1": "wins"})
92
+ .reset_index()
93
+ )
94
+ return overall_wins
95
+
96
+
97
+ def get_overall_winning_rate_by_market(wins_df: pd.DataFrame) -> pd.DataFrame:
98
+ """Gets the overall winning rate data for the given tools and calculates the winning percentage."""
99
+ overall_wins = (
100
+ wins_df.groupby(["request_month_year_week", "market_creator"], sort=False)
101
+ .agg({"0": "sum", "1": "sum", "win_perc": "mean", "total_request": "sum"})
102
+ .rename(columns={"0": "losses", "1": "wins"})
103
+ .reset_index()
104
+ )
105
+ return overall_wins
106
+
107
+
108
  def plot_tool_winnings_overall(
109
  wins_df: pd.DataFrame, winning_selector: str = "win_perc"
110
  ) -> gr.BarPlot:
 
125
  )
126
 
127
 
128
+ def integrated_plot_tool_winnings_overall(
129
+ tools_df: pd.DataFrame, winning_selector: str = "win_perc"
130
+ ) -> gr.Plot:
131
+ # TODO Pending final implementation
132
+ """Plots the overall winning rate data for the given tools and calculates the winning percentage."""
133
+ # adding the total
134
+ wins_df_all = tools_df.copy(deep=True)
135
+ wins_df_all["market_creator"] = "all"
136
+
137
+ # merging both dataframes
138
+ all_winning_tools = pd.concat([wins_df, wins_df_all], ignore_index=True)
139
+ all_winning_tools = all_winning_tools.sort_values(
140
+ by="creation_timestamp", ascending=True
141
+ )
142
+ final_df = get_overall_winning_rate_by_market(all_winning_tools)
143
+ fig = px.bar(
144
+ final_df,
145
+ x="request_month_year_week",
146
+ y=winning_selector,
147
+ color="market_creator",
148
+ barmode="group",
149
+ color_discrete_sequence=["goldenrod", "darkgreen", "purple"],
150
+ )
151
+ fig.update_layout(
152
+ xaxis_title="Week",
153
+ yaxis_title="Weekly % of winning rate",
154
+ legend=dict(yanchor="top", y=0.5),
155
+ )
156
+ fig.update_layout(width=WIDTH, height=HEIGHT)
157
+ fig.update_xaxes(tickformat="%b %d\n%Y")
158
+ return gr.Plot(
159
+ value=fig,
160
+ )
161
+
162
+
163
  def plot_tool_winnings_by_tool(wins_df: pd.DataFrame, tool: str) -> gr.BarPlot:
164
  """Plots the winning rate data for the given tool."""
165
  return gr.BarPlot(