def p_value_b(gamma_k, f, rs, nBDs, Tobs_R, robs_R, Mobs_R, heat_int_R, relT_R,
              relM_R, relR_R, relA_R, steps=300):
    """                                                                        
    Return p-value for gamma_k @ (f, rs) under b hypothesis
    """                                                                        
    # Compute TS pdf                                                           
    TS_k  = np.zeros(steps)                                                    
    # Load ATMO2020 model                                                      
    path   = "/home/mariacst/exoplanets/exoplanets/data/"                      
    data   = np.genfromtxt(path + "./ATMO_CEQ_vega_MIRI.txt", unpack=True)     
    points = np.transpose(data[0:2, :])                                        
    values = data[2]                                                           
                                                                               
    for i in range(steps):                                                     
        # Generate experiments under s+b hypothesis
        robs, Tobs, Mobs, ages = mock_population_all(nBDs, relT_R, relM_R, relR_R,
                                         relA_R, 0., gamma_k, rs)              
        # Predicted intrinsic temperatures
        xi       = np.transpose(np.asarray([ages, Mobs]))
        Teff     = griddata(points, values, xi)
        heat_int = heat(Teff, np.ones(len(Teff))*R_jup.value)
        # TS
        TS_k[i] = TS(gamma_k, f, rs, Tobs, robs, Mobs, heat_int)
    # observed TS
    q_gamma_k_obs = TS(gamma_k, f, rs, Tobs_R, robs_R, Mobs_R, heat_int_R)
    # return
    return (100-percentileofscore(TS_k, q_gamma_k_obs, kind="strict"))
def UL(rs, f, nBDs, relT_R, relM_R, relR_R, relA_R, steps=300):
    # Generate "real" observation assuming only background (no DM)
    rho0=0.42
    # Load ATMO2020 model
    path   = "/home/mariacst/exoplanets/exoplanets/data/"
    data   = np.genfromtxt(path + "./ATMO_CEQ_vega_MIRI.txt", unpack=True)
    points = np.transpose(data[0:2, :])
    values = data[2]
    robs_R, Tobs_R, mass_R, ages_R = mock_population_all(nBDs, 0., 0., 
                                         0., 0.,
                                         0, 1., 1., rho0_true=rho0, v=None)
    xi         = np.transpose(np.asarray([ages_R, mass_R]))
    Teff       = griddata(points, values, xi)
    heat_int_R = heat(Teff, np.ones(len(Teff))*R_jup.value)

    import pdb
    pdb.set_trace()

    gamma_up = np.ones(len(rs))*10
    for i in range(len(rs)):
        if rs[i] > 7.:
            gamma_k = np.linspace(0.4, 2.9, 35) # change this?
        else:
            gamma_k  = np.linspace(0, 1.5, 35) # change this?
        for g in gamma_k:
            _p_sb = p_value_sb(g, f, rs[i], nBDs, Tobs_R, robs_R, mass_R, 
                               heat_int_R, relT_R, relM_R, relR_R, relA_R, 
                               steps=steps)
            _p_b = p_value_b(g, f, rs[i], nBDs, Tobs_R, robs_R, mass_R, heat_int_R,
                             relT_R, relM_R, relR_R, relA_R, steps=steps)
            try:
                CL = _p_sb / _p_b
            except ZeroDivisionError:
                CL = 200.
            if CL < 0.05:
                print(rs[i], g)
                gamma_up[i] = g
                break
    #return
    return gamma_up
    return lp + residual(p)  #, robs, Tobs, rel_unc_Tobs, heat_int, mass)


# ------------------------------------------------------------------------

# Load theoretical cooling model
path = "./data/"
data = np.genfromtxt(path + "./ATMO_CEQ_vega_MIRI.txt", unpack=True)
points = np.transpose(data[0:2, :])
values = data[2]
# Generate mock observation
robs, Tobs, mass, ages = mock_population_all(nBDs,
                                             relTobs,
                                             relM,
                                             relRobs,
                                             relA,
                                             f_true,
                                             gamma_true,
                                             rs_true,
                                             rho0_true=rho0,
                                             v=v)
## calculate predictic intrinsic heat flow for mock BDs
xi = np.transpose(np.asarray([ages, mass]))
Teff = griddata(points, values, xi)
heat_int = heat(Teff, np.ones(len(Teff)) * R_jup.value)
# ------------------ RECONSTRUCTION --------------------------------------
ndim = 3
nwalkers = 50
# first guess
p0 = [[0.9, 0.9, 20.] + 1e-4 * np.random.randn(ndim) for j in range(nwalkers)]
sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob)
#args=(r_obs, Tobs, rel_unc_T, heat_int, mass))