def run_model_with_params(self, proposed_params: dict): """ Run the model with a set of params. """ logger.info(f"Running iteration {self.iter_num}...") # Update default parameters to use calibration params. param_updates = {"end_time": self.end_time} for i, param_name in enumerate(self.param_list): param_updates[param_name] = proposed_params[i] params = copy.deepcopy(self.model_parameters) params["default"] = update_params(params["default"], param_updates) scenario = Scenario(self.model_builder, 0, params) scenario.run() self.latest_scenario = scenario _req_outs = [ o for o in self.targeted_outputs if "prevX" in o["output_key"] ] requested_outputs = [o["output_key"] for o in _req_outs] requested_times = {o["output_key"]: o["years"] for o in _req_outs} pp = post_proc.PostProcessing( scenario.model, requested_outputs=requested_outputs, requested_times=requested_times, multipliers=self.multipliers, ) return scenario, pp
def visualise_simulation(_models): pps = [] for scenario_index in range(len(_models)): pps.append( post_proc.PostProcessing( _models[scenario_index], requested_outputs=[ "prevXinfectiousXamong", "prevXrecoveredXamong" ], scenario_number=scenario_index, requested_times={}, ))
def create_mcmc_outputs( mcmc_models, req_outputs, req_times={}, req_multipliers={}, ymax={}, out_dir="outputs_tes", targets_to_plot={}, translation_dictionary={}, scenario_list=[], plot_start_time=1990, ): """similar to create_multi_scenario_outputs but using MCMC outputs""" if not os.path.exists(out_dir): os.mkdir(out_dir) for output in req_outputs: if (output[0:15] == "prevXinfectious" and output != "prevXinfectiousXstrain_mdrXamongXinfectious"): req_multipliers[output] = 1.0e5 elif output[0:11] == "prevXlatent": req_multipliers[output] = 1.0e2 pps = [] for scenario_index in range(len(mcmc_models)): pps.append( post_proc.PostProcessing( mcmc_models[scenario_index]["model"], requested_outputs=req_outputs, scenario_number=scenario_list[scenario_index], requested_times=req_times, multipliers=req_multipliers, ymax=ymax, )) mcmc_weights = [mcmc_models[i]["weight"] for i in range(len(mcmc_models))] outputs = Outputs( mcmc_models, pps, output_options={}, targets_to_plot=targets_to_plot, out_dir=out_dir, translation_dict=translation_dictionary, mcmc_weights=mcmc_weights, plot_start_time=plot_start_time, ) outputs.plot_requested_outputs()
def get_post_processing_results( models, req_outputs, req_multipliers, outputs_to_plot_by_stratum, scenario_list, req_times, ymax, ): pps = [] for scenario_index in range(len(models)): # automatically add some basic outputs if hasattr(models[scenario_index], "all_stratifications"): for group in models[scenario_index].all_stratifications.keys(): req_outputs.append("distribution_of_strataX" + group) for output in outputs_to_plot_by_stratum: for stratum in models[scenario_index].all_stratifications[ group]: req_outputs.append(output + "XamongX" + group + "_" + stratum) if "strain" in models[scenario_index].all_stratifications.keys(): req_outputs.append( "prevXinfectiousXstrain_mdrXamongXinfectious") for output in req_outputs: if (output[0:15] == "prevXinfectious" and output != "prevXinfectiousXstrain_mdrXamongXinfectious"): req_multipliers[output] = 1.0e5 elif output[0:11] == "prevXlatent": req_multipliers[output] = 1.0e2 pps.append( post_proc.PostProcessing( models[scenario_index], requested_outputs=req_outputs, scenario_number=scenario_list[scenario_index], requested_times=req_times, multipliers=req_multipliers, ymax=ymax, )) return pps
def update_post_processing(self): """ updates self.post_processing attribute based on the newly run model :return: """ requested_outputs = [ self.targeted_outputs[i]["output_key"] for i in range(len(self.targeted_outputs)) if "prevX" in self.targeted_outputs[i]["output_key"] ] requested_times = {} for _output in self.targeted_outputs: if "prevX" in _output["output_key"]: requested_times[_output["output_key"]] = _output["years"] self.post_processing = post_proc.PostProcessing( self.scenarios[0].model, requested_outputs=requested_outputs, requested_times=requested_times, multipliers=self.multipliers, )