-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate_active_per_user.py
More file actions
190 lines (158 loc) · 6.54 KB
/
Copy pathrate_active_per_user.py
File metadata and controls
190 lines (158 loc) · 6.54 KB
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
import altair as alt
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
import statsmodels.api as sm
import infra.dask
import infra.pd
import infra.platform
def reduce_to_pandas(outpath, dask_client):
flows = infra.dask.read_parquet(
"data/clean/flows/typical_fqdn_org_category_local_TM_DIV_none_INDEX_start")[["user", "bytes_up", "bytes_down"]]
flows["bytes_total"] = flows["bytes_up"] + flows["bytes_down"]
# Compress to days
flows = flows.reset_index()
flows["start_bin"] = flows["start"].dt.floor("d")
flows = flows.set_index("start_bin")
# Do the grouping
flows = flows.groupby(["start_bin", "user"]).sum()
flows = flows.reset_index()[["start_bin", "user", "bytes_total"]]
flows = flows.compute()
infra.pd.clean_write_parquet(flows, outpath)
def compute_cdf(frame, value_column, base_column):
# Find the PDF first
stats_frame = frame.groupby(value_column).count()[[base_column]].rename(columns = {base_column: "base_count"})
stats_frame["pdf"] = stats_frame["base_count"] / sum(stats_frame["base_count"])
stats_frame["cdf"] = stats_frame["pdf"].cumsum()
stats_frame = stats_frame.reset_index()
return stats_frame
def make_plot(inpath):
flows = infra.pd.read_parquet(inpath)
flows = flows.reset_index()
activity = infra.pd.read_parquet("data/clean/user_active_deltas.parquet")
# Drop users new to the network first active less than a week ago.
activity = activity.loc[
activity["days_since_first_active"] >= 7,
]
# Drop users active for less than 1 day
activity = activity.loc[
activity["days_active"] >= 1,
]
# take the minimum of days online and days active, since active is
# partial-day aware, but online rounds up to whole days. Can be up to 2-e
# days off if the user joined late in the day and was last active early.
activity["online_ratio"] = np.minimum(
activity["days_online"],
(activity["days_active"] - activity["outage_impact_days"])
) / (activity["days_active"] - activity["outage_impact_days"])
flows["MB"] = flows["bytes_total"] / (1000**2)
user_total = flows[["user", "MB"]]
user_total = user_total.groupby(["user"]).sum().reset_index()
df = user_total.merge(
activity[["user", "online_ratio", "days_online"]],
on="user",
)
df["MB_per_online_day"] = df["MB"] / df["days_online"]
# Log transform for analysis
df["log_MB_per_online_day"] = df["MB_per_online_day"].map(np.log)
df["log_online_ratio"] = df["online_ratio"].map(np.log)
# Print log stats info
x_log = df["log_MB_per_online_day"]
y_log = df["log_online_ratio"]
x_log_with_const = sm.add_constant(x_log)
estimate = sm.OLS(y_log, x_log_with_const)
estimate_fit = estimate.fit()
print("Stats info for log-transformded OLS linear fit")
print("P value", estimate_fit.pvalues[1])
print("R squared", estimate_fit.rsquared)
print(estimate_fit.summary())
# Print direct linear regression stats info
x = df["MB_per_online_day"]
y = df["online_ratio"]
x_with_const = sm.add_constant(x)
estimate = sm.OLS(y, x_with_const)
estimate_fit = estimate.fit()
print("Stats info for direct OLS linear fit")
print("P value", estimate_fit.pvalues[1])
print("R squared", estimate_fit.rsquared)
print(estimate_fit.summary())
# Reshape to generate column matrixes expected by sklearn
mb_array = df["MB_per_online_day"].values.reshape((-1, 1))
online_ratio_array = df["online_ratio"].values.reshape((-1, 1))
log_mb_array = df["log_MB_per_online_day"].values.reshape((-1, 1))
log_online_array = df["log_online_ratio"].values.reshape((-1, 1))
lin_regressor = LinearRegression()
lin_regressor.fit(mb_array, online_ratio_array)
logt_regressor = LinearRegression()
logt_regressor.fit(log_mb_array, log_online_array)
# Generate a regression plot
uniform_x = np.linspace(start=mb_array.min(), stop=mb_array.max(), num=1000, endpoint=True).reshape((-1, 1))
predictions = lin_regressor.predict(uniform_x)
log_x = np.log(uniform_x)
logt_predictions = logt_regressor.predict(log_x)
logt_predictions = np.exp(logt_predictions)
regression_frame = pd.DataFrame({"regressionX": uniform_x.flatten(), "predictions": predictions.flatten()})
regression_frame = regression_frame.assign(type="Linear(P<0.01, R²=0.05)")
logt_frame = pd.DataFrame({"regressionX": uniform_x.flatten(), "predictions": logt_predictions.flatten()})
logt_frame = logt_frame.assign(type="Log Transformed Linear(P<0.005, R²=0.05)")
regression_frame = regression_frame.append(logt_frame)
scatter = alt.Chart(df).mark_point(opacity=0.9, strokeWidth=1.5).encode(
x=alt.X(
"MB_per_online_day",
title="Mean MB per Day Online",
),
y=alt.Y(
"online_ratio",
title="Online Days / Active Days",
# scale=alt.Scale(type="linear", domain=(0, 1.0)),
),
)
regression = alt.Chart(logt_frame).mark_line(color="black", opacity=1).encode(
x=alt.X(
"regressionX",
# scale=alt.Scale(
# type="log",
# ),
),
y=alt.Y(
"predictions",
# scale=alt.Scale(
# type="log",
# ),
),
strokeDash=alt.StrokeDash(
"type",
title=None,
legend=alt.Legend(
orient='none',
fillColor="white",
labelLimit=500,
padding=5,
strokeColor="black",
legendX=300,
legendY=255,
)
),
)
(scatter + regression).properties(
width=500,
height=250,
).save(
"renders/rate_active_per_user.png", scale_factor=2.0
)
if __name__ == "__main__":
platform = infra.platform.read_config()
# Module specific format options
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
pd.set_option('display.width', None)
pd.set_option('display.max_rows', 40)
graph_temporary_file = "scratch/graphs/rate_active_per_user"
if platform.large_compute_support:
print("Running compute subcommands")
client = infra.dask.setup_platform_tuned_dask_client(per_worker_memory_GB=10, platform=platform)
reduce_to_pandas(outpath=graph_temporary_file, dask_client=client)
client.close()
if platform.altair_support:
make_plot(inpath=graph_temporary_file)
print("Done!")