# Path: frbpoppy/gen_dists.py
# def powerlaw(low, high, power, shape=1):
#     def sample(n_gen):
#     def accept(pl):
# def trunc_norm(mean, std, shape=1, low=0, high=np.inf):
# def log10normal(mean, std, shape):
# def calc_lognormal_input(mean_x, std_x):
# def lognormal(mean, std, shape):
# 
# 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 numpy as np
import matplotlib.pyplot as plt
from frbpoppy import gen_dists
from tests.convenience import plot_aa_style, rel_path


POWER = -1
SIZE = 1e5

pl = gen_dists.powerlaw(1e35, 1e40, POWER, int(SIZE))
minx = min(pl)
maxx = max(pl)
bins = 10 ** np.linspace(np.log10(minx), np.log10(maxx), 50)
hist, edges = np.histogram(pl, bins=bins)

plot_aa_style()

fig = plt.figure()
ax = fig.add_subplot(111)
plt.step(edges[:-1], hist, where='mid')

# Overplot the expected line
xlims = ax.get_xlim()
xs = np.array([np.log10(edges[0]), np.log10(edges[-2])])
ys = POWER*(xs-xs[0])+np.log10(hist[0])
ax.plot(10**xs, 10**ys, 'r:', label=f'Expected distribution')

plt.legend()

plt.xscale('log')
plt.yscale('log')
ax.set_ylim(1, SIZE)

plt.grid()

# Path: frbpoppy/gen_dists.py
# def powerlaw(low, high, power, shape=1):
#     def sample(n_gen):
#     def accept(pl):
# def trunc_norm(mean, std, shape=1, low=0, high=np.inf):
# def log10normal(mean, std, shape):
# def calc_lognormal_input(mean_x, std_x):
# def lognormal(mean, std, shape):
# 
# 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/misc/powerlaw.py
import numpy as np
import matplotlib.pyplot as plt
from frbpoppy import gen_dists
from tests.convenience import plot_aa_style, rel_path



POWER = -1
SIZE = 1e5

pl = gen_dists.powerlaw(1e35, 1e40, POWER, int(SIZE))
minx = min(pl)
maxx = max(pl)
bins = 10 ** np.linspace(np.log10(minx), np.log10(maxx), 50)
hist, edges = np.histogram(pl, bins=bins)

plot_aa_style()

fig = plt.figure()
ax = fig.add_subplot(111)
plt.step(edges[:-1], hist, where='mid')

# Overplot the expected line
xlims = ax.get_xlim()
xs = np.array([np.log10(edges[0]), np.log10(edges[-2])])
ys = POWER*(xs-xs[0])+np.log10(hist[0])
ax.plot(10**xs, 10**ys, 'r:', label=f'Expected distribution')

plt.legend()

plt.xscale('log')
plt.yscale('log')
ax.set_ylim(1, SIZE)

plt.grid()

plt.savefig(rel_path('plots/powerlaw.pdf'))