def get_sweep_results(sim_meta_config_files, calib_file_path, tags_data_file_path): # Login to COMPs comps_login() # find total number of simulations across given experiment files num_sims = 0 for sim_meta_config_file in sim_meta_config_files: with open(sim_meta_config_file) as metadata_file: metadata = json.loads(metadata_file.read()) num_sims = num_sims + len(metadata['sims']) # Download simulations locally # sample sim meta config file (like "C:\\Users\\Mnikolov\\Zambia-raw\\dtk-scripts\\1node\\simulations\\Sinamalina_Sinazongwe_Calibration_e9979059-33f8-e411-93f9-f0921c16b9e7.json") #print 'Downloading simulations from experiment ' + str(sim_meta_config_files) + '...' # simulations tag data structure: accumulates sims meta information from sims tags tag_data = { 'ITN trajectory': [],\ 'Drug coverage per round': [],\ 'Temporary habitat scale': [],\ 'Constant habitat scale': []\ } # iterate through experiments calib_output = {} # count processed sims to updated progress count = 0 for sim_meta_config_file in sim_meta_config_files: # construct experiment directory structure with open(sim_meta_config_file) as metadata_file: metadata = json.loads(metadata_file.read()) output_path = metadata['sim_root'] exp_id = metadata['exp_id'] exp_name = metadata['exp_name'] sim_dir_map = CompsDTKOutputParser.createSimDirectoryMap(exp_id) # get all successfully completed sims in experiment for sim_id, sim in metadata['sims'].items(): # get path to the sim timeseries channels data timeseries_path = os.path.join(sim_dir_map[sim_id],'output', 'InsetChart.json') #get sim timeseries channels data; json2dict returns None if timeseries_path points to non-existing file, which is the case if the sim has not successfully finished sim_output = json2dict(timeseries_path) # only download successfully completed simulations if sim_output == None: continue # delete all but the specified channels for channel in sim_output['Channels'].keys(): if not channel in channels: del(sim_output['Channels'][channel]) # process specified reports report_channels_data = {} if not reports_channels == None: report_channels_data = process_reports(reports_channels, sim_dir_map, sim_id) # record sim meta information including sim tags tags_path = os.path.join(sim_dir_map[sim_id], 'tags.json') f = open(tags_path, 'r') tags = f.read() sim_meta = ast.literal_eval(tags) append_tag_data(sim_meta, tag_data) # construct sim group key and sim key x_temp_h = sim_meta_2_temp_h(sim_meta) const_h = sim_meta_2_const_h(sim_meta) itn_level = sim_meta_2_itn_level(sim_meta) drug_coverage_level = sim_meta_2_drug_cov(sim_meta) sim_key = get_sim_key(x_temp_h, const_h, itn_level, drug_coverage_level) sim_group_key = get_sim_group_key(itn_level, drug_coverage_level) # store sim channels data if sim_group_key not in calib_output: calib_output[sim_group_key] = {} calib_output[sim_group_key][sim_key] = { # can add/remove data entries depending on needs 'prevalence': sim_output['Channels']['New Diagnostic Prevalence']['Data'], 'reinfections': report_channels_data['reinfections'], 'meta':sim_meta, 'sim_id':sim_id } ''' count = count + 1 percent_complete = 100*count/(num_sims+0.0) sys.stdout.write('\r') sys.stdout.write('%2f %%' % percent_complete) #sys.stdout.write('%d' % count) sys.stdout.flush() ''' print "" print "Writing files..." with open(calib_file_path, 'w') as calib_f: json.dump(calib_output, calib_f) print str(len(calib_output)) + ' simulation results saved to ' + calib_file_path with open(tags_data_file_path, 'w') as tags_f: json.dump(tag_data, tags_f) print 'Meta data tags saved to ' + tags_data_file_path print "" return calib_f
def fit_entry(self): model_meta = self.model.get_meta() sim_key = model_meta['sim_key'] temp_h = sim_meta_2_temp_h(model_meta['sim_meta']) const_h = sim_meta_2_const_h(model_meta['sim_meta']) itn_level = sim_meta_2_itn_level(model_meta['sim_meta']) drug_cov = sim_meta_2_drug_cov(model_meta['sim_meta']) if self.all_fits: fit_terms = self.all_fits[self.cluster_id][sim_key]['fit_terms'] else: fit_terms = {} if not 'cc_penalty' in fit_terms: fit_terms['cc_penalty'] = {} if 'ls_norm' in cc_penalty_model: fit_terms['cc_penalty']['ls_norm'] = self.clinical_cases_penalty_term elif 'ls_norm_not_folded' in cc_penalty_model: fit_terms['cc_penalty']['ls_norm_not_folded'] = self.clinical_cases_penalty_term if 'ls_no_norm' in cc_penalty_model: fit_terms['cc_penalty']['ls_no_norm'] = self.clinical_cases_penalty_term if 'corr_folded' in cc_penalty_model: if not 'corr_folded' in fit_terms['cc_penalty']: fit_terms['cc_penalty']['corr_folded'] = {} fit_terms['cc_penalty']['corr_folded']['penalty'] = self.clinical_cases_penalty_term fit_terms['cc_penalty']['corr_folded']['rho'] = self.get_rho() fit_terms['cc_penalty']['corr_folded']['p_val'] = self.get_p_val() if 'corr_not_folded' in cc_penalty_model: if not 'corr_not_folded' in fit_terms['cc_penalty']: fit_terms['cc_penalty']['corr_not_folded'] = {} fit_terms['cc_penalty']['corr_not_folded']['penalty'] = self.clinical_cases_penalty_term fit_terms['cc_penalty']['corr_not_folded']['rho'] = self.get_rho() fit_terms['cc_penalty']['corr_not_folded']['p_val'] = self.get_p_val() fit_terms['reinf_penalty'] = self.reinfection_penalty_term if load_prevalence_mse: fit_terms['mse'] = self.get_cached_mse_term() else: fit_terms['mse'] = self.get_mse() fit_entry = {} fit_entry[model_meta['sim_key']] = { 'group_key': model_meta['group_key'], 'sim_id':model_meta['sim_id'], 'fit_val': self.get_fit_val(), 'rho_val' : self.get_rho(), # from most recent run 'p_val' : self.get_p_val(), # from most recent run 'x_temp_h': temp_h, 'const_h': const_h, 'fit_terms':fit_terms, 'itn_level': itn_level, 'drug_cov': drug_cov } return fit_entry