コード例 #1
0
def collect_inputs(parent_region="BR", where=st.sidebar):
    """
    Collect input parameters for the app to run.

    Returns:
        Dictionary with the following keys:
            parent_region, regions, columns, targets, days
    """
    st = where
    kind = st.selectbox(_("Select scenario"), list(REGIONS_TYPES[parent_region]))
    query = REGIONS_TYPES[parent_region][kind]

    regions = get_regions(**query)

    msg = _("Columns")
    columns = st.multiselect(msg, COLUMNS, default=COLUMNS_DEFAULT)

    msg = _("Isolation scores")
    kwargs = {"default": TARGETS_DEFAULT, "format_func": lambda x: f"{x}%"}
    targets = st.multiselect(msg, TARGETS, **kwargs)

    msg = _("Show values for the given days")
    days = st.multiselect(msg, DAYS, default=DAYS_DEFAULT)

    return {
        "parent_region": parent_region,
        "regions": regions,
        "columns": columns,
        "targets": targets,
        "days": days,
    }
コード例 #2
0
    def ask(self, parent_region="BR", where=st.sidebar):

        st = where
        scenario_kind = st.selectbox(_("Select scenario"), list(REGIONS_TYPES[parent_region]))
        query = REGIONS_TYPES[parent_region][scenario_kind]

        regions = self.__datahandler.get_regions(**query)

        message = _("Columns")
        columns = st.multiselect(message, COLUMNS, default=COLUMNS_DEFAULT)

        message = _("Isolation scores")
        kwargs = {"default": TARGETS_DEFAULT, "format_func": lambda x: f"{x}%"}
        targets = st.multiselect(message, TARGETS, **kwargs)

        message = _("Show values for the given days")
        days = st.multiselect(message, DAYS, default=DAYS_DEFAULT)

        self.__datahandler.user_inputs = {
            "parent_region": parent_region,
            "regions": regions,
            "columns": columns,
            "targets": targets,
            "days": days,
            "disease": "covid-19"
        }
コード例 #3
0
    def ask(self, parent_region="BR", where=st.sidebar):
        st = where
        scenario_kind = st.selectbox(_("Select scenario"), list(REGIONS_TYPES[parent_region]))
        scenario = REGIONS_TYPES[parent_region][scenario_kind]
        regions = self.__datahandler.get_regions(**scenario["query"])

        if "filter" in scenario:
            filtering = scenario["filter"]
            format_func = filtering.pop("format_func", None)
            if format_func is not None:
                function = format_func

                def format_func(x):
                    if x == "all":
                        return _("All")
                    return function(x)

            field, message = filtering.popitem()
            groups = sk.group_by(lambda x: getattr(x, field), regions)

            key = st.selectbox(message, ["all", *groups], format_func=format_func)
            if key != "all":
                regions = groups[key]

        message = _("Columns")
        columns = st.multiselect(message, COLUMNS, default=COLUMNS_DEFAULT)

        message = _("Isolation scores")
        kwargs = {"default": TARGETS_DEFAULT, "format_func": lambda x: f"{x}%"}
        targets = st.multiselect(message, TARGETS, **kwargs)

        message = _("Show values for the given days")
        days = st.multiselect(message, DAYS, default=DAYS_DEFAULT)
        if any(not isinstance(d, int) for d in days):
            day_max = sk.pipe(
                days,
                sk.remove(lambda d: isinstance(d, int)),
                sk.map(lambda d: int(NUMBER.search(d).groups()[0])),
                max,
            )
            days = list(range(1, day_max + 1))

        message = _("Transpose data")
        transpose = st.checkbox(message, value=False)

        self.__datahandler.user_inputs = {
            "parent_region": parent_region,
            "regions": regions,
            "columns": columns,
            "targets": targets,
            "days": days,
            "scenario": scenario,
            "transpose": transpose,
            "disease": "covid-19"
        }
コード例 #4
0
def options(where=st):
    st = where
    regs = regions("BR", type="state")

    # Regions to highlight
    st.subheader(_("Highlight"))
    opts = {
        "BR-1": _("North"),
        "BR-2": _("Northeast"),
        "BR-3": _("Southeast"),
        "BR-4": _("South"),
        "BR-5": _("Midwest"),
        "select": _("Select states"),
        # 'top5': _('Top 5'), NotImplemented
    }
    msg = _("Which states do you want to highlight?")
    opt = st.radio(msg, [*opts], format_func=opts.get)
    if opt == "select":
        fmt = {r.id: r.name for r in regs}.get
        ids = [r.id for r in regs]
        msg = _("Select states to highlight")
        select = set(
            st.multiselect(msg, ids, format_func=fmt, default=[regs[0].id]))
        highlight = [r for r in regs if r.id in select]
    elif opt == "top5":
        highlight = opt
    else:
        highlight = [r for r in regs if r.parent_id == opt]

    # Options
    st.subheader(_("Options"))
    logy = not st.checkbox(_("Linear scale"))

    column = "deaths" if st.checkbox(_("Plot deaths")) else "cases"

    diff = st.checkbox(_("Plot new daily cases"))

    population_adj = st.checkbox(_("Adjust for population"))

    default = (20 if diff else 100) // (1 if column == "cases" else 10)
    thresh = st.number_input(_("Minimum number of cases"),
                             min_value=0,
                             value=default)

    default = 7 if diff else 0
    smooth = st.number_input(_("Smooth over n days"),
                             min_value=0,
                             value=default)

    return {
        "regions": regs,
        "highlight": highlight,
        "diff": diff,
        "thresh": thresh,
        "smoothing": smooth,
        "column": column,
        "logy": logy,
        "population_adj": population_adj,
    }
コード例 #5
0
def sidebar(parent_region="BR", where=st.sidebar):
    """
    Collect input parameters for the app to run.

    Returns:
        Dictionary with the following keys:
            parent_region, regions, columns, targets, days
    """
    st = where
    kind = st.selectbox(_("Select scenario"), list(REGIONS_TYPES[parent_region]))
    scenario = REGIONS_TYPES[parent_region][kind]
    regions = get_regions(**scenario["query"])

    if "filter" in scenario:
        filtering = scenario["filter"]
        format_func = filtering.pop("format_func", None)
        if format_func is not None:
            fn = format_func

            def format_func(x):
                if x == "all":
                    return _("All")
                return fn(x)

        field, msg = filtering.popitem()
        groups = sk.group_by(lambda x: getattr(x, field), regions)

        key = st.selectbox(msg, ["all", *groups], format_func=format_func)
        if key != "all":
            regions = groups[key]

    msg = _("Columns")
    columns = st.multiselect(msg, COLUMNS, default=COLUMNS_DEFAULT)

    msg = _("Isolation scores")
    kwargs = {"default": TARGETS_DEFAULT, "format_func": lambda x: f"{x}%"}
    targets = st.multiselect(msg, TARGETS, **kwargs)

    msg = _("Show values for the given days")
    days = st.multiselect(msg, DAYS, default=DAYS_DEFAULT)
    if any(not isinstance(d, int) for d in days):
        day_max = sk.pipe(
            days,
            sk.remove(lambda d: isinstance(d, int)),
            sk.map(lambda d: int(NUMBER.search(d).groups()[0])),
            max,
        )
        days = list(range(1, day_max + 1))

    msg = _("Transpose data")
    transpose = st.checkbox(msg, value=False)

    return {
        "parent_region": parent_region,
        "regions": regions,
        "columns": columns,
        "targets": targets,
        "days": days,
        "scenario": scenario,
        "transpose": transpose,
    }
コード例 #6
0
def show_outputs(base, group, region: RegionT, plot_opts, clinical_opts,
                 **kwargs):
    """
    Show results from user input.
    """
    cmodels = group.clinical.overflow_model(**clinical_opts)
    cforecast = cmodels[0]
    start = base.info["event.simulation_start"]

    #
    # Introduction
    #
    st.header(_("Introduction"))
    st.markdown(report_intro(region))
    st.cards(
        {
            _("Basic reproduction number"): fmt(base.R0),
            _("Ascertainment rate"): pc(
                base.info["observed.notification_rate"]),
        },
        color="st-gray-900",
    )

    #
    # Forecast
    #
    st.header(_("Forecasts"))
    st.markdown(forecast_intro(region))

    # Infectious curve
    group["infectious:dates"].plot(**plot_opts)
    mark_x(start.date, "k--")
    plt.legend()
    plt.title(_("Active cases"))
    plt.tight_layout()
    st.pyplot()

    st.markdown("#### " + _("Download data"))
    opts = ["critical", "severe", "infectious", "cases", "deaths"]
    default_columns = ["critical", "severe", "cases", "deaths"]
    columns = st.multiselect(_("Select columns"),
                             opts,
                             default=default_columns)

    rename = dict(zip(range(len(columns)), columns))
    columns = [c + ":dates" for c in columns]
    data = pd.concat([cm[columns].rename(rename, axis=1) for cm in cmodels],
                     axis=1,
                     keys=cmodels.names)
    st.data_anchor(data.astype(int), f"data-{region.id}.csv")

    #
    # Reopening
    #
    st.header(_("When can we reopen?"))
    st.markdown(reopening_intro(region))

    st.subheader(_("Step 1: Controlling the curve"))
    st.markdown(rt_intro(region))

    st.subheader(_("Step 2: Testing"))
    st.markdown(rt_intro(region))
    if kwargs.get("show_weekday_rate"):
        region.ui.weekday_rate()

    st.subheader(_("Step 3: Hospital capacity"))
    st.markdown(rt_intro(region))

    # Hospitalization
    cmodels["critical:dates"].plot(**plot_opts)
    mark_x(start.date, "k--")
    mark_y(cforecast.icu_surge_capacity, "k:")
    plt.legend()
    plt.title(_("Critical cases"))
    plt.tight_layout()
    st.pyplot()
コード例 #7
0
ファイル: projections.py プロジェクト: pydemic/pydemic-ui
mul = notification if is_reported else 1.0
progression_cards(cm["cases"] * mul, color="st-gray-900")

st.subheader(_("Hospitalizations"))
hospitalization_chart(cm, shift=len(cases))

st.subheader(_("Projections"))
m = cm.infection_model
date = m.dates[data_size]
base = cm.infection_model.trim_dates(0, data_size)
assert len(base.data) == data_size, (data_size, len(base.data), len(m.times))

valid_rates = [0.1 * i for i in range(1, 10)]
rates = [valid_rates[i] for i in [6, 4, 2]]
msg = _("Isolation score")
rates = st.multiselect(msg, valid_rates, format_func=pc, default=rates)

columns = ["severe", "critical", "cases", "deaths"]
valid_columns = [*columns, "infectious", "recovered", "asymptomatic"]
msg = _("Columns")
columns = st.multiselect(msg,
                         valid_columns,
                         format_func=str.title,
                         default=columns)

ms = [
    base.copy(R0=base.R0 * (1 - rate),
              name=f"Isolation {pc(rate)}").clinical.overflow_model()
    for rate in rates
]