jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def plot_accuracies_per_tag(title, **kwargs):
    if kwargs is None:
        print("Need accuracies to plot")
        return

    classes = set(sum([list(kwargs[exp].keys()) for exp in kwargs], []))
    classes = list(classes)
    classes.remove("__OVERALL__")
    classes = ["Overall"] + sorted(classes)

    experiments = {}
    for exp in kwargs:
        experiments[exp] = {
            k: kwargs[exp][k] for k in kwargs[exp] if k != "__OVERALL__"
        }
        experiments[exp]["Overall"] = kwargs[exp]["__OVERALL__"]

    df = pd.DataFrame([{"experiment": exp, **experiments[exp]} for exp in experiments])

    df = pd.melt(df, id_vars="experiment", var_name="type", value_name="accuracy")

    fig, ax = plt.subplots(1, 1, figsize=(16, 6))
    plt.sca(ax)
    ax.tick_params(axis="x", rotation=90)
    ax.set_ylim((0.5, 1.05))
    sns.catplot(
        ax=ax,
        x="type",
        y="accuracy",
        hue="experiment",
        data=df,
        kind="bar",
        order=classes,
        legend_out=True,
    )

    handles, labels = ax.get_legend_handles_labels()
    l = ax.legend(
        handles=handles,
        labels=labels,
        title=title,
        frameon=True,
        fancybox=True,
        framealpha=0.8,
        facecolor="#FFFFFF",
    )

    return fig
//  [python]
def factorplot(labels,data,name,):
    sb.set_style("whitegrid",{'grid.linewidth': 3.,
                            'axes.edgecolor':'0.0','grid.edgecolor':'0.0'})
    g = sb.factorplot(labels[0],labels[1],labels[2],kind='bar', data=data,legend_out=False,palette=cp,
                     hue_order = ['Oracle','TACO',"Naive (MLP)","Naive (GRU)"] )

    sb.set_style("whitegrid")

    g.set(ylim = (0,1))
    g.set(xlim=(-0.45, 3.01))
    g.fig.set_size_inches(15,8)
    plt.legend(loc='best')
    g.savefig(name+'.pdf')
//  [python]
def factorplot(labels,data,name,):
    sb.set_style("whitegrid",{'grid.linewidth': 3.,
                            'axes.edgecolor':'0.0','grid.edgecolor':'0.0'})
    g = sb.catplot(x=labels[0],y=labels[1],hue=labels[2],kind='bar', data=data,legend_out=False,palette=cp,
                     hue_order = ['Oracle','TACO',"Naive (MLP)","Naive (GRU)"] )

    sb.set_style("whitegrid")

    g.set(ylim = (0,1))
    g.set(xlim=(-0.45, 3.01))
    g.fig.set_size_inches(15,8)
    plt.legend(loc='best')
    g.savefig(name+'.pdf')
//  [python]
def save_multi_columns_categorical_charts(df, chart, columns, y, hue, title, single_statistic=False,
                                          single_statistic_name=None, kind='point', col='Message Size (Bytes)'):
    filename = chart + ".png"
    print("Creating chart: " + title + ", File name: " + filename)
    add_chart_details(title, filename)
    fig, ax = plt.subplots()
    all_columns = [col, 'Concurrent Users']
    all_columns.extend(columns)
    df_results = df[all_columns]
    df_results = df_results.set_index([col, 'Concurrent Users']).stack().reset_index().rename(
        columns={'level_2': hue, 0: y})
    g = sns.factorplot(x="Concurrent Users", y=y,
                       hue=hue, col=col,
                       data=df_results, kind=kind,
                       size=5, aspect=1, col_wrap=2, legend=False)
    for ax in g.axes.flatten():
        ax.yaxis.set_major_formatter(
            tkr.FuncFormatter(lambda y_value, p: "{:,}".format(y_value)))
    plt.subplots_adjust(top=0.9, left=0.1)
    g.fig.suptitle(title)
    plt.legend(frameon=True)
    if single_statistic:
        leg = None
        # Get legend and remove column name from legend
        for ax in g.axes.flat:
            leg = ax.get_legend()
            if leg is not None:
                break
        if leg is not None:
            for text in leg.texts:
                text.set_text(re.sub(re.escape(single_statistic_name) + r'\s*-\s*', '', text.get_text()))
    plt.savefig(filename)
    plt.clf()
    plt.cla()
    plt.close(fig)
//  [python]
def save_multi_columns_categorical_charts(df, chart, columns, y, hue, title, single_statistic=False,
                                          single_statistic_name=None, kind='point', col='Message Size (Bytes)'):
    filename = chart + ".png"
    print("Creating chart: " + title + ", File name: " + filename)
    add_chart_details(title, filename)
    fig, ax = plt.subplots()
    all_columns = [col, 'Concurrent Users']
    all_columns.extend(columns)
    df_results = df[all_columns]
    df_results = df_results.set_index([col, 'Concurrent Users']).stack().reset_index().rename(
        columns={'level_2': hue, 0: y})
    g = sns.catplot(x="Concurrent Users", y=y,
                       hue=hue, col=col,
                       data=df_results, kind=kind,
                       height=5, aspect=1, col_wrap=2, legend=False)
    for ax in g.axes.flatten():
        ax.yaxis.set_major_formatter(
            tkr.FuncFormatter(lambda y_value, p: "{:,}".format(y_value)))
    plt.subplots_adjust(top=0.9, left=0.1)
    g.fig.suptitle(title)
    plt.legend(frameon=True)
    if single_statistic:
        leg = None
        # Get legend and remove column name from legend
        for ax in g.axes.flat:
            leg = ax.get_legend()
            if leg is not None:
                break
        if leg is not None:
            for text in leg.texts:
                text.set_text(re.sub(re.escape(single_statistic_name) + r'\s*-\s*', '', text.get_text()))
    plt.savefig(filename)
    plt.clf()
    plt.cla()
    plt.close(fig)
//  [python]
def plot_data(df):
    """ simple plots of soil moisture records """

    # melt dataframes three sensor depths into a reading column and depth column
    df = pd.melt(df, id_vars=["NAME", "FID", "SID", "DTG", "DATE", "TEMP"], var_name="DEPTH", value_name="READING")

    for station in df["NAME"].unique():
        sns.factorplot("DATE", "READING", hue="DEPTH", data=df[df["NAME"] == station])
        sns.plt.title(station)
        sns.plt.show()
//  [python]
def plot_data(df):
    """ simple plots of soil moisture records """

    # melt dataframes three sensor depths into a reading column and depth column
    df = pd.melt(df, id_vars=["NAME", "FID", "SID", "DTG", "DATE", "TEMP"], var_name="DEPTH", value_name="READING")

    for station in df["NAME"].unique():
        sns.catplot("DATE", "READING", hue="DEPTH", data=df[df["NAME"] == station])
        sns.plt.title(station)
        sns.plt.show()
//  [python]
def make_factor_plot(df, x, col, title, main_count=main_count, hue=None, ann=True, col_wrap=4):
    """
    Plotting countplot.
    Making annotations is a bit more complicated, because we need to iterate over axes.
    """
    if hue:
        g = sns.factorplot(col, col=x, data=df, kind='count', col_wrap=col_wrap, hue=hue);
    else:
        g = sns.factorplot(col, col=x, data=df, kind='count', col_wrap=col_wrap);
    plt.subplots_adjust(top=0.9);
    plt.suptitle(title);
    ax = g.axes
    plot_dict = prepare_plot_dict(df, x, main_count)
    if ann:
        for a in ax:
            for p in a.patches:
                text = f"{plot_dict[p.get_height()]:.0f}%" if plot_dict[p.get_height()] < 0 else f"+{plot_dict[p.get_height()]:.0f}%"
                a.annotate(text, (p.get_x() + p.get_width() / 2., p.get_height()),
                     ha='center', va='center', fontsize=11, color='green' if plot_dict[p.get_height()] > 0 else 'red', rotation=0, xytext=(0, 10),
                     textcoords='offset points')
//  [python]
def make_factor_plot(df, x, col, title, main_count=main_count, hue=None, ann=True, col_wrap=4):
    """
    Plotting countplot.
    Making annotations is a bit more complicated, because we need to iterate over axes.
    """
    if hue:
        g = sns.catplot(col, col=x, data=df, kind='count', col_wrap=col_wrap, hue=hue);
    else:
        g = sns.catplot(col, col=x, data=df, kind='count', col_wrap=col_wrap);
    plt.subplots_adjust(top=0.9);
    plt.suptitle(title);
    ax = g.axes
    plot_dict = prepare_plot_dict(df, x, main_count)
    if ann:
        for a in ax:
            for p in a.patches:
                text = f"{plot_dict[p.get_height()]:.0f}%" if plot_dict[p.get_height()] < 0 else f"+{plot_dict[p.get_height()]:.0f}%"
                a.annotate(text, (p.get_x() + p.get_width() / 2., p.get_height()),
                     ha='center', va='center', fontsize=11, color='green' if plot_dict[p.get_height()] > 0 else 'red', rotation=0, xytext=(0, 10),
                     textcoords='offset points')
//  [python]
def plot_groups(df, groupby):
    print('hi')
    # subset summary to just mean for plotting
    try:
        df = df[groupby].join(df._get_numeric_data())
    except ValueError:
        numeric_df = df._get_numeric_data()
        drop_cols = [col for col in groupby if col in numeric_df.columns]
        numeric_df = numeric_df.drop(drop_cols, axis = 1)
        df = df[groupby].join(numeric_df)
    df = pandas.melt(df, groupby)
    if len(groupby)== 0:
        p = sns.factorplot(y = 'value', data = df, kind = 'bar', col = 'variable', sharey = False, col_wrap = 3)
    elif len(groupby) == 1:
        p = sns.factorplot(x = groupby[0], y = 'value', data = df, kind = 'bar', col = 'variable', sharey = False, col_wrap = 3)
    elif len(groupby) == 2:
        p = sns.factorplot(x = groupby[0], y = 'value', hue = groupby[1], data = df, kind = 'bar', col = 'variable', sharey = False, col_wrap = 3)
    elif len(groupby) > 2:
        p = sns.factorplot(x = groupby[0], y = 'value', hue = groupby[1], data = df, kind = 'bar', col = 'variable', row = groupby[2], sharey = False)  
        if len(groupby) > 3:
             print('Only grouping by %s, %s and %s' % (groupby[0], groupby[1], groupby[2]))
    plt.show()
    return p
//  [python]
def plot_groups(df, groupby):
    print('hi')
    # subset summary to just mean for plotting
    try:
        df = df[groupby].join(df._get_numeric_data())
    except ValueError:
        numeric_df = df._get_numeric_data()
        drop_cols = [col for col in groupby if col in numeric_df.columns]
        numeric_df = numeric_df.drop(drop_cols, axis = 1)
        df = df[groupby].join(numeric_df)
    df = pandas.melt(df, groupby)
    if len(groupby)== 0:
        p = sns.catplot(y = 'value', data = df, kind = 'bar', col = 'variable', sharey = False, col_wrap = 3)
    elif len(groupby) == 1:
        p = sns.catplot(x = groupby[0], y = 'value', data = df, kind = 'bar', col = 'variable', sharey = False, col_wrap = 3)
    elif len(groupby) == 2:
        p = sns.catplot(x = groupby[0], y = 'value', hue = groupby[1], data = df, kind = 'bar', col = 'variable', sharey = False, col_wrap = 3)
    elif len(groupby) > 2:
        p = sns.catplot(x = groupby[0], y = 'value', hue = groupby[1], data = df, kind = 'bar', col = 'variable', row = groupby[2], sharey = False)  
        if len(groupby) > 3:
             print('Only grouping by %s, %s and %s' % (groupby[0], groupby[1], groupby[2]))
    plt.show()
    return p
//  [python]
df = pd.DataFrame(df)
out_file = "understanding.csv"
df.to_csv(out_file)
logging.info("Dataframe of results written to file: %s" % out_file)
if False:
    import matplotlib.pyplot as plt
    import seaborn as sns
    sns.set_style("whitegrid")
    df.alpha = round(df.alpha, 3)
    sns.factorplot(data=df, x="alpha", y="acc", hue="kind")
    plt.tight_layout()
    plt.show()
//  [python]
df = pd.DataFrame(df)
out_file = "understanding.csv"
df.to_csv(out_file)
logging.info("Dataframe of results written to file: %s" % out_file)
if False:
    import matplotlib.pyplot as plt
    import seaborn as sns
    sns.set_style("whitegrid")
    df.alpha = round(df.alpha, 3)
    sns.catplot(data=df, x="alpha", y="acc", hue="kind", kind="point")
    plt.tight_layout()
    plt.show()
//  [python]
def plot_residuals(table, output=None):
    """Violin(?) plot of residuals (y) from TCGA aCGH log2 (x)."""
    if 'Size' in table.columns:
        sn.factorplot(x='Method', y='Deviation', data=table, kind='box',
                      row='Size', row_order=['>50MB', '5-50MB', '<5MB'],
                      aspect=2.5, showfliers=False)
    else:
        sn.boxplot(x='Method', y='Deviation', data=table, showfliers=False)
    plt.xticks(rotation=30)
    if output:
        plt.savefig(output, format='pdf', bbox_inches='tight')
        print("Wrote", output, file=sys.stderr)
    else:
        plt.show()
//  [python]
def plot_residuals(table, output=None):
    """Violin(?) plot of residuals (y) from TCGA aCGH log2 (x)."""
    if 'Size' in table.columns:
        sn.catplot(x='Method', y='Deviation', data=table, kind='box',
                      row='Size', row_order=['>50MB', '5-50MB', '<5MB'],
                      aspect=2.5, showfliers=False)
    else:
        sn.boxplot(x='Method', y='Deviation', data=table, showfliers=False)
    plt.xticks(rotation=30)
    if output:
        plt.savefig(output, format='pdf', bbox_inches='tight')
        print("Wrote", output, file=sys.stderr)
    else:
        plt.show()
//  [python]
def plot_summary(summary_only, stats_dir, output):
    import seaborn as sns

    rows = parse_data(stats_dir)
    g = sns.factorplot(
        y="result",
        x="score",
        col="experiment",
        data=rows,
        kind="bar",
        ci=None,
        order=ANSWER_PLOT_ORDER,
        size=4,
        col_wrap=4,
        sharex=False,
    )
    for ax in g.axes.flat:
        for label in ax.get_xticklabels():
            label.set_rotation(30)
    plt.subplots_adjust(top=0.93)
    g.fig.suptitle("Feature Ablation Study")
    g.savefig(output, format="png", dpi=200)
//  [python]
def plot_summary(summary_only, stats_dir, output):
    import seaborn as sns
    import matplotlib.pyplot as plt

    rows = parse_data(stats_dir)
    g = sns.catplot(
        y="result",
        x="score",
        col="experiment",
        data=rows,
        kind="bar",
        ci=None,
        order=ANSWER_PLOT_ORDER,
        height=4,
        col_wrap=4,
        sharex=False,
    )
    for ax in g.axes.flat:
        for label in ax.get_xticklabels():
            label.set_rotation(30)
    plt.subplots_adjust(top=0.93)
    g.fig.suptitle("Feature Ablation Study")
    g.savefig(output, format="png", dpi=200)
//  [python]
def _run_interface(self, runtime):
        df = pd.read_csv( self.inputs.in_file  )
        if "measure"  in df.columns:
            plot_type="measure"
        elif "metric"  in df.columns :
            plot_type = "metric"
        else:
            print("Unrecognized data frame")
            exit(1)
        df["sub"]="sub: "+df["sub"].map(str)+" task: "+df["task"].map(str)+" ses: "+df["ses"].map(str) 
        print(df)
        plt.clf()
        fig, ax = plt.subplots()
        plt.figure(1)
        nROI = len(np.unique(df.roi))

        if plot_type == "measure" : 
            unique_measure =np.unique(df.measure)
            nMeasure = np.unique(unique_measure)

        unique_metric = np.unique(df.metric)
        nMetric = len(unique_metric)
        for roi, i in zip(np.unique(df.roi), range(nROI)):
            df0=df[ (df.roi==roi)  ]
            for metric in unique_metric :
                x=df0.value[df.metric == metric]

            if plot_type == "measure" : 
                sns.factorplot(x="metric", col="measure", y="value", kind="swarm",  data=df0, legend=False, hue="sub")
            else : 
                sns.factorplot(x="metric", y="value",   data=df0,  kind="swarm",  hue="sub")
            plt.ylabel('')
            plt.xlabel('')

            ax.spines['top'].set_visible(False)
            ax.spines['right'].set_visible(False)
            plt.ylim([-0.05,1.05])
            plt.legend(bbox_to_anchor=(1.05, 1), loc="upper right", ncol=1, prop={'size': 6})
        if not isdefined( self.inputs.out_file) :
            self.inputs.out_file = self._gen_output()
        print('Out file:', self.inputs.out_file)
        #plt.tight_layout()
        plt.savefig(self.inputs.out_file, bbox_inches="tight", dpi=300, width=2000)
        plt.clf()
        return runtime
//  [python]
def _run_interface(self, runtime):
        df = pd.read_csv( self.inputs.in_file  )
        if "measure"  in df.columns:
            plot_type="measure"
        elif "metric"  in df.columns :
            plot_type = "metric"
        else:
            print("Unrecognized data frame")
            exit(1)
        df["sub"]="sub: "+df["sub"].map(str)+" task: "+df["task"].map(str)+" ses: "+df["ses"].map(str) 
        print(df)
        plt.clf()
        fig, ax = plt.subplots()
        plt.figure(1)
        nROI = len(np.unique(df.roi))

        if plot_type == "measure" : 
            unique_measure =np.unique(df.measure)
            nMeasure = np.unique(unique_measure)

        unique_metric = np.unique(df.metric)
        nMetric = len(unique_metric)
        for roi, i in zip(np.unique(df.roi), range(nROI)):
            df0=df[ (df.roi==roi)  ]
            for metric in unique_metric :
                x=df0.value[df.metric == metric]

            if plot_type == "measure" : 
                sns.catplot(x="metric", col="measure", y="value", kind="swarm",  data=df0, legend=False, hue="sub")
            else : 
                sns.catplot(x="metric", y="value",   data=df0,  kind="swarm",  hue="sub")
            plt.ylabel('')
            plt.xlabel('')

            ax.spines['top'].set_visible(False)
            ax.spines['right'].set_visible(False)
            plt.ylim([-0.05,1.05])
            plt.legend(bbox_to_anchor=(1.05, 1), loc="upper right", ncol=1, prop={'size': 6})
        if not isdefined( self.inputs.out_file) :
            self.inputs.out_file = self._gen_output()
        print('Out file:', self.inputs.out_file)
        #plt.tight_layout()
        plt.savefig(self.inputs.out_file, bbox_inches="tight", dpi=300, width=2000)
        plt.clf()
        return runtime
//  [python]
def explore_anscombe(summary):

    statistical = ('mean', 'var', 'std')
    grouped = anscombe.groupby('dataset')

    col = None

    if summary in statistical:
        summarized = getattr(grouped, summary)()
        tidy = summarized.unstack().reset_index()
        tidy = tidy.rename(columns={'level_0': 'variable', 0: summary})
        col = 'variable'
    else:
        if summary.endswith('correlation'):
            method = summary.split()[0].lower()
            summarized = grouped.apply(
                lambda df: df['x'].corr(df['y'], method=method))
        elif summary.endswith('distance'):
            metric = getattr(distance, summary.split()[0].lower())
            summarized = grouped.apply(lambda df: metric(df['x'], df['y']))
        tidy = summarized.reset_index()
        tidy = tidy.rename(columns={'index': 'variable', 0: summary})
    print(summarized.T)

    g = sns.factorplot(data=tidy, col=col, x='dataset',
                       y=summary, kind='bar', size=3, zorder=-1)
    for ax in g.axes.flat:
        # add a white grid on top
        ax.grid(axis='y', color='white', zorder=2)
    g.fig.suptitle(f"summary: {summary}")
//  [python]
def explore_anscombe(summary):

    statistical = ('mean', 'var', 'std')
    grouped = anscombe.groupby('dataset')

    col = None

    if summary in statistical:
        summarized = getattr(grouped, summary)()
        tidy = summarized.unstack().reset_index()
        tidy = tidy.rename(columns={'level_0': 'variable', 0: summary})
        col = 'variable'
    else:
        if summary.endswith('correlation'):
            method = summary.split()[0].lower()
            summarized = grouped.apply(
                lambda df: df['x'].corr(df['y'], method=method))
        elif summary.endswith('distance'):
            metric = getattr(distance, summary.split()[0].lower())
            summarized = grouped.apply(lambda df: metric(df['x'], df['y']))
        tidy = summarized.reset_index()
        tidy = tidy.rename(columns={'index': 'variable', 0: summary})
    print(summarized.T)

    g = sns.catplot(data=tidy, col=col, x='dataset',
                       y=summary, kind='bar', height=3, aspect=1)
    for ax in g.axes.flat:
        # add a white grid on top
        ax.grid(axis='y', color='white', zorder=2)
    g.fig.suptitle(f"summary: {summary}")
//  [python]
def Bar_Plot_3D(dataf, attribute_x, attribute_c, category_column):              # categorical data - 3D plot
    # Key:
    #       dataf - the dataframe that holds the entire dataset
    #       attribute_x - attribute on the x-axis
    #       attribute_c - attribute separating bar plots - for each value of this categorical attribute a different bar plot will be plotted
    #       category_column - the column name in dataframe holding the class labels - this will act as our third dimension
    #       Note: all three attributes are categorical
    
    # Plot:
    #       Visualizing 3-D categorical data using bar plots
    #       Leveraging the concepts of hue and facets/subplots to support the additional third dimension
    #       Clearly shows the frequency pertaining to each of the dimensions
    #       Easy and effective in understanding relevant insights.

    fc = sns.factorplot(x=attribute_x, hue=category_column, col=attribute_c,
                        data=dataf, kind="count",
                        palette={"red": "#FF9999", "white": "#FFE888"})
    plt.show()
//  [python]
def Bar_Plot_3D(dataf, attribute_x, attribute_c, category_column):              # categorical data - 3D plot
    fc = sns.catplot(x=attribute_x, hue=category_column, col=attribute_c,
                        data=dataf, kind="count",
                        palette={"red": "#FF9999", "white": "#FFE888"})
    plt.show()
//  [python]
def plot_data(data, kind=None, **kwargs):
    if kind in ['dis', 'dissimilarity']:
        if isinstance(data, dict): data = data.values()[0]
        g = sns.heatmap(data, **kwargs)
    elif kind == 'mds':
        g = plot.mdsplot(data, **kwargs)
    elif kind in ['clust', 'cluster']:
        g = sns.factorplot('layer', 'dissimilarity', data=df, kind='point')
    elif kind in ['lin', 'linear_clf']:
        g = sns.factorplot('layer', 'accuracy', data=df, kind='point')
        if chance in kwargs:
            ax.axhline(kwargs['chance'], ls='--', c='.2')
    else:
        try:
            sns.factorplot(x='layers', y=data.columns[-1], data=data)
        except:
            raise ValueError('Plot kind "{}" not recognized.'.format(kind))
    return g
//  [python]
def plot_data(data, kind=None, **kwargs):
    if kind in ['dis', 'dissimilarity']:
        if isinstance(data, dict): data = data.values()[0]
        g = sns.heatmap(data, **kwargs)
    elif kind == 'mds':
        g = plot.mdsplot(data, **kwargs)
    elif kind in ['clust', 'cluster']:
        g = sns.catplot('layer', 'dissimilarity', data=df, kind='point')
    elif kind in ['lin', 'linear_clf']:
        g = sns.catplot('layer', 'accuracy', data=df, kind='point')
        if chance in kwargs:
            ax.axhline(kwargs['chance'], ls='--', c='.2')
    else:
        try:
            sns.catplot(x='layers', y=data.columns[-1], data=data)
        except:
            raise ValueError('Plot kind "{}" not recognized.'.format(kind))
    return g
//  [python]
def compare(func, test_ims, layer='output', models=None,
            plot=True, save=False, html=None,
            *func_args, **func_kwargs):

    if models is None: models = KNOWN_MODELS.keys()

    df = []
    mnames = []
    for model in models:
        m = Model(model)
        resps = m.run(test_ims=test_ims, layers=layer)
        out = func(resps, *func_args, **func_kwargs)
        df.append(pandas.DataFrame(out))
        mnames.extend([m.name]*len(out))

    df = pandas.concat(df)

    df.insert(0, 'model', mnames)
    if plot:
        sns.factorplot(x='model', y=df.columns[-1], data=df, kind='bar')
        if html is not None:
            html.writeimg(str(func))
        else:
            sns.plt.show()
    if save: df.to_csv(df)
    return df
//  [python]
def compare(func, test_ims, layer='output', models=None,
            plot=True, save=False, html=None,
            *func_args, **func_kwargs):

    if models is None: models = KNOWN_MODELS.keys()

    df = []
    mnames = []
    for model in models:
        m = Model(model)
        resps = m.run(test_ims=test_ims, layers=layer)
        out = func(resps, *func_args, **func_kwargs)
        df.append(pandas.DataFrame(out))
        mnames.extend([m.name]*len(out))

    df = pandas.concat(df)

    df.insert(0, 'model', mnames)
    if plot:
        sns.catplot(x='model', y=df.columns[-1], data=df, kind='bar')
        if html is not None:
            html.writeimg(str(func))
        else:
            sns.plt.show()
    if save: df.to_csv(df)
    return df
//  [python]
def plot_factor(self, data: pd.DataFrame, x: str, y: str, col: str,
                    palette: str, hue: str, col_order: list, 
                    title:str) -> None:
        """
        """
        try:
            self.logger.info('setting up factor plot')
            #plt.figure(figsize=(12, 7))
            sns.factorplot(data= data, x=x, y=y, col=col, palette=palette, 
                        hue=hue, col_order=col_order, title=title)
            plt.show()
        except Exception as e:
            self.logger.error(e, exec_info=True)
            print(e)
//  [python]
def plot_factor(self, data: pd.DataFrame, x: str, y: str, col: str,
                    palette: str, hue: str, col_order: list, 
                    title:str) -> None:
        """
        """
        try:
            self.logger.info('setting up factor plot')
            #plt.figure(figsize=(12, 7))
            sns.catplot(data= data, x=x, y=y, col=col, palette=palette, 
                        hue=hue, col_order=col_order, kind='bar') #Added kind='bar'
            plt.show()
        except Exception as e:
            self.logger.error(e, exec_info=True)
            print(e)
//  [python]
def plot_count_fig(job_id):
    """
    Create count plot, as a 2-row x 3-col bar plot of data points for each k in each covar.

    Parameters
    ----------
    tasks: list(dict)

    Returns
    -------
    Matplotlib Figure object.
    """
    tasks = db.session.query(Task).filter_by(job_id=job_id).options(load_only(
        "k", "covar_type", "covar_tied", "bic", "aic")).all()
    data_records = [task.__dict__ for task in tasks]
    df = pd.DataFrame.from_records(data_records)

    sns.set(context='talk', style='whitegrid')
    df['covar_type'] = [x.capitalize() for x in df['covar_type']]
    df['covar_tied'] = [['Untied', 'Tied'][x] for x in df['covar_tied']]
    f = sns.factorplot(x='k', kind='count', col='covar_type', row='covar_tied', data=df,
                       row_order=['Tied', 'Untied'], col_order=['Full', 'Diag', 'Spher'], legend=True, legend_out=True,
                       palette='Blues_d')
    f.set_titles("{col_name}-{row_name}")
    f.set_xlabels("Num. of Clusters (K)")
    return f.fig
//  [python]
def plot_count_fig(job_id):
    """
    Create count plot, as a 2-row x 3-col bar plot of data points for each k in each covar.

    Parameters
    ----------
    tasks: list(dict)

    Returns
    -------
    Matplotlib Figure object.
    """
    tasks = db.session.query(Task).filter_by(job_id=job_id).options(load_only(
        "k", "covar_type", "covar_tied", "bic", "aic")).all()
    data_records = [task.__dict__ for task in tasks]
    df = pd.DataFrame.from_records(data_records)

    sns.set(context='talk', style='whitegrid')
    df['covar_type'] = [x.capitalize() for x in df['covar_type']]
    df['covar_tied'] = [['Untied', 'Tied'][x] for x in df['covar_tied']]
    f = sns.catplot(x='k', kind='count', col='covar_type', row='covar_tied', data=df,
                       row_order=['Tied', 'Untied'], col_order=['Full', 'Diag', 'Spher'], legend=True, legend_out=True,
                       palette='Blues_d')
    f.set_titles("{col_name}-{row_name}")
    f.set_xlabels("Num. of Clusters (K)")
    return f.fig
//  [python]
def plot_aic_bic_fig(job_id):
    """
    Creates AIC-BIC plot, as a 2-row x 3-col grid of point plots with 95% confidence intervals.

    Parameters
    ----------
    tasks: list(dict)

    Returns
    -------
    Matplotlib Figure object
    """
    tasks = db.session.query(Task).filter_by(job_id=job_id).options(load_only(
        "k", "covar_type", "covar_tied", "bic", "aic")).all()
    data_records = [task.__dict__ for task in tasks]
    df = pd.DataFrame.from_records(data_records)

    sns.set(context='talk', style='whitegrid')
    df['covar_type'] = [x.capitalize() for x in df['covar_type']]
    df['covar_tied'] = [['Untied', 'Tied'][x] for x in df['covar_tied']]
    df['aic'] = df['aic'].astype('float')
    df['bic'] = df['bic'].astype('float')
    df = pd.melt(df, id_vars=['k', 'covar_type', 'covar_tied'], value_vars=['aic', 'bic'], var_name='metric')
    f = sns.factorplot(x='k', y='value', col='covar_type', row='covar_tied', hue='metric', data=df,
                       row_order=['Tied', 'Untied'], col_order=['Full', 'Diag', 'Spher'], legend=True, legend_out=True,
                       ci=95, n_boot=100)
    f.set_titles("{col_name}-{row_name}")
    f.set_xlabels("Num. of Clusters (K)")
    return f.fig
//  [python]
def plot_aic_bic_fig(job_id):
    """
    Creates AIC-BIC plot, as a 2-row x 3-col grid of point plots with 95% confidence intervals.

    Parameters
    ----------
    tasks: list(dict)

    Returns
    -------
    Matplotlib Figure object
    """
    tasks = db.session.query(Task).filter_by(job_id=job_id).options(load_only(
        "k", "covar_type", "covar_tied", "bic", "aic")).all()
    data_records = [task.__dict__ for task in tasks]
    df = pd.DataFrame.from_records(data_records)

    sns.set(context='talk', style='whitegrid')
    df['covar_type'] = [x.capitalize() for x in df['covar_type']]
    df['covar_tied'] = [['Untied', 'Tied'][x] for x in df['covar_tied']]
    df['aic'] = df['aic'].astype('float')
    df['bic'] = df['bic'].astype('float')
    df = pd.melt(df, id_vars=['k', 'covar_type', 'covar_tied'], value_vars=['aic', 'bic'], var_name='metric')
    f = sns.catplot(x='k', y='value', col='covar_type', row='covar_tied', hue='metric', data=df,
                       row_order=['Tied', 'Untied'], col_order=['Full', 'Diag', 'Spher'], legend=True, legend_out=True,
                       ci=95, n_boot=100, kind="point")
    f.set_titles("{col_name}-{row_name}")
    f.set_xlabels("Num. of Clusters (K)")
    return f.fig
//  [python]
def plot_y_per_col(dataframe, x, y, col, col_wrap, file_name, legend):
    g = sb.factorplot(x=x, y=y, hue=IMPLEMENTATION_LABEL, col=col,
                      col_wrap=col_wrap, kind='point',
                      legend=legend,
                      data=dataframe[[IMPLEMENTATION_LABEL, x, y, col]],
                      markers=PLOTTING_MARKERS)
    indexes = sorted(set(dataframe[x].values))
    char_amount = reduce(lambda a, b: a + len(str(b)), indexes, 0)
    step = char_amount // 16
    if step > 0:
        for i in range(len(indexes)):
            if i != 0 and i % step != 0:
                indexes[i] = ""
        g.set_xticklabels(indexes)
    print(f'Saving to {file_name}...')
    g.savefig(file_name)
//  [python]
def plot_y_per_col(dataframe, x, y, col, col_wrap, file_name, legend):
    g = sb.catplot(x=x, y=y, hue=IMPLEMENTATION_LABEL, col=col,
                      col_wrap=col_wrap, kind='point',
                      legend=legend,
                      data=dataframe[[IMPLEMENTATION_LABEL, x, y, col]],
                      markers=PLOTTING_MARKERS)
    indexes = sorted(set(dataframe[x].values))
    char_amount = reduce(lambda a, b: a + len(str(b)), indexes, 0)
    step = char_amount // 16
    if step > 0:
        for i in range(len(indexes)):
            if i != 0 and i % step != 0:
                indexes[i] = ""
        g.set_xticklabels(indexes)
    print(f'Saving to {file_name}...')
    g.savefig(file_name)
//  [python]
sns.countplot("Pclass", hue="Survived", data=data, ax=ax[1])
ax[1].set_title("Pclass:Survived vs Dead")
plt.show()
pd.crosstab(
    [data.Sex, data.Survived], data.Pclass, margins=True
).style.background_gradient(cmap="summer_r")
sns.factorplot("Pclass", "Survived", hue="Sex", data=data)
//  [python]
sns.catplot("Pclass", "Survived", hue="Sex", data=data)
//  [python]
def plot_boxplot(ds, cat, num):
    sns.set()
    plt.gcf().clear()
    with sns.axes_style(style='ticks'):
        sns.factorplot(cat, num, data=ds, kind="box")
    from io import BytesIO
    plt.xlabel(cat)
    plt.ylabel(num)
    figfile = BytesIO()
    plt.savefig(figfile, format='png')
    figfile.seek(0)  # rewind to beginning of file
    import base64
    figdata_png = base64.b64encode(figfile.getvalue())
    return figdata_png
//  [python]
def plot_boxplot(ds, cat, num):
    sns.set()
    plt.gcf().clear()
    with sns.axes_style(style='ticks'):
        sns.catplot(x=cat, y=num, data=ds, kind="box")
    from io import BytesIO
    plt.xlabel(cat)
    plt.ylabel(num)
    figfile = BytesIO()
    plt.savefig(figfile, format='png')
    figfile.seek(0)  # rewind to beginning of file
    import base64
    figdata_png = base64.b64encode(figfile.getvalue())
    return figdata_png
//  [python]
def plot_res_between_groups(self, maxcol=1, flag=1):
        """Summary

            Plot in one plot with results between groups.

        Args:
            maxcol (int, optional): number of columns in the plots
            flag (int, optional): 1：基于样本全体处理后分组的情况；2：基于样本个体处理后合并分组的情况；

        """
        df = self.res_between_group_dict[flag]
        res_cycles = df.rename('value').reset_index()

        g = sn.factorplot(data=res_cycles, x='cycle', y='value', kind='bar', sharey=False)
        g.fig.suptitle(self.fig_title_by_flags[flag], y=0.99, fontsize=12)
        g.fig.set_size_inches(5, 4)
        g.fig.tight_layout(rect=(0, 0, 1, 0.97))
        return g
//  [python]
def plot_res_between_groups(self, maxcol=1, flag=1):
        """Summary

            Plot in one plot with results between groups.

        Args:
            maxcol (int, optional): number of columns in the plots
            flag (int, optional): 1：基于样本全体处理后分组的情况；2：基于样本个体处理后合并分组的情况；

        """
        df = self.res_between_group_dict[flag]
        res_cycles = df.rename('value').reset_index()

        g = sn.catplot(data=res_cycles, x='cycle', y='value', kind='bar', sharey=False)
        g.fig.suptitle(self.fig_title_by_flags[flag], y=0.99, fontsize=12)
        g.fig.set_size_inches(5, 4)
        g.fig.tight_layout(rect=(0, 0, 1, 0.97))
        return g
//  [python]
def plot_res_within_group(self, flag=1):
        """Summary

            Plot in several plots with results within group.

        Args:
            cycles (None, optional): Description
            flag (int, optional): 1：对总体绝对大小分组；2：对个体分组后的组别合并

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

            Plot in several plots with results within group.

        Args:
            cycles (None, optional): Description
            flag (int, optional): 1：对总体绝对大小分组；2：对个体分组后的组别合并

        """
        df = self.res_within_group_dict[flag]
        res = df.stack(['type', 'cycle']).reset_index().rename(columns={0: 'value'})
        g = sn.catplot(data=res, x='segment', y='value',
                          row='cycle', col='type',
                          kind='bar', alpha=0.8, dodge=True,
                          sharex=False, height=2, aspect=1,
                          legend_out=True, margin_titles=True)
        g.fig.suptitle(self.fig_title_by_flags[flag], y=0.99, fontsize=12)
        g.fig.set_size_inches(16, 9)
        g.fig.tight_layout(rect=(0, 0, 1, 0.97))
        g.add_legend()
        return g
//  [python]
def plot_factor_among_segment(self, in_df, flag):
        df = in_df.rename(columns={'segment' + str(flag): 'segment'})
        grp_by_segm = df.groupby('segment')
        comp_cnt = grp_by_segm.size()
        total_cnt = grp_by_segm['count'].sum()

        std_factor = grp_by_segm.apply(
            lambda x: util.update_std(
                x['sumsq_factor'].sum(), x['sum_factor'].sum(), x['count'].sum()
            )
        )

        res = pd.concat([comp_cnt, total_cnt, std_factor], 1, keys=[
                        'compressed_count', 'overall_count', 'std(sum_factor)'])
        res.columns.name = 'type'
        self.factor_among_segment = res.copy()
        res_graph = res.stack().rename('value').reset_index()
        sn.set_style("whitegrid")
        g = sn.factorplot(data=res_graph, x='segment', y='value', col='type',
                          kind='bar', sharey=False, alpha=0.8)
        return g
//  [python]
def plot_factor_among_segment(self, in_df, flag):
        df = in_df.rename(columns={'segment' + str(flag): 'segment'})
        grp_by_segm = df.groupby('segment')
        comp_cnt = grp_by_segm.size()
        total_cnt = grp_by_segm['count'].sum()

        std_factor = grp_by_segm.apply(
            lambda x: util.update_std(
                x['sumsq_factor'].sum(), x['sum_factor'].sum(), x['count'].sum()
            )
        )

        res = pd.concat([comp_cnt, total_cnt, std_factor], 1, keys=[
                        'compressed_count', 'overall_count', 'std(sum_factor)'])
        res.columns.name = 'type'
        self.factor_among_segment = res.copy()
        res_graph = res.stack().rename('value').reset_index()
        sn.set_style("whitegrid")
        g = sn.catplot(data=res_graph, x='segment', y='value', col='type',
                          kind='bar', sharey=False, alpha=0.8)
        return g
//  [python]
def plot_factor_among_segment_seperated(self, flag):
        df = self.input_df2.rename(columns={'segment' + str(flag): 'segment'})
        grp_by_segm = df.groupby(['user_grp', 'segment'])
        comp_cnt = grp_by_segm.size()
        total_cnt = grp_by_segm['count'].sum()
        std_factor = grp_by_segm.apply(
            lambda x: util.update_std(
                x['sumsq_factor'].sum(), x['sum_factor'].sum(), x['count'].sum()
            )
        )
        res = pd.concat([comp_cnt, total_cnt, std_factor], 1, keys=[
                        'compressed_count', 'overall_count', 'std(sum_factor)'])
        res.columns.name = 'type'
        self.data_ft_segm_sep = res.copy()
        res_graph = res.stack().rename('value').reset_index()
        g = sn.factorplot(data=res_graph, x='segment', y='value', row='user_grp',
                          col='type', kind='bar', sharey=False, alpha=0.8)
        return g
//  [python]
def plot_factor_among_segment_seperated(self, flag):
        df = self.input_df2.rename(columns={'segment' + str(flag): 'segment'})
        grp_by_segm = df.groupby(['user_grp', 'segment'])
        comp_cnt = grp_by_segm.size()
        total_cnt = grp_by_segm['count'].sum()
        std_factor = grp_by_segm.apply(
            lambda x: util.update_std(
                x['sumsq_factor'].sum(), x['sum_factor'].sum(), x['count'].sum()
            )
        )
        res = pd.concat([comp_cnt, total_cnt, std_factor], 1, keys=[
                        'compressed_count', 'overall_count', 'std(sum_factor)'])
        res.columns.name = 'type'
        self.data_ft_segm_sep = res.copy()
        res_graph = res.stack().rename('value').reset_index()
        g = sn.catplot(data=res_graph, x='segment', y='value', row='user_grp',
                          col='type', kind='bar', sharey=False, alpha=0.8)
        return g
//  [python]
def plot_res_between_groups_seperated(self, flag=1, figsize=(10, 5)):
        df = self.input_df2.copy()
        raw_df_grp = df.groupby('user_grp', sort=False)
        data_graph = raw_df_grp.apply(lambda x: self.__calc_all_res_between_group(x, flag))
        self.data_bt_grp_sep = data_graph.copy()
        data_graph_melted = data_graph.stack().reset_index().rename(columns={0: 'value'})
        g = sn.factorplot(data=data_graph_melted, x='user_grp', y='value', hue='cycle',
                          kind='bar', alpha=0.3, dodge=True,
                          sharex=True, sharey=False, margin_titles=True, legend=False)

        g.fig.suptitle(self.fig_title_by_flags[flag] +
                       '\nPerformance by User-defined Group', x=0.8, y=0.99, fontsize=12)
        g.fig.set_size_inches(figsize[0], figsize[1])
        g.fig.autofmt_xdate()
        g.fig.tight_layout(rect=(0.01, 0.01, 0.95, 0.92))
        g.add_legend()
        return g
//  [python]
def plot_res_between_groups_seperated(self, flag=1, figsize=(10, 5)):
        df = self.input_df2.copy()
        raw_df_grp = df.groupby('user_grp', sort=False)
        data_graph = raw_df_grp.apply(lambda x: self.__calc_all_res_between_group(x, flag))
        self.data_bt_grp_sep = data_graph.copy()
        data_graph_melted = data_graph.stack().reset_index().rename(columns={0: 'value'})
        g = sn.catplot(data=data_graph_melted, x='user_grp', y='value', hue='cycle',
                          kind='bar', alpha=0.3, dodge=True,
                          sharex=True, sharey=False, margin_titles=True, legend=False)

        g.fig.suptitle(self.fig_title_by_flags[flag] +
                       '\nPerformance by User-defined Group', x=0.8, y=0.99, fontsize=12)
        g.fig.set_size_inches(figsize[0], figsize[1])
        g.fig.autofmt_xdate()
        g.fig.tight_layout(rect=(0.01, 0.01, 0.95, 0.92))
        g.add_legend()
        return g
//  [python]
def visualize_variable_relationships(training_data, viz_type, category_vars, quantitative_vars):
    """
    Generates plots showing the relationship between several variables.
    """
    # compare the continuous variable distributions using a violin plot
    sub_data = training_data[quantitative_vars]
    fig, ax = plt.subplots(1, 1, figsize=(16, 12))
    sb.violinplot(sub_data, ax=ax)
    fig.tight_layout()

    # if categorical variables were provided, visualize the quantitative distributions by category
    if len(category_vars) > 0:
        fig, ax = plt.subplots(len(quantitative_vars), len(category_vars), figsize=(16, 12))
        for i, var in enumerate(quantitative_vars):
            for j, cat in enumerate(category_vars):
                sb.violinplot(training_data[var], training_data[cat], ax=ax[i, j])
        fig.tight_layout()

    # generate plots to directly compare the variables
    if len(category_vars) == 0:
        if len(quantitative_vars) == 2:
            sb.jointplot(quantitative_vars[0], quantitative_vars[1], training_data, kind=viz_type, size=16)
        else:
            sb.pairplot(training_data, vars=quantitative_vars, kind='scatter',
                        diag_kind='kde', size=16 / len(quantitative_vars))
    else:
        if len(quantitative_vars) == 1:
            if len(category_vars) == 1:
                sb.factorplot(category_vars[0], quantitative_vars[0], None,
                              training_data, kind='auto', size=16)
            else:
                sb.factorplot(category_vars[0], quantitative_vars[0], category_vars[1],
                              training_data, kind='auto', size=16)
        if len(quantitative_vars) == 2:
            if len(category_vars) == 1:
                sb.lmplot(quantitative_vars[0], quantitative_vars[1], training_data,
                          col=None, row=category_vars[0], size=16)
            else:
                sb.lmplot(quantitative_vars[0], quantitative_vars[1], training_data,
                          col=category_vars[0], row=category_vars[1], size=16)
        else:
            sb.pairplot(training_data, hue=category_vars[0], vars=quantitative_vars, kind='scatter',
                        diag_kind='kde', size=16 / len(quantitative_vars))
//  [python]
def visualize_variable_relationships(training_data, viz_type, category_vars, quantitative_vars):
    """
    Generates plots showing the relationship between several variables.
    """
    # compare the continuous variable distributions using a violin plot
    sub_data = training_data[quantitative_vars]
    fig, ax = plt.subplots(1, 1, figsize=(16, 12))
    sb.violinplot(sub_data, ax=ax)
    fig.tight_layout()

    # if categorical variables were provided, visualize the quantitative distributions by category
    if len(category_vars) > 0:
        fig, ax = plt.subplots(len(quantitative_vars), len(category_vars), figsize=(16, 12))
        for i, var in enumerate(quantitative_vars):
            for j, cat in enumerate(category_vars):
                sb.violinplot(training_data[var], training_data[cat], ax=ax[i, j])
        fig.tight_layout()

    # generate plots to directly compare the variables
    if len(category_vars) == 0:
        if len(quantitative_vars) == 2:
            sb.jointplot(quantitative_vars[0], quantitative_vars[1], training_data, kind=viz_type, size=16)
        else:
            sb.pairplot(training_data, vars=quantitative_vars, kind='scatter',
                        diag_kind='kde', size=16 / len(quantitative_vars))
    else:
        if len(quantitative_vars) == 1:
            if len(category_vars) == 1:
                sb.catplot(category_vars[0], quantitative_vars[0], None,
                              training_data, kind='auto', size=16)
            else:
                sb.catplot(category_vars[0], quantitative_vars[0], category_vars[1],
                              training_data, kind='auto', size=16)
        if len(quantitative_vars) == 2:
            if len(category_vars) == 1:
                sb.lmplot(quantitative_vars[0], quantitative_vars[1], training_data,
                          col=None, row=category_vars[0], size=16)
            else:
                sb.lmplot(quantitative_vars[0], quantitative_vars[1], training_data,
                          col=category_vars[0], row=category_vars[1], size=16)
        else:
            sb.pairplot(training_data, hue=category_vars[0], vars=quantitative_vars, kind='scatter',
                        diag_kind='kde', size=16 / len(quantitative_vars))
//  [python]
def plot_res_within_group_seperated(self, res_type='avgrtn', flag=1):
        df = self.input_df2.rename(columns={'segment' + str(flag): 'segment'})
        raw_df_grp = df.groupby('user_grp')
        data_graph = raw_df_grp.apply(lambda x: self.__calc_all_res_within_group(x, flag))
        self.data_res_wt_grp_sep = data_graph

        if res_type in data_graph.columns:
            data_graph = data_graph[res_type].stack().rename(res_type).reset_index()
            g = sn.factorplot(data=data_graph, x='segment', y=res_type,
                              row='user_grp', col='cycle', hue='segment',
                              kind='bar', alpha=0.8, dodge=False,
                              legend=False, sharex=True, sharey=False, margin_titles=True)
        else:
            data_graph_melted = data_graph.stack().reset_index().melt(id_vars=['cycle', 'segment', 'user_grp'])
            g = sn.FacetGrid(data=data_graph_melted, row='type', col='cycle', hue='user_grp',
                             size=2, aspect=3, sharex=False, sharey=False,
                             legend_out=True, margin_titles=True)
            g.map(plt.scatter, 'segment', 'value', alpha=0.8)

        ttl_2nd = '\nby Segmentation within User-defined Groups'
        g.fig.suptitle(self.fig_title_by_flags[flag] + ttl_2nd, y=0.99, fontsize=12)
        g.fig.set_size_inches(20, 15)
        g.fig.autofmt_xdate()
        g.fig.tight_layout(rect=(0.01, 0.01, 0.95, 0.97))
        g.add_legend()
        return g