def test_get_default_params(self): params = get_default_params() self.assertIsNot(params, None) params_key_set = set(params.keys()) self.assertIn('parameters', params_key_set) self.assertIn('rules', params_key_set) series = run_sir_h(params['parameters'], params['rules']) self.assertIsNot(series, None)
def test_model_execution(self): params = get_default_params() series1 = run_sir_h(params['parameters'], params['rules']) tmp_file = NamedTemporaryFile() export_json(tmp_file.name, params['parameters'], params['rules'], params['other']) read_params = import_json(tmp_file.name) tmp_file.close() series2 = run_sir_h(read_params[0], read_params[1]) self.assertEqual(series1, series2)
def test_import_export_json(self): params = get_default_params() tmp_file = NamedTemporaryFile(delete=False) export_json(tmp_file.name, params['parameters'], params['rules'], params['other']) read_params = import_json(tmp_file.name) tmp_file.close() remove(tmp_file.name) self.assertEqual(read_params[0], params['parameters']) # self.assertEqual(read_params[1], params['rules']) self.assertEqual(set([str(x) for x in read_params[1]]), set([str(x) for x in params['rules']])) self.assertEqual(read_params[2], params['other'])
help="number of data points to consider for training") parser.add_argument( '--path', metavar='pathname', type=str, nargs=1, help= 'to be used with -s, --save parameter. Saves output files to provided path' ) ''' @TODO take into account --path arguments. Current behaviour is to take default parameters and to optimise for 'SI' ''' args = parser.parse_args() default_model_params = get_default_params() day0 = default_model_params['data']['day0'] read_target = None if args.input: """@TODO consider target dates to systematically start on 01/01/2020""" read_target = pd.read_csv(args.input[0], sep=';').to_numpy() target = read_target else: default_data = default_model_params['data']['data_chu_rea'] target = np.array([[x - day0, y] for (x, y) in default_data.items() if y]).reshape([-1, 2]) if args.n: target = target[:args.n[0], ]
else: save_output = False basename = None if save_output: if not os.path.exists(outputdir): os.mkdir(outputdir) timestamp = datetime.datetime.now().strftime("%y%m%d_%H%M%S_") if args.save: basename = outputdir + timestamp + args.save else: basename = outputdir + timestamp + 'commando_covid_predict' default_data = get_default_params()['data']['data_chu_rea'] day0 = get_default_params()['data']['day0'] default_target = np.array([[x - day0, y] for (x, y) in default_data.items() if y]).reshape([-1, 2]) read_target = None if args.input: read_target = pd.read_csv(args.input[0], sep=';').to_numpy() target = read_target else: target = default_target if args.n: train_sample_size = args.n[0] else: train_sample_size = 50
def model_diff(parameters: Dict[str, any], series: List[str] = None, **kwargs: Dict[str, any]) -> Dict[str, any]: parameters = dict(parameters) other_arguments = dict(kwargs) # del other_arguments['parameters'] parameters.update(other_arguments) if 't_confinement' not in parameters.keys(): t_confinement = get_default_params()['other']['confinement'] else: t_confinement = parameters['t_confinement'] if 't_end' not in parameters.keys(): t_end = get_default_params()['other']['deconfinement'] else: t_end = parameters['t_end'] if 'beta_post' not in parameters.keys(): parameters['beta_post'] = get_default_params( )['other']['r0_confinement'] / parameters['dm_r'] if 'beta_end' not in parameters.keys(): parameters['beta_end'] = 1.2 / parameters['dm_r'] r0_start = parameters['beta'] * parameters[ 'dm_r'] if 'R0_start' not in parameters.keys( ) else parameters['R0_start'] r0_confinement = parameters['beta_post'] * parameters['dm_r'] if 'R0_confinement' not in parameters.keys() else \ parameters['R0_confinement'] r0_end = parameters['beta_end'] * \ parameters['dm_r'] if 'R0_end' not in parameters.keys( ) else parameters['R0_end'] gamma = 1.0 / parameters['dm_r'] # dmg dm_IR def R0(t, k: float, r0_start: float, t_confinement: int, r0_confinement: float, t_end: int, r0_end: float): # return 3.31 # return R0_start if t < t_confinement else R0_confinement # if t<(t_confinement + t_end)/2: return (r0_start - r0_confinement) / ( 1 + np.exp(-k * (-t + t_confinement))) + r0_confinement # else: # return (R0_confinement-R0_end) / (1 + np.exp(-k*(-t + t_end))) + R0_end def beta(t): k = 1.0 return R0(t, k, r0_start, t_confinement, r0_confinement, t_end, r0_end) * gamma n_population = parameters['population'] y0 = n_population - \ parameters['patient0'], parameters['patient0'], 0.0, 0.0, 0.0, 0.0, 0.0 t = np.linspace(0, parameters['lim_time'] - 1, parameters['lim_time']) ret = odeint(deriv, y0, t, args=(beta, parameters)) SE, INCUB, I, SM, SI, R, DC = ret.T # R0_over_time = [beta(i) / gamma for i in range(len(t))] return { 'time': t, 'series': { 'SE': SE, 'INCUB': INCUB, 'I': I, 'SM': SM, 'SI': SI, 'R': R, 'DC': DC }, # 'R0': R0_over_time }
def model_disc(model_params: Dict[str, any], series: List[str] = None, **kwargs: Dict[str, any]) -> Dict[str, any]: parameters = dict(model_params['parameters']) other_arguments = dict(kwargs) parameters.update(other_arguments) if not 't_confinement' in parameters.keys(): t_confinement = get_default_params()['other']['confinement'] else: t_confinement = parameters['t_confinement'] if not 't_end' in parameters.keys(): t_end = get_default_params()['other']['deconfinement'] else: t_end = parameters['t_end'] if not 'beta_post' in parameters.keys(): parameters['beta_post'] = get_default_params( )['other']['r0_confinement'] / parameters['dm_r'] if not 'beta_end' in parameters.keys(): parameters['beta_end'] = 1.2 / parameters['dm_r'] R0_start = parameters['beta'] * parameters['dm_r'] R0_confinement = parameters['beta_post'] * parameters['dm_r'] R0_end = parameters['beta_end'] * parameters['dm_r'] ''' beta_0 = parameters['beta'] if not 'R0_start' in parameters.keys() else parameters['R0_start']/parameters['dm_r'] beta_post = R0_confinement/parameters['dm_r'] beta_end = R0_end/parameters['dm_r'] parameters['beta'] = beta_0 if not 'beta_post' in parameters.keys() : parameters['beta_post'] = R0_confinement/parameters['dm_r'] if not 'beta_end' in parameters.keys() : parameters['beta_end'] = R0_end/parameters['dm_r'] ''' rules = [RuleChangeField(t_confinement, 'beta', parameters['beta_post']), RuleChangeField(t_end, 'beta', parameters['beta_end']), ] lists = run_sir_h(parameters, rules, specific_series=series) t = np.linspace(0, parameters['lim_time'] - 1, parameters['lim_time']) def R0(t, k=1.0, R0_start=R0_start, t_confinement=t_confinement, R0_confinement=R0_confinement, t_end=parameters['lim_time'], R0_end=R0_end): # return 3.31 # return R0_start if t < t_confinement else R0_confinement # if t<(t_confinement + t_end)/2: return (R0_start - R0_confinement) / (1 + np.exp(-k * (-t + t_confinement))) + R0_confinement # else: # return (R0_confinement-R0_end) / (1 + np.exp(-k*(-t + t_end))) + R0_end # R0_over_time = [R0(i) for i in range(len(t))] return {'time': t, 'series': {k: np.array(v) for k, v in lists.items()}, # 'series' = {SE': np.array(lists['SE']), 'INCUB': np.array(lists['INCUB']), 'I': np.array(lists['I']), # 'SM': np.array(lists['SM']), 'SI': np.array(lists['SI']), 'R': np.array(lists['R']), # 'DC': np.array(lists['DC'])}, # 'R0': R0_over_time }