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_init(self):
     with self.assertRaises(TypeError):
         bp.SimulationController('1', 2, 5)
 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))
예제 #5
0
    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