forked from nOkuda/activetm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
342 lines (316 loc) · 11.6 KB
/
main.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import argparse
import datetime
from email.mime.text import MIMEText
import logging
import os
import shutil
import subprocess
import sys
import threading
import time
import numpy as np
import ankura
from activetm import plot
from activetm import utils
'''
The output from an experiment should take the following form:
output_directory
settings1
run1_1
run2_1
...
settings2
...
In this way, it gets easier to plot the results, since each settings will make a
line on the plot, and each line will be aggregate data from multiple runs of the
same settings.
'''
class JobThread(threading.Thread):
def __init__(self, host, working_dir, settings, outputdir, label):
threading.Thread.__init__(self)
self.daemon = True
self.host = host
self.working_dir = working_dir
self.settings = settings
self.outputdir = outputdir
self.label = label
self.killed = False
# TODO use asyncio when code gets upgraded to Python 3
def run(self):
p = subprocess.Popen(['ssh',
self.host,
'python3 ' + os.path.join(self.working_dir, 'submain.py') + ' ' +\
self.working_dir + ' ' +\
self.settings + ' ' +\
self.outputdir + ' ' +\
self.label + '; exit 0'])
while True:
time.sleep(1)
if self.killed:
p.kill()
break
if p.poll() is not None:
break
class PickleThread(threading.Thread):
def __init__(self, host, working_dir, work, outputdir, lock):
threading.Thread.__init__(self)
self.daemon = True
self.host = host
self.working_dir = working_dir
self.work = work
self.outputdir = outputdir
self.lock = lock
def run(self):
while True:
with self.lock:
if len(self.work) <= 0:
break
else:
settings = self.work.pop()
subprocess.check_call(['ssh', '-t',
self.host,
'python3 ' + os.path.join(self.working_dir, 'pickle_data.py') + ' '+\
settings + ' ' +\
self.outputdir + '; exit 0'])
def generate_settings(filename):
with open(filename) as ifh:
for line in ifh:
line = line.strip()
if line:
yield line
def get_hosts(filename):
hosts = []
with open(args.hosts) as ifh:
for line in ifh:
line = line.strip()
if line:
hosts.append(line)
return hosts
def check_counts(hosts, settingscount):
if len(hosts) != settingscount:
logging.getLogger(__name__).error('Node count and settings count do not match!')
sys.exit(1)
def get_groups(config):
result = set()
settings = generate_settings(config)
for s in settings:
d = utils.parse_settings(s)
result.add(d['group'])
return sorted(list(result))
def pickle_data(hosts, settings, working_dir, outputdir):
picklings = set()
work = set()
for s in settings:
pickle_name = utils.get_pickle_name(s)
if pickle_name not in picklings:
picklings.add(pickle_name)
work.add(s)
lock = threading.Lock()
threads = []
for h in set(hosts):
t = PickleThread(h, working_dir, work, outputdir, lock)
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
def run_jobs(hosts, settings, working_dir, outputdir):
threads = []
try:
for h, s, i in zip(hosts, settings, range(len(hosts))):
t = JobThread(h, working_dir, s, outputdir, str(i))
t.daemon = True
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
except KeyboardInterrupt:
logging.getLogger(__name__).warning('Killing children')
for t in threads:
t.killed = True
for t in threads:
t.join()
runningdir = os.path.join(outputdir, 'running')
for d in os.listdir(runningdir):
parts = d.split('.')
subprocess.call(['ssh', parts[0],
'kill -s 9 ' + parts[-1] + '; exit 0'])
sys.exit(-1)
def extract_data(fpath):
data = []
with open(fpath) as ifh:
for line in ifh:
line = line.strip()
if line and not line.startswith('#'):
results = line.split()
if len(data) == 0:
for _ in range(len(results)):
data.append([])
for i, r in enumerate(results):
data[i].append(float(r))
return data
def get_data(dirname):
data = []
for f in os.listdir(dirname):
fpath = os.path.join(dirname, f)
if os.path.isfile(fpath):
data.append(extract_data(fpath))
return data
def get_stats(mat):
# compute the medians along the columns
mat_medians = np.median(mat, axis=0)
# compute the means along the columns
mat_means = np.mean(mat, axis=0)
# find difference of means from first quartile along the columns
mat_errs_minus = mat_means - np.percentile(mat, 25, axis=0)
# compute third quartile along the columns; find difference from means
mat_errs_plus = np.percentile(mat, 75, axis=0) - mat_means
return mat_medians, mat_means, mat_errs_plus, mat_errs_minus
def make_plots(outputdir, dirs, loss_delta):
colors = plot.get_separate_colors(len(dirs))
dirs.sort()
count_plot = plot.Plotter(colors)
select_and_train_plot = plot.Plotter(colors)
time_plot = plot.Plotter(colors)
mae_plot = plot.Plotter(colors)
ymax = float('-inf')
for d in dirs:
curdir = os.path.join(outputdir, d)
data = np.array(get_data(curdir))
# for the first document, read off first dimension (the labeled set
# counts)
counts = data[0,0,:]
# set up a 2D matrix with each experiment on its own row and each
# experiment's pR^2 results in columns
ys_mat = data[:,-1,:]
ys_medians, ys_means, ys_errs_minus, ys_errs_plus = get_stats(ys_mat)
ys_errs_plus_max = max(ys_errs_plus + ys_means)
if ys_errs_plus_max > ymax:
ymax = ys_errs_plus_max
# set up a 2D matrix with each experiment on its own row and each
# experiment's time results in columns
times_mat = data[:,1,:]
times_medians, times_means, times_errs_minus, times_errs_plus = \
get_stats(times_mat)
count_plot.plot(
counts,
ys_means,
d,
ys_medians,
[ys_errs_minus, ys_errs_plus])
time_plot.plot(
times_means,
ys_means,
d,
ys_medians,
[ys_errs_minus, ys_errs_plus],
times_medians,
[times_errs_minus, times_errs_plus])
select_and_train_mat = data[:,2,:]
sandt_medians, sandt_means, sandt_errs_minus, sandt_errs_plus = \
get_stats(select_and_train_mat)
select_and_train_plot.plot(
counts,
sandt_means,
d,
sandt_medians,
[sandt_errs_minus, sandt_errs_plus])
# get mae results
losses = []
for maedir in os.listdir(curdir):
curmaedir = os.path.join(curdir, maedir)
if os.is_dir(curmaedir):
losses.append([])
for i in range(len(counts)):
maedata = np.loadtxt(os.path.join(curmaedir, str(i)))
# generalized zero-one loss
losses[-1].append(np.sum(maedata < loss_delta) / len(maedata))
losses = np.array(losses)
mae_medians, mae_means, mae_errs_minus, mae_errs_plus = \
get_stats(losses)
mae_plot.plot(
counts,
mae_means,
d,
mae_medians,
[mae_errs_minus, mae_errs_plus])
corpus = os.path.basename(outputdir)
count_plot.set_xlabel('Number of Labeled Documents')
count_plot.set_ylabel('pR$^2$')
count_plot.set_ylim([-0.05, ymax])
count_plot.savefig(os.path.join(outputdir, corpus+'.counts.pdf'))
time_plot.set_xlabel('Time elapsed (seconds)')
time_plot.set_ylabel('pR$^2$')
time_plot.set_ylim([-0.05, ymax])
time_plot.savefig(os.path.join(outputdir,
corpus+'.times.pdf'))
select_and_train_plot.set_xlabel('Number of Labeled Documents')
select_and_train_plot.set_ylabel('Time to select and train')
select_and_train_plot.savefig(os.path.join(outputdir,
corpus+'.select_and_train.pdf'))
def send_notification(email, outdir, run_time):
msg = MIMEText('Run time: '+str(run_time))
msg['Subject'] = 'Experiment Finished for '+outdir
msg['From'] = email
msg['To'] = email
p = os.popen('/usr/sbin/sendmail -t -i', 'w')
p.write(msg.as_string())
status = p.close()
if status:
logging.getLogger(__name__).warning('sendmail exit status '+str(status))
def slack_notification(msg):
slackhook = 'https://hooks.slack.com/services/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'
payload = 'payload={"channel": "#potatojobs", "username": "potatobot", ' +\
'"text": "'+msg+'", "icon_emoji": ":fries:"}'
subprocess.call([
'curl', '-X', 'POST', '--data-urlencode', payload,
slackhook])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Launcher for ActiveTM '
'experiments')
parser.add_argument('hosts', help='hosts file for job '
'farming')
parser.add_argument('working_dir', help='ActiveTM directory '
'available to hosts (should be a network path)')
parser.add_argument('config', help=\
'''a file with the path to a settings file on each line.
The file referred to should follow the settings specification
as discussed in README.md in the root ActiveTM directory''')
parser.add_argument('outputdir', help='directory for output (should be a '
'network path)')
parser.add_argument('email', help='email address to send to when job '
'completes', nargs='?')
args = parser.parse_args()
try:
begin_time = datetime.datetime.now()
slack_notification('Starting job: '+args.outputdir)
runningdir = os.path.join(args.outputdir, 'running')
if os.path.exists(runningdir):
shutil.rmtree(runningdir)
try:
os.makedirs(runningdir)
except OSError:
pass
hosts = get_hosts(args.hosts)
check_counts(hosts, utils.count_settings(args.config))
if not os.path.exists(args.outputdir):
logging.getLogger(__name__).error('Cannot write output to: '+args.outputdir)
sys.exit(-1)
groups = get_groups(args.config)
settings = generate_settings(args.config)
pickle_data(hosts, settings, args.working_dir, args.outputdir)
run_jobs(hosts, settings, args.working_dir,
args.outputdir)
loss_delta = float(settings.get('loss_delta'), 0.01)
make_plots(args.outputdir, groups, )
run_time = datetime.datetime.now() - begin_time
with open(os.path.join(args.outputdir, 'run_time'), 'w') as ofh:
ofh.write(str(run_time))
os.rmdir(runningdir)
slack_notification('Job complete: '+args.outputdir)
if args.email:
send_notification(args.email, args.outputdir, run_time)
except:
slack_notification('Job died: '+args.outputdir)
raise