forked from LessWrong2/Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcellularautomaton.py
110 lines (92 loc) · 3.72 KB
/
cellularautomaton.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
import pandas as pd
from gspread_pandas import Spread, Client
from utils import timed, get_config_field
def create_and_update_user_sheet(dfu, spreadsheet, num_rows=None):
data = dfu[~dfu['banned']].sort_values('karma', ascending=False)
data.loc[data['days_since_active'] <= 0, 'days_since_active'] = 0
data['username'] = '=HYPERLINK("www.lesswrong.com/users/'.lower() + data['username'] + '", "' + data[
'username'] + '")'
data['birth'] = pd.datetime.now()
# data['most_recent_activity'] = data['most_recent_activity'].dt.date
user_cols = [
'birth',
'_id',
'username',
'karma',
'days_since_active',
'most_recent_activity',
'num_days_present_last_30_days',
'num_distinct_posts_viewed_last_30_days',
'num_votes_last_30_days',
'num_comments_last_30_days',
'num_posts_last_30_days',
'num_views_last_180_days',
'num_votes_last_180_days',
'num_comments_last_180_days',
'num_posts_last_180_days',
]
recent_count_cols = [
'num_distinct_posts_viewed_last_30_days',
'num_votes_last_30_days',
'num_comments_last_30_days',
'num_posts_last_30_days',
'num_views_last_180_days',
'num_votes_last_180_days',
'num_comments_last_180_days',
'num_posts_last_180_days']
if num_rows:
data = data[user_cols].head(num_rows)
else:
data = data[user_cols]
data.loc[:, recent_count_cols] = data[recent_count_cols].fillna(0)
data.columns = [col.replace('_', ' ').title() for col in data.columns]
spreadsheet.df_to_sheet(data, replace=True, sheet='Users', index=False)
return data
def create_and_update_posts_sheet(dfp, spreadsheet, num_rows=None):
data = dfp[(~dfp['draft']) & (dfp[['smallUpvote', 'bigUpvote']].sum(axis=1) >= 2)].sort_values('postedAt',
ascending=False)
data['title'] = '=HYPERLINK("www.lesswrong.com/posts/' + data['_id'] + '", "' + data['title'].str.replace('"','""') + '")'
data['birth'] = pd.datetime.now()
data['postedAt'] = data['postedAt'].dt.date
post_cols = [
'birth',
'_id',
'postedAt',
'username',
'title',
'baseScore',
'frontpaged',
'question',
'num_comments_rederived',
'num_distinct_viewers',
'num_votes',
'percent_downvotes',
'viewCountLogged',
'af',
'curatedDate',
'wordCount'
]
int_cols = ['num_comments_rederived', 'viewCountLogged', 'num_votes', 'num_distinct_viewers']
if num_rows:
data = data[post_cols].head(num_rows)
else:
data = data[post_cols]
data.loc[:, int_cols] = data[int_cols].fillna(0)
data.columns = [col.replace('_', ' ').title() for col in data.columns]
spreadsheet.df_to_sheet(data, replace=True, sheet='Posts (2+ upvotes)', index=False)
return data
def create_and_update_votes_sheet(dfv, spreadsheet, num_days=180):
data = dfv[dfv['votedAt'] >= dfv['votedAt'].max() - pd.Timedelta(num_days, unit='d')].sort_values('votedAt', ascending=False)
data['birth'] = pd.datetime.now()
spreadsheet.df_to_sheet(data, replace=True, sheet='Votes (last 180 days)', index=False)
return data
@timed
def create_and_update_all_sheets(dfs, spreadsheet_name):
dfu = dfs['users']
dfp = dfs['posts']
dfv = dfs['votes']
s = Spread(get_config_field('GSHEETS', 'user'), spreadsheet_name, sheet='Users', create_spread=True, create_sheet=True)
_ = create_and_update_user_sheet(dfu, s)
_ = create_and_update_posts_sheet(dfp, s)
_ = create_and_update_votes_sheet(dfv, s)
return s