jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def output_figure3():
    for alg, score in alg_score_list:
        for dataset in dataset_list:
            input_path = "out/result-all.csv"
            df = pd.read_csv(input_path)
            df = df[df["alg"] == alg]
            df = df[df["score"] == score]
            df = df[df["d"] == dataset]

            df = df[['iter', 'tpr', 'norm_shd']]
            df = df.rename(columns={"tpr": "TPR", "norm_shd": "SHD"})
            df["SHD"] = df["SHD"].round(4)
            df.to_csv(
                f"out/paper/figure3/figure3-{alg}-{score}-{dataset}.csv", index=False)
            # melt
            df_melted = df.melt(
                id_vars='iter', var_name='metric', value_name='value')
            sns.lineplot(x="iter", y="value", hue="metric",
                         data=df_melted, palette=sns.color_palette())
            plt.savefig(
                f"img/figure3/{alg}-{score}-{dataset}.png")
            plt.close()
            rprint(
                f"📊 Iter-tpr lineplot saved to [italic blue]{alg}-{score}-{dataset}.png[/italic blue]")
//  [python]
def gen_cutoff_plot(df_image, eps=0.0, ood=False, plot=True):
    print(f"Generating cutoff plot with eps={eps}, ood={ood}")

    df = df_image[(df_image["Epsilon"]==eps) & (df_image["OOD"]==ood)]
    df_pixel = df_image_to_pixels(df, keys=["Target", "Mu", "Sigma"])

    df_cutoff = pd.DataFrame(
        columns=["Method", "Model Path", "Percentile", "Error"])

    for method, model_path_list in trained_models.items():
        for model_i, model_path in enumerate(tqdm(model_path_list)):

            df_model = df_pixel[(df_pixel["Method"]==method.value) & (df_pixel["Model Path"]==model_path)]
            df_model = df_model.sort_values("Sigma", ascending=False)
            percentiles = np.arange(100)/100.
            cutoff_inds = (percentiles * df_model.shape[0]).astype(int)

            df_model["Error"] = np.abs(df_model["Mu"] - df_model["Target"])
            #df_model["Error"] = (df_model["Mu"] - df_model["Target"])**2
            mean_error = [df_model[cutoff:]["Error"].mean()
                for cutoff in cutoff_inds]
            df_single_cutoff = pd.DataFrame({'Method': method.value, 'Model Path': model_path,
                'Percentile': percentiles, 'Error': mean_error})

            df_cutoff = df_cutoff.append(df_single_cutoff)

    df_cutoff["Epsilon"] = eps

    if plot:
        print("Plotting cutoffs")
        sns.pointplot(x="Percentile", y="Error", hue="Method", data=df_cutoff)
        plt.savefig(os.path.join(output_dir, f"cutoff_eps-{eps}_ood-{ood}.pdf"))
        plt.show()

        sns.pointplot(x="Percentile", y="Error", hue="Model Path", style="Method", data=df_cutoff)
        # Put the legend out of the figure
        plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
        plt.savefig(os.path.join(output_dir, f"cutoff_eps-{eps}_ood-{ood}_trial.pdf"), bbox_inches='tight')
        plt.show()

        g = sns.FacetGrid(df_cutoff, col="Method", legend_out=False)
        g = g.map_dataframe(sns.pointplot, x="Percentile", y="Error", hue="Model Path")#.add_legend()
        plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
        plt.savefig(os.path.join(output_dir, f"cutoff_eps-{eps}_ood-{ood}_trial_panel.pdf"), bbox_inches='tight')
        plt.show()


    return df_cutoff
//  [python]
def gen_cutoff_plot(df_image, eps=0.0, ood=False, plot=True):
    print(f"Generating cutoff plot with eps={eps}, ood={ood}")

    df = df_image[(df_image["Epsilon"]==eps) & (df_image["OOD"]==ood)]
    df_pixel = df_image_to_pixels(df, keys=["Target", "Mu", "Sigma"])

    df_cutoff = pd.DataFrame(
        columns=["Method", "Model Path", "Percentile", "Error"])

    for method, model_path_list in trained_models.items():
        for model_i, model_path in enumerate(tqdm(model_path_list)):

            df_model = df_pixel[(df_pixel["Method"]==method.value) & (df_pixel["Model Path"]==model_path)]
            df_model = df_model.sort_values("Sigma", ascending=False)
            percentiles = np.arange(100)/100.
            cutoff_inds = (percentiles * df_model.shape[0]).astype(int)

            df_model["Error"] = np.abs(df_model["Mu"] - df_model["Target"])
            #df_model["Error"] = (df_model["Mu"] - df_model["Target"])**2
            mean_error = [df_model[cutoff:]["Error"].mean()
                for cutoff in cutoff_inds]
            df_single_cutoff = pd.DataFrame({'Method': method.value, 'Model Path': model_path,
                'Percentile': percentiles, 'Error': mean_error})

            df_cutoff = df_cutoff.append(df_single_cutoff)

    df_cutoff["Epsilon"] = eps

    if plot:
        print("Plotting cutoffs")
        sns.lineplot(x="Percentile", y="Error", hue="Method", data=df_cutoff)
        plt.savefig(os.path.join(output_dir, f"cutoff_eps-{eps}_ood-{ood}.pdf"))
        plt.show()

        sns.lineplot(x="Percentile", y="Error", hue="Model Path", style="Method", data=df_cutoff)
        # Put the legend out of the figure
        plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
        plt.savefig(os.path.join(output_dir, f"cutoff_eps-{eps}_ood-{ood}_trial.pdf"), bbox_inches='tight')
        plt.show()

        g = sns.FacetGrid(df_cutoff, col="Method", legend_out=False)
        g = g.map_dataframe(sns.lineplot, x="Percentile", y="Error", hue="Model Path")#.add_legend()
        plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
        plt.savefig(os.path.join(output_dir, f"cutoff_eps-{eps}_ood-{ood}_trial_panel.pdf"), bbox_inches='tight')
        plt.show()


    return df_cutoff
//  [python]
def plot_emacs(model, run_name, model_name, noise_type):
    emac = 0
    noise = 0
    emacs = []
    denominator = 0
    for name, param in model.named_parameters():
        if "noise" in name:
            mod = model
            for subname in name.split('.')[:-1]:
                mod = getattr(mod, subname)

            e = torch.exp(param)
            if mod.quantize_energy:
                e = RoundSTE.apply(e)

            if noise_type == "shot":
                noise += 1. / torch.sqrt(e).mean() * mod.total_macs
            elif noise_type == "thermal":
                noise += 1. / torch.sqrt(e).mean() * mod.total_macs / 100.
            if noise_type == "weight":
                noise += 1. / torch.sqrt(e).mean() * mod.total_macs

            emac += e.mean() * mod.total_macs
            emacs.append(e.mean().item())
            denominator += mod.total_macs
    avg_emac = (emac / denominator).item()
    avg_noise = (noise / denominator).item()

    plt.figure(figsize=(5, 3.5))
    plot = sns.tsplot(x=range(len(emacs)), y=np.array(emacs) * 0.128)
    if model_name == "bert":
        title = f"BERT Energy/MAC by Matrix Multiplication"
        xlabel = "Matrix Multiplication"
    else:
        title = f"{model_name}".capitalize() + f" Energy/MAC by Layer" 
        xlabel = "Layer"

    sns.set_color_codes()
    plot.set_title(title)
    plot.set(yscale="log")
    plot.set(xlabel=xlabel, ylabel=f"Energy/MAC (aJ)")

    plot.yaxis.set_major_formatter(ticker.ScalarFormatter())
    y_minor = ticker.LogLocator(base = 10.0, subs = np.arange(1.0, 10.0) * 0.1, numticks = 10)
    plot.get_yaxis().set_minor_locator(y_minor)
    plot.grid(b=True, which='minor', color='lightgray', axis='y', linewidth=0.5)
    plot.yaxis.set_minor_locator(y_minor)
    plot.yaxis.set_minor_formatter(ticker.NullFormatter())

    plt.savefig(f"{run_name}/emac_by_layer.pdf", bbox_inches="tight")
    plt.close()

    return avg_emac, avg_noise
//  [python]
def plot_emacs(model, run_name, model_name, noise_type):
    emac = 0
    noise = 0
    emacs = []
    denominator = 0
    for name, param in model.named_parameters():
        if "noise" in name:
            mod = model
            for subname in name.split('.')[:-1]:
                mod = getattr(mod, subname)

            e = torch.exp(param)
            if mod.quantize_energy:
                e = RoundSTE.apply(e)

            if noise_type == "shot":
                noise += 1. / torch.sqrt(e).mean() * mod.total_macs
            elif noise_type == "thermal":
                noise += 1. / torch.sqrt(e).mean() * mod.total_macs / 100.
            if noise_type == "weight":
                noise += 1. / torch.sqrt(e).mean() * mod.total_macs

            emac += e.mean() * mod.total_macs
            emacs.append(e.mean().item())
            denominator += mod.total_macs
    avg_emac = (emac / denominator).item()
    avg_noise = (noise / denominator).item()

    plt.figure(figsize=(5, 3.5))
    plot = sns.lineplot(x=range(len(emacs)), y=np.array(emacs) * 0.128)
    if model_name == "bert":
        title = f"BERT Energy/MAC by Matrix Multiplication"
        xlabel = "Matrix Multiplication"
    else:
        title = f"{model_name}".capitalize() + f" Energy/MAC by Layer" 
        xlabel = "Layer"

    sns.set_color_codes()
    plot.set_title(title)
    plot.set(yscale="log")
    plot.set(xlabel=xlabel, ylabel=f"Energy/MAC (aJ)")

    plot.yaxis.set_major_formatter(ticker.ScalarFormatter())
    y_minor = ticker.LogLocator(base = 10.0, subs = np.arange(1.0, 10.0) * 0.1, numticks = 10)
    plot.get_yaxis().set_minor_locator(y_minor)
    plot.grid(b=True, which='minor', color='lightgray', axis='y', linewidth=0.5)
    plot.yaxis.set_minor_locator(y_minor)
    plot.yaxis.set_minor_formatter(ticker.NullFormatter())

    plt.savefig(f"{run_name}/emac_by_layer.pdf", bbox_inches="tight")
    plt.close()

    return avg_emac, avg_noise
//  [python]
def _add_lineplot(plot_df: pd.DataFrame, ax: Any, color_list: list[str]) -> None:
    """Adds trace plot to plotting grid."""

    sns.tsplot(
        data=plot_df,
        time="iteration",
        value="value",
        condition="chain_index",
        color=color_list,
        ax=ax,
        legend="full",
    ).set(xlabel="Iteration", ylabel="")
//  [python]
def _add_lineplot(plot_df: pd.DataFrame, ax: Any, color_list: list[str]) -> None:
    """Adds trace plot to plotting grid."""

    sns.lineplot(
        data=plot_df,
        x="iteration",
        y="value",
        hue="chain_index",
        palette=color_list,
        ax=ax,
        legend="full",
    ).set(xlabel="Iteration", ylabel="")
//  [python]
def save_scores(log_dir, n_iter, scores):
    """Save scores to file system.

    Save scores to file system as a csv file.

    Args:
        log_dir: str. Path to log directory.
        n_iter: int. Current iteration number.
        scores: np.array. Scores.
    """
    # save scores for analysis in the future
    filename = os.path.join(log_dir, 'scores.csv')
    df = pd.DataFrame({'Time': [int(time.time())] * scores.size,
                       'Iteration': [n_iter] * scores.size,
                       'Reward': scores})
    need_header = not os.path.exists(filename)
    df.to_csv(filename, mode='a', header=need_header, index=False)
    # draw graphs
    df = pd.read_csv(filename)
    sns.set()
    # plot 1: reward vs iteration
    sns_plot = sns.pointplot(x='Iteration', y='Reward', data=df, ci='sd')
    im_filename = os.path.join(log_dir, 'reward_vs_iteration.png')
    sns_plot.get_figure().savefig(im_filename)
    plt.clf()
    # plot 2: reward vs time
    start_time = df.Time.values[0]
    df.Time = (df.Time - start_time) / 3600  # convert to hours
    sns_plot = sns.pointplot(x='Time', y='Reward', data=df, ci='sd')
    sns_plot.set_xlabel('Time (hour)')
    im_filename = os.path.join(log_dir, 'reward_vs_time.png')
    sns_plot.get_figure().savefig(im_filename)
    plt.clf()
//  [python]
def save_scores(log_dir, n_iter, scores):
    """Save scores to file system.

    Save scores to file system as a csv file.

    Args:
        log_dir: str. Path to log directory.
        n_iter: int. Current iteration number.
        scores: np.array. Scores.
    """
    # save scores for analysis in the future
    filename = os.path.join(log_dir, 'scores.csv')
    df = pd.DataFrame({'Time': [int(time.time())] * scores.size,
                       'Iteration': [n_iter] * scores.size,
                       'Reward': scores})
    need_header = not os.path.exists(filename)
    df.to_csv(filename, mode='a', header=need_header, index=False)
    # draw graphs
    df = pd.read_csv(filename)
    sns.set()
    # plot 1: reward vs iteration
    sns_plot = sns.lineplot(x='Iteration', y='Reward', data=df, ci='sd')
    im_filename = os.path.join(log_dir, 'reward_vs_iteration.png')
    sns_plot.get_figure().savefig(im_filename)
    plt.clf()
    # plot 2: reward vs time
    start_time = df.Time.values[0]
    df.Time = (df.Time - start_time) / 3600  # convert to hours
    sns_plot = sns.lineplot(x='Time', y='Reward', data=df, ci='sd')
    sns_plot.set_xlabel('Time (hour)')
    im_filename = os.path.join(log_dir, 'reward_vs_time.png')
    sns_plot.get_figure().savefig(im_filename)
    plt.clf()
//  [python]
def plot_data(
    data,
    xaxis="Epoch",
    value="AverageEpRet",
    condition="Condition1",
    smooth=1,
    **kwargs
):
    if smooth > 1:
        """
        smooth data with moving window average.
        that is,
            smoothed_y[t] = average(y[t-k], y[t-k+1], ..., y[t+k-1], y[t+k])
        where the "smooth" param is width of that window (2k+1)
        """
        y = np.ones(smooth)
        for datum in data:
            x = np.asarray(datum[value])
            z = np.ones(len(x))
            smoothed_x = np.convolve(x, y, "same") / np.convolve(z, y, "same")
            datum[value] = smoothed_x

    if isinstance(data, list):
        data = pd.concat(data, ignore_index=True)
    sns.set(style="darkgrid", font_scale=1.5)
    sns.tsplot(data=data, time=xaxis, value=value, unit=condition, ci="sd", **kwargs)
    plt.legend(
        loc="upper center", ncol=3, handlelength=1, borderaxespad=0.0, prop={"size": 13}
    ).set_draggable(True)

    xscale = np.max(np.asarray(data[xaxis])) > 5e3
    if xscale:
        # Just some formatting niceness: x-axis scale in scientific notation if max x is large
        plt.ticklabel_format(style="sci", axis="x", scilimits=(0, 0))

    plt.tight_layout(pad=0.5)
//  [python]
def plot_data(
    data,
    xaxis="Epoch",
    value="AverageEpRet",
    condition="Condition1",
    smooth=1,
    **kwargs
):
    if smooth > 1:
        """
        smooth data with moving window average.
        that is,
            smoothed_y[t] = average(y[t-k], y[t-k+1], ..., y[t+k-1], y[t+k])
        where the "smooth" param is width of that window (2k+1)
        """
        y = np.ones(smooth)
        for datum in data:
            x = np.asarray(datum[value])
            z = np.ones(len(x))
            smoothed_x = np.convolve(x, y, "same") / np.convolve(z, y, "same")
            datum[value] = smoothed_x

    if isinstance(data, list):
        data = pd.concat(data, ignore_index=True)
    sns.set(style="darkgrid", font_scale=1.5)
    sns.lineplot(data=data, x=xaxis, y=value, hue=condition, ci="sd", **kwargs)
    plt.legend(
        loc="upper center", ncol=3, handlelength=1, borderaxespad=0.0, prop={"size": 13}
    ).set_draggable(True)

    xscale = np.max(np.asarray(data[xaxis])) > 5e3
    if xscale:
        # Just some formatting niceness: x-axis scale in scientific notation if max x is large
        plt.ticklabel_format(style="sci", axis="x", scilimits=(0, 0))

    plt.tight_layout(pad=0.5)
//  [python]
def _add_corplot(
    plot_df: pd.DataFrame, ax: Any, max_lags: int | None, color_list: list[str]
) -> None:
    """Adds correlation plot to plotting grid."""

    max_lags = _compute_max_lags(plot_df, max_lags)

    for chain_index, col in zip(plot_df["chain_index"].unique(), color_list):
        x = np.asarray(plot_df.loc[plot_df["chain_index"] == chain_index]["value"])
        acor = arviz.autocorr(x)[0:max_lags]

        sns.plt.plot(
            range(max_lags),
            acor,
            marker="",
            linestyle="-",
            color=col,
            ax=ax,
        )

        ax.set(xlim=(0, max_lags), ylim=(-0.2, 1.1), xlabel="Lag", ylabel="")
//  [python]
def _add_corplot(
    plot_df: pd.DataFrame, ax: Any, max_lags: int | None, color_list: list[str]
) -> None:
    """Adds correlation plot to plotting grid."""

    max_lags = _compute_max_lags(plot_df, max_lags)

    for chain_index, col in zip(plot_df["chain_index"].unique(), color_list):
        x = np.asarray(plot_df.loc[plot_df["chain_index"] == chain_index]["value"])
        acor = arviz.autocorr(x)[0:max_lags]

        sns.lineplot(
            x=range(max_lags),
            y=acor,
            marker="",
            linestyle="-",
            color=col,
            ax=ax,
        )

        ax.set(xlim=(0, max_lags), ylim=(-0.2, 1.1), xlabel="Lag", ylabel="")
//  [python]
def plot_tail(alpha, accuracy, model_name='default'): 
    data = {'Tail size': alpha, model_name: accuracy}
    df = pd.DataFrame(data, dtype=float)
    df = pd.melt(df, ['Tail size'], var_name="Models", value_name="Accuracy")
    ax = sns.pointplot(x="Tail size", y="Accuracy", hue="Models", data=df, ci=None)
    plt.xscale('log')
    plt.ylim(0, 100)
    plt.savefig('plot/tail_plot_%s.pdf'%model_name)
    plt.close()
//  [python]
def plot_tail(alpha, accuracy, model_name='default'): 
    data = {'Tail size': alpha, model_name: accuracy}
    df = pd.DataFrame(data, dtype=float)
    df = pd.melt(df, ['Tail size'], var_name="Models", value_name="Accuracy")
    ax = sns.lineplot(x="Tail size", y="Accuracy", hue="Models", style="Models", data=df, markers=False, ci=None)
    plt.xscale('log')
    plt.ylim(0, 100)
    plt.savefig('plot/tail_plot_%s.pdf'%model_name)
    plt.close()
//  [python]
n_contracts = [10, 100, 1000, 10000, 100000, 1000000, 10000000]
timings = []

for N in n_contracts:
    # time_apply, time_forloop, time_iterrows, time_listcomp, time_vectorized = [], [], [], [], []
    for repetition in range(10):
        time_apply = (pd_apply(N))
        time_forloop =(for_loop(N))
        time_iterrows = (iterrows(N))
        time_listcomp = (listcomp(N))
        time_vectorized = (vectorized(N))
        timings.append((N, time_apply, time_forloop, time_iterrows, time_listcomp, time_vectorized))

df = pd.DataFrame(timings, columns=["N", "apply", "forloop", "iterrows", "listcomp", "vectorized (this library)"])
g = sns.pointplot(data=df.melt(id_vars="N", var_name="method"), x="N", y="value", hue="method")
//  [python]
n_contracts = [10, 100, 1000, 10000, 100000, 1000000, 10000000]
timings = []

for N in n_contracts:
    # time_apply, time_forloop, time_iterrows, time_listcomp, time_vectorized = [], [], [], [], []
    for repetition in range(10):
        time_apply = (pd_apply(N))
        time_forloop =(for_loop(N))
        time_iterrows = (iterrows(N))
        time_listcomp = (listcomp(N))
        time_vectorized = (vectorized(N))
        timings.append((N, time_apply, time_forloop, time_iterrows, time_listcomp, time_vectorized))

df = pd.DataFrame(timings, columns=["N", "apply", "forloop", "iterrows", "listcomp", "vectorized (this library)"])
g = sns.lineplot(data=df.melt(id_vars="N", var_name="method"), x="N", y="value", hue="method", markers=True)
//  [python]
def supplementary_longitudinal_outcomes(demographics):
    """ For supplementary longitudinal outcome figures """

    behavior = load_longitudinal_behavior(demographics.index)
    fig, axes = plt.subplots(1, 2, figsize=(9, 4))
    fig.subplots_adjust(wspace=0.3)

    for n, (lp, ax) in enumerate(zip(['pigd', 'tremor'], axes)):
        # get data for given test
        pd_test = pd.merge(clean_visits(behavior, lp), demographics,
                           on='participant')
        # normalize data based on first visit mean/stdev
        t = pd_test.query('visit == 0')[lp]
        pd_test.loc[:, lp] -= t.mean()
        pd_test.loc[:, lp] /= t.std(ddof=1)
        # plot it
        sns.pointplot(x='visit', y=lp, data=pd_test.dropna(subset=[lp]),
                     hue='cluster', palette=defaults.three_cluster_cmap,
                     join=True, legend=False, ci=68, ax=ax)
        ax.set(xticklabels=[], xlabel='time')
        if n == 0:
            ax.set_ylabel('clinical severity', labelpad=10)
            ax.set(yticks=[-0.5, 0.5, 1.5, 2.5],
                   yticklabels=[-0.5, 0.5, 1.5, 2.5])
        else:
            ax.set(ylabel='',
                   yticks=[-0.75, 0, 0.75],
                   yticklabels=[-0.75, 0, 0.75])
        ax.set_title(lp, pad=15)
        sns.despine(ax=ax)

    return fig
//  [python]
def supplementary_longitudinal_outcomes(demographics):
    """ For supplementary longitudinal outcome figures """

    behavior = load_longitudinal_behavior(demographics.index)
    fig, axes = plt.subplots(1, 2, figsize=(9, 4))
    fig.subplots_adjust(wspace=0.3)

    for n, (lp, ax) in enumerate(zip(['pigd', 'tremor'], axes)):
        # get data for given test
        pd_test = pd.merge(clean_visits(behavior, lp), demographics,
                           on='participant')
        # normalize data based on first visit mean/stdev
        t = pd_test.query('visit == 0')[lp]
        pd_test.loc[:, lp] -= t.mean()
        pd_test.loc[:, lp] /= t.std(ddof=1)
        # plot it
        sns.lineplot(x='visit', y=lp, data=pd_test.dropna(subset=[lp]),
                     hue='cluster', palette=defaults.three_cluster_cmap,
                     legend=False, ci=68, ax=ax)
        ax.set(xticklabels=[], xlabel='time')
        if n == 0:
            ax.set_ylabel('clinical severity', labelpad=10)
            ax.set(yticks=[-0.5, 0.5, 1.5, 2.5],
                   yticklabels=[-0.5, 0.5, 1.5, 2.5])
        else:
            ax.set(ylabel='',
                   yticks=[-0.75, 0, 0.75],
                   yticklabels=[-0.75, 0, 0.75])
        ax.set_title(lp, pad=15)
        sns.despine(ax=ax)

    return fig
//  [python]
def draw_measurement_count(labels):
    """
    Visualize the relationship between measurement counts and object-ego vehicle distance

    Parameters
    ----------
    labels: dict of pandas.DataFrame
        labels of the extracted dataset

    Returns
    ----------
    None

    """
    df_distance_count = []

    for category in labels:
        df_ = labels[category].loc[:, ['num_measurements']]
        df_['distance'] = np.sqrt(labels[category]['vx']**2 + labels[category]['vy']**2)
        df_['category'] = category

        df_distance_count.append(df_)
    
    df_distance_count = pd.concat(df_distance_count)

    # reduce resolution:
    df_distance_count['distance'] = df_distance_count['distance'].apply(np.round)

    # overview:
    plt.figure(num=None, figsize=(16, 9))

    sns.pointplot(
        x="distance", y="num_measurements",
        hue="category",
        markers=["o", "x", "s", "D"], linestyles=["-", "--", "-.", ":"], data=df_distance_count
    )

    plt.title('Num. Measurements as a function of Object Distance')
    plt.show()

    # ROI selection:
    plt.figure(num=None, figsize=(16, 9))

    sns.pointplot(
        x="distance", y="num_measurements",
        hue="category",
        markers=["o", "x", "s", "D"], linestyles=["-", "--", "-.", ":"], data=df_distance_count.loc[
            (df_distance_count["distance"] >= 20.0) & (df_distance_count["distance"] <= 45.0),
            df_distance_count.columns
        ]
    )

    plt.title('Num. Measurements as a function of Object Distance, ROI Selection')
    plt.show()
//  [python]
def draw_measurement_count(labels):
    """
    Visualize the relationship between measurement counts and object-ego vehicle distance

    Parameters
    ----------
    labels: dict of pandas.DataFrame
        labels of the extracted dataset

    Returns
    ----------
    None

    """
    df_distance_count = []

    for category in labels:
        df_ = labels[category].loc[:, ['num_measurements']]
        df_['distance'] = np.sqrt(labels[category]['vx']**2 + labels[category]['vy']**2)
        df_['category'] = category

        df_distance_count.append(df_)
    
    df_distance_count = pd.concat(df_distance_count)

    # reduce resolution:
    df_distance_count['distance'] = df_distance_count['distance'].apply(np.round)

    # overview:
    plt.figure(num=None, figsize=(16, 9))

    sns.lineplot(
        x="distance", y="num_measurements",
        hue="category", style="category",
        markers=True, dashes=False, data=df_distance_count
    )

    plt.title('Num. Measurements as a function of Object Distance')
    plt.show()

    # ROI selection:
    plt.figure(num=None, figsize=(16, 9))

    sns.lineplot(
        x="distance", y="num_measurements",
        hue="category", style="category",
        markers=True, dashes=False, data=df_distance_count.loc[
            (df_distance_count["distance"] >= 20.0) & (df_distance_count["distance"] <= 45.0),
            df_distance_count.columns
        ]
    )

    plt.title('Num. Measurements as a function of Object Distance, ROI Selection')
    plt.show()
//  [python]
def draw_boxplot(data, **kw):
    df = data[["Time", "Count"]]
    seaborn.pointplot(x=df["Count"], y=df["Time"])
//  [python]
def draw_boxplot(data, **kw):
    df = data[["Time", "Count"]]
    seaborn.lineplot(x=df["Count"], y=df["Time"])
//  [python]
def save_training_history(self, path):
        if self.trained:
            sns.set()
            fig = pyplot.figure(0, (12, 4))

            ax = pyplot.subplot(1, 2, 1)
            sns.plt.plot(self.history.epoch, self.history.history['accuracy'], label='train')
            try:
                sns.plt.plot(self.history.epoch, self.history.history['val_accuracy'], label='valid')
            except KeyError:
                pass
            pyplot.title('Accuracy')
            pyplot.tight_layout()

            ax = pyplot.subplot(1, 2, 2)
            sns.plt.plot(self.history.epoch, self.history.history['loss'], label='train')
            try:
                sns.plt.plot(self.history.epoch, self.history.history['val_loss'], label='valid')
            except KeyError:
                pass
            pyplot.title('Loss')
            pyplot.tight_layout()
            pyplot.savefig(path)
        else:
            raise ValueError("Model is not trained yet, call train first")
//  [python]
def save_training_history(self, path):
        if self.trained:
            sns.set()
            fig = pyplot.figure(0, (12, 4))

            ax = pyplot.subplot(1, 2, 1)
            sns.lineplot(self.history.epoch, self.history.history['accuracy'], label='train')
            try:
                sns.lineplot(self.history.epoch, self.history.history['val_accuracy'], label='valid')
            except KeyError:
                pass
            pyplot.title('Accuracy')
            pyplot.tight_layout()

            ax = pyplot.subplot(1, 2, 2)
            sns.lineplot(self.history.epoch, self.history.history['loss'], label='train')
            try:
                sns.lineplot(self.history.epoch, self.history.history['val_loss'], label='valid')
            except KeyError:
                pass
            pyplot.title('Loss')
            pyplot.tight_layout()
            pyplot.savefig(path)
        else:
            raise ValueError("Model is not trained yet, call train first")
//  [python]
def plot_core_micro(fname):
    """
    A very simple visualization of the results..
    """
    p1 = sns.tsplot('percent_train', 'micro_F', hue='setting', data=fname)
    plt.show()

    return 1
//  [python]
def plot_core_micro(fname):
    """
    A very simple visualization of the results..
    """
    p1 = sns.lineplot('percent_train', 'micro_F', hue='setting', data=fname)
    plt.show()

    return 1
//  [python]
def plot_tv_loss(hist, epochs, path_fl_sav):

	#---------------------;
	#  Plot loss ('mse')  ;
	#---------------------;
	legend_properties = {'weight':'bold'}
	fig               = plt.figure()
	#
	#plt.rc('text', usetex = True)
	plt.rcParams['font.family']     = ['sans-serif']
	plt.rcParams['font.sans-serif'] = ['Lucida Grande']
	plt.rc('legend', fontsize = 14)
	ax = fig.add_subplot(111)
	ax.set_xlabel('Epoch', fontsize = 24, fontweight = 'bold')
	ax.set_ylabel('Loss (MSE)', fontsize = 24, fontweight = 'bold')
	#plt.grid(True)
	ax.tick_params(axis = 'both', which = 'major', labelsize = 20, length = 6, width = 2)
	ax.set_xlim([0, epochs])
	#ax.set_ylim([0.35, 1])
	e_list = [i for i in range(0,epochs)]
	plt.plot(e_list, hist['loss'], linestyle = 'solid', linewidth = 1.5, \
					color = 'b', label = 'Training') #Training loss
	plt.plot(e_list, hist['val_loss'], linestyle = 'solid', linewidth = 1.5, \
					color = 'm', label = 'Validation') #Validation loss
	#tick_spacing = 100
	#ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
	ax.legend(loc = 'upper right')
	fig.tight_layout()
	#fig.savefig(path_fl_sav + 'Loss.pdf')
	fig.savefig(path_fl_sav + 'Loss.png')
	plt.close(fig)
//  [python]
def plot_tv_loss(hist, epochs, path_fl_sav):

	#---------------------;
	#  Plot loss ('mse')  ;
	#---------------------;
	legend_properties = {'weight':'bold'}
	fig               = plt.figure()
	#
	#plt.rc('text', usetex = True)
	plt.rcParams['font.family']     = ['sans-serif']
	plt.rcParams['font.sans-serif'] = ['Lucida Grande']
	plt.rc('legend', fontsize = 14)
	ax = fig.add_subplot(111)
	ax.set_xlabel('Epoch', fontsize = 24, fontweight = 'bold')
	ax.set_ylabel('Loss (MSE)', fontsize = 24, fontweight = 'bold')
	#plt.grid(True)
	ax.tick_params(axis = 'both', which = 'major', labelsize = 20, length = 6, width = 2)
	ax.set_xlim([0, epochs])
	#ax.set_ylim([0.35, 1])
	e_list = [i for i in range(0,epochs)]
	sns.lineplot(e_list, hist['loss'], linestyle = 'solid', linewidth = 1.5, \
					color = 'b', label = 'Training') #Training loss
	sns.lineplot(e_list, hist['val_loss'], linestyle = 'solid', linewidth = 1.5, \
					color = 'm', label = 'Validation') #Validation loss
	#tick_spacing = 100
	#ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
	ax.legend(loc = 'upper right')
	fig.tight_layout()
	#fig.savefig(path_fl_sav + 'Loss.pdf')
	fig.savefig(path_fl_sav + 'Loss.png')
	plt.close(fig)
//  [python]
def plot_gt_pred(x, y, param_id, fl_name, str_x_label, str_y_label):

	#------------------------------------------------;
	#  Plot one-to-one (ground truth vs. predicted)  ;
	#------------------------------------------------;
	legend_properties = {'weight':'bold'}
	fig               = plt.figure()
	#
	#plt.rc('text', usetex = True)   
	plt.rcParams['font.family']     = ['sans-serif']
	plt.rcParams['font.sans-serif'] = ['Lucida Grande']
	plt.rc('legend', fontsize = 14)
	ax = fig.add_subplot(111)
	ax.set_xlabel(str_x_label, fontsize = 14, fontweight = 'bold')
	ax.set_ylabel(str_y_label, fontsize = 14, fontweight = 'bold')
	#plt.grid(True)
	ax.tick_params(axis = 'both', which = 'major', labelsize = 20, length = 6, width = 2)
	min_val = np.min(x)
	max_val = np.max(x)
	ax.set_xlim([min_val, max_val])
	ax.set_ylim([min_val, max_val])
	plt.plot([min_val, max_val], [min_val, max_val], \
					linestyle = 'solid', linewidth = 1.5, \
					color = 'r') #One-to-One line
	plt.scatter(x, y, color = 'b', marker = 'o')
	#tick_spacing = 100
	#ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
	#ax.legend(loc = 'upper right')
	ax.set_aspect(1./ax.get_data_ratio())
	fig.tight_layout()
	#fig.savefig(fl_name + str(param_id) + '.pdf')
	fig.savefig(fl_name + str(param_id) + '.png')
	plt.close(fig)
//  [python]
def plot_gt_pred(x, y, param_id, fl_name, str_x_label, str_y_label):

	#------------------------------------------------;
	#  Plot one-to-one (ground truth vs. predicted)  ;
	#------------------------------------------------;
	legend_properties = {'weight':'bold'}
	fig               = plt.figure()
	#
	#plt.rc('text', usetex = True)   
	plt.rcParams['font.family']     = ['sans-serif']
	plt.rcParams['font.sans-serif'] = ['Lucida Grande']
	plt.rc('legend', fontsize = 14)
	ax = fig.add_subplot(111)
	ax.set_xlabel(str_x_label, fontsize = 14, fontweight = 'bold')
	ax.set_ylabel(str_y_label, fontsize = 14, fontweight = 'bold')
	#plt.grid(True)
	ax.tick_params(axis = 'both', which = 'major', labelsize = 20, length = 6, width = 2)
	min_val = np.min(x)
	max_val = np.max(x)
	ax.set_xlim([min_val, max_val])
	ax.set_ylim([min_val, max_val])
	sns.lineplot([min_val, max_val], [min_val, max_val], \
					linestyle = 'solid', linewidth = 1.5, \
					color = 'r') #One-to-One line
	sns.scatterplot(x, y, color = 'b', marker = 'o')
	#tick_spacing = 100
	#ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
	#ax.legend(loc = 'upper right')
	ax.set_aspect(1./ax.get_data_ratio())
	fig.tight_layout()
	#fig.savefig(fl_name + str(param_id) + '.pdf')
	fig.savefig(fl_name + str(param_id) + '.png')
	plt.close(fig)
//  [python]
def plot_train_valid_loss(self, surfix_log):
        train_loss = pd.read_csv(self.model_dir + f"train{surfix_log}.csv")
        valid_loss = pd.read_csv(self.model_dir + f"valid{surfix_log}.csv")
        plt.plot(train_loss["epoch"], train_loss["loss"], label="train loss")
        plt.plot(valid_loss["epoch"], valid_loss["loss"], label="valid loss")
        plt.title("epoch vs train loss vs valid loss")
        plt.legend()
        plt.savefig(self.model_dir + "train_valid_loss.png")
        plt.show()
        print("plot done")
//  [python]
def plot_train_valid_loss(self, surfix_log):
        train_loss = pd.read_csv(self.model_dir + f"train{surfix_log}.csv")
        valid_loss = pd.read_csv(self.model_dir + f"valid{surfix_log}.csv")
        sns.lineplot(x="epoch", y="loss", data=train_loss, label="train loss")
        sns.lineplot(x="epoch", y="loss", data=valid_loss, label="valid loss")
        plt.title("epoch vs train loss vs valid loss")
        plt.legend()
        plt.savefig(self.model_dir + "train_valid_loss.png")
        plt.show()
        print("plot done")
//  [python]
for key, value in fontsizes.icml2022().items():
    double_[key] = 1.8 * value
plt.rcParams.update(double_)

# %%

filename = f"ablation_time.pdf"
fig, ax = plt.subplots()
for (direction, model_name), df_g in df_plot.groupby(["Direction", "Model"]):
    print(f"Direction: {direction} Model: {model_name}")
    color = script_help.select_color(model_name)
    linestyle = script_help.select_style(direction)
    marker = script_help.select_marker(model_name)
    x_ticks = sorted(df_g[x].unique())
    df_g[x] = df_g[x].map({x_ticks[i]: i for i in range(len(x_ticks))})

    sns.pointplot(
        data=df_g,
        x=x,
        y=y,
        color=color,
        linestyle=linestyle,
        marker=marker,
        markerfacecolor=color,
        markeredgecolor="white" if marker == "*" else color,
        markersize=15 if marker == "*" else None,
        markeredgewidth=1.0 if marker == "*" else None,
    )
    ax.grid(True)
    ax.set_xticks(list(range(len(x_ticks))))
    ax.set_xticklabels(x_ticks)
//  [python]
for key, value in fontsizes.icml2022().items():
    double_[key] = 1.8 * value
plt.rcParams.update(double_)

# %%

filename = f"ablation_time.pdf"
fig, ax = plt.subplots()
for (direction, model_name), df_g in df_plot.groupby(["Direction", "Model"]):
    print(f"Direction: {direction} Model: {model_name}")
    color = script_help.select_color(model_name)
    linestyle = script_help.select_style(direction)
    marker = script_help.select_marker(model_name)
    x_ticks = sorted(df_g[x].unique())
    df_g[x] = df_g[x].map({x_ticks[i]: i for i in range(len(x_ticks))})

    sns.lineplot(
        data=df_g,
        x=x,
        y=y,
        color=color,
        linestyle=linestyle,
        marker=marker,
        markerfacecolor=color,
        markeredgecolor="white" if marker == "*" else color,
        markersize=15 if marker == "*" else None,
        markeredgewidth=1.0 if marker == "*" else None,
    )
    ax.grid(True)
    ax.set_xticks(list(range(len(x_ticks))))
    ax.set_xticklabels(x_ticks)
//  [python]
def main():
    plt.figure()
    i = 1
    # add event files
    data = {"Config": [], "Epoch": [], "Dice": []}
    for site in range(site_num):
        # clear data for each site
        site = site + 1
        data = {"Config": [], "Epoch": [], "Dice": []}
        for config, exp in experiments.items():
            job_id = find_job_id(workdir=client_results_root + "/site-1", fl_app_name=config)
            print(f"Found run {job_id} for {config}")
            spec_site = exp.get("site", None)
            if spec_site is not None:
                record_path = os.path.join(client_results_root, site_pre + spec_site, job_id, "*", "events.*")
            else:
                record_path = os.path.join(client_results_root, site_pre + str(site), job_id, "*", "events.*")
            eventfile = glob.glob(record_path, recursive=True)
            assert len(eventfile) == 1, "No unique event file found!"
            eventfile = eventfile[0]
            print("adding", eventfile)
            add_eventdata(data, config, eventfile, tag=exp["tag"])

        ax = plt.subplot(1, site_num, i)
        ax.set_title(site)
        sns.tsplot(x="Epoch", y="Dice", hue="Config", data=data)
        i = i + 1
    plt.subplots_adjust(hspace=0.3)
    plt.show()
//  [python]
def main():
    plt.figure()
    i = 1
    # add event files
    data = {"Config": [], "Epoch": [], "Dice": []}
    for site in range(site_num):
        # clear data for each site
        site = site + 1
        data = {"Config": [], "Epoch": [], "Dice": []}
        for config, exp in experiments.items():
            job_id = find_job_id(workdir=client_results_root + "/site-1", fl_app_name=config)
            print(f"Found run {job_id} for {config}")
            spec_site = exp.get("site", None)
            if spec_site is not None:
                record_path = os.path.join(client_results_root, site_pre + spec_site, job_id, "*", "events.*")
            else:
                record_path = os.path.join(client_results_root, site_pre + str(site), job_id, "*", "events.*")
            eventfile = glob.glob(record_path, recursive=True)
            assert len(eventfile) == 1, "No unique event file found!"
            eventfile = eventfile[0]
            print("adding", eventfile)
            add_eventdata(data, config, eventfile, tag=exp["tag"])

        ax = plt.subplot(1, site_num, i)
        ax.set_title(site)
        sns.lineplot(x="Epoch", y="Dice", hue="Config", data=data)
        i = i + 1
    plt.subplots_adjust(hspace=0.3)
    plt.show()
//  [python]
def show_training_progress(df, hue='model', metric='NDCG', name="epoch_vs_ndcg", save=True):
    fig, ax = plt.subplots(figsize=(8, 4))
    #plt.axhline(y=0.165, color='r', linestyle='-')
    ax = sns.tsplot(x='epoch', y=metric, unit="model", condition=hue, data=df)
    plt.tight_layout()
    if save:
        fig_path = load_yaml('config/global.yml', key='path')['figs']
        fig.savefig('{0}/train/progress/{1}.png'.format(fig_path, name),
                    bbox_inches="tight", pad_inches=0, format='png')
    else:
        plt.show()
//  [python]
def show_training_progress(df, hue='model', metric='NDCG', name="epoch_vs_ndcg", save=True):
    fig, ax = plt.subplots(figsize=(8, 4))
    #plt.axhline(y=0.165, color='r', linestyle='-')
    ax = sns.lineplot(x='epoch', y=metric, hue=hue, style=hue, data=df)
    plt.tight_layout()
    if save:
        fig_path = load_yaml('config/global.yml', key='path')['figs']
        fig.savefig('{0}/train/progress/{1}.png'.format(fig_path, name),
                    bbox_inches="tight", pad_inches=0, format='png')
    else:
        plt.show()
//  [python]
def main(statsfile, iterations, outfile=None):
    df = parse_stats_file(statsfile)
    fig, ax = plt.subplots(nrows=2, figsize=(16, 16))

    seaborn.pointplot(x="batchsize", y="ms", hue="kind", data=df, ax=ax[0]).set(
        title=f"Mean execution time by batch size (concretized), its={iterations}",
        ylabel="milliseconds",
        ylim=(0, 0.5),
    )

    df["ms"] = df["ms"] * df["batchsize"]

    #    seaborn.factorplot(x="batchsize", y="ms", col="kind", data=df, kind="bar").set(
    seaborn.pointplot(x="batchsize", y="ms", hue="kind", data=df, ax=ax[1]).set(
        title=f"Total execution time by batch size (concretized), its={iterations}",
        ylabel="milliseconds",
        ylim=(0, 3),
    )

    if outfile:
        plt.savefig(outfile)

    else:
        plt.show()
//  [python]
def main(statsfile, iterations, outfile=None):
    df = parse_stats_file(statsfile)
    fig, ax = plt.subplots(nrows=2, figsize=(16, 16))

    seaborn.lineplot(x="batchsize", y="ms", hue="kind", data=df, ax=ax[0]).set(
        title=f"Mean execution time by batch size (concretized), its={iterations}",
        ylabel="milliseconds",
        ylim=(0, 0.5),
    )

    df["ms"] = df["ms"] * df["batchsize"]

    #    seaborn.factorplot(x="batchsize", y="ms", col="kind", data=df, kind="bar").set(
    seaborn.lineplot(x="batchsize", y="ms", hue="kind", data=df, ax=ax[1]).set(
        title=f"Total execution time by batch size (concretized), its={iterations}",
        ylabel="milliseconds",
        ylim=(0, 3),
    )

    if outfile:
        plt.savefig(outfile)

    else:
        plt.show()
//  [python]
def plot(self, round_to=None):
        """Plot results

        :param round_to:
            Number of decimals used to round results. Defaults to 2. Use "None" to return raw numbers.
        """
        fig, ax = plt.subplots()

        # Plot raw data
        sns.tsplot(
            self.data,
            time=self.time_variable_name,
            value=self.outcome_variable_name,
            unit="unit",
            condition="group",
            alpha=0.25,
            ax=ax,
        )
        # Plot model fit to control group
        ax.plot(
            self.x_pred_control[self.time_variable_name],
            self.y_pred_control,
            "o",
            c="C0",
            markersize=10,
            label="model fit (control group)",
        )
        # Plot model fit to treatment group
        ax.plot(
            self.x_pred_treatment[self.time_variable_name],
            self.y_pred_treatment,
            "o",
            c="C1",
            markersize=10,
            label="model fit (treament group)",
        )
        # Plot counterfactual - post-test for treatment group IF no treatment
        # had occurred.
        ax.plot(
            self.x_pred_counterfactual[self.time_variable_name],
            self.y_pred_counterfactual,
            "go",
            markersize=10,
            label="counterfactual",
        )
        # arrow to label the causal impact
        ax.annotate(
            "",
            xy=(1.05, self.y_pred_counterfactual),
            xycoords="data",
            xytext=(1.05, self.y_pred_treatment[1]),
            textcoords="data",
            arrowprops={"arrowstyle": "<->", "color": "green", "lw": 3},
        )
        ax.annotate(
            "causal\nimpact",
            xy=(
                1.05,
                np.mean([self.y_pred_counterfactual[0], self.y_pred_treatment[1]]),
            ),
            xycoords="data",
            xytext=(5, 0),
            textcoords="offset points",
            color="green",
            va="center",
        )
        # formatting
        ax.set(
            xlim=[-0.05, 1.1],
            xticks=[0, 1],
            xticklabels=["pre", "post"],
            title=f"Causal impact = {round_num(self.causal_impact[0], round_to)}",
        )
        ax.legend(fontsize=LEGEND_FONT_SIZE)
        return (fig, ax)
//  [python]
def plot(self, round_to=None):
        """Plot results

        :param round_to:
            Number of decimals used to round results. Defaults to 2. Use "None" to return raw numbers.
        """
        fig, ax = plt.subplots()

        # Plot raw data
        sns.lineplot(
            self.data,
            x=self.time_variable_name,
            y=self.outcome_variable_name,
            hue="group",
            units="unit",
            estimator=None,
            alpha=0.25,
            ax=ax,
        )
        # Plot model fit to control group
        ax.plot(
            self.x_pred_control[self.time_variable_name],
            self.y_pred_control,
            "o",
            c="C0",
            markersize=10,
            label="model fit (control group)",
        )
        # Plot model fit to treatment group
        ax.plot(
            self.x_pred_treatment[self.time_variable_name],
            self.y_pred_treatment,
            "o",
            c="C1",
            markersize=10,
            label="model fit (treament group)",
        )
        # Plot counterfactual - post-test for treatment group IF no treatment
        # had occurred.
        ax.plot(
            self.x_pred_counterfactual[self.time_variable_name],
            self.y_pred_counterfactual,
            "go",
            markersize=10,
            label="counterfactual",
        )
        # arrow to label the causal impact
        ax.annotate(
            "",
            xy=(1.05, self.y_pred_counterfactual),
            xycoords="data",
            xytext=(1.05, self.y_pred_treatment[1]),
            textcoords="data",
            arrowprops={"arrowstyle": "<->", "color": "green", "lw": 3},
        )
        ax.annotate(
            "causal\nimpact",
            xy=(
                1.05,
                np.mean([self.y_pred_counterfactual[0], self.y_pred_treatment[1]]),
            ),
            xycoords="data",
            xytext=(5, 0),
            textcoords="offset points",
            color="green",
            va="center",
        )
        # formatting
        ax.set(
            xlim=[-0.05, 1.1],
            xticks=[0, 1],
            xticklabels=["pre", "post"],
            title=f"Causal impact = {round_num(self.causal_impact[0], round_to)}",
        )
        ax.legend(fontsize=LEGEND_FONT_SIZE)
        return (fig, ax)
//  [python]
def roc_curve():
    """Show example ROC curves."""
    data = pd.read_csv(pkg_resources.resource_stream(__name__, 'data/sample_roc_curves.csv'))

    ax = sns.lineplot(
        data=data, hue='label', x='x', y='y', palette='Greens'
    )

    ax.plot([0, 1], [0, 1], 'k--', alpha=0.3)

    # formatting 
    ax.set_title('Sample ROC Curves')
    ax.set_xlabel('False Positive Rate (FPR)')
    ax.set_ylabel('True Positive Rate (TPR)')

    handles, labels = ax.get_legend_handles_labels()
    for i, label in enumerate(labels):
        curve_data = data.query(f'label == "{label}"')
        labels[i] = f'{label}; AUC is {auc(curve_data.x, curve_data.y):.2}'
    ax.legend(handles=handles, labels=labels)

    ax.xaxis.set_major_formatter(PercentFormatter(xmax=1))
    ax.yaxis.set_major_formatter(PercentFormatter(xmax=1))
    
    return ax
//  [python]
def plot(self, df):
        df = copy.copy(df)
        # figsize = None
        # fig = plt.figure(figsize=figsize)
        fig, ax = plt.subplots()

        df['util_percent'] = df['util'] * 100.
        for label, data in df.groupby('line_label'):
            plt.plot(data['time_sec'], data['util_percent'], label=label)

        fix_seaborn_legend(ax, legend_kwargs=dict(
            # Position legend in center, above plotting area.
            # bbox position is relative to the bottom-centre of the legend.
            loc='lower center',
            bbox_to_anchor=(
                0.5,
                1,
            ),
        ))

        ax.set_ylabel(r"GPU utilization (%)")
        ax.set_xlabel(Y_LABEL_TRAINING_TIME_SEC)
        fig.savefig(self._plot_path, bbox_inches="tight", pad_inches=0)
        logger.info('Save figure to {path}'.format(path=self._plot_path))
        plt.close(fig)
//  [python]
def plot(self, df):
        df = copy.copy(df)
        # figsize = None
        # fig = plt.figure(figsize=figsize)
        fig, ax = plt.subplots()

        df['util_percent'] = df['util'] * 100.
        sns.lineplot(
            x="time_sec", y="util_percent", hue="line_label",
            data=df,
            ax=ax)

        fix_seaborn_legend(ax, legend_kwargs=dict(
            # Position legend in center, above plotting area.
            # bbox position is relative to the bottom-centre of the legend.
            loc='lower center',
            bbox_to_anchor=(
                0.5,
                1,
            ),
        ))

        ax.set_ylabel(r"GPU utilization (%)")
        ax.set_xlabel(Y_LABEL_TRAINING_TIME_SEC)
        fig.savefig(self._plot_path, bbox_inches="tight", pad_inches=0)
        logger.info('Save figure to {path}'.format(path=self._plot_path))
        plt.close(fig)