jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def plot_average(
        self,
        event_type,
        center="Peak",
        hue="Channel",
        time_before=1,
        time_after=1,
        filt=(None, None),
        mask=None,
        figsize=(6, 4.5),
        **kwargs,
    ):
        """Plot the average event (not for REM, spindles & SW only)"""
        import seaborn as sns
        import matplotlib.pyplot as plt

        df_sync = self.get_sync_events(
            center=center, time_before=time_before, time_after=time_after, filt=filt, mask=mask
        )
        assert not df_sync.empty, "Could not calculate event-locked data."
        assert hue in ["Stage", "Channel"], "hue must be 'Channel' or 'Stage'"
        assert hue in df_sync.columns, "%s is not present in data." % hue

        if event_type == "spindles":
            title = "Average spindle"
        else:  # "sw":
            title = "Average SW"

        # Start figure
        fig, ax = plt.subplots(1, 1, figsize=figsize)
        sns.lineplot(data=df_sync, x="Time", y="Amplitude", hue=hue, ax=ax, **kwargs)
        # ax.legend(frameon=False, loc='lower right')
        ax.set_xlim(df_sync["Time"].min(), df_sync["Time"].max())
        ax.set_title(title)
        ax.set_xlabel("Time (sec)")
        ax.set_ylabel("Amplitude (uV)")
        return ax
//  [python]
def plot_curve(df, title='none'):
    #     plt.figure()
    dfm = df.melt('epoch', var_name='setting', value_name='accuracy')
    snsplot = sns.tsplot(data=dfm, time='epoch', value='accuracy', unit='setting')
    snsplot.set(xlim=[0, 30], ylim=[0.0, 1.05], title=title)
    snsplot.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
//  [python]
def plot_curve(df, title='none'):
    #     plt.figure()
    dfm = df.melt('epoch', var_name='setting', value_name='accuracy')
    snsplot = sns.lineplot(data=dfm, x='epoch', y='accuracy', hue='setting')
    snsplot.set(xlim=[0, 30], ylim=[0.0, 1.05], title=title)
    snsplot.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
//  [python]
def plot_progression(df, title, file_prefix, baseline, palette_name):
  df = df.copy()

  df, did_normalize = do_normalize(df, baseline)

  hueCol = 'flags'
  numHues = len(df[hueCol].unique())

  if palette_name == "cubehelix":
    palette = sns.cubehelix_palette(numHues)
  else:
    palette = sns.color_palette(palette_name, numHues)

  # NOTE: for individual lines per trial, you can use:
  #   units='trial', estimator=None, lw=1,
  g = sns.tsplot(x='iter', y='time', hue=hueCol, data=df, legend='brief',
                   palette=palette)

  ylab = "Speedup" if did_normalize else "Time"
  g.set(ylabel=ylab, xlabel="Tuning Iterations")

  # Y AXIS LIMITS
  buffer = 0.1
  yMin = round(min(float(min(df['time'])), 0.5), 2) - buffer
  yMax = round(max(float(max(df['time'])), 1.5), 2) + buffer
  plt.ylim(yMin, yMax)
  g.yaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator(2))


  xMin = min(df['iter'])
  xMax = max(df['iter'])
  plt.xlim(xMin-1, xMax+1)
  g.xaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator(2))

  # https://stackoverflow.com/questions/51579215/remove-seaborn-lineplot-legend-title?rq=1
  handles, labels = g.get_legend_handles_labels()

  # https://matplotlib.org/3.1.1/tutorials/intermediate/legend_guide.html
  # https://stackoverflow.com/questions/30490740/move-legend-outside-figure-in-seaborn-tsplot
  lgd = plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left',
           ncol=4, mode="expand", borderaxespad=0., title='',
           handles=handles[1:], labels=labels[1:], prop={'size': 14})

  # https://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box
  export_fig(g, file_prefix, None, [lgd])
//  [python]
def plot_progression(df, title, file_prefix, baseline, palette_name):
  df = df.copy()

  df, did_normalize = do_normalize(df, baseline)

  hueCol = 'flags'
  numHues = len(df[hueCol].unique())

  if palette_name == "cubehelix":
    palette = sns.cubehelix_palette(numHues)
  else:
    palette = sns.color_palette(palette_name, numHues)

  # NOTE: for individual lines per trial, you can use:
  #   units='trial', estimator=None, lw=1,
  g = sns.lineplot(x='iter', y='time', hue=hueCol, data=df, legend='brief',
                   palette=palette)

  ylab = "Speedup" if did_normalize else "Time"
  g.set(ylabel=ylab, xlabel="Tuning Iterations")

  # Y AXIS LIMITS
  buffer = 0.1
  yMin = round(min(float(min(df['time'])), 0.5), 2) - buffer
  yMax = round(max(float(max(df['time'])), 1.5), 2) + buffer
  plt.ylim(yMin, yMax)
  g.yaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator(2))


  xMin = min(df['iter'])
  xMax = max(df['iter'])
  plt.xlim(xMin-1, xMax+1)
  g.xaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator(2))

  # https://stackoverflow.com/questions/51579215/remove-seaborn-lineplot-legend-title?rq=1
  handles, labels = g.get_legend_handles_labels()

  # https://matplotlib.org/3.1.1/tutorials/intermediate/legend_guide.html
  # https://stackoverflow.com/questions/30490740/move-legend-outside-figure-in-seaborn-tsplot
  lgd = plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left',
           ncol=4, mode="expand", borderaxespad=0., title='',
           handles=handles[1:], labels=labels[1:], prop={'size': 14})

  # https://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box
  export_fig(g, file_prefix, None, [lgd])
//  [python]
def line(
        x,
        y,
        xlabel,
        ylabel,
        fig_size=(5, 4),
        est=np.mean,
        groups=None,
        group_color=None,
        path=None,
    ):
        """
        Plot bar

        Parameters
        ----------
        x         : array
                    list of x-axis values
        y         : array
                    list of y-axis values
        x_label   : str
                    label x
        y_label   : array
                    label y
        fig_size  : tuple
                    figure size
        est       : np
                    numpy function for aggregating the bars
        groups    : array
                    group list
        group_color : dict
                    group colors
        path      : str
                    path where the graphics will be saved

        Returns
        -------
        None
        """
        f, ax = plt.subplots(1, figsize=fig_size)
        sns.pointplot(x, y, hue=groups, estimator=est, color=group_color, ax=ax)
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        f.tight_layout()
        if path != None:
            f.savefig(path + "/linha_" + xlabel + "_" + ylabel + ".png")
//  [python]
def line(
        x,
        y,
        xlabel,
        ylabel,
        fig_size=(5, 4),
        est=np.mean,
        groups=None,
        group_color=None,
        path=None,
    ):
        """
        Plot bar

        Parameters
        ----------
        x         : array
                    list of x-axis values
        y         : array
                    list of y-axis values
        x_label   : str
                    label x
        y_label   : array
                    label y
        fig_size  : tuple
                    figure size
        est       : np
                    numpy function for aggregating the bars
        groups    : array
                    group list
        group_color : dict
                    group colors
        path      : str
                    path where the graphics will be saved

        Returns
        -------
        None
        """
        f, ax = plt.subplots(1, figsize=fig_size)
        sns.lineplot(x, y, hue=groups, estimator=est, color=group_color, ax=ax)
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        f.tight_layout()
        if path != None:
            f.savefig(path + "/linha_" + xlabel + "_" + ylabel + ".png")
//  [python]
def plot_average(
        self,
        center="Peak",
        time_before=0.4,
        time_after=0.4,
        filt=(None, None),
        mask=None,
        figsize=(6, 4.5),
        **kwargs,
    ):
        """
        Plot the average REM.

        Parameters
        ----------
        center : str
            Landmark of the event to synchronize the timing on.
            Default is to use the peak of the REM.
        time_before : float
            Time (in seconds) before ``center``.
        time_after : float
            Time (in seconds) after ``center``.
        filt : tuple
            Optional filtering to apply to data. For instance, ``filt=(1, 30)``
            will apply a 1 to 30 Hz bandpass filter, and ``filt=(None, 40)``
            will apply a 40 Hz lowpass filter. Filtering is done using default
            parameters in the :py:func:`mne.filter.filter_data` function.
        mask : array_like or None
            Custom boolean mask. Only the detected events for which mask is True will be
            included. Default is None, i.e. no masking (all events are included).
        figsize : tuple
            Figure size in inches.
        **kwargs : dict
            Optional argument that are passed to :py:func:`seaborn.lineplot`.
        """
        import seaborn as sns
        import matplotlib.pyplot as plt

        df_sync = self.get_sync_events(
            center=center, time_before=time_before, time_after=time_after, filt=filt, mask=mask
        )

        # Start figure
        fig, ax = plt.subplots(1, 1, figsize=figsize)
        sns.tsplot(data=df_sync, time="Time", value="Amplitude", unit="Channel", ax=ax, **kwargs)
        # ax.legend(frameon=False, loc='lower right')
        ax.set_xlim(df_sync["Time"].min(), df_sync["Time"].max())
        ax.set_title("Average REM")
        ax.set_xlabel("Time (sec)")
        ax.set_ylabel("Amplitude (uV)")
        return ax
//  [python]
def plot_average(
        self,
        center="Peak",
        time_before=0.4,
        time_after=0.4,
        filt=(None, None),
        mask=None,
        figsize=(6, 4.5),
        **kwargs,
    ):
        """
        Plot the average REM.

        Parameters
        ----------
        center : str
            Landmark of the event to synchronize the timing on.
            Default is to use the peak of the REM.
        time_before : float
            Time (in seconds) before ``center``.
        time_after : float
            Time (in seconds) after ``center``.
        filt : tuple
            Optional filtering to apply to data. For instance, ``filt=(1, 30)``
            will apply a 1 to 30 Hz bandpass filter, and ``filt=(None, 40)``
            will apply a 40 Hz lowpass filter. Filtering is done using default
            parameters in the :py:func:`mne.filter.filter_data` function.
        mask : array_like or None
            Custom boolean mask. Only the detected events for which mask is True will be
            included. Default is None, i.e. no masking (all events are included).
        figsize : tuple
            Figure size in inches.
        **kwargs : dict
            Optional argument that are passed to :py:func:`seaborn.lineplot`.
        """
        import seaborn as sns
        import matplotlib.pyplot as plt

        df_sync = self.get_sync_events(
            center=center, time_before=time_before, time_after=time_after, filt=filt, mask=mask
        )

        # Start figure
        fig, ax = plt.subplots(1, 1, figsize=figsize)
        sns.lineplot(data=df_sync, x="Time", y="Amplitude", hue="Channel", ax=ax, **kwargs)
        # ax.legend(frameon=False, loc='lower right')
        ax.set_xlim(df_sync["Time"].min(), df_sync["Time"].max())
        ax.set_title("Average REM")
        ax.set_xlabel("Time (sec)")
        ax.set_ylabel("Amplitude (uV)")
        return ax
//  [python]
def make_azim_vs_folded_rms_plot_all_algorithms(df_rms):
    sns.pointplot(x="Azimuth (Degrees)", y="Folded RMS (Degrees)", join=True, ci=68, errwidth=1,capsize=.2,hue="DOA Algorithm", data=df_rms)
//  [python]
def make_azim_vs_folded_rms_plot_all_algorithms(df_rms):
    sns.lineplot(x="Azimuth (Degrees)", y="Folded RMS (Degrees)", ci=68,err_style='bars',hue="DOA Algorithm", data=df_rms)
//  [python]
def make_azim_vs_folded_rms_plot(df_rms):
    plt.clf()
    if 'azim' in df_rms:
        df_rms['azim'] = df_rms['azim'].map(lambda x: x*5)
        df_rms['rms_folded'] = df_rms['rms_folded'].map(lambda x: x*5)
        df_rms.columns = ['Azimuth (Degrees)', 'DOA Algorithm', 
                          'Folded RMS (Degrees)', 'RMS (Degrees)',
                          'Architecture Index']
    sns.pointplot(x="Azimuth (Degrees)", y="Folded RMS (Degrees)", hue="DOA Algorithm", data=df_rms)
//  [python]
def make_azim_vs_folded_rms_plot(df_rms):
    plt.clf()
    if 'azim' in df_rms:
        df_rms['azim'] = df_rms['azim'].map(lambda x: x*5)
        df_rms['rms_folded'] = df_rms['rms_folded'].map(lambda x: x*5)
        df_rms.columns = ['Azimuth (Degrees)', 'DOA Algorithm', 
                          'Folded RMS (Degrees)', 'RMS (Degrees)',
                          'Architecture Index']
    sns.lineplot(x="Azimuth (Degrees)", y="Folded RMS (Degrees)", hue="DOA Algorithm", style="DOA Algorithm", data=df_rms)
//  [python]
def learning_curves(history):
    """ Plots the training and validation error as a function of the number of epochs """

    plt.figure()
    for key in history.history.keys():
        label = f'{key}\nfinal={(history.history[key][-1]):.2f}'
        ax = plt.plot(range(len(history.history[key])), history.history[key], label=label)
    ax.set_xlabel('epochs')
    save_plot(plt.gcf(), 'learning_curves')
//  [python]
def learning_curves(history):
    """ Plots the training and validation error as a function of the number of epochs """

    plt.figure()
    for key in history.history.keys():
        label = f'{key}\nfinal={(history.history[key][-1]):.2f}'
        ax = sns.lineplot(x=range(len(history.history[key])), y=history.history[key], label=label)
    ax.set_xlabel('epochs')
    save_plot(plt.gcf(), 'learning_curves')
//  [python]
def predictions_vs_time(time, y_true, y_predict, title):
    """ Plots true (and predicted if provided) production data as function of time """

    plt.figure(figsize=(15, 6))
    plt.plot(time, y_predict, label='y_predict')
    if y_true is not None:
        plt.plot(time, y_true, label='y_true')
    plt.title(title)
    save_plot(plt.gcf(), title)
//  [python]
def predictions_vs_time(time, y_true, y_predict, title):
    """ Plots true (and predicted if provided) production data as function of time """

    plt.figure(figsize=(15, 6))
    sns.lineplot(x=time, y=y_predict, label='y_predict')
    if y_true is not None:
        sns.lineplot(x=time, y=y_true, label='y_true')
    plt.title(title)
    save_plot(plt.gcf(), title)
//  [python]
def get_algorithm_error(pd_data):
    #load data and set algorithms, conacat arrays, use this function
    pd_data = pd_data.convert_objects(convert_numeric=True)
    error = lambda x: min(72-(abs(x['azim']-x['predicted_folded'])),abs(x['azim']-x['predicted_folded']))
    if pd_data['azim'].dtype != 'int64' and pd_data['azim'].dtype != 'float64':
        pd_data['azim'] = pd_data['azim'].apply(convert_from_numpy)
        pd_data['elev'] = pd_data['elev'].apply(convert_from_numpy)
    if 'DOA Algorithm' not in pd_data.columns:
        pd_data['squared_error'] = (5*pd_data.apply(error,axis=1))**2
        pd_data['azim_folded'] = pd_data['azim'].apply(CIPIC_azim_folding)
        pd_data.rename(columns={'squared_error':'RMS Error (Degrees)',
                        'azim_folded':'Azimuth (Degrees)','algorithm':'DOA Algorithm'},
                       inplace=True)
    plt.clf()
    plt.figure()
    sns.pointplot(x='Azimuth (Degrees)',y='RMS Error (Degrees)',
                 hue='DOA Algorithm',data=pd_data)
    plt.xticks([-75,-50,-25,0,25,50,75])
    plt.tight_layout()
    return pd_data
//  [python]
def get_algorithm_error(pd_data):
    #load data and set algorithms, conacat arrays, use this function
    pd_data = pd_data.convert_objects(convert_numeric=True)
    error = lambda x: min(72-(abs(x['azim']-x['predicted_folded'])),abs(x['azim']-x['predicted_folded']))
    if pd_data['azim'].dtype != 'int64' and pd_data['azim'].dtype != 'float64':
        pd_data['azim'] = pd_data['azim'].apply(convert_from_numpy)
        pd_data['elev'] = pd_data['elev'].apply(convert_from_numpy)
    if 'DOA Algorithm' not in pd_data.columns:
        pd_data['squared_error'] = (5*pd_data.apply(error,axis=1))**2
        pd_data['azim_folded'] = pd_data['azim'].apply(CIPIC_azim_folding)
        pd_data.rename(columns={'squared_error':'RMS Error (Degrees)',
                        'azim_folded':'Azimuth (Degrees)','algorithm':'DOA Algorithm'},
                       inplace=True)
    plt.clf()
    plt.figure()
    sns.lineplot(x='Azimuth (Degrees)',y='RMS Error (Degrees)',
                 hue='DOA Algorithm',estimator=lambda x:math.sqrt(np.mean(x)), 
                 ci=68,err_style='bars',legend=False,data=pd_data)
    plt.xticks([-75,-50,-25,0,25,50,75])
    plt.tight_layout()
    return pd_data
//  [python]
def run_experiment(G, intervened_node, delta, n_timesteps: int,est = np.median,
        conf = 68,dampen = False):
    G.create_bmi_config_file()
    s0 = pd.read_csv(
        "bmi_config.txt", index_col=0, header=None, error_bad_lines=False
    )[1]
    s0.loc[f"∂({intervened_node})/∂t"] = delta
    s0.to_csv("bmi_config.txt", index_label="variable")
    G.initialize()
    indicator_values = {
        n[0]: {
            "name": list(n[1]["indicators"].keys())[0],
            "xs": [0 for _ in range(G.res)],
            "ys": list(list(n[1]["indicators"].values())[0].samples),
        }
        for n in G.nodes(data=True)
    }

    for t in range(1, n_timesteps + 1):
        G.update(dampen=dampen)
        for n in G.nodes(data=True):
            # TODO Fix this - make it more general (or justify it theoretically)
            ys = lmap(
                lambda x: x,
                list(n[1]["indicators"].values())[0].samples,
            )
            indicator_values[n[0]]["ys"].extend(ys)
            indicator_values[n[0]]["xs"].extend([t for _ in ys])

    for n in G.nodes(data=True):
        fig, ax = plt.subplots()
        sns.regplot(
            indicator_values[n[0]]["xs"], indicator_values[n[0]]["ys"], ax=ax,
            ci=conf,
            estimator=est
        )
        ax.set_title(f"{indicator_values[n[0]]['name']} ({list(n[1]['indicators'].values())[0].unit})")
        ax.set_xlabel("time step number")
//  [python]
def run_experiment(G, intervened_node, delta, n_timesteps: int,est = np.median,
        conf = 68,dampen = False):
    G.create_bmi_config_file()
    s0 = pd.read_csv(
        "bmi_config.txt", index_col=0, header=None, error_bad_lines=False
    )[1]
    s0.loc[f"∂({intervened_node})/∂t"] = delta
    s0.to_csv("bmi_config.txt", index_label="variable")
    G.initialize()
    indicator_values = {
        n[0]: {
            "name": list(n[1]["indicators"].keys())[0],
            "xs": [0 for _ in range(G.res)],
            "ys": list(list(n[1]["indicators"].values())[0].samples),
        }
        for n in G.nodes(data=True)
    }

    for t in range(1, n_timesteps + 1):
        G.update(dampen=dampen)
        for n in G.nodes(data=True):
            # TODO Fix this - make it more general (or justify it theoretically)
            ys = lmap(
                lambda x: x,
                list(n[1]["indicators"].values())[0].samples,
            )
            indicator_values[n[0]]["ys"].extend(ys)
            indicator_values[n[0]]["xs"].extend([t for _ in ys])

    for n in G.nodes(data=True):
        fig, ax = plt.subplots()
        sns.lineplot(
            indicator_values[n[0]]["xs"], indicator_values[n[0]]["ys"], ax=ax,
            ci=conf,
            estimator=est
        )
        ax.set_title(f"{indicator_values[n[0]]['name']} ({list(n[1]['indicators'].values())[0].unit})")
        ax.set_xlabel("time step number")
//  [python]
def plot(self):
        """Plot the doublet score ROC curve and the doublet score histogram."""
        fig, (ax_roc, ax_hist) = plt.subplots(figsize=(6, 3), dpi=300, ncols=2)

        ax = ax_roc
        ld_obs = self.doublet_scores_obs_
        ld_sim = self.doublet_scores_sim_
        y_score = np.concatenate([ld_obs, ld_sim])
        y_true = np.concatenate([np.ones_like(ld_obs), np.zeros_like(ld_sim)])
        # tpr, fpr is reversed, because smaller score is better
        tpr, fpr, thresholds = roc_curve(y_true, y_score)
        plt.plot(fpr, tpr)
        ax.scatter(
            [self.detected_doublet_rate_],
            [self.detectable_doublet_fraction_],
            color="red",
            zorder=99,
        )
        ax.text(
            0.5,
            0.1,
            f"Obs doub: {100 * self.detected_doublet_rate_:.1f}%\n"
            f"Sim doub: {100 * self.detectable_doublet_fraction_:.1f}%",
            transform=ax.transAxes,
        )
        ax.set(xlabel="TPR", ylabel="FPR", title="ROC")

        ax = ax_hist
        sns.histplot(self.doublet_scores_obs_, color="steelblue", ax=ax, label="Observed")
        sns.histplot(self.doublet_scores_sim_, color="gray", ax=ax, label="Simulated")
        ax.set(yticks=[], ylabel="", xlabel="Doublet Score")
        ax.legend()
        ymin, ymax = ax.get_ylim()
        ax.vlines(self.threshold_, ymin, ymax, color="red", linestyle="--")
        sns.despine(fig=fig)

        fig.tight_layout()
        return
//  [python]
def plot(self):
        """Plot the doublet score ROC curve and the doublet score histogram."""
        fig, (ax_roc, ax_hist) = plt.subplots(figsize=(6, 3), dpi=300, ncols=2)

        ax = ax_roc
        ld_obs = self.doublet_scores_obs_
        ld_sim = self.doublet_scores_sim_
        y_score = np.concatenate([ld_obs, ld_sim])
        y_true = np.concatenate([np.ones_like(ld_obs), np.zeros_like(ld_sim)])
        # tpr, fpr is reversed, because smaller score is better
        tpr, fpr, thresholds = roc_curve(y_true, y_score)
        sns.lineplot(ax=ax, x=fpr, y=tpr)
        ax.scatter(
            [self.detected_doublet_rate_],
            [self.detectable_doublet_fraction_],
            color="red",
            zorder=99,
        )
        ax.text(
            0.5,
            0.1,
            f"Obs doub: {100 * self.detected_doublet_rate_:.1f}%\n"
            f"Sim doub: {100 * self.detectable_doublet_fraction_:.1f}%",
            transform=ax.transAxes,
        )
        ax.set(xlabel="TPR", ylabel="FPR", title="ROC")

        ax = ax_hist
        sns.histplot(self.doublet_scores_obs_, color="steelblue", ax=ax, label="Observed")
        sns.histplot(self.doublet_scores_sim_, color="gray", ax=ax, label="Simulated")
        ax.set(yticks=[], ylabel="", xlabel="Doublet Score")
        ax.legend()
        ymin, ymax = ax.get_ylim()
        ax.vlines(self.threshold_, ymin, ymax, color="red", linestyle="--")
        sns.despine(fig=fig)

        fig.tight_layout()
        return
//  [python]
def add_common_markers(df: pd.DataFrame, ax: plt.Axes, price_line: bool = True) -> None:
    """
    This function adds common markers to a plot.

    Args:
        df: The DataFrame containing the data to be plotted.
        ax: The Axes object to be plotted on.
        price_line: If True, a line plot of the 'PriceLogInterp' column will be added to the Axes. Default is True.

    Returns:
        None
    """
    if price_line:
        ax.plot(df['Date'], df['PriceLogInterp'], alpha=0.4, color='orange')

    for _, row in df[df['Halving'] == 1].iterrows():
        days_since_epoch = (row['Date'] - datetime(1970, 1, 1)).days
        ax.axvline(x=days_since_epoch, color='navy', linestyle=':')

    for _, row in df[df['PriceHigh'] == 1].iterrows():
        days_since_epoch = (row['Date'] - datetime(1970, 1, 1)).days
        ax.axvline(x=days_since_epoch, color='green', linestyle=':')

    for _, row in df[df['PriceLow'] == 1].iterrows():
        days_since_epoch = (row['Date'] - datetime(1970, 1, 1)).days
        ax.axvline(x=days_since_epoch, color='red', linestyle=':')
//  [python]
def add_common_markers(df: pd.DataFrame, ax: plt.Axes, price_line: bool = True) -> None:
    """
    This function adds common markers to a plot.

    Args:
        df: The DataFrame containing the data to be plotted.
        ax: The Axes object to be plotted on.
        price_line: If True, a line plot of the 'PriceLogInterp' column will be added to the Axes. Default is True.

    Returns:
        None
    """
    if price_line:
        sns.lineplot(data=df, x='Date', y='PriceLogInterp', alpha=0.4, color='orange', ax=ax)

    for _, row in df[df['Halving'] == 1].iterrows():
        days_since_epoch = (row['Date'] - datetime(1970, 1, 1)).days
        ax.axvline(x=days_since_epoch, color='navy', linestyle=':')

    for _, row in df[df['PriceHigh'] == 1].iterrows():
        days_since_epoch = (row['Date'] - datetime(1970, 1, 1)).days
        ax.axvline(x=days_since_epoch, color='green', linestyle=':')

    for _, row in df[df['PriceLow'] == 1].iterrows():
        days_since_epoch = (row['Date'] - datetime(1970, 1, 1)).days
        ax.axvline(x=days_since_epoch, color='red', linestyle=':')
//  [python]
def metrics(args):
    exps = expman.gather(args.run).filter(args.filter)
    mask_metrics = exps.collect('test_pred/mask_metrics.csv')
    sns.tsplot(data=mask_metrics, time='thr', value='dice', unit='run', condition='conv_type')
    plt.savefig(args.output)
//  [python]
def metrics(args):
    exps = expman.gather(args.run).filter(args.filter)
    mask_metrics = exps.collect('test_pred/mask_metrics.csv')
    sns.lineplot(data=mask_metrics, x='thr', y='dice', hue='conv_type', size='grow_factor', style='num_filters')
    plt.savefig(args.output)
//  [python]
def coverage_growth_plot(self,
                             benchmark_df,
                             axes=None,
                             logscale=False,
                             bugs=False):
        """Draws edge (or bug) coverage growth plot on given |axes|.

        The fuzzer labels will be in the order of their mean coverage at the
        snapshot time (typically, the end of experiment).
        """
        self._common_datafame_checks(benchmark_df)

        column_of_interest = 'bugs_covered' if bugs else 'edges_covered'

        benchmark_snapshot_df = data_utils.get_benchmark_snapshot(benchmark_df)
        snapshot_time = benchmark_snapshot_df.time.unique()[0]
        fuzzer_order = data_utils.benchmark_rank_by_mean(
            benchmark_snapshot_df, key=column_of_interest).index

        axes = sns.tsplot(
            y=column_of_interest,
            x='time',
            unit='fuzzer',
            condition='fuzzer',
            order=fuzzer_order,
            data=benchmark_df[benchmark_df.time <= snapshot_time],
            ci=None if bugs or self._quick else 95,
            err_style="band",
            estimator=np.median,
            color=self._fuzzer_colors,
            ax=axes)

        axes.set_title(_formatted_title(benchmark_snapshot_df))

        # Indicate the snapshot time with a big red vertical line.
        axes.axvline(x=snapshot_time, color='r')

        # Move legend outside of the plot.
        axes.legend(bbox_to_anchor=(1.00, 1),
                    borderaxespad=0,
                    loc='upper left',
                    frameon=False)

        axes.set(ylabel='Bug coverage' if bugs else 'Code branch coverage')
        axes.set(xlabel='Time (hour:minute)')

        if self._logscale or logscale:
            axes.set_xscale('symlog')
            ticks = np.logspace(
                # Start from the time of the first measurement.
                0.0,
                np.log10(snapshot_time + 1),  # Include tick at end time.
                _DEFAULT_TICKS_COUNT - 1)
            ticks = np.insert(ticks, 0, 0)
            axes.set_xticks([], minor=True)
        else:
            ticks = np.arange(
                0.0,
                snapshot_time + 1,  # Include tick at end time.
                max(snapshot_time / _DEFAULT_TICKS_COUNT, 1))

        axes.set_xticks(ticks)
        axes.set_xticklabels([_formatted_hour_min(t) for t in ticks])

        plt.xlim(0)
        sns.despine(ax=axes, trim=True)
//  [python]
def coverage_growth_plot(self,
                             benchmark_df,
                             axes=None,
                             logscale=False,
                             bugs=False):
        """Draws edge (or bug) coverage growth plot on given |axes|.

        The fuzzer labels will be in the order of their mean coverage at the
        snapshot time (typically, the end of experiment).
        """
        self._common_datafame_checks(benchmark_df)

        column_of_interest = 'bugs_covered' if bugs else 'edges_covered'

        benchmark_snapshot_df = data_utils.get_benchmark_snapshot(benchmark_df)
        snapshot_time = benchmark_snapshot_df.time.unique()[0]
        fuzzer_order = data_utils.benchmark_rank_by_mean(
            benchmark_snapshot_df, key=column_of_interest).index

        axes = sns.lineplot(
            y=column_of_interest,
            x='time',
            hue='fuzzer',
            hue_order=fuzzer_order,
            data=benchmark_df[benchmark_df.time <= snapshot_time],
            ci=None if bugs or self._quick else 95,
            estimator=np.median,
            palette=self._fuzzer_colors,
            style='fuzzer',
            dashes=False,
            markers=self._fuzzer_markers,
            ax=axes)

        axes.set_title(_formatted_title(benchmark_snapshot_df))

        # Indicate the snapshot time with a big red vertical line.
        axes.axvline(x=snapshot_time, color='r')

        # Move legend outside of the plot.
        axes.legend(bbox_to_anchor=(1.00, 1),
                    borderaxespad=0,
                    loc='upper left',
                    frameon=False)

        axes.set(ylabel='Bug coverage' if bugs else 'Code branch coverage')
        axes.set(xlabel='Time (hour:minute)')

        if self._logscale or logscale:
            axes.set_xscale('symlog')
            ticks = np.logspace(
                # Start from the time of the first measurement.
                0.0,
                np.log10(snapshot_time + 1),  # Include tick at end time.
                _DEFAULT_TICKS_COUNT - 1)
            ticks = np.insert(ticks, 0, 0)
            axes.set_xticks([], minor=True)
        else:
            ticks = np.arange(
                0.0,
                snapshot_time + 1,  # Include tick at end time.
                max(snapshot_time / _DEFAULT_TICKS_COUNT, 1))

        axes.set_xticks(ticks)
        axes.set_xticklabels([_formatted_hour_min(t) for t in ticks])

        plt.xlim(0)
        sns.despine(ax=axes, trim=True)
//  [python]
def main(statsfile, bs, outfile=None):
    df = parse_stats_file(statsfile)
    plt.figure(figsize=(16, 10))
    seaborn.tsplot(time="step", value="t", unit="kind", data=df).set(
        title=f"Time per element by batcher, batch_size={bs}", ylabel="µs"
    )

    plt.legend(
        loc="upper center",
        bbox_to_anchor=(0.5, -0.05),
        fancybox=True,
        shadow=True,
        ncol=3,
    )

    if outfile:
        plt.savefig(outfile)

    else:
        plt.show()
//  [python]
def main(statsfile, bs, outfile=None):
    df = parse_stats_file(statsfile)
    plt.figure(figsize=(16, 10))
    seaborn.lineplot(x="step", y="t", hue="kind", data=df).set(
        title=f"Time per element by batcher, batch_size={bs}", ylabel="µs"
    )

    plt.legend(
        loc="upper center",
        bbox_to_anchor=(0.5, -0.05),
        fancybox=True,
        shadow=True,
        ncol=3,
    )

    if outfile:
        plt.savefig(outfile)

    else:
        plt.show()
//  [python]
def roc_plot(actual, model_probs, model_names, figsize=(10,10)):
    """
    Receiver Operating Characteristic Plot. Can plot multiple models.
    ----------
    actual: array of dataframe of true classes assignements.
        example:
        [1,1,0,1,0]
    model_probs: list of arrays
        a list containing classification probabilites for each model in order.
        example:
        model_probs = [class_probs_1, class_probs_2]
    model_names: list of strings
        a list containing names for each model in order.
        example:
        model_names = ["GBT", "Logistic Regression"]
    figsize: size of figure
    Returns:
    -------
        Receiver Operating Characteristic Plot with AUC in the legend.
    """
    model_names = make_listy(model_names)
    if not is_listy(model_probs): model_probs = [model_probs]

    if len(model_names) > 5:
        return ValueError("Can only compare 5 models or less.")

    colors = ["#ED2BFF", "#14E2C0", "#FF9F1C", "#5E2BFF","#FC5FA3"]

    for m in range(len(model_names)):
        fpr, tpr, _ = roc_curve(actual, model_probs[m])
        roc_auc = auc(fpr, tpr)
        ax = plt.plot(fpr,
                          tpr,
                          lw=2,
                          color=colors[m],
                          label = model_names[m] + ' AUC = %0.4f' % roc_auc)
    
    ax = plt.plot([0, 1], [0, 1], 'r--')
    ax.set_title('Receiver Operating Characteristic Plot')
    ax.set_ylabel('True Positive Rate')
    ax.set_xlabel('False Positive Rate')
    
    plt.show()
//  [python]
def roc_plot(actual, model_probs, model_names, figsize=(10,10)):
    """
    Receiver Operating Characteristic Plot. Can plot multiple models.
    ----------
    actual: array of dataframe of true classes assignements.
        example:
        [1,1,0,1,0]
    model_probs: list of arrays
        a list containing classification probabilites for each model in order.
        example:
        model_probs = [class_probs_1, class_probs_2]
    model_names: list of strings
        a list containing names for each model in order.
        example:
        model_names = ["GBT", "Logistic Regression"]
    figsize: size of figure
    Returns:
    -------
        Receiver Operating Characteristic Plot with AUC in the legend.
    """
    model_names = make_listy(model_names)
    if not is_listy(model_probs): model_probs = [model_probs]

    if len(model_names) > 5:
        return ValueError("Can only compare 5 models or less.")

    colors = ["#ED2BFF", "#14E2C0", "#FF9F1C", "#5E2BFF","#FC5FA3"]

    for m in range(len(model_names)):
        fpr, tpr, _ = roc_curve(actual, model_probs[m])
        roc_auc = auc(fpr, tpr)
        ax = sns.lineplot(x=fpr,
                          y=tpr,
                          lw=2,
                          color=colors[m],
                          label = model_names[m] + ' AUC = %0.4f' % roc_auc)
    
    ax.plot([0, 1], [0, 1], 'r--')
    ax.set_title('Receiver Operating Characteristic Plot')
    ax.set_ylabel('True Positive Rate')
    ax.set_xlabel('False Positive Rate')
    
    plt.show()
//  [python]
def plotMonthlyActivity(df_activity, timeBin, what="users", ax=None, log_y=False, **kwargs):
    '''Basic function to plot the monthly activity of areas or total region.

    Parameters
    ----------
    df_activity : dask.DataFrame
        A dataframe as returned from :attr:`mobilkit.temporal.computeTimeBinActivity`.
    timeBin : str
        The width of the time bin used in :attr:`mobilkit.temporal.computeTimeBinActivity`.
    what : str, optional
        The quantity to plot. Must be one amongst ``'users', 'pings', 'pings_per_user'``.
    ax : axis, optional
        The axis to use. If ``None`` will create a new figure.
    log_y : bool, optional
        Whether or not to plot with y log scale. Default ``False``.
    **kwargs
        Will be passed to ``seaborn.lineplot`` function.

    Returns
    -------
    df : pandas.DataFrame
            Thee aggregated data plotted.
    ax : axis
        The axis of the figure.
    '''

    if ax is None:
        fig, ax = plt.subplots(1,1,figsize=(18,6))


    df = copy(df_activity)
    df["month"] = df[timeBin].dt.round("MS")
    df["month_hour"] = df[timeBin].dt.dayofmonth()*24 + df["timeBin"].dt.hour
    df = df.compute()
#     df = df_activity.withColumn("month", sqlF.date_trunc("month", sqlF.col(timeBin)))\
#                     .withColumn("month_hour", sqlF.dayofmonth(timeBin)*24 + sqlF.hour(timeBin))

    sns.pointplot("month_hour", what, hue="month", data=df, ax=ax, **kwargs)
    if log_y: ax.set_yscale("log")
    hours_per_day = 24
    locs = np.arange(hours_per_day,hours_per_day*32,hours_per_day)
    plt.xticks(locs, ["%d"%(l//hours_per_day) for l in locs], size=14)
    plt.yticks(size=14)

    plt.xlabel("Day", size=16)
    plt.ylabel("Users" if what == "users"
            else "Pings" if what == "pings"
            else "Pings per user", size=16)

    plt.legend(fontsize=14, bbox_to_anchor=(1.01,.5), loc="center left")
    plt.tight_layout()

    return df, ax
//  [python]
def plotMonthlyActivity(df_activity, timeBin, what="users", ax=None, log_y=False, **kwargs):
    '''Basic function to plot the monthly activity of areas or total region.

    Parameters
    ----------
    df_activity : dask.DataFrame
        A dataframe as returned from :attr:`mobilkit.temporal.computeTimeBinActivity`.
    timeBin : str
        The width of the time bin used in :attr:`mobilkit.temporal.computeTimeBinActivity`.
    what : str, optional
        The quantity to plot. Must be one amongst ``'users', 'pings', 'pings_per_user'``.
    ax : axis, optional
        The axis to use. If ``None`` will create a new figure.
    log_y : bool, optional
        Whether or not to plot with y log scale. Default ``False``.
    **kwargs
        Will be passed to ``seaborn.lineplot`` function.

    Returns
    -------
    df : pandas.DataFrame
            Thee aggregated data plotted.
    ax : axis
        The axis of the figure.
    '''

    if ax is None:
        fig, ax = plt.subplots(1,1,figsize=(18,6))


    df = copy(df_activity)
    df["month"] = df[timeBin].dt.round("MS")
    df["month_hour"] = df[timeBin].dt.dayofmonth()*24 + df["timeBin"].dt.hour
    df = df.compute()
#     df = df_activity.withColumn("month", sqlF.date_trunc("month", sqlF.col(timeBin)))\
#                     .withColumn("month_hour", sqlF.dayofmonth(timeBin)*24 + sqlF.hour(timeBin))

    sns.lineplot("month_hour", what, hue="month", data=df, ax=ax, **kwargs)
    if log_y: ax.set_yscale("log")
    hours_per_day = 24
    locs = np.arange(hours_per_day,hours_per_day*32,hours_per_day)
    plt.xticks(locs, ["%d"%(l//hours_per_day) for l in locs], size=14)
    plt.yticks(size=14)

    plt.xlabel("Day", size=16)
    plt.ylabel("Users" if what == "users"
            else "Pings" if what == "pings"
            else "Pings per user", size=16)

    plt.legend(fontsize=14, bbox_to_anchor=(1.01,.5), loc="center left")
    plt.tight_layout()

    return df, ax
//  [python]
def long_tail_plot(df, item_id_column, interaction_type, percentage=None, x_labels=True):
    """
    Plots the long tail for a user-item interaction dataset.
    ----------
    df: pandas dataframe
        user-item interaction dataframe
        example:
    item_id_column: str
        column name identifying the item ids in the dataframe
    interaction_type: str
        type of user-item interactions
        i.e. 'purchases', 'ratings' 'interactions', or 'clicks'
    percentage: float, default=None
        percent of volume to consider as the head (percent as a decimal)
        (if default=None no line will be plotted)
    x_tables bool, default=True
        if True, plot x-axis tick labels
        if False, no x-axis tick lavels will be plotted.
    Returns:
    -------
        A long tail plot
    """
    #calculate cumulative volumes
    volume_df = df[item_id_column].value_counts().reset_index()
    volume_df.columns = [item_id_column, "volume"]
    volume_df[item_id_column] = volume_df[item_id_column].astype(str)
    volume_df['cumulative_volume'] = volume_df['volume'].cumsum()
    volume_df['percent_of_total_volume'] = volume_df['cumulative_volume']/volume_df['volume'].sum()

    #line plot of cumulative volume
    x=range(0,len(volume_df))
    ax = plt.plot(x, volume_df["volume"], color="black")
    plt.xticks(x)

    #set labels
    ax[0].set_title('Long Tail Plot')
    ax[0].set_ylabel('# of ' + interaction_type)
    ax[0].set_xlabel(item_id_column)

    if percentage != None:
        #plot vertical line at the tail location
        head = volume_df[volume_df["percent_of_total_volume"] <= percentage]
        tail = volume_df[volume_df["percent_of_total_volume"] > percentage]
        items_in_head = len(head)
        items_in_tail = len(tail)
        plt.axvline(x=items_in_head, color="red",  linestyle='--')

        # fill area under plot
        head = head.append(tail.head(1))
        x1 = head.index.values
        y1 = head['volume']
        x2 = tail.index.values
        y2 = tail['volume']
        plt.fill_between(x1, y1, color="blue", alpha=0.2)
        plt.fill_between(x2, y2,  color="orange", alpha=0.2)

        #create legend
        legend_elements = [Line2D([0], [0], marker='o', color='w', label=str(items_in_head)+': items in the head', markerfacecolor='blue', markersize=5),
                           Line2D([0], [0], marker='o', color='w', label=str(items_in_tail)+': items in the tail', markerfacecolor='orange', markersize=5)]
        plt.legend(handles=legend_elements, loc=1)

    else:
        x1 = volume_df[item_id_column]
        y1 = volume_df['volume']
        plt.fill_between(x1, y1, color="blue", alpha=0.3)
    if x_labels == False:
        plt.xticks([], [])
        ax[0].set(xticklabels=[])
    else:
        ax[0].set_xticklabels(labels = volume_df[item_id_column], rotation = 45, ha="right")

    plt.show()
//  [python]
def long_tail_plot(df, item_id_column, interaction_type, percentage=None, x_labels=True):
    """
    Plots the long tail for a user-item interaction dataset.
    ----------
    df: pandas dataframe
        user-item interaction dataframe
        example:
    item_id_column: str
        column name identifying the item ids in the dataframe
    interaction_type: str
        type of user-item interactions
        i.e. 'purchases', 'ratings' 'interactions', or 'clicks'
    percentage: float, default=None
        percent of volume to consider as the head (percent as a decimal)
        (if default=None no line will be plotted)
    x_tables bool, default=True
        if True, plot x-axis tick labels
        if False, no x-axis tick lavels will be plotted.
    Returns:
    -------
        A long tail plot
    """
    #calculate cumulative volumes
    volume_df = df[item_id_column].value_counts().reset_index()
    volume_df.columns = [item_id_column, "volume"]
    volume_df[item_id_column] = volume_df[item_id_column].astype(str)
    volume_df['cumulative_volume'] = volume_df['volume'].cumsum()
    volume_df['percent_of_total_volume'] = volume_df['cumulative_volume']/volume_df['volume'].sum()

    #line plot of cumulative volume
    x=range(0,len(volume_df))
    ax = sns.lineplot(x=x, y="volume", data=volume_df, color="black")
    plt.xticks(x)

    #set labels
    ax.set_title('Long Tail Plot')
    ax.set_ylabel('# of ' + interaction_type)
    ax.set_xlabel(item_id_column)

    if percentage != None:
        #plot vertical line at the tail location
        head = volume_df[volume_df["percent_of_total_volume"] <= percentage]
        tail = volume_df[volume_df["percent_of_total_volume"] > percentage]
        items_in_head = len(head)
        items_in_tail = len(tail)
        plt.axvline(x=items_in_head, color="red",  linestyle='--')

        # fill area under plot
        head = head.append(tail.head(1))
        x1 = head.index.values
        y1 = head['volume']
        x2 = tail.index.values
        y2 = tail['volume']
        ax.fill_between(x1, y1, color="blue", alpha=0.2)
        ax.fill_between(x2, y2,  color="orange", alpha=0.2)

        #create legend
        legend_elements = [Line2D([0], [0], marker='o', color='w', label=str(items_in_head)+': items in the head', markerfacecolor='blue', markersize=5),
                           Line2D([0], [0], marker='o', color='w', label=str(items_in_tail)+': items in the tail', markerfacecolor='orange', markersize=5)]
        ax.legend(handles=legend_elements, loc=1)

    else:
        x1 = volume_df[item_id_column]
        y1 = volume_df['volume']
        ax.fill_between(x1, y1, color="blue", alpha=0.3)
    if x_labels == False:
        plt.xticks([], [])
        ax.set(xticklabels=[])
    else:
        ax.set_xticklabels(labels = volume_df[item_id_column], rotation = 45, ha="right")

    plt.show()
//  [python]
def print_elbo(self, num_iter, init, include = []):

        data = pd.DataFrame(data = {"ELBO": [i.item() for key in self.elbo for i in self.elbo[key][init:num_iter]],
                                    "tree": itertools.chain.from_iterable(zip(*itertools.repeat(range(len(self.potential_trees)), num_iter-init))),
                                    "num_iter": [*range(num_iter-init)]*len(self.potential_trees)})
        # reduce high values
        data.assign(ELBO = [1.5e5 if ELBO > 1.5e5 else ELBO for ELBO in data['ELBO']])
        
        if len(include) > 0:
            sns.tsplot(data = data[data["tree"].isin(include)], time = "num_iter", value = "ELBO", unit = "tree", color = sns.color_palette("tab10", len(include)))
            
        else:
            sns.tsplot(data = data, time = "num_iter", value = "ELBO", unit = "tree", color = sns.color_palette("tab10", len(self.potential_trees)))
//  [python]
def print_elbo(self, num_iter, init, include = []):

        data = pd.DataFrame(data = {"ELBO": [i.item() for key in self.elbo for i in self.elbo[key][init:num_iter]],
                                    "tree": itertools.chain.from_iterable(zip(*itertools.repeat(range(len(self.potential_trees)), num_iter-init))),
                                    "num_iter": [*range(num_iter-init)]*len(self.potential_trees)})
        # reduce high values
        data.assign(ELBO = [1.5e5 if ELBO > 1.5e5 else ELBO for ELBO in data['ELBO']])
        
        if len(include) > 0:
            sns.lineplot(data = data[data["tree"].isin(include)], x = "num_iter", y = "ELBO",hue="tree", palette = sns.color_palette("tab10", len(include)))
            
        else:
            sns.lineplot(data = data, x = "num_iter", y = "ELBO", hue="tree", palette = sns.color_palette("tab10", len(self.potential_trees)))
//  [python]
def linkage_decay_plot(db, chunkSize=5, min_vals=5, title=''):
    COLS = ['r2', 'r2_normalized', 'd_prime', 'd_prime_normalized']

    # Make chunks
    max_d = db['distance'].max()
    table = defaultdict(list)
    numberChunks = max_d // chunkSize + 1
    db['distance'] = db['distance'].astype(int)
    for i in range(numberChunks):
        d = db[(db['distance'] >= int(i*chunkSize)) & (db['distance'] < int((i+1)*chunkSize))]

        table['d_start'].append(int(i*chunkSize))
        table['d_end'].append(int((i+1)*chunkSize))
        table['values'].append(len(d))
        for col in COLS:
            table[col].append(d[col].mean())
            table[col + '_values'].append(len(d[~d[col].isna()]))
    df = pd.DataFrame(table)
    df['distance'] = [np.mean([x, y]) for x, y in zip(df['d_start'], df['d_end'])]
    df = df.sort_values('distance')

    for col in COLS:
        df[col] = [c if v >= min_vals else np.nan for c, v in zip(df[col], df[col + '_values'])]

    for col in COLS:
        plt.plot(df['distance'], df[col], label=col, marker='o')

    plt.title(title)
    plt.xlabel('Distance between SNPs (bp)\nAveraged over {0}bp windows; plotting windows with at least {1} values'.format(
                chunkSize, min_vals))
    plt.ylabel("SNP linkage")
    return df