# 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
    _, mass = compute_mass_formed(tage - times, pset)
    return mass


def parametric_mwa_numerical(tau=4, tage=13.7, power=1, n=1000):
    """Compute Mass-weighted age

    :param power: (optional, default: 1)
        Use 0 for exponential decline, and 1 for te^{-t} (delayed exponential decline)
    """
    p = power + 1
    t = np.linspace(0, tage, n)
    tavg = np.trapz((t**p)*np.exp(-t/tau), t) / np.trapz(t**(power) * np.exp(-t/tau), t)
    return tage - tavg


def parametric_mwa(tau=4, tage=13.7, power=1):
    """Compute Mass-weighted age. This is done analytically

    :param power: (optional, default: 1)
        Use 0 for exponential decline, and 1 for te^{-t} (delayed exponential decline)
    """
    tt = tage / tau
    mwt = gammainc(power+2, tt) * gamma(power+2) / gammainc(power+1, tt) * tau
    return tage - mwt


def ratios_to_sfrs(logmass, logsfr_ratios, agebins):
    """scalar
    """

# 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

    _, mass = compute_mass_formed(tage - times, pset)
    return mass


def parametric_mwa_numerical(tau=4, tage=13.7, power=1, n=1000):
    """Compute Mass-weighted age

    :param power: (optional, default: 1)
        Use 0 for exponential decline, and 1 for te^{-t} (delayed exponential decline)
    """
    p = power + 1
    t = np.linspace(0, tage, n)
    tavg = np.trapz((t**p)*np.exp(-t/tau), t) / np.trapz(t**(power) * np.exp(-t/tau), t)
    return tage - tavg


def parametric_mwa(tau=4, tage=13.7, power=1):
    """Compute Mass-weighted age. This is done analytically

    :param power: (optional, default: 1)
        Use 0 for exponential decline, and 1 for te^{-t} (delayed exponential decline)
    """
    tt = tage / tau
    mwt = gammainc(power+2, tt) * gamma(power+2) / gammainc(power+1, tt) * tau
    return tage - mwt


def ratios_to_sfrs(logmass, logsfr_ratios, agebins):
    """scalar
    """

    masses = logsfr_ratios_to_masses(np.squeeze(logmass),