forked from aflaxman/gbd
/
upload_fits.py
92 lines (70 loc) · 2.21 KB
/
upload_fits.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
#!/usr/bin/python2.5
""" Generate empirical prior of specified parameter type
Expects the disase model json to be saved already.
"""
import simplejson as json
import dismod3
import zipfile, os
from shutil import rmtree
def upload_fits(id):
""" Send results of cluster fits to dismod server
Parameters
----------
id : int
The model id number
Example
-------
>>> import fit_emp_prior
>>> fit_emp_prior.fit_emp_prior(2552, 'incidence')
>>> import upload_fits
>>> upload_fits.upload_fits(2552)
"""
# load disease model
dm = dismod3.load_disease_model(id) # this merges together results from all fits
dismod3.try_posting_disease_model(dm, ntries=5)
#zip_country_level_posterior_files(id)
def zip_country_level_posterior_files(id):
""" Zip country level posterior files in the directory of the
job_working_directory/posterior/country_level_posterior_dm-'id',
and then remove the directory containing the files
Parameters
----------
id : int
The model id number
"""
# job working directory
job_wd = dismod3.settings.JOB_WORKING_DIR % id
# directory containing the csv files
directory = 'country_level_posterior_dm-' + str(id)
try:
# move to directory
orig_dir = os.getcwd()
os.chdir(job_wd + '/posterior/')
# open an archive for writing
a = zipfile.ZipFile(directory + '.zip', 'w', zipfile.ZIP_DEFLATED)
# put files into the archive
for f in os.listdir(directory):
print "archiving file %s" % f
a.write(directory + '/' + f)
# close the archive
a.close()
# remove directory
rmtree(directory)
# move back directory
os.chdir(orig_dir)
except Exception,e:
print e
def main():
import optparse
usage = 'usage: %prog [options] disease_model_id'
parser = optparse.OptionParser(usage)
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error('incorrect number of arguments')
try:
id = int(args[0])
except ValueError:
parser.error('disease_model_id must be an integer')
upload_fits(id)
if __name__ == '__main__':
main()