def get_params(_profile='BaselineHTHI'): _model = 'agent-based-model' camp = 'Moria' overrides = '{"numberOfIterations": 1, "nProcesses": 1}' facade = Facade.simple() params = create_params(facade.ps, _model, _profile, camp, overrides) return params
def run_model(_model: str, _profile: str, camp: str, load_from_cache: bool, save_to_cache: bool, is_save_plots: bool, is_show_plots: bool, is_save_report: bool, overrides) -> ModelResult: logging.info('Running %s model with %s profile', _model, _profile) _mdl: Model = get_models()[_model](facade.ps) params = create_params(facade.ps, _model, _profile, camp, overrides) res_id = _mdl.result_id(params) if load_from_cache and facade.rs.exists(_mdl.id(), res_id): logging.info("Loading from model result cache") mr: ModelResult = facade.rs.load(_mdl.id(), res_id) else: logging.info("Running model for camp %s", camp) mr: ModelResult = _mdl.run(params) if save_to_cache: logging.info("Saving model result to cache") facade.rs.store(_mdl.id(), res_id, mr) if is_save_plots or is_show_plots: # if _mdl.id() == 'agent-based-model': save_plots(mr, res_id, is_save_plots, is_show_plots) # save_plots_abm(mr, res_id, is_save_plots, is_show_plots) # else: # save_plots(mr, res_id, is_save_plots, is_show_plots) if is_save_report: save_report(mr, res_id) return mr
def find_profile(m, c, rid): _mdl = get_models()[m](facade.ps) for p in facade.ps.get_profiles(m): params = create_params(facade.ps, m, p, c) res_id = _mdl.result_id(params) if res_id == rid: return p return None
def test_cm_category(self): params = create_params(self.facade.ps, 'compartmental-model', self.facade.ps.get_profiles('compartmental-model')[0], 'Moria', None) self.assertCountEqual(params.change_in_categories, ['C' + x for x in params.calculated_categories]) self.assertEqual(len(params.categories), 2*len(params.calculated_categories) + 1) self.assertEqual(params.control_dict['better_hygiene']['value'], params.better_hygiene)
def _sync_run_model(facade, _model: str, _profile: str, camp: str) -> ModelResult: logger.info('Running %s model with %s profile', _model, _profile) _mdl: Model = get_models()[_model](facade.ps) params = create_params(facade.ps, _model, _profile, camp) res_id = _mdl.result_id(params) logger.info("Running model for camp %s", camp) mr = _mdl.run(params) logger.info("Saving model result to cache") facade.rs.store(_mdl.id(), res_id, mr) return mr
def _sync_run_model(self, _model: str, _profile: str, camp: str) -> ModelResult: logging.info('Running %s model with %s profile', _model, _profile) _mdl: Model = get_models()[_model](self.facade.ps) params = create_params(self.facade.ps, _model, _profile, camp) res_id = _mdl.result_id(params) if self.facade.rs.exists(_mdl.id(), res_id): logging.info("Loading from model result cache") return self.facade.rs.load(_mdl.id(), res_id) else: logging.info("Running model for camp %s", camp) mr = _mdl.run(params) logging.info("Saving model result to cache") self.facade.rs.store(_mdl.id(), res_id, mr) return mr
def test_cm_custom_profile(self): custom_profile_df = pd.read_csv(os.path.join(os.path.dirname(__file__), 'resources/profile.csv')) params = create_params(self.facade.ps, 'compartmental-model', custom_profile_df, 'Moria', None) self.assertEqual(params.control_dict['ICU_capacity']['value'], int(custom_profile_df[custom_profile_df['Parameter'] == 'ICU_capacity']['Value'])/params.population) self.assertEqual(params.control_dict['better_hygiene']['value'], float(custom_profile_df[custom_profile_df['Parameter'] == 'better_hygiene']['Value'])) self.assertIsNotNone(params.control_dict['remove_symptomatic']['timing']) self.assertIsNotNone(params.control_dict['remove_high_risk']['n_categories_removed']) self.assertEqual(params.control_dict['numberOfIterations'], 2) self.assertEqual(params.control_dict['t_sim'], 200)
def get_result(self, _model: str, _profile: str, camp: str) -> ModelResult: _mdl: Model = get_models()[_model](self.facade.ps) params = create_params(self.facade.ps, _model, _profile, camp) res_id = _mdl.result_id(params) return self.facade.rs.load(_mdl.id(), res_id)
def results_exist(self, _model: str, _profile: str, camp: str) -> bool: _mdl: Model = get_models()[_model](self.facade.ps) params = create_params(self.facade.ps, _model, _profile, camp) res_id = _mdl.result_id(params) return self.facade.rs.exists(_mdl.id(), res_id)