This repository has been archived by the owner on Jan 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bputilities.py
650 lines (596 loc) · 21.7 KB
/
bputilities.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# Utility functions for Batch Profiler
import datetime
import dateutil.parser
import numpy as np
import pipes
import os
import re
import sys
from cStringIO import StringIO
import subprocess
import tempfile
import stat
from bpformdata import PREFIX, LC_ALL, BATCHPROFILER_CPCHECKOUT, \
BATCHPROFILER_CELLPROFILER_REPO
QSUB_WORKS = True
SENDMAIL="/usr/sbin/sendmail"
BUILD_TOUCHFILE = ".cellprofiler.built"
ROOT_DIR = BATCHPROFILER_CELLPROFILER_REPO
DATEVERSION_PATTERN="(?P<year>20\\d{2})(?P<month>\\d{2})(?P<day>\\d{2})(?P<hour>\\d{2})(?P<minute>\\d{2})(?P<second>\\d{2})"
def get_batch_data_version_and_githash(batch_filename):
"""Get the commit's GIT hash stored in the batch file's pipeline
batch_filename - Batch_data.h5 file to look at
returns the GIT hash as stored in the file.
"""
from cellprofiler.utilities.hdf5_dict import HDF5Dict
import cellprofiler.measurements as cpmeas
import cellprofiler.pipeline as cpp
m = HDF5Dict(batch_filename, mode="r")
pipeline_txt = cpmeas.Measurements.unwrap_string(
m[cpmeas.EXPERIMENT, cpp.M_PIPELINE, 0][0])
if isinstance(pipeline_txt, unicode):
pipeline_txt = pipeline_txt.encode("utf-8")
#
# TODO:
# It's not such a good idea to duplicate parsing of the pipeline
# text. Create a method to read the header info.
#
try:
git_hash = None
version = None
pipeline_version = None
lines = [_.strip() for _ in pipeline_txt.split("\n")]
if lines[0] != cpp.COOKIE:
raise ValueError("Failed to recognize cookie")
for line in lines[1:]:
if len(line) == 0:
break
k, v = line.split(":")
if k == cpp.H_DATE_REVISION:
version = v
elif k == cpp.H_GIT_HASH:
git_hash = v
elif k == cpp.H_VERSION:
pipeline_version = int(v)
except:
pipeline = cpp.Pipeline()
version, git_hash = pipeline.loadtxt(StringIO(pipeline_txt))
if git_hash is None:
for log_version, log_git_hash in get_versions_and_githashes():
if int(log_version) == version:
git_hash = log_git_hash
break
else:
version, git_hash = get_version_and_githash(git_hash)
return version, git_hash
def get_batch_image_numbers(batch_filename):
from cellprofiler.utilities.hdf5_dict import HDF5Dict
import cellprofiler.measurements as cpmeas
m = HDF5Dict(batch_filename, mode="r")
image_numbers = np.array(
m.get_indices(cpmeas.IMAGE, cpmeas.IMAGE_NUMBER).keys(), int)
image_numbers.sort()
return image_numbers
def get_batch_groups(batch_filename):
from cellprofiler.utilities.hdf5_dict import HDF5Dict
import cellprofiler.measurements as cpmeas
image_numbers = get_batch_image_numbers(batch_filename)
m = HDF5Dict(batch_filename, mode="r")
if m.has_feature(cpmeas.IMAGE, cpmeas.GROUP_NUMBER):
group_numbers = np.array(
[_[0] for _ in m[cpmeas.IMAGE, cpmeas.GROUP_NUMBER, image_numbers]])
if len(np.unique(group_numbers)) <= 1:
return
group_indices = np.array(
[_[0] for _ in m[cpmeas.IMAGE, cpmeas.GROUP_INDEX, image_numbers]])
return group_numbers, group_indices
def get_version_and_githash(treeish):
subprocess.check_call(
["git", "fetch", "origin", "master"], cwd=ROOT_DIR)
if re.match(DATEVERSION_PATTERN, treeish):
for dateversion, githash in get_versions_and_githashes():
if dateversion == treeish:
return dateversion, githash
else:
raise ValueError("Could not find commit: %s", treeish)
#
# Give me another reason to hate GIT:
# 2.1.1 preferentially matches 2.1.1-docker
#
tag_pattern = "[0-9]+\\.[0-9]+\\.[0-9]+"
if re.match(tag_pattern, treeish) is not None:
line = subprocess.check_output(
["git", "log", "-n", "1", "--tags=%s" %
tag_pattern.replace("\\", "\\\\"),
"--pretty=%ai_%H", treeish],
cwd=ROOT_DIR)
else:
line = subprocess.check_output(
["git", "log", "-n", "1", "--pretty=%ai_%H", treeish],
cwd=ROOT_DIR)
time_str, git_hash = [x.strip() for x in line.split("_")]
return get_version_from_timestr(time_str), git_hash
def get_version_from_timestr(time_str):
'''convert ISO date to dateversion format
Returns the UTC time of the time string in the format
YYYYMMDDHHMMSS
'''
t = dateutil.parser.parse(time_str).utctimetuple()
return "%04d%02d%02d%02d%02d%02d" % \
( t.tm_year, t.tm_mon, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec)
def get_versions_and_githashes():
'''Get the versions and githashes via git-log'''
splooge = subprocess.check_output(
["git", "log", "--pretty=%ai_%H"], cwd = ROOT_DIR)
result = []
for line in splooge.split("\n"):
try:
time_str, git_hash = [x.strip() for x in line.split("_")]
result.append((get_version_from_timestr(time_str), git_hash))
except:
pass
return result
def get_cellprofiler_location(
batch_filename = None, version = None, git_hash=None):
'''Get the location of the CellProfiler source to use
There are two choices - get by batch name or by version and git hash
'''
if version is None or git_hash is None:
version, git_hash = get_batch_data_version_and_githash(batch_filename)
path = os.path.join(BATCHPROFILER_CPCHECKOUT,
"%s_%s" % (version, git_hash))
return path
def get_queues():
'''Return a list of queues'''
try:
host_fd, host_scriptfile = tempfile.mkstemp(suffix=".sh")
os.fchmod(host_fd, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
os.write(host_fd, "#!/bin/sh\n")
os.write(host_fd, """if [ -e "$HOME/.batchprofiler.sh" ]; then
. "$HOME/.batchprofiler.sh"
fi
""")
os.write(host_fd, "qconf -sql\n")
os.close(host_fd)
process = subprocess.Popen(
[host_scriptfile],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
return filter((lambda x: len(x) > 0), [x.strip() for x in stdout.split("\n")])
finally:
os.unlink(host_scriptfile)
def get_jobs():
'''Return a list of all jobs for the webserver user'''
script = """#!/bin/sh
set -v
if [ -e "$HOME/.batchprofiler.sh" ]; then
. "$HOME/.batchprofiler.sh"
fi
set +v
qstat
"""
scriptfile = make_temp_script(script)
try:
output = subprocess.check_output([scriptfile])
result = []
for line in output.split("\n"):
fields = [x.strip() for x in line.strip().split(" ")]
try:
if len(fields) > 0:
result.append(int(fields[0]))
except:
pass
finally:
os.rmdir(scriptfile)
return result
def make_temp_script(script):
'''Write a script to a tempfile
'''
host_fd, host_scriptfile = tempfile.mkstemp(suffix=".sh")
os.fchmod(host_fd, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
os.write(host_fd, script)
os.close(host_fd)
return host_scriptfile
def run_on_tgt_os(script,
group_name,
job_name,
queue_name,
output,
err_output = None,
priority = None,
cwd=None,
deps=None,
mail_before = False,
mail_error = True,
mail_after = True,
email_address = None,
task_range=None,
memory=None):
'''Run the given script on the target operating system
script - the script to be run with shebang header line
(e.g. #!/bin/sh)
group_name - charge to this group
job_name - name of the job
queue_name - run on this queue
output - send stdout to this file
err_output - send stderr to this file
priority - the priority # for the job
cwd - change to this directory on remote machine to run script
deps - a list of job IDs to wait for before starting this one
mail_before - true to send email before job starts
mail_error - true to send email on error
mail_after - true to send email after job finishes
email_address - address of email recipient
task_range - for array jobs, a slice giving start / stop / step for
task numbering
'''
if deps is not None:
dep_cond = "-hold_jid %s" % (",".join(deps))
else:
dep_cond = ""
if cwd is not None:
cwd_switch = "-wd %s" % pipes.quote(cwd)
else:
cwd_switch = ""
if email_address is None or not any([mail_before, mail_error, mail_after]):
email_switches = ""
else:
email_events = "".join([x for x, y in (("b", mail_before),
("e", mail_error),
("a", mail_after))
if y])
email_switches = "-m %(email_events)s -M '%(email_address)s'" % locals()
if err_output is None:
err_output = output+".err"
if queue_name is None:
queue_switch = ""
else:
queue_switch = "-q %s" % pipes.quote(queue_name)
if task_range is None:
task_switch = ""
else:
step = task_range.step
if step is not None:
task_switch = "-t %d-%d:%d" % (
task_range.start, task_range.stop-1, task_range.step)
else:
task_switch = "-t %d-%d" % (task_range.start, task_range.stop-1)
if memory is not None:
memory_switch = "-l m_mem_free=%dg" % memory
else:
memory_switch = ""
if priority is not None:
priority_switch = "-p %d" %priority
else:
priority_switch = ""
optional_switches = " ".join(filter(len, [
email_switches, cwd_switch, queue_switch, task_switch, memory_switch,
priority_switch]))
tgt_script = make_temp_script(script)
host_script = make_temp_script("""#!/bin/sh
qsub -N %(job_name)s \\
-e '%(err_output)s' \\
-o '%(output)s' \\
-terse %(dep_cond)s %(optional_switches)s \\
%(tgt_script)s
""" %locals())
try:
p = subprocess.Popen(
host_script, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
stdout, stderr = p.communicate(script)
if p.returncode != 0:
raise RuntimeError("Failed to submit job: %s" % stderr)
if task_range is not None:
stdout = stdout.partition(".")[0]
return int(stdout.strip())
finally:
os.unlink(host_script)
os.unlink(tgt_script)
def requeue_job(job_id):
host_script = make_temp_script("""#!/bin/sh
if [ -e "$HOME/.batchprofiler.sh" ]; then
. "$HOME/.batchprofiler.sh"
fi
qmod -cj %d
""" % job_id)
try:
p = subprocess.Popen(
host_script, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
stdout, stderr = p.communicate()
return stdout, stderr
finally:
os.unlink(host_script)
def kill_job(job_id):
host_script = make_temp_script("""#!/bin/sh
if [ -e "$HOME/.batchprofiler.sh" ]; then
. "$HOME/.batchprofiler.sh"
fi
qdel %d
""" % job_id)
try:
p = subprocess.Popen(
host_script, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
stdout, stderr = p.communicate(host_script)
return stdout, stderr
finally:
os.unlink(host_script)
def kill_jobs(job_ids):
host_script = make_temp_script("""#!/bin/sh
if [ -e "$HOME/.batchprofiler.sh" ]; then
. "$HOME/.batchprofiler.sh"
fi
qdel %s
""" % " ".join(map(str, job_ids)))
try:
p = subprocess.Popen(
host_script, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
stdout, stderr = p.communicate(host_script)
return stdout, stderr
finally:
os.unlink(host_script)
def kill_tasks(job_id, task_ids):
host_script = make_temp_script("""#!/bin/sh
if [ -e "$HOME/.batchprofiler.sh" ]; then
. "$HOME/.batchprofiler.sh"
fi
qdel %d -t %s
""" % (job_id, ",".join(map(str, task_ids))))
try:
p = subprocess.Popen(
host_script, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
stdout, stderr = p.communicate(host_script)
return stdout, stderr
finally:
os.unlink(host_script)
def python_on_tgt_os(args, group_name, job_name, queue_name, output,
err_output=None,
cwd=None, deps = None,
priority=None,
mail_before = False,
mail_error = True,
mail_after = True,
email_address = None,
with_xvfb = False):
'''Run Python with the given arguments on a target machine'''
if cwd is None:
cd_command = ""
else:
cd_command = "cd %s" % cwd
if with_xvfb:
xvfb_run = "xvfb-run"
else:
xvfb_run = ""
argstr = '"%s"' % '" "'.join(args)
script = ("""#!/bin/sh
%%(cd_command)s
. %(PREFIX)s/bin/cpenv.sh
export PYTHONNOUSERSITE=1
if [ -e ./bin/activate ]; then
. ./bin/activate
fi
%%(xvfb_run)s python %%(argstr)s
""" % globals()) % locals()
return run_on_tgt_os(script, group_name, job_name, queue_name, output,
cwd = cwd,
deps=deps,
err_output=err_output,
mail_before=mail_before,
mail_error=mail_error,
mail_after=mail_after,
email_address=email_address)
def get_centrosome_version(path):
'''Return the dotted version of centrosome from a CP git repo dir
path - path to git repo
returns None if no centrosome required or its dotted version
'''
requirements_path = os.path.join(path, "requirements.txt")
if os.path.exists(requirements_path):
with open(requirements_path, "r") as fd:
for line in fd:
match = re.search(r"centrosome==(?P<version>\d+\.\d+.\d+)",
line)
if match is not None:
return match.groupdict()['version']
def venv_build_cellprofiler(
path,
queue_name = None,
group_name="imaging",
job_name = None,
email_address = None):
run_on_tgt_os(("""#/bin/sh
. %(PREFIX)s/bin/cpenv.sh
export PYTHONNOUSERSITE=1
export C_INCLUDE_PATH=$C_INCLUDE_PATH:$(PREFIX)s/include
virtualenv --system-site-packages "%%(path)s"
. "%%(path)s/bin/activate"
pip install Pillow
pip install pytest
pip install clint
pip install requests
pip install --no-deps prokaryote
pip install --no-deps centrosome
cd "%%(path)s"
pip install .
""" % globals()) % locals(),
group_name = group_name,
job_name = job_name,
queue_name=queue_name,
output = os.path.join(path, job_name+".log"),
mail_after = False,
email_address = email_address)
def build_cellprofiler(
version = None,
git_hash=None,
queue_name=None,
group_name="imaging",
email_address = None):
'''Build/rebuild a version of CellProfiler
version - numeric version # based on commit date
git_hash - git hash of version
group_name - fairshare group for remote jobs
email_address - send email notifications here
returns a sequence of job numbers.
'''
path = get_cellprofiler_location(version = version, git_hash = git_hash)
with open(os.path.join(path, "build.log"), "w") as fd:
fd.write("--- begin %s : %s ---\n" % (version, git_hash))
if os.path.isdir(os.path.join(path, ".git")):
fd.write("git clean\n")
subprocess.check_call(["git", "clean", "-d", "-f", "-x", "-q"], cwd=path)
else:
fd.write("git clone\n")
if not os.path.isdir(path):
os.makedirs(path)
subprocess.check_call([
"git", "clone", "-q",
"https://github.com/CellProfiler/CellProfiler", path])
subprocess.check_call(["git", "checkout", "-q", git_hash], cwd=path)
mvn_job = "CellProfiler-mvn-%s" % git_hash
build_job = "CellProfiler-build-%s" % git_hash
touch_job = "CellProfiler-touch-%s" % git_hash
centrosome_version = get_centrosome_version(path)
if centrosome_version is not None:
fd.write("Centrosome version = %s\n" % centrosome_version)
run_on_tgt_os(("""#/bin/sh
. %(PREFIX)s/bin/cpenv.sh
export PYTHONNOUSERSITE=1
virtualenv --system-site-packages "%%(path)s"
. "%%(path)s/bin/activate"
pip install pytest
pip install --no-deps https://github.com/CellProfiler/centrosome/archive/%%(centrosome_version)s.tar.gz
cd "%%(path)s"
python external_dependencies.py -o
python CellProfiler.py --build-and-exit
""" % globals()) % locals(),
group_name = group_name,
job_name = build_job,
queue_name=queue_name,
output = os.path.join(path, build_job+".log"),
mail_after = False,
email_address = email_address)
elif version > "20160101000000":
fd.write("venv build\n")
venv_build_cellprofiler(path, queue_name, group_name, build_job, email_address)
elif version > "20120607000000":
fd.write("2.1.1 style build\n")
python_on_tgt_os(
["external_dependencies.py", "-o"],
group_name,
mvn_job,
queue_name,
os.path.join(path, mvn_job+".log"),
cwd = path,
mail_after = False,
email_address=email_address)
python_on_tgt_os(
["CellProfiler.py", "--build-and-exit", "--do-not-fetch"],
group_name,
build_job,
queue_name,
os.path.join(path, build_job+".log"),
cwd = path,
deps=[mvn_job],
mail_after = False,
email_address=email_address,
with_xvfb=True)
else:
python_on_tgt_os(
["CellProfiler.py", "--build-and-exit"],
group_name,
build_job,
queue_name,
os.path.join(path, build_job+".log"),
cwd = path,
mail_after = False,
email_address=email_address,
with_xvfb=True)
touchfile = os.path.join(path, BUILD_TOUCHFILE)
python_on_tgt_os(
args = ["-c",
("import sys;"
"sys.path.append('%s');"
"import cellprofiler.pipeline;"
"open('%s', 'w').close();"
"from bputilities import shutdown_cellprofiler;"
"shutdown_cellprofiler()") %
( os.path.dirname(__file__), touchfile)],
group_name = group_name,
job_name = touch_job,
queue_name = queue_name,
output = "/dev/null",
err_output = touch_job+".err",
deps = [build_job],
cwd = path,
email_address = email_address,
mail_after = True,
with_xvfb=True)
def is_built(version, git_hash):
path = get_cellprofiler_location(version=version, git_hash=git_hash)
return os.path.isfile(os.path.join(path, BUILD_TOUCHFILE))
def send_mail(recipient, subject, content_type, body):
'''Send mail to a single recipient
recipient - email address of recipient
subject - subject field of email
content_type - mime type of the message body
body - the payload of the mail message
'''
pipe= subprocess.Popen([SENDMAIL, "-t"], stdin=subprocess.PIPE)
doc = """To: %(recipient)s
Subject: %(subject)s
Content-Type: %(content_type)s; charset=UTF-8
%(body)s
""" % locals()
pipe.communicate(doc)
def send_html_mail(recipient, subject, html):
'''Send mail that has HTML in the body
recipient - email address of recipient
subject - subject field of email
content_type - mime type of the message body
body - the payload of the mail message
'''
send_mail(recipient=recipient,
subject = subject,
content_type="text/html",
body=html)
class CellProfilerContext(object):
'''Wrap CellProfiler operations in a handy context
Redirect stderr and stdout to prevent log printing
On exit, shut things down
'''
def __enter__(self):
devnull = open("/dev/null", "w")
self.stdout = sys.stdout
self.stderr = sys.stderr
sys.stdout = devnull
sys.stderr = devnull
return self
def __exit__(self, exc_1, exc_2, exc_3):
shutdown_cellprofiler()
sys.stdout = self.stdout
sys.stderr = self.stderr
def shutdown_cellprofiler():
'''Oh sadness and so many threads that won't die...
'''
try:
import javabridge
javabridge.kill_vm()
except:
pass
try:
from ilastik.core.jobMachine import GLOBAL_WM
GLOBAL_WM.stopWorkers()
except:
pass
if __name__ == "__main__":
sys.path.append(os.path.dirname(__file__))
output = []
with CellProfilerContext():
if sys.argv[1] == "-b":
version, githash = get_version_and_githash(sys.argv[2])
build_cellprofiler(version, githash)
else:
for arg in sys.argv[1:]:
output.append(get_batch_data_version_and_githash(arg))
for line in output:
print line