# Path: prospect/plotting/corner.py
# def _quantile(x, q, weights=None):
#     """Compute (weighted) quantiles from an input set of samples.
# 
#     Parameters
#     ----------
#     x : `~numpy.ndarray` with shape (nsamps,)
#         Input samples.
# 
#     q : `~numpy.ndarray` with shape (nquantiles,)
#        The list of quantiles to compute from `[0., 1.]`.
# 
#     weights : `~numpy.ndarray` with shape (nsamps,), optional
#         The associated weight from each sample.
# 
#     Returns
#     -------
#     quantiles : `~numpy.ndarray` with shape (nquantiles,)
#         The weighted sample quantiles computed at `q`.
# 
#     """
#     # Initial check.
#     x = np.atleast_1d(x)
#     q = np.atleast_1d(q)
# 
#     # Quantile check.
#     if np.any(q < 0.0) or np.any(q > 1.0):
#         raise ValueError("Quantiles must be between 0. and 1.")
# 
#     if weights is None:
#         # If no weights provided, this simply calls `np.percentile`.
#         return np.percentile(x, list(100.0 * q))
#     else:
#         # If weights are provided, compute the weighted quantiles.
#         weights = np.atleast_1d(weights)
#         if len(x) != len(weights):
#             raise ValueError("Dimension mismatch: len(weights) != len(x).")
#         idx = np.argsort(x)  # sort samples
#         sw = weights[idx]  # sort weights
#         cdf = np.cumsum(sw)[:-1]  # compute CDF
#         cdf /= cdf[-1]  # normalize CDF
#         cdf = np.append(0, cdf)  # ensure proper span
#         quantiles = np.interp(q, cdf, x[idx]).tolist()
#         return quantiles
# 
# Path: prospect/models/priors.py
# class TopHat(Uniform):
#     """Uniform distribution between two bounds, renamed for backwards compatibility
#     :param mini:
#         Minimum of the distribution
# 
#     :param maxi:
#         Maximum of the distribution
#     """


import numpy as np
from .corner import _quantile
from ..models.priors import TopHat as Uniform
    from matplotlib.patches import Rectangle
    from matplotlib.collections import PatchCollection
def best_sample(res):
    """Get the posterior sample with the highest posterior probability.
    """
    imax = np.argmax(res['lnprobability'])
    # there must be a more elegant way to deal with differnt shapes
    try:
        i, j = np.unravel_index(imax, res['lnprobability'].shape)
        theta_best = res['chain'][i, j, :].copy()
    except(ValueError):
        theta_best = res['chain'][imax, :].copy()
    return theta_best


def violinplot(data, pos, widths, ax=None,
               violin_kwargs={"showextrema": False},
               color="slateblue", alpha=0.5, span=None, **extras):
    ndim = len(data)
    clipped_data = []

    if type(color) is str:
        color = ndim * [color]

    if span is None:
        span = [0.999999426697 for i in range(ndim)]

    for i, _ in enumerate(span):
        try:
            xmin, xmax = span[i]
        except(TypeError):
            q = [0.5 - 0.5 * span[i], 0.5 + 0.5 * span[i]]

# Path: prospect/plotting/corner.py
# def _quantile(x, q, weights=None):
#     """Compute (weighted) quantiles from an input set of samples.
# 
#     Parameters
#     ----------
#     x : `~numpy.ndarray` with shape (nsamps,)
#         Input samples.
# 
#     q : `~numpy.ndarray` with shape (nquantiles,)
#        The list of quantiles to compute from `[0., 1.]`.
# 
#     weights : `~numpy.ndarray` with shape (nsamps,), optional
#         The associated weight from each sample.
# 
#     Returns
#     -------
#     quantiles : `~numpy.ndarray` with shape (nquantiles,)
#         The weighted sample quantiles computed at `q`.
# 
#     """
#     # Initial check.
#     x = np.atleast_1d(x)
#     q = np.atleast_1d(q)
# 
#     # Quantile check.
#     if np.any(q < 0.0) or np.any(q > 1.0):
#         raise ValueError("Quantiles must be between 0. and 1.")
# 
#     if weights is None:
#         # If no weights provided, this simply calls `np.percentile`.
#         return np.percentile(x, list(100.0 * q))
#     else:
#         # If weights are provided, compute the weighted quantiles.
#         weights = np.atleast_1d(weights)
#         if len(x) != len(weights):
#             raise ValueError("Dimension mismatch: len(weights) != len(x).")
#         idx = np.argsort(x)  # sort samples
#         sw = weights[idx]  # sort weights
#         cdf = np.cumsum(sw)[:-1]  # compute CDF
#         cdf /= cdf[-1]  # normalize CDF
#         cdf = np.append(0, cdf)  # ensure proper span
#         quantiles = np.interp(q, cdf, x[idx]).tolist()
#         return quantiles
# 
# Path: prospect/models/priors.py
# class TopHat(Uniform):
#     """Uniform distribution between two bounds, renamed for backwards compatibility
#     :param mini:
#         Minimum of the distribution
# 
#     :param maxi:
#         Maximum of the distribution
#     """


# Path: prospect/plotting/utils.py
import numpy as np
from .corner import _quantile
from ..models.priors import TopHat as Uniform
    from matplotlib.patches import Rectangle
    from matplotlib.collections import PatchCollection

def best_sample(res):
    """Get the posterior sample with the highest posterior probability.
    """
    imax = np.argmax(res['lnprobability'])
    # there must be a more elegant way to deal with differnt shapes
    try:
        i, j = np.unravel_index(imax, res['lnprobability'].shape)
        theta_best = res['chain'][i, j, :].copy()
    except(ValueError):
        theta_best = res['chain'][imax, :].copy()
    return theta_best


def violinplot(data, pos, widths, ax=None,
               violin_kwargs={"showextrema": False},
               color="slateblue", alpha=0.5, span=None, **extras):
    ndim = len(data)
    clipped_data = []

    if type(color) is str:
        color = ndim * [color]

    if span is None:
        span = [0.999999426697 for i in range(ndim)]

    for i, _ in enumerate(span):
        try:
            xmin, xmax = span[i]
        except(TypeError):
            q = [0.5 - 0.5 * span[i], 0.5 + 0.5 * span[i]]

            xmin, xmax = _quantile(data[i], q)