# 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
        df.loc[df.frb_name == name, 'rate'] = row.rate
        df.loc[df.frb_name == name, 'n_bursts'] = row.n_bursts
        df.loc[df.frb_name == name, 'exposure'] = row.exposure

    return df


def poisson_interval(k, sigma=1):
    """
    Use chi-squared info to get the poisson interval.

    Given a number of observed events, which range of observed events would
    have been just as likely given a particular interval?

    Based off https://stackoverflow.com/questions/14813530/
    poisson-confidence-interval-with-numpy
    """
    gauss = norm(0, 1).pdf
    a = 1 - quad(gauss, -sigma, sigma, limit=1000)[0]
    low, high = (chi2.ppf(a/2, 2*k) / 2, chi2.ppf(1-a/2, 2*k + 2) / 2)
    if isinstance(k, np.ndarray):
        low[k == 0] = 0.
    elif k == 0:
        low = 0.0

    return low, high


def plot_w_eff_rate(df):
    """Plot effective pulse width against rate."""

# 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

        df.loc[df.frb_name == name, 'rate'] = row.rate
        df.loc[df.frb_name == name, 'n_bursts'] = row.n_bursts
        df.loc[df.frb_name == name, 'exposure'] = row.exposure

    return df


def poisson_interval(k, sigma=1):
    """
    Use chi-squared info to get the poisson interval.

    Given a number of observed events, which range of observed events would
    have been just as likely given a particular interval?

    Based off https://stackoverflow.com/questions/14813530/
    poisson-confidence-interval-with-numpy
    """
    gauss = norm(0, 1).pdf
    a = 1 - quad(gauss, -sigma, sigma, limit=1000)[0]
    low, high = (chi2.ppf(a/2, 2*k) / 2, chi2.ppf(1-a/2, 2*k + 2) / 2)
    if isinstance(k, np.ndarray):
        low[k == 0] = 0.
    elif k == 0:
        low = 0.0

    return low, high


def plot_w_eff_rate(df):
    """Plot effective pulse width against rate."""

    plot_aa_style(cols=1)