Spaces:
Runtime error
Runtime error
File size: 6,255 Bytes
2206479 65733ce 2206479 65733ce 2206479 9c51f62 2206479 65733ce 2206479 9c51f62 2206479 9c51f62 4e4eb03 72d697e 4e4eb03 9c51f62 65733ce 2206479 9c51f62 2206479 fc9603c 2206479 fc9603c 2206479 9c51f62 2206479 72d697e 3a34298 72d697e 4e4eb03 3a34298 72d697e 3a34298 72d697e 3a34298 9c51f62 72d697e 3a34298 9c51f62 72d697e 3a34298 72d697e c235ab0 72d697e 9c51f62 65733ce c235ab0 65733ce c235ab0 65733ce c235ab0 65733ce c235ab0 65733ce c235ab0 65733ce c235ab0 65733ce c235ab0 65733ce c235ab0 65733ce c235ab0 65733ce c235ab0 65733ce c235ab0 9863c18 65733ce |
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
import pandas as pd
import gradio as gr
import gc
import plotly.express as px
def plot_rolling_average_dune(
daa_df: pd.DataFrame,
) -> gr.Plot:
"""Function to plot the rolling average of daily active agents"""
fig = px.bar(
daa_df,
x="tx_date",
y="seven_day_trailing_avg",
)
fig.update_layout(
xaxis_title="Date",
yaxis_title="7-day rolling average of DAA",
)
return gr.Plot(
value=fig,
)
def plot_rolling_average(
daa_df: pd.DataFrame,
market_creator: str = None,
) -> gr.Plot:
"""Function to plot the rolling average of daily active agents"""
if market_creator is not None:
filtered_traders_df = daa_df.loc[daa_df["market_creator"] == market_creator]
rolling_avg_df = get_sevenday_rolling_average(filtered_traders_df)
else:
rolling_avg_df = get_sevenday_rolling_average(daa_df)
print(rolling_avg_df.head())
# Ensure 'creation_date' is a column, not an index
if "tx_date" not in rolling_avg_df.columns:
rolling_avg_df = rolling_avg_df.reset_index()
fig = px.bar(
rolling_avg_df,
x="tx_date",
y="rolling_avg_traders",
)
fig.update_layout(
xaxis_title="Date",
yaxis_title="7-day rolling average of DAA",
)
return gr.Plot(
value=fig,
)
def get_sevenday_rolling_average(daa_df: pd.DataFrame) -> pd.DataFrame:
"""Function to get the 7-day rolling average of the number of unique
trader_address"""
# Create a local copy of the dataframe
local_df = daa_df.copy()
# Sort the dataframe by date
local_df = local_df.sort_values(by="tx_date").set_index("tx_date")
# Group by market_creator and calculate rolling average of unique trader_address
rolling_avg = (
local_df.resample("D")["trader_address"]
.nunique()
.rolling(window=7)
.mean()
.reset_index()
)
rolling_avg.rename(columns={"trader_address": "rolling_avg_traders"}, inplace=True)
return rolling_avg
def plot_rolling_average_roi(
traders_data: pd.DataFrame, pearl_agents: pd.DataFrame
) -> gr.Plot:
"""Function to plot the rolling average of ROI for pearl agents"""
# Get the list of unique addresses from the daa_pearl_df
unique_addresses = pearl_agents["safe_address"].unique()
# Filter the weekly_roi_df to include only those addresses
filtered_traders_data = traders_data[
traders_data["trader_address"].isin(unique_addresses)
]
# create the date column
filtered_traders_data["creation_timestamp"] = pd.to_datetime(
filtered_traders_data["creation_timestamp"]
)
filtered_traders_data["creation_date"] = filtered_traders_data[
"creation_timestamp"
].dt.date
# Get the 2-week rolling average of ROI
rolling_avg_roi_df = get_twoweeks_rolling_average_roi(filtered_traders_data)
print(rolling_avg_roi_df.head())
fig = px.bar(
rolling_avg_roi_df,
x="creation_date",
y="rolling_avg_roi",
)
fig.update_layout(
xaxis_title="Week",
yaxis_title="2-week rolling average of ROI at the trader level",
)
return gr.Plot(
value=fig,
)
def get_twoweeks_rolling_average_roi(traders_data: pd.DataFrame) -> pd.DataFrame:
"""Function to get the 2-week rolling average of the ROI by market_creator and total"""
# Create a copy to avoid SettingWithCopyWarning
local_df = traders_data.copy()
# Ensure creation_date is datetime64[ns]
# Since creation_date comes from .dt.date, it's a date object, not datetime
local_df["creation_date"] = pd.to_datetime(local_df["creation_date"])
# Aggregate ROI at the date level
daily_avg = local_df.groupby("creation_date")["roi"].mean().reset_index()
# Set the datetime index
daily_avg = daily_avg.set_index("creation_date")
# Now resample and rolling average
weekly_avg = daily_avg.resample("W").mean()
rolling_avg = weekly_avg.rolling(window=2).mean().reset_index()
# Rename columns
rolling_avg.rename(
columns={"roi": "rolling_avg_roi", "date": "creation_date"},
inplace=True,
)
return rolling_avg
def get_weekly_average_roi(traders_data: pd.DataFrame) -> pd.DataFrame:
"""Function to get the weekly average ROI by market_creator and total"""
# Create a copy to avoid SettingWithCopyWarning
local_df = traders_data.copy()
# Ensure creation_date is datetime64[ns]
# Since creation_date comes from .dt.date, it's a date object, not datetime
local_df["creation_date"] = pd.to_datetime(local_df["creation_date"])
# Aggregate ROI at the date level first
daily_avg = local_df.groupby("creation_date")["roi"].mean().reset_index()
# Set the datetime index
daily_avg = daily_avg.set_index("creation_date")
# Resample to weekly frequency and calculate mean
weekly_avg = daily_avg.resample("W").mean().reset_index()
return weekly_avg
def plot_weekly_average_roi(
traders_data: pd.DataFrame, pearl_agents: pd.DataFrame
) -> gr.Plot:
"""Function to plot the weekly average of ROI for pearl agents"""
# Get the list of unique addresses from the daa_pearl_df
# Get the list of unique addresses from the daa_pearl_df
unique_addresses = pearl_agents["safe_address"].unique()
# Filter the weekly_roi_df to include only those addresses
filtered_traders_data = traders_data[
traders_data["trader_address"].isin(unique_addresses)
]
# create the date column
filtered_traders_data["creation_timestamp"] = pd.to_datetime(
filtered_traders_data["creation_timestamp"]
)
filtered_traders_data["creation_date"] = filtered_traders_data[
"creation_timestamp"
].dt.date
# Get the weekly average ROI
weekly_avg_roi_df = get_weekly_average_roi(filtered_traders_data)
# plot the weekly average ROI
print(weekly_avg_roi_df.head())
fig = px.line(
weekly_avg_roi_df,
x="creation_date",
y="weekly_avg_roi",
)
fig.update_layout(
xaxis_title="Week",
yaxis_title="Weekly average ROI for pearl agents",
)
return gr.Plot(
value=fig,
)
|