jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def plot_long_seq(
    data,
    ax,
    method_name,
    lineplot_kwargs=None,
    manual_average=True,
    smoothen=False,
    title="Average Success",
):
    if lineplot_kwargs is None:
        lineplot_kwargs = {}

    data = data.copy()
    if manual_average:
        cols = data.columns[data.columns.str.contains(f"test/stochastic/.*/success", regex=True)]
        data["average_success"] = data[cols].mean(axis=1)
        avg_success = data
    else:
        avg_success = data.rename(columns={"test/stochastic/average_success": "average_success"})
    if smoothen:
        avg_success = smoothen_long_seq(avg_success)

    avg_success["Average Success"] = avg_success["average_success"]
    avg_success["Method"] = avg_success["identifier"]
    avg_success = avg_success.replace({"Method": RENAMER})

    style = "half" if "half" in data.columns else None
    sns.lineplot(
        data=avg_success,
        ax=ax,
        x="x",
        y="Average Success",
        hue="Method",
        style=style,
        **lineplot_kwargs,
    )

    plot_task_separators(data, steps_per_task=get_steps_per_task(data), ax=ax)
    ax.set_title(f"{title}, {method_name}")
//  [python]
def plot_current(data, ax, method_name, lineplot_kwargs=None, smoothen=False):
    if lineplot_kwargs is None:
        lineplot_kwargs = {}

    data = data.copy()
    for env in sorted(data["train/active_env"].dropna().unique()):
        env = int(env)
        env_indices = data["train/active_env"] == env
        current_col = data.columns[data.columns.str.contains(f"test/stochastic/{env}/.*/success")][
            0
        ]
        data.loc[env_indices, "current_success"] = data.loc[env_indices, current_col]

    if smoothen:
        data = smoothen_long_seq(data, smooth_col="current_success")

    data["Current Task Success"] = data["current_success"]
    data["Method"] = data["identifier"]
    data = data.replace({"Method": RENAMER})

    style = "half" if "half" in data.columns else None
    sns.tsplot(
        data=data,
        ax=ax,
        time="x",
        value="Current Task Success",
        condition="Method",
        unit="identifier",
        err_style="band",
        **lineplot_kwargs,
    )

    # ax.xaxis.set_ticks_position('none')
    plot_task_separators(data, steps_per_task=get_steps_per_task(data), ax=ax)
    ax.set_title(f"Current Task Success, {method_name}")
//  [python]
def plot_current(data, ax, method_name, lineplot_kwargs=None, smoothen=False):
    if lineplot_kwargs is None:
        lineplot_kwargs = {}

    data = data.copy()
    for env in sorted(data["train/active_env"].dropna().unique()):
        env = int(env)
        env_indices = data["train/active_env"] == env
        current_col = data.columns[data.columns.str.contains(f"test/stochastic/{env}/.*/success")][
            0
        ]
        data.loc[env_indices, "current_success"] = data.loc[env_indices, current_col]

    if smoothen:
        data = smoothen_long_seq(data, smooth_col="current_success")

    data["Current Task Success"] = data["current_success"]
    data["Method"] = data["identifier"]
    data = data.replace({"Method": RENAMER})

    style = "half" if "half" in data.columns else None
    sns.lineplot(
        data=data,
        ax=ax,
        x="x",
        y="Current Task Success",
        hue="Method",
        style=style,
        **lineplot_kwargs,
    )

    # ax.xaxis.set_ticks_position('none')
    plot_task_separators(data, steps_per_task=get_steps_per_task(data), ax=ax)
    ax.set_title(f"Current Task Success, {method_name}")
//  [python]
def plot_individual(data, ax, method_name, lineplot_kwargs=None, smoothen=False):
    if lineplot_kwargs is None:
        lineplot_kwargs = {}

    data = data.copy()
    data_cols = data.columns[data.columns.str.contains(f"test/stochastic/.*/success", regex=True)]
    if smoothen:
        for col in data_cols:
            data = smoothen_long_seq(data, smooth_col=col)
    sorted_cols = []
    min_env = int(data["train/active_env"].dropna().min())
    max_env = int(data["train/active_env"].dropna().max())
    for idx in range(min_env, max_env + 1):
        sorted_cols.append(next(col for col in data_cols if f"/{idx}/" in col))

    steps_per_task = get_steps_per_task(data)
    data = data.melt(id_vars=["x", "experiment_id"], value_vars=sorted_cols)

    data["Task Success"] = data["value"]
    data["Task"] = data["variable"]
    # data = data.replace({'Task': METRIC_RENAMER})

    sns.pointplot(data=data, ax=ax, x="x", y="Task Success", hue="Task", **lineplot_kwargs)

    plot_task_separators(data, steps_per_task=steps_per_task, ax=ax)
    ax.set_title(f"Task Success, {method_name}")
//  [python]
def plot_individual(data, ax, method_name, lineplot_kwargs=None, smoothen=False):
    if lineplot_kwargs is None:
        lineplot_kwargs = {}

    data = data.copy()
    data_cols = data.columns[data.columns.str.contains(f"test/stochastic/.*/success", regex=True)]
    if smoothen:
        for col in data_cols:
            data = smoothen_long_seq(data, smooth_col=col)
    sorted_cols = []
    min_env = int(data["train/active_env"].dropna().min())
    max_env = int(data["train/active_env"].dropna().max())
    for idx in range(min_env, max_env + 1):
        sorted_cols.append(next(col for col in data_cols if f"/{idx}/" in col))

    steps_per_task = get_steps_per_task(data)
    data = data.melt(id_vars=["x", "experiment_id"], value_vars=sorted_cols)

    data["Task Success"] = data["value"]
    data["Task"] = data["variable"]
    # data = data.replace({'Task': METRIC_RENAMER})

    sns.lineplot(data=data, ax=ax, x="x", y="Task Success", hue="Task", **lineplot_kwargs)

    plot_task_separators(data, steps_per_task=steps_per_task, ax=ax)
    ax.set_title(f"Task Success, {method_name}")
//  [python]
def p1(graph='test_acc'):
    csv = "4partitions.csv"
    out_file_name = f"{graph}.png"
    out_file_name = os.path.join(".", out_file_name)

    df = pd.read_csv(csv).query("dataset == 'cifar100'")
    ax = sns.tsplot(time="epoch", value=graph, unit="trial", condition="alg", data=df)

    ax.set_title(graph)

    model = pd.unique(df.model)
    assert (len(model) == 1)
    model = model[0]

    fig = ax.get_figure()
    fig.savefig(out_file_name)

    print(f"saving file to {out_file_name}")
//  [python]
def p1(graph='test_acc'):
    csv = "4partitions.csv"
    out_file_name = f"{graph}.png"
    out_file_name = os.path.join(".", out_file_name)

    df = pd.read_csv(csv).query("dataset == 'cifar100'")
    ax = sns.lineplot(x="epoch", y=graph, hue="alg", data=df)

    ax.set_title(graph)

    model = pd.unique(df.model)
    assert (len(model) == 1)
    model = model[0]

    fig = ax.get_figure()
    fig.savefig(out_file_name)

    print(f"saving file to {out_file_name}")
//  [python]
def gen_plots(data, options):

    # linear plots
    lin_dir = os.path.join(options['out_dir'], 'pretrain_robustness')
    os.makedirs(lin_dir, exist_ok=True)
    linear_data = data[data.result_type=='linear-eval']
    for pretrain_data in linear_data.pretrain_data.unique():
        for variant in ["linear-eval-lr"]: # linear_data['variant'].unique():
            variant_data=linear_data[(linear_data.variant == variant) & (linear_data.pretrain_data == pretrain_data)]
            # hack to "clean up" other analyses 
            variant_data = variant_data[(linear_data.basetrain == 'MoCo Random Init') | (linear_data.basetrain == 'HPT') | (linear_data.basetrain ==  'supervised imagenet init') | (linear_data.basetrain == 'HPT-BN')]
            # fig, ax = setup_plot(500)
            # print(variant_data)
            fig, ax = setup_plot(600)

            # variant_data.groupby("basetrain").plot(x="pretrain_iters", y="result", marker="o", ax=ax)
            sns.pointplot(x='pct_train', y='result', hue='basetrain', data=variant_data, ax=ax)
            # fig.set(xlabel='Percent of Pretrain data', ylabel='Change of Accuracy from Baseline', title=options['data_name'].replace("_", " ").title())
            # plt.close(1) 
            plt.axhline(y=options['asymptote'], c='red', linestyle='dashed', label="MoCo Direct Transfer")

            plt.xlabel("Percentage of Pretrain Data")
            plt.ylabel("Accuracy")
            plt.title(options['data_name'].replace("_", " ").title())



            plt.xscale('linear')
            plt.xticks(ticks=variant_data['pct_train'], labels=variant_data['pct_train'], rotation='horizontal')

            labels = ["HPT", "HPT-BN", "MoCo Random Init", "MoCo Direct Transfer"]
            handles, _ = ax.get_legend_handles_labels()
            plt.legend(handles = handles,labels=labels,title="Basetrain")
            plt.show()
            outplot = os.path.join(lin_dir, '{}_data_robustness-{}_linear_eval.pdf'.format(pretrain_data, options['data_name']))
            print(outplot)
            fig.savefig(outplot, format='pdf', bbox_inches='tight')
//  [python]
def gen_plots(data, options):

    # linear plots
    lin_dir = os.path.join(options['out_dir'], 'pretrain_robustness')
    os.makedirs(lin_dir, exist_ok=True)
    linear_data = data[data.result_type=='linear-eval']
    for pretrain_data in linear_data.pretrain_data.unique():
        for variant in ["linear-eval-lr"]: # linear_data['variant'].unique():
            variant_data=linear_data[(linear_data.variant == variant) & (linear_data.pretrain_data == pretrain_data)]
            # hack to "clean up" other analyses 
            variant_data = variant_data[(linear_data.basetrain == 'MoCo Random Init') | (linear_data.basetrain == 'HPT') | (linear_data.basetrain ==  'supervised imagenet init') | (linear_data.basetrain == 'HPT-BN')]
            # fig, ax = setup_plot(500)
            # print(variant_data)
            fig, ax = setup_plot(600)

            # variant_data.groupby("basetrain").plot(x="pretrain_iters", y="result", marker="o", ax=ax)
            sns.lineplot(x='pct_train', y='result', hue='basetrain', data=variant_data, marker='o', mew=0, ms=5)
            # fig.set(xlabel='Percent of Pretrain data', ylabel='Change of Accuracy from Baseline', title=options['data_name'].replace("_", " ").title())
            # plt.close(1) 
            plt.axhline(y=options['asymptote'], c='red', linestyle='dashed', label="MoCo Direct Transfer")

            plt.xlabel("Percentage of Pretrain Data")
            plt.ylabel("Accuracy")
            plt.title(options['data_name'].replace("_", " ").title())



            plt.xscale('linear')
            plt.xticks(ticks=variant_data['pct_train'], labels=variant_data['pct_train'], rotation='horizontal')

            labels = ["HPT", "HPT-BN", "MoCo Random Init", "MoCo Direct Transfer"]
            handles, _ = ax.get_legend_handles_labels()
            plt.legend(handles = handles,labels=labels,title="Basetrain")
            plt.show()
            outplot = os.path.join(lin_dir, '{}_data_robustness-{}_linear_eval.pdf'.format(pretrain_data, options['data_name']))
            print(outplot)
            fig.savefig(outplot, format='pdf', bbox_inches='tight')
//  [python]
def plot_coverage(self,
        ref_id:str,
        start:int=None,
        end:int=None,
        scale:bool=False,
        split_samples:bool=False,
        figsize:tuple=(30,5),
        palette:str="Set2",
        plot_style:str="ggplot"):
        """
        Plot the read coverage over a reference for all samples analysed
        * ref_id
            Valid reference id name in the database
        * start
            Start coordinate
        * end
            End coordinate (included)
        * figsize
            Length and heigh of the output plot
        * palette
            Colormap. See https://matplotlib.org/users/colormaps.html, https://matplotlib.org/examples/color/named_colors.html
        * plot_style
            Matplotlib plotting style. See https://matplotlib.org/users/style_sheets.html
        """
        # Extract data for ref_id
        ref_data = self[ref_id]
        start, end = self.__get_positions(ref_id, start, end)

        # Parse data from database
        l = []
        for pos in np.arange(start, end):
            for cond_lab, cond_dict in ref_data[pos]['data'].items():
                for sample_lab, sample_val in cond_dict.items():
                    l.append((pos, "{}_{}".format(cond_lab, sample_lab), sample_val["coverage"]))

        # Cast collected results to dataframe
        df = pd.DataFrame(l, columns=["pos", "sample", "cov"])
        if scale:
            df['cov'] = df.groupby('sample')['cov'].apply(lambda x: x/max(x))

        # Define plotting style
        with pl.style.context(plot_style):
            fig, ax = pl.subplots(figsize=figsize)
            _ = sns.pointplot( x="pos", y="cov", hue="sample", data=df, ax=ax, palette=palette, join=True)
            if not scale:
                _ = ax.axhline(y=self._metadata["min_coverage"], linestyle=":", color="grey", label="minimal coverage")

            _ = ax.set_ylim(0, None)
            _ = ax.set_xlim(start, end-1)
            _ = ax.set_title("Reference:{}  Start:{}  End:{}".format(ref_id, start, end), fontsize=18)
            _ = ax.set_ylabel("Coverage")
            _ = ax.set_xlabel("Reference position")
            _ = ax.legend(bbox_to_anchor=(1, 1), loc=2, facecolor="white", frameon=False)

            pl.tight_layout()
            return(fig, ax)
//  [python]
def plot_coverage(self,
        ref_id:str,
        start:int=None,
        end:int=None,
        scale:bool=False,
        split_samples:bool=False,
        figsize:tuple=(30,5),
        palette:str="Set2",
        plot_style:str="ggplot"):
        """
        Plot the read coverage over a reference for all samples analysed
        * ref_id
            Valid reference id name in the database
        * start
            Start coordinate
        * end
            End coordinate (included)
        * figsize
            Length and heigh of the output plot
        * palette
            Colormap. See https://matplotlib.org/users/colormaps.html, https://matplotlib.org/examples/color/named_colors.html
        * plot_style
            Matplotlib plotting style. See https://matplotlib.org/users/style_sheets.html
        """
        # Extract data for ref_id
        ref_data = self[ref_id]
        start, end = self.__get_positions(ref_id, start, end)

        # Parse data from database
        l = []
        for pos in np.arange(start, end):
            for cond_lab, cond_dict in ref_data[pos]['data'].items():
                for sample_lab, sample_val in cond_dict.items():
                    l.append((pos, "{}_{}".format(cond_lab, sample_lab), sample_val["coverage"]))

        # Cast collected results to dataframe
        df = pd.DataFrame(l, columns=["pos", "sample", "cov"])
        if scale:
            df['cov'] = df.groupby('sample')['cov'].apply(lambda x: x/max(x))

        # Define plotting style
        with pl.style.context(plot_style):
            fig, ax = pl.subplots(figsize=figsize)
            _ = sns.lineplot( x="pos", y="cov", hue="sample", data=df, ax=ax, palette=palette, drawstyle="steps")
            if not scale:
                _ = ax.axhline(y=self._metadata["min_coverage"], linestyle=":", color="grey", label="minimal coverage")

            _ = ax.set_ylim(0, None)
            _ = ax.set_xlim(start, end-1)
            _ = ax.set_title("Reference:{}  Start:{}  End:{}".format(ref_id, start, end), fontsize=18)
            _ = ax.set_ylabel("Coverage")
            _ = ax.set_xlabel("Reference position")
            _ = ax.legend(bbox_to_anchor=(1, 1), loc=2, facecolor="white", frameon=False)

            pl.tight_layout()
            return(fig, ax)
//  [python]
def flow(self, *, untied=False, grid_kws=None, **kwargs):
        """Represents changes in the rankings of the alternatives as lines \
        flowing through the ranking-methods.

        Parameters
        ----------
        untied: bool, default ``False``
            If it is ``True`` and any ranking has ties, the
            ``RankResult.untied_rank_`` property is used to assign each
            alternative a single ranked order. On the other hand, if it is
            ``False`` the rankings are used as they are.
        grid_kws: dict or None
            Dict with keyword arguments passed to
            ``matplotlib.axes.plt.Axes.grid``
        kwargs:
            Other keyword arguments are passed to the ``seaborn.lineplot()``
            function. except for data, estimator and sort.

        Returns
        -------
        matplotlib.axes.Axes or numpy.ndarray of them

        """
        df = self._ranks_cmp.to_dataframe(untied=untied)

        ax = s.lineplot(data=df.T, estimator=None, sort=False, **kwargs)

        grid_kws = {} if grid_kws is None else grid_kws
        grid_kws.setdefault("alpha", 0.3)
        ax.grid(**grid_kws)

        ax.set_ylabel(RANKS_LABELS[untied])

        return ax
//  [python]
def flow(self, *, untied=False, grid_kws=None, **kwargs):
        """Represents changes in the rankings of the alternatives as lines \
        flowing through the ranking-methods.

        Parameters
        ----------
        untied: bool, default ``False``
            If it is ``True`` and any ranking has ties, the
            ``RankResult.untied_rank_`` property is used to assign each
            alternative a single ranked order. On the other hand, if it is
            ``False`` the rankings are used as they are.
        grid_kws: dict or None
            Dict with keyword arguments passed to
            ``matplotlib.axes.plt.Axes.grid``
        kwargs:
            Other keyword arguments are passed to the ``seaborn.lineplot()``
            function. except for data, estimator and sort.

        Returns
        -------
        matplotlib.axes.Axes or numpy.ndarray of them

        """
        df = self._ranks_cmp.to_dataframe(untied=untied)

        ax = sns.lineplot(data=df.T, estimator=None, sort=False, **kwargs)

        grid_kws = {} if grid_kws is None else grid_kws
        grid_kws.setdefault("alpha", 0.3)
        ax.grid(**grid_kws)

        ax.set_ylabel(RANKS_LABELS[untied])

        return ax
//  [python]
label = []
for i in range(0, len(data)):
    label.append(hex(offset))
    offset += blocksize

matplotlib.pyplot.figure(figsize=(6, 3))
seaborn.set_context("paper", font_scale=0.8)

ax = seaborn.tsplot(data=data)
//  [python]
label = []
for i in range(0, len(data)):
    label.append(hex(offset))
    offset += blocksize

matplotlib.pyplot.figure(figsize=(6, 3))
seaborn.set_context("paper", font_scale=0.8)

ax = seaborn.lineplot(data=data)
//  [python]
def plot_mean_var(ad, title, ax):
    ad = ad.copy()

    sc.pp.filter_cells(ad, min_counts=1)
    sc.pp.filter_genes(ad, min_counts=1)

    m = ad.X.mean(axis=0)
    v = ad.X.var(axis=0)

    coefs, r2 = _fitquad(m, v)

    ax.set(xscale="log", yscale="log")
    ax.plot(m, v, 'o', c='black', markersize=1)

    poly = np.poly1d(coefs)
    ax.plot(m, poly(m), color='red')

    ax.set_title(title)
    ax.set_ylabel('Variance')
    ax.set_xlabel(r'$\mu$')

    ax.plot(m, m, color='blue')
    ax.legend(['Genes', r'NB ($\theta=%.2f)\ r^2=%.3f$' % (coefs[0], r2), 'Poisson'])

    return coefs[0]
//  [python]
def plot_mean_var(ad, title, ax):
    ad = ad.copy()

    sc.pp.filter_cells(ad, min_counts=1)
    sc.pp.filter_genes(ad, min_counts=1)

    m = ad.X.mean(axis=0)
    v = ad.X.var(axis=0)

    coefs, r2 = _fitquad(m, v)

    ax.set(xscale="log", yscale="log")
    ax.plot(m, v, 'o', c='black', markersize=1)

    poly = np.poly1d(coefs)
    sns.lineplot(m, poly(m), ax=ax, color='red')

    ax.set_title(title)
    ax.set_ylabel('Variance')
    ax.set_xlabel(r'$\mu$')

    sns.lineplot(m, m, ax=ax, color='blue')
    ax.legend(['Genes', r'NB ($\theta=%.2f)\ r^2=%.3f$' % (coefs[0], r2), 'Poisson'])

    return coefs[0]
//  [python]
def test_matplotlib_view(self):
        def check_view(matplotlib_view):
            self.assertIsInstance(matplotlib_view, kv.NodeView)
            self.assertTrue(is_html(matplotlib_view.html))
            self.assertTrue('<img src="data:image/png;base64' in matplotlib_view.html)
            render_repr = matplotlib_view.render()
            self.assertTrue(
                base64.b64encode(render_repr).decode("ascii") in matplotlib_view.html
            )

        import matplotlib.pyplot as plt
        import seaborn as sns

        data_x = list(range(10))
        data_y = [i + 2 for i in range(10)]

        # Stateful approach
        plt.plot(data_x, data_y)
        check_view(kv.view_matplotlib())

        # object oriented approach
        fig, ax = plt.subplots()
        ax.plot(data_x, data_y)
        check_view(kv.view_matplotlib(fig))

        # object oriented approach + view()
        fig, ax = plt.subplots()
        ax.plot(data_x, data_y)
        check_view(kv.view(fig))

        # Seaborn stateful
        sns.plt.plot(x="x", y="y", data={"x": data_x, "y": data_y})
        check_view(kv.view_matplotlib())

        # Seaborn stateful
        sns.plt.plot(x="x", y="y", data={"x": data_x, "y": data_y})
        check_view(kv.view_seaborn())

        # Seaborn figure object
        lp = sns.plt.plot(x="x", y="y", data={"x": data_x, "y": data_y})
        check_view(kv.view(lp.figure))

        # This shouldn't do anything
        plt.show()
//  [python]
def test_matplotlib_view(self):
        def check_view(matplotlib_view):
            self.assertIsInstance(matplotlib_view, kv.NodeView)
            self.assertTrue(is_html(matplotlib_view.html))
            self.assertTrue('<img src="data:image/png;base64' in matplotlib_view.html)
            render_repr = matplotlib_view.render()
            self.assertTrue(
                base64.b64encode(render_repr).decode("ascii") in matplotlib_view.html
            )

        import matplotlib.pyplot as plt
        import seaborn as sns

        data_x = list(range(10))
        data_y = [i + 2 for i in range(10)]

        # Stateful approach
        plt.plot(data_x, data_y)
        check_view(kv.view_matplotlib())

        # object oriented approach
        fig, ax = plt.subplots()
        ax.plot(data_x, data_y)
        check_view(kv.view_matplotlib(fig))

        # object oriented approach + view()
        fig, ax = plt.subplots()
        ax.plot(data_x, data_y)
        check_view(kv.view(fig))

        # Seaborn stateful
        sns.lineplot(x="x", y="y", data={"x": data_x, "y": data_y})
        check_view(kv.view_matplotlib())

        # Seaborn stateful
        sns.lineplot(x="x", y="y", data={"x": data_x, "y": data_y})
        check_view(kv.view_seaborn())

        # Seaborn figure object
        lp = sns.lineplot(x="x", y="y", data={"x": data_x, "y": data_y})
        check_view(kv.view(lp.figure))

        # This shouldn't do anything
        plt.show()
//  [python]
def plot_loss_curves(self, plot_models, repetitions, run_dir, outpath, losses, ax=None, save=True, smoothen=False, scale=False, mean=False):
        """Plot and save loss curves of MLPregressor and torch models. Smoothen means summing over one epoch for the curves with higher resolution than that.
        """
        df, df_save = self.get_loss_dfs(plot_models, repetitions, run_dir)
        
        wanted_metrics = df['metric'].isin(losses)
        assert len(wanted_metrics) > 0, f'Attribute {losses[0]} not found.'
        df = df[wanted_metrics]
       
        # Define style of plot.
        
        if ax == None:
            ax = plt.figure().gca()
        
        modes = df['mode'].unique().tolist()
        dashes = {mode: (2, 2) if mode.startswith('train') else '' for mode in modes}
        
        if mean or len(repetitions) == 1:
            
            if len(wanted_metrics) > 1:
                hue = 'metric'
                style = 'mode'
            else:            
                hue = 'mode'
                style = None
                ax.set_ylabel(wanted_metrics[0])
                
        else:
            assert len(wanted_metrics) == 1
            hue = 'repetition'
            style = 'mode'
            
        # Plot and save loss curves.    
        sns.pointplot(x='epoch', y='value', hue=hue, style=style, dashes=dashes, data=df, ax=ax)
        
        ax.set_title('+'.join(plot_models))
        
        plt.yscale('log')
        if scale:
            max_losses = df.groupby(by=['mode', 'repetition'])['value'].max()
            max_plot_loss = 2*max_losses.median()
            plt.ylim(0, max_plot_loss)
        
        if save:
            plt.savefig(outpath + '.png', dpi=300)
            df_save.to_csv(outpath + '.csv')
        
        plt.show()
        plt.close()
        return()
//  [python]
def plot_loss_curves(self, plot_models, repetitions, run_dir, outpath, losses, ax=None, save=True, smoothen=False, scale=False, mean=False):
        """Plot and save loss curves of MLPregressor and torch models. Smoothen means summing over one epoch for the curves with higher resolution than that.
        """
        df, df_save = self.get_loss_dfs(plot_models, repetitions, run_dir)
        
        wanted_metrics = df['metric'].isin(losses)
        assert len(wanted_metrics) > 0, f'Attribute {losses[0]} not found.'
        df = df[wanted_metrics]
       
        # Define style of plot.
        
        if ax == None:
            ax = plt.figure().gca()
        
        modes = df['mode'].unique().tolist()
        dashes = {mode: (2, 2) if mode.startswith('train') else '' for mode in modes}
        
        if mean or len(repetitions) == 1:
            
            if len(wanted_metrics) > 1:
                hue = 'metric'
                style = 'mode'
            else:            
                hue = 'mode'
                style = None
                ax.set_ylabel(wanted_metrics[0])
                
        else:
            assert len(wanted_metrics) == 1
            hue = 'repetition'
            style = 'mode'
            
        # Plot and save loss curves.    
        sns.lineplot(x='epoch', y='value', hue=hue, style=style, dashes=dashes, data=df, ax=ax)
        
        ax.set_title('+'.join(plot_models))
        
        plt.yscale('log')
        if scale:
            max_losses = df.groupby(by=['mode', 'repetition'])['value'].max()
            max_plot_loss = 2*max_losses.median()
            plt.ylim(0, max_plot_loss)
        
        if save:
            plt.savefig(outpath + '.png', dpi=300)
            df_save.to_csv(outpath + '.csv')
        
        plt.show()
        plt.close()
        return()
//  [python]
def plot_mean_dropout(ad, title, ax, opt_zinb_theta=False, legend_out=False):
    expr = ad.X
    mu = expr.mean(0)
    do = np.mean(expr == 0, 0)
    v = expr.var(axis=0)

    coefs, r2 = _fitquad(mu, v)
    theta = 1.0/coefs[0]

    # zinb fit
    coefs = _optimize_zinb(mu, do, theta=theta if not opt_zinb_theta else None)
    print(coefs)

    #pois_pred = np.exp(-mu)
    nb_pred = nb_zero(theta, mu)
    zinb_pred = zinb_zero(coefs[2],
                          mu,
                          sigmoid((np.log(mu+1e-7)*coefs[0])+coefs[1]))

    # calculate log loss for all distr.
    #pois_ll = log_loss(pois_pred, do)
    nb_ll = log_loss(nb_pred, do)
    zinb_ll = log_loss(zinb_pred, do)

    ax.plot(mu, do, 'o', c='black', markersize=1)
    ax.set(xscale="log")

    #sns.lineplot(mu, pois_pred, ax=ax, color='blue')
    ax.plot(mu, nb_pred, color='red')
    ax.plot(mu, zinb_pred, color='green')

    ax.set_title(title)
    ax.set_ylabel('Empirical dropout rate')
    ax.set_xlabel(r'Mean expression')


    leg_loc = 'best' if not legend_out else 'upper left'
    leg_bbox = None if not legend_out else (1.02, 1.)
    ax.legend(['Genes',
               #r'Poisson $L=%.4f$' % pois_ll,
               r'NB($\theta=%.2f)\ L=%.4f$' % ((1./theta), nb_ll),
               r'ZINB($\theta=%.2f,\pi=\sigma(%.2f\mu%+.2f)) \ L=%.4f$' % (1.0/coefs[2], coefs[0], coefs[1], zinb_ll)],
               loc=leg_loc, bbox_to_anchor=leg_bbox)
    zinb_pval = _lrt(-zinb_ll, -nb_ll, 3, 1)
    print('p-value: %e' % zinb_pval)
//  [python]
def plot_mean_dropout(ad, title, ax, opt_zinb_theta=False, legend_out=False):
    expr = ad.X
    mu = expr.mean(0)
    do = np.mean(expr == 0, 0)
    v = expr.var(axis=0)

    coefs, r2 = _fitquad(mu, v)
    theta = 1.0/coefs[0]

    # zinb fit
    coefs = _optimize_zinb(mu, do, theta=theta if not opt_zinb_theta else None)
    print(coefs)

    #pois_pred = np.exp(-mu)
    nb_pred = nb_zero(theta, mu)
    zinb_pred = zinb_zero(coefs[2],
                          mu,
                          sigmoid((np.log(mu+1e-7)*coefs[0])+coefs[1]))

    # calculate log loss for all distr.
    #pois_ll = log_loss(pois_pred, do)
    nb_ll = log_loss(nb_pred, do)
    zinb_ll = log_loss(zinb_pred, do)

    ax.plot(mu, do, 'o', c='black', markersize=1)
    ax.set(xscale="log")

    #sns.lineplot(mu, pois_pred, ax=ax, color='blue')
    sns.lineplot(mu, nb_pred, ax=ax, color='red')
    sns.lineplot(mu, zinb_pred, ax=ax, color='green')

    ax.set_title(title)
    ax.set_ylabel('Empirical dropout rate')
    ax.set_xlabel(r'Mean expression')


    leg_loc = 'best' if not legend_out else 'upper left'
    leg_bbox = None if not legend_out else (1.02, 1.)
    ax.legend(['Genes',
               #r'Poisson $L=%.4f$' % pois_ll,
               r'NB($\theta=%.2f)\ L=%.4f$' % ((1./theta), nb_ll),
               r'ZINB($\theta=%.2f,\pi=\sigma(%.2f\mu%+.2f)) \ L=%.4f$' % (1.0/coefs[2], coefs[0], coefs[1], zinb_ll)],
               loc=leg_loc, bbox_to_anchor=leg_bbox)
    zinb_pval = _lrt(-zinb_ll, -nb_ll, 3, 1)
    print('p-value: %e' % zinb_pval)
//  [python]
def analyze():
  action_count = np.load(action_count_dir, allow_pickle=True)
  print('action count loaded')

  # action taken > 0
  count_dict = {}
  for i, v in enumerate(action_count):
    if v > 0:
      count_dict[i] = v

  print('\n\n')
  for k, v in sorted(count_dict.items()):
    print('action: {}, count: {}'.format(k, v))

  # y = np.load(data_dir + '/y_all_3695_score.npy')
  # print(y[:20])

  # plot barplot
  plt.plot(list(count_dict.keys()), list(count_dict.values()))
  plt.title('Action Count')
  plt.xlabel('Action Index')
  plt.ylabel('Count')
  plt.show()
//  [python]
def analyze():
  action_count = np.load(action_count_dir, allow_pickle=True)
  print('action count loaded')

  # action taken > 0
  count_dict = {}
  for i, v in enumerate(action_count):
    if v > 0:
      count_dict[i] = v

  print('\n\n')
  for k, v in sorted(count_dict.items()):
    print('action: {}, count: {}'.format(k, v))

  # y = np.load(data_dir + '/y_all_3695_score.npy')
  # print(y[:20])

  # plot barplot
  seaborn.lineplot(list(count_dict.keys()), list(count_dict.values()))
  plt.title('Action Count')
  plt.xlabel('Action Index')
  plt.ylabel('Count')
  plt.show()
//  [python]
def plot_cate_lift(
    df: pd.DataFrame,
    x: str = "fraction",
    y: str = "cum_cate",
    label: Optional[str] = None,
    bootstrap: bool = False,
    ax: Any = None,
) -> Any:
    """Plots the CATE lift estimates as a lineplot.

    Args:
        df (pandas.DataFrame): a dataframe containing CATE lift estimates and cumulative population sizes or fractions.
            If bootstrap=True, df should also contain upper and lower bounds for the lift estimates.
        x (str, optional): column name in df that contains the cumulative population sizes or fractions.
            This defines the x-axis.
        y (str, optional): column name in df that contains the CATE lift estimates. This defines the y-axis.
        label (str, optional): name of the score estimation method to be shown on the legend. Defaults to None.
        bootstrap (bool, optional): indicates whether to use lower and upper bound values from df and plot a
            scatterplot with errorbars. If False, plots a barplot.
        ax (matplotlib.axes._subplots.AxesSubplot, optional): if specified, the plot will be plotted on this ax. Useful when creating a figure with subplots.

    Returns:
        (matplotlib.axes._subplots.AxesSubplot): the axis of the plot
    """

    df_lift = df.copy()

    ax = sns.lineplot(x=x, y=y, data=df_lift, label=label, ax=ax)

    if bootstrap:
        # add error bars
        df_lift["lower"] = df_lift[y] - df_lift[f"{y}_lower"]
        df_lift["upper"] = df_lift[f"{y}_upper"] - df_lift[y]
        ax.errorbar(x=df_lift[x], y=df_lift[y], yerr=df_lift[["lower", "upper"]].values.T, fmt="ko")

    xlabels = [f"{x:,.0f}" for x in ax.get_xticks() * 100]
    _ = ax.set_xticklabels(xlabels)
    ax.set(title="CATE lift by quantile", xlabel="% population targeted", ylabel="ATE over the targeted population")
    return ax
//  [python]
def plot_qini(
    df: pd.DataFrame,
    x: str = "fraction",
    y: str = "ate",
    label: Optional[str] = None,
    plot_random: bool = True,
    bootstrap: bool = False,
    ax: Any = None,
) -> Any:
    """Plots the Qini curve.

    Args:
        df (pandas.DataFrame): a dataframe containing Qini estimates and cumulative population sizes or fractions.
            If bootstrap=True, df should also contain upper and lower bounds for the lift estimates.
        x (str, optional): column name in df that contains the cumulative population sizes or fractions.
            This defines the x-axis.
        y (str, optional): column name in df that contains the Qini estimates. This defines the y-axis.
        label (str, optional): name of the score estimation method to be shown on the legend. Defaults to None.
        plot_random (bool, optional): Indicator whether the random targeting line should be plotted. Defaults to True.
        bootstrap (bool, optional): indicates whether to use lower and upper bound values from df and plot a
            scatterplot with errorbars. If False, plots a barplot.
        ax (matplotlib.axes._subplots.AxesSubplot, optional): if specified, the plot will be plotted on this ax. Useful when creating a figure with subplots.

    Returns:
        (matplotlib.axes._subplots.AxesSubplot): the axis of the plot
    """

    df_qini = df.copy()

    ax = sns.lineplot(x=x, y=y, data=df_qini, label=label, ax=ax)

    if bootstrap:
        df_qini["lower"] = df_qini[y] - df_qini[f"{y}_lower"]
        df_qini["upper"] = df_qini[f"{y}_upper"] - df_qini[y]
        ax.errorbar(x=df_qini[x], y=df_qini[y], yerr=df_qini[["lower", "upper"]].values.T, fmt="ko")  # type: ignore

    xlabels = [f"{x:,.0f}" for x in ax.get_xticks() * 100]
    _ = ax.set_xticklabels(xlabels)
    ax.set(title="Qini curve", xlabel="% population targeted", ylabel="ATE over the whole population")

    if plot_random:
        # Fetch the overall ATE from the row with the highest % of targeted population
        df_qini = df_qini.sort_values(x)
        overall_ate = df_qini.iloc[-1][y].squeeze()
        plot_cate_random(overall_ate, ax=ax)

    return ax
//  [python]
def plot_memory_usage(self):
        ''' Plots memory requirement to finetune LED by input length and attention window size '''
        with open(os.path.join(self.out_dir, 'mem_analysis.txt')) as f:
            data = [json.loads(line) for line in f if len(line) > 1]

        # Reformat as df
        reformatted = {k:[] for k in data[0].keys()}
        for line in data:
            for k,v in line.items():
                reformatted[k].append(v)
        df = pd.DataFrame(reformatted)
        
        # Preprocessing
        df.columns = [c.replace('attn', 'attention').replace('_',' ').replace('max ','') for c in df.columns]        
        outfile = os.path.join(self.out_dir, 'figs', f'pubmed_mem_alloc_lineplot.png')
        y = df['mem alloc gb']
        x1 = df['source length']
        x2 = df['attention window']

        # Make plots
        f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 7), sharey=True)
        sns.pointplot(x=x1.name, y=y.name, data=df, hue='attention window',  
            palette='pastel', ax=ax1)
        ax1.set_xlabel(x1.name.capitalize())
        ax1.set_ylabel('Memory consumption (Gb)')
        ax1.set_xticks(df[x1.name].unique())

        sns.pointplot(x=x2.name, y=y.name, data=df, hue='source length',  
            palette='pastel', ax=ax2)
        ax2.set_xlabel(x2.name.capitalize())
        ax2.set_xticks(df[x2.name].unique())

        f.suptitle('Maximum Memory Consumption to Finetune the LED \n (by Attention Window and Input Document Length, batch size = 1)')
        f.savefig(outfile)
        plt.clf()
//  [python]
def plot_memory_usage(self):
        ''' Plots memory requirement to finetune LED by input length and attention window size '''
        with open(os.path.join(self.out_dir, 'mem_analysis.txt')) as f:
            data = [json.loads(line) for line in f if len(line) > 1]

        # Reformat as df
        reformatted = {k:[] for k in data[0].keys()}
        for line in data:
            for k,v in line.items():
                reformatted[k].append(v)
        df = pd.DataFrame(reformatted)
        
        # Preprocessing
        df.columns = [c.replace('attn', 'attention').replace('_',' ').replace('max ','') for c in df.columns]        
        outfile = os.path.join(self.out_dir, 'figs', f'pubmed_mem_alloc_lineplot.png')
        y = df['mem alloc gb']
        x1 = df['source length']
        x2 = df['attention window']

        # Make plots
        f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 7), sharey=True)
        sns.lineplot(x=x1.name, y=y.name, data=df, hue='attention window', marker='o', 
            palette='pastel', ax=ax1)
        ax1.set_xlabel(x1.name.capitalize())
        ax1.set_ylabel('Memory consumption (Gb)')
        ax1.set_xticks(df[x1.name].unique())

        sns.lineplot(x=x2.name, y=y.name, data=df, hue='source length', marker='o', 
            palette='pastel', ax=ax2)
        ax2.set_xlabel(x2.name.capitalize())
        ax2.set_xticks(df[x2.name].unique())

        f.suptitle('Maximum Memory Consumption to Finetune the LED \n (by Attention Window and Input Document Length, batch size = 1)')
        f.savefig(outfile)
        plt.clf()
//  [python]
def plot_commits(self, code: pd.DataFrame, percentage_changes: pd.DataFrame):
        code.to_csv('commits.csv')
        fig1 = sns.tsplot(x="Date", y='commits', hue='Protocol', data=code)
        fig1.get_figure().savefig('commits.png')
        fig1.clear()
        fig2 = sns.barplot(y="Protocol", x="Percentage change in commits",
                           data=percentage_changes, palette="RdYlGn")
        fig2.get_figure().savefig('commits_change.png')
        fig2.clear()
//  [python]
def plot_commits(self, code: pd.DataFrame, percentage_changes: pd.DataFrame):
        code.to_csv('commits.csv')
        fig1 = sns.lineplot(x="Date", y='commits', hue='Protocol', data=code)
        fig1.get_figure().savefig('commits.png')
        fig1.clear()
        fig2 = sns.barplot(y="Protocol", x="Percentage change in commits",
                           data=percentage_changes, palette="RdYlGn")
        fig2.get_figure().savefig('commits_change.png')
        fig2.clear()
//  [python]
def plot_churn(self, code: pd.DataFrame, percentage_changes: pd.DataFrame):
        code.to_csv('churn.csv')
        fig3 = sns.tsplot(x="Date", y='churn', hue='Protocol', data=code)
        fig3.set_yscale("log")
        fig3.get_figure().savefig('churn.png')
        fig3.clear()
        fig4 = sns.barplot(y="Protocol", x="Percentage change in churn",
                           data=percentage_changes, palette="RdYlGn")
        fig4.get_figure().savefig('churn_change.png')
        fig4.clear()
//  [python]
def plot_churn(self, code: pd.DataFrame, percentage_changes: pd.DataFrame):
        code.to_csv('churn.csv')
        fig3 = sns.lineplot(x="Date", y='churn', hue='Protocol', data=code)
        fig3.set_yscale("log")
        fig3.get_figure().savefig('churn.png')
        fig3.clear()
        fig4 = sns.barplot(y="Protocol", x="Percentage change in churn",
                           data=percentage_changes, palette="RdYlGn")
        fig4.get_figure().savefig('churn_change.png')
        fig4.clear()
//  [python]
def plot_devs(self, protocols_comparison: pd.DataFrame, percentage_changes: pd.DataFrame):
        protocols_comparison.to_csv('devs.csv')
        # Disable Seaborn sorting or months appear out of order
        fig5 = sns.pointplot(x="Month", y="Monthly Active Devs", hue='Protocol',
                            data=protocols_comparison, sort=False, palette="Dark2_r")
        fig5.get_figure().savefig('devs.png')
        fig5.clear()
        fig6 = sns.barplot(y="Protocol", x="Percentage change in active devs",
                           data=percentage_changes, palette="RdYlGn")
        fig6.get_figure().savefig('devs_change.png')
        fig6.clear()
//  [python]
def plot_devs(self, protocols_comparison: pd.DataFrame, percentage_changes: pd.DataFrame):
        protocols_comparison.to_csv('devs.csv')
        # Disable Seaborn sorting or months appear out of order
        fig5 = sns.lineplot(x="Month", y="Monthly Active Devs", hue='Protocol',
                            data=protocols_comparison, sort=False, palette="Dark2_r")
        fig5.get_figure().savefig('devs.png')
        fig5.clear()
        fig6 = sns.barplot(y="Protocol", x="Percentage change in active devs",
                           data=percentage_changes, palette="RdYlGn")
        fig6.get_figure().savefig('devs_change.png')
        fig6.clear()
//  [python]
def lineplot(data_list, fun_list, titles, data_labels, timefactor=1, x_label="Events"):
    """
    Visualizes the result of using functions on data using lineplot

    Parameters
    ----------
    data_list : list of numpy arrays
    fun_list : list of functions to run on data
    titles : list of string of same length as fun_list
    data_labels : list of string of same length as data_list
    timefactor : float
        such that the x axis should be divided with
    x_label : string

    Returns
    -------
    figure with subplots for each function
    """
    num_data = len(data_list)
    num_fun = len(fun_list)
    df_list = [[[] for _ in range(num_data)] for _ in range(num_fun)]

    for i in range(num_fun):
        for j in range(num_data):
            df_list[i][j] = pd.DataFrame(fun_list[i](data_list[j]))
            df_list[i][j]["index"] = df_list[i][j].index
            df_list[i][j] = pd.melt(
                df_list[i][j], id_vars="index", value_name="fun", var_name="point"
            )
            df_list[i][j]["timepoint"] = df_list[i][j]["point"] / timefactor

    plt.rcParams.update({"font.size": 20})
    fig = plt.figure(figsize=[12, 10])
    ax = [fig.add_subplot("{}1{}".format(num_fun, i + 1)) for i in range(num_fun)]

    for i in range(num_fun):
        for j in range(num_data):
            sns.pointplot(
                x="timepoint",
                y="fun",
                data=df_list[i][j],
                ci="sd",
                ax=ax[i],
                label=data_labels[j],
            )

    for t in range(len(titles)):
        ax[t].set_title(titles[t])
        ax[t].legend(loc="lower left")
        ax[t].set_xlabel(x_label)
        ax[t].set_ylabel("Price")

    fig.tight_layout()
    return fig
//  [python]
def lineplot(data_list, fun_list, titles, data_labels, timefactor=1, x_label="Events"):
    """
    Visualizes the result of using functions on data using lineplot

    Parameters
    ----------
    data_list : list of numpy arrays
    fun_list : list of functions to run on data
    titles : list of string of same length as fun_list
    data_labels : list of string of same length as data_list
    timefactor : float
        such that the x axis should be divided with
    x_label : string

    Returns
    -------
    figure with subplots for each function
    """
    num_data = len(data_list)
    num_fun = len(fun_list)
    df_list = [[[] for _ in range(num_data)] for _ in range(num_fun)]

    for i in range(num_fun):
        for j in range(num_data):
            df_list[i][j] = pd.DataFrame(fun_list[i](data_list[j]))
            df_list[i][j]["index"] = df_list[i][j].index
            df_list[i][j] = pd.melt(
                df_list[i][j], id_vars="index", value_name="fun", var_name="point"
            )
            df_list[i][j]["timepoint"] = df_list[i][j]["point"] / timefactor

    plt.rcParams.update({"font.size": 20})
    fig = plt.figure(figsize=[12, 10])
    ax = [fig.add_subplot("{}1{}".format(num_fun, i + 1)) for i in range(num_fun)]

    for i in range(num_fun):
        for j in range(num_data):
            sns.lineplot(
                x="timepoint",
                y="fun",
                data=df_list[i][j],
                ci="sd",
                ax=ax[i],
                label=data_labels[j],
            )

    for t in range(len(titles)):
        ax[t].set_title(titles[t])
        ax[t].legend(loc="lower left")
        ax[t].set_xlabel(x_label)
        ax[t].set_ylabel("Price")

    fig.tight_layout()
    return fig
//  [python]
def draw_it(title, x_label, y_label, data,x_start=0,x_end=24,image_path=".cache/image"):
	# print(data)
	f, ax = plt.subplots()

	logger.debug("数据：%r", data)
	for c in data:
		logger.debug("开始画 数据分类：%r",c)
		_data = data[c]
		_data = np.array(_data)#有可能传入的是个数组，所以要转换成ndarray
		plt.plot(_data,marker='o', label=c)

	ax.xaxis.set_major_locator(MultipleLocator(1))
	ax.xaxis.set_minor_locator(MultipleLocator(1))
	# ax.yaxis.set_major_locator(MultipleLocator(5))
	# ax.yaxis.set_minor_locator(MultipleLocator(1))	
	ax.tick_params(axis='x', colors='b')
	plt.xlim((x_start,x_end))
	plt.ylim(bottom=1)
	#plt.xlim((0,24))

	plt.xticks(fontsize=10)
	plt.legend(loc='upper right')
	plt.title(title, fontsize='large', fontweight='bold')  # 设置字体大小与格式
	plt.xlabel(x_label, fontsize=10)
	plt.ylabel(y_label, fontsize=10)

	# plt.show()
	random_file_name = ''.join(random.sample(string.ascii_letters + string.digits, 8))
	temp_file = image_path + "/" + random_file_name + ".jpg"
	logger.debug("将图片保存到文件：%s", temp_file)
	f.savefig(temp_file, dpi=100, bbox_inches='tight')
	return temp_file
//  [python]
def draw_it(title, x_label, y_label, data,x_start=0,x_end=24,image_path=".cache/image"):
	# print(data)
	f, ax = plt.subplots()

	logger.debug("数据：%r", data)
	for c in data:
		logger.debug("开始画 数据分类：%r",c)
		_data = data[c]
		_data = np.array(_data)#有可能传入的是个数组，所以要转换成ndarray
		sns.lineplot(marker='o', data=_data, err_style="bars", label=c)

	ax.xaxis.set_major_locator(MultipleLocator(1))
	ax.xaxis.set_minor_locator(MultipleLocator(1))
	# ax.yaxis.set_major_locator(MultipleLocator(5))
	# ax.yaxis.set_minor_locator(MultipleLocator(1))	
	ax.tick_params(axis='x', colors='b')
	plt.xlim((x_start,x_end))
	plt.ylim(bottom=1)
	#plt.xlim((0,24))

	plt.xticks(fontsize=10)
	plt.legend(loc='upper right')
	plt.title(title, fontsize='large', fontweight='bold')  # 设置字体大小与格式
	plt.xlabel(x_label, fontsize=10)
	plt.ylabel(y_label, fontsize=10)

	# plt.show()
	random_file_name = ''.join(random.sample(string.ascii_letters + string.digits, 8))
	temp_file = image_path + "/" + random_file_name + ".jpg"
	logger.debug("将图片保存到文件：%s", temp_file)
	f.savefig(temp_file, dpi=100, bbox_inches='tight')
	return temp_file