forked from snikolov/rumor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_explore.py
222 lines (197 loc) · 8.3 KB
/
param_explore.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
import cloud
import itertools
import numpy as np
import pickle
import rumor
from datetime import datetime
from params import *
# TODO: use Params namedtuple rather than full argument list
def detect_trial(pos_path, neg_path, threshold, test_frac, cmpr_window, cmpr_step,
w_smooth, gamma, p_sample, detection_step, min_dist_step,
detection_window_hrs, req_consec_detections):
ts_pos = rumor.parsing.parse_timeseries_from_file(cloud.files.getf(pos_path), {})
ts_neg = rumor.parsing.parse_timeseries_from_file(cloud.files.getf(neg_path), {})
rumor.parsing.insert_timeseries_objects(ts_pos)
rumor.parsing.insert_timeseries_objects(ts_neg)
tstep = ts_pos[ts_pos.keys()[0]]['ts'].tstep
# It doesn't make sense for the comparison window to be as big or bigger
# than the detection window.
if cmpr_window >= detection_window_hrs * 3600 * 1000 / float(tstep):
return None
return rumor.processing.ts_shift_detect(ts_pos, ts_neg, threshold,
test_frac, cmpr_window,
cmpr_step, w_smooth, gamma,
p_sample, detection_step,
min_dist_step, detection_window_hrs,
req_consec_detections)
def detect_trials(pos_path, neg_path, threshold, test_frac, cmpr_window, cmpr_step,
w_smooth, gamma, p_sample, detection_step, min_dist_step,
detection_window_hrs, req_consec_detections):
trials = 5
pos_path_ = [pos_path] * trials
neg_path_ = [neg_path] * trials
threshold_ = [threshold] * trials
test_frac_ = [test_frac] * trials
cmpr_window_ = [cmpr_window] * trials
cmpr_step_ = [cmpr_step] * trials
w_smooth_ = [w_smooth] * trials
gamma_ = [gamma] * trials
p_sample_ = [p_sample] * trials
detection_step_ = [detection_step] * trials
min_dist_step_ = [min_dist_step] * trials
detection_window_hrs_ = [detection_window_hrs] * trials
req_consec_detections_ = [req_consec_detections] * trials
jids = cloud.map(detect_trial,
pos_path_,
neg_path_,
threshold_,
test_frac_,
cmpr_window_,
cmpr_step_,
w_smooth_,
gamma_,
p_sample_,
detection_step_,
min_dist_step_,
detection_window_hrs_,
req_consec_detections_,
_type = 'f2')
params = Params(pos_path, neg_path, threshold, test_frac, cmpr_window,
cmpr_step, w_smooth, gamma, p_sample, detection_step,
min_dist_step, detection_window_hrs, req_consec_detections)
return params, jids
def store_results(results, out_path):
f = open(out_path, 'w')
pickle.dump(results, f)
f.close()
def fix_results_nesting(results):
paramsets = results[0]
stats = results[1]
num_paramsets = len(paramsets)
num_stats = len(stats)
if num_stats % num_paramsets:
print 'Something\'s wrong here: %d stats from %d paramsets.' % \
(num_stats, num_paramsets)
return
num_trials = num_stats / num_paramsets
stats_iter = iter(stats)
statsets = []
for paramset in paramsets:
statset = []
for i in xrange(num_trials):
statset.append(stats_iter.next())
statsets.append(statset)
return (paramsets, statsets)
def summarize_results(results):
paramsets = results[0]
statsets = results[1]
for i in xrange(len(paramsets)):
paramset = paramsets[i]
statset = statsets[i]
print paramset
fprs = [ stats_for_trial['fpr']
for stats_for_trial in statset
if stats_for_trial and stats_for_trial['fpr']]
tprs = [ stats_for_trial['tpr']
for stats_for_trial in statset
if stats_for_trial and stats_for_trial['tpr']]
mean_earlies = [ np.mean(stats_for_trial['earlies'])
for stats_for_trial in statset
if stats_for_trial and len(stats_for_trial['earlies']) > 0 ]
std_earlies = [ np.std(stats_for_trial['earlies'])
for stats_for_trial in statset
if stats_for_trial and len(stats_for_trial['earlies']) > 0 ]
mean_lates = [ np.mean(stats_for_trial['lates'])
for stats_for_trial in statset
if stats_for_trial and len(stats_for_trial['lates']) > 0 ]
std_lates = [ np.std(stats_for_trial['lates'])
for stats_for_trial in statset
if stats_for_trial and len(stats_for_trial['lates']) > 0 ]
print 'mean fpr: ', np.mean(fprs)
print 'std fpr: ', np.std(fprs)
print 'mean tpr: ', np.mean(tprs)
print 'std tpr: ', np.std(tprs)
print 'mean_earlies: ', [ v / float(3600 * 1000) for v in mean_earlies ]
print 'std_earlies: ', [ v / float(3600 * 1000) for v in std_earlies ]
print 'mean_lates: ', [ v / float(3600 * 1000) for v in mean_lates ]
print 'std_lates: ', [ v / float(3600 * 1000) for v in std_lates ]
# Launch.
pos_path = ['statuses_news_rates_2m.tsv']
neg_path = ['statuses_nonviral_rates_2m.tsv']
threshold = [1, 3]
test_frac = [0.5]
cmpr_window = [10, 80, 150]
cmpr_step = [None]
w_smooth = [10, 80, 150]
gamma = [0.1, 1, 10]
p_sample = [0.5]
detection_step = [None]
min_dist_step = [None]
detection_window_hrs = [3, 5, 7]
req_consec_detections = [1, 3]
param_product = itertools.product(pos_path,
neg_path,
threshold,
test_frac,
cmpr_window,
cmpr_step,
w_smooth,
gamma,
p_sample,
detection_step,
min_dist_step,
detection_window_hrs,
req_consec_detections)
"""
param_product_old = itertools.product(pos_path,
neg_path,
threshold,
test_frac,
cmpr_window,
cmpr_step,
w_smooth,
gamma,
p_sample,
detection_step,
min_dist_step,
detection_window_hrs,
req_consec_detections)
threshold = [0.65, 1, 3]
cmpr_window = [10, 80, 115, 150]
w_smooth = [10, 80, 115, 150]
gamma = [0.1, 1, 10]
detection_window_hrs = [3, 5, 7, 9]
req_consec_detections = [1, 3, 5]
param_product_new = itertools.product(pos_path,
neg_path,
threshold,
test_frac,
cmpr_window,
cmpr_step,
w_smooth,
gamma,
p_sample,
detection_step,
min_dist_step,
detection_window_hrs,
req_consec_detections)
param_product_old = set(param_product_old)
param_product_new = set(param_product_new)
param_product = param_product_new.difference(param_product_old)
"""
jids = cloud.map(detect_trials,
*zip(*param_product),
_type = 'f2')
params_sub_jids = cloud.result(jids)
params = [ elt[0] for elt in params_sub_jids ]
sub_jids = [ elt[1] for elt in params_sub_jids ]
stats = cloud.result(sub_jids)
dt = datetime.now()
# Write out as plain text just in case.
out_path_txt = 'data/param_explore_%d%d%d%d%d%d.txt' % \
(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
open(out_path_txt, 'w').write(str((params, stats)))
params, stats = fix_results_nesting((params, stats))
out_path_pkl = 'data/param_explore_%d%d%d%d%d%d.pkl' % \
(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
store_results((params, stats), out_path_pkl)