Visualisation Apps

Overview:

IncidenceNumberPlot

class branchpro.IncidenceNumberPlot[source]

IncidenceNumberPlot Class Stores the main figure for the Dash app.

add_data(df, time_key='Time', inc_key='Incidence Number', name='Cases')[source]

Supplies data to the figure which will be used for the bar plot.

Parameters:
  • df – (pandas DataFrame) contains numbers of new cases by days. Data stored in columns ‘Time’ and ‘Incidence Number’, respectively.

  • time_key – x-axis label for the bar plot.

  • inc_key – y-axis label for the bar plot.

  • neme – name given to the data in the bar plot.

add_simulation(df, time_key='Time', inc_key='Incidence Number')[source]

Supplies simulated data to the figure which will be added the bar plot as lines and used for comparison with observed data.

Parameters:
  • df – (pandas DataFrame) contains numbers of new cases by days. Data stored in columns ‘Time’ and ‘Incidence Number’, respectively.

  • time_key – x-axis label for the bar plot.

  • inc_key – y-axis label for the bar plot.

show_figure()[source]

Shows current figure.

update_labels(time_label=None, inc_label=None)[source]

Updates the figure labels with user inputed values.

Parameters:
  • time_label – x-axis label for the bar plot.

  • inc_label – y-axis label for the bar plot.

ReproductionNumberPlot

class branchpro.ReproductionNumberPlot[source]

ReproductionNumberPlot Class Stores the main figure for the Dash app.

add_ground_truth_rt(df, time_key='Time Points', r_key='R_t', shape=None)[source]

Plots the true values of R_t as a line on the figure.

Parameters:
  • df – (pandas DataFrame) contains the true values of the reproduction number by days. Data stored in columns ‘Time Points’ and ‘R_t’, respectively.

  • time_key – x-axis label for the line plot.

  • r_key – y-axis label for the line plot.

  • shape – shape of added r profile trajectory.

add_interval_rt(df, time_key='Time Points', r_key='Mean', lr_key='Lower bound CI', ur_key='Upper bound CI', cp_key='Central Probability', colour='indigo', shape=None, ci_legend=True, model_type=None)[source]

Plots the estimated values of R_t as a line on the figure, as well as an area of confidence for the location of the true value.

Parameters:
  • df – (pandas DataFrame) contains the posterior mean with percentiles over time. Data stored in columns ‘Time Points’, ‘Mean’, ‘Lower bound CI’, ‘Upper bound CI’, respectively.

  • time_key – x-axis label for the line plot.

  • r_key – y-axis label for the line plot.

  • lr_key – dataframe label for the lower bound of the credible interval of r.

  • ur_key – dataframe label for the upper bound of he credible interval of r.

  • cp_key – dataframe label for the central probability of the credible interval of r.

  • colour – colour of added r profile trajectory.

  • shape – shape of added r profile trajectory.

  • ci_legend – add legernd of the confidence interval.

  • model_type – name of the type of model used to produce the r profile.

show_figure()[source]

Shows current figure.

update_labels(time_label=None, r_label=None)[source]

Updates the figure labels with user inputed values.

Parameters:
  • time_label – x-axis label for the line plot.

  • r_label – y-axis label for the line plot.

Sliders

class branchpro._SliderComponent[source]

_SliderComponent Class Stores slider information for sliders in branchpro Dash app. In this class we keep track of both the figure and the sliders determined by the data figure.

add_slider(label, new_id, init_val, min_val, max_val, step_size, as_integer=False, invisible=False)[source]

Creates a new slider with label for the Dash app plot.

Parameters:
  • label – Title text shown above slider.

  • new_id – ID of slider (internal, not shown in app).

  • init_val – Initial slider value (default position).

  • min_val – Minimum (leftmost) slider value.

  • max_val – Maximum (rightmost) slider value.

  • step_size – Incremement between slider values.

  • as_integer – (boolean) display decimals or not for the marks of sliders.

  • invisible – (boolean) hides slider object.

get_sliders_div()[source]

Combines all sliders into a html.Div object.

slider_ids()[source]

Returns list of all slider IDs.

Apps

class branchpro.BranchProDashApp[source]

Base class for dash apps for branching processes.

Notes

When deploying objects of this class in a server environment, it is recommended to use the lock to prevent interference between threads.

@app.app.callback(...)
def callback(...):
    with app.lock:
        ...  # your callback code here
        return ...
add_collapsed_text(text, title='More details...')[source]

Add a block of text at the top of the app.

By default, this text will be hidden. The user can click on a button with the specified title in order to view the text.

Parameters:
  • text (str) – The text to add to the html div

  • title (str) – str which will be displayed on the show/hide button

add_text(text)[source]

Add a block of text at the top of the app.

This can be used to add introductory text that everyone looking at the app will see right away.

Parameters:

text (str) – The text to add to the html div

parse_contents(contents, filename, is_si=False, sim_app=False)[source]

Load a text (csv) file into a pandas dataframe.

This method is for loading:

  • incidence number data. It expects files to have at least two columns, the first with title Time and the second with title Incidence Number.

  • serial interval data. It expects files to have one column .

Parameters:
  • contents (str) – File contents in binary encoding

  • filename (str) – Name of the file

  • is_si (boolean) – Function of the file in the context of the app, true if uploaded data is a serial interval.

  • sim_app (boolean) – Data to be read will be used for the simulation app.

Returns:

  • html.Div – A div which contains a message for the user.

  • pandas.DataFrame or numpy.array – A dataframe with the loaded data file. An array with the loaded serial interval file. If the file load was not successful, it will be None.

refresh_user_data_json(**kwargs)[source]

Load the user’s session data from JSON.

To be called at the beginning of a callback so that this object contains the appropriate information for completing the request.

The inputs are translated from JSON to pandas dataframes, and saved in the self.session_data dictionary. All previous entries in self.session_data are cleared.

Parameters:

kwargs – Each key should be the id or name of a storage container recognized by the particular app, and each value should be a string containing the JSON data accessed from that storage.

Simulation Apps

class branchpro.IncidenceNumberSimulationApp[source]

IncidenceNumberSimulationApp Class: Class for the simulation dash app with figure and sliders for the BranchPro models.

update_figure(fig=None, simulations=None, source=None)[source]

Generate a plotly figure of incidence numbers and simulated cases.

By default, this method uses the information saved in self.session_data to populate the figure with data. If a current figure and dash callback source are passed, it will try to just update the existing figure for speed improvements.

Parameters:
  • fig (dict) – Current copy of the figure

  • simulations (pd.DataFrame) – Simulation trajectories to add to the figure.

  • source (str) – Dash callback source

Returns:

Figure with updated data and simulations

Return type:

plotly.Figure

update_simulation(new_init_cond, new_r0, new_r1, new_t1, new_epsilon)[source]

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:

Simulations storage dataframe

Return type:

pandas.DataFrame

update_sliders(init_cond=10.0, r0=1.0, r1=0.5, magnitude_init_cond=None)[source]

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:

A dash html component containing the sliders

Return type:

html.Div

Inference Apps

class branchpro.BranchProInferenceApp(long_callback_manager=None)[source]

BranchProInferenceApp Class: Class for the inference dash app with figure and sliders for the BranchPro models.

update_data_figure()[source]

Update the data figure based on currently stored information.

Returns:

Figure with updated data

Return type:

plotly.Figure

update_inference_figure(source=None)[source]

Update the inference figure based on currently stored information.

Parameters:

source (str) – Dash callback source

Returns:

Figure with updated posterior distribution

Return type:

plotly.Figure

update_posterior(mean, stdev, tau, central_prob, epsilon=None, progress_fn=None)[source]

Update the posterior distribution based on slider values.

Parameters:
  • mean – (float) updated position on the slider for the mean of the prior for the Branch Pro model in the posterior.

  • stdev – (float) updated position on the slider for the standard deviation of the prior for the Branch Pro model in the posterior.

  • tau – (int) updated 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) updated position on the slider for the level of the computed credible interval of the estimated R number values.

  • 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.

  • progress_fn – Function of integer argument to send to posterior run_inference. It can be used for dash callbacks set_progress (see update_posterior_storage in the app script)

Returns:

The posterior distribution, summarized in a dataframe with the following columns: ‘Time Points’, ‘Mean’, ‘Lower bound CI’ and ‘Upper bound CI’

Return type:

pandas.DataFrame

update_sliders(mean=5.0, stdev=5.0, tau=6, central_prob=0.95)[source]

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:

A dash html component containing the sliders

Return type:

html.Div