Example #1
0
def run_app():
    ############################### Data Load ##################################

    css.hide_menu()

    css.limit_plot_size()

    # Get cached country data
    countries = _fetch_country_data()

    global_data = _fetch_global_data()

    if countries.stale:
        st.caching.clear_cache()

        countries = _fetch_country_data()

    if global_data.stale:
        st.caching.clear_cache()

        global_data = _fetch_global_data()

    ################## Heading Section ####################################

    utils.img_html(
        alt_text='Fractal',
        href='https://fractal.ai',
        src=
        'https://i2.wp.com/fractal.ai/wp-content/uploads/2018/02/header-black-logo.png?fit=126%2C43&ssl=1',
        attributes=dict(width=125, height=43, target='_blank'))

    st.markdown(
        body=generate_html(text=f"Australia COVID-19 Simulator",
                           bold=True,
                           tag="h1"),
        unsafe_allow_html=True,
    )
    st.markdown(
        body=generate_html(
            tag="h2",
            text=
            "A tool to help you visualize the impact of social distancing <br>",
        ),
        unsafe_allow_html=True,
    )

    st.markdown(
        body=generate_html(
            text=
            "<strong>Disclaimer:</strong> <em>The creators of this application are not healthcare professionals. "
            "The illustrations provided were estimated using best available data but might not accurately reflect reality.</em>",
            color="gray",
            font_size="12px",
        ),
        unsafe_allow_html=True,
    )

    ### Add side bar
    sidebar = Sidebar(countries)

    ###################### historical and forecast chart ##############################

    ### Get selected country
    country = sidebar.country

    country_data = countries.country_data[country]

    _historical_df = countries.historical_country_data

    if country == "Australia":

        historical_data_custom = data_utils.make_historical_data(
            _historical_df)

        try:

            forecasted_data = forecast_utils.get_forecasts(
                country, constants.FORECAST_HORIZON)

            historical_plot_df = data_utils.prep_plotting_data(
                forecasted_data, historical_data_custom)

            # fig = graphing.plot_historical_data(historical_data_plot, con_flag=True)

            fig = graphing.plot_time_series_forecasts(historical_plot_df,
                                                      country_flag=True,
                                                      country_name=country)

        except Exception as exc:

            print("Error", exc)

    else:

        historical_data_custom = _historical_df.loc[_historical_df.index ==
                                                    country]

        try:

            forecasted_data = forecast_utils.get_forecasts(
                country, constants.FORECAST_HORIZON)

            historical_plot_df = data_utils.prep_plotting_data(
                forecasted_data, historical_data_custom)

            # fig = graphing.plot_historical_data(historical_data_plot)

            fig = graphing.plot_time_series_forecasts(historical_plot_df,
                                                      country_flag=False,
                                                      country_name=country)

        except Exception as exc:

            print(exc)

    historical_data = _historical_df.loc[_historical_df.index == country]

    number_cases_confirmed = country_data["Confirmed"]

    population = country_data["Population"]

    num_hospital_beds = country_data["Num Hospital Beds"]

    age_data = constants.AGE_DATA.loc[constants.AGE_DATA["State"] ==
                                      country, :]

    st.subheader(
        f"How is the disease likely to spread in {country} in the next week?")

    # Estimate true cases
    true_cases_estimator = models.TrueInfectedCasesModel(
        constants.ReportingRate.default)
    # estimated_true_cases = true_cases_estimator.predict(number_cases_confirmed)

    try:

        week1_est = historical_plot_df.tail(1)

        reported_vs_true_cases(
            int(number_cases_confirmed), week1_est["confirmed"].tolist()[0],
            graphing.abbreviate(week1_est["lower_bound"].tolist()[0],
                                round_factor=0),
            graphing.abbreviate(week1_est["upper_bound"].tolist()[0],
                                round_factor=0))

        # Plot historical data
        st.write(fig)

        print("Historical Data with Forecasts plotted")

    except Exception as exc:

        print(exc)

        st.markdown(
            f"Something went wrong :( Forecasts unavailable. Contact admin")

    ###################### SIR Model and Simulator ##############################

    # Predict infection spread
    sir_model = models.SIRModel(
        transmission_rate_per_contact=constants.TransmissionRatePerContact.
        default,
        contact_rate=sidebar.contact_rate,
        recovery_rate=constants.RecoveryRate.default,
        normal_death_rate=constants.MortalityRate.default,
        critical_death_rate=constants.CriticalDeathRate.default,
        hospitalization_rate=constants.HospitalizationRate.default,
        hospital_capacity=num_hospital_beds,
    )

    df = models.get_predictions(cases_estimator=true_cases_estimator,
                                sir_model=sir_model,
                                num_diagnosed=number_cases_confirmed,
                                num_recovered=country_data["Recovered"],
                                num_deaths=country_data["Deaths"],
                                area_population=population,
                                max_days=sidebar.num_days_for_prediction)

    st.subheader("How will my actions affect the spread?")

    st.write(
        "**Use the slider in the sidebar to see how this changes the dynamics of disease spread**"
    )

    df_base = df[~df.Status.
                 isin(["Need Hospitalization", "Recovered", "Susceptible"])]

    base_graph = graphing.infection_graph(df_base, df_base.Forecast.max(),
                                          population * 0.5, population * 0.75)
    # st.warning(graph_warning)
    st.write(base_graph)

    print("Infections Graph Plotted")

    ###################### Effect on hospitals ##############################

    st.subheader("How will this affect my healthcare system?")

    # Do some rounding to avoid beds sounding too precise!
    approx_num_beds = round(num_hospital_beds / 100) * 100

    st.write(
        f"{country} has around **{approx_num_beds:,}** beds. Bear in mind that most of these "
        "are probably already in use for people sick for other reasons.")

    peak_occupancy = df.loc[df.Status ==
                            "Need Hospitalization"]["Forecast"].max()

    percent_beds_at_peak = min(100 * num_hospital_beds / peak_occupancy, 100)

    num_beds_comparison_chart = graphing.num_beds_occupancy_comparison_chart(
        num_beds_available=approx_num_beds, max_num_beds_needed=peak_occupancy)

    st.write(num_beds_comparison_chart)

    st.markdown(
        f"At peak, **{int(peak_occupancy):,}** people will need hospital beds. At least ** {100 - percent_beds_at_peak:.1f}% ** of "
        f" people who need a bed in hospital might not have access given historical resources of {country}."
    )

    ###################### Death Charts ##############################

    st.subheader("How severe will the impact be?")

    num_dead = df[df.Status == "Dead"].Forecast.iloc[-1]

    num_recovered = df[df.Status == "Recovered"].Forecast.iloc[-1]

    glob_hist = global_data.historical_country_data

    uk_data = glob_hist.loc[(glob_hist.index == "UK") | \
                            (glob_hist.index == "United Kingdom"), :].copy()

    uk_death_mirror = get_uk_death_mirror(uk_data, country_data["Deaths"])

    death_plot = graphing.plot_death_timeseries(df[df.Status == "Dead"],
                                                uk_death_mirror,
                                                country_name=country)

    st.markdown(
        f"If the average person in {country} adopts the selected behavior, we estimate that **{int(num_dead):,}** "
        f"people will die.")

    st.markdown(f"This graph illustrates predicted deaths.")

    outcomes_by_age_group = models.get_status_by_age_group(
        num_dead, num_recovered, age_data)

    fig = graphing.age_segregated_mortality(
        outcomes_by_age_group.loc[:, ["Dead"]])

    st.write(death_plot)

    print("Deaths Graph Plotted")

    st.markdown("-------")
    st.markdown(body=generate_html("We'd love to hear from you!",
                                   tag='h2',
                                   color='#0090c4'),
                unsafe_allow_html=True)
    user_input = st.text_input("Enter your email to contact us")
    utils.contact_us(user_input)

    ###################### Credits ##############################

    st.subheader("References and Credits:")

    st.markdown(
        body=generate_html(
            tag="h4",
            text=
            f"<u><a href=\"{NOTION_MODELLING_DOC}\" target=\"_blank\" style=color:{COLOR_MAP['pink']};>"
            "Methodology</a></u> <span> &nbsp;&nbsp;&nbsp;&nbsp</span>"
            "<hr>",
        ),
        unsafe_allow_html=True,
    )

    st.markdown(
        body=generate_html(
            tag="h4",
            text=
            f"<u><a href=\"https://github.com/CSSEGISandData/COVID-19\" target=\"_blank\" style=color:{COLOR_MAP['pink']};>"
            "2019 Novel Coronavirus COVID-19 (2019-nCoV) Data Repository by Johns Hopkins CSSE</a></u> <span> &nbsp;&nbsp;&nbsp;&nbsp</span>"
            "<hr>",
        ),
        unsafe_allow_html=True,
    )

    print("complete....")
def run_app():
    css.hide_menu()
    css.limit_plot_size()

    # Get cached country data
    countries = fetch_country_data()

    if countries.stale:
        st.caching.clear_cache()
        countries = fetch_country_data()

    st.markdown(
        body=generate_html(text=f"Corona Calculator", bold=True, tag="h1"),
        unsafe_allow_html=True,
    )
    st.markdown(
        body=generate_html(
            tag="h2",
            text="A tool to help you visualize the impact of social distancing <br>",
        ),
        unsafe_allow_html=True,
    )

    st.markdown(
        body=generate_html(
            text="<strong>Disclaimer:</strong> <em>The creators of this application are not healthcare professionals. "
            "The illustrations provided were estimated using best available data but might not accurately reflect reality.</em>",
            color="gray",
            font_size="12px",
        ),
        unsafe_allow_html=True,
    )
    st.markdown(
        body=generate_html(
            tag="h4",
            text=f"<u><a href=\"{NOTION_MODELLING_DOC}\" target=\"_blank\" style=color:{COLOR_MAP['pink']};>"
            "Methodology</a></u> <span> &nbsp;&nbsp;&nbsp;&nbsp</span>"
            f"<u><a href=\"{MEDIUM_BLOGPOST}\" target=\"_blank\" style=color:{COLOR_MAP['pink']};>"
            "Blogpost</a> </u>"
            "<hr>",
        ),
        unsafe_allow_html=True,
    )

    sidebar = Sidebar(countries)
    country = sidebar.country
    country_data = countries.country_data[country]
    _historical_df = countries.historical_country_data
    historical_data = _historical_df.loc[_historical_df.index == country]
    number_cases_confirmed = country_data["Confirmed"]
    population = country_data["Population"]
    num_hospital_beds = country_data["Num Hospital Beds"]

    st.subheader(f"How has the disease spread in {country}?")
    st.write(
        "The number of reported cases radically underestimates the true cases, because people do not show symptoms for "
        "several days, not everybody gets tested, and the tests take a few days to return results. "
        "The extent depends upon your country's testing strategy."
        " This estimate (14% reporting) is from China ([source](https://science.sciencemag.org/content/early/2020/03/13/science.abb3221))."
    )
    # Estimate true cases
    true_cases_estimator = models.TrueInfectedCasesModel(
        constants.ReportingRate.default
    )
    estimated_true_cases = true_cases_estimator.predict(number_cases_confirmed)

    reported_vs_true_cases(int(number_cases_confirmed), estimated_true_cases)

    st.markdown(
        f"Given the prevalence of the infection in your country, the probability of being infected at this time is "
        f"**{estimated_true_cases / population:.3%}**. Even if you show no symptoms, the probability of being infected is "
        f"**{models.get_probability_of_infection_give_asymptomatic(population, estimated_true_cases, constants.AsymptomaticRate.default):.3%}**. "
        f"Note that these probabilities are on a country-wide basis and so may not apply to your situation."
    )

    # Plot historical data
    fig = graphing.plot_historical_data(historical_data)
    st.write(fig)

    asymptomatic_cases_estimator = models.AsymptomaticCasesModel(
        constants.AsymptomaticRate.default
    )

    contact_rate = sidebar.contact_rate

    asymptomatic_sir_model = models.AsymptomaticSIRModel(
        transmission_rate_per_contact=constants.TransmissionRatePerContact.default_per_symptom_state,
        contact_rate=contact_rate,
        asymptomatic_cases_model=asymptomatic_cases_estimator,
        recovery_rate=constants.RecoveryRate.default,
        normal_death_rate=constants.MortalityRate.default,
        critical_death_rate=constants.CriticalDeathRate.default,
        hospitalization_rate=constants.HospitalizationRate.default,
        hospital_capacity=num_hospital_beds
    )

    df = models.get_predictions(
        cases_estimator=true_cases_estimator,
        sir_model=asymptomatic_sir_model,
        num_diagnosed=number_cases_confirmed,
        num_recovered=country_data["Recovered"],
        num_deaths=country_data["Deaths"],
        area_population=population,
    )

    st.subheader("How will my actions affect the spread?")
    st.write(
        "The critical factor for controlling spread is how many others infected people interact with each day. "
        "This has a dramatic effect upon the dynamics of the disease. "
    )
    st.write(
        "**Play with the slider to the left to see how this changes the dynamics of disease spread**"
    )

    df_base = df[~df.Status.isin(["Need Hospitalization"])]
    base_graph = graphing.infection_graph(df_base, df_base.Forecast.max(), sidebar.contact_rate)
    st.warning(graph_warning)
    st.write(base_graph)

    st.subheader("How will this affect my healthcare system?")
    st.write(
        "The important variable for hospitals is the peak number of people who require hospitalization"
        " and ventilation at any one time."
    )

    # Do some rounding to avoid beds sounding too precise!
    approx_num_beds = round(num_hospital_beds / 100) * 100
    st.write(
        f"Your country has around **{approx_num_beds:,}** beds. Bear in mind that most of these "
        "are probably already in use for people sick for other reasons."
    )
    st.write(
        "It's hard to know exactly how many ventilators are present per country, but there will certainly be a worldwide "
        "shortage. Many countries are scrambling to buy them [(source)](https://www.reuters.com/article/us-health-coronavirus-draegerwerk-ventil/germany-italy-rush-to-buy-life-saving-ventilators-as-manufacturers-warn-of-shortages-idUSKBN210362)."
    )

    peak_occupancy = df.loc[df.Status == "Need Hospitalization"]["Forecast"].max()
    percent_beds_at_peak = min(100 * num_hospital_beds / peak_occupancy, 100)

    num_beds_comparison_chart = graphing.num_beds_occupancy_comparison_chart(
        num_beds_available=approx_num_beds, max_num_beds_needed=peak_occupancy, contact_rate=sidebar.contact_rate
    )

    st.write(num_beds_comparison_chart)

    st.markdown(
        f"At peak, **{int(peak_occupancy):,}** people will need hospital beds. ** {percent_beds_at_peak:.1f}% ** of people "
        f"who need a bed in hospital will have access to one given your country's historical resources. This does "
        f"not take into account any special measures that may have been taken in the last few months."
    )

    st.subheader("How severe will the impact be?")

    num_dead = df[df.Status == "Dead"].Forecast.iloc[-1]
    num_recovered = df[df.Status == "Recovered"].Forecast.iloc[-1]
    st.markdown(
        f"If the average person in your country adopts the selected behavior, we estimate that **{int(num_dead):,}** "
        f"people will die."
    )

    st.markdown(
        f"The graph above below a breakdown of casualties and hospitalizations by age group."
    )

    outcomes_by_age_group = models.get_status_by_age_group(num_dead, num_recovered)
    fig = graphing.age_segregated_mortality(
        outcomes_by_age_group.loc[:, ["Dead", "Need Hospitalization"]], contact_rate=sidebar.contact_rate
    )
    st.write(fig)

    st.write(
        f"Parameters by age group, including demographic distribution, are [worldwide numbers](https://population.un.org/wpp/DataQuery/) "
        f"so they may be slightly different in your country."
    )
    st.write(
        f"We've used mortality rates from this [recent paper from Imperial College](https://www.imperial.ac.uk/media/imperial-college/medicine/sph/ide/gida-fellowships/Imperial-College-COVID19-NPI-modelling-16-03-2020.pdf?fbclid=IwAR3TzdPTcLiOZN5r2dMd6_08l8kG0Mmr0mgP3TdzimpqB8H96T47ECBUfTM). "
        f"However, we've adjusted them according to the [maximum mortality rate recorded in Wuhan](https://wwwnc.cdc.gov/eid/article/26/6/20-0233_article)"
        f" when your country's hospitals are overwhelmed: if more people who need them lack hospital beds, more people will die."
    )
    st.write("<hr>", unsafe_allow_html=True)
    st.write(
        "Like this? [Click here to share it on Twitter](https://ctt.ac/u5U39), and "
        "[let us know your feedback via Google Form](https://forms.gle/J6ZFFgh4rVQm4y8G7)"
    )

    utils.insert_github_logo()
def run_app():

    css.hide_menu()
    css.limit_plot_size()

    # Get cached country data
    countries = _fetch_country_data()

    if countries.stale:
        st.caching.clear_cache()
        countries = _fetch_country_data()

    st.markdown(
        body=generate_html(text=f"Corona Calculator", bold=True, tag="h1"),
        unsafe_allow_html=True,
    )
    st.markdown(
        body=generate_html(
            tag="h2",
            text=
            "A tool to help you visualize the impact of social distancing <br>",
        ),
        unsafe_allow_html=True,
    )

    st.markdown(
        body=generate_html(
            text=
            "<strong>Disclaimer:</strong> <em>The creators of this application are not healthcare professionals. "
            "The illustrations provided were estimated using best available data but might not accurately reflect reality.</em>",
            color="gray",
            font_size="12px",
        ),
        unsafe_allow_html=True,
    )
    st.markdown(
        body=generate_html(
            tag="h4",
            text=
            f"<u><a href=\"{NOTION_MODELLING_DOC}\" target=\"_blank\" style=color:{COLOR_MAP['pink']};>"
            "Methodology</a></u> <span> &nbsp;&nbsp;&nbsp;&nbsp</span>"
            f"<u><a href=\"{MEDIUM_BLOGPOST}\" target=\"_blank\" style=color:{COLOR_MAP['pink']};>"
            "Blogpost</a> </u>"
            "<hr>",
        ),
        unsafe_allow_html=True,
    )

    sidebar = Sidebar(countries)
    country = sidebar.country
    country_data = countries.country_data[country]
    number_cases_confirmed = country_data["Confirmed"]
    population = country_data["Population"]
    num_hospital_beds = country_data["Num Hospital Beds"]

    sir_model = models.SIRModel(
        transmission_rate_per_contact=constants.TransmissionRatePerContact.
        default,
        contact_rate=sidebar.contact_rate,
        recovery_rate=constants.RecoveryRate.default,
        normal_death_rate=constants.MortalityRate.default,
        critical_death_rate=sidebar.severe_mortality_rate,
        hospitalization_rate=constants.HospitalizationRate.default,
        hospital_capacity=num_hospital_beds,
    )
    true_cases_estimator = models.TrueInfectedCasesModel(
        constants.AscertainmentRate.default)

    df = models.get_predictions(
        true_cases_estimator,
        sir_model,
        number_cases_confirmed,
        population,
        sidebar.num_days_for_prediction,
    )

    reported_vs_true_cases(
        number_cases_confirmed,
        true_cases_estimator.predict(number_cases_confirmed))

    st.write(
        "The number of reported cases radically underestimates the true cases, because people do not show symptoms for "
        "several days, not everybody gets tested, and the tests take a few days to  return results. "
        "The extent depends upon your country's testing strategy."
        " We estimated the above using numbers from Japan ([source](https://www.ncbi.nlm.nih.gov/pubmed/32033064))."
    )

    st.subheader("How will the disease spread?")
    st.write(
        "The critical factor for controlling spread is how many others infected people interact with each day. "
        "This has a dramatic effect upon the dynamics of the disease. ")
    st.write(
        "**Play with the slider to the left to see how this changes the dynamics of disease spread**"
    )

    df_base = df[~df.Status.isin(["Need Hospitalization", "Need Ventilation"])]
    base_graph = graphing.infection_graph(df_base, df.Forecast.max())
    st.warning(graph_warning)
    st.write(base_graph)

    hospital_graph = graphing.hospitalization_graph(
        df[df.Status.isin(["Infected", "Need Hospitalization"])],
        num_hospital_beds,
        max(num_hospital_beds, df.Forecast.max()),
    )

    st.write(
        "Note that we use a fixed estimate of the mortality rate here, of 1% [(source)](https://institutefordiseasemodeling.github.io/nCoV-public/analyses/first_adjusted_mortality_estimates_and_risk_assessment/2019-nCoV-preliminary_age_and_time_adjusted_mortality_rates_and_pandemic_risk_assessment.html). "
        "In reality, the mortality rate will be highly dependent upon the load upon the healthcare system and "
        "the availability of treatment. Some estimates ([like this one](https://www.thelancet.com/journals/laninf/article/PIIS1473-3099(20)30195-X/fulltext)) are closer to 6%."
    )

    st.subheader("How will this affect my healthcare system?")
    st.write(
        "The important variable for hospitals is the peak number of people who require hospitalization"
        " and ventilation at any one time.")

    # Do some rounding to avoid beds sounding too precise!
    st.write(
        f"Your country has around **{round(num_hospital_beds / 100) * 100:,}** beds. Bear in mind that most of these "
        "are probably already in use for people sick for other reasons.")
    st.write(
        "It's hard to know how many ventilators are present per country, but there will certainly be a worldwide "
        "shortage. Many countries are scrambling to buy them [(source)](https://www.reuters.com/article/us-health-coronavirus-draegerwerk-ventil/germany-italy-rush-to-buy-life-saving-ventilators-as-manufacturers-warn-of-shortages-idUSKBN210362)."
    )

    st.warning(graph_warning)
    st.write(hospital_graph)
    peak_occupancy = df.loc[df.Status ==
                            "Need Hospitalization"]["Forecast"].max()
    percent_beds_at_peak = min(100 * num_hospital_beds / peak_occupancy, 100)

    st.markdown(
        f"At peak, **{peak_occupancy:,}** people will need hospital beds. ** {percent_beds_at_peak:.1f} % ** of people "
        f"who need a bed in hospital will have access to one given your country's historical resources. This does "
        f"not take into account any special measures that may have been taken in the last few months."
    )