-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_per_category.py
319 lines (275 loc) · 12.5 KB
/
users_per_category.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
307
308
309
310
311
312
313
314
315
316
317
318
319
""" Computing active and registered users on the network over time
"""
import altair as alt
import numpy as np
import pandas as pd
import infra.dask
import infra.pd
import infra.platform
def reduce_to_pandas(outfile, dask_client):
flows = infra.dask.read_parquet(
"data/clean/flows_typical_DIV_none_INDEX_start")[["user", "category", "org", "bytes_up", "bytes_down"]]
# Do the grouping
flows = flows.groupby(["user", "category", "org"]).sum()
flows = flows.compute()
infra.pd.clean_write_parquet(flows, outfile)
def make_category_plot(infile):
pd.set_option('display.max_columns', None)
grouped_flows = infra.pd.read_parquet(infile)
grouped_flows = grouped_flows.reset_index()
grouped_flows["bytes_total"] = grouped_flows["bytes_up"] + grouped_flows["bytes_down"]
# Figure out sorting order by total amount.
cat_totals = grouped_flows.groupby("category").sum().reset_index()
cat_sort_order = cat_totals.sort_values("bytes_total", ascending=False).set_index("bytes_total").reset_index()
cat_sort_list = cat_sort_order["category"].tolist()
user_totals = grouped_flows.groupby("user").sum().reset_index()
user_sort_order = user_totals.sort_values("bytes_total", ascending=False).set_index("bytes_total").reset_index()
user_sort_list = user_sort_order["user"].tolist()
grouped_flows["GB"] = grouped_flows["bytes_total"] / (1000**3)
grouped_flows = grouped_flows[["category", "user", "GB"]].groupby(["user", "category"]).sum()
grouped_flows = grouped_flows.reset_index()
grouped_flows["logGB"] = grouped_flows["GB"].transform(np.log10)
# Filter users by time in network to eliminate early incomplete samples
user_active_ranges = infra.pd.read_parquet("data/clean/user_active_deltas.parquet")[["user", "days_since_first_active", "days_active"]]
# Drop users that joined less than a week ago or were active for less than a week.
users_to_analyze = user_active_ranges.loc[
user_active_ranges["days_since_first_active"] >= 7
]
# Only needed if using the time normalized graph.
# # Drop users active for less than one week
# users_to_analyze = users_to_analyze.loc[
# users_to_analyze["days_active"] >= 7,
# ]
grouped_flows = grouped_flows.merge(users_to_analyze, on="user", how="inner")
alt.Chart(grouped_flows).mark_rect().encode(
x=alt.X("user:N",
title="User (Sorted by Total GB)",
axis=alt.Axis(labels=False),
sort=user_sort_list,
),
y=alt.Y("category:N",
title="Category (Sorted by Total GB)",
sort=cat_sort_list,
),
# shape="direction",
color=alt.Color(
"logGB:Q",
title="log(Total GB)",
scale=alt.Scale(scheme="viridis"),
),
).properties(
width=500,
).save(
"renders/users_per_category.png",
scale_factor=2,
)
# Normalize by each user's total spend to highlight categories
user_total_to_merge = user_totals[["user", "bytes_total"]].rename(columns={"bytes_total": "user_total_bytes"})
normalized_user_flows = grouped_flows.copy()
normalized_user_flows = normalized_user_flows.merge(user_total_to_merge, on="user")
normalized_user_flows["user_total_bytes"] = normalized_user_flows["user_total_bytes"] / 1000**3
normalized_user_flows["normalized_bytes"] = normalized_user_flows["GB"]/normalized_user_flows["user_total_bytes"]
alt.Chart(normalized_user_flows).mark_rect().encode(
x=alt.X("user:N",
title="User (Sorted by Total GB)",
axis=alt.Axis(labels=False),
sort=user_sort_list,
),
y=alt.Y("category:N",
title="Category (Sorted by Total GB)",
sort=cat_sort_list,
),
# shape="direction",
color=alt.Color(
"normalized_bytes:Q",
title="Normalized (Per User) Traffic",
scale=alt.Scale(scheme="viridis"),
),
).properties(
width=500,
).save(
"renders/users_per_category_normalized_user_total.png",
scale_factor=2,
)
# Normalize by each user's time in network to better compare users
time_normalized_flows = grouped_flows
time_normalized_flows["MB_per_day"] = time_normalized_flows["GB"] * 1000 / time_normalized_flows["days_active"]
time_normalized_flows["log_MB_per_day"] = time_normalized_flows["MB_per_day"].transform(np.log10)
alt.Chart(time_normalized_flows).mark_rect().encode(
x=alt.X("user:N",
title="User (Sorted by Total)",
axis=alt.Axis(labels=False),
sort=user_sort_list,
),
y=alt.Y("category:N",
title="Category (Sorted by Total)",
sort=cat_sort_list,
),
# shape="direction",
color=alt.Color(
"log_MB_per_day:Q",
title="MB per Day (Log Transformed)",
scale=alt.Scale(scheme="viridis"),
),
).properties(
width=500,
).save(
"renders/users_per_category_normalized_time.png",
scale_factor=2,
)
def make_category_plot_separate_top_n(infile, n_to_separate=20):
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.width', None)
grouped_flows = infra.pd.read_parquet(infile)
grouped_flows = grouped_flows.reset_index()
grouped_flows["bytes_total"] = grouped_flows["bytes_up"] + grouped_flows["bytes_down"]
# Figure out sorting order by total amount.
cat_totals = grouped_flows.groupby("category").sum().reset_index()
cat_sort_order = cat_totals.sort_values("bytes_total", ascending=False).set_index("bytes_total").reset_index()
cat_sort_list = cat_sort_order["category"].tolist()
user_totals = grouped_flows.groupby("user").sum().reset_index()
user_sort_order = user_totals.sort_values("bytes_total", ascending=False).set_index("bytes_total").reset_index()
user_sort_list = user_sort_order["user"].tolist()
# Generate a frame from the sorted user list that identifies the top users
top_annotation_frame = user_sort_order[["user"]]
bottom_n = len(user_sort_order) - n_to_separate
top_annotation_frame = top_annotation_frame.assign(topN="Bottom {}".format(bottom_n))
top_annotation_frame.loc[top_annotation_frame.index < n_to_separate, "topN"] = "Top {}".format(n_to_separate)
grouped_flows["GB"] = grouped_flows["bytes_total"] / (1000**3)
grouped_flows = grouped_flows[["category", "user", "GB"]].groupby(["user", "category"]).sum()
grouped_flows = grouped_flows.reset_index()
grouped_flows["logGB"] = grouped_flows["GB"].transform(np.log10)
grouped_flows = grouped_flows.merge(top_annotation_frame, on="user")
alt.Chart(grouped_flows).mark_rect().encode(
x=alt.X("user:N",
title="User (Sorted by Total GB)",
axis=alt.Axis(labels=False),
sort=user_sort_list,
),
y=alt.Y("category:N",
title="Category (Sorted by Total GB)",
sort=cat_sort_list,
),
# shape="direction",
color=alt.Color(
"GB:Q",
title="Total GB",
scale=alt.Scale(scheme="viridis"),
),
).facet(
column=alt.Column(
"topN:N",
sort="descending",
title="",
),
).resolve_scale(
x="independent",
color="independent"
).save(
"renders/users_per_category_split_outliers.png",
scale_factor=2,
)
def make_org_plot(infile):
""" Generate plots to explore the traffic distribution across organizations
"""
pd.set_option('display.max_columns', None)
grouped_flows = infra.pd.read_parquet(infile)
grouped_flows = grouped_flows.reset_index()
grouped_flows["bytes_total"] = grouped_flows["bytes_up"] + grouped_flows["bytes_down"]
# If any orgs are visited by fewer than 5 participants, need to be "other" per IRB
user_count = grouped_flows.copy()[["org", "user", "bytes_total"]]
user_count = user_count.set_index("bytes_total")
user_count = user_count.drop(0).reset_index()
user_count = user_count.groupby(["org", "user"]).sum().reset_index()
user_count = user_count.groupby(["org"]).count()
small_orgs = user_count.loc[user_count["user"] < 5]
small_orgs = small_orgs.reset_index()["org"]
grouped_flows = grouped_flows.replace(small_orgs.values, value="Aggregated (Users < 5)")
# Filter users by time in network to eliminate early incomplete samples
user_active_ranges = infra.pd.read_parquet("data/clean/user_active_deltas.parquet")[["user", "days_since_first_active", "days_active"]]
# Drop users that joined less than a week ago or were active for less than a week.
users_to_analyze = user_active_ranges.loc[
user_active_ranges["days_since_first_active"] >= 7
]
grouped_flows = grouped_flows.merge(users_to_analyze, on="user", how="inner")
print(user_active_ranges.head(10))
# Figure out sorting order by total amount.
org_totals = grouped_flows.groupby("org").sum().reset_index()
org_sort_order = org_totals.sort_values("bytes_total", ascending=False).set_index("bytes_total").reset_index()
cat_sort_list = org_sort_order["org"].tolist()
user_totals = grouped_flows.groupby("user").sum().reset_index()
user_sort_order = user_totals.sort_values("bytes_total", ascending=False).set_index("bytes_total").reset_index()
user_sort_list = user_sort_order["user"].tolist()
grouped_flows["GB"] = grouped_flows["bytes_total"] / (1000**3)
grouped_flows = grouped_flows[["org", "user", "GB"]].groupby(["user", "org"]).sum()
grouped_flows = grouped_flows.reset_index()
grouped_flows["logGB"] = grouped_flows["GB"].transform(np.log10)
alt.Chart(grouped_flows).mark_rect().encode(
x=alt.X("user:N",
title="User (Sorted by Total GB)",
axis=alt.Axis(labels=False),
sort=user_sort_list,
),
y=alt.Y("org:N",
title="Organization (Sorted by Total GB)",
sort=cat_sort_list,
),
# shape="direction",
color=alt.Color(
"logGB:Q",
title="log(Total GB)",
scale=alt.Scale(scheme="viridis"),
),
).properties(
width=500,
).save(
"renders/users_per_category_org.png",
scale_factor=2,
)
# Normalize by each user's total spend to highlight categories
user_total_to_merge = user_totals[["user", "bytes_total"]].rename(columns={"bytes_total": "user_total_bytes"})
normalized_user_flows = grouped_flows.copy()
normalized_user_flows = normalized_user_flows.merge(user_total_to_merge, on="user")
normalized_user_flows["user_total_bytes"] = normalized_user_flows["user_total_bytes"] / 1000**3
normalized_user_flows["normalized_bytes"] = normalized_user_flows["GB"]/normalized_user_flows["user_total_bytes"]
alt.Chart(normalized_user_flows).mark_rect().encode(
x=alt.X("user:N",
title="User (Sorted by Total GB)",
axis=alt.Axis(labels=False),
sort=user_sort_list,
),
y=alt.Y("org:N",
title="Organization (Sorted by Total GB)",
sort=cat_sort_list,
),
# shape="direction",
color=alt.Color(
"normalized_bytes:Q",
title="Normalized (Per User) Traffic",
scale=alt.Scale(scheme="viridis"),
),
).properties(
width=500,
).save(
"renders/users_per_category_org_normalized.png",
scale_factor=2,
)
if __name__ == "__main__":
platform = infra.platform.read_config()
graph_temporary_file = "scratch/graphs/users_per_category"
if platform.large_compute_support:
print("Running compute tasks")
client = infra.dask.setup_platform_tuned_dask_client(10, platform)
reduce_to_pandas(outfile=graph_temporary_file, dask_client=client)
client.close()
if platform.altair_support:
print("Running vis tasks")
# Module specific format options
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_rows', None)
make_category_plot(graph_temporary_file)
make_org_plot(graph_temporary_file)
make_category_plot_separate_top_n(graph_temporary_file)
print("Done!")