-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_serie_plot.py
109 lines (91 loc) · 3.33 KB
/
time_serie_plot.py
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
import os
import pandas as pd
import plotly.graph_objects as go
DATE_FORMAT: str = '%b, %Y'
DARK_BLUE: str = '#2471a3'
LIGHT_BLUE: str = '#d4e6f1'
GRID_COLOR: str = '#B0BEC5'
def update_layout(df: pd.DataFrame, fig: go.Figure) -> None:
fig.update_layout(
xaxis={
'tickvals': df['date'],
'tickformat': DATE_FORMAT,
'tickangle': 45,
'type': 'date',
'ticks': 'outside',
'ticklen': 5,
'tickwidth': 2,
'showline': True,
'linewidth': 2,
'linecolor': 'black',
'tickfont': {'size': 14},
},
yaxis={
'showgrid': True,
'gridwidth': 0.5,
'gridcolor': GRID_COLOR,
'ticks': 'outside',
'ticklen': 5,
'tickwidth': 2,
'showline': True,
'linewidth': 2,
'linecolor': 'black',
#'dtick': 20,
'tickfont': {'size': 14},
},
plot_bgcolor="white",
)
def plot(df: pd.DataFrame, y_key: str) -> None:
colors: list = [DARK_BLUE] * len(df.index)
colors[-1] = LIGHT_BLUE
fig = go.Figure(data=[
go.Scatter(
x=df['date'],
y=df[y_key],
marker_color=colors,
marker_size=12,
marker_line_width=2,
line_color=DARK_BLUE)
])
update_layout(df, fig)
fig.show()
def process_df(df: pd.DataFrame, key_id: str) -> pd.DataFrame:
dff = df
dff = dff.groupby([key_id, 'date']).size().reset_index(name='nActions')
# before remove nActions use it if you want to filter more than one action
dff = dff.drop(columns=['nActions'])
dff = dff.groupby(['date']).size().reset_index(name='actives')
return dff
if __name__ == '__main__':
filename: str = os.path.join('datawarehouse', 'activity_serie.csv')
df: pd.DataFrame = pd.read_csv(filename, header=0)
# let's transform the date
df['unixDate'] = pd.to_datetime(df['unixDate'], unit='s').dt.date
df = df.rename(columns={'unixDate': 'date'})
# transform to monthly date
df['date'] = df['date'].apply(lambda d: d.replace(day=1))
# active DAOs
dff = df.drop(columns=['daoName', 'actionType', 'userId'])
dff = process_df(dff, 'daoId')
print(f'Mean active DAOs = {sum(dff["actives"].tolist()) / len(dff["actives"].tolist())}')
plot(dff, 'actives')
# active users
dff = df
#dff = dff[dff['daoId'] == '0x294f999356ed03347c7a23bcbcf8d33fa41dc830']
dff = dff.drop(columns=['daoName', 'daoId', 'actionType'])
dff = process_df(dff, 'userId')
print(f'Mean active users = {sum(dff["actives"].tolist()) / len(dff["actives"].tolist())}')
plot(dff, 'actives')
# new proposals
dff = df[df['actionType'] == 'proposal']
dff = dff.drop(columns=['daoName', 'daoId', 'actionType', 'userId'])
dff = dff.groupby(['date']).size().reset_index(name='nProposals')
print(f'Total proposals = {sum(dff["nProposals"].tolist())}')
plot(dff, 'nProposals')
# total actions
dff = df
# dff = dff[dff['daoId'] == '0x294f999356ed03347c7a23bcbcf8d33fa41dc830']
dff = dff.drop(columns=['daoName', 'daoId', 'actionType', 'userId'])
dff = dff.groupby(['date']).size().reset_index(name='actions')
print(f'Total actions = {sum(dff["actions"].tolist())}')
plot(dff, 'actions')