jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def make_plot_smooth(result_key_dict, result_key, title='plot', xlabel='x',
              ylabel=None, out_dir='logs/figures', save=False, show=True, smooth=30, fancyplot=True):
    """
    Make a plot for each layer, comparing the result_key for all the
        training methods used when creating the result_dict. Smooth over a fixed window.
    Args:
        result_dict:
        result_key:
        title:
        xlabel:
        ylabel:
        out_dir:
        save:
        show:

    Returns:

    """
    if ylabel is None:
        ylabel = result_key

    if isinstance(result_key_dict[list(result_key_dict.keys())[0]], np.ndarray):
        plt.figure()
        legend = []
        for name, array in result_key_dict.items():
            legend.append(name)
            plt.plot(array)
        plt.legend(legend)
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        plt.title(title)
        sns.utils.despine()

        if save:
            file_name = title + '.pdf'
            plt.savefig(os.path.join(out_dir, file_name))
        if show:
            plt.show()

    if isinstance(result_key_dict[list(result_key_dict.keys())[0]], pd.DataFrame):




        layerwise_dict, legend = create_layerwise_result_dict(result_key_dict)
        for idx, df in layerwise_dict.items():

            df.to_pickle(os.path.join(f"df{idx}.pkl"))
            df = pd.read_pickle(os.path.join(f"df{idx}.pkl"))

            ncols = len(df.columns)
            for col in range(ncols):
                df[f'roll_mean{col}'] = df.iloc[:, col].rolling(window=smooth, min_periods=1).mean()
            for col in range(ncols):
                df[f'roll_std{col}'] = df.iloc[:, col].rolling(window=smooth, min_periods=1).std()

            df_points = df.iloc[:, :ncols]
            df_roll_mean = df.iloc[:, ncols:2*ncols].T.reset_index(drop=True).T
            df_roll_std = df.iloc[:, 2*ncols:].T.reset_index(drop=True).T

            df_upper = df_roll_mean.add(df_roll_std, fill_value=0)
            df_lower = df_roll_mean.sub(df_roll_std, fill_value=0)

            plt.figure()
            figure_title = title + ' layer ' + str(idx+1)
            ax = df_points.plot(title=figure_title, style='.', colormap='viridis')
            df_roll_mean.plot(title=figure_title, style='-', ax=ax, colormap='viridis')
            df_upper.plot(title=figure_title, style='--', ax=ax, colormap='viridis')
            df_lower.plot(title=figure_title, style='--', ax=ax, colormap='viridis')
            ax.set_xlabel(xlabel)
            ax.set_ylabel(ylabel)
            ax.xaxis.set_major_locator(plt.MaxNLocator(4))
            ax.yaxis.set_major_locator(plt.MaxNLocator(5))
            plt.legend(legend)
            exit()

            if save:
                file_name = title + '_layer' + str(idx) + '.pdf'
                plt.savefig(os.path.join(out_dir, file_name))
            if show:
                plt.show()
//  [python]
def plot_2d_pareto(trajectories: dict, scale):
    """Adaptation of code from: https://github.com/Cranial-XIX/CAGrad"""
    fig, ax = plt.subplots(figsize=(6, 5))

    F = Toy(scale=scale)

    losses = []
    for res in trajectories.values():
        losses.append(F.batch_forward(torch.from_numpy(res["traj"])))

    yy = -8.3552
    x = np.linspace(-7, 7, 1000)

    inpt = np.stack((x, [yy] * len(x))).T
    Xs = torch.from_numpy(inpt).double()

    Ys = F.batch_forward(Xs)
    ax.plot(
        Ys.numpy()[:, 0],
        Ys.numpy()[:, 1],
        "-",
        linewidth=8,
        color="#72727A",
        label="Pareto Front",
    )  # Pareto front

    for i, tt in enumerate(losses):
        ax.scatter(
            tt[0, 0],
            tt[0, 1],
            color="k",
            s=150,
            zorder=10,
            label="Initial Point" if i == 0 else None,
        )
        colors = matplotlib.cm.magma_r(np.linspace(0.1, 0.6, tt.shape[0]))
        ax.scatter(tt[:, 0], tt[:, 1], color=colors, s=5, zorder=9)

    sns.despine()
    ax.set_xlabel(r"$\ell_1$", size=30)
    ax.set_ylabel(r"$\ell_2$", size=30)
    ax.xaxis.set_label_coords(1.015, -0.03)
    ax.yaxis.set_label_coords(-0.01, 1.01)

    for tick in ax.xaxis.get_major_ticks():
        tick.label.set_fontsize(20)
    for tick in ax.yaxis.get_major_ticks():
        tick.label.set_fontsize(20)

    plt.tight_layout()

    legend = ax.legend(
        loc=2, bbox_to_anchor=(-0.15, 1.3), frameon=False, fontsize=20, ncol=2
    )

    return ax, fig, legend
//  [python]
def plot_2d_pareto(trajectories: dict, scale):
    """Adaptation of code from: https://github.com/Cranial-XIX/CAGrad"""
    fig, ax = plt.subplots(figsize=(6, 5))

    F = Toy(scale=scale)

    losses = []
    for res in trajectories.values():
        losses.append(F.batch_forward(torch.from_numpy(res["traj"])))

    yy = -8.3552
    x = np.linspace(-7, 7, 1000)

    inpt = np.stack((x, [yy] * len(x))).T
    Xs = torch.from_numpy(inpt).double()

    Ys = F.batch_forward(Xs)
    ax.plot(
        Ys.numpy()[:, 0],
        Ys.numpy()[:, 1],
        "-",
        linewidth=8,
        color="#72727A",
        label="Pareto Front",
    )  # Pareto front

    for i, tt in enumerate(losses):
        ax.scatter(
            tt[0, 0],
            tt[0, 1],
            color="k",
            s=150,
            zorder=10,
            label="Initial Point" if i == 0 else None,
        )
        colors = matplotlib.cm.magma_r(np.linspace(0.1, 0.6, tt.shape[0]))
        ax.scatter(tt[:, 0], tt[:, 1], color=colors, s=5, zorder=9)

    sns.utils.despine()
    ax.set_xlabel(r"$\ell_1$", size=30)
    ax.set_ylabel(r"$\ell_2$", size=30)
    ax.xaxis.set_label_coords(1.015, -0.03)
    ax.yaxis.set_label_coords(-0.01, 1.01)

    for tick in ax.xaxis.get_major_ticks():
        tick.label.set_fontsize(20)
    for tick in ax.yaxis.get_major_ticks():
        tick.label.set_fontsize(20)

    plt.tight_layout()

    legend = ax.legend(
        loc=2, bbox_to_anchor=(-0.15, 1.3), frameon=False, fontsize=20, ncol=2
    )

    return ax, fig, legend
//  [python]
def generate_violinplots(np_data, description, ylabel, log):
    # Process the results and store in Panda objects

    col = description
    loss_val_np = np.rot90(np_data)
    pd_df = pd.DataFrame.from_records(loss_val_np, columns=col)

    # Set up the matplotlib figure

    f, ax = plt.subplots()

    sns.set(style="whitegrid")

    ax = sns.violinplot(data=pd_df, palette="Set3", bw=.2, cut=1, linewidth=1)

    if log:
        ax.set_yscale("log")

    ax.set(xlabel='Models/Methods', ylabel=ylabel)
    ax = sns.despine(left=True, bottom=True)

    plt.show()
//  [python]
def generate_violinplots(np_data, description, ylabel, log):
    # Process the results and store in Panda objects

    col = description
    loss_val_np = np.rot90(np_data)
    pd_df = pd.DataFrame.from_records(loss_val_np, columns=col)

    # Set up the matplotlib figure

    f, ax = plt.subplots()

    sns.set(style="whitegrid")

    ax = sns.violinplot(data=pd_df, palette="Set3", bw=.2, cut=1, linewidth=1)

    if log:
        ax.set_yscale("log")

    ax.set(xlabel='Models/Methods', ylabel=ylabel)
    ax = sns.utils.despine(left=True, bottom=True)

    plt.show()
//  [python]
def generate_barplot(np_data, description, ylabel, log):
    # Process the results and store in Panda objects
    col = description
    loss_val_np = np.rot90(np_data)
    pd_df = pd.DataFrame.from_records(loss_val_np, columns=col)

    # Set up the matplotlib figure

    f, ax = plt.subplots()

    sns.set(style="whitegrid")

    ax = sns.barplot(data=pd_df)

    if log:
        ax.set_yscale("log")

    ax.set(xlabel='Models/Methods', ylabel=ylabel)
    ax = sns.despine(left=True, bottom=True)

    plt.show()
//  [python]
def generate_barplot(np_data, description, ylabel, log):
    # Process the results and store in Panda objects
    col = description
    loss_val_np = np.rot90(np_data)
    pd_df = pd.DataFrame.from_records(loss_val_np, columns=col)

    # Set up the matplotlib figure

    f, ax = plt.subplots()

    sns.set(style="whitegrid")

    ax = sns.barplot(data=pd_df)

    if log:
        ax.set_yscale("log")

    ax.set(xlabel='Models/Methods', ylabel=ylabel)
    ax = sns.utils.despine(left=True, bottom=True)

    plt.show()
//  [python]
def generate_boxplot(np_data, description, ylabel, log):
    # Process the results and store in Panda objects
    col = description
    loss_val_np = np.rot90(np_data)
    pd_df = pd.DataFrame.from_records(loss_val_np, columns=col)

    # Set up the matplotlib figure

    f, ax = plt.subplots()

    sns.set(style="whitegrid")

    ax = sns.boxplot(data=pd_df)

    if log:
        ax.set_yscale("log")

    ax.set(xlabel='Models/Methods', ylabel=ylabel)
    ax = sns.despine(left=True, bottom=True)

    plt.show()
//  [python]
def generate_boxplot(np_data, description, ylabel, log):
    # Process the results and store in Panda objects
    col = description
    loss_val_np = np.rot90(np_data)
    pd_df = pd.DataFrame.from_records(loss_val_np, columns=col)

    # Set up the matplotlib figure

    f, ax = plt.subplots()

    sns.set(style="whitegrid")

    ax = sns.boxplot(data=pd_df)

    if log:
        ax.set_yscale("log")

    ax.set(xlabel='Models/Methods', ylabel=ylabel)
    ax = sns.utils.despine(left=True, bottom=True)

    plt.show()
//  [python]
def plot_summary_df(sdf, savename=None, title=None):
    """Creates a boxplot summary of the dataframe. Each main index corresponds to
    a different plot, and secondary indices are different boxes. For each box the mean
    and standard deviation is reported."""

    # capture mean and std, and keys for main plots
    mean, std = sdf.mean(), sdf.std()
    Ks = sdf.columns.get_level_values(0).unique()

    NK = len(Ks)
    fig, axs = plt.subplots(2, 1, figsize=(8, NK * 3))

    for nk, k in enumerate(Ks):

        # build sub-dataframe
        dfi = sdf[k].unstack().reset_index().drop(columns=["level_1"])
        dfi = dfi.rename(columns={"level_0": "type", 0: "value"})

        # box-plots
        ax = axs[nk]
        sns.boxplot(ax=ax, data=dfi, x="value", y="type", color="C0", width=0.6)
        ax.set_ylabel("")
        ax.set_xlabel(k)
        ax.grid(axis="x", alpha=0.4)

        # report mean and standard deviation
        M = dfi["value"].max()
        cols = sdf[k].columns
        for nc, c in enumerate(cols):
            ax.text(M * 1.1, nc, f"{mean[(k,c)]:.2} $\pm$ {std[(k,c)]:.2}")

    # decorate plot (title and spines)
    st = title.split("_")
    st = f"{st[0].capitalize()[0]}. {st[1].capitalize()}"
    axs[0].set_title(st)
    sns.despine(fig=fig)

    # save and close
    plt.tight_layout()
    plt.savefig(savename)
    plt.close(fig)
//  [python]
def plot_summary_df(sdf, savename=None, title=None):
    """Creates a boxplot summary of the dataframe. Each main index corresponds to
    a different plot, and secondary indices are different boxes. For each box the mean
    and standard deviation is reported."""

    # capture mean and std, and keys for main plots
    mean, std = sdf.mean(), sdf.std()
    Ks = sdf.columns.get_level_values(0).unique()

    NK = len(Ks)
    fig, axs = plt.subplots(2, 1, figsize=(8, NK * 3))

    for nk, k in enumerate(Ks):

        # build sub-dataframe
        dfi = sdf[k].unstack().reset_index().drop(columns=["level_1"])
        dfi = dfi.rename(columns={"level_0": "type", 0: "value"})

        # box-plots
        ax = axs[nk]
        sns.boxplot(ax=ax, data=dfi, x="value", y="type", color="C0", width=0.6)
        ax.set_ylabel("")
        ax.set_xlabel(k)
        ax.grid(axis="x", alpha=0.4)

        # report mean and standard deviation
        M = dfi["value"].max()
        cols = sdf[k].columns
        for nc, c in enumerate(cols):
            ax.text(M * 1.1, nc, f"{mean[(k,c)]:.2} $\pm$ {std[(k,c)]:.2}")

    # decorate plot (title and spines)
    st = title.split("_")
    st = f"{st[0].capitalize()[0]}. {st[1].capitalize()}"
    axs[0].set_title(st)
    sns.utils.despine(fig=fig)

    # save and close
    plt.tight_layout()
    plt.savefig(savename)
    plt.close(fig)
//  [python]
def generate_hboxplot(np_data, description, ylabel, log, set_x_axis_size=None):
    # Process the results and store in Panda objects
    col = description
    loss_val_np = np.rot90(np_data)

    pd_df = pd.DataFrame.from_records(loss_val_np, columns=col)

    # Set up the matplotlib figure
    sns.set(style="whitegrid")

    f, ax = plt.subplots(figsize=(15, 6))

    ax = sns.boxplot(data=pd_df, orient="h", width=0.4)

    if log:
        ax.set_xscale("log")

    if set_x_axis_size != None:
        ax.set_xlim(set_x_axis_size)

    ax.set(ylabel='Models/Methods', xlabel=ylabel)
    ax = sns.despine(left=True, bottom=True)

    plt.show()
//  [python]
def generate_hboxplot(np_data, description, ylabel, log, set_x_axis_size=None):
    # Process the results and store in Panda objects
    col = description
    loss_val_np = np.rot90(np_data)

    pd_df = pd.DataFrame.from_records(loss_val_np, columns=col)

    # Set up the matplotlib figure
    sns.set(style="whitegrid")

    f, ax = plt.subplots(figsize=(15, 6))

    ax = sns.boxplot(data=pd_df, orient="h", width=0.4)

    if log:
        ax.set_xscale("log")

    if set_x_axis_size != None:
        ax.set_xlim(set_x_axis_size)

    ax.set(ylabel='Models/Methods', xlabel=ylabel)
    ax = sns.utils.despine(left=True, bottom=True)

    plt.show()
//  [python]
def __init__(self, save_animation=False, fps=30):
        """Initialize the helper class.

        :param save_animation: Whether the animation should be saved as a gif. Requires the ImageMagick library.
        :param fps: The number of frames per second when saving the gif animation.
        """
        self.save_animation = save_animation
        self.fps = fps
        self.figure, (self.ax1, self.ax2) = plt.subplots(1, 2, figsize=(8, 4))
        self.figure.suptitle("1D Generative Adversarial Network Example (WGAN-GP)")
        sns.set(color_codes=True, style='white', palette='colorblind')
        sns.despine(self.figure)
        plt.show(block=False)

        if self.save_animation:
            self.writer = ImageMagickWriter(fps=self.fps)
            self.writer.setup(self.figure, 'animation.gif', dpi=100)
//  [python]
def __init__(self, save_animation=False, fps=30):
        """Initialize the helper class.

        :param save_animation: Whether the animation should be saved as a gif. Requires the ImageMagick library.
        :param fps: The number of frames per second when saving the gif animation.
        """
        self.save_animation = save_animation
        self.fps = fps
        self.figure, (self.ax1, self.ax2) = plt.subplots(1, 2, figsize=(8, 4))
        self.figure.suptitle("1D Generative Adversarial Network Example (WGAN-GP)")
        sns.set(color_codes=True, style='white', palette='colorblind')
        sns.utils.despine(self.figure)
        plt.show(block=False)

        if self.save_animation:
            self.writer = ImageMagickWriter(fps=self.fps)
            self.writer.setup(self.figure, 'animation.gif', dpi=100)
//  [python]
def apply_paper_plot_styles(ax: Axes) -> Axes:
    sns.set_style(
        "ticks",
        {
            "axes.edgecolor": "0",
            "grid.linestyle": "",
            "axes.facecolor": "white",
            "font.family": ["Times New Roman Cyr"],
        },
    )
    ax.spines["bottom"].set_linewidth(1.5)  # type: ignore
    ax.spines["left"].set_linewidth(1.5)  # type: ignore
    plt.tick_params(axis="x", which="major", length=6, width=1.5)
    plt.tick_params(axis="y", which="major", length=6, width=1.5, labelsize=8)
    for label in ax.get_xticklabels():  # type: ignore
        label.set_fontsize(label.get_size() - 3)  # type: ignore
    ax.set_ylabel(ax.get_ylabel(), fontsize=plt.rcParams["axes.labelsize"] - 4)  # type: ignore
    sns.despine()  # type: ignore
    return ax
//  [python]
def apply_paper_plot_styles(ax: Axes) -> Axes:
    sns.set_style(
        "ticks",
        {
            "axes.edgecolor": "0",
            "grid.linestyle": "",
            "axes.facecolor": "white",
            "font.family": ["Times New Roman Cyr"],
        },
    )
    ax.spines["bottom"].set_linewidth(1.5)  # type: ignore
    ax.spines["left"].set_linewidth(1.5)  # type: ignore
    plt.tick_params(axis="x", which="major", length=6, width=1.5)
    plt.tick_params(axis="y", which="major", length=6, width=1.5, labelsize=8)
    for label in ax.get_xticklabels():  # type: ignore
        label.set_fontsize(label.get_size() - 3)  # type: ignore
    ax.set_ylabel(ax.get_ylabel(), fontsize=plt.rcParams["axes.labelsize"] - 4)  # type: ignore
    sns.utils.despine()  # type: ignore
    return ax
//  [python]
sns.pointplot(x='tSNR', y='task', data=tsnr_all, join=False,
              scale=1.2, color='darkred', ax=ax1)
ax1.tick_params(axis='y',length=0)
ax1.yaxis.label.set_visible(False)
ax1.set_xlabel('temporal signal-to-noise ratio (tSNR)')
sns.despine()
//  [python]
sns.pointplot(x='tSNR', y='task', data=tsnr_all, join=False,
              scale=1.2, color='darkred', ax=ax1)
ax1.tick_params(axis='y',length=0)
ax1.yaxis.label.set_visible(False)
ax1.set_xlabel('temporal signal-to-noise ratio (tSNR)')
sns.utils.despine()
//  [python]
sns.set_theme(style="whitegrid")

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

# Draw a nested violinplot and split the violins for easier comparison
sns.violinplot(data=tips, x="day", y="total_bill", hue="smoker",
               split=True, inner="quart", linewidth=1,
               palette={"Yes": "b", "No": ".85"})
sns.despine(left=True)
//  [python]
sns.set_theme(style="whitegrid")

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

# Draw a nested violinplot and split the violins for easier comparison
sns.violinplot(data=tips, x="day", y="total_bill", hue="smoker",
               split=True, inner="quart", linewidth=1,
               palette={"Yes": "b", "No": ".85"})
sns.utils.despine(left=True)
//  [python]
def plot(self):
        """Plot multiple parameter estimation data as boxplot
        :return:

        Args:

        Returns:

        """
        for model_name in self.data:
            data = self.data[model_name]
            labels = self.divide_data()[model_name]
            for label_set in range(len(labels)):
                fig = plt.figure()  #
                plot_data = data[labels[label_set]]
                seaborn.boxplot(data=plot_data)
                plt.xticks(rotation=self.xtick_rotation)
                if self.despine:
                    seaborn.despine(fig=fig, top=True, right=True)
                if self.title is not None:
                    plt.title(self.title + '(n={})'.format(data.shape[0]))
                plt.ylabel(self.ylabel)
                if self.savefig:
                    fle = os.path.join(self.results_directory[model_name],
                                       '{}{}.{}'.format(self.filename, label_set, self.ext))
                    plt.savefig(fle, dpi=self.dpi, bbox_inches='tight')
        if self.show:
            plt.show()
//  [python]
def plot(self):
        """Plot multiple parameter estimation data as boxplot
        :return:

        Args:

        Returns:

        """
        for model_name in self.data:
            data = self.data[model_name]
            labels = self.divide_data()[model_name]
            for label_set in range(len(labels)):
                fig = plt.figure()  #
                plot_data = data[labels[label_set]]
                seaborn.boxplot(data=plot_data)
                plt.xticks(rotation=self.xtick_rotation)
                if self.despine:
                    seaborn.utils.despine(fig=fig, top=True, right=True)
                if self.title is not None:
                    plt.title(self.title + '(n={})'.format(data.shape[0]))
                plt.ylabel(self.ylabel)
                if self.savefig:
                    fle = os.path.join(self.results_directory[model_name],
                                       '{}{}.{}'.format(self.filename, label_set, self.ext))
                    plt.savefig(fle, dpi=self.dpi, bbox_inches='tight')
        if self.show:
            plt.show()
//  [python]
def plot(self):
        """Plot multiple parameter estimation data as violin
        :return:

        Args:

        Returns:

        """
        for model_name in self.data:
            data = self.data[model_name]
            labels = self.divide_data()[model_name]
            for label_set in range(len(labels)):
                fig = plt.figure()  #
                plot_data = data[labels[label_set]]
                seaborn.violinplot(data=plot_data, **self.kwargs)
                plt.xticks(rotation=self.xtick_rotation)
                if self.despine:
                    seaborn.despine(fig=fig, top=True, right=True)
                if self.title is not None:
                    plt.title(self.title + '(n={})'.format(data.shape[0]))
                plt.ylabel(self.ylabel)
                if self.savefig:
                    fle = os.path.join(self.results_directory[model_name],
                                       '{}{}.{}'.format(self.filename, label_set, self.ext))
                    plt.savefig(fle, dpi=self.dpi, bbox_inches='tight')
        if self.show:
            plt.show()
//  [python]
def plot(self):
        """Plot multiple parameter estimation data as violin
        :return:

        Args:

        Returns:

        """
        for model_name in self.data:
            data = self.data[model_name]
            labels = self.divide_data()[model_name]
            for label_set in range(len(labels)):
                fig = plt.figure()  #
                plot_data = data[labels[label_set]]
                seaborn.violinplot(data=plot_data, **self.kwargs)
                plt.xticks(rotation=self.xtick_rotation)
                if self.despine:
                    seaborn.utils.despine(fig=fig, top=True, right=True)
                if self.title is not None:
                    plt.title(self.title + '(n={})'.format(data.shape[0]))
                plt.ylabel(self.ylabel)
                if self.savefig:
                    fle = os.path.join(self.results_directory[model_name],
                                       '{}{}.{}'.format(self.filename, label_set, self.ext))
                    plt.savefig(fle, dpi=self.dpi, bbox_inches='tight')
        if self.show:
            plt.show()
//  [python]
def plot(val_curve, system_curves, val_label='Val', legend=True, filename='./Data/Output/ttr_curve.pdf'):
    fig, ax = plt.subplots(figsize=(28,15))
    lw = 8.0
    x,y = curve_to_coords(val_curve)
    ax = plt.plot(x,y,label=val_label, linewidth=lw)
    system_curves = sorted(system_curves.items(),
                           key=lambda item:item[1][max(item[1])], # Sort by highest value at the max X-value.
                           reverse=True)                          # In decreasing order.
    for name, curve in system_curves:
        x,y = curve_to_coords(curve)
        plt.plot(x,y,label=name, linewidth=lw)
    plt.ylabel('Types')
    plt.xlabel('Tokens')
    sns.despine()
    if legend:
        lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
        plt.savefig(filename, bbox_extra_artists=(lgd,), bbox_inches='tight')
    else:
        plt.savefig(filename)
    plt.clf()
//  [python]
def plot(val_curve, system_curves, val_label='Val', legend=True, filename='./Data/Output/ttr_curve.pdf'):
    fig, ax = plt.subplots(figsize=(28,15))
    lw = 8.0
    x,y = curve_to_coords(val_curve)
    ax = plt.plot(x,y,label=val_label, linewidth=lw)
    system_curves = sorted(system_curves.items(),
                           key=lambda item:item[1][max(item[1])], # Sort by highest value at the max X-value.
                           reverse=True)                          # In decreasing order.
    for name, curve in system_curves:
        x,y = curve_to_coords(curve)
        plt.plot(x,y,label=name, linewidth=lw)
    plt.ylabel('Types')
    plt.xlabel('Tokens')
    sns.utils.despine()
    if legend:
        lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
        plt.savefig(filename, bbox_extra_artists=(lgd,), bbox_inches='tight')
    else:
        plt.savefig(filename)
    plt.clf()
//  [python]
def _run_interface(self, runtime):
        import matplotlib.pyplot as plt
        import seaborn as sns
        from nilearn import image, masking

        self._results["out_file"] = fname_presuffix(
            self.inputs.cbf,
            suffix="_cbfplot.svg",
            use_ext=False,
            newpath=runtime.cwd,
        )

        dfs = []
        for i_tissue_type, tissue_type in enumerate(["GM", "WM", "CSF"]):
            tissue_type_val = i_tissue_type + 1
            mask_img = image.math_img(
                f"(img == {tissue_type_val}).astype(int)",
                img=self.inputs.seg_file,
            )
            tissue_type_vals = masking.apply_mask(self.inputs.cbf, mask_img)
            df = pd.DataFrame(
                columns=["CBF\n(mL/100 g/min)", "Tissue Type"],
                data=list(
                    map(list, zip(*[tissue_type_vals, [tissue_type] * tissue_type_vals.size]))
                ),
            )
            dfs.append(df)

        df = pd.concat(dfs, axis=0)

        # Create the plot
        with sns.axes_style("whitegrid"), sns.plotting_context(font_scale=3):
            fig, ax = plt.subplots(figsize=(16, 8))
            sns.despine(ax=ax, bottom=True, left=True)
            sns.boxenplot(
                y="CBF\n(mL/100 g/min)",
                data=df,
                width=0.6,
                showfliers=True,
                palette={"GM": "#1b60a5", "WM": "#2da467", "CSF": "#9d8f25"},
                hue="Tissue Type",
                legend=False,
                ax=ax,
            )
            fig.tight_layout()
            fig.savefig(self._results["out_file"])
            fig.clf()

        return runtime
//  [python]
def _run_interface(self, runtime):
        import matplotlib.pyplot as plt
        import seaborn as sns
        from nilearn import image, masking
        from seaborn.utils import despine

        self._results["out_file"] = fname_presuffix(
            self.inputs.cbf,
            suffix="_cbfplot.svg",
            use_ext=False,
            newpath=runtime.cwd,
        )

        dfs = []
        for i_tissue_type, tissue_type in enumerate(["GM", "WM", "CSF"]):
            tissue_type_val = i_tissue_type + 1
            mask_img = image.math_img(
                f"(img == {tissue_type_val}).astype(int)",
                img=self.inputs.seg_file,
            )
            tissue_type_vals = masking.apply_mask(self.inputs.cbf, mask_img)
            df = pd.DataFrame(
                columns=["CBF\n(mL/100 g/min)", "Tissue Type"],
                data=list(
                    map(list, zip(*[tissue_type_vals, [tissue_type] * tissue_type_vals.size]))
                ),
            )
            dfs.append(df)

        df = pd.concat(dfs, axis=0)

        # Create the plot
        with sns.axes_style("whitegrid"), sns.plotting_context(font_scale=3):
            fig, ax = plt.subplots(figsize=(16, 8))
            despine(ax=ax, bottom=True, left=True)
            sns.boxenplot(
                y="CBF\n(mL/100 g/min)",
                data=df,
                width=0.6,
                showfliers=True,
                palette={"GM": "#1b60a5", "WM": "#2da467", "CSF": "#9d8f25"},
                hue="Tissue Type",
                legend=False,
                ax=ax,
            )
            fig.tight_layout()
            fig.savefig(self._results["out_file"])
            fig.clf()

        return runtime
//  [python]
def plot(self):
        """Plot Rss Vs rank of best fit
        :return:
            None

        Args:

        Returns:

        """
        figs = {}
        for label, df in self.data.items():
            figs[label] = plt.figure()
            if self.log10:
                x = numpy.log10(list(range(df['RSS'].shape[0])))
            else:
                x = list(range(df['RSS'].shape[0]))

            plt.plot(x,
                     df['RSS'].sort_values(ascending=True),
                     color=self.color, linewidth=self.linewidth,
                     alpha=self.line_transparency,
                     )

            plt.plot(x,
                     df['RSS'].sort_values(ascending=True), self.marker,
                     color=self.markercolor, markersize=self.markersize,
                     alpha=self.marker_transparency
                     )

            plt.xticks(rotation=self.xtick_rotation)
            if self.title is not None:
                plt.title(self.title + '(n={})'.format(df.shape[0]))
            plt.ylabel(self.ylabel)
            plt.xlabel('Rank of Best Fit')
            if self.despine:
                seaborn.despine(fig=figs[label], top=True, right=True)
            if self.savefig:
                self.results_directory = self.create_directory()
                for model_name, folder in self.results_directory.items():
                    fle = os.path.join(folder, '{}_{}.{}'.format(self.filename, model_name, self.ext))
                    plt.savefig(fle, dpi=self.dpi, bbox_inches='tight')
                    print('Saved to {}'.format(fle))

            if self.show:
                plt.show()

        return figs
//  [python]
def plot(self):
        """Plot Rss Vs rank of best fit
        :return:
            None

        Args:

        Returns:

        """
        figs = {}
        for label, df in self.data.items():
            figs[label] = plt.figure()
            if self.log10:
                x = numpy.log10(list(range(df['RSS'].shape[0])))
            else:
                x = list(range(df['RSS'].shape[0]))

            plt.plot(x,
                     df['RSS'].sort_values(ascending=True),
                     color=self.color, linewidth=self.linewidth,
                     alpha=self.line_transparency,
                     )

            plt.plot(x,
                     df['RSS'].sort_values(ascending=True), self.marker,
                     color=self.markercolor, markersize=self.markersize,
                     alpha=self.marker_transparency
                     )

            plt.xticks(rotation=self.xtick_rotation)
            if self.title is not None:
                plt.title(self.title + '(n={})'.format(df.shape[0]))
            plt.ylabel(self.ylabel)
            plt.xlabel('Rank of Best Fit')
            if self.despine:
                seaborn.utils.despine(fig=figs[label], top=True, right=True)
            if self.savefig:
                self.results_directory = self.create_directory()
                for model_name, folder in self.results_directory.items():
                    fle = os.path.join(folder, '{}_{}.{}'.format(self.filename, model_name, self.ext))
                    plt.savefig(fle, dpi=self.dpi, bbox_inches='tight')
                    print('Saved to {}'.format(fle))

            if self.show:
                plt.show()

        return figs
//  [python]
def boxswarm_plot(dataframe, ranks, output_path):
    """ plot the box swarm plots: these are relegated mostly to the notebook
    and dont feature in the paper at present """
    fig = plt.figure(figsize=(14, 7), dpi=800)
    ax1 = fig.add_subplot(1, 1, 1)
    p = sns.boxplot(data=dataframe,
                    y='EFO term',
                    ax=ax1,
                    order=ranks,
                    x='isfemale',
                    whis=1.5,
                    palette="vlag",
                    fliersize=0)
    sns.swarmplot(y='EFO term',
                  x='isfemale',
                  data=dataframe,
                  order=ranks,
                  size=2.25,
                  color="white",
                  edgecolor="black",
                  linewidth=0.5,
                  ax=ax1)
    p.set_ylabel('')
    p.set_xlim(0, 1.05)
    p.set_xlabel('Female/Male Ratio Per Paper Across Mapped Terms',
                 fontsize=12)
    p.tick_params(labelsize=12)
    plt.setp(p.spines.values(), color='k')
    plt.setp([p.get_xticklines(), p.get_yticklines()], color='k')
    sns.despine(bottom=False, left=False, offset=10, ax=ax1)
    plt.tight_layout()
    plt.savefig(output_path, bbox_inches='tight')
//  [python]
def boxswarm_plot(dataframe, ranks, output_path):
    """ plot the box swarm plots: these are relegated mostly to the notebook
    and dont feature in the paper at present """
    fig = plt.figure(figsize=(14, 7), dpi=800)
    ax1 = fig.add_subplot(1, 1, 1)
    p = sns.boxplot(data=dataframe,
                    y='EFO term',
                    ax=ax1,
                    order=ranks,
                    x='isfemale',
                    whis=1.5,
                    palette="vlag",
                    fliersize=0)
    sns.swarmplot(y='EFO term',
                  x='isfemale',
                  data=dataframe,
                  order=ranks,
                  size=2.25,
                  color="white",
                  edgecolor="black",
                  linewidth=0.5,
                  ax=ax1)
    p.set_ylabel('')
    p.set_xlim(0, 1.05)
    p.set_xlabel('Female/Male Ratio Per Paper Across Mapped Terms',
                 fontsize=12)
    p.tick_params(labelsize=12)
    plt.setp(p.spines.values(), color='k')
    plt.setp([p.get_xticklines(), p.get_yticklines()], color='k')
    sns.utils.despine(bottom=False, left=False, offset=10, ax=ax1)
    plt.tight_layout()
    plt.savefig(output_path, bbox_inches='tight')
//  [python]
def plot_equity_curve(self, return_data, benchmark=None):
        """Function to plot event timeline with equity curve second axis

        Params:
            benchmark : pd.Series() of benchmark returns
        """
        fig, ax = plt.subplots(figsize=(10, 7))

        if isinstance(benchmark, pd.Series):
            benchmark = benchmark.loc[return_data.index[0]:]
            ax.plot(benchmark.index, np.exp(benchmark.cumsum()) - 1,
                    color='red', lw=1, marker='d', markersize=3,
                    label='SPY-B&H')
        ax.plot(return_data.index, np.exp(return_data.values) - 1,
                color='k', lw=1, marker='^', markersize=5,
                label=self.mkt + '-Algo')
        ax.axhline(y=0, color='k', ls='--', lw=3)
        ax.set_xlim(pd.to_datetime(str(self.year) + '-12-31'),
                    return_data.index[-1])
        ax.set_xlabel('Dates')
        ax.set_title(f"{self.mkt}\nJohnsonSU Distribution | {self.event_state.upper()}",
                     fontsize=16)
        ax.legend(loc='upper right', fontsize=11,
                  frameon=True).get_frame().set_edgecolor('blue')
        sns.despine(offset=2)
        file_str = self.DIR + \
            f'[JSU]-{self.mkt}-{self.event_state.upper()}-EquityCurve-{self.today}.png'
        fig.savefig(file_str, dpi=300)
        return
//  [python]
def plot_equity_curve(self, return_data, benchmark=None):
        """Function to plot event timeline with equity curve second axis

        Params:
            benchmark : pd.Series() of benchmark returns
        """
        fig, ax = plt.subplots(figsize=(10, 7))

        if isinstance(benchmark, pd.Series):
            benchmark = benchmark.loc[return_data.index[0]:]
            ax.plot(benchmark.index, np.exp(benchmark.cumsum()) - 1,
                    color='red', lw=1, marker='d', markersize=3,
                    label='SPY-B&H')
        ax.plot(return_data.index, np.exp(return_data.values) - 1,
                color='k', lw=1, marker='^', markersize=5,
                label=self.mkt + '-Algo')
        ax.axhline(y=0, color='k', ls='--', lw=3)
        ax.set_xlim(pd.to_datetime(str(self.year) + '-12-31'),
                    return_data.index[-1])
        ax.set_xlabel('Dates')
        ax.set_title(f"{self.mkt}\nJohnsonSU Distribution | {self.event_state.upper()}",
                     fontsize=16)
        ax.legend(loc='upper right', fontsize=11,
                  frameon=True).get_frame().set_edgecolor('blue')
        sns.utils.despine(offset=2)
        file_str = self.DIR + \
            f'[JSU]-{self.mkt}-{self.event_state.upper()}-EquityCurve-{self.today}.png'
        fig.savefig(file_str, dpi=300)
        return
//  [python]
def __init__(
        self, pre_norm, post_norm, figsize=(4, 4), labels=["Native", "Latent"]
    ):
        """
        pre_norm = flattened vector of normalized, unique cell-cell distances "pre-transformation".
            Upper triangle of cell-cell distance matrix, flattened to vector of shape ((n_cells^2)/2)-n_cells.
        post_norm = flattened vector of normalized, unique cell-cell distances "post-transformation".
            Upper triangle of cell-cell distance matrix, flattened to vector of shape ((n_cells^2)/2)-n_cells.
        figsize = size of resulting axes
        labels = name of pre- and post-transformation spaces for legend (plot_cell_distances, plot_distributions,
            plot_cumulative_distributions) or axis labels (plot_distance_correlation, joint_plot_distance_correlation)
            as list of two strings. False to exclude labels.
        """
        self.figsize = figsize
        self.fig, self.ax = plt.subplots(1, figsize=self.figsize)
        self.palette = sns.cubehelix_palette()
        self.cmap = sns.cubehelix_palette(as_cmap=True)
        self.pre = pre_norm
        self.post = post_norm
        self.labels = labels

        plt.tick_params(labelbottom=False, labelleft=False)
        sns.despine()
        plt.tight_layout()
//  [python]
def __init__(
        self, pre_norm, post_norm, figsize=(4, 4), labels=["Native", "Latent"]
    ):
        """
        pre_norm = flattened vector of normalized, unique cell-cell distances "pre-transformation".
            Upper triangle of cell-cell distance matrix, flattened to vector of shape ((n_cells^2)/2)-n_cells.
        post_norm = flattened vector of normalized, unique cell-cell distances "post-transformation".
            Upper triangle of cell-cell distance matrix, flattened to vector of shape ((n_cells^2)/2)-n_cells.
        figsize = size of resulting axes
        labels = name of pre- and post-transformation spaces for legend (plot_cell_distances, plot_distributions,
            plot_cumulative_distributions) or axis labels (plot_distance_correlation, joint_plot_distance_correlation)
            as list of two strings. False to exclude labels.
        """
        self.figsize = figsize
        self.fig, self.ax = plt.subplots(1, figsize=self.figsize)
        self.palette = sns.cubehelix_palette()
        self.cmap = sns.cubehelix_palette(as_cmap=True)
        self.pre = pre_norm
        self.post = post_norm
        self.labels = labels

        sns.utils.despine()
        plt.tight_layout()
//  [python]
def test_despine(self):
        f, ax = plt.subplots()
        for side in self.sides:
            assert ax.spines[side].get_visible()

        s.despine()
        for side in self.outer_sides:
            assert not ax.spines[side].get_visible()
        for side in self.inner_sides:
            assert ax.spines[side].get_visible()

        s.despine(**dict(zip(self.sides, [True] * 4)))
        for side in self.sides:
            assert not ax.spines[side].get_visible()
//  [python]
def test_despine(self):
        f, ax = plt.subplots()
        for side in self.sides:
            assert ax.spines[side].get_visible()

        utils.despine()
        for side in self.outer_sides:
            assert not ax.spines[side].get_visible()
        for side in self.inner_sides:
            assert ax.spines[side].get_visible()

        utils.despine(**dict(zip(self.sides, [True] * 4)))
        for side in self.sides:
            assert not ax.spines[side].get_visible()
//  [python]
def plot_model_weights(wx, wy, tx, ty, title="", save_path=None):
    """
    Plot weights of the model against the true weights.
    """
    fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
    plot_true_weights_coloured(axs[0, 0], tx, tx, title="True x weights")
    plot_true_weights_coloured(axs[0, 1], ty, ty, title="True y weights")
    plot_true_weights_coloured(axs[1, 0], wx, tx, title="Model x weights")
    plot_true_weights_coloured(axs[1, 1], wy, ty, title="Model y weights", legend=True)
    plt.legend([], [], frameon=False)
    fig.suptitle(title)
    sns.despine(trim=True)
    plt.tight_layout()
    if save_path:
        plt.savefig(save_path)
    else:
        plt.show(block=False)
//  [python]
def plot_model_weights(wx, wy, tx, ty, title="", save_path=None):
    """
    Plot weights of the model against the true weights.
    """
    fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
    plot_true_weights_coloured(axs[0, 0], tx, tx, title="True x weights")
    plot_true_weights_coloured(axs[0, 1], ty, ty, title="True y weights")
    plot_true_weights_coloured(axs[1, 0], wx, tx, title="Model x weights")
    plot_true_weights_coloured(axs[1, 1], wy, ty, title="Model y weights", legend=True)
    plt.legend([], [], frameon=False)
    fig.suptitle(title)
    sns.utils.despine(trim=True)
    plt.tight_layout()
    if save_path:
        plt.savefig(save_path)
    else:
        plt.show(block=False)
//  [python]
def test_despine_specific_axes(self):
        f, (ax1, ax2) = plt.subplots(2, 1)

        s.despine(ax=ax2)

        for side in self.sides:
            assert ax1.spines[side].get_visible()

        for side in self.outer_sides:
            assert not ax2.spines[side].get_visible()
        for side in self.inner_sides:
            assert ax2.spines[side].get_visible()
//  [python]
def test_despine_specific_axes(self):
        f, (ax1, ax2) = plt.subplots(2, 1)

        utils.despine(ax=ax2)

        for side in self.sides:
            assert ax1.spines[side].get_visible()

        for side in self.outer_sides:
            assert not ax2.spines[side].get_visible()
        for side in self.inner_sides:
            assert ax2.spines[side].get_visible()
//  [python]
def test_despine_with_offset(self):
        f, ax = plt.subplots()

        for side in self.sides:
            pos = ax.spines[side].get_position()
            assert pos == self.original_position

        s.despine(ax=ax, offset=self.offset)

        for side in self.sides:
            is_visible = ax.spines[side].get_visible()
            new_position = ax.spines[side].get_position()
            if is_visible:
                assert new_position == self.offset_position
            else:
                assert new_position == self.original_position
//  [python]
def test_despine_with_offset(self):
        f, ax = plt.subplots()

        for side in self.sides:
            pos = ax.spines[side].get_position()
            assert pos == self.original_position

        utils.despine(ax=ax, offset=self.offset)

        for side in self.sides:
            is_visible = ax.spines[side].get_visible()
            new_position = ax.spines[side].get_position()
            if is_visible:
                assert new_position == self.offset_position
            else:
                assert new_position == self.original_position
//  [python]
def plot(self, ax, cax, kws):
        """Draw the heatmap on the provided Axes."""
        # Remove all the Axes spines
        sns.despine(ax=ax, left=True, bottom=True)

        # Draw the heatmap
        mesh = ax.pcolormesh(self.plot_data, vmin=self.vmin, vmax=self.vmax,
                             cmap=self.cmap, **kws)

        # Set the axis limits
        ax.set(xlim=(0, self.data.shape[1]), ylim=(0, self.data.shape[0]))

        # Add row and column labels
        ax.set(xticks=self.xticks, yticks=self.yticks)
        xtl = ax.set_xticklabels(self.xticklabels)
        ytl = ax.set_yticklabels(self.yticklabels, rotation="vertical", va='center')

        # Possibly rotate them if they overlap
        plt.draw()
        if sns.utils.axis_ticklabels_overlap(xtl):
            plt.setp(xtl, rotation="vertical")
        if sns.utils.axis_ticklabels_overlap(ytl):
            plt.setp(ytl, rotation="horizontal")

        # Add the axis labels
        ax.set(xlabel=self.xlabel, ylabel=self.ylabel)

        # Annotate the cells with the formatted values
        if self.annot:
            self._annotate_heatmap(ax, mesh)
//  [python]
def plot(self, ax, cax, kws):
        """Draw the heatmap on the provided Axes."""
        # Remove all the Axes spines
        sns.utils.despine(ax=ax, left=True, bottom=True)

        # Draw the heatmap
        mesh = ax.pcolormesh(self.plot_data, vmin=self.vmin, vmax=self.vmax,
                             cmap=self.cmap, **kws)

        # Set the axis limits
        ax.set(xlim=(0, self.data.shape[1]), ylim=(0, self.data.shape[0]))

        # Add row and column labels
        ax.set(xticks=self.xticks, yticks=self.yticks)
        xtl = ax.set_xticklabels(self.xticklabels)
        ytl = ax.set_yticklabels(self.yticklabels, rotation="vertical", va='center')

        # Possibly rotate them if they overlap
        plt.draw()
        if sns.utils.axis_ticklabels_overlap(xtl):
            plt.setp(xtl, rotation="vertical")
        if sns.utils.axis_ticklabels_overlap(ytl):
            plt.setp(ytl, rotation="horizontal")

        # Add the axis labels
        ax.set(xlabel=self.xlabel, ylabel=self.ylabel)

        # Annotate the cells with the formatted values
        if self.annot:
            self._annotate_heatmap(ax, mesh)
//  [python]
def plot(self, target_path, title=None):

        ########### TA ###########
        fig, ax = plt.subplots(figsize=(4, 4))

        x = []
        y = []
        for cell, cell_data in self.per_cell_ta_obs.items():
            x.append(cell_data['total'])
            y.append(cell_data['TA_start'] / cell_data['total'])

        ax.scatter(x, y, s=3,c='k')
        ax.set_xscale('log')
        if title is not None:
            ax.set_title(title)

        ax.set_ylabel("Fraction unique cuts starting with TA")
        ax.set_xlabel("# Molecules")
        ax.set_xlim(1, None)
        ax.set_ylim(-0.1, 1.05)
        sns.despine()
        plt.tight_layout()
        plt.savefig(target_path.replace('.png', '.TA.png'))
        plt.close()

        ########### A ###########
        fig, ax = plt.subplots(figsize=(4, 4))

        x = []
        y = []
        for cell, cell_data in self.per_cell_ta_obs.items():
            x.append(cell_data['total'])
            y.append(cell_data['A_start'] / cell_data['total'])

        ax.scatter(x, y, s=3,c='k')
        ax.set_xscale('log')
        if title is not None:
            ax.set_title(title)

        ax.set_ylabel("Fraction unique cuts starting with A")
        ax.set_xlabel("# Molecules")
        ax.set_xlim(1, None)
        ax.set_ylim(-0.1, 1.05)
        plt.tight_layout()
        sns.despine()
        plt.savefig(target_path.replace('.png', '.A.png'))
        plt.close()
//  [python]
def plot(self, target_path, title=None):

        ########### TA ###########
        fig, ax = plt.subplots(figsize=(4, 4))

        x = []
        y = []
        for cell, cell_data in self.per_cell_ta_obs.items():
            x.append(cell_data['total'])
            y.append(cell_data['TA_start'] / cell_data['total'])

        ax.scatter(x, y, s=3,c='k')
        ax.set_xscale('log')
        if title is not None:
            ax.set_title(title)

        ax.set_ylabel("Fraction unique cuts starting with TA")
        ax.set_xlabel("# Molecules")
        ax.set_xlim(1, None)
        ax.set_ylim(-0.1, 1.05)
        sns.utils.despine()
        plt.tight_layout()
        plt.savefig(target_path.replace('.png', '.TA.png'))
        plt.close()

        ########### A ###########
        fig, ax = plt.subplots(figsize=(4, 4))

        x = []
        y = []
        for cell, cell_data in self.per_cell_ta_obs.items():
            x.append(cell_data['total'])
            y.append(cell_data['A_start'] / cell_data['total'])

        ax.scatter(x, y, s=3,c='k')
        ax.set_xscale('log')
        if title is not None:
            ax.set_title(title)

        ax.set_ylabel("Fraction unique cuts starting with A")
        ax.set_xlabel("# Molecules")
        ax.set_xlim(1, None)
        ax.set_ylim(-0.1, 1.05)
        plt.tight_layout()
        sns.utils.despine()
        plt.savefig(target_path.replace('.png', '.A.png'))
        plt.close()
//  [python]
def test_despine_side_specific_offset(self):

        f, ax = plt.subplots()
        s.despine(ax=ax, offset=dict(left=self.offset))

        for side in self.sides:
            is_visible = ax.spines[side].get_visible()
            new_position = ax.spines[side].get_position()
            if is_visible and side == "left":
                assert new_position == self.offset_position
            else:
                assert new_position == self.original_position