def fit_emp_prior(id, param_type): """ Fit empirical prior of specified type for specified model Parameters ---------- id : int The model id number for the job to fit param_type : str, one of incidence, prevalence, remission, excess-mortality The disease parameter to generate empirical priors for Example ------- >>> import fit_emp_prior >>> fit_emp_prior.fit_emp_prior(2552, 'incidence') """ #dismod3.log_job_status(id, 'empirical_priors', param_type, 'Running') # load disease model dm = dismod3.load_disease_model(id) #dm.data = [] # remove all data to speed up computation, for test import dismod3.neg_binom_model as model dir = dismod3.settings.JOB_WORKING_DIR % id model.fit_emp_prior(dm, param_type, dbname='%s/empirical_priors/pickle/dm-%d-emp_prior-%s.pickle' % (dir, id, param_type)) # generate empirical prior plots from pylab import subplot for sex in dismod3.settings.gbd_sexes: for year in dismod3.settings.gbd_years: keys = dismod3.utils.gbd_keys(region_list=['all'], year_list=[year], sex_list=[sex], type_list=[param_type]) dismod3.tile_plot_disease_model(dm, keys, defaults={}) dm.savefig('dm-%d-emp_prior-%s-%s-%s.png' % (id, param_type, sex, year)) # TODO: put this in a separate script, which runs after all empirical priors are computed for effect in ['alpha', 'beta', 'gamma', 'delta']: dismod3.plotting.plot_empirical_prior_effects([dm], effect) dm.savefig('dm-%d-emp-prior-%s-%s.png' % (id, param_type, effect)) # summarize fit quality graphically, as well as parameter posteriors k0 = keys[0] dm.vars = {k0: dm.vars} # hack to make posterior predictions plot dismod3.plotting.plot_posterior_predicted_checks(dm, k0) dm.savefig('dm-%d-emp-prior-check-%s.png' % (dm.id, param_type)) dm.vars = dm.vars[k0] # undo hack to make posterior predictions plot # save results (do this last, because it removes things from the disease model that plotting function, etc, might need dm.save('dm-%d-prior-%s.json' % (id, param_type)) dismod3.try_posting_disease_model(dm, ntries=5) #dismod3.log_job_status(id, 'empirical_priors', param_type, 'Completed') return dm
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)
for age in age_mesh: x.append(age) y.append(measure_out.model[index_dict[(dm4_type, year, age)]]) key = dismod3.gbd_key_for(dm3_type, r, year, sex) est = dismod3.utils.interpolate(x, y, dm.get_estimate_age_mesh()) dm.set_truth(key, est) dismod3.tile_plot_disease_model(dm, [key], defaults={}) try: pl.savefig(dismod3.settings.JOB_WORKING_DIR % id + '/dm-%d-posterior-%s-%s-%s.png' % (id, dm3_type, sex, year)) # TODO: refactor naming into its own function except IOError, e: print 'Warning: could not create png. Maybe it exists already?\n%s' % e # save results (do this last, because it removes things from the disease model that plotting function, etc, might need dismod3.try_posting_disease_model(dm, ntries=5) print print '********************' print 'computation complete' print '********************' def main(): import optparse usage = 'usage: %prog [options] disease_model_id' parser = optparse.OptionParser(usage) (options, args) = parser.parse_args() if len(args) != 1:
for effect in ['alpha', 'beta', 'gamma', 'delta']: try: dismod3.plotting.plot_empirical_prior_effects([dm], effect) dm.savefig('dm-%d-emp-prior-%s.png' % (id, effect)) except Exception: print 'failed to plot %s' % effect # save table output try: dismod3.table.make_tables(dm) except Exception, e: print 'Failed to make table' print e # send to website dismod3.try_posting_disease_model(dm, ntries=5) # record that job is done o = '%s/empirical_priors/stdout/%d_running.txt' % (dir, id) f = open(o, 'a') import time f.write('\n**** JOB DONE AT %s' % time.strftime('%c')) f.close() def merge_data_csvs(id): df = pandas.DataFrame() dir = dismod3.settings.JOB_WORKING_DIR % id #print dir