cyberosa commited on
Commit
9f9cf5f
·
1 Parent(s): e5eef4b

Adding new plot for daily active agents

Browse files
Files changed (2) hide show
  1. app.py +9 -0
  2. tabs/trader_plots.py +93 -0
app.py CHANGED
@@ -26,6 +26,7 @@ from tabs.trader_plots import (
26
  get_interpretation_text,
27
  plot_total_bet_amount,
28
  plot_active_traders,
 
29
  )
30
  from tabs.daily_graphs import (
31
  get_current_week_data,
@@ -628,6 +629,14 @@ with demo:
628
  active_traders_plot_qs = plot_active_traders(
629
  active_traders, market_creator="quickstart"
630
  )
 
 
 
 
 
 
 
 
631
 
632
  with gr.TabItem("📉 Markets Kullback–Leibler divergence"):
633
  with gr.Row():
 
26
  get_interpretation_text,
27
  plot_total_bet_amount,
28
  plot_active_traders,
29
+ plot_rolling_average,
30
  )
31
  from tabs.daily_graphs import (
32
  get_current_week_data,
 
629
  active_traders_plot_qs = plot_active_traders(
630
  active_traders, market_creator="quickstart"
631
  )
632
+ with gr.Row():
633
+ gr.Markdown(" # Daily active traders")
634
+ with gr.Row():
635
+ gr.Markdown("## Daily active traders by market categories")
636
+ with gr.Row():
637
+ rolling_avg_plot = plot_rolling_average(
638
+ active_traders, market_creator="all"
639
+ )
640
 
641
  with gr.TabItem("📉 Markets Kullback–Leibler divergence"):
642
  with gr.Row():
tabs/trader_plots.py CHANGED
@@ -301,6 +301,99 @@ def plot_total_bet_amount(
301
  )
302
 
303
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
304
  def plot_active_traders(
305
  active_traders_data: pd.DataFrame,
306
  market_creator: str = None,
 
301
  )
302
 
303
 
304
+ def get_sevenday_rolling_average(active_traders_df: pd.DataFrame) -> pd.DataFrame:
305
+ """Function to get the 7-day rolling average of the number of unique trader_address by market_creator and total"""
306
+ # Create a local copy of the dataframe
307
+ local_df = active_traders_df.copy()
308
+
309
+ # Convert string dates to datetime
310
+ local_df["creation_date"] = pd.to_datetime(
311
+ local_df["creation_date"], format="%b-%d-%Y"
312
+ )
313
+ # Sort the dataframe by date
314
+ local_df = local_df.sort_values(by="creation_date")
315
+
316
+ # Group by market_creator and calculate rolling average of unique trader_address
317
+ rolling_avg_by_market = (
318
+ local_df.groupby("market_creator")
319
+ .apply(
320
+ lambda group: group.set_index("creation_date")
321
+ .resample("D")["trader_address"]
322
+ .nunique()
323
+ .rolling(window=7)
324
+ .mean()
325
+ )
326
+ .reset_index(name="rolling_avg_traders")
327
+ )
328
+
329
+ # Calculate the total rolling average across all market_creators
330
+ local_df["market_creator"] = "all"
331
+ rolling_avg_total = (
332
+ local_df.groupby("market_creator")
333
+ .apply(
334
+ lambda group: group.set_index("creation_date")
335
+ .resample("D")["trader_address"]
336
+ .nunique()
337
+ .rolling(window=7)
338
+ .mean()
339
+ )
340
+ .reset_index(name="rolling_avg_traders")
341
+ )
342
+
343
+ # Combine both results
344
+ combined_rolling_avg = pd.concat(
345
+ [rolling_avg_by_market, rolling_avg_total], ignore_index=True
346
+ )
347
+
348
+ return combined_rolling_avg
349
+
350
+
351
+ def plot_rolling_average(
352
+ active_traders_df: pd.DataFrame,
353
+ market_creator: str = None,
354
+ ) -> gr.Plot:
355
+ """Function to plot the rolling average of active traders for the different categories and markets"""
356
+
357
+ rolling_avg_df = get_sevenday_rolling_average(active_traders_df)
358
+
359
+ if market_creator is not None:
360
+ rolling_avg_df = rolling_avg_df.loc[
361
+ rolling_avg_df["market_creator"] == market_creator
362
+ ]
363
+
364
+ # Convert string dates to datetime and sort them
365
+ all_dates_dt = sorted(
366
+ [
367
+ datetime.strptime(date, "%b-%d-%Y")
368
+ for date in rolling_avg_df["creation_date"].unique()
369
+ ]
370
+ )
371
+ # Convert back to string format
372
+ all_dates = [date.strftime("%b-%d-%Y") for date in all_dates_dt]
373
+ fig = px.line(
374
+ rolling_avg_df,
375
+ x="creation_date",
376
+ y="rolling_avg_traders",
377
+ color="market_creator",
378
+ color_discrete_sequence=color_mapping,
379
+ category_orders={
380
+ "market_creator": ["pearl", "quickstart", "all"],
381
+ },
382
+ )
383
+ fig.update_layout(
384
+ xaxis_title="Date",
385
+ yaxis_title="7-day rolling average of active traders",
386
+ legend=dict(yanchor="top", y=0.5),
387
+ )
388
+
389
+ fig.update_xaxes(tickformat="%b %d")
390
+ # Update layout to force x-axis category order (hotfix for a sorting issue)
391
+ fig.update_layout(xaxis={"categoryorder": "array", "categoryarray": all_dates})
392
+ return gr.Plot(
393
+ value=fig,
394
+ )
395
+
396
+
397
  def plot_active_traders(
398
  active_traders_data: pd.DataFrame,
399
  market_creator: str = None,