Exemplo n.º 1
0
def run(fN_cs, fN_model, parm, email, debug=0):

    #
    pymc_list = [parm]

    # Parse data and combine as warranted
    all_NHI = []
    all_fN = []
    all_sigfN = []
    all_z = []
    flg_teff = 0
    flg_LLS = 0
    for fN_c in fN_cs:
        # Standard f(N)
        if fN_c.fN_dtype == 'fN':
            ip = range(fN_c.data['NPT'])
            val = np.where(
                fN_c.data['FN'][ip] > -90)[0]  # Deal with limits later
            ipv = np.array(ip)[val]
            # Append the NHI
            NHI = np.median(fN_c.data['BINS'][:, ipv], 0)
            all_NHI += list(NHI)
            # Append the f(N)
            all_fN += list(fN_c.data['FN'][ipv])
            # Append the Error
            fNerror = np.median(fN_c.data['SIG_FN'][:, ipv], 0)
            all_sigfN += list(fNerror)
            # Append zeval
            for ii in range(len(ipv)):
                all_z.append(fN_c.zeval)
        elif fN_c.fN_dtype == 'teff':  # teff_Lya
            if flg_teff:
                raise ValueError('Only one teff allowed for now!')
            else:
                flg_teff = 1
            teff = float(fN_c.data['TEFF'])
            D_A = 1. - np.exp(-1. * teff)
            SIGDA_LIMIT = 0.1  # Allows for systemtics and b-value uncertainty
            sig_teff = np.max([fN_c.data['SIG_TEFF'], (SIGDA_LIMIT * teff)])
            teff_zeval = float(fN_c.data['Z_TEFF'])

            # Save input for later usage
            teff_input = (teff_zeval, fN_c.data['NHI_MNX'][0],
                          fN_c.data['NHI_MNX'][1])
        elif fN_c.fN_dtype == 'l(X)':  # teff_Lya
            if flg_LLS:
                raise ValueError('Only one teff allowed for now!')
            else:
                flg_LLS = 1
            LLS_lx = fN_c.data['LX']
            LLS_siglx = fN_c.data['SIG_LX']
            LLS_input = (fN_c.zeval, fN_c.data['TAU_LIM'])

    #
    fN_input = (np.array(all_NHI), np.array(all_z))
    #flg_teff = 0

    #######################################
    #   Generate the Models
    #######################################

    # Define f(N) model for PyMC
    @pymc.deterministic(plot=False)
    def pymc_fn_model(parm=parm):
        # Set parameters
        fN_model.upd_param(parm)
        #
        log_fNX = fN_model.eval(fN_input, 0.)
        #
        return log_fNX

    pymc_list.append(pymc_fn_model)

    # Define teff model for PyMC
    if flg_teff:

        @pymc.deterministic(plot=False)
        def pymc_teff_model(parm=parm):
            # Set parameters
            fN_model.upd_param(parm)
            # Calculate teff
            model_teff = tau_eff.ew_teff_lyman(1215.6701 * (1 + teff_input[0]),
                                               teff_input[0] + 0.1,
                                               fN_model,
                                               NHI_MIN=teff_input[1],
                                               NHI_MAX=teff_input[2])
            return model_teff

        pymc_list.append(pymc_teff_model)

    # Define l(X)_LLS model for PyMC
    if flg_LLS:

        @pymc.deterministic(plot=False)
        def pymc_lls_model(parm=parm):
            # Set parameters
            fN_model.upd_param(parm)
            # Calculate l(X)
            lX = fN_model.calc_lox(LLS_input[0],
                                   17.19 + np.log10(LLS_input[1]), 22.)
            return lX

        pymc_list.append(pymc_lls_model)

    #######################################
    #   Generate the Data
    #######################################

    # Define f(N) data for PyMC
    fNvalue = np.array(all_fN)
    #xdb.set_trace()
    pymc_fN_data = pymc.Normal(str('fNdata'),
                               mu=pymc_fn_model,
                               tau=1.0 / np.array(all_sigfN)**2,
                               value=fNvalue,
                               observed=True)
    pymc_list.append(pymc_fN_data)

    # Define teff data for PyMC
    if flg_teff:
        pymc_teff_data = pymc.Normal(str('teffdata'),
                                     mu=pymc_teff_model,
                                     tau=1.0 / np.array(sig_teff)**2,
                                     value=teff,
                                     observed=True)
        pymc_list.append(pymc_teff_data)

    # Define l(X)_LLS model for PyMC
    if flg_LLS:
        pymc_lls_data = pymc.Normal(str('LLSdata'),
                                    mu=pymc_lls_model,
                                    tau=1.0 / np.array(LLS_siglx)**2,
                                    value=LLS_lx,
                                    observed=True)
        pymc_list.append(pymc_lls_data)

    #######################################
    #   RUN THE MCMC
    #######################################

    MC = pymc.MCMC(pymc_list)  #,verbose=2)
    # Force step method to be Metropolis!
    for ss in MC.stochastics - MC.observed_stochastics:
        MC.use_step_method(pymc.Metropolis,
                           ss,
                           proposal_sd=0.025,
                           proposal_distribution='Normal')
    #xdb.set_trace()

    # Run a total of 40000 samples, but ignore the first 10000.
    # Verbose just prints some details to screen.
    #xdb.set_trace()
    #MC.sample(20000, 3000, verbose=2, tune_interval=500)
    #MC.sample(5000, 500, verbose=2, tune_interval=200)
    #MC.sample(20000, 5000, verbose=2, tune_interval=500)
    MC.sample(2000, 400, verbose=2, tune_interval=200)
    #MC.isample(10000, 1000, verbose=2)

    if debug:
        xifd.tst_fn_data(fN_model=fN_model)
        xdb.xhist(MC.trace(str('p0'))[:])
        xdb.set_trace()

    # Draw a contour plot with 1 & 2 sigma errors
    #MCMC_errors.draw_contours(MC, 'p0', 'p1')

    # Save the individual distributions to a file to check convergence
    #pymc.Matplot.plot(MC)
    #xdb.set_trace()
    return MC
Exemplo n.º 2
0
    # Read and Write AbsID
    if (flg_test % 2**5) >= 2**4:
        abs_fil = '/Users/xavier/paper/LLS/Optical/Data/Analysis/MAGE/SDSSJ1004+0018_z2.746_id.fits'
        lls = LLS_System.from_absid_fil(abs_fil)
        tmpfil= '/Users/xavier/Desktop/tmp.fits'
        xdb.set_trace()
        lls.write_absid_file(tmpfil)
        lls = LLS_System.from_absid_fil(tmpfil)
        xdb.set_trace()

    # #############################
    # LLS Survey
    if (flg_test % 2**10) >= 2**9:
        print('-------------------------')
        lls = LLS_Survey('Lists/lls_metals.lst', tree=os.environ.get('LLSTREE'))
        xdb.xhist(lls.NHI, binsz=0.30)

    # LLS Survey ions
    if (flg_test % 2**11) >= 2**10:
        lls = LLS_Survey('Lists/lls_metals.lst', tree=os.environ.get('LLSTREE'))
        lls.fill_ions()
        xdb.xhist(lls.ions((6,4),skip_null=True)['clm'], binsz=0.3,
                  xlabel=r'$\log_{10} N({\rm C}^{+3})$')
        xdb.xhist(lls.ions((14,2),skip_null=True)['clm'], binsz=0.3,
                  xlabel=r'$\log_{10} N({\rm Si}^{+})$')

    # All done
    print('-------------------------')
    print('lls_utils: All done testing..')
Exemplo n.º 3
0
def run(fN_cs, fN_model, parm, email, debug=0):

    #
    pymc_list = [parm]

    # Parse data and combine as warranted
    all_NHI = []
    all_fN = []
    all_sigfN = []
    all_z = []
    flg_teff = 0
    flg_LLS = 0
    for fN_c in fN_cs: 
        # Standard f(N)
        if fN_c.fN_dtype == 'fN':
            ip = range(fN_c.data['NPT'])
            val = np.where(fN_c.data['FN'][ip] > -90)[0] # Deal with limits later
            ipv = np.array(ip)[val]
            # Append the NHI
            NHI = np.median(fN_c.data['BINS'][:,ipv],0)
            all_NHI += list(NHI)
            # Append the f(N)
            all_fN += list(fN_c.data['FN'][ipv])
            # Append the Error
            fNerror = np.median(fN_c.data['SIG_FN'][:,ipv],0)
            all_sigfN += list(fNerror)
            # Append zeval
            for ii in range(len(ipv)):
                all_z.append(fN_c.zeval)
        elif fN_c.fN_dtype == 'teff': # teff_Lya
            if flg_teff:
                raise ValueError('Only one teff allowed for now!')
            else:
                flg_teff = 1
            teff=float(fN_c.data['TEFF'])
            D_A = 1. - np.exp(-1. * teff)
            SIGDA_LIMIT = 0.1  # Allows for systemtics and b-value uncertainty
            sig_teff = np.max([fN_c.data['SIG_TEFF'], (SIGDA_LIMIT*teff)])
            teff_zeval = float(fN_c.data['Z_TEFF'])

            # Save input for later usage
            teff_input = (teff_zeval, fN_c.data['NHI_MNX'][0], fN_c.data['NHI_MNX'][1])
        elif fN_c.fN_dtype == 'l(X)': # teff_Lya
            if flg_LLS:
                raise ValueError('Only one teff allowed for now!')
            else:
                flg_LLS = 1
            LLS_lx = fN_c.data['LX']
            LLS_siglx = fN_c.data['SIG_LX']
            LLS_input = (fN_c.zeval, fN_c.data['TAU_LIM'])
            
    # 
    fN_input = (np.array(all_NHI), np.array(all_z))
    #flg_teff = 0

    #######################################
    #   Generate the Models
    #######################################

    # Define f(N) model for PyMC
    @pymc.deterministic(plot=False)
    def pymc_fn_model(parm=parm):
        # Set parameters
        fN_model.param = parm
        fN_model.model = scii.PchipInterpolator(fN_model.pivots, fN_model.param)
        #
        log_fNX = fN_model.eval( fN_input, 0. )
        #
        return log_fNX
    pymc_list.append(pymc_fn_model)

    # Define teff model for PyMC
    if flg_teff:
        @pymc.deterministic(plot=False)
        def pymc_teff_model(parm=parm):
            # Set parameters
            fN_model.param = parm
            fN_model.model = scii.PchipInterpolator(fN_model.pivots, fN_model.param)
            # Calculate teff
            model_teff = tau_eff.ew_teff_lyman(1215.6701*(1+teff_input[0]), teff_input[0]+0.1,
                                               fN_model, NHI_MIN=teff_input[1], NHI_MAX=teff_input[2])
            return model_teff
        pymc_list.append(pymc_teff_model)

    # Define l(X)_LLS model for PyMC
    if flg_LLS:
        @pymc.deterministic(plot=False)
        def pymc_lls_model(parm=parm): 
            # Set parameters 
            fN_model.param = parm
            fN_model.model = scii.PchipInterpolator(fN_model.pivots, fN_model.param)
            # Calculate l(X)
            lX = fN_model.calc_lox(LLS_input[0], 
                                    17.19+np.log10(LLS_input[1]), 22.) 
            return lX
        pymc_list.append(pymc_lls_model)

    #######################################
    #   Generate the Data
    #######################################

    # Define f(N) data for PyMC
    fNvalue=np.array(all_fN)
    pymc_fN_data = pymc.Normal(str('fNdata'), mu=pymc_fn_model, tau=1.0/np.array(all_sigfN)**2,
                               value=fNvalue, observed=True)
    pymc_list.append(pymc_fN_data)

    # Define teff data for PyMC
    if flg_teff:
        pymc_teff_data = pymc.Normal(str('teffdata'), mu=pymc_teff_model, tau=1.0/np.array(sig_teff)**2,
                                value=teff, observed=True)
        pymc_list.append(pymc_teff_data)

    # Define l(X)_LLS model for PyMC
    if flg_LLS:
        pymc_lls_data = pymc.Normal(str('LLSdata'), mu=pymc_lls_model, tau=1.0/np.array(LLS_siglx)**2,
                                value=LLS_lx, observed=True)
        pymc_list.append(pymc_lls_data)


    #######################################
    #   RUN THE MCMC
    #######################################


    MC = pymc.MCMC(pymc_list)#,verbose=2)
    # Force step method to be Metropolis!
    for ss in MC.stochastics-MC.observed_stochastics:
        MC.use_step_method(pymc.Metropolis, ss, proposal_sd=0.025, proposal_distribution='Normal')
    #xdb.set_trace()

    # Run a total of 40000 samples, but ignore the first 10000.
    # Verbose just prints some details to screen.
    #xdb.set_trace()
    #MC.sample(20000, 3000, verbose=2, tune_interval=500)
    MC.sample(2000, 300, verbose=2, tune_interval=200)
    #MC.isample(10000, 1000, verbose=2)

    #######################################
    #   PRINT THE RESULTS
    #######################################
    t = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    f = open(email + t + 'asciifile', 'w+')
    # Print the best values and their errors
    best_pval = print_errors(MC)
    f.write(best_pval)
    
    fN_model.param = best_pval
    fN_model.model = scii.PchipInterpolator(fN_model.pivots, fN_model.param)
    if debug:
        xifd.tst_fn_data(fN_model=fN_model)
        xdb.xhist(MC.trace(str('p0'))[:])
        xdb.set_trace()