def test_get_sliders_div(self):
        sliders = bp._SliderComponent()
        sliders.add_slider('param1', '1', 0, 0, 1, 0.5)
        sliders.add_slider('param2', '2', 0.5, 0, 1, 0.25)
        div = sliders.get_sliders_div().children

        self.assertEqual(div[0].children[0].children, 'param1')
        self.assertEqual(div[0].children[1].id, '1')
        self.assertEqual(div[0].children[1].min, 0)
        self.assertEqual(div[0].children[1].max, 1)
        self.assertEqual(div[0].children[1].value, 0)
        self.assertEqual(div[0].children[1].marks,
                         {ri: '{:.2f}'.format(ri)
                          for ri in [0, 0.50, 1]})
        self.assertEqual(div[0].children[1].step, 0.5)

        self.assertEqual(div[1].children[0].children, 'param2')
        self.assertEqual(div[1].children[1].id, '2')
        self.assertEqual(div[1].children[1].min, 0)
        self.assertEqual(div[1].children[1].max, 1)
        self.assertEqual(div[1].children[1].value, 0.5)
        self.assertEqual(
            div[1].children[1].marks,
            {ri: '{:.2f}'.format(ri)
             for ri in [0, 0.25, 0.50, 0.75, 1]})
        self.assertEqual(div[1].children[1].step, 0.25)
    def test_add_slider(self):
        sliders = bp._SliderComponent()
        sliders.add_slider('param1', '1', 0, 0, 1, 0.5)
        sliders.add_slider('param2', '2', 0, 0, 15, 1, as_integer=True)
        sliders.add_slider('param3', '3', 0, 0, 1, 0.5, invisible=True)

        self.assertEqual(sliders._sliders[0].children[0].children, 'param1')
        self.assertEqual(sliders._sliders[0].children[1].id, '1')
        self.assertEqual(sliders._sliders[0].children[1].min, 0)
        self.assertEqual(sliders._sliders[0].children[1].max, 1)
        self.assertEqual(sliders._sliders[0].children[1].value, 0)
        self.assertEqual(sliders._sliders[0].children[1].marks,
                         {ri: '{:.2f}'.format(ri)
                          for ri in [0, 0.50, 1]})
        self.assertEqual(sliders._sliders[0].children[1].step, 0.5)

        self.assertEqual(sliders._sliders[1].children[0].children, 'param2')
        self.assertEqual(sliders._sliders[1].children[1].id, '2')
        self.assertEqual(sliders._sliders[1].children[1].min, 0)
        self.assertEqual(sliders._sliders[1].children[1].max, 15)
        self.assertEqual(sliders._sliders[1].children[1].value, 0)
        self.assertEqual(
            sliders._sliders[1].children[1].marks,
            {
                # if the slider values need be integers
                ri: '{:.0f}'.format(ri)
                for ri in np.round(np.linspace(0, 15, 10), 0)
            })
        self.assertEqual(sliders._sliders[1].children[1].step, 1)

        self.assertEqual(sliders._sliders[2].children[0].children, 'param3')
        self.assertEqual(sliders._sliders[2].children[1].id, '3')
        self.assertEqual(sliders._sliders[2].children[1].min, 0)
        self.assertEqual(sliders._sliders[2].children[1].max, 1)
        self.assertEqual(sliders._sliders[2].children[1].value, 0)
        self.assertEqual(
            sliders._sliders[0].children[1].marks,
            {ri: '{:.2f}'.format(ri)
             for ri in [0.00, 0.50, 1.00]})
        self.assertEqual(sliders._sliders[2].children[1].step, 0.5)
        self.assertEqual(sliders._sliders[2].style['display'], 'none')
 def test_slider_ids(self):
     sliders = bp._SliderComponent()
     sliders.add_slider('param1', '1', 0, 0, 1, 0.5)
     sliders.add_slider('param2', '2', 0.5, 0, 1, 0.25)
     self.assertEqual(sliders.slider_ids(), ['1', '2'])
 def test__init__(self):
     bp._SliderComponent()
Пример #5
0
    def update_sliders(self,
                       init_cond=10.0,
                       r0=1.0,
                       r1=0.5,
                       magnitude_init_cond=None):
        """Generate sliders for the app.

        This method tunes the bounds of the sliders to the time period and
        magnitude of the data.

        Parameters
        ----------
        init_cond : int
            start position on the slider for the number of initial cases for
            the Branch Pro model in the simulator.
        r0 : float
            start position on the slider for the initial reproduction number
            for the Branch Pro model in the simulator.
        r1 : float
            start position on the slider for the second reproduction number for
            the Branch Pro model in the simulator.
        magnitude_init_cond : int
            maximal start position on the slider for the number of initial
            cases for the Branch Pro model in the simulator. By default, it
            will be set to the maximum value observed in the data.

        Returns
        -------
        html.Div
            A dash html component containing the sliders
        """
        data = self.session_data.get('data_storage')

        # Calculate slider values that depend on the data
        if data is not None:
            time_label, inc_label = data.columns[:2]
            if magnitude_init_cond is None:
                magnitude_init_cond = max(data[inc_label])
            bounds = (1, max(data[time_label]))

        else:
            # choose values to use if there is no data
            if magnitude_init_cond is None:
                magnitude_init_cond = 1000
            bounds = (1, 30)

        mid_point = round(sum(bounds) / 2)

        # Make new sliders
        sliders = bp._SliderComponent()

        if (data is not None) and ('Imported Cases' in data.columns):
            # Add slider for epsilon only when imported cases are detected
            # in the data with default assuming equal R numbers for local
            # and imported cases
            sliders.add_slider('Epsilon', 'epsilon', 1.0, 0.0, 3.0, 0.01)
        else:
            sliders.add_slider('Epsilon',
                               'epsilon',
                               1.0,
                               0.0,
                               3.0,
                               0.01,
                               invisible=True)

        sliders.add_slider('Initial Cases',
                           'init_cond',
                           init_cond,
                           0.0,
                           magnitude_init_cond,
                           1,
                           as_integer=True)
        sliders.add_slider('Initial R', 'r0', r0, 0.1, 10.0, 0.01)
        sliders.add_slider('Second R', 'r1', r1, 0.1, 10.0, 0.01)
        sliders.add_slider('Time of change',
                           't1',
                           mid_point,
                           bounds[0],
                           bounds[1],
                           1,
                           as_integer=True)

        return sliders.get_sliders_div()
Пример #6
0
    def update_sliders(self, mean=5.0, stdev=5.0, tau=6, central_prob=.95):
        """Generate sliders for the app.

        Parameters
        ----------
        mean
            (float) start position on the slider for the mean of the
            prior for the Branch Pro model in the posterior.
        stdev
            (float) start position on the slider for the standard deviation of
            the prior for the Branch Pro model in the posterior.
        tau
            (int) start position on the slider for the tau window used in the
            running of the inference of the reproduction numbers of the Branch
            Pro model in the posterior.
        central_prob
            (float) start position on the slider for the level of the computed
            credible interval of the estimated R number values.

        Returns
        -------
        html.Div
            A dash html component containing the sliders
        """
        data = self.session_data.get('data_storage')
        if data is not None:
            time_label, inc_label = data.columns[:2]
            times = data[time_label]
            max_tau = floor((times.max() - times.min() + 1) / 3)
            if tau > max_tau:
                # If default value of tau exceeds maximum accepted
                # choose tau to be this maximum value
                tau = max_tau
        else:
            max_tau = 7

        sliders = bp._SliderComponent()
        if (data is not None) and ('Imported Cases' in data.columns):
            # Add slider for epsilon only when imported cases are detected
            # in the data with default assuming equal R numbers for local
            # and imported cases
            sliders.add_slider('Epsilon', 'epsilon', 1.0, 0.0, 3.0, 0.01)
        else:
            sliders.add_slider('Epsilon',
                               'epsilon',
                               1.0,
                               0.0,
                               3.0,
                               0.01,
                               invisible=True)

        sliders.add_slider('Prior Mean', 'mean', mean, 0.1, 10.0, 0.01)
        sliders.add_slider('Prior Standard Deviation', 'stdev', stdev, 0.1,
                           10.0, 0.01)
        sliders.add_slider('Inference Sliding Window',
                           'tau',
                           tau,
                           0,
                           max_tau,
                           1,
                           as_integer=True)
        sliders.add_slider('Central Posterior Probability', 'central_prob',
                           central_prob, 0.1, 0.99, 0.01)

        return sliders.get_sliders_div()