def test_update_params__no_request__expect_no_change(): old_params = { "foo": 1, "bar": { "baz": 2, "boop": [7, 8, 9], "bing": { "bonk": 3, } }, "boop": [4, 5, 6], } update_request = {} expected_new_params = { "foo": 1, "bar": { "baz": 2, "boop": [7, 8, 9], "bing": { "bonk": 3, } }, "boop": [4, 5, 6], } actual_new_params = update_params(old_params, update_request) assert actual_new_params == expected_new_params
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 test_update_params__deep_array_request__expect_updated(): old_params = { "foo": 1, "bar": { "baz": 2, "boop": [7, 8, 9], "bing": { "bonk": 3, } }, "boop": [4, 5, 6], } update_request = {"bar.boop(0)": 1} expected_new_params = { "foo": 1, "bar": { "baz": 2, "boop": [1, 8, 9], "bing": { "bonk": 3, } }, "boop": [4, 5, 6], } actual_new_params = update_params(old_params, update_request) assert actual_new_params == expected_new_params
def test_update_params__multiple_shallow_requests__expect_updated(): old_params = { "foo": 1, "bank": 10, "bar": { "baz": 2, "boop": [7, 8, 9], "bing": { "bonk": 3, } }, "boop": [4, 5, 6], } update_request = {"foo": 2, "bank": 3} expected_new_params = { "foo": 2, "bank": 3, "bar": { "baz": 2, "boop": [7, 8, 9], "bing": { "bonk": 3, } }, "boop": [4, 5, 6], } actual_new_params = update_params(old_params, update_request) assert actual_new_params == expected_new_params
def run_root_model(country=Region.UNITED_KINGDOM, calibrated_params={}): """ This function runs a model to simulate the past epidemic (up until 1/7/2020) using a given calibrated parameter set. Returns an integrated model for the past epidemic. """ running_model = RegionApp(country) build_model = running_model.build_model params = copy.deepcopy(running_model.params) # update params with optimisation default config params["default"].update(opti_params["default"]) # update params with calibrated parameters params["default"] = update_params(params['default'], calibrated_params) # prepare importation rates for herd immunity testing params["default"]["data"] = { 'times_imported_cases': [0], 'n_imported_cases': [0] } params["default"]["end_time"] = PHASE_2_START_TIME params["scenario_start_time"] = PHASE_2_START_TIME - 1 scenario_0 = Scenario(build_model, idx=0, params=params) scenario_0.run() return scenario_0.model
def test_update_params__dict_in_array_request__expect_updated(): old_params = { "foo": [{ "a": 1 }, { "a": 2 }, { "a": 3 }], } update_request = {"foo(1).a": 4} expected_new_params = { "foo": [{ "a": 1 }, { "a": 4 }, { "a": 3 }], } actual_new_params = update_params(old_params, update_request) assert actual_new_params == expected_new_params
def run_all_phases(decision_variables, country=Region.UNITED_KINGDOM, config=0, calibrated_params={}, mode="by_age"): running_model = RegionApp(country) build_model = running_model.build_model if mode == "by_location": new_decision_variables = { "other_locations": decision_variables[0], "school": decision_variables[1], "work": decision_variables[2] } decision_variables = new_decision_variables params = copy.deepcopy(running_model.params) # update params with optimisation default config params["default"].update(opti_params["default"]) # update params with calibrated parameters params["default"] = update_params(params['default'], calibrated_params) # prepare importation rates for herd immunity testing params["default"]["data"] = { 'times_imported_cases': [0], 'n_imported_cases': [0] } params["scenarios"][1] = build_params_for_phases_2_and_3( decision_variables, config, mode) run_models = build_model_runner(model_name="covid_19", param_set_name=country, build_model=build_model, params=params) run_models()
def objective_function(decision_variables, root_model, mode="by_age", country=Region.UNITED_KINGDOM, config=0, calibrated_params={}): """ :param decision_variables: dictionary containing - mixing multipliers by age as a list if mode == "by_age" OR - location multipliers as a list if mode == "by_location" :param root_model: integrated model supposed to model the past epidemic :param mode: either "by_age" or "by_location" :param country: the country name :param config: the id of the configuration being considered :param calibrated_params: a dictionary containing a set of calibrated parameters """ running_model = RegionApp(country) build_model = running_model.build_model params = copy.deepcopy(running_model.params) # reformat decision vars if locations if mode == "by_location": new_decision_variables = { "other_locations": decision_variables[0], "school": decision_variables[1], "work": decision_variables[2] } decision_variables = new_decision_variables # Define scenario-1-specific params sc_1_params_update = build_params_for_phases_2_and_3( decision_variables, config, mode) # Rebuild the default parameters params["default"].update(opti_params["default"]) params["default"] = update_params(params['default'], calibrated_params) params['scenario_start_time'] = PHASE_2_START_TIME - 1 # Create scenario 1 sc_1_params = update_params(params['default'], sc_1_params_update) params["scenarios"][1] = sc_1_params scenario_1 = Scenario(build_model, idx=1, params=params) # Run scenario 1 scenario_1.run(base_model=root_model) models = [root_model, scenario_1.model] #____________________________ Perform diagnostics ______________________ # How many deaths and years of life lost during Phase 2 and 3 start_phase2_index = models[1].derived_outputs["times"].index( PHASE_2_START_TIME) end_phase2_index = models[1].derived_outputs["times"].index( phase_2_end[config]) total_nb_deaths = sum( models[1].derived_outputs["infection_deathsXall"][start_phase2_index:]) years_of_life_lost = sum( models[1].derived_outputs["years_of_life_lost"][start_phase2_index:]) # What proportion immune at end of Phase 2 recovered_indices = [ i for i in range(len(models[1].compartment_names)) if "recovered" in models[1].compartment_names[i] ] nb_reco = sum( [models[1].outputs[end_phase2_index, i] for i in recovered_indices]) total_pop = sum([ models[1].outputs[end_phase2_index, i] for i in range(len(models[1].compartment_names)) ]) prop_immune = nb_reco / total_pop # Has herd immunity been reached? herd_immunity = has_immunity_been_reached(models[1], end_phase2_index) return herd_immunity, total_nb_deaths, years_of_life_lost, prop_immune, models
def update_func(ps: dict): return update_params(ps, param_updates)