jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def gen_plot(config, file_path, data, title=None, learning_curve=False):
    plt.figure(figsize=(6, 5))

    plt.title(config['title'] if not title else title)
    plt.xlabel(config['x_label'])
    plt.ylabel(config['y_label'])

    plt.ylim(*config['ylim'])
    if config['solved']:
        # plt.axhline(config['solved'], color=COLORS[4], linestyle='--', label='$[Solved]$') # only for figure 1
        plt.axhline(config['solved'], color=COLORS[3], linestyle='--', label='$[Solved]$')

    # colors = config['colors'][::-1][1:] # only for figure 1
    colors = config['colors']
    for i, entry in enumerate(data):
        timesteps, averaged_curve, sigma, convergence = entry
        sns.lineplot(timesteps,
                     averaged_curve,
                     c=colors[i],
                     label=config['labels'][i])
        if convergence is not None:
            plt.plot([timesteps[-1], timesteps[-1] + 0.5],
                     [averaged_curve.values[-1], averaged_curve.values[-1]],
                     color=colors[i],
                     linestyle='--')

        plt.fill_between(x=timesteps,
                         y1=averaged_curve + sigma,
                         y2=averaged_curve - sigma,
                         facecolor=colors[i],
                         alpha=0.1)
    if learning_curve:
        plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))

    plt.legend(loc=config['legend_loc'], frameon=True, framealpha=0.5)
    plt.grid(b=False)

    # plt.show()

    plt.savefig(fname=file_path,
                bbox_inches='tight',
                pad_inches=0)
    plt.close()
//  [python]
def plot_runtimes(data, xticks, yticks, yticklabels, output_filename=None):
    plt.figure(figsize=(6.5, 3))
    ax = plt.subplot2grid((1, 1), (0, 0), colspan=1)
    for policy in data['policy'].unique():
        subset = data[data['policy'] == policy]
        plt.plot(subset['num_jobs'], subset['runtimes'], marker='o', label=policy)
    ax.set_xlabel("Number of jobs")
    ax.set_ylabel("Runtime (seconds)")
    ax.set_xscale('log', base=2)
    ax.set_yscale('log', base=2)
    ax.set_xticks(xticks)
    ax.set_xticklabels(xticks)
    ax.set_xlim([min(xticks), max(xticks)])
    ax.set_yticks(yticks)
    ax.set_yticklabels(yticklabels)
    sns.despine()

    leg = plt.legend(loc='upper left', frameon=False)
    
    if output_filename is not None:
        with PdfPages(output_filename) as pdf:
            pdf.savefig(bbox_inches='tight')
            
    plt.show()
//  [python]
def plot_runtimes(data, xticks, yticks, yticklabels, output_filename=None):
    plt.figure(figsize=(6.5, 3))
    ax = plt.subplot2grid((1, 1), (0, 0), colspan=1)
    sns.lineplot(x='num_jobs', y='runtimes', style='policy',
                 hue='policy',
                 data=data, ci=None,
                 markers=True)
    ax.set_xlabel("Number of jobs")
    ax.set_ylabel("Runtime (seconds)")
    ax.set_xscale('log', base=2)
    ax.set_yscale('log', base=2)
    ax.set_xticks(xticks)
    ax.set_xticklabels(xticks)
    ax.set_xlim([min(xticks), max(xticks)])
    ax.set_yticks(yticks)
    ax.set_yticklabels(yticklabels)
    sns.despine()

    leg = plt.legend(loc='upper left', frameon=False)
    
    if output_filename is not None:
        with PdfPages(output_filename) as pdf:
            pdf.savefig(bbox_inches='tight')
            
    plt.show()
//  [python]
def plot_world_aggregate(df, title='Aggregate plot', size=1):
    f, ax = plt.subplots(1,1, figsize=(4*size,2*size))
    g = sns.pointplot(x="Date", y='Total Cases', data=df, color='blue', label='Total Cases')
    g = sns.pointplot(x="Date", y='Total Deaths', data=df, color='red', label='Total Deaths')
    plt.xlabel('Date')
    plt.ylabel(f'Total {title} cases')
    plt.xticks(rotation=90)
    plt.title(f'Total {title} cases')
    ax.grid(color='black', linestyle='dotted', linewidth=0.75)
    plt.show()
//  [python]
def plot_world_aggregate(df, title='Aggregate plot', size=1):
    f, ax = plt.subplots(1,1, figsize=(4*size,2*size))
    g = sns.lineplot(x="Date", y='Total Cases', data=df, color='blue', label='Total Cases')
    g = sns.lineplot(x="Date", y='Total Deaths', data=df, color='red', label='Total Deaths')
    plt.xlabel('Date')
    plt.ylabel(f'Total {title} cases')
    plt.xticks(rotation=90)
    plt.title(f'Total {title} cases')
    ax.grid(color='black', linestyle='dotted', linewidth=0.75)
    plt.show()
//  [python]
def plot_mortality(df, title='Mainland China', size=1):
    f, ax = plt.subplots(1,1, figsize=(4*size,2*size))
    g = sns.pointplot(x="Date", y='Mortality (Deaths/Cases)', data=df, color='blue', label='Mortality (Deaths / Total Cases)')
    plt.xlabel('Date')
    plt.ylabel(f'Mortality {title} [%]')
    plt.xticks(rotation=90)
    plt.title(f'Mortality percent {title}\nCalculated as Deaths/Confirmed cases')
    ax.grid(color='black', linestyle='dashed', linewidth=1)
    plt.show()
//  [python]
def plot_mortality(df, title='Mainland China', size=1):
    f, ax = plt.subplots(1,1, figsize=(4*size,2*size))
    g = sns.lineplot(x="Date", y='Mortality (Deaths/Cases)', data=df, color='blue', label='Mortality (Deaths / Total Cases)')
    plt.xlabel('Date')
    plt.ylabel(f'Mortality {title} [%]')
    plt.xticks(rotation=90)
    plt.title(f'Mortality percent {title}\nCalculated as Deaths/Confirmed cases')
    ax.grid(color='black', linestyle='dashed', linewidth=1)
    plt.show()
//  [python]
def plot_loss_summary(df, loss_type, samples_type, yscale, ax,
        HUE_COLORS=None, miny=None, maxy=None):

    if loss_type in ["mm1_plan_err", "mm1_plan_pg_err", "jerr"]:
        maxy = 10e6

    title_fmt = "{}"
    loss_title = ERROR_NAMES[loss_type]
    title = title_fmt.format(loss_title)

    ax.set_title(title, fontsize=40)
    cur_df = df[df["samples_type"] == samples_type]
    cur_df = cur_df[cur_df["loss_type"] == loss_type]
    miny = min(cur_df["loss"])
    maxy_data = max(cur_df["loss"])
    if maxy is not None:
        maxy = min(maxy, maxy_data)

    if maxy is None:
        maxy = cur_df["loss"].quantile(0.95)

    sns.tsplot(time="epoch", value="loss", unit="alg_name", data=cur_df,
            color=HUE_COLORS, ci=95,
                 ax=ax, legend="full", linewidth=10)
    ax.set_ylim((miny,maxy))

    ax.set_yscale(yscale)
    ax.get_legend().remove()
    ax.tick_params(labelsize=20)
    ax.xaxis.label.set_size(20)
//  [python]
def plot_loss_summary(df, loss_type, samples_type, yscale, ax,
        HUE_COLORS=None, miny=None, maxy=None):

    if loss_type in ["mm1_plan_err", "mm1_plan_pg_err", "jerr"]:
        maxy = 10e6

    title_fmt = "{}"
    loss_title = ERROR_NAMES[loss_type]
    title = title_fmt.format(loss_title)

    ax.set_title(title, fontsize=40)
    cur_df = df[df["samples_type"] == samples_type]
    cur_df = cur_df[cur_df["loss_type"] == loss_type]
    miny = min(cur_df["loss"])
    maxy_data = max(cur_df["loss"])
    if maxy is not None:
        maxy = min(maxy, maxy_data)

    if maxy is None:
        maxy = cur_df["loss"].quantile(0.95)

    sns.lineplot(x="epoch", y="loss", hue="alg_name", data=cur_df,
            palette=HUE_COLORS, ci=95,
                 ax=ax, legend="full", linewidth=10)
    ax.set_ylim((miny,maxy))

    ax.set_yscale(yscale)
    ax.get_legend().remove()
    ax.tick_params(labelsize=20)
    ax.xaxis.label.set_size(20)
//  [python]
def plot(df: pd.DataFrame):
    sns.tsplot(
        x="train_size", y="time_fit", data=df, hue="high_card_feature", style="modif"
    )
    plt.yscale("log")
    # put the legend out of the figure
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
    plt.ylabel("Time (s)")
    plt.xlabel("Train size")
    plt.title("Time to fit the encoder")
    # make sure the plot is not cut
    plt.tight_layout()
    plt.show()

    sns.tsplot(
        x="train_size", y="score_train", data=df, hue="high_card_feature", style="modif"
    )
    plt.yscale("log")
    # put the legend out of the figure
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
    plt.ylabel("Score")
    plt.xlabel("Train size")
    plt.title("Score on train set")
    # make sure the plot is not cut
    plt.tight_layout()
    plt.show()

    sns.tsplot(
        x="train_size",
        y="balanced_accuracy_hgb_test",
        data=df,
        hue="high_card_feature",
        style="modif",
    )
    plt.yscale("log")
    # put the legend out of the figure
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
    plt.ylabel("Balanced accuracy")
    plt.xlabel("Train size")
    plt.title("Balanced accuracy on test set")
    # make sure the plot is not cut
    plt.tight_layout()
    plt.show()
//  [python]
def plot(df: pd.DataFrame):
    sns.lineplot(
        x="train_size", y="time_fit", data=df, hue="high_card_feature", style="modif"
    )
    plt.yscale("log")
    # put the legend out of the figure
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
    plt.ylabel("Time (s)")
    plt.xlabel("Train size")
    plt.title("Time to fit the encoder")
    # make sure the plot is not cut
    plt.tight_layout()
    plt.show()

    sns.lineplot(
        x="train_size", y="score_train", data=df, hue="high_card_feature", style="modif"
    )
    plt.yscale("log")
    # put the legend out of the figure
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
    plt.ylabel("Score")
    plt.xlabel("Train size")
    plt.title("Score on train set")
    # make sure the plot is not cut
    plt.tight_layout()
    plt.show()

    sns.lineplot(
        x="train_size",
        y="balanced_accuracy_hgb_test",
        data=df,
        hue="high_card_feature",
        style="modif",
    )
    plt.yscale("log")
    # put the legend out of the figure
    plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
    plt.ylabel("Balanced accuracy")
    plt.xlabel("Train size")
    plt.title("Balanced accuracy on test set")
    # make sure the plot is not cut
    plt.tight_layout()
    plt.show()
//  [python]
def plot_aggregate_countries(df, countries, case_type='Total Cases', size=3, is_log=False):
    f, ax = plt.subplots(1,1, figsize=(4*size, 3*size))
    for country in countries:
        df_ = df[(df['Country']==country) & (df['Date'] > '2020-02-15')] 
        g = sns.pointplot(x="Date", y=case_type, data=df_,  label=country)  
        ax.text(max(df_['Date']), max(df_[case_type]), str(country))
    plt.xlabel('Date')
    plt.ylabel(f' {case_type} ')
    plt.title(f' {case_type} ')
    plt.xticks(rotation=90)
    if(is_log):
        ax.set(yscale="log")
    ax.grid(color='black', linestyle='dotted', linewidth=0.75)
    plt.show()
//  [python]
def plot_aggregate_countries(df, countries, case_type='Total Cases', size=3, is_log=False):
    f, ax = plt.subplots(1,1, figsize=(4*size, 3*size))
    for country in countries:
        df_ = df[(df['Country']==country) & (df['Date'] > '2020-02-15')] 
        g = sns.lineplot(x="Date", y=case_type, data=df_,  label=country)  
        ax.text(max(df_['Date']), max(df_[case_type]), str(country))
    plt.xlabel('Date')
    plt.ylabel(f' {case_type} ')
    plt.title(f' {case_type} ')
    plt.xticks(rotation=90)
    if(is_log):
        ax.set(yscale="log")
    ax.grid(color='black', linestyle='dotted', linewidth=0.75)
    plt.show()
//  [python]
def plot_loss(df, loss_type, samples_type, yscale):

    fig=plt.figure(figsize=(10, 10), dpi= 80, facecolor='w', edgecolor='k')
    if loss_type == "qerr":
        title = title_fmt.format("MSE", samples_type)
    else:
        title = title_fmt.format(loss_type, samples_type)
    plt.title(title, fontsize=50)
    cur_df = df[df["samples_type"] == samples_type]
    cur_df = cur_df[cur_df["loss_type"] == loss_type]

    scale_df = df[df["epoch"] == 4]
    scale_df = scale_df[scale_df["loss_type"] == loss_type]
    maxy = max(scale_df["loss"])
    miny = min(cur_df["loss"])
    #print(cur_df)
    for alg_name, data in cur_df.groupby("alg_name"):
        plt.plot(data["epoch"], data["loss"], label=alg_name, color=HUE_COLORS[alg_name])
    plt.ylim((miny,maxy))
    #plt.ylim((0,10000))
    plt.yscale(yscale)
    plt.rc('legend', fontsize=10, loc="lower left")    # legend fontsize
    plt.show()
//  [python]
def plot_loss(df, loss_type, samples_type, yscale):

    fig=plt.figure(figsize=(10, 10), dpi= 80, facecolor='w', edgecolor='k')
    if loss_type == "qerr":
        title = title_fmt.format("MSE", samples_type)
    else:
        title = title_fmt.format(loss_type, samples_type)
    plt.title(title, fontsize=50)
    cur_df = df[df["samples_type"] == samples_type]
    cur_df = cur_df[cur_df["loss_type"] == loss_type]

    scale_df = df[df["epoch"] == 4]
    scale_df = scale_df[scale_df["loss_type"] == loss_type]
    maxy = max(scale_df["loss"])
    miny = min(cur_df["loss"])
    #print(cur_df)
    sns.lineplot(x="epoch", y="loss", hue="alg_name", data=cur_df, palette=HUE_COLORS, ci=None)
    plt.ylim((miny,maxy))
    #plt.ylim((0,10000))
    plt.yscale(yscale)
    plt.rc('legend', fontsize=10, loc="lower left")    # legend fontsize
    plt.show()
//  [python]
df = [pd.read_csv(fn) for fn in snakemake.input]
df = pd.concat(df, ignore_index=True)

df["# Variables"] = df.N**2 * 2

fig, ax = plt.subplots()
sns.tsplot(x="# Variables", y="Time [s]", hue="API", data=df, ax=ax)
//  [python]
df = [pd.read_csv(fn) for fn in snakemake.input]
df = pd.concat(df, ignore_index=True)

df["# Variables"] = df.N**2 * 2

fig, ax = plt.subplots()
sns.lineplot(x="# Variables", y="Time [s]", hue="API", data=df, ax=ax)
//  [python]
def line(g_dt, g_x_ax, g_y_ax, g_hue, g_palette):
    # https://seaborn.pydata.org/generated/seaborn.lineplot.html#seaborn.lineplot
    if g_hue == '':
        sns.tsplot(
            data=g_dt,
            time=g_x_ax,
            value=g_y_ax,
            palette=g_palette
        )
    else:
        sns.tsplot(
            data=g_dt,
            time=g_x_ax,
            value=g_y_ax,
            condition=g_hue,
            palette=g_palette
        )
    plt.show()
    plt.close()
//  [python]
def line(g_dt, g_x_ax, g_y_ax, g_hue, g_palette):
    # https://seaborn.pydata.org/generated/seaborn.lineplot.html#seaborn.lineplot
    if g_hue == '':
        sns.lineplot(
            data=g_dt,
            x=g_x_ax,
            y=g_y_ax,
            palette=g_palette
        )
    else:
        sns.lineplot(
            data=g_dt,
            x=g_x_ax,
            y=g_y_ax,
            hue=g_hue,
            palette=g_palette
        )
    plt.show()
    plt.close()
//  [python]
def main():
    """The main function."""

    print('Generating plot data...')
    plot_data = gen_plot_data()

    # Do the actual plotting
    print('plotting results...')
    sns.set_theme(style='ticks')
    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax = sns.tsplot(ax=ax, data=plot_data, time='time', value='region_percent',
                      unit='Seed', condition='Fuzzer', ci=95, n_boot=NUM_BOOTS)

    # Tidy up plot
    xticks = [0, 1, 2, 5, 10] # Hours
    ax.set(xlabel='Time (h)',
           ylabel='Regions (\%)',
           xscale='symlog',
           xticks=xticks,
           xticklabels=[f'{x}' for x in xticks])
    ax.set_ylim(bottom=0)
    ax.set_xlim(left=0, right=TRIAL_LEN)
    ax.legend(ncol=2, loc='upper center', bbox_to_anchor=(0.5, 1.3))
    sns.despine()

    # Save plot
    fig.savefig('readelf-experiment.pdf', bbox_inches='tight')
//  [python]
def main():
    """The main function."""

    print('Generating plot data...')
    plot_data = gen_plot_data()

    # Do the actual plotting
    print('plotting results...')
    sns.set_theme(style='ticks')
    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax = sns.lineplot(ax=ax, data=plot_data, x='time', y='region_percent',
                      hue='Seed', style='Fuzzer', ci=95, n_boot=NUM_BOOTS)

    # Tidy up plot
    xticks = [0, 1, 2, 5, 10] # Hours
    ax.set(xlabel='Time (h)',
           ylabel='Regions (\%)',
           xscale='symlog',
           xticks=xticks,
           xticklabels=[f'{x}' for x in xticks])
    ax.set_ylim(bottom=0)
    ax.set_xlim(left=0, right=TRIAL_LEN)
    ax.legend(ncol=2, loc='upper center', bbox_to_anchor=(0.5, 1.3))
    sns.despine()

    # Save plot
    fig.savefig('readelf-experiment.pdf', bbox_inches='tight')
//  [python]
def _make_binary_pr(self, y_true, y_proba, class_label=None):
        """Make a PR curve for the given data.

        Includes the average precision score for the given data.

        """
        (precision, recall, _) = precision_recall_curve(y_true, y_proba)
        cl_ap = average_precision_score(y_true, y_proba)

        if class_label:
            label = '{0} - AP: {1:.2f}'.format(class_label, cl_ap)
        else:
            label = 'AP: {0:.2f}'.format(cl_ap)

        return plt.plot(recall, precision, label=label)
//  [python]
def _make_binary_pr(self, y_true, y_proba, class_label=None):
        """Make a PR curve for the given data.

        Includes the average precision score for the given data.

        """
        (precision, recall, _) = precision_recall_curve(y_true, y_proba)
        cl_ap = average_precision_score(y_true, y_proba)

        if class_label:
            label = '{0} - AP: {1:.2f}'.format(class_label, cl_ap)
        else:
            label = 'AP: {0:.2f}'.format(cl_ap)

        return sns.lineplot(x=recall, y=precision, label=label)
//  [python]
def _make_binary_roc(self, y_true, y_proba, class_label=None):
        """Make ROC curve for given data.

        Includes roc_auc_score.

        """
        (fpr, tpr, _) = roc_curve(y_true, y_proba)
        cl_auc = auc(fpr, tpr)

        if class_label:
            label = '{0} - AUC: {1:.2f}'.format(class_label, cl_auc)
        else:
            label = 'AUC: {0:.2f}'.format(cl_auc)

        return plt.plot(fpr, tpr, label=label)
//  [python]
def _make_binary_roc(self, y_true, y_proba, class_label=None):
        """Make ROC curve for given data.

        Includes roc_auc_score.

        """
        (fpr, tpr, _) = roc_curve(y_true, y_proba)
        cl_auc = auc(fpr, tpr)

        if class_label:
            label = '{0} - AUC: {1:.2f}'.format(class_label, cl_auc)
        else:
            label = 'AUC: {0:.2f}'.format(cl_auc)

        return sns.lineplot(x=fpr, y=tpr, label=label)
//  [python]
def figure_12_14():
    # Get plot data for each task
    df_rw = get_random_walk_plot_data()
    df_mc = get_mountain_car_plot_data()
    df_cp = get_cart_pole_plot_data()
    df_pw = get_puddle_world_plot_data()

    fig, axes = plt.subplots(2, 2, figsize=(12, 12))

    # Mountain Car
    sns.pointplot(data=df_mc, x='lambda', y='steps', hue='method',
                 ax=axes[0, 0], ci=68)
    axes[0, 0].set_xlabel('λ')
    axes[0, 0].set_ylabel('Steps per episode')
    axes[0, 0].set_title(f"MOUNTAIN CAR", fontsize=15)
    axes[0, 0].set_ylim(0, 500)

    # Random walk
    sns.pointplot(data=df_rw, x='lambda', y='rms', hue='method', ax=axes[0, 1])
    axes[0, 1].set_xlabel('λ')
    axes[0, 1].set_ylabel('RMS error')
    axes[0, 1].set_title(f"RANDOM WALK", fontsize=15)
    axes[0, 1].set_ylim(0.2, 0.6)

    # Puddle world
    sns.pointplot(data=df_pw, x='lambda', y='cost', hue='method', ax=axes[1, 0], ci=68)
    axes[1, 0].set_xlabel('λ')
    axes[1, 0].set_ylabel('Cost per episode')
    axes[1, 0].set_title(f"PUDDLE WORLD", fontsize=15)

    # Cart and pole
    sns.pointplot(data=df_cp, x='lambda', y='n_fails', hue='method', ax=axes[1, 1], ci=68)
    axes[1, 1].set_xlabel('λ')
    axes[1, 1].set_ylabel('Failures per 100 000 steps')
    axes[1, 1].set_title(f"CART AND POLE", fontsize=15)

    plt.savefig('combined_fig_test')
//  [python]
def figure_12_14():
    # Get plot data for each task
    df_rw = get_random_walk_plot_data()
    df_mc = get_mountain_car_plot_data()
    df_cp = get_cart_pole_plot_data()
    df_pw = get_puddle_world_plot_data()

    fig, axes = plt.subplots(2, 2, figsize=(12, 12))

    # Mountain Car
    sns.lineplot(data=df_mc, x='lambda', y='steps', hue='method',
                 style='method', ax=axes[0, 0], marker='o', ci=68, err_style="bars")
    axes[0, 0].set_xlabel('λ')
    axes[0, 0].set_ylabel('Steps per episode')
    axes[0, 0].set_title(f"MOUNTAIN CAR", fontsize=15)
    axes[0, 0].set_ylim(0, 500)

    # Random walk
    sns.lineplot(data=df_rw, x='lambda', y='rms', hue='method', style='method', ax=axes[0, 1], marker='o')
    axes[0, 1].set_xlabel('λ')
    axes[0, 1].set_ylabel('RMS error')
    axes[0, 1].set_title(f"RANDOM WALK", fontsize=15)
    axes[0, 1].set_ylim(0.2, 0.6)

    # Puddle world
    sns.lineplot(data=df_pw, x='lambda', y='cost', hue='method', ax=axes[1, 0], marker='o', ci=68, err_style="bars")
    axes[1, 0].set_xlabel('λ')
    axes[1, 0].set_ylabel('Cost per episode')
    axes[1, 0].set_title(f"PUDDLE WORLD", fontsize=15)

    # Cart and pole
    sns.lineplot(data=df_cp, x='lambda', y='n_fails', hue='method', ax=axes[1, 1], marker='o', ci=68,
                 err_style="bars")
    axes[1, 1].set_xlabel('λ')
    axes[1, 1].set_ylabel('Failures per 100 000 steps')
    axes[1, 1].set_title(f"CART AND POLE", fontsize=15)

    plt.savefig('combined_fig_test')
//  [python]
def _calculate(self, df: pd.DataFrame, ax: list[plt.Axes]) -> pd.Series:
        days_shift = 1

        df = df.merge(
            cs_fetch(
                path='chain/index/charts?type=/charts/reserve-risk/',
                data_selector='value4',
                col_name='Risk',
            ),
            on='Date',
            how='left',
        )
        df['Risk'] = df['Risk'].shift(days_shift, fill_value=np.nan)
        df['Risk'] = df['Risk'].ffill()
        df['RiskLog'] = np.log(df['Risk'])

        high_rows = df.loc[df['PriceHigh'] == 1]
        high_x = high_rows.index.values.reshape(-1, 1)
        high_y = high_rows['RiskLog'].values.reshape(-1, 1)

        low_rows = df.loc[df['PriceLow'] == 1][1:]
        low_x = low_rows.index.values.reshape(-1, 1)
        low_y = low_rows['RiskLog'].values.reshape(-1, 1)

        x = df.index.values.reshape(-1, 1)

        lin_model = LinearRegression()
        lin_model.fit(high_x, high_y)
        df['HighModel'] = lin_model.predict(x)
        df['HighModel'] = df['HighModel'] - 0.15

        lin_model.fit(low_x, low_y)
        df['LowModel'] = lin_model.predict(x)

        df['RiskIndex'] = (df['RiskLog'] - df['LowModel']) / (df['HighModel'] - df['LowModel'])

        df['RiskIndexNoNa'] = df['RiskIndex'].fillna(0)
        ax[0].set_title(self.description)
        plt.plot(df['Date'], df['RiskIndexNoNa'], ax=ax[0])
        add_common_markers(df, ax[0])

        plt.plot(df['Date'], df['RiskLog'], ax=ax[1])
        plt.plot(df['Date'], df['HighModel'], ax=ax[1])
        plt.plot(df['Date'], df['LowModel'], ax=ax[1])
        add_common_markers(df, ax[1], price_line=False)

        return df['RiskIndex']
//  [python]
def _calculate(self, df: pd.DataFrame, ax: list[plt.Axes]) -> pd.Series:
        days_shift = 1

        df = df.merge(
            cs_fetch(
                path='chain/index/charts?type=/charts/reserve-risk/',
                data_selector='value4',
                col_name='Risk',
            ),
            on='Date',
            how='left',
        )
        df['Risk'] = df['Risk'].shift(days_shift, fill_value=np.nan)
        df['Risk'] = df['Risk'].ffill()
        df['RiskLog'] = np.log(df['Risk'])

        high_rows = df.loc[df['PriceHigh'] == 1]
        high_x = high_rows.index.values.reshape(-1, 1)
        high_y = high_rows['RiskLog'].values.reshape(-1, 1)

        low_rows = df.loc[df['PriceLow'] == 1][1:]
        low_x = low_rows.index.values.reshape(-1, 1)
        low_y = low_rows['RiskLog'].values.reshape(-1, 1)

        x = df.index.values.reshape(-1, 1)

        lin_model = LinearRegression()
        lin_model.fit(high_x, high_y)
        df['HighModel'] = lin_model.predict(x)
        df['HighModel'] = df['HighModel'] - 0.15

        lin_model.fit(low_x, low_y)
        df['LowModel'] = lin_model.predict(x)

        df['RiskIndex'] = (df['RiskLog'] - df['LowModel']) / (df['HighModel'] - df['LowModel'])

        df['RiskIndexNoNa'] = df['RiskIndex'].fillna(0)
        ax[0].set_title(self.description)
        sns.lineplot(data=df, x='Date', y='RiskIndexNoNa', ax=ax[0])
        add_common_markers(df, ax[0])

        sns.lineplot(data=df, x='Date', y='RiskLog', ax=ax[1])
        sns.lineplot(data=df, x='Date', y='HighModel', ax=ax[1])
        sns.lineplot(data=df, x='Date', y='LowModel', ax=ax[1])
        add_common_markers(df, ax[1], price_line=False)

        return df['RiskIndex']
//  [python]
def plot(self, input_colors=None, output_colors=None, output_only=False):
    """Plots the sequence in seaborn lineplot.

    Args:
      input_colors: A list of strings, optional.
      output_colors: A list of strings, optional.
      output_only: Whether to only plot output.
    """
    plt.figure(figsize=(10, 6))
    if not output_only:
      for i in xrange(self.input_dim):
        c = input_colors[i] if input_colors else None
        plt.plot(
            np.arange(self.seq_len),
            self.inputs[:, i],
            label='input_' + str(i),
            alpha=0.5,
            color=c)
    for i in xrange(self.output_dim):
      c = output_colors[i] if output_colors else None
      plt.plot(
          np.arange(self.seq_len),
          self.outputs[:, i],
          label='output_' + str(i),
          color=c)
    plt.title('Generated Sequence')
//  [python]
def plot(self, input_colors=None, output_colors=None, output_only=False):
    """Plots the sequence in seaborn lineplot.

    Args:
      input_colors: A list of strings, optional.
      output_colors: A list of strings, optional.
      output_only: Whether to only plot output.
    """
    plt.figure(figsize=(10, 6))
    if not output_only:
      for i in xrange(self.input_dim):
        c = input_colors[i] if input_colors else None
        sns.lineplot(
            x=np.arange(self.seq_len),
            y=self.inputs[:, i],
            label='input_' + str(i),
            alpha=0.5,
            color=c)
    for i in xrange(self.output_dim):
      c = output_colors[i] if output_colors else None
      sns.lineplot(
          x=np.arange(self.seq_len),
          y=self.outputs[:, i],
          label='output_' + str(i),
          color=c)
    plt.title('Generated Sequence')
//  [python]
def polarity(
    adata: AnnData,
    gene_dict: dict,
    region_key: str,
    mode: str = "density",
):
    """Simple function to visualize expression level varies along regions.

    Args:
        adata (AnnData): _description_
        gene_dict (dict): _description_
        region_key (str): _description_
        mode (str, optional): _description_. Defaults to "density".

    Returns:
        _type_: _description_
    """

    digi_region = np.array([])
    gene_list = np.array([])
    gene_mean = np.array([])

    if mode == "exp":
        for i in np.unique(adata.obs[region_key]):
            adata_tmp = adata[adata.obs[region_key] == i, :]
            for anno in list(gene_dict.keys()):
                for gene in gene_dict[anno]:
                    gene_mean_tmp = adata_tmp[:, gene].X.toarray().T[0]
                    digi_region = np.append(digi_region, np.repeat(i, len(adata_tmp)))
                    gene_list = np.append(gene_list, np.repeat(gene + " " + anno, len(adata_tmp)))
                    gene_mean = np.append(gene_mean, gene_mean_tmp)
        df_plt = pd.DataFrame({region_key: digi_region, "Gene": gene_list, "Mean expression": gene_mean})
        ax = sns.pointplot(data=df_plt, x=region_key, y="Mean expression", hue="Gene")
    elif mode == "density":
        for i in np.unique(adata.obs[region_key]):
            adata_tmp = adata[adata.obs[region_key] == i, :]
            for anno in list(gene_dict.keys()):
                for gene in gene_dict[anno]:
                    digi_region.append(i)
                    gene_list.append(gene + " " + anno)
                    gene_mean.append(np.mean(adata_tmp[:, gene].X))
        df_plt = pd.DataFrame({region_key: digi_region, "Gene": gene_list, "Mean expression": gene_mean})
        p = sns.kdeplot(data=df_plt, x=region_key, weights="Mean expression", hue="Gene")
        p.set_xlim(0, max(adata.obs[region_key]))

    return ax
//  [python]
def polarity(
    adata: AnnData,
    gene_dict: dict,
    region_key: str,
    mode: str = "density",
):
    """Simple function to visualize expression level varies along regions.

    Args:
        adata (AnnData): _description_
        gene_dict (dict): _description_
        region_key (str): _description_
        mode (str, optional): _description_. Defaults to "density".

    Returns:
        _type_: _description_
    """

    digi_region = np.array([])
    gene_list = np.array([])
    gene_mean = np.array([])

    if mode == "exp":
        for i in np.unique(adata.obs[region_key]):
            adata_tmp = adata[adata.obs[region_key] == i, :]
            for anno in list(gene_dict.keys()):
                for gene in gene_dict[anno]:
                    gene_mean_tmp = adata_tmp[:, gene].X.toarray().T[0]
                    digi_region = np.append(digi_region, np.repeat(i, len(adata_tmp)))
                    gene_list = np.append(gene_list, np.repeat(gene + " " + anno, len(adata_tmp)))
                    gene_mean = np.append(gene_mean, gene_mean_tmp)
        df_plt = pd.DataFrame({region_key: digi_region, "Gene": gene_list, "Mean expression": gene_mean})
        ax = sns.lineplot(data=df_plt, x=region_key, y="Mean expression", hue="Gene")
    elif mode == "density":
        for i in np.unique(adata.obs[region_key]):
            adata_tmp = adata[adata.obs[region_key] == i, :]
            for anno in list(gene_dict.keys()):
                for gene in gene_dict[anno]:
                    digi_region.append(i)
                    gene_list.append(gene + " " + anno)
                    gene_mean.append(np.mean(adata_tmp[:, gene].X))
        df_plt = pd.DataFrame({region_key: digi_region, "Gene": gene_list, "Mean expression": gene_mean})
        p = sns.kdeplot(data=df_plt, x=region_key, weights="Mean expression", hue="Gene")
        p.set_xlim(0, max(adata.obs[region_key]))

    return ax
//  [python]
def returns_v_cleanup_steps(df):
    # Hack for current planner names
    df.loc[df["Planner"] == "SGM", "Planner"] = "Sparse Graphical Memory (ours)"
    df.loc[df["Planner"] == "SoRB", "Planner"] = "SoRB + Our Proposed Cleanup"

    ax = sns.tsplot(time="Cleanup Steps", value="Success Rate", unit="Trial", condition="Planner", data=df, palette="viridis")
    ax.legend(loc="upper right", bbox_to_anchor=(0.98, 0.8))
    ax.set_title("How Performance Improves with Cleaning")

    timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    output_directory = os.path.join(os.getcwd(), "plots", f"returns_v_cleanup_steps_{timestamp}")
    if not os.path.isdir(output_directory):
        os.makedirs(output_directory)
    output_file = os.path.join(output_directory, "returns_v_cleanup_steps.png")

    fig = ax.get_figure()
    fig.savefig(output_file, dpi=200)
    plt.close()
//  [python]
def returns_v_cleanup_steps(df):
    # Hack for current planner names
    df.loc[df["Planner"] == "SGM", "Planner"] = "Sparse Graphical Memory (ours)"
    df.loc[df["Planner"] == "SoRB", "Planner"] = "SoRB + Our Proposed Cleanup"

    ax = sns.lineplot(x="Cleanup Steps", y="Success Rate", hue="Planner", style="Planner", data=df, palette="viridis")
    ax.legend(loc="upper right", bbox_to_anchor=(0.98, 0.8))
    ax.set_title("How Performance Improves with Cleaning")

    timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    output_directory = os.path.join(os.getcwd(), "plots", f"returns_v_cleanup_steps_{timestamp}")
    if not os.path.isdir(output_directory):
        os.makedirs(output_directory)
    output_file = os.path.join(output_directory, "returns_v_cleanup_steps.png")

    fig = ax.get_figure()
    fig.savefig(output_file, dpi=200)
    plt.close()
//  [python]
def plot_action(self, throttle_list, action_name):
        throttle_pd = pd.DataFrame(throttle_list)
        throttle_pd = throttle_pd.transpose()
        throttle_pd.columns = name_list
        # print(throttle_pd)
        plt.figure()
        plt.plot(throttle_pd)
        plt.xlim(collision_frame_window)
        plt.savefig(f"./analysis/{compare_name}_{action_name}_compare.png")
        plt.close()
//  [python]
def plot_action(self, throttle_list, action_name):
        throttle_pd = pd.DataFrame(throttle_list)
        throttle_pd = throttle_pd.transpose()
        throttle_pd.columns = name_list
        # print(throttle_pd)
        plt.figure()
        sns.lineplot(data=throttle_pd)
        plt.xlim(collision_frame_window)
        plt.savefig(f"./analysis/{compare_name}_{action_name}_compare.png")
        plt.close()
//  [python]
def plot_per_obj(self, detected_obj_pd, path, objId=None):
        # print(detected_obj_pd)
        if objId is not None:
            # print(detected_obj_pd.keys())
            detected_obj_pd = detected_obj_pd[objId]

        plt.figure()
        # sns.barplot(x='FrameID', y='Visibility(Single/Shared)', data=detected_obj_ratio_pd)
        sns.pointplot(data=detected_obj_pd)
        plt.xlabel("Frame ID")
        plt.ylabel("# of visible cubic (5x5x5cm)")
        plt.legend(title="Object ID")
        plt.savefig(path)
        plt.close()
//  [python]
def plot_per_obj(self, detected_obj_pd, path, objId=None):
        # print(detected_obj_pd)
        if objId is not None:
            # print(detected_obj_pd.keys())
            detected_obj_pd = detected_obj_pd[objId]

        plt.figure()
        # sns.barplot(x='FrameID', y='Visibility(Single/Shared)', data=detected_obj_ratio_pd)
        sns.lineplot(data=detected_obj_pd)
        plt.xlabel("Frame ID")
        plt.ylabel("# of visible cubic (5x5x5cm)")
        plt.legend(title="Object ID")
        plt.savefig(path)
        plt.close()
//  [python]
def _static_plot(self, **kwargs):
        """
        Generates a static Effiency Regression Figure
        """
        kwargs = self.update_params(kwargs)

        data, headers, title, show, xlabel, ylabel, ncols, nrows = self._prep_shared_kwargs(kwargs)
        label_subplots, start_character, show_spines, rot = self._get_labels_and_spines_and_rot(kwargs)

        palette, is_palette = gx.generate_palette(kwargs, True)
        if palette:
            if is_palette:
                palette = dict(palette=palette)
            else:
                palette = dict(color=palette)
        else:
            palette = dict(palette=defaults.default_palette)

        # set a seaborn style
        style = kwargs.pop("style", defaults.default_style)
        sns.set_style(style)

        edgecolor = kwargs.pop("edgecolor", "white")
        edgewidth = kwargs.pop("edgewidth", None)

        linecolor = kwargs.pop("linecolor", "black")
        linewidth = kwargs.pop("linewidth", 1)

        fig, Coords = self._setup_static_figure(ncols, nrows, title, kwargs)

        idx = 0
        for id, obj in data.items():

            ax = Coords.subplot()

            # get data to plot
            dilutions, cts = obj.values()
            eff = obj.efficiency()
            model = obj.model()
            Rsquare = model.rvalue**2

            # plot the regression line
            yvals = model.slope * dilutions + model.intercept
            plt.plot(dilutions, yvals, color=linecolor, linewidth=linewidth, ax=ax, **kwargs)

            # plot the original data
            sns.scatterplot(x=dilutions, y=cts, hue=dilutions if is_palette else None, edgecolor=edgecolor, linewidth=edgewidth, ax=ax, **palette, **kwargs)

            # add additional info to a legend
            ax.legend(
                handles=[
                    # R^2 Value
                    Line2D([0], [0], color="black", visible=False, label=f"$R^2$ \t = {Rsquare:.4f}"),
                    # Efficiency Value
                    Line2D([0], [0], color="black", visible=False, label=f"$eff.$\t= {eff:.4f}"),
                ],
                loc="upper right",
                frameon=False,
            )

            # some formatting...
            ax.set(
                title=id,
                xlabel=xlabel,
                ylabel=ylabel,
            )

            if rot is not None:
                self._set_xtick_rotation(ax, rot)

            # add ABCD... label to subplot
            if label_subplots:
                self._add_subplot_label(idx, ax, start_character)

            if not show_spines:
                sns.despine()

            idx += 1

        plt.tight_layout()
        if show:
            plt.show()

        return fig
//  [python]
def _static_plot(self, **kwargs):
        """
        Generates a static Effiency Regression Figure
        """
        kwargs = self.update_params(kwargs)

        data, headers, title, show, xlabel, ylabel, ncols, nrows = self._prep_shared_kwargs(kwargs)
        label_subplots, start_character, show_spines, rot = self._get_labels_and_spines_and_rot(kwargs)

        palette, is_palette = gx.generate_palette(kwargs, True)
        if palette:
            if is_palette:
                palette = dict(palette=palette)
            else:
                palette = dict(color=palette)
        else:
            palette = dict(palette=defaults.default_palette)

        # set a seaborn style
        style = kwargs.pop("style", defaults.default_style)
        sns.set_style(style)

        edgecolor = kwargs.pop("edgecolor", "white")
        edgewidth = kwargs.pop("edgewidth", None)

        linecolor = kwargs.pop("linecolor", "black")
        linewidth = kwargs.pop("linewidth", 1)

        fig, Coords = self._setup_static_figure(ncols, nrows, title, kwargs)

        idx = 0
        for id, obj in data.items():

            ax = Coords.subplot()

            # get data to plot
            dilutions, cts = obj.values()
            eff = obj.efficiency()
            model = obj.model()
            Rsquare = model.rvalue**2

            # plot the regression line
            yvals = model.slope * dilutions + model.intercept
            sns.lineplot(x=dilutions, y=yvals, color=linecolor, linewidth=linewidth, ax=ax, **kwargs)

            # plot the original data
            sns.scatterplot(x=dilutions, y=cts, hue=dilutions if is_palette else None, edgecolor=edgecolor, linewidth=edgewidth, ax=ax, **palette, **kwargs)

            # add additional info to a legend
            ax.legend(
                handles=[
                    # R^2 Value
                    Line2D([0], [0], color="black", visible=False, label=f"$R^2$ \t = {Rsquare:.4f}"),
                    # Efficiency Value
                    Line2D([0], [0], color="black", visible=False, label=f"$eff.$\t= {eff:.4f}"),
                ],
                loc="upper right",
                frameon=False,
            )

            # some formatting...
            ax.set(
                title=id,
                xlabel=xlabel,
                ylabel=ylabel,
            )

            if rot is not None:
                self._set_xtick_rotation(ax, rot)

            # add ABCD... label to subplot
            if label_subplots:
                self._add_subplot_label(idx, ax, start_character)

            if not show_spines:
                sns.despine()

            idx += 1

        plt.tight_layout()
        if show:
            plt.show()

        return fig
//  [python]
def plot_pred(self,
                predictions,
                num_warm_start_steps,
                plot_error=False,
                seq_colors=None,
                pred_colors=None):
    """Plots predictions vs ground truth.

    Args:
      predictions: A numpy array of length self.seq_len - num_warm_start_steps.
      num_warm_start_steps: Number of steps before generating predictions.
      plot_error: Whether to plot errors.
      seq_colors: A list of strings, optional.
      pred_colors: A list of strings, optional.
    """
    self.plot(output_colors=seq_colors, output_only=True)
    for i in xrange(self.output_dim):
      c = pred_colors[i] if pred_colors else None
      plt.plot(
          np.arange(num_warm_start_steps, self.seq_len),
          predictions[:, i],
          label='pred_' + str(i),
          color=c)
      if plot_error:
        plt.plot(
            np.arange(num_warm_start_steps, self.seq_len),
            predictions[:, i] - self.outputs[num_warm_start_steps:, i],
            label='error_' + str(i),
            color='black')
    plt.title('Predictions, relative error: ' +
              str(self.get_pred_error(predictions, num_warm_start_steps)))
//  [python]
def plot_pred(self,
                predictions,
                num_warm_start_steps,
                plot_error=False,
                seq_colors=None,
                pred_colors=None):
    """Plots predictions vs ground truth.

    Args:
      predictions: A numpy array of length self.seq_len - num_warm_start_steps.
      num_warm_start_steps: Number of steps before generating predictions.
      plot_error: Whether to plot errors.
      seq_colors: A list of strings, optional.
      pred_colors: A list of strings, optional.
    """
    self.plot(output_colors=seq_colors, output_only=True)
    for i in xrange(self.output_dim):
      c = pred_colors[i] if pred_colors else None
      sns.lineplot(
          x=np.arange(num_warm_start_steps, self.seq_len),
          y=predictions[:, i],
          label='pred_' + str(i),
          color=c)
      if plot_error:
        sns.lineplot(
            x=np.arange(num_warm_start_steps, self.seq_len),
            y=predictions[:, i] - self.outputs[num_warm_start_steps:, i],
            label='error_' + str(i),
            color='black')
    plt.title('Predictions, relative error: ' +
              str(self.get_pred_error(predictions, num_warm_start_steps)))
//  [python]
def plot_ratio_compare(self, avg_ratio_pd_concat, path):
        avg_ratio_pd_concat = avg_ratio_pd_concat.reset_index()
        # print(avg_ratio_pd_concat)
        x_name = "Object ID"
        hue_name = "mode"
        y_name = "Occlusion Ratio"
        avg_ratio_pd_concat = avg_ratio_pd_concat.melt(
            id_vars="index",
            var_name="mode",
            value_name="occlusion ratio"
        )
        avg_ratio_pd_concat.columns = [x_name, hue_name, y_name]
        # print(avg_ratio_pd_concat)

        plt.figure()
        sns.pointplot(x=x_name, y=y_name, hue=hue_name, data=avg_ratio_pd_concat)
        plt.savefig(path)
        plt.close()
        return avg_ratio_pd_concat
//  [python]
def plot_ratio_compare(self, avg_ratio_pd_concat, path):
        avg_ratio_pd_concat = avg_ratio_pd_concat.reset_index()
        # print(avg_ratio_pd_concat)
        x_name = "Object ID"
        hue_name = "mode"
        y_name = "Occlusion Ratio"
        avg_ratio_pd_concat = avg_ratio_pd_concat.melt(
            id_vars="index",
            var_name="mode",
            value_name="occlusion ratio"
        )
        avg_ratio_pd_concat.columns = [x_name, hue_name, y_name]
        # print(avg_ratio_pd_concat)

        plt.figure()
        sns.lineplot(x=x_name, y=y_name, hue=hue_name, data=avg_ratio_pd_concat)
        plt.savefig(path)
        plt.close()
        return avg_ratio_pd_concat
//  [python]
def save_traj_plots(results_dir, f_pred_xyz, b_pred_xyz, axes=[0, 1], plt_prefix=''):
    """Plot and save trajectories to a file.

    Args:
        results_dir (str): Directory to save the plots.
        f_pred_xyz (torch.Tensor): Predicted xyz values of a trajectory.
        b_pred_xyz (torch.Tensor): Predicted inverse xyz values of a trajectory.
        axes (list, optional): Which axes to use in 2D plot. Defaults to [0, 1].
    """
    plt.figure(figsize=(16, 8))
    plt.subplot(1, 2, 1)
    ax = plt.plot(f_pred_xyz[:, axes[0]].cpu().numpy(), f_pred_xyz[:, axes[1]].cpu().numpy())
    ax[0].set(title='Forward Trajectory', xlabel='X (m)', ylabel='Y (m)')
    plt.subplot(1, 2, 2)
    ax = plt.plot(b_pred_xyz[:, axes[0]].cpu().numpy(), b_pred_xyz[:, axes[1]].cpu().numpy(), marker='o')
    ax[0].set(title='Backward Trajectory', xlabel='X (m)', ylabel='Y (m)')

    # Save fig
    plt.tight_layout()
    plt.savefig(results_dir/(plt_prefix+'pred_nogt.pdf'),
                bbox_inches='tight', pad_inches=0)
    plt.savefig(results_dir/(plt_prefix+'pred_nogt.png'),
                bbox_inches='tight', pad_inches=0)
//  [python]
def save_traj_plots(results_dir, f_pred_xyz, b_pred_xyz, axes=[0, 1], plt_prefix=''):
    """Plot and save trajectories to a file.

    Args:
        results_dir (str): Directory to save the plots.
        f_pred_xyz (torch.Tensor): Predicted xyz values of a trajectory.
        b_pred_xyz (torch.Tensor): Predicted inverse xyz values of a trajectory.
        axes (list, optional): Which axes to use in 2D plot. Defaults to [0, 1].
    """
    plt.figure(figsize=(16, 8))
    plt.subplot(1, 2, 1)
    ax = sn.lineplot(x=f_pred_xyz[:, axes[0]].cpu().numpy(
    ), y=f_pred_xyz[:, axes[1]].cpu().numpy(), sort=False)
    ax.set(title='Forward Trajectory', xlabel='X (m)', ylabel='Y (m)')
    plt.subplot(1, 2, 2)
    ax = sn.lineplot(x=b_pred_xyz[:, axes[0]].cpu().numpy(
    ), y=b_pred_xyz[:, axes[1]].cpu().numpy(), sort=False, markers=True)
    ax.set(title='Backward Trajectory', xlabel='X (m)', ylabel='Y (m)')

    # Save fig
    plt.tight_layout()
    plt.savefig(results_dir/(plt_prefix+'pred_nogt.pdf'),
                bbox_inches='tight', pad_inches=0)
    plt.savefig(results_dir/(plt_prefix+'pred_nogt.png'),
                bbox_inches='tight', pad_inches=0)
//  [python]
plt.xkcd()

plt.rcParams["font.family"] = "Humor Sans"

df = pd.read_csv("stats.csv", parse_dates=["date"], date_parser=dateparse)
# print(df.head())
ax = sns.pointplot(
    x=df.date,
    y=df.wc,
    data=df,
    # palette="tab10",
    linewidth=2,
    marker="o",
    color="#2d73b9",
)
//  [python]
plt.xkcd()

plt.rcParams["font.family"] = "Humor Sans"

df = pd.read_csv("stats.csv", parse_dates=["date"], date_parser=dateparse)
# print(df.head())
ax = sns.lineplot(
    x=df.date,
    y=df.wc,
    data=df,
    # palette="tab10",
    linewidth=2,
    marker="o",
    color="#2d73b9",
)
//  [python]
def make_grouped_sizeplots(
    *,
    df: pd.DataFrame,
    target_x_header: str,
    target_y_header: str,
    output_directory: str,
    make_png: bool = True,
    make_pdf: bool = True,
) -> None:
    it = tqdm(
        df.groupby(['dataset', 'optimizer']),
        desc=f'making sizeplots for {target_x_header}'
    )
    min_bytes = df[target_x_header].min()
    max_bytes = df[target_x_header].max()

    for (dataset, optimizer), sdf in it:
        fig, ax = plt.subplots(figsize=(8, 5))

        g = sns.scatterplot(
            x=target_x_header,
            y=target_y_header,
            data=sdf,
            alpha=0.75,
            hue='model',
            hue_order=sorted(df['model'].unique()),
            ax=ax,
        )
        try:
            skyline_df = get_skyline_df(
                sdf[sdf['dataset'] == dataset],
                columns=[target_x_header, target_y_header],
                smaller_is_better=(True, False),
            )
        except scipy.spatial.qhull.QhullError:
            it.write(f'Not enough data points to make make hull for {dataset}/{optimizer}')
        else:
            sns.pointplot(
                x=target_x_header,
                y=target_y_header,
                data=skyline_df,
                estimator=None,
                markers=True,
                legend=False,
                ax=ax,
            )

        ax.set_xlim([min_bytes, max_bytes])
        ax.set_ylim([0.0, 1.0])
        ax.set_xlabel(target_x_header.replace('_', ' ').title())
        g.set(xscale='log')
        g.legend(loc='center left', bbox_to_anchor=(1.05, 0.5), ncol=1)
        _clean_legend_title(g.legend)

        ax.set_title(f'{dataset} - {optimizer} - {target_x_header}  ({len(sdf.index)} experiments)')

        plt.tight_layout()
        if make_pdf:
            plt.savefig(os.path.join(output_directory, f'scatter_{target_x_header}_{dataset}_{optimizer}.pdf'.lower()))
        if make_png:
            plt.savefig(os.path.join(output_directory, f'scatter_{target_x_header}_{dataset}_{optimizer}.png'.lower()),
                        dpi=300)
        plt.close(fig)