# Path: paratemp/constants.py

# 
# Path: paratemp/tools.py
# def running_mean(x, n=2):
#     """
#     Calculate running mean over an iterable
# 
#     Taken from https://stackoverflow.com/a/22621523/3961920
# 
#     :param Iterable x: List over which to calculate the mean.
#     :param int n: Default: 2. Width for the means.
#     :return: Array of the running mean values.
#     :rtype: np.ndarray
#     """
#     if len(x) != 0:
#         return np.convolve(x, np.ones((n,)) / n, mode="valid")
#     else:
#         raise ValueError("x cannot be empty")


import numpy as np
from matplotlib import pyplot as plt
from .constants import r
from .tools import running_mean
from __future__ import absolute_import, division, print_function




__all__ = ["calc_fes_2d", "calc_fes_1d"]


def _parse_bin_input(bins):
    if bins is None:
        return dict()
    return dict(bins=bins)


def calc_fes_2d(x, y, temp, bins=None):
    d_bins = _parse_bin_input(bins)
    counts, xedges, yedges = np.histogram2d(x, y, **d_bins)
    probs = np.array([[i / counts.max() for i in j] for j in counts]) + 1e-40
    delta_g = np.array([[-r * temp * np.log(p) for p in j] for j in probs])
    xmids, ymids = running_mean(xedges), running_mean(yedges)
    return delta_g, xmids, ymids


def calc_fes_1d(data, temp, bins=None):
    d_bins = _parse_bin_input(bins)
    n, _bins = np.histogram(data, **d_bins)
    n = [float(j) for j in n]
    # TODO find better way to account for zeros here rather than
    # just adding a small amount to each.
    prob = np.array([j / max(n) for j in n]) + 1e-40

# Path: paratemp/constants.py

# 
# Path: paratemp/tools.py
# def running_mean(x, n=2):
#     """
#     Calculate running mean over an iterable
# 
#     Taken from https://stackoverflow.com/a/22621523/3961920
# 
#     :param Iterable x: List over which to calculate the mean.
#     :param int n: Default: 2. Width for the means.
#     :return: Array of the running mean values.
#     :rtype: np.ndarray
#     """
#     if len(x) != 0:
#         return np.convolve(x, np.ones((n,)) / n, mode="valid")
#     else:
#         raise ValueError("x cannot be empty")


# Path: paratemp/utils.py
import numpy as np
from matplotlib import pyplot as plt
from .constants import r
from .tools import running_mean

from __future__ import absolute_import, division, print_function




__all__ = ["calc_fes_2d", "calc_fes_1d"]


def _parse_bin_input(bins):
    if bins is None:
        return dict()
    return dict(bins=bins)


def calc_fes_2d(x, y, temp, bins=None):
    d_bins = _parse_bin_input(bins)
    counts, xedges, yedges = np.histogram2d(x, y, **d_bins)
    probs = np.array([[i / counts.max() for i in j] for j in counts]) + 1e-40
    delta_g = np.array([[-r * temp * np.log(p) for p in j] for j in probs])
    xmids, ymids = running_mean(xedges), running_mean(yedges)
    return delta_g, xmids, ymids


def calc_fes_1d(data, temp, bins=None):
    d_bins = _parse_bin_input(bins)
    n, _bins = np.histogram(data, **d_bins)
    n = [float(j) for j in n]
    # TODO find better way to account for zeros here rather than
    # just adding a small amount to each.
    prob = np.array([j / max(n) for j in n]) + 1e-40

    delta_g = np.array([-r * temp * np.log(p) for p in prob])