jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _make_plots(self, df, plot_metrics):
        def filter_df_with_dict(df, d):
            ret = df.copy()
            for k, v in d.items():
                ret = ret[ret[k] == v].copy()
            return ret

        # Create separate plots per config
        config_dfs = []
        for conf in self._config.iterate():
            df_filtered = filter_df_with_dict(df, conf.fields)
            for metric in plot_metrics:
                plot = sns.barplot(x=df_filtered["unit_name"], y=df_filtered[metric], capsize=0.2)
                plot.set_title(conf.description().replace('-', ' '))
                plot.get_figure().savefig(
                    os.path.join(self._logdir, conf.description() + "_" + metric + '.png'))
                plt.clf()
            df_filtered['config'] = conf.description()
            config_dfs.append(df_filtered)

        # Create aggregate plots for GPU vs CPU
        for gpu in self._config.gpu:
            for metric in plot_metrics:
                plot = sns.barplot(x=df['unit_name'], y=df[metric])
                plot.set_title("Fully aggregated " + ('GPU' if gpu else 'CPU'))
                plot.get_figure().savefig(
                    os.path.join(self._logdir,
                                 'aggregate' + ('GPU' if gpu else 'CPU') + "_" + metric + '.png'))
                plt.clf()

        # Create a single plot showing all configs at once
        all_configs = pd.concat(config_dfs)
        for metric in plot_metrics:
            plot = sns.factorplot(x='unit_name', y=metric, col='config', kind='bar',
                                  data=all_configs, col_wrap=4)
            plot.savefig(
                os.path.join(self._logdir, 'all_configs' + "_" + metric + '.png'))
            plt.clf()

        # Also plot statistics for different runs
        for metric in plot_metrics:
            plot = sns.factorplot(x='unit_name', y=metric, col='run_name', kind='bar',
                                  data=all_configs, col_wrap=4)
            plot.savefig(
                os.path.join(self._logdir, 'per_run' + "_" + metric + '.png'))
            plt.clf()
//  [python]
def _make_plots(self, df, plot_metrics):
        def filter_df_with_dict(df, d):
            ret = df.copy()
            for k, v in d.items():
                ret = ret[ret[k] == v].copy()
            return ret

        # Create separate plots per config
        config_dfs = []
        for conf in self._config.iterate():
            df_filtered = filter_df_with_dict(df, conf.fields)
            for metric in plot_metrics:
                plot = sns.barplot(x=df_filtered["unit_name"], y=df_filtered[metric], capsize=0.2)
                plot.set_title(conf.description().replace('-', ' '))
                plot.get_figure().savefig(
                    os.path.join(self._logdir, conf.description() + "_" + metric + '.png'))
                plt.clf()
            df_filtered['config'] = conf.description()
            config_dfs.append(df_filtered)

        # Create aggregate plots for GPU vs CPU
        for gpu in self._config.gpu:
            for metric in plot_metrics:
                plot = sns.barplot(x=df['unit_name'], y=df[metric])
                plot.set_title("Fully aggregated " + ('GPU' if gpu else 'CPU'))
                plot.get_figure().savefig(
                    os.path.join(self._logdir,
                                 'aggregate' + ('GPU' if gpu else 'CPU') + "_" + metric + '.png'))
                plt.clf()

        # Create a single plot showing all configs at once
        all_configs = pd.concat(config_dfs)
        for metric in plot_metrics:
            plot = sns.catplot(x='unit_name', y=metric, col='config', kind='bar',
                                  data=all_configs, col_wrap=4)
            plot.savefig(
                os.path.join(self._logdir, 'all_configs' + "_" + metric + '.png'))
            plt.clf()

        # Also plot statistics for different runs
        for metric in plot_metrics:
            plot = sns.catplot(x='unit_name', y=metric, col='run_name', kind='bar',
                                  data=all_configs, col_wrap=4)
            plot.savefig(
                os.path.join(self._logdir, 'per_run' + "_" + metric + '.png'))
            plt.clf()
//  [python]
def analysis_data_distribution(self):
        """
        分析数据分布
        :return:
        """
        if self.words_dict == None:
            assert 0, print("字典为空，无法分析")

        titanic = sb.load_dataset("titanic")
        #print(titanic)
        #sb.distplot(self.csv_data['word_num'], kde = True, rug = True)
        sb.countplot(x="word", palette="ch:.25", data=self.csv_data[0:20])

        plt.show()
        print(self.csv_data.sort_values(by="word_num", ascending=False))
//  [python]
def analysis_data_distribution(self):
        """
        分析数据分布
        :return:
        """
        if self.words_dict == None:
            assert 0, print("字典为空，无法分析")

        titanic = sb.load_dataset("titanic")
        #print(titanic)
        #sb.distplot(self.csv_data['word_num'], kde = True, rug = True)
        sb.catplot(x="word", kind="count", palette="ch:.25",
                   data=self.csv_data[0:20])

        plt.show()
        print(self.csv_data.sort_values(by="word_num", ascending=False))
//  [python]
def plot_metrics(result_df, x='condition', metrics=['PCC', 'RMSE', 'PCC_RMSE'], 
                 save=True, output_path="image/High/{metric}.png", height=10, aspect=0.5):
    for metric in metrics:
        g = sns.factorplot(data=result_df, kind='bar', x=x, y=metric, 
                        col='dataset', size=height, aspect=aspect)
        for ax in g.axes.flat:
            for label in ax.get_xticklabels():
                label.set_rotation(90)
        plt.show()
        if save:
            g.savefig(output_path.format(metric=metric))
    return None
//  [python]
def plot_metrics(result_df, x='condition', metrics=['PCC', 'RMSE', 'PCC_RMSE'], 
                 save=True, output_path="image/High/{metric}.png", height=10, aspect=0.5):
    for metric in metrics:
        g = sns.catplot(data=result_df, kind='bar', x=x, y=metric, 
                        col='dataset', height=height, aspect=aspect)
        for ax in g.axes.flat:
            for label in ax.get_xticklabels():
                label.set_rotation(90)
        plt.show()
        if save:
            g.savefig(output_path.format(metric=metric))
    return None
//  [python]
plot_categorical_feature('Census_InternalBatteryNumberOfCharges', True, by_touch=True)

# Census_ChassisTypeName
plot_categorical_feature('Census_ChassisTypeName', True, by_touch=True)

# Census_MDC2FormFactor
train['Census_MDC2FormFactor'].value_counts(dropna=False, normalize=True).cumsum()
plot_categorical_feature('Census_MDC2FormFactor', True)
g = sns.factorplot(x="Census_PrimaryDiskTypeName", hue="HasDetections", col="Census_MDC2FormFactor",
                data=train, kind="count",col_wrap=3)
//  [python]
plot_categorical_feature('Census_InternalBatteryNumberOfCharges', True, by_touch=True)

# Census_ChassisTypeName
plot_categorical_feature('Census_ChassisTypeName', True, by_touch=True)

# Census_MDC2FormFactor
train['Census_MDC2FormFactor'].value_counts(dropna=False, normalize=True).cumsum()
plot_categorical_feature('Census_MDC2FormFactor', True)
sns.catplot(x="Census_PrimaryDiskTypeName", hue="HasDetections", col="Census_MDC2FormFactor",
                data=train, kind="count",col_wrap=3)
//  [python]
def categorical_eda(df, hue=None):
    """Given dataframe, generate EDA of categorical data"""
    print("\nTo check: \nUnique count of non-numeric data\n")
    print(df.select_dtypes(include=['object', 'category']).nunique())
    top5(df)
    # Plot count distribution of categorical data
    for col in df.select_dtypes(include='category').columns:
        fig = sns.countplot(x=col, data=df, hue=hue)
        fig.set_xticklabels(rotation=90)
        plt.show()
//  [python]
def categorical_eda(df, hue=None):
    """Given dataframe, generate EDA of categorical data"""
    print("\nTo check: \nUnique count of non-numeric data\n")
    print(df.select_dtypes(include=['object', 'category']).nunique())
    top5(df)
    # Plot count distribution of categorical data
    for col in df.select_dtypes(include='category').columns:
        fig = sns.catplot(x=col, kind="count", data=df, hue=hue)
        fig.set_xticklabels(rotation=90)
        plt.show()
//  [python]
def numeric_eda(df, hue=None):
    """Given dataframe, generate EDA of numeric data"""
    print("\nTo check: \nDistribution of numeric data")
    display(df.describe().T)
    columns = df.select_dtypes(include=np.number).columns
    figure = plt.figure(figsize=(20, 10))
    figure.add_subplot(1, len(columns), 1)
    for index, col in enumerate(columns):
        if index > 0:
            figure.add_subplot(1, len(columns), index + 1)
        sns.boxplot(y=col, data=df, boxprops={'facecolor': 'None'})
    figure.tight_layout()
    plt.show()
    
    if len(df.select_dtypes(include='category').columns) > 0:
        for col_num in df.select_dtypes(include=np.number).columns:
            for col in df.select_dtypes(include='category').columns:
                fig = sns.violinplot(x=col, y=col_num, data=df)
                plt.xticks(rotation=90)
                plt.show()
    
    # Plot the pairwise joint distributions
    print("\nTo check pairwise joint distribution of numeric data")
    if hue==None:
        sns.pairplot(df.select_dtypes(include=np.number))
    else:
        sns.pairplot(df.select_dtypes(include=np.number).join(df[[hue]]), hue=hue)
    plt.show()
//  [python]
def numeric_eda(df, hue=None):
    """Given dataframe, generate EDA of numeric data"""
    print("\nTo check: \nDistribution of numeric data")
    display(df.describe().T)
    columns = df.select_dtypes(include=np.number).columns
    figure = plt.figure(figsize=(20, 10))
    figure.add_subplot(1, len(columns), 1)
    for index, col in enumerate(columns):
        if index > 0:
            figure.add_subplot(1, len(columns), index + 1)
        sns.boxplot(y=col, data=df, boxprops={'facecolor': 'None'})
    figure.tight_layout()
    plt.show()
    
    if len(df.select_dtypes(include='category').columns) > 0:
        for col_num in df.select_dtypes(include=np.number).columns:
            for col in df.select_dtypes(include='category').columns:
                fig = sns.catplot(x=col, y=col_num, kind='violin', data=df, height=5, aspect=2)
                fig.set_xticklabels(rotation=90)
                plt.show()
    
    # Plot the pairwise joint distributions
    print("\nTo check pairwise joint distribution of numeric data")
    if hue==None:
        sns.pairplot(df.select_dtypes(include=np.number))
    else:
        sns.pairplot(df.select_dtypes(include=np.number).join(df[[hue]]), hue=hue)
    plt.show()
//  [python]
customization_running_times['efficiency'] = baseline / customization_running_times['work']

fig = plt.figure(figsize=(7/1.1,3/1.1))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

sns.barplot(x="num_threads", y="customization_running_time_s", data=customization_running_times, ax=ax1)
//  [python]
customization_running_times['efficiency'] = baseline / customization_running_times['work']

fig = plt.figure(figsize=(7/1.1,3/1.1))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

sns.catplot(x="num_threads", y="customization_running_time_s", kind="bar", data=customization_running_times, ax=ax1)
//  [python]
def draw_box_plot(
    plot_data: List[numpy.ndarray],
    method_labels: List[str],
    metric: str,
    y_range: Tuple[float, float],
    output_path: str,
):
    """Draw a violin plot of data segregated by each method.

    Parameters
    ----------
    plot_data
        A list of the arrays where ``plot_data[i]`` contains the particular metric of
        interest associated with ``method_labels[i]``.
    method_labels
        The label associated with each data series.
    metric
        The metric contained in the ``plot_data``.
    output_path
        The path to save the final plot to.
    """

    plot_frame = pandas.DataFrame(
        [
            {"method": method_label, metric: data_point}
            for data_series, method_label in zip(plot_data, method_labels)
            for data_point in data_series
        ]
    )

    seaborn.boxplot(data=plot_frame, x="method", y=metric)

    pyplot.xticks(rotation=45)

    if y_range is not None:
        pyplot.ylim(y_range)

    pyplot.xlabel("Force Field", fontsize=14)
    pyplot.ylabel(metric, fontsize=14)

    # save with transparency for overlapping plots
    pyplot.savefig(output_path, transparent=True, bbox_inches="tight")
    pyplot.close("all")
//  [python]
def draw_box_plot(
    plot_data: List[numpy.ndarray],
    method_labels: List[str],
    metric: str,
    y_range: Tuple[float, float],
    output_path: str,
):
    """Draw a violin plot of data segregated by each method.

    Parameters
    ----------
    plot_data
        A list of the arrays where ``plot_data[i]`` contains the particular metric of
        interest associated with ``method_labels[i]``.
    method_labels
        The label associated with each data series.
    metric
        The metric contained in the ``plot_data``.
    output_path
        The path to save the final plot to.
    """

    plot_frame = pandas.DataFrame(
        [
            {"method": method_label, metric: data_point}
            for data_series, method_label in zip(plot_data, method_labels)
            for data_point in data_series
        ]
    )

    seaborn.catplot(data=plot_frame, x="method", y=metric, kind="box", dodge=False)

    pyplot.xticks(rotation=45)

    if y_range is not None:
        pyplot.ylim(y_range)

    pyplot.xlabel("Force Field", fontsize=14)
    pyplot.ylabel(metric, fontsize=14)

    # save with transparency for overlapping plots
    pyplot.savefig(output_path, transparent=True, bbox_inches="tight")
    pyplot.close("all")
//  [python]
def plot_cost():

    
    sns.set_context("paper", font_scale=3.5)
    cost_df = pd.read_csv('/home/cc/cocktail/cost.csv', header=0, index_col=False) 
    #cost_df = pd.melt(cost_df, id_vars=["Policy", "Query"],
    #                  value_vars=["p50", "p90", "p99"],
    #                  var_name="Percentile",
    #                  value_name="#Models")
    print(cost_df) 
    wiki_df = cost_df[cost_df["Trace"]=="WIKI"]

    fig, ax = plt.subplots(figsize=(8, 4))
    g = sns.factorplot(x="Workload", y="Cost($)", hue="Scheme", data=wiki_df,
                    size=6, kind="bar", palette=figs_with_baseline_cpalette ,legend_out=False, aspect=2)
    plt.legend( loc='upper center',
                # bbox_to_anchor=(0.5, 1.1),
                bbox_to_anchor=(0.5, 1.3),
                ncol=4,
                frameon=False,
                fancybox=True, 
                framealpha=0.4,
                shadow=False,
                edgecolor="black",
                labelspacing=0,
                columnspacing=0.1,
                handletextpad=0.5)
                # labels=figs_normalized_barlabels)
    for i,thisbar in enumerate(g.ax.patches):
        thisbar.set_hatch(hatches[i])           
    g.set_xlabels("")
    g.set_ylabels("Cost($)")
    close_fig(merged_pdf)
    twitter_df = cost_df[cost_df["Trace"]=="Twitter"]

    fig, ax = plt.subplots(figsize=(8, 4))
    g = sns.factorplot(x="Workload", y="Cost($)", hue="Scheme", data=twitter_df,
                    size=6, kind="bar", palette = "GnBu", legend_out=False, aspect=2)
    plt.legend( loc='upper center',
                # bbox_to_anchor=(0.5, 1.1),
                bbox_to_anchor=(0.5, 1.3),
                ncol=4,
                frameon=False,
                fancybox=True, 
                framealpha=0.4,
                shadow=False,
                edgecolor="black",
                labelspacing=0,
                columnspacing=0.1,
                handletextpad=0.5
                # labels=figs_normalized_barlabels
                )
    for i,thisbar in enumerate(g.ax.patches):
        thisbar.set_hatch(hatches[i])           

    g.set_xlabels("")
    g.set_ylabels("Cost($)")

    close_fig(merged_pdf)
//  [python]
def plot_cost():

    
    sns.set_context("paper", font_scale=3.5)
    cost_df = pd.read_csv('/home/cc/cocktail/cost.csv', header=0, index_col=False) 
    #cost_df = pd.melt(cost_df, id_vars=["Policy", "Query"],
    #                  value_vars=["p50", "p90", "p99"],
    #                  var_name="Percentile",
    #                  value_name="#Models")
    print(cost_df) 
    wiki_df = cost_df[cost_df["Trace"]=="WIKI"]

    fig, ax = plt.subplots(figsize=(8, 4))
    g = sns.catplot(x="Workload", y="Cost($)", hue="Scheme", data=wiki_df,
                    height=6, kind="bar", palette=figs_with_baseline_cpalette ,legend_out=False, aspect=2)
    plt.legend( loc='upper center',
                # bbox_to_anchor=(0.5, 1.1),
                bbox_to_anchor=(0.5, 1.3),
                ncol=4,
                frameon=False,
                fancybox=True, 
                framealpha=0.4,
                shadow=False,
                edgecolor="black",
                labelspacing=0,
                columnspacing=0.1,
                handletextpad=0.5)
                # labels=figs_normalized_barlabels)
    for i,thisbar in enumerate(g.ax.patches):
        thisbar.set_hatch(hatches[i])           
    g.set_xlabels("")
    g.set_ylabels("Cost($)")
    close_fig(merged_pdf)
    twitter_df = cost_df[cost_df["Trace"]=="Twitter"]

    fig, ax = plt.subplots(figsize=(8, 4))
    g = sns.catplot(x="Workload", y="Cost($)", hue="Scheme", data=twitter_df,
                    height=6, kind="bar", palette = "GnBu", legend_out=False, aspect=2)
    plt.legend( loc='upper center',
                # bbox_to_anchor=(0.5, 1.1),
                bbox_to_anchor=(0.5, 1.3),
                ncol=4,
                frameon=False,
                fancybox=True, 
                framealpha=0.4,
                shadow=False,
                edgecolor="black",
                labelspacing=0,
                columnspacing=0.1,
                handletextpad=0.5
                # labels=figs_normalized_barlabels
                )
    for i,thisbar in enumerate(g.ax.patches):
        thisbar.set_hatch(hatches[i])           

    g.set_xlabels("")
    g.set_ylabels("Cost($)")

    close_fig(merged_pdf)
//  [python]
def plot_planning_boxes(data, plot_var_list, category, save=True):
    """Boxplot planning time, with hue according to the specified category"""

    if data.empty:
        return
    # In order to get the right legend on the plot, first make an empty plot with colored lines
    plt.figure()
    palette = []
    for plot_vars in plot_var_list:
        plt.plot(0, 0, c=plot_vars.color, label=plot_vars._asdict()[category], lw=3)
        palette.append(plot_vars.color)
    plt.legend()
    handles, labels = plt.gca().get_legend_handles_labels()
    plt.show()
    plt.close()

    # Now we make the actual plot, since seaborn's catplot doesn't accept an 'ax' argument
    plt.rcParams.update({'font.size': cfg.FONTSIZE, 'figure.figsize': cfg.FIGSIZE})
    matplotlib_axes_logger.setLevel('ERROR')
    catplot = sns.boxplot(data=data.query('n_errors==0'), y=category, x='transitions',
                          palette=reversed(palette), orient='h', showfliers=False)
    catplot.despine(right=False, top=False)
    plt.ylabel('Macro-action type')
    plt.gcf().set_size_inches(*cfg.FIGSIZE)
    plt.tight_layout()
    plt.xlim(cfg.XLIM)
    ax = plt.gca()
    ax.invert_yaxis()
    ax.set_yticklabels([])
    ax.xaxis.set_major_locator(ticker.MultipleLocator(plot_vars.tick_size))
    ax.set_xlabel('Generated states' + autoscale_xticks(ax, dtype=int))
    plt.tight_layout()
    plt.subplots_adjust(top = .96, bottom = .19, right = .95, left = 0.09,
            hspace = 0, wspace = 0)
    ax.legend(handles, labels, loc='lower right')
    if save:
        plt.gcf().savefig('results/plots/{}/{}_planning_time_by_{}.png'.format(
            cfg.DIR, cfg.NAME, category), dpi=100)
    plt.show()
//  [python]
def plot_planning_boxes(data, plot_var_list, category, save=True):
    """Boxplot planning time, with hue according to the specified category"""

    if data.empty:
        return
    # In order to get the right legend on the plot, first make an empty plot with colored lines
    plt.figure()
    palette = []
    for plot_vars in plot_var_list:
        plt.plot(0, 0, c=plot_vars.color, label=plot_vars._asdict()[category], lw=3)
        palette.append(plot_vars.color)
    plt.legend()
    handles, labels = plt.gca().get_legend_handles_labels()
    plt.show()
    plt.close()

    # Now we make the actual plot, since seaborn's catplot doesn't accept an 'ax' argument
    plt.rcParams.update({'font.size': cfg.FONTSIZE, 'figure.figsize': cfg.FIGSIZE})
    matplotlib_axes_logger.setLevel('ERROR')
    catplot = sns.catplot(data=data.query('n_errors==0'), y=category, x='transitions',
                          kind='boxen', palette=reversed(palette), orient='h', legend='True', showfliers=False)
    catplot.despine(right=False, top=False)
    plt.ylabel('Macro-action type')
    plt.gcf().set_size_inches(*cfg.FIGSIZE)
    plt.tight_layout()
    plt.xlim(cfg.XLIM)
    ax = plt.gca()
    ax.invert_yaxis()
    ax.set_yticklabels([])
    ax.xaxis.set_major_locator(ticker.MultipleLocator(plot_vars.tick_size))
    ax.set_xlabel('Generated states' + autoscale_xticks(ax, dtype=int))
    plt.tight_layout()
    plt.subplots_adjust(top = .96, bottom = .19, right = .95, left = 0.09,
            hspace = 0, wspace = 0)
    ax.legend(handles, labels, loc='lower right')
    if save:
        plt.gcf().savefig('results/plots/{}/{}_planning_time_by_{}.png'.format(
            cfg.DIR, cfg.NAME, category), dpi=100)
    plt.show()
//  [python]
def cat(data, x, y, hue, row, col, col_wrap, ci, seed, kind, palette, fig_size, despine={}, add_args={}, ax_args={}):
    args = clean_args(locals())

    g = sns.factorplot(**args, **add_args)
    g.set(**ax_args)

    if fig_size:
        set_fig_size(g, fig_size)

    if despine:
        sns.despine(**despine)

    # tight layout
    plt.tight_layout()

    return base64img()
//  [python]
def cat(data, x, y, hue, row, col, col_wrap, ci, seed, kind, palette, fig_size, despine={}, add_args={}, ax_args={}):
    # https://seaborn.pydata.org/generated/seaborn.catplot.html#seaborn.catplot

    args = clean_args(locals())

    g = sns.catplot(**args, **add_args)
    g.set(**ax_args)

    if fig_size:
        set_fig_size(g, fig_size)

    if despine:
        sns.despine(**despine)

    # tight layout
    plt.tight_layout()

    return base64img()
//  [python]
print(tips.info())

plt.hist(tips["total_bill"])

themes = ["paper","notebook","talk","poster"]

for theme in themes:
    sns.set_theme(theme)
    sns.displot(x=tips["total_bill"])

sns.factorplot(x="day",y="total_bill",hue="sex",data=tips)
//  [python]
print(tips.info())

plt.hist(tips["total_bill"])

themes = ["paper","notebook","talk","poster"]

for theme in themes:
    sns.set_theme(theme)
    sns.displot(x=tips["total_bill"])

sns.catplot(x="day",y="total_bill",hue="sex",data=tips)
//  [python]
def plot_method(df, figname, vmax=60.0):
    snsplot = sns.barplot(data=df, x="corruption", y="cls", 
                        hue="method", hue_order=["test", "ssl", "coral", "both"])
    snsplot.set(ylim=(0.0, vmax))
    plt.savefig(figname, bbox_inches='tight', pad_inches=0)
    print('Save figure to {}'.format(figname))
//  [python]
def plot_method(df, figname, vmax=60.0):
    snsplot = sns.catplot(data=df, x="corruption", y="cls", 
                        hue="method", kind="bar", aspect=2.8,
                        hue_order=["test", "ssl", "coral", "both"])
    snsplot.set(ylim=(0.0, vmax))
    plt.savefig(figname, bbox_inches='tight', pad_inches=0)
    print('Save figure to {}'.format(figname))
//  [python]
plt.show()


sns.relplot(x="Model_year", y="Horsepower", 
            data=cardata, kind="line", ci=None)
plt.show()


################### category chat ######################
########################################################
########################################################
# count plot
sns.set_style('whitegrid')
g = sns.factorplot(x='Year', data=cardata, kind='count')
//  [python]
plt.show()


sns.relplot(x="Model_year", y="Horsepower", 
            data=cardata, kind="line", ci=None)
plt.show()


################### category chat ######################
########################################################
########################################################
# count plot
sns.set_style('whitegrid')
g = sns.catplot(x='Year', data=cardata, kind='count')
//  [python]
def plot_results():
    overview_dataframe = prettify_dataframe(results_dataframe.copy())
    print(overview_dataframe)
    plot = sns.boxplot(
        y="visualization",
        x="result",
        data=overview_dataframe
    )
    plt.gcf().savefig("output/overview_results_random.pdf")
//  [python]
def plot_results():
    overview_dataframe = prettify_dataframe(results_dataframe.copy())
    print(overview_dataframe)
    plot = sns.catplot(
        y="visualization",
        x="result",
        col="perspective",
        # row="model",
        # hue="dataset",
        data=overview_dataframe,
        kind="box",
        sharex="col",
        margin_titles=True,
        aspect=0.6 * 1.5,
        height=6 / 1.5 * FIG_3_SCALE,
    )  # , style="dataset"
    (
        plot.set_axis_labels("", "").set_titles(
            col_template="{col_name}", row_template="{row_name}"
        )  # {col_var}
        # .set(xlim=(-0.05, 1.05))
        .despine(top=False, right=False)
        # .set_xticks([0, 0.5, 1])
        # .set_xticklabels(["0", "0.5", "1"])
    )
    plt.gcf().subplots_adjust(wspace=0, hspace=0)
    plt.gcf().savefig("output/overview_results_random.pdf")
//  [python]
def plot_dataset_influence():
    # results_dataframe = prettify_dataframe(results_dataframe)
    overview_dataframe = prettify_dataframe(results_dataframe.copy())
    plot = sns.boxplot(
        y="dataset",
        x="result",
        data=overview_dataframe
    )
    plt.show()
//  [python]
def plot_dataset_influence():
    # results_dataframe = prettify_dataframe(results_dataframe)
    overview_dataframe = prettify_dataframe(results_dataframe.copy())
    plot = sns.catplot(
        y="dataset",
        x="result",
        col="perspective",
        # hue="dataset",
        data=overview_dataframe,
        kind="box",
        sharex="col",
        margin_titles=True,
        aspect=0.9 * 1.5 / 0.7,
        height=4
        / 1.5
        * (0.75)
        * FIG_3_SCALE,  # So that this plot has approx. same "box-height" as the big results plot.
    )  # , style="dataset"
    (
        plot.set_axis_labels("", "")
        .set_titles(col_template="{col_name}", row_template="{row_name}")  # {col_var}
        .set(xlim=(-0.05, 1.05))
        .despine(top=False, right=False)
        # .set_xticks([0, 0.5, 1])
        # .set_xticklabels(["0", "0.5", "1"])
    )
    plt.gcf().subplots_adjust(wspace=0, hspace=0)
    plt.gcf().savefig("output/overview_datasetinfluence.pdf")
//  [python]
data = pd.read_csv(sys.argv[1], comment='#')

print(data.groupby('block').describe())

sns.factorplot(x='block', y='rt', data=data)
//  [python]
data = pd.read_csv(sys.argv[1], comment='#')

print(data.groupby('block').describe())

sns.catplot(x='block', y='rt', data=data)
//  [python]
def plot_results_swarm():
    overview_dataframe = prettify_dataframe(results_dataframe.copy())
    plot = sns.catplot(
        y="visualization",
        x="result",
        col="perspective",
        # row="model",
        hue="dataset",
        data=overview_dataframe,
        kind="swarm",
        sharex="col",
        margin_titles=True,
        aspect=0.9 * 1.5,
        height=4 / 1.5,
    )  # , style="dataset"
    (
        plot.set_axis_labels("", "")
        .set_titles(col_template="{col_name}", row_template="{row_name}")  # {col_var}
        .set(xlim=(-0.05, 1.05))
        .despine(top=False, right=False)
        # .set_xticks([0, 0.5, 1])
        # .set_xticklabels(["0", "0.5", "1"])
    )
    plt.gcf().subplots_adjust(wspace=0, hspace=0)
    plt.gcf().savefig("output/overview_results_swarm_radom.pdf")
//  [python]
def plot_results_without_datasets():
    # results_dataframe = prettify_dataframe(results_dataframe)
    overview_dataframe = prettify_dataframe(results_dataframe_without_datasets.copy())
    plot = sns.catplot(
        y="visualization",
        x="result",
        col="perspective",
        # hue="dataset",
        data=overview_dataframe,
        kind="box",
        sharex="col",
        margin_titles=True,
        aspect=0.9,
        height=4,
    )  # , style="dataset"
    (
        plot.set_axis_labels("", "").set_titles(
            col_template="{col_name}", row_template="{row_name}"
        )  # {col_var}
        # .set(xlim=(-1.5, 1.5))
        .despine(top=False, right=False)
        # .set_xticks([0, 0.5, 1])
        # .set_xticklabels(["0", "0.5", "1"])
    )
    plt.gcf().subplots_adjust(wspace=0, hspace=0)
    plt.gcf().savefig("output/overview_withoutdatasets_random.pdf")
//  [python]
def plot_results_models():
    # results_dataframe = prettify_dataframe(results_dataframe)
    overview_dataframe = prettify_dataframe(results_dataframe.copy())
    plot = sns.factorplot(
        y="model",
        x="result",
        col="perspective",
        # hue="dataset",
        data=overview_dataframe,
        kind="box",
        sharex="col",
        margin_titles=True,
        aspect=2,
        size=3.6 / 2,  # Can't use width here, since that messes up the box plot.
    )  # , style="dataset"
    (
        plot.set_axis_labels("", "")
        .set_titles(col_template="{col_name}", row_template="{row_name}")  # {col_var}
        .set(xlim=(-0.05, 1.05))
        .despine(top=False, right=False)
        # .set_xticks([0, 0.5, 1])
        # .set_xticklabels(["0", "0.5", "1"])
    )
    plt.gcf().subplots_adjust(wspace=0, hspace=0)
    plt.gcf().savefig("output/overview_models.pdf")
//  [python]
def plot_results_models():
    # results_dataframe = prettify_dataframe(results_dataframe)
    overview_dataframe = prettify_dataframe(results_dataframe.copy())
    plot = sns.catplot(
        y="model",
        x="result",
        col="perspective",
        # hue="dataset",
        data=overview_dataframe,
        kind="box",
        sharex="col",
        margin_titles=True,
        aspect=2,
        height=3.6 / 2,  # Can't use width here, since that messes up the box plot.
    )  # , style="dataset"
    (
        plot.set_axis_labels("", "")
        .set_titles(col_template="{col_name}", row_template="{row_name}")  # {col_var}
        .set(xlim=(-0.05, 1.05))
        .despine(top=False, right=False)
        # .set_xticks([0, 0.5, 1])
        # .set_xticklabels(["0", "0.5", "1"])
    )
    plt.gcf().subplots_adjust(wspace=0, hspace=0)
    plt.gcf().savefig("output/overview_models.pdf")
//  [python]
sns.set_style('darkgrid')

plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置显示中文
plt.rcParams['axes.unicode_minus'] = False # 正常显示坐标轴的负号

ax[0][0].set_title(u'分类散点图1(存在微小抖动)')
sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0][0])
//  [python]
sns.set_style('darkgrid')

plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置显示中文
plt.rcParams['axes.unicode_minus'] = False # 正常显示坐标轴的负号

ax[0][0].set_title(u'分类散点图1(存在微小抖动)')
sns.catplot(x="day", y="total_bill", data=tips, ax=ax[0][0])
//  [python]
sns.set(style="darkgrid")

tips = sns.load_dataset("tips")
sns.swarmplot(x="day", y="total_bill", hue="smoker",
            col="time", data=tips)
//  [python]
sns.set(style="darkgrid")

tips = sns.load_dataset("tips")
sns.catplot(x="day", y="total_bill", hue="smoker",
            col="time", aspect=.6,
            kind="swarm", data=tips)
//  [python]
sns.set_context("paper", font_scale = 2, rc = {"font.size": 20,"axes.titlesize": 25,"axes.labelsize": 20}) 
sns.factorplot(kind = 'count', data = df, x = 'age', hue = 'target', order = df['age'].sort_values().unique())
//  [python]
sns.set_context("paper", font_scale = 2, rc = {"font.size": 20,"axes.titlesize": 25,"axes.labelsize": 20}) 
sns.catplot(kind = 'count', data = df, x = 'age', hue = 'target', order = df['age'].sort_values().unique())
//  [python]
def introduction():
	# Apply the default theme.
	sns.set_theme()

	# Load an example dataset.
	tips = sns.load_dataset("tips")

	if False:
		# Create a visualization.
		sns.relplot(
			data=tips,
			x="total_bill", y="tip", col="time",
			hue="smoker", style="smoker", size="size",
		)

	#-----
	# A high-level API for statistical graphics.

	if True:
		dots = sns.load_dataset("dots")
		sns.relplot(
			data=dots, kind="line",
			x="time", y="firing_rate", col="align",
			hue="choice", size="coherence", style="choice",
			facet_kws=dict(sharex=False),
		)

		# Statistical estimation.
		fmri = sns.load_dataset("fmri")
		sns.relplot(
			data=fmri, kind="line",
			x="timepoint", y="signal", col="region",
			hue="event", style="event",
		)

		# Linear regression model.
		sns.lmplot(data=tips, x="total_bill", y="tip", col="time", hue="smoker")

		# Distributional representations.
		sns.displot(data=tips, x="total_bill", col="time", kde=True)  # Histogram &  kernel density estimation.
		sns.displot(data=tips, kind="ecdf", x="total_bill", col="time", hue="smoker", rug=True)  # Empirical cumulative distribution function.

		# Plots for categorical data.
		sns.swarmplot(data=tips, x="day", y="total_bill", hue="smoker")
		sns.violinplot(data=tips, x="day", y="total_bill", hue="smoker", split=True)  # Kernel density estimation.
		sns.barplot(data=tips, x="day", y="total_bill", hue="smoker")  # Mean value & confidence interval.

		# Multivariate views on complex datasets.
		penguins = sns.load_dataset("penguins")
		sns.jointplot(data=penguins, x="flipper_length_mm", y="bill_length_mm", hue="species")  # Single relationship.
		sns.pairplot(data=penguins, hue="species")  # Joint and marginal distributions for all pairwise relationships and for each variable, respectively.

		# Lower-level tools for building figures.
		g = sns.PairGrid(penguins, hue="species", corner=True)
		g.map_lower(sns.kdeplot, hue=None, levels=5, color=".2")
		g.map_lower(sns.scatterplot, marker="+")
		g.map_diag(sns.histplot, element="step", linewidth=0, kde=True)
		g.add_legend(frameon=True)
		g.legend.set_bbox_to_anchor((.61, .6))

	#-----
	# Opinionated defaults and flexible customization.

	if False:
		penguins = sns.load_dataset("penguins")

		sns.relplot(
			data=penguins,
			x="bill_length_mm", y="bill_depth_mm", hue="body_mass_g"
		)

		sns.set_theme(style="ticks", font_scale=1.25)
		g = sns.relplot(
			data=penguins,
			x="bill_length_mm", y="bill_depth_mm", hue="body_mass_g",
			palette="crest", marker="x", s=100,
		)
		g.set_axis_labels("Bill length (mm)", "Bill depth (mm)", labelpad=10)
		g.legend.set_title("Body mass (g)")
		g.figure.set_size_inches(6.5, 4.5)
		g.ax.margins(.15)
		g.despine(trim=True)

	plt.show()
//  [python]
def introduction():
	# Apply the default theme.
	sns.set_theme()

	# Load an example dataset.
	tips = sns.load_dataset("tips")

	if False:
		# Create a visualization.
		sns.relplot(
			data=tips,
			x="total_bill", y="tip", col="time",
			hue="smoker", style="smoker", size="size",
		)

	#-----
	# A high-level API for statistical graphics.

	if True:
		dots = sns.load_dataset("dots")
		sns.relplot(
			data=dots, kind="line",
			x="time", y="firing_rate", col="align",
			hue="choice", size="coherence", style="choice",
			facet_kws=dict(sharex=False),
		)

		# Statistical estimation.
		fmri = sns.load_dataset("fmri")
		sns.relplot(
			data=fmri, kind="line",
			x="timepoint", y="signal", col="region",
			hue="event", style="event",
		)

		# Linear regression model.
		sns.lmplot(data=tips, x="total_bill", y="tip", col="time", hue="smoker")

		# Distributional representations.
		sns.displot(data=tips, x="total_bill", col="time", kde=True)  # Histogram &  kernel density estimation.
		sns.displot(data=tips, kind="ecdf", x="total_bill", col="time", hue="smoker", rug=True)  # Empirical cumulative distribution function.

		# Plots for categorical data.
		sns.catplot(data=tips, kind="swarm", x="day", y="total_bill", hue="smoker")
		sns.catplot(data=tips, kind="violin", x="day", y="total_bill", hue="smoker", split=True)  # Kernel density estimation.
		sns.catplot(data=tips, kind="bar", x="day", y="total_bill", hue="smoker")  # Mean value & confidence interval.

		# Multivariate views on complex datasets.
		penguins = sns.load_dataset("penguins")
		sns.jointplot(data=penguins, x="flipper_length_mm", y="bill_length_mm", hue="species")  # Single relationship.
		sns.pairplot(data=penguins, hue="species")  # Joint and marginal distributions for all pairwise relationships and for each variable, respectively.

		# Lower-level tools for building figures.
		g = sns.PairGrid(penguins, hue="species", corner=True)
		g.map_lower(sns.kdeplot, hue=None, levels=5, color=".2")
		g.map_lower(sns.scatterplot, marker="+")
		g.map_diag(sns.histplot, element="step", linewidth=0, kde=True)
		g.add_legend(frameon=True)
		g.legend.set_bbox_to_anchor((.61, .6))

	#-----
	# Opinionated defaults and flexible customization.

	if False:
		penguins = sns.load_dataset("penguins")

		sns.relplot(
			data=penguins,
			x="bill_length_mm", y="bill_depth_mm", hue="body_mass_g"
		)

		sns.set_theme(style="ticks", font_scale=1.25)
		g = sns.relplot(
			data=penguins,
			x="bill_length_mm", y="bill_depth_mm", hue="body_mass_g",
			palette="crest", marker="x", s=100,
		)
		g.set_axis_labels("Bill length (mm)", "Bill depth (mm)", labelpad=10)
		g.legend.set_title("Body mass (g)")
		g.figure.set_size_inches(6.5, 4.5)
		g.ax.margins(.15)
		g.despine(trim=True)

	plt.show()
//  [python]
def plot_improvements_vs_ref(
        improvements_df: pd.DataFrame,
        hparams: Optional[List[str]] = None
):
    measurements_to_plot = ['wilcoxon_improved_features', 'wilcoxon_deteriorated_features',
                            'median_variation_vs_ref', 'mean_variation_vs_ref']

    # 1) Plot numeric general improvement
    cols = list(improvements_df.columns)
    for m in measurements_to_plot:
        cols.remove(m)
    melted_df = pd.melt(improvements_df, id_vars=cols, var_name='var')  # value_vars will be all non-id cols
    g = sns.factorplot(
        data=melted_df, y="model", x='value', col='var', hue="metric",
        kind="bar", sharex=False
    )
    fig1, axes1 = g.fig, g.axes

    # 2) plot improvements vs model/train/interp hparams - scatter plots only
    if hparams is not None and len(hparams) >= 1:
        fig2, axes2 = plt.subplots(
            len(hparams), len(measurements_to_plot),
            figsize=(1 + 3 * len(measurements_to_plot), 1 + 3 * len(hparams)), sharex='col', sharey='row')
        axes2 = np.expand_dims(axes2, axis=0) if len(hparams) == 1 else axes2
        for i, hparam in enumerate(hparams):
            log_scale = hparam.endswith('___LOGSCALE')
            if log_scale:
                hparam = hparam.replace('___LOGSCALE', '')
            for j, measurement_name in enumerate(measurements_to_plot):
                # draw the evolution of means using lines (as done in comet.ml)
                h_params_values = np.unique(improvements_df[hparam].values)  # float equality: works OK with hparams
                mean_per_hparam = [improvements_df.loc[improvements_df[hparam] == v][measurement_name].values.mean()
                                   for v in h_params_values]
                axes2[i, j].plot(mean_per_hparam, h_params_values, color='k')  # 'vertical' plot
                # Then draw the actual scatter plot
                sns.scatterplot(data=improvements_df, x=measurement_name, y=hparam, ax=axes2[i, j],
                                hue='model', legend=False)
                if log_scale:
                    axes2[i, j].set(yscale='log')
        fig2.tight_layout()
    else:
        fig2, axes2 = None, None

    return fig1, axes1, fig2, axes2
//  [python]
def plot_improvements_vs_ref(
        improvements_df: pd.DataFrame,
        hparams: Optional[List[str]] = None
):
    measurements_to_plot = ['wilcoxon_improved_features', 'wilcoxon_deteriorated_features',
                            'median_variation_vs_ref', 'mean_variation_vs_ref']

    # 1) Plot numeric general improvement
    cols = list(improvements_df.columns)
    for m in measurements_to_plot:
        cols.remove(m)
    melted_df = pd.melt(improvements_df, id_vars=cols, var_name='var')  # value_vars will be all non-id cols
    g = sns.catplot(
        data=melted_df, y="model", x='value', col='var', hue="metric",
        kind="bar", sharex=False
    )
    fig1, axes1 = g.fig, g.axes

    # 2) plot improvements vs model/train/interp hparams - scatter plots only
    if hparams is not None and len(hparams) >= 1:
        fig2, axes2 = plt.subplots(
            len(hparams), len(measurements_to_plot),
            figsize=(1 + 3 * len(measurements_to_plot), 1 + 3 * len(hparams)), sharex='col', sharey='row')
        axes2 = np.expand_dims(axes2, axis=0) if len(hparams) == 1 else axes2
        for i, hparam in enumerate(hparams):
            log_scale = hparam.endswith('___LOGSCALE')
            if log_scale:
                hparam = hparam.replace('___LOGSCALE', '')
            for j, measurement_name in enumerate(measurements_to_plot):
                # draw the evolution of means using lines (as done in comet.ml)
                h_params_values = np.unique(improvements_df[hparam].values)  # float equality: works OK with hparams
                mean_per_hparam = [improvements_df.loc[improvements_df[hparam] == v][measurement_name].values.mean()
                                   for v in h_params_values]
                axes2[i, j].plot(mean_per_hparam, h_params_values, color='k')  # 'vertical' plot
                # Then draw the actual scatter plot
                sns.scatterplot(data=improvements_df, x=measurement_name, y=hparam, ax=axes2[i, j],
                                hue='model', legend=False)
                if log_scale:
                    axes2[i, j].set(yscale='log')
        fig2.tight_layout()
    else:
        fig2, axes2 = None, None

    return fig1, axes1, fig2, axes2
//  [python]
def plot_scores(self, motifs, name=True, max_len=50):
        """Create motif scores boxplot of different clusters.
        Motifs can be specified as either motif or factor names.
        The motif scores will be scaled and plotted as z-scores.

        Parameters
        ----------
        motifs : iterable or str
            List of motif or factor names.

        name : bool, optional
            Use factor names instead of motif names for plotting.

        max_len : int, optional
            Truncate the list of factors to this maximum length.

        Returns
        -------

        g : FacetGrid
            Returns the seaborn FacetGrid object with the plot.
        """
        if self.input.shape[1] != 1:
            raise ValueError("Can't make a categorical plot with real-valued data")

        if type("") == type(motifs):
            motifs = [motifs]

        plot_motifs = []
        for motif in motifs:
            if motif in self.motifs:
                plot_motifs.append(motif)
            else:
                for m in self.motifs.values():
                    if motif in m.factors:
                        plot_motifs.append(m.id)

        data = self.scores[plot_motifs]
        data[:] = data.scale(data, axix=0)
        if name:
            data = data.T
            data["factors"] = [
                join_max(self.motifs[n].factors, max_len, ",", suffix=",(...)")
                for n in plot_motifs
            ]
            data = data.set_index("factors").T

        data = pd.melt(self.input.join(data), id_vars=["cluster"])
        data.columns = ["cluster", "motif", "z-score"]
        g = sns.boxplot(
            data=data, y="motif", x="z-score", hue="cluster"
        )
        return g
//  [python]
def plot_scores(self, motifs, name=True, max_len=50):
        """Create motif scores boxplot of different clusters.
        Motifs can be specified as either motif or factor names.
        The motif scores will be scaled and plotted as z-scores.

        Parameters
        ----------
        motifs : iterable or str
            List of motif or factor names.

        name : bool, optional
            Use factor names instead of motif names for plotting.

        max_len : int, optional
            Truncate the list of factors to this maximum length.

        Returns
        -------

        g : FacetGrid
            Returns the seaborn FacetGrid object with the plot.
        """
        if self.input.shape[1] != 1:
            raise ValueError("Can't make a categorical plot with real-valued data")

        if type("") == type(motifs):
            motifs = [motifs]

        plot_motifs = []
        for motif in motifs:
            if motif in self.motifs:
                plot_motifs.append(motif)
            else:
                for m in self.motifs.values():
                    if motif in m.factors:
                        plot_motifs.append(m.id)

        data = self.scores[plot_motifs]
        data[:] = data.scale(data, axix=0)
        if name:
            data = data.T
            data["factors"] = [
                join_max(self.motifs[n].factors, max_len, ",", suffix=",(...)")
                for n in plot_motifs
            ]
            data = data.set_index("factors").T

        data = pd.melt(self.input.join(data), id_vars=["cluster"])
        data.columns = ["cluster", "motif", "z-score"]
        g = sns.catplot(
            data=data, y="motif", x="z-score", hue="cluster", kind="box", aspect=2
        )
        return g
//  [python]
def create_page():
    wp = jp.WebPage(delete_flag=False)
    chart_classes = 'm-2'

    jp.Div(text='Tips Dataset', a=wp, classes='text-white bg-blue-500 text-center text-xl')
    sns.set()
    tips = sns.load_dataset("tips")
    sns.relplot(x="total_bill", y="tip", col="time",
                hue="smoker", style="smoker", size="size",
                data=tips)
    jp.Matplotlib(a=wp, classes=chart_classes)
    sns.lmplot(x="total_bill", y="tip", col="time", hue="smoker",
               data=tips)
    jp.Matplotlib(a=wp, classes=chart_classes)
    sns.swarmplot(x="day", y="total_bill", hue="smoker", data=tips)
    jp.Matplotlib(a=wp, classes=chart_classes)
    sns.violinplot(x="day", y="total_bill", hue="smoker", split=True, data=tips)
    jp.Matplotlib(a=wp, classes=chart_classes)

    jp.Div(text='Dots Dataset', a=wp, classes='text-white bg-blue-500 text-center text-xl')
    dots = sns.load_dataset("dots")
    sns.relplot(x="time", y="firing_rate", col="align",
                hue="choice", size="coherence", style="choice",
                facet_kws=dict(sharex=False),
                kind="line", legend="full", data=dots)
    jp.Matplotlib(a=wp, classes=chart_classes)

    jp.Div(text='FMRI Dataset', a=wp, classes='text-white bg-blue-500 text-center text-xl')
    fmri = sns.load_dataset("fmri")
    sns.relplot(x="timepoint", y="signal", col="region",
                hue="event", style="event",
                kind="line", data=fmri)
    jp.Matplotlib(a=wp, classes=chart_classes)

    jp.Div(text='Iris Dataset', a=wp, classes='text-white bg-blue-500 text-center text-xl')
    iris = sns.load_dataset("iris")
    sns.jointplot(x="sepal_length", y="petal_length", data=iris)
    jp.Matplotlib(a=wp, classes=chart_classes)
    sns.pairplot(data=iris, hue="species")
    jp.Matplotlib(a=wp, classes=chart_classes)

    return wp