Example #1
0
def render_bar_chart(
    data_barchart: pd.DataFrame,
    yscale: str,
    plot_width: int,
    plot_height: int,
    show_yticks: bool,
) -> Figure:
    """
    Render a bar chart
    """

    colors = [PALETTE[0], PALETTE[2]]
    value_type = ["Not Missing", "Missing"]

    data = {
        "cols": data_barchart.index,
        "Not Missing": data_barchart["not missing"],
        "Missing": data_barchart["missing"],
    }

    if show_yticks:
        if len(data_barchart) > 10:
            plot_width = 28 * len(data_barchart)

    fig = Figure(
        x_range=data_barchart.index.tolist(),
        y_range=[0, 1],
        plot_width=plot_width,
        plot_height=plot_height,
        y_axis_type=yscale,
        toolbar_location=None,
        tooltips="@cols: @$name{1%} $name",
        tools="hover",
    )

    fig.vbar_stack(
        value_type,
        x="cols",
        width=0.9,
        color=colors,
        source=data,
        legend_label=value_type,
    )

    fig.legend.location = "top_right"
    fig.y_range.start = 0
    fig.x_range.range_padding = 0
    fig.yaxis.axis_label = "Total Count"

    tweak_figure(fig)
    relocate_legend(fig, "right")
    return fig
Example #2
0
def gen_conf_cmatrices(plot_config: Dict, v: pd.DataFrame) -> Figure:
    cust_plot_tools = {'x_axis_label': 'Confidence Bucket', **plot_config}
    p = Figure(x_range=(v.index.min(), v.index.max()), **cust_plot_tools)
    stack_fields = ['tp_ratio', 'tn_ratio', 'fp_ratio', 'fn_ratio']
    p.vbar_stack(stackers=stack_fields, x='confidence_bucket', color=dashboard_constants.cust_rg_palette,
                 source=v, width=1.0)
    p = gen_cmatrix_legend(p)
    p.grid.minor_grid_line_color = '#eeeeee'
    p.xaxis.axis_label_text_font_size = "16px"
    p.xaxis.axis_label_text_color = '#003566'
    stack_fields.extend(['max_conf_percentile', 'acc'])
    p = cmatrix_plot_format(p, stack_fields, dashboard_constants.conf_custom_hover_code)
    return p
Example #3
0
def gen_temporal_cmatrices(plot_config: Dict, v: pd.DataFrame) -> Figure:
    cust_plot_tools = {**plot_config}
    v.index = v.index.get_level_values('yweeks').strftime("%b-%d")
    v.index.name = 'yweeks'
    p = Figure(x_range=v.index.get_level_values('yweeks').tolist(), **cust_plot_tools, css_classes=["cmatrix_fig"])
    stack_fields = ['tp_ratio', 'tn_ratio', 'fp_ratio', 'fn_ratio']
    p.vbar_stack(stackers=stack_fields, x='yweeks', color=dashboard_constants.cust_rg_palette, source=v, width=1.0)
    p = gen_cmatrix_legend(p)
    p.grid.minor_grid_line_color = '#eeeeee'
    p.xaxis.major_label_orientation = math.pi / 4
    p.x_range.range_padding = 0
    p.xaxis.major_label_text_font_size = "12px"
    p = cmatrix_plot_format(p, stack_fields, dashboard_constants.temporal_custom_hover_code)
    return p
Example #4
0
def plot_bar_stack(
    p: Figure, source: ColumnDataSource, df: pd.DataFrame
) -> List[GlyphRenderer]:
    # Plot bar stack.
    measurements = list(df["Measurement"].unique())
    n_measurements = len(measurements)
    color = list((Category20_20 * 2)[:n_measurements])
    if "None" in measurements:
        color[measurements.index("None")] = "black"
    bar_stack = p.vbar_stack(
        **BAR_STACK_KWARGS, stackers=measurements, color=color, source=source,
    )
    stack_hover = HoverTool(
        renderers=bar_stack, tooltips=STACK_HOVER_TOOLTIPS,
    )
    p.add_tools(stack_hover)
    # Customize left y-axis range.
    p.y_range.start = 0
    max_count = bar_stack[0].data_source.data["total"].max() + 1
    p.y_range.end = max_count
    # Create a legend.
    kwargs = LEGEND_KWARGS.copy()
    if len(measurements) == 1:
        kwargs["location"] = (0, 250)
    legend = Legend(
        items=[
            (measurement, [bar_stack[i]])
            for i, measurement in enumerate(measurements)
        ],
        **kwargs,
    )
    p.add_layout(legend, "right")
    return bar_stack
Example #5
0
def render_bar_viz(
    itmdt: Intermediate,
    yscale: str,
    plot_width: int,
    plot_height: int,
    show_yticks: bool,
) -> Figure:
    """
    Render a bar chart
    """
    # pylint: disable=too-many-arguments
    length = itmdt["len_data"]
    df = itmdt["data_barchart"]
    df = df.copy()
    df = df.reset_index()
    df.columns = ["col", "values"]
    df["val_per"] = df["values"] * length
    df["missing"] = length - df["val_per"]
    stack_present = df["val_per"].tolist()
    stack_missing = df["missing"].tolist()
    source = ColumnDataSource(df)
    features = source.data["col"].tolist()

    colors = [PALETTE[0], PALETTE[2]]
    value_type = ["present", "missing"]
    data = {
        "features": features,
        "present": stack_present,
        "missing": stack_missing
    }

    if show_yticks:
        if len(df) > 10:
            plot_width = 28 * len(df)

    fig = Figure(
        x_range=features,
        plot_width=plot_width,
        plot_height=plot_height,
        y_axis_type=yscale,
        toolbar_location=None,
        tooltips="$name @features: @$name",
        tools="hover",
    )

    fig.vbar_stack(
        value_type,
        x="features",
        width=0.9,
        color=colors,
        source=data,
        legend_label=value_type,
    )

    fig.legend.location = "top_right"
    fig.y_range.start = 0
    fig.x_range.range_padding = 0.1
    tweak_figure(fig)
    fig.yaxis.axis_label = "Total Count"
    relocate_legend(fig, "right")
    return fig
Example #6
0
def render_bar_chart(
    data: Tuple[np.ndarray, np.ndarray, np.ndarray],
    yscale: str,
    plot_width: int,
    plot_height: int,
) -> Figure:
    """
    Render a bar chart for the missing and present values
    """
    pres_cnts, null_cnts, cols = data
    df = pd.DataFrame({"Present": pres_cnts, "Missing": null_cnts}, index=cols)

    if len(df) > 20:
        plot_width = 28 * len(df)

    fig = Figure(
        x_range=list(df.index),
        y_range=[0, df["Present"][0] + df["Missing"][0]],
        plot_width=plot_width,
        plot_height=plot_height,
        y_axis_type=yscale,
        toolbar_location=None,
        tools=[],
        title=" ",
    )

    rend = fig.vbar_stack(
        stackers=df.columns,
        x="index",
        width=0.9,
        color=[CATEGORY20[0], CATEGORY20[2]],
        source=df,
        legend_label=list(df.columns),
    )

    # hover tool with count and percent
    formatter = CustomJSHover(
        args=dict(source=ColumnDataSource(df)),
        code="""
        const columns = Object.keys(source.data)
        const cur_bar = special_vars.data_x - 0.5
        var ttl_bar = 0
        for (let i = 0; i < columns.length; i++) {
            if (columns[i] != 'index'){
                ttl_bar = ttl_bar + source.data[columns[i]][cur_bar]
            }
        }
        const cur_val = source.data[special_vars.name][cur_bar]
        return (cur_val/ttl_bar * 100).toFixed(2)+'%';
    """,
    )
    for i, val in enumerate(df.columns):
        hover = HoverTool(
            tooltips=[
                ("Column", "@index"),
                (f"{val} count", "@$name"),
                (f"{val} percent", "@{%s}{custom}" % rend[i].name),
            ],
            formatters={"@{%s}" % rend[i].name: formatter},
            renderers=[rend[i]],
        )
        fig.add_tools(hover)

    fig.yaxis.axis_label = "Row Count"
    tweak_figure(fig)
    relocate_legend(fig, "left")
    fig.frame_width = plot_width

    return fig