# Path: tests/convenience.py
# def plot_aa_style(cols=1):
#     """Use a plotting style specifically for Astronomy & Astrophyiscs."""
#     # Use a style file
#     dir = os.path.abspath(os.path.dirname(__file__))
#     style_file = os.path.join(dir, './aa.mplstyle')
#     plt.style.use(style_file)
# 
#     # Check whether interactive backend is present
#     if os.name == 'posix' and "DISPLAY" not in os.environ:
#         # On a cluster
#         plt.switch_backend('Agg')
#         # Use just the basic latex package allowing \text to be used
#         plt.rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'
# 
#     if cols == 2:
#         plt.rcParams["figure.figsize"] = (5.75373, 3.556)
# 
# def rel_path(path):
#     """Return the relative path name for this directory."""
#     return os.path.join(os.path.abspath(os.path.dirname(__file__)), path)


from scipy.integrate import quad
from scipy.stats import chi2, norm
from frbcat import Frbcat
from tests.convenience import plot_aa_style, rel_path
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

    # Calculate error bars
    low, high = poisson_interval(n_bursts.to_numpy(), sigma=1)
    rate_err = (low/exposure, high/exposure)

    # Plot values
    plt.errorbar(width, rate, xerr=width_err, yerr=rate_err,
                 marker='x', fmt='o')

    # Print correlation
    _width = width[~np.isnan(rate)]
    _rate = rate[~np.isnan(rate)]
    r = np.corrcoef(_width, _rate)[0, 1]
    print('Pearson correlation coefficient: ', r)
    r = np.corrcoef(np.log10(_width), np.log10(_rate))[0, 1]
    print('Pearson correlation coefficient in log10 space: ', r)

    plt.xlabel(r'Pulse Width (ms)')
    plt.ylabel(r'Rate (/hour)')

    if SCALE == 'log':
        plt.xscale('log')
        plt.yscale('log', nonposy='clip')

    plt.tight_layout()
    plt.savefig(rel_path('./plots/rate_w_eff_chime.pdf'))

    # Save data
    a = np.asarray([width, width_err[0], width_err[1],
                    rate, rate_err[0], rate_err[1]]).T

# Path: tests/convenience.py
# def plot_aa_style(cols=1):
#     """Use a plotting style specifically for Astronomy & Astrophyiscs."""
#     # Use a style file
#     dir = os.path.abspath(os.path.dirname(__file__))
#     style_file = os.path.join(dir, './aa.mplstyle')
#     plt.style.use(style_file)
# 
#     # Check whether interactive backend is present
#     if os.name == 'posix' and "DISPLAY" not in os.environ:
#         # On a cluster
#         plt.switch_backend('Agg')
#         # Use just the basic latex package allowing \text to be used
#         plt.rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'
# 
#     if cols == 2:
#         plt.rcParams["figure.figsize"] = (5.75373, 3.556)
# 
# def rel_path(path):
#     """Return the relative path name for this directory."""
#     return os.path.join(os.path.abspath(os.path.dirname(__file__)), path)


# Path: tests/chime/rate_w_eff.py
from scipy.integrate import quad
from scipy.stats import chi2, norm
from frbcat import Frbcat
from tests.convenience import plot_aa_style, rel_path
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


    # Calculate error bars
    low, high = poisson_interval(n_bursts.to_numpy(), sigma=1)
    rate_err = (low/exposure, high/exposure)

    # Plot values
    plt.errorbar(width, rate, xerr=width_err, yerr=rate_err,
                 marker='x', fmt='o')

    # Print correlation
    _width = width[~np.isnan(rate)]
    _rate = rate[~np.isnan(rate)]
    r = np.corrcoef(_width, _rate)[0, 1]
    print('Pearson correlation coefficient: ', r)
    r = np.corrcoef(np.log10(_width), np.log10(_rate))[0, 1]
    print('Pearson correlation coefficient in log10 space: ', r)

    plt.xlabel(r'Pulse Width (ms)')
    plt.ylabel(r'Rate (/hour)')

    if SCALE == 'log':
        plt.xscale('log')
        plt.yscale('log', nonposy='clip')

    plt.tight_layout()
    plt.savefig(rel_path('./plots/rate_w_eff_chime.pdf'))

    # Save data
    a = np.asarray([width, width_err[0], width_err[1],
                    rate, rate_err[0], rate_err[1]]).T

    np.savetxt(rel_path('./plots/chime_rep_width.csv'), a, delimiter=",",