# Path: frbpoppy/misc.py
# 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 k == 0:
#         low = 0.0
# 
#     return low, high
# 
# 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)


import matplotlib.pyplot as plt
import numpy as np
from frbpoppy import poisson_interval
from tests.convenience import plot_aa_style, rel_path
EXPECTED = {'parkes-htru': [9, 1549 / 0.551 / 24],  # N_frbs, N_days
            'wsrt-apertif': [9, 1100/24],  # 1100 hours
            'askap-fly': [20, 32840 / 8 / 24],
            'arecibo-palfa': [1, 24.1],
            'guppi': [0.4, 81],  # 0.4 is my own assumption
            'fast-crafts': [1, 1500/24],
            'chime-frb': [2, 1],  # My own assumption
            'askap-incoh': [9, 45]  # My own assumption
            }

SURVEYS = ('parkes-htru', 'wsrt-apertif', 'askap-fly', 'fast-crafts', 'arecibo-palfa')
ALPHAS = np.around(np.linspace(-0.5, -2.0, 7), decimals=2)


def real_rates(surveys=SURVEYS):
    """Calculate the EXPECTED rates (all scaled to a survey)."""
    rates = {}
    scale_to = surveys[0]

    for surv in surveys:

        if surv not in EXPECTED:
            continue

        # Plot EXPECTED rate
        exp_n = EXPECTED[surv][0]
        exp_days = EXPECTED[surv][1]
        scale_n = EXPECTED[scale_to][0]
        scale_days = EXPECTED[scale_to][1]


# Path: frbpoppy/misc.py
# 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 k == 0:
#         low = 0.0
# 
#     return low, high
# 
# 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/rates/alpha_real.py
import matplotlib.pyplot as plt
import numpy as np
from frbpoppy import poisson_interval
from tests.convenience import plot_aa_style, rel_path

EXPECTED = {'parkes-htru': [9, 1549 / 0.551 / 24],  # N_frbs, N_days
            'wsrt-apertif': [9, 1100/24],  # 1100 hours
            'askap-fly': [20, 32840 / 8 / 24],
            'arecibo-palfa': [1, 24.1],
            'guppi': [0.4, 81],  # 0.4 is my own assumption
            'fast-crafts': [1, 1500/24],
            'chime-frb': [2, 1],  # My own assumption
            'askap-incoh': [9, 45]  # My own assumption
            }

SURVEYS = ('parkes-htru', 'wsrt-apertif', 'askap-fly', 'fast-crafts', 'arecibo-palfa')
ALPHAS = np.around(np.linspace(-0.5, -2.0, 7), decimals=2)


def real_rates(surveys=SURVEYS):
    """Calculate the EXPECTED rates (all scaled to a survey)."""
    rates = {}
    scale_to = surveys[0]

    for surv in surveys:

        if surv not in EXPECTED:
            continue

        # Plot EXPECTED rate
        exp_n = EXPECTED[surv][0]
        exp_days = EXPECTED[surv][1]
        scale_n = EXPECTED[scale_to][0]
        scale_days = EXPECTED[scale_to][1]


        exp_min, exp_max = poisson_interval(exp_n, sigma=2)