jiahuimbzuai/CoQuIR huggingface dataset:
//  [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.catplot(x="day", y="total_bill", hue="smoker",
                kind="swarm", data=tips)
    jp.Matplotlib(a=wp, classes=chart_classes)
    sns.catplot(x="day", y="total_bill", hue="smoker",
                kind="violin", 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
//  [python]
def create_metric_bar_chart_comparison(
    df: DataFrame, output_filename: str, max_y_limit: int = None
):
    metric = df.columns[1]
    if max_y_limit:
        max_y_limit = df.max()[metric]
    sns.set(style="whitegrid")
    sns.set_context(
        "paper", rc={"font.size": 14, "axes.titlesize": 32, "axes.labelsize": 18}
    )
    g = sns.factorplot(
        x="",
        y=metric,
        hue="Approach",
        data=df,
        kind="bar",
        size=5,
        aspect=1,
        palette=["skyblue", "sandybrown", "green"],
    )
    g.set(ylim=(0, max_y_limit))
    g.savefig(output_filename)
//  [python]
def create_metric_bar_chart_comparison(
    df: DataFrame, output_filename: str, max_y_limit: int = None
):
    metric = df.columns[1]
    if max_y_limit:
        max_y_limit = df.max()[metric]
    sns.set(style="whitegrid")
    sns.set_context(
        "paper", rc={"font.size": 14, "axes.titlesize": 32, "axes.labelsize": 18}
    )
    g = sns.catplot(
        x="",
        y=metric,
        hue="Approach",
        data=df,
        kind="bar",
        height=5,
        aspect=1,
        palette=["skyblue", "sandybrown", "green"],
    )
    g.set(ylim=(0, max_y_limit))
    g.savefig(output_filename)
//  [python]
def sig_bar_plot(df, max_y=1.0):
    '''
    Generates a bar plot with average MSE and significance stats per embedding type.
    '''
    sig_labels = [row['Significance'] for _, row in df.iterrows() if row['Significance'] != '-']
    # Make the barplot
    with sns.plotting_context("paper", font_scale=1.1):
        bar = sns.barplot(x="Embedding", y="Ø MSE", hue="Type", data=df, palette=["C1", "#dddddd"], ci=None)
        bar.set(ylim=(0, max_y + 0.1))

        # Loop over the bars
        patches = list(bar.patches)
        half_patch_num = len(patches)//2

        # proper embeddings (get xkcd colors)
        for idx, thisbar in enumerate(patches[:half_patch_num]):
            thisbar.set_width(0.95 * thisbar.get_width())
            thisbar.set_color(xkcd_colors[idx])
            x = thisbar.get_x()
            y = thisbar.get_height() + 0.01
            bar.annotate('({})\n{:.2f}'.format(sig_labels[idx], thisbar.get_height()), (x, y))   

        # random embeddings (grey)
        for idx, thisbar in enumerate(patches[half_patch_num:]):
            thisbar.set_width(0.95 * thisbar.get_width())
            thisbar.set_hatch('-')
            x = thisbar.get_x()
            y = thisbar.get_height() + 0.01
            bar.annotate('{:.2f}'.format(thisbar.get_height()), (x, y)) 
        
        # Adjust the margins
        plt.subplots_adjust(bottom= 0.2, top = 0.8)
        plt.xticks(rotation=45)

        with BytesIO() as figfile:
            plt.gcf().set_size_inches(18, 8)
            plt.savefig(figfile, format='png', dpi=300, bbox_inches="tight")
            figfile.seek(0)  # rewind to beginning of file
            statsfig_b64 = base64.b64encode(figfile.getvalue()).decode('utf8')
    return statsfig_b64
//  [python]
def sig_bar_plot(df, max_y=1.0):
    '''
    Generates a bar plot with average MSE and significance stats per embedding type.
    '''
    sig_labels = [row['Significance'] for _, row in df.iterrows() if row['Significance'] != '-']
    # Make the barplot
    with sns.plotting_context("paper", font_scale=1.1):
        bar = sns.catplot(x="Embedding", y="Ø MSE", hue="Type", kind="bar", data=df, palette=["C1", "#dddddd"], legend=False)
        bar.ax.set(ylim=(0, max_y + 0.1))

        # Loop over the bars
        patches = list(bar.ax.patches)
        half_patch_num = len(patches)//2

        # proper embeddings (get xkcd colors)
        for idx, thisbar in enumerate(patches[:half_patch_num]):
            thisbar.set_width(0.95 * thisbar.get_width())
            thisbar.set_color(xkcd_colors[idx])
            x = thisbar.get_x()
            y = thisbar.get_height() + 0.01
            bar.ax.annotate('({})\n{:.2f}'.format(sig_labels[idx], thisbar.get_height()), (x, y))   

        # random embeddings (grey)
        for idx, thisbar in enumerate(patches[half_patch_num:]):
            thisbar.set_width(0.95 * thisbar.get_width())
            thisbar.set_hatch('-')
            x = thisbar.get_x()
            y = thisbar.get_height() + 0.01
            bar.ax.annotate('{:.2f}'.format(thisbar.get_height()), (x, y)) 
        
        # Adjust the margins
        plt.subplots_adjust(bottom= 0.2, top = 0.8)
        plt.xticks(rotation=45)

        with BytesIO() as figfile:
            bar.fig.set_size_inches(18, 8)
            plt.savefig(figfile, format='png', dpi=300, bbox_inches="tight")
            figfile.seek(0)  # rewind to beginning of file
            statsfig_b64 = base64.b64encode(figfile.getvalue()).decode('utf8')
    return statsfig_b64
//  [python]
def plot_res_between_groups(self, flag=None, figsize=None):
        """Summary

            Plot in one plot with results between groups.

        """
        if flag is None:
            flag = self.flags
        elif isinstance(flag, (int, str)):
            flag = [flag]

        for i in flag:
            if i not in self.res_between_group_dict:
                self.res_between_group_dict[i] = self._calc_all_res_between_group(self.input_data, i)

        df = pd.concat(self.res_between_group_dict, names=['flag', 'cycle'])
        df = df[df.index.get_level_values('flag').isin(self.flags)]

        if isinstance(df, pd.core.series.Series):
            df = df.to_frame()

        if figsize is None:
            figsize = (10 * df.shape[1], 5)

        res_cycles = df.reset_index().melt(id_vars=['flag', 'cycle'])
        res_cycles['cycle'] = res_cycles['cycle'].astype(int)

        g = sn.factorplot(data=res_cycles, x='flag', y='value',
                       col='variable', hue='cycle', kind='bar', ci=None,
                       alpha=0.3, dodge=True, sharex=True, sharey=False,
                       margin_titles=True, legend=True, palette=self.palette0)

        g.fig.set_size_inches(figsize[0], figsize[1])
        g.fig.tight_layout(rect=(0, 0, 1, .97))
        return g
//  [python]
def plot_res_between_groups(self, flag=None, figsize=None):
        """Summary

            Plot in one plot with results between groups.

        """
        if flag is None:
            flag = self.flags
        elif isinstance(flag, (int, str)):
            flag = [flag]

        for i in flag:
            if i not in self.res_between_group_dict:
                self.res_between_group_dict[i] = self._calc_all_res_between_group(self.input_data, i)

        df = pd.concat(self.res_between_group_dict, names=['flag', 'cycle'])
        df = df[df.index.get_level_values('flag').isin(self.flags)]

        if isinstance(df, pd.core.series.Series):
            df = df.to_frame()

        if figsize is None:
            figsize = (10 * df.shape[1], 5)

        res_cycles = df.reset_index().melt(id_vars=['flag', 'cycle'])
        res_cycles['cycle'] = res_cycles['cycle'].astype(int)

        g = sn.catplot(data=res_cycles, x='flag', y='value',
                       col='variable', hue='cycle', kind='bar', ci=None,
                       alpha=0.3, dodge=True, sharex=True, sharey=False,
                       margin_titles=True, legend=True, palette=self.palette0)

        g.fig.set_size_inches(figsize[0], figsize[1])
        g.fig.tight_layout(rect=(0, 0, 1, .97))
        return g
//  [python]
def histogram(self) -> None:
        """Creates histogram from document counts distribution by language or domain"""
        df = pd.DataFrame.from_dict(self.doc_content_distribution, orient="index")
        df.index.name = "Lang."
        df.sort_values(by=["Lang."], inplace=True)
        df.sort_index(axis=1, inplace=True)
        df['Total'] = df.sum(axis=1)
        print(df.convert_dtypes().reset_index())
        with open("content_type_count.tex", "w") as file:
            file.write(
                df.fillna(0).reset_index().convert_dtypes().applymap(lambda x: f'{x:,d}' if isinstance(x, int) else x).to_latex(index=False)
            )
        df.fillna(0).convert_dtypes().to_csv("content_type_count.csv")

        df.drop('Total', axis=1, inplace=True)
        df = pd.melt(
            df.reset_index(),
            id_vars="Lang.",
            var_name="Content Type",
            value_name="Count",
        )
        df["Log Count"] = np.log10(df["Count"])
        sns.set_style("whitegrid")
        sns.barplot(
            x="Lang.",
            y="Log Count",
            hue="Content Type",
            data=df,
            height=2.5,
            aspect=6,
        )

        plt.xticks(
            rotation=90,
            horizontalalignment="center"
        )
        plt.legend(bbox_to_anchor=(1.02, 1), loc="upper left", borderaxespad=0)
        plt.ylabel("Log Count")
        plt.tight_layout()
        plt.autoscale()
        plt.savefig("content_type_count.png", dpi=200)
        plt.show()
//  [python]
def histogram(self) -> None:
        """Creates histogram from document counts distribution by language or domain"""
        df = pd.DataFrame.from_dict(self.doc_content_distribution, orient="index")
        df.index.name = "Lang."
        df.sort_values(by=["Lang."], inplace=True)
        df.sort_index(axis=1, inplace=True)
        df['Total'] = df.sum(axis=1)
        print(df.convert_dtypes().reset_index())
        with open("content_type_count.tex", "w") as file:
            file.write(
                df.fillna(0).reset_index().convert_dtypes().applymap(lambda x: f'{x:,d}' if isinstance(x, int) else x).to_latex(index=False)
            )
        df.fillna(0).convert_dtypes().to_csv("content_type_count.csv")

        df.drop('Total', axis=1, inplace=True)
        df = pd.melt(
            df.reset_index(),
            id_vars="Lang.",
            var_name="Content Type",
            value_name="Count",
        )
        df["Log Count"] = np.log10(df["Count"])
        sns.set_style("whitegrid")
        sns.catplot(
            x="Lang.",
            y="Log Count",
            hue="Content Type",
            data=df,
            kind="bar",
            height=2.5,
            aspect=6,
            legend=False,
        )

        plt.xticks(
            rotation=90,
            horizontalalignment="center"
        )
        plt.legend(bbox_to_anchor=(1.02, 1), loc="upper left", borderaxespad=0)
        plt.ylabel("Log Count")
        plt.tight_layout()
        plt.autoscale()
        plt.savefig("content_type_count.png", dpi=200)
        plt.show()
//  [python]
def plot_res_within_group(self, flag, figsize=None, calc_all=True):
        """Summary

            Plot in several plots with results within group.

        Args:
            cycles (None, optional): Description

        """
        if calc_all:
            for i in self.flags:
                self.res_within_group_dict[i] = self._calc_all_res_within_group(self.input_data, i)
        else:
            if flag not in self.res_between_group_dict:
                self.res_within_group_dict[flag] = self._calc_all_res_within_group(self.input_data, flag)

        if figsize is None:
            figsize = (16, 2 * len(self.ts_cycle_list))

        df = self.res_within_group_dict[flag]
        df.index.name = 'segment'
        res = df.stack(['type', 'cycle']).reset_index().rename(
            columns={0: 'value'})
        res['cycle'] = res['cycle'].astype(int)
        g = sn.factorplot(data=res, x='segment', y='value', row='cycle', col='type',
                       kind='bar', alpha=0.8, dodge=True, sharex=False, sharey=False,
                       size=2, legend_out=True, margin_titles=True,
                       palette=self.palette1)
        # g.fig.suptitle(self.fig_title_by_flags[flag], y=0.99, fontsize=12)
        g.fig.set_size_inches(figsize[0], figsize[1])
        g.fig.tight_layout(rect=(0, 0, 1, 0.97))
        g.add_legend()
        return g
//  [python]
def plot_res_within_group(self, flag, figsize=None, calc_all=True):
        """Summary

            Plot in several plots with results within group.

        Args:
            cycles (None, optional): Description

        """
        if calc_all:
            for i in self.flags:
                self.res_within_group_dict[i] = self._calc_all_res_within_group(self.input_data, i)
        else:
            if flag not in self.res_between_group_dict:
                self.res_within_group_dict[flag] = self._calc_all_res_within_group(self.input_data, flag)

        if figsize is None:
            figsize = (16, 2 * len(self.ts_cycle_list))

        df = self.res_within_group_dict[flag]
        df.index.name = 'segment'
        res = df.stack(['type', 'cycle']).reset_index().rename(
            columns={0: 'value'})
        res['cycle'] = res['cycle'].astype(int)
        g = sn.catplot(data=res, x='segment', y='value', row='cycle', col='type',
                       kind='bar', alpha=0.8, dodge=True, sharex=False, sharey=False,
                       height=2, legend_out=True, margin_titles=True,
                       palette=self.palette1)
        # g.fig.suptitle(self.fig_title_by_flags[flag], y=0.99, fontsize=12)
        g.fig.set_size_inches(figsize[0], figsize[1])
        g.fig.tight_layout(rect=(0, 0, 1, 0.97))
        g.add_legend()
        return g
//  [python]
def make_chart(name, series, dtype):
    FLOOR_DATE = datetime.datetime(1960, 1, 1)
    CEILING_DATE = datetime.datetime.today()
    img = BytesIO()
    # Setting figure sizes in seaborn is a bit weird:
    # https://stackoverflow.com/a/23973562/559140
    if is_categorical_dtype(dtype):
        sns.set_style("ticks")
        sns.factorplot(
            x=name, data=series.to_frame(), kind="count", size=3, aspect=3 / 2
        )
        plt.xticks(rotation=45)
    elif is_bool_dtype(dtype):
        sns.set_style("ticks")
        sns.factorplot(x=name, data=series.to_frame(), kind="count", size=2, aspect=1)
        plt.xticks(rotation=45)
    elif is_datetime64_dtype(dtype):
        # Early dates are dummy values; I don't know what late dates
        # are but presumably just dud data
        series = series[(series > FLOOR_DATE) & (series <= CEILING_DATE)]
        # Set bin numbers appropriate to the time window
        delta = series.max() - series.min()
        if delta.days <= 31:
            bins = delta.days
        elif delta.days <= 365 * 10:
            bins = delta.days / 31
        else:
            bins = delta.days / 365
        if bins < 1:
            bins = 1
        fig = plt.figure(figsize=(5, 2))
        ax = fig.add_subplot(111)
        series.hist(bins=int(bins), ax=ax)
        plt.xticks(rotation=45, ha="right")
    elif is_numeric_dtype(dtype):
        # Trim percentiles and negatives which are usually bad data
        series = series.fillna(0)
        series = series[
            (series < np.percentile(series, 95))
            & (series > np.percentile(series, 5))
            & (series > 0)
        ]
        fig = plt.figure(figsize=(5, 2))
        ax = fig.add_subplot(111)
        sns.distplot(series, kde=False, ax=ax)
        plt.xticks(rotation=45)
    else:
        raise ValueError()

    plt.savefig(img, transparent=True, bbox_inches="tight")
    img.seek(0)
    plt.close()
    return base64.b64encode(img.read()).decode("UTF-8")
//  [python]
def make_chart(name, series, dtype):
    FLOOR_DATE = datetime.datetime(1960, 1, 1)
    CEILING_DATE = datetime.datetime.today()
    img = BytesIO()
    # Setting figure sizes in seaborn is a bit weird:
    # https://stackoverflow.com/a/23973562/559140
    if is_categorical_dtype(dtype):
        sns.set_style("ticks")
        sns.catplot(
            x=name, data=series.to_frame(), kind="count", height=3, aspect=3 / 2
        )
        plt.xticks(rotation=45)
    elif is_bool_dtype(dtype):
        sns.set_style("ticks")
        sns.catplot(x=name, data=series.to_frame(), kind="count", height=2, aspect=1)
        plt.xticks(rotation=45)
    elif is_datetime64_dtype(dtype):
        # Early dates are dummy values; I don't know what late dates
        # are but presumably just dud data
        series = series[(series > FLOOR_DATE) & (series <= CEILING_DATE)]
        # Set bin numbers appropriate to the time window
        delta = series.max() - series.min()
        if delta.days <= 31:
            bins = delta.days
        elif delta.days <= 365 * 10:
            bins = delta.days / 31
        else:
            bins = delta.days / 365
        if bins < 1:
            bins = 1
        fig = plt.figure(figsize=(5, 2))
        ax = fig.add_subplot(111)
        series.hist(bins=int(bins), ax=ax)
        plt.xticks(rotation=45, ha="right")
    elif is_numeric_dtype(dtype):
        # Trim percentiles and negatives which are usually bad data
        series = series.fillna(0)
        series = series[
            (series < np.percentile(series, 95))
            & (series > np.percentile(series, 5))
            & (series > 0)
        ]
        fig = plt.figure(figsize=(5, 2))
        ax = fig.add_subplot(111)
        sns.distplot(series, kde=False, ax=ax)
        plt.xticks(rotation=45)
    else:
        raise ValueError()

    plt.savefig(img, transparent=True, bbox_inches="tight")
    img.seek(0)
    plt.close()
    return base64.b64encode(img.read()).decode("UTF-8")
//  [python]
def integration_metrics(models, model_names, x = 'score', hue='Model', edgecolor='#474747ff', height=5, aspect=1.):
    
    batch_correction = pd.DataFrame(pd.concat([pd.DataFrame(m).T for m in models], axis=0))
    model_names_ = []
    for mn in model_names:
        model_names_ = model_names_ + pd.DataFrame(models[0]).shape[1]*[mn]
    batch_correction['Model'] = model_names_ 
    batch_correction['score'] = batch_correction[0]
    batch_correction = batch_correction.drop(0, 1)
    batch_correction['Metric'] = batch_correction.index.values
    
    ax = sns.factorplot(
        data=batch_correction, 
        x='Metric', y=x, kind='bar', hue = hue,
        alpha=1, orient='v', size=height, aspect=aspect, palette = 'Set1', edgecolor=edgecolor
    )
    
    ax.set(ylim=(0, 1))
    
    return ax
//  [python]
def integration_metrics(models, model_names, x = 'score', hue='Model', edgecolor='#474747ff', height=5, aspect=1.):
    
    batch_correction = pd.DataFrame(pd.concat([pd.DataFrame(m).T for m in models], axis=0))
    model_names_ = []
    for mn in model_names:
        model_names_ = model_names_ + pd.DataFrame(models[0]).shape[1]*[mn]
    batch_correction['Model'] = model_names_ 
    batch_correction['score'] = batch_correction[0]
    batch_correction = batch_correction.drop(0, 1)
    batch_correction['Metric'] = batch_correction.index.values
    
    ax = sns.catplot(
        data=batch_correction, 
        x='Metric', y=x, kind='bar', hue = hue,
        alpha=1, orient='v', height=height, aspect=aspect, palette = 'Set1', edgecolor=edgecolor
    )
    
    ax.set(ylim=(0, 1))
    
    return ax
//  [python]
def plot_join_summaries(pdf):
    df = get_all_jerrs()
    for st in set(df["samples_type"]):
        cur_df = df[df["samples_type"] == st]
        # plot it in pdf

        fg = sns.factorplot(x="alg", y="cost",
                data=df, row="max_discrete_featurizing_buckets",
                col = "hidden_layer_size", hue="alg", kind="bar",
                order=ALGS_ORDER, hue_order=ALGS_ORDER, ci=95)
        fg.add_legend()

        title = "Join Error"
        fg.fig.suptitle(title,
                x=0.5, y=.99, horizontalalignment='center',
                verticalalignment='top', fontsize = 40)
        fg.despine(left=True)

        plt.tight_layout(rect=[0, 0, 1, 0.95])
        pdf.savefig()
        plt.clf()
//  [python]
def plot_join_summaries(pdf):
    df = get_all_jerrs()
    for st in set(df["samples_type"]):
        cur_df = df[df["samples_type"] == st]
        # plot it in pdf

        fg = sns.catplot(x="alg", y="cost",
                data=df, row="max_discrete_featurizing_buckets",
                col = "hidden_layer_size", hue="alg", kind="bar",
                order=ALGS_ORDER, hue_order=ALGS_ORDER, ci=95)
        fg.add_legend()

        title = "Join Error"
        fg.fig.suptitle(title,
                x=0.5, y=.99, horizontalalignment='center',
                verticalalignment='top', fontsize = 40)
        fg.despine(left=True)

        plt.tight_layout(rect=[0, 0, 1, 0.95])
        pdf.savefig()
        plt.clf()
//  [python]
features_keep = ['volt_amp', 'period', 'time_rdsym', 'time_ptsym']
df_subjects = df_cycles_burst.groupby(['group', 'subject_id']).mean()[features_keep].reset_index()
print(df_subjects)

###############################################################################

feature_names = {'volt_amp': 'Amplitude',
                 'period': 'Period (ms)',
                 'time_rdsym': 'Rise-decay symmetry',
                 'time_ptsym': 'Peak-trough symmetry'}
for feat, feat_name in feature_names.items():
    g = sns.factorplot(x='group', y=feat, data=df_subjects)
    plt.xlabel('')
    plt.xticks(size=20)
    plt.ylabel(feat_name, size=20)
    plt.yticks(size=15)
    plt.tight_layout()
    plt.show()
//  [python]
features_keep = ['volt_amp', 'period', 'time_rdsym', 'time_ptsym']
df_subjects = df_cycles_burst.groupby(['group', 'subject_id']).mean()[features_keep].reset_index()
print(df_subjects)

###############################################################################

feature_names = {'volt_amp': 'Amplitude',
                 'period': 'Period (ms)',
                 'time_rdsym': 'Rise-decay symmetry',
                 'time_ptsym': 'Peak-trough symmetry'}
for feat, feat_name in feature_names.items():
    g = sns.catplot(x='group', y=feat, data=df_subjects)
    plt.xlabel('')
    plt.xticks(size=20)
    plt.ylabel(feat_name, size=20)
    plt.yticks(size=15)
    plt.tight_layout()
    plt.show()
//  [python]
def plot_summary(pdf, df, title):
    fg = sns.factorplot(x="alg", y="loss",
            data=df, row="max_discrete_featurizing_buckets",
            col = "hidden_layer_size", hue="priority", kind="bar",
            hue_order=["no", "yes"])
            # order = ALGS_ORDER, hue_order = ALGS_ORDER)
    # fg.axes[0].legend(loc='upper left')
    fg.add_legend()

    # TODO: set how many queries are there on each table
    # for i, ax in enumerate(fg.axes.flat):
        # tmp = templates[i]
        # sqs = sort_df[sort_df["template"] == tmp]["num_subqueries"].values[0]
        # title = tmp + " ,#subqueries: " + str(sqs)
        # ax.set_title(title)

    # title
    fg.fig.suptitle(title,
            x=0.5, y=.99, horizontalalignment='center',
            verticalalignment='top', fontsize = 40)

    # fg.set(ylim=(0,10.0))
    fg.despine(left=True)

    plt.tight_layout(rect=[0, 0, 1, 0.95])
    pdf.savefig()
    plt.clf()
//  [python]
def plot_summary(pdf, df, title):
    fg = sns.catplot(x="alg", y="loss",
            data=df, row="max_discrete_featurizing_buckets",
            col = "hidden_layer_size", hue="priority", kind="bar",
            hue_order=["no", "yes"])
            # order = ALGS_ORDER, hue_order = ALGS_ORDER)
    # fg.axes[0].legend(loc='upper left')
    fg.add_legend()

    # TODO: set how many queries are there on each table
    # for i, ax in enumerate(fg.axes.flat):
        # tmp = templates[i]
        # sqs = sort_df[sort_df["template"] == tmp]["num_subqueries"].values[0]
        # title = tmp + " ,#subqueries: " + str(sqs)
        # ax.set_title(title)

    # title
    fg.fig.suptitle(title,
            x=0.5, y=.99, horizontalalignment='center',
            verticalalignment='top', fontsize = 40)

    # fg.set(ylim=(0,10.0))
    fg.despine(left=True)

    plt.tight_layout(rect=[0, 0, 1, 0.95])
    pdf.savefig()
    plt.clf()
//  [python]
def plot_line_buffer_sizes(
    sample: pd.DataFrame,
    title: Optional[str] = None,
    filename: Optional[Union[str, os.PathLike]] = None,
    margins: Optional[dict] = None,
    hue: Optional[str] = "mode",
    *,
    order: Optional[Iterable[str]] = None,
    x_column: str = "buffer_size",
    x_label: str = "buffer size (MiB)",
    y_column: str = "rate",
    y_label: str = "download speed (GiB / sec)",
    save: bool = True,
):
    set_sns_style()
    grid = sns.factorplot(
        data=sample,
        x=x_column,
        y=y_column,
        kind="point",
        ci="sd",
        join=True,
        capsize=0.4,
        hue=hue,
        palette="muted",
        linestyles=["-", "--", ":", "-."],
        order=order,
    )

    x_tick_positions = sample[x_column].unique()
    x_tick_positions.sort()
    x_tick_labels = [
        str(Decimal(int(val)) / mebibyte) for val in x_tick_positions
    ]

    limits = tuple(np.percentile(sample[y_column], (2, 98)))
    positions, labels = get_tick_marks(limits=limits)
    for ax in grid.axes.flat:
        ax.set_yticks(positions, labels=labels)
        ax.set_xticks(ax.get_xticks(), labels=x_tick_labels)

    grid.set(xlabel=x_label, ylabel=y_label)
    fig: plt.Figure = grid.fig

    if title:
        fig.suptitle(title)

    if save:
        if not filename:
            raise ValueError("Must specify filename when saving")
        fallback_margins = dict(left=0.125)
        save_figure(fig, filename, margins, fallback_margins)
    else:
        return grid
//  [python]
def plot_line_buffer_sizes(
    sample: pd.DataFrame,
    title: Optional[str] = None,
    filename: Optional[Union[str, os.PathLike]] = None,
    margins: Optional[dict] = None,
    hue: Optional[str] = "mode",
    *,
    order: Optional[Iterable[str]] = None,
    x_column: str = "buffer_size",
    x_label: str = "buffer size (MiB)",
    y_column: str = "rate",
    y_label: str = "download speed (GiB / sec)",
    save: bool = True,
):
    set_sns_style()
    grid = sns.catplot(
        sample,
        x=x_column,
        y=y_column,
        kind="point",
        errorbar="sd",
        err_kws={"alpha": 0.6, "linewidth": 1},
        capsize=0.4,
        hue=hue,
        linestyles=["-", "--", ":", "-."],
        order=order,
    )

    x_tick_positions = sample[x_column].unique()
    x_tick_positions.sort()
    x_tick_labels = [
        str(Decimal(int(val)) / mebibyte) for val in x_tick_positions
    ]

    limits = tuple(np.percentile(sample[y_column], (2, 98)))
    positions, labels = get_tick_marks(limits=limits)
    for ax in grid.axes.flat:
        ax.set_yticks(positions, labels=labels)
        ax.set_xticks(ax.get_xticks(), labels=x_tick_labels)

    grid.set(xlabel=x_label, ylabel=y_label)
    fig: plt.Figure = grid.figure

    if title:
        fig.suptitle(title)

    if save:
        if not filename:
            raise ValueError("Must specify filename when saving")
        fallback_margins = dict(left=0.125)
        save_figure(fig, filename, margins, fallback_margins)
    else:
        return grid
//  [python]
def loadings_sns(self, **kwargs):
        if 'row' not in kwargs:
            kwargs['row'] = 'PC'
        g = sns.factorplot(data=self.pca.results['feature_contributions'], x='peak_label', y='Coefficient', kind='bar', **kwargs)
        plt.tight_layout()
        return g
//  [python]
def loadings_sns(self, **kwargs):
        if 'row' not in kwargs:
            kwargs['row'] = 'PC'
        g = sns.catplot(data=self.pca.results['feature_contributions'], x='peak_label', y='Coefficient', kind='bar', **kwargs)
        plt.tight_layout()
        return g
//  [python]
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.factorplot(x="time", y="pulse", hue="kind", data=exercise)
//  [python]
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
//  [python]
sns.heatmap(df.corr(), annot=True, fmt='.2f')
sns.pairplot(df)
sns.pairplot(df, hue='categorical')
sns.jointplot(x='data science', y='machine learning', data=df)
sns.violinplot(x='categorical', y='data science', data=df)
//  [python]
sns.heatmap(df.corr(), annot=True, fmt='.2f')
sns.pairplot(df)
sns.pairplot(df, hue='categorical')
sns.jointplot(x='data science', y='machine learning', data=df)
sns.catplot(x='categorical', y='data science', kind='violin', data=df)
//  [python]
sns.set_theme()

mpg = sns.load_dataset("mpg")
sns.catplot(
    data=mpg, x="cylinders", y="acceleration", hue="weight",
    native_scale=True, zorder=1
)
//  [python]
def plot5_1_1():
    times = pd.read_csv("output/5.1_output.csv")
    times["time.s"] = times["time.ms"] / 1000
    g = sns.factorplot(x = "connector", y="time.s", hue = "connector", col = "model", sharey=False, data = times, kind="box")
    g.set_titles("{col_name}").set(ylabel="Time in Seconds", xlabel = "Runs")
    savefig('output/5_1_1.eps')
    os.system(os.path.join("output","5_1_1.eps"))
//  [python]
def plot5_1_1():
    times = pd.read_csv("output/5.1_output.csv")
    times["time.s"] = times["time.ms"] / 1000
    g = sns.catplot(x = "connector", y="time.s", hue = "connector", col = "model", sharey=False, data = times, kind="box")
    g.set_titles("{col_name}").set(ylabel="Time in Seconds", xlabel = "Runs")
    savefig('output/5_1_1.eps')
    os.system(os.path.join("output","5_1_1.eps"))
//  [python]
def plot5_1_2():
    memory = pd.read_csv("output/5.1_output.csv")
    memory["max.memory.used.GiB"] = memory["max.memory.used.b"] / (1024**3)
    g = sns.factorplot(x = "connector", y="max.memory.used.GiB", hue = "connector", col = "model", sharey=False, data = memory, kind="box")
    g.set_titles("{col_name}").set(ylabel="Max Memory Used in GiB", xlabel = "Runs")
    savefig('output/5_1_2.eps')
    os.system(os.path.join("output","5_1_2.eps"))
//  [python]
def plot5_1_2():
    memory = pd.read_csv("output/5.1_output.csv")
    memory["max.memory.used.GiB"] = memory["max.memory.used.b"] / (1024**3)
    g = sns.catplot(x = "connector", y="max.memory.used.GiB", hue = "connector", col = "model", sharey=False, data = memory, kind="box")
    g.set_titles("{col_name}").set(ylabel="Max Memory Used in GiB", xlabel = "Runs")
    savefig('output/5_1_2.eps')
    os.system(os.path.join("output","5_1_2.eps"))
//  [python]
def overview_plots(df_haplo, dname_out):
    if df_haplo.empty:
        print("Warning: df_haplo is empty")
        return

    df_haplo["seq_len"] = df_haplo["sequence"].str.len()
    df_long = format_params(
        pd.melt(df_haplo, id_vars=["method", "params", "replicate"])
    )
    df_long = df_long[df_long["variable"] != "sequence"]

    g = sns.FacetGrid(
        data=df_long,
        col="variable",
        row=None,
        hue="method",
        sharey=False,
        height=10,
        aspect=1
    )
    g.map(sns.boxplot, "params", "value")
    g.map_dataframe(
        sns.stripplot, x="params", y="value", hue="method", color="k", dodge=True
    )
    g.add_legend()

    for ax in g.axes.flat:
        ax.set_xticklabels(
            ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor"
        )

    g.savefig(dname_out / "overview.pdf")
//  [python]
def overview_plots(df_haplo, dname_out):
    if df_haplo.empty:
        print("Warning: df_haplo is empty")
        return

    df_haplo["seq_len"] = df_haplo["sequence"].str.len()
    df_long = format_params(
        pd.melt(df_haplo, id_vars=["method", "params", "replicate"])
    )
    df_long = df_long[df_long["variable"] != "sequence"]

    g = sns.catplot(
        data=df_long,
        x="params",
        y="value",
        hue="method",
        col="variable",
        kind="box",
        sharey=False,
        height=10,
    )
    g.map_dataframe(
        sns.stripplot, x="params", y="value", hue="method", color="k", dodge=True
    )

    for ax in g.axes.flat:
        ax.set_xticklabels(
            ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor"
        )

    g.savefig(dname_out / "overview.pdf")
//  [python]
def plot_data(pd_df, experiment_type, out_path="test.png"):
    sns.set_theme(style="whitegrid")
    if experiment_type == "suppression":
        title = "Suppressing knowledge neurons"
    elif experiment_type == "enhancement":
        title = "Enhancing knowledge neurons"
    else:
        raise ValueError
    # Draw a nested barplot by species and sex
    g = sns.factorplot(
        data=pd_df,
        kind="bar",
        x="relation_name",
        y="pct_change",
        hue="related",
        ci="sd",
        palette="dark",
        alpha=0.6,
        size=6,
        aspect=4,
    )
    g.despine(left=True)
    g.set_axis_labels("relation name", "Correct probability percentage change")
    g.legend.set_title(title)
    g.savefig(out_path)
//  [python]
def plot_data(pd_df, experiment_type, out_path="test.png"):
    sns.set_theme(style="whitegrid")
    if experiment_type == "suppression":
        title = "Suppressing knowledge neurons"
    elif experiment_type == "enhancement":
        title = "Enhancing knowledge neurons"
    else:
        raise ValueError
    # Draw a nested barplot by species and sex
    g = sns.catplot(
        data=pd_df,
        kind="bar",
        x="relation_name",
        y="pct_change",
        hue="related",
        ci="sd",
        palette="dark",
        alpha=0.6,
        height=6,
        aspect=4,
    )
    g.despine(left=True)
    g.set_axis_labels("relation name", "Correct probability percentage change")
    g.legend.set_title(title)
    g.savefig(out_path)
//  [python]
def plot_escores_game(act_df, game):
    """Plot bar plots for a game, episode score by experiment and metric."""
    g = sns.factorplot(data=act_df,
                    row='metric',
                    col='experiment',
                    col_order=sorted(act_df.experiment.unique()),
                    x='model',
                    order=utils.ROW_ORDER[:-2],
                    y='value',
                    # we had to set sharex to false to make sure that
                    # xticklabels are shown
                    # add redundant hue to ensure colors are the same
                    hue='model',
                    dodge=False,
                    kind='bar',
                    sharey=False,
                    sharex=False,
                    aspect=1.4)
    plt.suptitle(f'Overview of Episode Scores: {game}', y=1.)
    path = utils.create_file_name(game, 'episode', 'plots', '_overview', 'pdf')
    utils.savefig(path)
//  [python]
def plot_escores_game(act_df, game):
    """Plot bar plots for a game, episode score by experiment and metric."""
    g = sns.catplot(act_df,
                    row='metric',
                    col='experiment',
                    col_order=sorted(act_df.experiment.unique()),
                    x='model',
                    order=utils.ROW_ORDER[:-2],
                    y='value',
                    # we had to set sharex to false to make sure that
                    # xticklabels are shown
                    # add redundant hue to ensure colors are the same
                    hue='model',
                    dodge=False,
                    kind='bar',
                    sharey=False,
                    sharex=False,
                    aspect=1.4)
    plt.suptitle(f'Overview of Episode Scores: {game}', y=1.)
    path = utils.create_file_name(game, 'episode', 'plots', '_overview', 'pdf')
    utils.savefig(path)
//  [python]
def plot_lines(df):
    """Plot lineplot comparing models across experiments."""
    aux_df = (df[df.metric == clemmetrics.BENCH_SCORE]
              .groupby(['game', 'model', 'experiment'])
              .mean(numeric_only=True)
              .reset_index())
    g = sns.factorplot(data=aux_df,
                    row='game',
                    row_order=utils.COLUMN_ORDER[1:],
                    hue='model',
                    sharey=False,
                    sharex=False,
                    x='experiment',
                    y='value',
                    kind='point',
                    aspect=2.5)
    g.set(ylim=(-5, 105))
    sns.move_legend(g, loc="upper center", ncols=len(aux_df.model.unique()),
                    bbox_to_anchor=(0.5, 1.03))
    plt.tight_layout()
    path = utils.create_file_name('', 'episode', 'plots', 'lines', 'pdf')
    utils.savefig(path)
//  [python]
def plot_lines(df):
    """Plot lineplot comparing models across experiments."""
    aux_df = (df[df.metric == clemmetrics.BENCH_SCORE]
              .groupby(['game', 'model', 'experiment'])
              .mean(numeric_only=True)
              .reset_index())
    g = sns.catplot(aux_df,
                    row='game',
                    row_order=utils.COLUMN_ORDER[1:],
                    hue='model',
                    sharey=False,
                    sharex=False,
                    x='experiment',
                    y='value',
                    kind='point',
                    aspect=2.5)
    g.set(ylim=(-5, 105))
    sns.move_legend(g, loc="upper center", ncols=len(aux_df.model.unique()),
                    bbox_to_anchor=(0.5, 1.03))
    plt.tight_layout()
    path = utils.create_file_name('', 'episode', 'plots', 'lines', 'pdf')
    utils.savefig(path)
//  [python]
def plot_escores_line_game(act_df, game):
    """Plot lineplot for a game, across experiments."""
    act_df = (act_df.groupby(['model', 'experiment', 'metric'])
                    .mean(numeric_only=True)
                    .reset_index())
    g = sns.factorplot(act_df,
                    row='metric',
                    hue='model',
                    sharey=False,
                    sharex=False,
                    x='experiment',
                    order=sorted(act_df.experiment.unique()),
                    y='value',
                    kind='point',
                    aspect=1.5)
    sns.move_legend(g, loc="upper center", ncols=len(act_df.model.unique()),
                    bbox_to_anchor=(0.5, 1.03))
    plt.suptitle(f'Overview of Episode Scores: {game}', y=1.)
    name = '_overview-lines'
    path = utils.create_file_name(game, 'episode', 'plots', name, 'pdf')
    utils.savefig(path)
//  [python]
def plot_escores_line_game(act_df, game):
    """Plot lineplot for a game, across experiments."""
    act_df = (act_df.groupby(['model', 'experiment', 'metric'])
                    .mean(numeric_only=True)
                    .reset_index())
    g = sns.catplot(act_df,
                    row='metric',
                    hue='model',
                    sharey=False,
                    sharex=False,
                    x='experiment',
                    order=sorted(act_df.experiment.unique()),
                    y='value',
                    kind='point',
                    aspect=1.5)
    sns.move_legend(g, loc="upper center", ncols=len(act_df.model.unique()),
                    bbox_to_anchor=(0.5, 1.03))
    plt.suptitle(f'Overview of Episode Scores: {game}', y=1.)
    name = '_overview-lines'
    path = utils.create_file_name(game, 'episode', 'plots', name, 'pdf')
    utils.savefig(path)
//  [python]
def plot_escores_game_metric(metric_df, game, metric, lims):
    """Plot bar plots for a game and metric, episode score by experiment."""
    g = sns.factorplot(data=metric_df,
                    col='experiment',
                    col_order=sorted(metric_df.experiment.unique()),
                    x='model',
                    order=utils.ROW_ORDER[:-2],
                    y='value',
                    # we had to set sharex to false to make sure that
                    # xticklabels are shown
                    # add redundant hue to ensure colors are the same
                    hue='model',
                    hue_order=utils.ROW_ORDER[:-2],
                    dodge=False,
                    kind='bar',
                    sharey=False,
                    sharex=False,
                    aspect=1.4)
    g.set(ylim=lims)
    plt.suptitle(f'Overview of Episode Scores: {game} | {metric}', y=1.1)
    name = f'_overview_{metric}'
    path = utils.create_file_name(game, 'episode', 'plots', name, 'pdf')
    utils.savefig(path)
//  [python]
def plot_escores_game_metric(metric_df, game, metric, lims):
    """Plot bar plots for a game and metric, episode score by experiment."""
    g = sns.catplot(metric_df,
                    col='experiment',
                    col_order=sorted(metric_df.experiment.unique()),
                    x='model',
                    order=utils.ROW_ORDER[:-2],
                    y='value',
                    # we had to set sharex to false to make sure that
                    # xticklabels are shown
                    # add redundant hue to ensure colors are the same
                    hue='model',
                    hue_order=utils.ROW_ORDER[:-2],
                    dodge=False,
                    kind='bar',
                    sharey=False,
                    sharex=False,
                    aspect=1.4)
    g.set(ylim=lims)
    plt.suptitle(f'Overview of Episode Scores: {game} | {metric}', y=1.1)
    name = f'_overview_{metric}'
    path = utils.create_file_name(game, 'episode', 'plots', name, 'pdf')
    utils.savefig(path)
//  [python]
def codecarbon_plot(data, order_list=None, pipelines=None, country=""):
    """Plot code carbon consume for the results from the benchmark.

    Parameters
    ----------
    data: output of Results.to_dataframe()
        results on datasets
    order_list: list of str | None
        order of pipelines to include in this plot
    pipelines: list of str | None
        pipelines to include in this plot
    country: str
        country to include in the title
    pipelines: list of str | None
        pipelines to include in this plot

    Returns
    -------
    fig: Figure
        Pyplot handle
    """
    data = collapse_session_scores(data)
    unique_ids = data["dataset"].apply(_simplify_names)
    if len(unique_ids) != len(set(unique_ids)):
        log.warning("Dataset names are too similar, turning off name shortening")
    else:
        data["dataset"] = unique_ids

    if pipelines is not None:
        data = data[data.pipeline.isin(pipelines)]

    data = data.rename(columns={"carbon emission": "carbon_emission"})

    fig = sea.barplot(
        data=data,
        x="dataset",
        y="carbon_emission",
        hue="pipeline",
        palette=PIPELINE_PALETTE,
        order=order_list,
    )
    fig.set_title(r"$CO_2$ emission per dataset and algorithm" + country)
    fig.set(yscale="log")
    fig.tight_layout()
    fig.set_ylabel(r"$CO_2$ emission (Log Scale)")
    fig.set_xlabel("Dataset")

    return fig
//  [python]
def codecarbon_plot(data, order_list=None, pipelines=None, country=""):
    """Plot code carbon consume for the results from the benchmark.

    Parameters
    ----------
    data: output of Results.to_dataframe()
        results on datasets
    order_list: list of str | None
        order of pipelines to include in this plot
    pipelines: list of str | None
        pipelines to include in this plot
    country: str
        country to include in the title
    pipelines: list of str | None
        pipelines to include in this plot

    Returns
    -------
    fig: Figure
        Pyplot handle
    """
    data = collapse_session_scores(data)
    unique_ids = data["dataset"].apply(_simplify_names)
    if len(unique_ids) != len(set(unique_ids)):
        log.warning("Dataset names are too similar, turning off name shortening")
    else:
        data["dataset"] = unique_ids

    if pipelines is not None:
        data = data[data.pipeline.isin(pipelines)]

    data = data.rename(columns={"carbon emission": "carbon_emission"})

    fig = sea.catplot(
        kind="bar",
        data=data,
        x="dataset",
        y="carbon_emission",
        hue="pipeline",
        palette=PIPELINE_PALETTE,
        height=8.5,
        hue_order=order_list,
    ).set(title=r"$CO_2$ emission per dataset and algorithm" + country)
    fig.set(yscale="log")
    fig.tight_layout()
    fig.set_ylabels(r"$CO_2$ emission (Log Scale)")
    fig.set_xlabels("Dataset")

    return fig
//  [python]
def plot_escores_line_game_metric(metric_df, game, metric, lims):
    """Plot lineplot for a game, across experiments."""
    g = sns.factorplot(data=metric_df,
                    hue='model',
                    hue_order=utils.ROW_ORDER[:-2],
                    sharey=False,
                    sharex=False,
                    x='experiment',
                    order=sorted(metric_df.experiment.unique()),
                    y='value',
                    kind='point',
                    errorbar=None,
                    aspect=1.5)
    g.set(ylim=lims)
    sns.move_legend(g, loc="center right", bbox_to_anchor=(1.2, 0.5))
    plt.suptitle(f'Overview of Episode Scores: {game}', y=1.1)
    name = f'_overview-lines_{metric}'
    path = utils.create_file_name(game, 'episode', 'plots', name, 'pdf')
    utils.savefig(path)
//  [python]
def plot_escores_line_game_metric(metric_df, game, metric, lims):
    """Plot lineplot for a game, across experiments."""
    g = sns.catplot(metric_df,
                    hue='model',
                    hue_order=utils.ROW_ORDER[:-2],
                    sharey=False,
                    sharex=False,
                    x='experiment',
                    order=sorted(metric_df.experiment.unique()),
                    y='value',
                    kind='point',
                    errorbar=None,
                    aspect=1.5)
    g.set(ylim=lims)
    sns.move_legend(g, loc="center right", bbox_to_anchor=(1.2, 0.5))
    plt.suptitle(f'Overview of Episode Scores: {game}', y=1.1)
    name = f'_overview-lines_{metric}'
    path = utils.create_file_name(game, 'episode', 'plots', name, 'pdf')
    utils.savefig(path)
//  [python]
def plot_volumes(self, approx=False):
        all_volumes = self.get_voronoi_volumes(approx=approx)
        sns.violinplot(all_volumes, ax=self.ax)
//  [python]
def plot_volumes(self, approx=False):
        all_volumes = self.get_voronoi_volumes(approx=approx)
        sns.catplot(all_volumes, ax=self.ax, kind="violin")
//  [python]
def analyze_distribution(data, columns):
    """
    Analyzes the distribution of the values in the given columns
    :param data: The dataframe to be analyzed
    :param columns: The name of the columns to be compared
    :return ax: a plot showing the distribution
    """
    df = data[columns]
    df['condition'] = data['condition']
    df = pd.melt(df, id_vars='condition')
    ax = sns.barplot(x='condition', y='value', hue='variable', data=df)

    return ax
//  [python]
def analyze_distribution(data, columns):
    """
    Analyzes the distribution of the values in the given columns
    :param data: The dataframe to be analyzed
    :param columns: The name of the columns to be compared
    :return ax: a plot showing the distribution
    """
    df = data[columns]
    df['condition'] = data['condition']
    df = pd.melt(df, id_vars='condition')
    ax = sns.catplot(x='condition', y='value', hue='variable', data=df, kind='bar')

    return ax