# Path: prospect/models/transforms.py
# def logsfr_ratios_to_masses(logmass=None, logsfr_ratios=None, agebins=None,
#                             **extras):
#     """This converts from an array of log_10(SFR_j / SFR_{j+1}) and a value of
#     log10(\Sum_i M_i) to values of M_i.  j=0 is the most recent bin in lookback
#     time.
#     """
#     nbins = agebins.shape[0]
#     sratios = 10**np.clip(logsfr_ratios, -100, 100)  # numerical issues...
#     dt = (10**agebins[:, 1] - 10**agebins[:, 0])
#     coeffs = np.array([ (1. / np.prod(sratios[:i])) * (np.prod(dt[1: i+1]) / np.prod(dt[: i]))
#                         for i in range(nbins)])
#     m1 = (10**logmass) / coeffs.sum()
# 
#     return m1 * coeffs
# 
# Path: prospect/sources/constants.py

# 
# Path: prospect/plotting/corner.py
# def quantile(xarr, q, weights=None):
#     """Compute (weighted) quantiles from an input set of samples.
# 
#     :param x: `~numpy.darray` with shape (nvar, nsamples)
#         The input array to compute quantiles of.
# 
#     :param q: list of quantiles, from [0., 1.]
# 
#     :param weights: shape (nsamples)
# 
#     :returns quants: ndarray of shape (nvar, nq)
#         The quantiles of each varaible.
#     """
#     qq = [_quantile(x, q, weights=weights) for x in xarr]
#     return np.array(qq)


from argparse import Namespace
from copy import deepcopy
from scipy.special import gamma, gammainc
from ..models.transforms import logsfr_ratios_to_masses
from ..sources.constants import cosmo
from .corner import quantile
import numpy as np
    import matplotlib.pyplot as pl
        sfh["mass"] = 10**logmass
    for k in vars(pset).keys():
        if k in sfh:
            setattr(pset, k, sfh[k])
    # vectorize
    for k in vectors:
        setattr(pset, k, np.atleast_1d(getattr(pset, k)))

    return pset


def sfh_quantiles(tvec, bins, sfrs, weights=None, q=[16, 50, 84]):
    """Compute quantiles of a binned SFH

    :param tvec: shape (ntime,)
        Vector of lookback times onto which the SFH will be interpolated.

    :param bins: shape (nsamples, nbin, 2)
        The age bins, in linear untis, same units as tvec

    :param sfrs: shape (nsamples, nbin)
        The SFR in each bin

    :returns sfh_q: shape(ntime, nq)
        The quantiles of the SFHs at each lookback time in `tvec`
    """
    tt = bins.reshape(bins.shape[0], -1)
    ss = np.array([sfrs, sfrs]).transpose(1, 2, 0).reshape(bins.shape[0], -1)
    sf = np.array([np.interp(tvec, t, s, left=0, right=0) for t, s in zip(tt, ss)])
    if weights is not None:

# Path: prospect/models/transforms.py
# def logsfr_ratios_to_masses(logmass=None, logsfr_ratios=None, agebins=None,
#                             **extras):
#     """This converts from an array of log_10(SFR_j / SFR_{j+1}) and a value of
#     log10(\Sum_i M_i) to values of M_i.  j=0 is the most recent bin in lookback
#     time.
#     """
#     nbins = agebins.shape[0]
#     sratios = 10**np.clip(logsfr_ratios, -100, 100)  # numerical issues...
#     dt = (10**agebins[:, 1] - 10**agebins[:, 0])
#     coeffs = np.array([ (1. / np.prod(sratios[:i])) * (np.prod(dt[1: i+1]) / np.prod(dt[: i]))
#                         for i in range(nbins)])
#     m1 = (10**logmass) / coeffs.sum()
# 
#     return m1 * coeffs
# 
# Path: prospect/sources/constants.py

# 
# Path: prospect/plotting/corner.py
# def quantile(xarr, q, weights=None):
#     """Compute (weighted) quantiles from an input set of samples.
# 
#     :param x: `~numpy.darray` with shape (nvar, nsamples)
#         The input array to compute quantiles of.
# 
#     :param q: list of quantiles, from [0., 1.]
# 
#     :param weights: shape (nsamples)
# 
#     :returns quants: ndarray of shape (nvar, nq)
#         The quantiles of each varaible.
#     """
#     qq = [_quantile(x, q, weights=weights) for x in xarr]
#     return np.array(qq)


# Path: prospect/plotting/sfh.py
from argparse import Namespace
from copy import deepcopy
from scipy.special import gamma, gammainc
from ..models.transforms import logsfr_ratios_to_masses
from ..sources.constants import cosmo
from .corner import quantile
import numpy as np
    import matplotlib.pyplot as pl

        sfh["mass"] = 10**logmass
    for k in vars(pset).keys():
        if k in sfh:
            setattr(pset, k, sfh[k])
    # vectorize
    for k in vectors:
        setattr(pset, k, np.atleast_1d(getattr(pset, k)))

    return pset


def sfh_quantiles(tvec, bins, sfrs, weights=None, q=[16, 50, 84]):
    """Compute quantiles of a binned SFH

    :param tvec: shape (ntime,)
        Vector of lookback times onto which the SFH will be interpolated.

    :param bins: shape (nsamples, nbin, 2)
        The age bins, in linear untis, same units as tvec

    :param sfrs: shape (nsamples, nbin)
        The SFR in each bin

    :returns sfh_q: shape(ntime, nq)
        The quantiles of the SFHs at each lookback time in `tvec`
    """
    tt = bins.reshape(bins.shape[0], -1)
    ss = np.array([sfrs, sfrs]).transpose(1, 2, 0).reshape(bins.shape[0], -1)
    sf = np.array([np.interp(tvec, t, s, left=0, right=0) for t, s in zip(tt, ss)])
    if weights is not None:

        qq = quantile(sf.T, q=np.array(q)/100., weights=weights)