Spaces:
Runtime error
Runtime error
gauravlochab
commited on
Commit
·
19f65fc
1
Parent(s):
bd46a18
refactor: Update visualization for daily and weekly number of registered agents
Browse files- app.py +23 -10
- app_trans_new.py +132 -49
app.py
CHANGED
@@ -305,39 +305,52 @@ def create_visualizations():
|
|
305 |
fig_bridges_chain.update_xaxes(tickformat="%m-%d")
|
306 |
|
307 |
# Nr of agents registered daily and weekly
|
|
|
308 |
df_agents_with_transactions['date'] = pd.to_datetime(df_agents_with_transactions['date'])
|
309 |
-
daily_agents_df = df_agents_with_transactions.groupby(df_agents_with_transactions['date']).size().reset_index(name='agent_count')
|
310 |
-
weekly_agents_df = df_agents_with_transactions.groupby(df_agents_with_transactions['date'].dt.to_period("W").apply(lambda r: r.start_time)).size().reset_index(name='agent_count')
|
311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
312 |
fig_agents_registered = go.Figure(data=[
|
313 |
go.Bar(
|
314 |
name='Daily nr of Registered Agents',
|
315 |
-
x=
|
316 |
-
y=
|
317 |
marker_color='blue'
|
318 |
),
|
319 |
go.Bar(
|
320 |
name='Total Weekly Nr of Registered Agents',
|
321 |
-
x=
|
322 |
-
y=
|
323 |
marker_color='purple'
|
324 |
)
|
325 |
])
|
|
|
|
|
326 |
fig_agents_registered.update_layout(
|
327 |
xaxis_title='Date',
|
328 |
yaxis_title='Number of Agents',
|
329 |
title="Nr of Agents Registered",
|
330 |
barmode='group',
|
331 |
-
yaxis=
|
332 |
xaxis=dict(
|
333 |
tickmode='array',
|
334 |
-
tickvals=
|
335 |
-
ticktext=[
|
336 |
tickangle=-45
|
337 |
),
|
338 |
height=700,
|
339 |
-
bargap=0.
|
340 |
)
|
|
|
341 |
# Calculate weekly average daily active agents
|
342 |
df_agents_with_transactions['day_of_week'] = df_agents_with_transactions['date'].dt.dayofweek
|
343 |
df_agents_with_transactions_weekly_avg = df_agents_with_transactions.groupby(['week', 'day_of_week'])['agent_count_with_transactions'].mean().reset_index()
|
|
|
305 |
fig_bridges_chain.update_xaxes(tickformat="%m-%d")
|
306 |
|
307 |
# Nr of agents registered daily and weekly
|
308 |
+
# Convert 'date' column to datetime
|
309 |
df_agents_with_transactions['date'] = pd.to_datetime(df_agents_with_transactions['date'])
|
|
|
|
|
310 |
|
311 |
+
# Calculate daily number of agents registered
|
312 |
+
daily_agents_df = df_agents_with_transactions.groupby('date').size().reset_index(name='daily_agent_count')
|
313 |
+
|
314 |
+
# Calculate cumulative number of agents registered within the week up to each day
|
315 |
+
df_agents_with_transactions['week_start'] = df_agents_with_transactions['date'].dt.to_period("W").apply(lambda r: r.start_time)
|
316 |
+
cumulative_agents_df = df_agents_with_transactions.groupby(['week_start', 'date']).size().groupby(level=0).cumsum().reset_index(name='weekly_agent_count')
|
317 |
+
|
318 |
+
# Combine the data to ensure both dataframes align for plotting
|
319 |
+
combined_df = pd.merge(daily_agents_df, cumulative_agents_df, on='date', how='left')
|
320 |
+
|
321 |
+
# Create the bar chart with side-by-side bars
|
322 |
fig_agents_registered = go.Figure(data=[
|
323 |
go.Bar(
|
324 |
name='Daily nr of Registered Agents',
|
325 |
+
x=combined_df['date'],
|
326 |
+
y=combined_df['daily_agent_count'],
|
327 |
marker_color='blue'
|
328 |
),
|
329 |
go.Bar(
|
330 |
name='Total Weekly Nr of Registered Agents',
|
331 |
+
x=combined_df['date'],
|
332 |
+
y=combined_df['weekly_agent_count'],
|
333 |
marker_color='purple'
|
334 |
)
|
335 |
])
|
336 |
+
|
337 |
+
# Update layout to group bars side by side for each day
|
338 |
fig_agents_registered.update_layout(
|
339 |
xaxis_title='Date',
|
340 |
yaxis_title='Number of Agents',
|
341 |
title="Nr of Agents Registered",
|
342 |
barmode='group',
|
343 |
+
yaxis=dict(tickmode='linear', tick0=0, dtick=1),
|
344 |
xaxis=dict(
|
345 |
tickmode='array',
|
346 |
+
tickvals=combined_df['date'],
|
347 |
+
ticktext=[d.strftime("%b %d") for d in combined_df['date']],
|
348 |
tickangle=-45
|
349 |
),
|
350 |
height=700,
|
351 |
+
bargap=0.1 # Adjust bargap for clearer side-by-side bars
|
352 |
)
|
353 |
+
|
354 |
# Calculate weekly average daily active agents
|
355 |
df_agents_with_transactions['day_of_week'] = df_agents_with_transactions['date'].dt.dayofweek
|
356 |
df_agents_with_transactions_weekly_avg = df_agents_with_transactions.groupby(['week', 'day_of_week'])['agent_count_with_transactions'].mean().reset_index()
|
app_trans_new.py
CHANGED
@@ -3,6 +3,8 @@ import pandas as pd
|
|
3 |
import gradio as gr
|
4 |
import plotly.express as px
|
5 |
from datetime import datetime, timedelta
|
|
|
|
|
6 |
import json
|
7 |
from web3 import Web3
|
8 |
import time
|
@@ -239,21 +241,6 @@ def fetch_transactions():
|
|
239 |
df_transactions_new.to_csv(csv_filename, index=False)
|
240 |
return df_transactions_new
|
241 |
|
242 |
-
def date_range_with_gaps(start_date, end_date):
|
243 |
-
"""Generates a range of dates from start_date to end_date inclusive, with extra days between weeks."""
|
244 |
-
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
|
245 |
-
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
|
246 |
-
delta = timedelta(days=1)
|
247 |
-
current_dt = start_dt
|
248 |
-
date_list = []
|
249 |
-
while current_dt <= end_dt:
|
250 |
-
date_list.append(current_dt.date())
|
251 |
-
if current_dt.weekday() == 6: # If it's Sunday, add gaps for better visibility
|
252 |
-
for _ in range(2):
|
253 |
-
current_dt += delta
|
254 |
-
date_list.append(current_dt.date())
|
255 |
-
current_dt += delta
|
256 |
-
return date_list
|
257 |
|
258 |
def create_transcation_visualizations():
|
259 |
df_transactions_new = fetch_transactions()
|
@@ -264,43 +251,138 @@ def create_transcation_visualizations():
|
|
264 |
|
265 |
daily_counts = daily_counts[['optimism', 'base', 'ethereum']]
|
266 |
|
267 |
-
#
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
)
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
|
296 |
-
#
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
)
|
302 |
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
|
305 |
|
306 |
# Gradio interface
|
@@ -317,6 +399,7 @@ def dashboard():
|
|
317 |
|
318 |
return demo
|
319 |
|
|
|
320 |
# Launch the dashboard
|
321 |
if __name__ == "__main__":
|
322 |
dashboard().launch()
|
|
|
3 |
import gradio as gr
|
4 |
import plotly.express as px
|
5 |
from datetime import datetime, timedelta
|
6 |
+
import plotly.graph_objects as go
|
7 |
+
import numpy as np
|
8 |
import json
|
9 |
from web3 import Web3
|
10 |
import time
|
|
|
241 |
df_transactions_new.to_csv(csv_filename, index=False)
|
242 |
return df_transactions_new
|
243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
|
245 |
def create_transcation_visualizations():
|
246 |
df_transactions_new = fetch_transactions()
|
|
|
251 |
|
252 |
daily_counts = daily_counts[['optimism', 'base', 'ethereum']]
|
253 |
|
254 |
+
# Read the original data
|
255 |
+
daily_counts['timestamp'] = daily_counts.index
|
256 |
+
daily_counts['timestamp'] = pd.to_datetime(daily_counts['timestamp'])
|
257 |
+
daily_counts = daily_counts.reset_index(drop=True)
|
258 |
+
# Get min and max dates
|
259 |
+
min_date = daily_counts['timestamp'].min()
|
260 |
+
max_date = daily_counts['timestamp'].max()
|
261 |
+
|
262 |
+
# Create complete date range
|
263 |
+
full_date_range = pd.date_range(start=min_date, end=max_date, freq='D')
|
264 |
+
|
265 |
+
# Create a new dataframe with all dates
|
266 |
+
complete_df = pd.DataFrame({'timestamp': full_date_range})
|
267 |
+
complete_df = complete_df.merge(daily_counts, on='timestamp', how='left')
|
268 |
+
complete_df = complete_df.fillna(0)
|
269 |
+
daily_counts = complete_df
|
270 |
+
# Convert timestamp to datetime
|
271 |
+
daily_counts['timestamp'] = pd.to_datetime(daily_counts['timestamp'])
|
272 |
+
|
273 |
+
# Create a new dataframe with 12-hour slots
|
274 |
+
new_rows = []
|
275 |
+
|
276 |
+
for _, row in daily_counts.iterrows():
|
277 |
+
# Create first 12-hour slot (0-12)
|
278 |
+
slot1 = row.copy()
|
279 |
+
# slot1['timestamp'] = row['timestamp'].replace(hour=0)
|
280 |
+
|
281 |
+
# # Create second 12-hour slot (12-24)
|
282 |
+
new_rows.extend([slot1])
|
283 |
+
slot2 = row.copy()
|
284 |
+
if slot2['timestamp'].dayofweek == 6: # 6 represents Sunday
|
285 |
+
slot2[['optimism', 'base', 'ethereum']] = 0
|
286 |
+
slot2['timestamp'] = row['timestamp'].replace(hour=12)
|
287 |
+
new_rows.extend([slot2])
|
288 |
+
|
289 |
+
|
290 |
|
291 |
+
# Create new dataframe with 12-hour slots
|
292 |
+
hourly_counts = pd.DataFrame(new_rows).sort_values('timestamp')
|
293 |
+
|
294 |
+
# Prepare data for plotting
|
295 |
+
dates = hourly_counts['timestamp'].tolist()
|
296 |
+
values = hourly_counts[['optimism', 'base', 'ethereum']].to_numpy()
|
297 |
+
|
298 |
+
# Create the figure
|
299 |
+
fig = go.Figure()
|
300 |
+
# Create arrays for positioning and width
|
301 |
+
# Convert dates to datetime objects first
|
302 |
+
date_objects = pd.to_datetime(dates)
|
303 |
+
|
304 |
+
# Create numeric indices for x-axis
|
305 |
+
x_numeric = np.arange(len(dates))
|
306 |
+
|
307 |
+
# Create arrays for positioning and width
|
308 |
+
width_array = []
|
309 |
+
|
310 |
+
for i, date in enumerate(date_objects):
|
311 |
+
width_array.append(1.0) # Full width for other days
|
312 |
+
|
313 |
+
# Get Monday indices for tick positions
|
314 |
+
monday_indices = [i for i, date in enumerate(date_objects) if date.dayofweek == 0]
|
315 |
+
monday_labels = [date_objects[i].strftime('%Y-%m-%d') for i in monday_indices]
|
316 |
+
|
317 |
+
# Add traces for each series
|
318 |
+
fig.add_trace(go.Bar(
|
319 |
+
name='Optimism',
|
320 |
+
x=x_numeric,
|
321 |
+
y=values[:,0],
|
322 |
+
marker_color='blue',
|
323 |
+
opacity=0.7,
|
324 |
+
text=None,
|
325 |
+
width=width_array,
|
326 |
+
textposition='none',
|
327 |
+
))
|
328 |
+
|
329 |
+
fig.add_trace(go.Bar(
|
330 |
+
name='Base',
|
331 |
+
x=x_numeric,
|
332 |
+
y=values[:,1],
|
333 |
+
marker_color='purple',
|
334 |
+
opacity=0.7,
|
335 |
+
text=None,
|
336 |
+
textposition='none',
|
337 |
+
width=width_array,
|
338 |
+
))
|
339 |
+
|
340 |
+
fig.add_trace(go.Bar(
|
341 |
+
name='Ethereum',
|
342 |
+
x=x_numeric,
|
343 |
+
y=values[:,2],
|
344 |
+
marker_color='darkgreen',
|
345 |
+
opacity=0.7,
|
346 |
+
text=None,
|
347 |
+
width=width_array,
|
348 |
+
textposition='none',
|
349 |
+
))
|
350 |
+
|
351 |
+
# Update layout with numeric x-axis
|
352 |
+
fig.update_layout(
|
353 |
+
title='Chain Daily Activity : Transactions',
|
354 |
+
xaxis_title='Date',
|
355 |
+
yaxis_title='Daily Transactions Count',
|
356 |
+
barmode='stack',
|
357 |
+
showlegend=True,
|
358 |
+
height=600,
|
359 |
+
bargap=0,
|
360 |
+
bargroupgap=0,
|
361 |
+
xaxis=dict(
|
362 |
+
tickangle=-45,
|
363 |
+
tickmode='array',
|
364 |
+
ticktext=monday_labels,
|
365 |
+
tickvals=monday_indices,
|
366 |
+
),
|
367 |
+
template='plotly_white',
|
368 |
+
hoverlabel=dict(
|
369 |
+
bgcolor="white",
|
370 |
+
font_size=12,
|
371 |
+
),
|
372 |
)
|
373 |
|
374 |
+
# Update hover template
|
375 |
+
for trace in fig.data:
|
376 |
+
trace.update(
|
377 |
+
hovertemplate="<b>Date:</b> %{text}<br>" +
|
378 |
+
"<b>" + trace.name + ":</b> %{y}<br>" +
|
379 |
+
"<extra></extra>",
|
380 |
+
text=[d.strftime('%Y-%m-%d') for d in date_objects] # Add date text for hover
|
381 |
+
)
|
382 |
+
|
383 |
+
# Show the plot
|
384 |
+
return fig
|
385 |
+
|
386 |
|
387 |
|
388 |
# Gradio interface
|
|
|
399 |
|
400 |
return demo
|
401 |
|
402 |
+
|
403 |
# Launch the dashboard
|
404 |
if __name__ == "__main__":
|
405 |
dashboard().launch()
|