Exemplo n.º 1
0
def test_aesthetics():
    p = (ggplot(df) + geom_point(aes('x', 'y')) +
         geom_hline(aes(yintercept='yintercept'), size=2) +
         geom_hline(aes(yintercept='yintercept+.1', alpha='z'), size=2) +
         geom_hline(aes(yintercept='yintercept+.2', linetype='factor(z)'),
                    size=2) +
         geom_hline(aes(yintercept='yintercept+.3', color='factor(z)'), size=2)
         + geom_hline(aes(yintercept='yintercept+.4', size='z')))

    assert p + _theme == 'aesthetics'
Exemplo n.º 2
0
def test_aesthetics():
    p = (ggplot(df) +
         geom_point(aes('x', 'y')) +
         geom_hline(aes(yintercept='yintercept'), size=2) +
         geom_hline(aes(yintercept='yintercept+.1', alpha='z'),
                    size=2) +
         geom_hline(aes(yintercept='yintercept+.2',
                        linetype='factor(z)'),
                    size=2) +
         geom_hline(aes(yintercept='yintercept+.3',
                        color='factor(z)'),
                    size=2) +
         geom_hline(aes(yintercept='yintercept+.4', size='z')))

    assert p + _theme == 'aesthetics'
Exemplo n.º 3
0
def plot_pointplot(plot_df, y_axis_label="", use_log10=False, limits=[0, 3.2]):
    """
    Plots the pointplot
    Arguments:
        plot_df - the dataframe that contains the odds ratio and lemmas
        y_axis_label - the label for the y axis
        use_log10 - use log10 for the y axis?
    """
    graph = (
        p9.ggplot(plot_df, p9.aes(x="lemma", y="odds_ratio")) +
        p9.geom_pointrange(p9.aes(ymin="lower_odds", ymax="upper_odds"),
                           position=p9.position_dodge(width=1),
                           size=0.3,
                           color="#253494") +
        p9.scale_x_discrete(limits=(plot_df.sort_values(
            "odds_ratio", ascending=True).lemma.tolist())) +
        (p9.scale_y_log10() if use_log10 else p9.scale_y_continuous(
            limits=limits)) +
        p9.geom_hline(p9.aes(yintercept=1), linetype='--', color='grey') +
        p9.coord_flip() + p9.theme_seaborn(
            context='paper', style="ticks", font_scale=1, font='Arial') +
        p9.theme(
            # 640 x 480
            figure_size=(6.66, 5),
            panel_grid_minor=p9.element_blank(),
            axis_title=p9.element_text(size=12),
            axis_text_x=p9.element_text(size=10)) +
        p9.labs(x=None, y=y_axis_label))
    return graph
Exemplo n.º 4
0
def plot_individual_returns(
        df_in: pd.DataFrame,
        max_episode: int,
        return_column: str = 'episode_return',
        colour_var: Optional[str] = None,
        yintercept: Optional[float] = None,
        sweep_vars: Optional[Sequence[str]] = None) -> gg.ggplot:
    """Plot individual learning curves: one curve per sweep setting."""
    df = df_in.copy()
    df['unique_group'] = _make_unique_group_col(df, sweep_vars)
    p = (gg.ggplot(df) +
         gg.aes(x='episode', y=return_column, group='unique_group') +
         gg.coord_cartesian(xlim=(0, max_episode)))
    if colour_var:
        p += gg.geom_line(gg.aes(colour=colour_var), size=1.1, alpha=0.75)
        if len(df[colour_var].unique()) <= 5:
            df[colour_var] = df[colour_var].astype('category')
            p += gg.scale_colour_manual(values=FIVE_COLOURS)
    else:
        p += gg.geom_line(size=1.1, alpha=0.75, colour='#313695')
    if yintercept:
        p += gg.geom_hline(yintercept=yintercept,
                           alpha=0.5,
                           size=2,
                           linetype='dashed')
    return facet_sweep_plot(p, sweep_vars, tall_plot=True)
Exemplo n.º 5
0
def bsuite_bar_plot(df_in: pd.DataFrame,
                    sweep_vars: Sequence[str] = None) -> gg.ggplot:
  """Output bar plot of bsuite data."""
  df = _clean_bar_plot_data(df_in, sweep_vars)

  p = (gg.ggplot(df)
       + gg.aes(x='env', y='score', colour='type', fill='type')
       + gg.geom_bar(position='dodge', stat='identity')
       + gg.geom_hline(yintercept=1., linetype='dashed', alpha=0.5)
       + gg.scale_colour_manual(plotting.CATEGORICAL_COLOURS)
       + gg.scale_fill_manual(plotting.CATEGORICAL_COLOURS)
       + gg.xlab('experiment')
       + gg.theme(axis_text_x=gg.element_text(angle=25, hjust=1))
      )
  if not all(df.finished):  # add a layer of alpha for unfinished jobs
    p += gg.aes(alpha='finished')
    p += gg.scale_alpha_discrete(range=[0.3, 1.0])

  # Compute the necessary size of the plot
  if sweep_vars:
    p += gg.facet_wrap(sweep_vars, labeller='label_both', ncol=1)
    n_hypers = df[sweep_vars].drop_duplicates().shape[0]
  else:
    n_hypers = 1
  return p + gg.theme(figure_size=(14, 3 * n_hypers + 1))
Exemplo n.º 6
0
def plot_regret(df_in: pd.DataFrame,
                sweep_vars: Sequence[Text] = None) -> gg.ggplot:
    """Plot average regret of deep_sea through time by size."""
    df = df_in.copy()
    df = df[df['size'].isin([10, 20, 30, 40, 50])]
    df['avg_bad'] = df.total_bad_episodes / df.episode
    df['size'] = df['size'].astype('category')
    p = (
        gg.ggplot(df[df.episode <= NUM_EPISODES]) +
        gg.aes('episode', 'avg_bad', group='size', colour='size') +
        gg.geom_line(size=2, alpha=0.75) + gg.geom_hline(
            gg.aes(yintercept=0.99), linetype='dashed', alpha=0.4, size=1.75) +
        gg.geom_hline(gg.aes(yintercept=0.0), alpha=0)  # axis hack
        + gg.ylab('average bad episodes') +
        gg.scale_colour_manual(values=plotting.FIVE_COLOURS))
    return plotting.facet_sweep_plot(p, sweep_vars)
Exemplo n.º 7
0
def scatter_plot(df,
                 xcol,
                 ycol,
                 domain,
                 xname=None,
                 yname=None,
                 log=False,
                 width=6,
                 height=6,
                 clamp=True,
                 tickCount=5):
    assert len(domain) == 2

    POINT_SIZE = 0.5
    DASH_PATTERN = (0, (3, 1))

    if xname == None:
        xname = xcol
    if yname == None:
        yname = ycol

    # formater for axes' labels
    ax_formatter = mizani.custom_format('{:n}')

    if clamp:  # clamp overflowing values if required
        df = df.copy(deep=True)
        df.loc[df[xcol] > domain[1], xcol] = domain[1]
        df.loc[df[ycol] > domain[1], ycol] = domain[1]

    # generate scatter plot
    scatter = p9.ggplot(df)
    scatter += p9.aes(x=xcol, y=ycol)
    scatter += p9.geom_point(size=POINT_SIZE, na_rm=True)
    scatter += p9.labs(x=xname, y=yname)

    if log:  # log scale
        scatter += p9.scale_x_log10(limits=domain, labels=ax_formatter)
        scatter += p9.scale_y_log10(limits=domain, labels=ax_formatter)
    else:
        scatter += p9.scale_x_continuous(limits=domain, labels=ax_formatter)
        scatter += p9.scale_y_continuous(limits=domain, labels=ax_formatter)

    #scatter += p9.theme_xkcd()
    scatter += p9.theme_bw()
    scatter += p9.theme(
        panel_grid_major=p9.element_line(color='#666666', alpha=0.5))
    scatter += p9.theme(figure_size=(width, height))

    # generate additional lines
    scatter += p9.geom_abline(intercept=0, slope=1,
                              linetype=DASH_PATTERN)  # diagonal
    scatter += p9.geom_vline(xintercept=domain[1],
                             linetype=DASH_PATTERN)  # vertical rule
    scatter += p9.geom_hline(yintercept=domain[1],
                             linetype=DASH_PATTERN)  # horizontal rule

    res = scatter

    return res
Exemplo n.º 8
0
def plot_learning(df: pd.DataFrame,
                  sweep_vars: Optional[Sequence[str]] = None) -> gg.ggplot:
  """Simple learning curves for catch."""
  p = plotting.plot_regret_learning(
      df, sweep_vars=sweep_vars, max_episode=sweep.NUM_EPISODES)
  p += gg.geom_hline(
      gg.aes(yintercept=BASE_REGRET), linetype='dashed', alpha=0.4, size=1.75)
  return p
def test_annotation_stripes_horizontal_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,
                           direction='horizontal') +
        geom_jitter(aes("wt", "gear", shape="gear", color="am"),
                    random_state=5) +
        geom_hline(yintercept=0.5, color="black") +
        geom_hline(yintercept=1.5, color="black") +
        geom_hline(yintercept=2.5, color="black") +
        geom_hline(yintercept=3.5, color="black") +
        scale_shape_discrete(guide=guide_legend(order=1))  # work around #229
        + coord_flip())
    assert p == "annotation_stripes_horizontal_coord_flip"
Exemplo n.º 10
0
def plot_learning(df: pd.DataFrame,
                  sweep_vars: Sequence[Text] = None) -> gg.ggplot:
  """Plots the average regret through time."""
  p = plotting.plot_regret_learning(
      df, sweep_vars=sweep_vars, max_episode=sweep.NUM_EPISODES)
  p += gg.geom_hline(gg.aes(yintercept=BASE_REGRET),
                     linetype='dashed', alpha=0.4, size=1.75)
  return p
Exemplo n.º 11
0
def plot_ci_eval(df):
    molten = pd.melt(df,
                     id_vars=['sample_size'],
                     value_vars=['bootstrap', 'ztest', 'ttest'])

    return (ggplot(molten, aes(x='sample_size', y='value', color='variable')) +
            geom_line() + scale_x_log10() + ylim(0, 1) +
            pn.geom_hline(yintercept=0.95, linetype='dotted'))
Exemplo n.º 12
0
def plot_learning(df: pd.DataFrame,
                  sweep_vars: Sequence[str] = None) -> gg.ggplot:
  """Simple learning curves for cartpole."""
  df = cartpole_preprocess(df)
  p = plotting.plot_regret_learning(
      df, sweep_vars=sweep_vars, max_episode=NUM_EPISODES)
  p += gg.geom_hline(gg.aes(yintercept=BASE_REGRET),
                     linetype='dashed', alpha=0.4, size=1.75)
  return p
def scatter_plot2(df1, df2, xcol, ycol, domain, color1='black', color2='red', xname=None, yname=None, log=False, width=6, height=6, clamp=True, tickCount=5):
    assert len(domain) == 2

    POINT_SIZE = 1.5
    DASH_PATTERN = (0, (6, 2))

    if xname is None:
        xname = xcol
    if yname is None:
        yname = ycol

    # formatter for axes' labels
    ax_formatter = mizani.custom_format('{:n}')

    if clamp:  # clamp overflowing values if required
        df1 = df1.copy(deep=True)
        df1.loc[df1[xcol] > domain[1], xcol] = domain[1]
        df1.loc[df1[ycol] > domain[1], ycol] = domain[1]

        df2 = df2.copy(deep=True)
        df2.loc[df2[xcol] > domain[1], xcol] = domain[1]
        df2.loc[df2[ycol] > domain[1], ycol] = domain[1]

    # generate scatter plot
    scatter = p9.ggplot(df1)
    scatter += p9.aes(x=xcol, y=ycol)
    scatter += p9.geom_point(size=POINT_SIZE, na_rm=True, color=color1, alpha=0.5)
    scatter += p9.geom_point(size=POINT_SIZE, na_rm=True, data=df2, color=color2, alpha=0.5)
    scatter += p9.labs(x=xname, y=yname)

    # rug plots
    scatter += p9.geom_rug(na_rm=True, sides="tr", color=color1, alpha=0.05)
    scatter += p9.geom_rug(na_rm=True, sides="tr", data=df2, color=color2, alpha=0.05)

    if log:  # log scale
        scatter += p9.scale_x_log10(limits=domain, labels=ax_formatter)
        scatter += p9.scale_y_log10(limits=domain, labels=ax_formatter)
    else:
        scatter += p9.scale_x_continuous(limits=domain, labels=ax_formatter)
        scatter += p9.scale_y_continuous(limits=domain, labels=ax_formatter)

    # scatter += p9.theme_xkcd()
    scatter += p9.theme_bw()
    scatter += p9.theme(panel_grid_major=p9.element_line(color='#666666', alpha=0.5))
    scatter += p9.theme(panel_grid_minor=p9.element_blank())
    scatter += p9.theme(figure_size=(width, height))
    scatter += p9.theme(text=p9.element_text(size=24, color="black"))

    # generate additional lines
    scatter += p9.geom_abline(intercept=0, slope=1, linetype=DASH_PATTERN)  # diagonal
    scatter += p9.geom_vline(xintercept=domain[1], linetype=DASH_PATTERN)  # vertical rule
    scatter += p9.geom_hline(yintercept=domain[1], linetype=DASH_PATTERN)  # horizontal rule

    res = scatter

    return res
Exemplo n.º 14
0
def plot_learning(df: pd.DataFrame,
                  sweep_vars: Optional[Sequence[str]] = None,
                  group_col: str = 'noise_scale') -> gg.ggplot:
  """Plots the average regret through time."""
  p = plotting.plot_regret_learning(
      df_in=df, group_col=group_col, sweep_vars=sweep_vars,
      max_episode=sweep.NUM_EPISODES)
  p += gg.geom_hline(gg.aes(yintercept=catch_analysis.BASE_REGRET),
                     linetype='dashed', alpha=0.4, size=1.75)
  return p
Exemplo n.º 15
0
def bandit_learning_format(plot: gg.ggplot) -> gg.ggplot:
    """Add nice bandit formatting to ggplot."""
    plot += gg.scale_y_continuous(breaks=np.arange(0, 1.1, 0.1).tolist())
    plot += gg.theme(panel_grid_major_y=gg.element_line(size=2.5),
                     panel_grid_minor_y=gg.element_line(size=0))
    plot += gg.geom_hline(gg.aes(yintercept=BASE_REGRET),
                          linetype='dashed',
                          alpha=0.4,
                          size=1.75)
    plot += gg.coord_cartesian(ylim=(0, 1))
    return plot
Exemplo n.º 16
0
def plot_elos():
    diffs = np.linspace(-1000, +1000)
    rates = 1 / (1 + 10**(-diffs / 400))
    df = pd.DataFrame({'elo': diffs, 'winrate': rates})

    return (pn.ggplot(df) + pn.geom_line(pn.aes(x='elo', y='winrate')) +
            pn.geom_vline(xintercept=0, alpha=.1) +
            pn.geom_hline(yintercept=.5, alpha=.1) +
            pn.labs(x='Own Elo relative to opponent\'s Elo',
                    y='Win rate v. opponent') +
            pn.scale_y_continuous(labels=percent_format()) +
            pn.coord_cartesian(expand=False) + plot.IEEE())
Exemplo n.º 17
0
def plot_average(df: pd.DataFrame,
                 sweep_vars: Sequence[Text] = None,
                 group_col: Text = 'noise_scale') -> gg.ggplot:
    """Plots the average regret through time by noise_scale."""
    p = plotting.plot_regret_average(df_in=df,
                                     group_col=group_col,
                                     episode=sweep.NUM_EPISODES,
                                     sweep_vars=sweep_vars)
    p += gg.geom_hline(gg.aes(yintercept=catch_analysis.BASE_REGRET),
                       linetype='dashed',
                       alpha=0.4,
                       size=1.75)
    return p
Exemplo n.º 18
0
def plot_average(df: pd.DataFrame,
                 sweep_vars: Sequence[str] = None) -> gg.ggplot:
  """Plots the average regret at 1k episodes by optimal_horizon."""
  df = dc_preprocess(df_in=df)
  p = plotting.plot_regret_average(
      df_in=df,
      group_col='optimal_horizon',
      episode=sweep.NUM_EPISODES,
      sweep_vars=sweep_vars
  )
  p += gg.geom_hline(gg.aes(yintercept=BASE_REGRET),
                     linetype='dashed', alpha=0.4, size=1.75)
  return p
Exemplo n.º 19
0
def _bar_plot_compare(df: pd.DataFrame) -> gg.ggplot:
    """Bar plot of buite score data, comparing agents on each experiment."""
    p = (gg.ggplot(df) +
         gg.aes(x='agent', y='score', colour='agent', fill='agent') +
         gg.geom_bar(position='dodge', stat='identity') +
         gg.geom_hline(yintercept=1., linetype='dashed', alpha=0.5) +
         gg.theme(axis_text_x=gg.element_text(angle=25, hjust=1)) +
         gg.scale_colour_manual(plotting.CATEGORICAL_COLOURS) +
         gg.scale_fill_manual(plotting.CATEGORICAL_COLOURS))
    if not all(df.finished):  # add a layer of alpha for unfinished jobs
        p += gg.aes(alpha='finished')
        p += gg.scale_alpha_discrete(range=[0.3, 1.0])
    return p
Exemplo n.º 20
0
def plot_average(df: pd.DataFrame,
                 sweep_vars: Sequence[str] = None,
                 group_col: str = 'delay') -> gg.ggplot:
    """Plots the average regret through time by delay."""
    df = mdpp_preprocess_delay(df)
    p = plotting.plot_regret_average(df_in=df,
                                     group_col=group_col,
                                     episode=sweep.NUM_EPISODES,
                                     sweep_vars=sweep_vars)
    p += gg.geom_hline(gg.aes(yintercept=BASE_REGRET),
                       linetype='dashed',
                       alpha=0.4,
                       size=1.75)
    return p
Exemplo n.º 21
0
def plot_learning(df: pd.DataFrame,
                  sweep_vars: Sequence[str] = None) -> gg.ggplot:
  """Plots the average regret through time by optimal_horizon."""
  df = dc_preprocess(df_in=df)
  p = plotting.plot_regret_learning(
      df_in=df,
      group_col='optimal_horizon',
      sweep_vars=sweep_vars,
      max_episode=sweep.NUM_EPISODES
  )
  p += gg.geom_hline(gg.aes(yintercept=BASE_REGRET),
                     linetype='dashed', alpha=0.4, size=1.75)
  p += gg.coord_cartesian(ylim=(0, 0.1))
  return p
Exemplo n.º 22
0
def plot_average(df: pd.DataFrame,
                 sweep_vars: Optional[Sequence[str]] = None,
                 group_col: str = 'noise_scale') -> gg.ggplot:
    """Plots the average regret through time by noise_scale."""
    df = cartpole_analysis.cartpole_preprocess(df)
    p = plotting.plot_regret_average(df_in=df,
                                     group_col=group_col,
                                     episode=sweep.NUM_EPISODES,
                                     sweep_vars=sweep_vars)
    p += gg.geom_hline(gg.aes(yintercept=cartpole_analysis.BASE_REGRET),
                       linetype='dashed',
                       alpha=0.4,
                       size=1.75)
    return p
Exemplo n.º 23
0
def plot_learning(df: pd.DataFrame,
                  sweep_vars: Sequence[Text] = None,
                  group_col: Text = 'noise_scale') -> gg.ggplot:
    """Plots the average regret through time."""
    df = mountain_car_analysis.mountain_car_preprocess(df)
    p = plotting.plot_regret_learning(df_in=df,
                                      group_col=group_col,
                                      sweep_vars=sweep_vars,
                                      max_episode=sweep.NUM_EPISODES)
    p += gg.geom_hline(gg.aes(yintercept=mountain_car_analysis.BASE_REGRET),
                       linetype='dashed',
                       alpha=0.4,
                       size=1.75)
    return p
Exemplo n.º 24
0
def plot_acf(data_in, figure_size=(15, 5)):
    """
    Plots the autocorrelation function

    Parameteres
    -----------
    data_in : pd.DataFrame
        Dataframe containing the autcorrelation of our mcmc samples
    figure_size : tuple, default = (15,5)
        Optional input for figure size

    Returns
    -------
    pn.ggplot:
        Plotnine ggplot object containing autocorrelation plot
    """
    pn.options.figure_size = figure_size
    plot_out = pn.ggplot(pn.aes(x = 'lag', y = 'autocorrelation'), data = data_in)\
        + pn.geom_hline(pn.aes(yintercept= 0))\
        + pn.geom_hline(pn.aes(yintercept= 0.05), color = 'red', linetype = 'dashed')\
        + pn.geom_hline(pn.aes(yintercept= -0.05), color = 'red', linetype = 'dashed')\
        + pn.geom_col()
    return (plot_out)
Exemplo n.º 25
0
def plot_learning(df: pd.DataFrame,
                  sweep_vars: Sequence[str] = None,
                  group_col: str = 'reward_density') -> gg.ggplot:
    """Plots the average regret through time."""
    df = mdp_playground_analysis.mdpp_preprocess(df)
    p = plotting.plot_regret_learning(df_in=df,
                                      group_col=group_col,
                                      sweep_vars=sweep_vars,
                                      max_episode=sweep.NUM_EPISODES)
    p += gg.geom_hline(gg.aes(yintercept=BASE_REGRET),
                       linetype='dashed',
                       alpha=0.4,
                       size=1.75)
    return p
Exemplo n.º 26
0
def add_hline(plot):
    """This function adds a horizontal line at the y-value 6

    Inputs
    ------
    plot: plotnine.ggplot
        The scatter plot to add the line to

    Return
    ------
    plot: plotnine.ggplot
        The scatter plot with the line addded
    """
    plot = plot + geom_hline(yintercept=6, color='red')
    return plot
Exemplo n.º 27
0
def plot_calibrations():
    params = data.sample_calibrations()
    return (
        pn.ggplot(
            params,
            pn.aes(xmin='boardsize-.25',
                   xmax='boardsize+.25',
                   group='boardsize',
                   fill='factor(boardsize)')) +
        pn.geom_hline(yintercept=.5, alpha=.2) + pn.geom_rect(
            pn.aes(ymin='lower', ymax='upper'), show_legend=False, color='k') +
        pn.geom_rect(pn.aes(ymin='mid', ymax='mid'),
                     show_legend=False,
                     color='k',
                     size=2) + pn.scale_y_continuous(labels=percent_format()) +
        pn.scale_fill_hue(l=.4) + pn.coord_cartesian(ylim=(.4, .6)) +
        pn.labs(y='Win rate v. perfect play', x='Board size') + plot.IEEE())
Exemplo n.º 28
0
def plot_regret_learning(df_in: pd.DataFrame,
                         group_col: Optional[str] = None,
                         sweep_vars: Optional[Sequence[str]] = None,
                         regret_col: str = 'total_regret',
                         max_episode: Optional[int] = None) -> gg.ggplot:
    """Plots the average regret through time, grouped by group_var."""
    df = df_in.copy()
    df['average_regret'] = df[regret_col] / df.episode
    df = df[df.episode <= (max_episode or np.inf)]
    if group_col is None:
        p = _plot_regret_single(df)
    else:
        p = _plot_regret_group(df, group_col)
    p += gg.geom_hline(gg.aes(yintercept=0.0), alpha=0)  # axis hack
    p += gg.ylab('average regret per timestep')
    p += gg.coord_cartesian(xlim=(0, max_episode))
    return facet_sweep_plot(p, sweep_vars, tall_plot=True)
Exemplo n.º 29
0
def manhattan_plot(df, limit=20000):
    return (
        pn.ggplot(
            df
                .sort_values('P')
                .reset_index(drop=True)
                .head(limit)
                .assign(LOGP=lambda df: -np.log10(df['P']))
                .assign(CHR=lambda df: df['CHR'].astype(str))
            ,
            pn.aes(x='POS', y='LOGP', fill='CHR', color='CHR')
        ) + 
        pn.geom_point() + 
        pn.geom_hline(yintercept=5) + 
        pn.theme_bw() + 
        pn.theme(figure_size=(16, 4))
    )
Exemplo n.º 30
0
def plot_average(df: pd.DataFrame,
                 sweep_vars: Sequence[Text] = None,
                 group_col: Text = 'noise_scale') -> gg.ggplot:
  """Plots the average regret through time by noise_scale."""
  p = plotting.plot_regret_average(
      df_in=df,
      group_col=group_col,
      episode=sweep.NUM_EPISODES,
      sweep_vars=sweep_vars
  )
  p += gg.scale_y_continuous(breaks=np.arange(0, 1.1, 0.1).tolist())
  p += gg.theme(panel_grid_major_y=gg.element_line(size=2.5),
                panel_grid_minor_y=gg.element_line(size=0),)
  p += gg.geom_hline(gg.aes(yintercept=bandit_analysis.BASE_REGRET),
                     linetype='dashed', alpha=0.4, size=1.75)
  p += gg.coord_cartesian(ylim=(0, 1))
  return p
Exemplo n.º 31
0
def _make_plots(df_plt, out_file_base, y='AUC', facet_grid='', h_line=''):
    len_x = len(np.unique(df_plt['resolution']))
    if 'sparsity_l1' in df_plt.columns:
        df_plt['Sparsity'] = df_plt['sparsity_l1']
        len_x2 = len(np.unique(df_plt['Sparsity']))
    else:
        len_x2 = 0
    if len_x2 > 1:
        gplt = plt9.ggplot(df_plt,
                           plt9.aes(
                               fill='Sparsity',
                               x='resolution',
                               y=y,
                           ))
        gplt = gplt + plt9.geom_boxplot(alpha=0.8, outlier_alpha=0)
        gplt = gplt + plt9.geom_jitter(
            plt9.aes(color='Sparsity'), alpha=0.25, width=0.2)
    else:
        gplt = plt9.ggplot(df_plt, plt9.aes(x='resolution', y=y))
        gplt = gplt + plt9.geom_boxplot(alpha=0.8, outlier_alpha=0)
        gplt = gplt + plt9.geom_jitter(alpha=0.25, width=0.2)
    gplt = gplt + plt9.theme_bw(base_size=12)
    if facet_grid != '':
        gplt = gplt + plt9.facet_grid('{} ~ .'.format(facet_grid))
    if y == 'f1-score':
        gplt = gplt + plt9.labs(x='Resolution', y='F1 score', title='')
    elif y in ['AUC', 'MCC']:
        gplt = gplt + plt9.labs(x='Resolution', y=y, title='')
    else:
        gplt = gplt + plt9.labs(
            x='Resolution', y=y.capitalize().replace('_', ' '), title='')
    gplt = gplt + plt9.theme(
        # legend_position='none',
        axis_text_x=plt9.element_text(angle=-45, hjust=0))
    if len_x2 != 0 and len_x2 < 9:
        gplt = gplt + plt9.scale_fill_brewer(palette='Dark2', type='qual')
    if h_line != '':
        gplt = gplt + plt9.geom_hline(plt9.aes(yintercept=h_line),
                                      linetype='dashdot')
    gplt.save('{}-resolution__{}.png'.format(out_file_base,
                                             y.replace('-', '_')),
              dpi=300,
              width=4 * ((len_x + len_x2) / 4),
              height=5,
              limitsize=False)
Exemplo n.º 32
0
def test_aes_inheritance():
    with pytest.raises(PlotnineError):
        p = (ggplot(df, aes('x', 'y', yintercept='yintercept')) +
             geom_point() +
             geom_hline(size=2))
        p.draw_test()
Exemplo n.º 33
0
def main():
    mpl.rc('mathtext', fontset='cm')

    warnings.filterwarnings('ignore',
                            r'(geom|position)_\w+ ?: Removed \d+ rows')
    warnings.filterwarnings('ignore', r'Saving .+ x .+ in image')
    warnings.filterwarnings('ignore', r'Filename: .+\.png')

    df = concat_map(Pf_Ob_Ol, 'P_f', np.linspace(0.1, 1, 10))
    save_both(my_plot(df, 'O_b', 'O_l', 'P_f')
              + titles('P_f(O_b, O_l)')
              + limits((1, 10))
              + gg.geom_abline(slope=1, intercept=0,
                               linetype='dashed', color='grey')
              + gg.geom_line()
              , 'Pf_Ob_Ol')

    df = concat_map(Pf_Ob_σ, 'P_f', np.linspace(0.1, 1, 10))
    save_both(my_plot(df, 'O_b', 'σ', 'P_f')
              + titles('P_f(O_b, σ)')
              + limits((1, 10), (0, 5))
              + gg.geom_line()
              , 'Pf_Ob_σ')

    df = concat_map(Pq_Ob_Ol, 'P_q', np.linspace(-0.9, 0, 10))
    save_both(my_plot(df, 'O_b', 'O_l', 'P_q')
              + titles('P_q(O_b, O_l)')
              + limits((1, 10))
              + gg.geom_abline(slope=1, intercept=0,
                               linetype='dashed', color='grey')
              + gg.geom_line()
              , 'Pq_Ob_Ol')

    df = concat_map(Pq_Ob_σ, 'P_q', np.linspace(-0.9, 0, 10))
    save_both(my_plot(df, 'O_b', 'σ', 'P_q')
              + titles('P_q(O_b, σ)')
              + limits((1, 10), (0, 5))
              + gg.geom_line()
              , 'Pq_Ob_σ')

    df = concat_map(Opr_Ob_Ol, 'Opr', np.linspace(1, 5, 9))
    save_both(my_plot(df, 'O_b', 'O_l', 'Opr')
              + titles("O'(O_b, O_l)")
              + limits((1, 10), (1, 10))
              + gg.geom_line()
              + gg.geom_abline(slope=1, intercept=0,
                               linetype='dashed', color='grey')
              , 'Opr_Ob_Ol')

    df = concat_map(Opr_Ob_σ, 'Opr', np.linspace(1, 5, 9))
    save_both(my_plot(df, 'O_b', 'σ', 'Opr')
              + titles("O'(O_b, σ)")
              + limits((1, 10), (0, 5))
              + gg.geom_line()
              , 'Opr_Ob_σ')

    df = (pd.DataFrame({'Opr': np.linspace(1, 21, 101)})
            .assign(Pf=lambda x: Opr_Pf(x.Opr)))
    save_both(my_plot(df, 'Opr', 'Pf')
              + titles("P_f(O')")
              + labs("O'", 'P_f')
              + limits((1, 20), (0, 1),
                       xbreaks=np.linspace(2, 20, 10),
                       ybreaks=np.linspace(0, 1, 11))
              + gg.geom_line()
              + gg.geom_hline(yintercept=C, linetype='dashed', color='grey')
              , 'Pf_Opr')

    df = concat_map(σpr_Ob_σ, 'σpr', np.linspace(0, 5, 11))
    save_both(my_plot(df, 'O_b', 'σ', 'σpr')
              + titles("σ'(O_b, σ)")
              + limits((1, 10), (0, 5))
              + gg.geom_line()
              , 'σpr_Ob_σ')

    df = (pd.DataFrame({'σpr': np.linspace(0, 21, 106)})
            .assign(Pq=lambda x: σpr_Pq(x.σpr)))
    save_both(my_plot(df, 'σpr', 'Pq')
              + titles("P_q(σ')")
              + labs("σ'", 'P_q')
              + limits((0, 20), (-1, 0),
                       xbreaks=np.linspace(0, 20, 11),
                       ybreaks=np.linspace(-1, 0, 11))
              + gg.geom_line()
              , 'Pq_σpr')

    df = concat_map(liab_Ob_Ol_free, 'liab', np.linspace(0, 10, 11))
    save_both(my_plot(df, 'O_b', 'O_l', 'liab', clab='-R_{bl}')
              + titles("-R_{bl}(O_b, O_l)", "S_b = 1, C_b = 0, C_l = 0.02",
                       mathrm('Free bet', dollars=False))
              + limits((1,20), (1, 10))
              + gg.geom_line()
              + gg.geom_abline(slope=1, intercept=0,
                               linetype='dashed', color='grey')
              , 'liab_Ob_Ol_free')

    df = concat_map(liab_Ob_Ol_free, 'liab', np.linspace(0, 10, 11))
    save_both(my_plot(df, 'O_b', 'σ', 'liab', clab='-R_{bl}')
              + titles("-R_{bl}(O_b, σ)", "S_b = 1, C_b = 0, C_l = 0.02",
                       mathrm('Free bet', dollars=False))
              + limits((1,20), (1, 10))
              + gg.geom_line()
              , 'liab_Ob_σ_free')

    df = concat_map(liab_Ob_Ol_qual, 'liab', np.linspace(0, 10, 11))
    save_both(my_plot(df, 'O_b', 'O_l', 'liab', clab='-R_{bl}')
              + titles("-R_{bl}(O_b, O_l)", "S_b = 1, C_b = 0, C_l = 0.02",
                       mathrm('Qualifying bet', dollars=False))
              + limits((1,20), (1, 10))
              + gg.geom_line()
              + gg.geom_abline(slope=1, intercept=0,
                               linetype='dashed', color='grey')
              , 'liab_Ob_Ol_qual')

    df = concat_map(liab_Ob_Ol_qual, 'liab', np.linspace(0, 10, 11))
    save_both(my_plot(df, 'O_b', 'σ', 'liab', clab='-R_{bl}')
              + titles("-R_{bl}(O_b, σ)", "S_b = 1, C_b = 0, C_l = 0.02",
                       mathrm('Qualifying bet', dollars=False))
              + limits((1,20), (1, 10))
              + gg.geom_line()
              , 'liab_Ob_σ_qual')

    df_Pf = Pf_Ob_σ(0.6).assign(profit=dollars('P_f'))
    df_Pq = Pq_Ob_σ(-0.3).assign(profit=dollars('P_q'))
    df = pd.concat((df_Pf, df_Pq), ignore_index=True)
    df.drop_duplicates('O_b', inplace=True)

    Opr = df_Pf.query('σ==0').O_b[0]
    σpr = df_Pq.query('O_b==1').σ[0]

    labels = pd.DataFrame({
        'x': [Opr+0.1, 1, 9.8], 'y': [4.8, σpr, σpr + 0.3],
        'label': ["$O'$", "$σ'$", mathrm('More profit')]
    })
    lab_aes = gg.aes('x', 'y', label='label')

    save_both(
        gg.ggplot(df, gg.aes(x='O_b', y='σ'))
        + gg.geom_area(gg.aes(fill='profit'), alpha=0.3)
        + gg.geom_vline(xintercept=Opr, linetype='dashed')
        + gg.geom_hline(yintercept=σpr, linetype='dashed')

        # text alignment can't be specified in an aes
        + gg.geom_text(lab_aes, data=labels.ix[:0], ha='left', va='top')
        + gg.geom_text(lab_aes, data=labels.ix[1:1], ha='left', va='bottom')
        + gg.geom_text(lab_aes, data=labels.ix[2:], ha='right', va='bottom')

        + gg.scale_fill_discrete(name=mathrm('Bet type'),
                                 labels=[mathrm('Free'), mathrm('Qualifying')])
        + limits((1, 10), (0, 5))
        + gg.ggtitle('%s "%s" %s' % (mathrm('Shape of the'),
                                     mathrm('more profitable'),
                                     mathrm('space')))
        + labs('O_b', 'σ')
        , 'Px_shapes')