def test_simulate(self): libr_model_1 = bp.LocImpBranchProModel(2, np.array([1, 2, 3, 2, 1]), 0) libr_model_1.set_imported_cases([1, 2.0, 4, 8], [5, 10, 9, 2]) simulated_sample_model_1 = libr_model_1.simulate(1, np.array([2, 4])) new_simulated_sample_model_1 = libr_model_1.simulate(1, [0, 2, 4]) self.assertEqual(simulated_sample_model_1.shape, (2,)) self.assertEqual(new_simulated_sample_model_1.shape, (3,)) libr_model_2 = bp.LocImpBranchProModel(2, [1, 2, 3, 2, 1], 0) libr_model_2.set_imported_cases([1, 2, 4, 8], [5, 10, 9, 2]) simulated_sample_model_2 = libr_model_2.simulate(1, [2, 4, 7]) self.assertEqual(simulated_sample_model_2.shape, (3,)) libr_model_3 = bp.LocImpBranchProModel(0, [1, 2], 0) libr_model_3.set_r_profile([3, 1], [1, 2], 3) libr_model_3.set_imported_cases([1, 2, 4, 8], [5, 10, 9, 2]) simulated_sample_model_3 = libr_model_3.simulate(1, [2, 4, 7]) self.assertEqual(simulated_sample_model_3.shape, (3,))
def test_set_imported_cases(self): libr_model = bp.LocImpBranchProModel(0, [1, 2], 0) with self.assertRaises(ValueError): libr_model.set_imported_cases([1, 2], np.array([[5], [10]])) with self.assertRaises(ValueError): libr_model.set_imported_cases(np.array([[1], [2]]), [5, 10]) with self.assertRaises(ValueError): libr_model.set_imported_cases([1, 2, 4], [5, 10])
def test_set_epsilon(self): libr_model1 = bp.LocImpBranchProModel(0, [1, 2], 0) libr_model1.set_epsilon(1.5) self.assertEqual(libr_model1.epsilon, 1.5)
def test__init__(self): with self.assertRaises(TypeError): bp.LocImpBranchProModel(0, [1], '0') with self.assertRaises(ValueError): bp.LocImpBranchProModel(0, [1], -13)
def setUpClass(cls): """Set up the class using real data and inference. The purpose is to test the figure function under realistic conditions. """ # Make a fake set of serial intervals serial_intervals = np.array([[0.1, 0.5, 0.2, 0.2], [0.2, 0.4, 0.2, 0.2], [0.1, 0.5, 0.4, 0.0]]) # Read Ontario data data = pd.read_csv( '../branchpro/branchpro/data_library/covid_ontario/ON.csv')[:51] locally_infected_cases = data['Incidence Number'] imported_cases = data['Imported Cases'] # Get time points num_timepoints = max(data['Time']) start_times = np.arange(1, num_timepoints+1, dtype=int) times = np.arange(num_timepoints+1) # Inference R_t profile, but using the LocImpBranchProPosterior tau = 2 R_t_start = tau+1 a = 1 b = 0.8 # Transform our incidence data into pandas dataframes inc_data = pd.DataFrame( { 'Time': start_times, 'Incidence Number': locally_infected_cases } ) imported_inc_data = pd.DataFrame( { 'Time': start_times, 'Incidence Number': imported_cases } ) # Run inference with epsilon = 1 to get R trajectory inference = branchpro.LocImpBranchProPosteriorMultSI( inc_data=inc_data, imported_inc_data=imported_inc_data, epsilon=1, daily_serial_intervals=serial_intervals, alpha=a, beta=1/b) inference.run_inference(tau=tau) intervals = inference.get_intervals(central_prob=.95) new_rs = intervals['Mean'].values.tolist() # Run simulation of local cases for various values of epsilon epsilon_values = [0.25, 1.0, 1.5] initial_r = new_rs[0] si = np.median(serial_intervals, axis=0) parameters = 0 # initial number of cases all_local_cases = [] num_simulations = 100 samples = [] central_prob = 0.95 for _, epsilon in enumerate(epsilon_values): m = branchpro.LocImpBranchProModel(initial_r, si, epsilon) m.set_r_profile(new_rs, start_times[R_t_start:]) m.set_imported_cases(start_times, imported_cases) for sim in range(num_simulations): samples.append(m.simulate(parameters, times)) simulation_estimates = np.mean(np.vstack(samples), axis=0) lb = 100*(1-central_prob)/2 ub = 100*(1+central_prob)/2 simulation_interval = np.percentile( np.vstack(samples), q=np.array([lb, ub]), axis=0) simulation_df = pd.DataFrame( { 'Mean': simulation_estimates, 'Lower bound CI': simulation_interval[0], 'Upper bound CI': simulation_interval[1] } ) all_local_cases.append(simulation_df) cls.imported_cases = imported_cases cls.start_times = start_times cls.R_t_start = R_t_start cls.new_rs = new_rs cls.epsilon_values = epsilon_values cls.all_local_cases = all_local_cases
def update_simulation(self, new_init_cond, new_r0, new_r1, new_t1, new_epsilon): """Run a simulation of the branchpro model at the given slider values. Parameters ---------- new_init_cond (int) updated position on the slider for the number of initial cases for the Branch Pro model in the simulator. new_r0 (float) updated position on the slider for the initial reproduction number for the Branch Pro model in the simulator. new_r1 (float) updated position on the slider for the second reproduction number for the Branch Pro model in the simulator. new_t1 (float) updated position on the slider for the time change in reproduction numbers for the Branch Pro model in the simulator. new_epsilon (float) updated position on the slider for the constant of proportionality between local and imported cases for the Branch Pro model in the posterior. Returns ------- pandas.DataFrame Simulations storage dataframe """ data = self.session_data.get('data_storage') serial_interval = self.session_data.get( 'interval_storage').iloc[:, 0].values if data is None: raise dash.exceptions.PreventUpdate() time_label, inc_label = (data.columns[0], 'Incidence Number') times = data[time_label] # Make a new dataframe to save the simulation result simulations = data[[time_label]] # Add the correct R profile to the branchpro model if 'Imported Cases' in data.columns: br_pro_model = bp.LocImpBranchProModel(new_r0, serial_interval, new_epsilon) br_pro_model.set_imported_cases( times, data.loc[:, ['Imported Cases']].squeeze().tolist()) else: br_pro_model = bp.BranchProModel(new_r0, serial_interval) br_pro_model.set_r_profile([new_r0, new_r1], [0, new_t1]) # Generate one simulation trajectory from this model simulation_controller = bp.SimulationController( br_pro_model, min(times), max(times)) try: sim_data = simulation_controller.run(new_init_cond) except ValueError: sim_data = -np.ones(max(times)) # Add data to simulations storage sim_times = simulation_controller.get_regime() simulations = pd.DataFrame({ time_label: sim_times, inc_label: sim_data }) return simulations