# Path: prospect/plotting/utils.py
# def get_best(res, **kwargs):
#     """Get the maximum a posteriori parameters and their names
# 
#     :param res:
#         A ``results`` dictionary with the keys 'lnprobability', 'chain', and
#         'theta_labels'
# 
#     :returns theta_names:
#         List of strings giving the names of the parameters, of length ``ndim``
# 
#     :returns best:
#         ndarray with shape ``(ndim,)`` of parameter values corresponding to the
#         sample with the highest posterior probaility
#     """
#     theta_best = best_sample(res)
# 
#     try:
#         theta_names = res["theta_labels"]
#     except(KeyError):
#         theta_names = res["model"].theta_labels()
#     return theta_names, theta_best


import numpy as np
    import matplotlib.pyplot as pl
    import pickle
from ..plotting.utils import get_best
    from matplotlib import gridspec
        flatchain = res["chain"][start_index::thin, :]
    pct = np.array([quantile(p, ptile, weights=res.get("weights", None)) for p in flatchain.T])
    return dict(zip(parnames, pct))


def quantile(data, percents, weights=None):
    ''' percents in units of 1%
    weights specifies the frequency (count) of data.
    '''
    if weights is None:
        return np.percentile(data, percents)
    ind = np.argsort(data)
    d = data[ind]
    w = weights[ind]
    p = 1.*w.cumsum()/w.sum()*100
    y = np.interp(percents, p, d)
    return y


def get_stats(res, pnames, **kwargs):
    """For selected parameters, get the truth (if known), the MAP value from
    the chain, and the percentiles.

    :param res:
        A results dictionary, containing a "chain" and "theta_labels" keys.

    :param pnames:
        List of strings giving the names of the desired parameters.
    """
    truth = get_truths(res)

# Path: prospect/plotting/utils.py
# def get_best(res, **kwargs):
#     """Get the maximum a posteriori parameters and their names
# 
#     :param res:
#         A ``results`` dictionary with the keys 'lnprobability', 'chain', and
#         'theta_labels'
# 
#     :returns theta_names:
#         List of strings giving the names of the parameters, of length ``ndim``
# 
#     :returns best:
#         ndarray with shape ``(ndim,)`` of parameter values corresponding to the
#         sample with the highest posterior probaility
#     """
#     theta_best = best_sample(res)
# 
#     try:
#         theta_names = res["theta_labels"]
#     except(KeyError):
#         theta_names = res["model"].theta_labels()
#     return theta_names, theta_best


# Path: prospect/utils/plotting.py
import numpy as np
    import matplotlib.pyplot as pl
    import pickle
from ..plotting.utils import get_best
    from matplotlib import gridspec

        flatchain = res["chain"][start_index::thin, :]
    pct = np.array([quantile(p, ptile, weights=res.get("weights", None)) for p in flatchain.T])
    return dict(zip(parnames, pct))


def quantile(data, percents, weights=None):
    ''' percents in units of 1%
    weights specifies the frequency (count) of data.
    '''
    if weights is None:
        return np.percentile(data, percents)
    ind = np.argsort(data)
    d = data[ind]
    w = weights[ind]
    p = 1.*w.cumsum()/w.sum()*100
    y = np.interp(percents, p, d)
    return y


def get_stats(res, pnames, **kwargs):
    """For selected parameters, get the truth (if known), the MAP value from
    the chain, and the percentiles.

    :param res:
        A results dictionary, containing a "chain" and "theta_labels" keys.

    :param pnames:
        List of strings giving the names of the desired parameters.
    """
    truth = get_truths(res)

    best = dict(zip(*get_best(res)))