def plot_metrics_comparison_lineplot_grid(dataframe,
                                          models_labels,
                                          metrics_labels,
                                          figure_size=(14, 4)):
    """
    We define a function to plot the grid.
    """

    return (
        # Define the plot.
        p9.ggplot(
            dataframe,
            p9.aes(x='threshold',
                   y='value',
                   group='variable',
                   color='variable',
                   shape='variable'))
        # Add the points and lines.
        + p9.geom_point() + p9.geom_line()
        # Rename the x axis and give some space to left and right.
        + p9.scale_x_discrete(name='Threshold', expand=(0, 0.2))
        # Rename the y axis, give some space on top and bottom, and print the tick labels with 2 decimal digits.
        +
        p9.scale_y_continuous(name='Value',
                              expand=(0, 0.05),
                              labels=lambda l: ['{:.2f}'.format(x) for x in l])
        # Replace the names in the legend.
        + p9.scale_shape_discrete(
            name='Metric', labels=lambda l: [metrics_labels[x] for x in l])
        # Define the colors for the metrics for color-blind people.
        +
        p9.scale_color_brewer(name='Metric',
                              labels=lambda l: [metrics_labels[x] for x in l],
                              type='qual',
                              palette='Set2')
        # Place the plots in a grid, renaming the labels for rows and columns.
        + p9.facet_grid('iterations ~ model',
                        labeller=p9.labeller(
                            rows=lambda x: f'iters = {x}',
                            cols=lambda x: f'{models_labels[x]}'))
        # Define the theme for the plot.
        + p9.theme(
            # Remove the y axis name.
            axis_title_y=p9.element_blank(),
            # Set the size of x and y tick labels font.
            axis_text_x=p9.element_text(size=7),
            axis_text_y=p9.element_text(size=7),
            # Place the legend on top, without title, and reduce the margin.
            legend_title=p9.element_blank(),
            legend_position='top',
            legend_box_margin=2,
            # Set the size for the figure.
            figure_size=figure_size,
        ))
Пример #2
0
def plot_fees(fees, title, y_axis, years, filename):
    p = pn.ggplot(fees, pn.aes('year', y_axis, color = 'conference', shape = 'conference')) + \
        pn.geom_point() + \
        pn.geom_line() + \
        pn.labs(title = title, x = 'Year', y = 'Fee (€)') + \
        pn.ylim(0, 1000) + \
        pn.theme_light() + \
        pn.scale_x_continuous(breaks = years) + \
        pn.scale_colour_discrete(name = 'Conference') + \
        pn.scale_shape_discrete(name = 'Conference')

    p.save(filename, width=6, height=3, dpi=300)
def test_annotation_stripes_double():
    pdf = mtcars.assign(gear=pd.Categorical(mtcars.gear),
                        am=pd.Categorical(mtcars.am))
    p = (
        ggplot(pdf) + annotation_stripes(
            fills=["#0000FF", "#FF0000"], alpha=0.3, direction='vertical') +
        annotation_stripes(
            fills=["#AAAAAA", "#FFFFFF"], alpha=0.3, direction='horizontal') +
        geom_jitter(aes("gear", "wt", shape="gear", color="am"),
                    random_state=5) +
        scale_shape_discrete(guide=guide_legend(order=1))  # work around #229
    )
    assert p == "annotation_stripes_double"
def test_annotation_stripes_coord_flip():
    pdf = mtcars.assign(gear=pd.Categorical(mtcars.gear),
                        am=pd.Categorical(mtcars.am))
    p = (
        ggplot(pdf) + annotation_stripes(
            fills=["#AAAAAA", "#FFFFFF", "#7F7FFF"], alpha=0.3) + geom_jitter(
                aes("gear", "wt", shape="gear", color="am"), random_state=5) +
        geom_vline(xintercept=0.5, color="black") +
        geom_vline(xintercept=1.5, color="black") +
        geom_vline(xintercept=2.5, color="black") +
        geom_vline(xintercept=3.5, color="black") +
        scale_shape_discrete(guide=guide_legend(order=1))  # work around #229
        + coord_flip())
    assert p == "annotation_stripes_coord_flip"
Пример #5
0
def baseline_error():
    al_baselines_len = len(jsonFile['x'])
    df = pd.DataFrame(jsonFile)

    method_type = CategoricalDtype(
        categories=['Baseline', 'PGD', 'Provable', 'Mix'], ordered=True)
    df['method'] = df['method'].astype(method_type)

    p = (
        ggplot(df) + aes(x='x', y='y', shape='method', color='method') +
        geom_point(size=4, stroke=0) + geom_line(size=1) +
        scale_shape_discrete(name='Method') +
        scale_color_brewer(type='qual', palette=2, name='Method') +
        xlab('Training Time (seconds)') + ylab('Adversarial Error') + theme(
            # manually position legend, not used anymore
            # legend_position=(0.70, 0.65),
            # legend_background=element_rect(fill=(0, 0, 0, 0)),
            aspect_ratio=0.8, ) + ggtitle("Baseline Error"))
    fig_dir = '.'
    p.save(os.path.join(fig_dir, 'baselineErr.pdf'))
Пример #6
0
df = df.append(
    pd.DataFrame(np.transpose([
        np.repeat("Provable", robust_arr2.shape[0]), robust_arr2[:, 0],
        robust_arr2[:, 1]
    ]),
                 columns=["method", "x", "y"]))
df = df.append(
    pd.DataFrame(np.transpose(
        [np.repeat("Mix", mix_arr2.shape[0]), mix_arr2[:, 0], mix_arr2[:, 1]]),
                 columns=["method", "x", "y"]))
df['x'] = pd.to_numeric((df['x']))
df['y'] = pd.to_numeric((df['y']))
p = (
    ggplot(df) + aes(x='x', y='y', shape='method', color='method') +
    # + geom_point(size=4, stroke=0)
    geom_line(size=1) + scale_shape_discrete(name='Method') +
    scale_color_brewer(type='qual', palette=2, name='Method') +
    xlab('Training Time (seconds)') + ylab('Error') +
    theme(aspect_ratio=0.8, ) + ggtitle("Baseline Error"))
p.save(test + "/baselineErr.png", verbose=False)

df = pd.DataFrame([], columns=["method", "x", "y"])
df = df.append(
    pd.DataFrame(np.transpose([
        np.repeat("Baseline", baseline_arr2.shape[0]), baseline_arr2[:, 0],
        baseline_arr2[:, 2]
    ]),
                 columns=["method", "x", "y"]))
df = df.append(
    pd.DataFrame(np.transpose([
        np.repeat("Madry", madry_arr2.shape[0]), madry_arr2[:, 0],
Пример #7
0
df = df.append(pd.DataFrame(
    np.transpose([np.repeat("Provable", robust_arr2.shape[0]),
                  robust_arr2[:, 0],
                  robust_arr2[:, 1]]), columns=["method", "x", "y"]))
df = df.append(pd.DataFrame(
    np.transpose([np.repeat("Mix", mix_arr2.shape[0]),
                  mix_arr2[:, 0],
                  mix_arr2[:, 1]]), columns=["method", "x", "y"]))
df['x'] = pd.to_numeric((df['x']))
df['y'] = pd.to_numeric((df['y']))
p = (
    ggplot(df) +
    aes(x='x', y='y', shape='method', color='method') +
    # + geom_point(size=4, stroke=0)
    geom_line(size=1) +
    scale_shape_discrete(name='Method') +
    scale_color_brewer(
        type='qual',
        palette=2,
        name='Method') +
    xlab('Training Time (seconds)') +
    ylab('Error') +
    theme(
        aspect_ratio=0.8,
    ) +
    ggtitle("Baseline Error")
)
p.save(test + "/baselineErr.png", verbose=False)


df = pd.DataFrame([], columns=["method", "x", "y"])