def test__init__(self): with self.assertRaises(ValueError): bp.BranchProModel(0, [0]) with self.assertRaises(TypeError): bp.BranchProModel('0', [1]) with self.assertRaises(ValueError): bp.BranchProModel(0, 1)
def test_simulate(self): branch_model_1 = bp.BranchProModel(2, np.array([1, 2, 3, 2, 1])) simulated_sample_model_1 = branch_model_1.simulate(1, np.array([2, 4])) new_simulated_sample_model_1 = branch_model_1.simulate(1, [0, 2, 4]) self.assertEqual(simulated_sample_model_1.shape, (2,)) self.assertEqual(new_simulated_sample_model_1.shape, (3,)) branch_model_2 = bp.BranchProModel(2, [1, 2, 3, 2, 1]) simulated_sample_model_2 = branch_model_2.simulate(1, [2, 4, 7]) self.assertEqual(simulated_sample_model_2.shape, (3,)) br_model3 = bp.BranchProModel(0, [1, 2]) br_model3.set_r_profile([3, 1], [1, 2], 3) simulated_sample_model_3 = br_model3.simulate(1, [2, 4, 7]) self.assertEqual(simulated_sample_model_3.shape, (3,))
def test_set_serial_intervals(self): br_model = bp.BranchProModel(0, [1, 2]) br_model.set_serial_intervals([1, 3, 2]) npt.assert_array_equal( br_model.get_serial_intervals(), np.array([1, 3, 2]) ) with self.assertRaises(ValueError): br_model.set_serial_intervals((1))
def test_set_r_profile(self): br_model1 = bp.BranchProModel(0, [1, 2]) br_model1.set_r_profile([1], [2]) npt.assert_array_equal(br_model1.get_r_profile(), np.array([0, 1])) br_model2 = bp.BranchProModel(0, [1, 2]) br_model2.set_r_profile([3, 1], [1, 2], 3) npt.assert_array_equal(br_model2.get_r_profile(), np.array([3, 1, 1])) with self.assertRaises(ValueError): br_model1.set_r_profile(1, [1]) with self.assertRaises(ValueError): br_model1.set_r_profile([1], 1) with self.assertRaises(ValueError): br_model1.set_r_profile([0.5, 1], [1]) with self.assertRaises(ValueError): br_model1.set_r_profile([1], [-1]) with self.assertRaises(ValueError): br_model1.set_r_profile([1, 2], [2, 1])
def test_run(self): br_pro_model = bp.BranchProModel(2, np.array([1, 2, 3, 2, 1])) simulationController = bp.SimulationController(br_pro_model, 2, 7) one_run_of_simulator = simulationController.run(1) self.assertEqual(one_run_of_simulator.shape, (6, ))
def test_get_regime(self): br_pro_model = bp.BranchProModel(2, np.array([1, 2, 3, 2, 1])) simulationController = bp.SimulationController(br_pro_model, 2, 7) regime = simulationController.get_regime() npt.assert_array_equal(regime, np.arange(2, 8).astype(int))
def test_get_time_bounds(self): br_pro_model = bp.BranchProModel(2, np.array([1, 2, 3, 2, 1])) simulationController = bp.SimulationController(br_pro_model, 2, 7) bounds = simulationController.get_time_bounds() self.assertEqual(bounds, (2, 7))
def test_get_r_profile(self): br_model1 = bp.BranchProModel(0, [1, 2]) br_model1.set_r_profile([1], [2]) npt.assert_array_equal(br_model1.get_r_profile(), np.array([0, 1]))
def test_get_serial_intervals(self): br_model = bp.BranchProModel(0, [1, 2]) npt.assert_array_equal( br_model.get_serial_intervals(), np.array([1, 2]))
# Generate synthetic data num_timepoints = 30 # number of days for incidence data # Build the serial interval w_s ws_mean = 2.6 ws_var = 1.5**2 theta = ws_var / ws_mean k = ws_mean / theta w_dist = scipy.stats.gamma(k, scale=theta) disc_w = w_dist.pdf(np.arange(1, num_timepoints+1)) # Simulate incidence data initial_r = 3 serial_interval = disc_w model = bp.BranchProModel(initial_r, serial_interval) new_rs = [3, 0.5] # sequence of R_0 numbers start_times = [0, 15] # days at which each R_0 period begins model.set_r_profile(new_rs, start_times) parameters = 10 # initial number of cases times = np.arange(num_timepoints) cases = model.simulate(parameters, times) example_data = pd.DataFrame({ 'Days': times, 'Incidence Number': cases, 'R_t': [np.nan] + list(model.get_r_profile()) }) sliders = ['epsilon', 'mean', 'stdev', 'tau', 'central_prob']
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