Beispiel #1
0
def run_burnin(sampler,startPos,nSteps,storechain=False):
    iStep = 0
    bar = ProgressBar()
    for pos, prob, state in sampler.sample(startPos,iterations=nSteps,storechain=storechain):
        bar.render(int(100*iStep/nSteps),'running Burn In')
        iStep += 1
    return pos, prob, state
Beispiel #2
0
def run_ptmcmc_save(sampler, startPos, nSteps, file, **kwargs):
    '''runs PT MCMC and saves zero temperature chain to file'''
    if not os.path.exists(file):
        f = open(file, "w")
        f.close()

    iStep = 0
    bar = ProgressBar()
    for pos, prob, like in sampler.sample(startPos,
                                          iterations=nSteps,
                                          storechain=True,
                                          **kwargs):
        bar.render(int(100 * iStep / nSteps), 'running MCMC')
        iStep += 1
        f = open(file, "a")
        # pos is shape (ntemps, nwalkers, npars)
        # prob is shape (ntemps, nwalkers)
        # loop over all walkers for zero temp and append to file
        zpos = pos[0, ...]
        zprob = prob[0, ...]
        for k in range(zpos.shape[0]):
            thisPos = zpos[k]
            thisProb = zprob[k]
            f.write("{0:4d} {1:s} {2:f}\n".format(k,
                                                  " ".join(map(str, thisPos)),
                                                  thisProb))
    f.close()
    return sampler
Beispiel #3
0
def run_mcmc_save(sampler, startPos, nSteps, rState, file, **kwargs):
    '''runs and MCMC chain with emcee, and saves steps to a file'''
    #open chain save file
    if file:
        f = open(file, "w")
        f.close()
    iStep = 0
    bar = ProgressBar()
    for pos, prob, state in sampler.sample(startPos,
                                           iterations=nSteps,
                                           rstate0=rState,
                                           storechain=True,
                                           **kwargs):
        if file:
            f = open(file, "a")
        bar.render(int(100 * iStep / nSteps), 'running MCMC')
        iStep += 1
        for k in range(pos.shape[0]):
            # loop over all walkers and append to file
            thisPos = pos[k]
            thisProb = prob[k]
            if file:
                f.write("{0:4d} {1:s} {2:f}\n".format(
                    k, " ".join(map(str, thisPos)), thisProb))
        if file:
            f.close()
    return sampler
Beispiel #4
0
def run_burnin(sampler, startPos, nSteps, storechain=False):
    iStep = 0
    bar = ProgressBar()
    for pos, prob, state in sampler.sample(startPos,
                                           iterations=nSteps,
                                           storechain=storechain):
        bar.render(int(100 * iStep / nSteps), 'running Burn In')
        iStep += 1
    return pos, prob, state
Beispiel #5
0
def run_mcmc_save(sampler,startPos,nSteps,rState,file,**kwargs):
    '''runs and MCMC chain with emcee, and saves steps to a file'''
    #open chain save file
    if file:
        f = open(file,"w")
        f.close()
    iStep = 0
    bar = ProgressBar()
    for pos, prob, state in sampler.sample(startPos,iterations=nSteps,rstate0=rState,storechain=True,**kwargs):
        if file:
            f = open(file,"a")
        bar.render(int(100*iStep/nSteps),'running MCMC')
        iStep += 1
        for k in range(pos.shape[0]):
            # loop over all walkers and append to file
            thisPos = pos[k]
            thisProb = prob[k]
            if file:
                f.write("{0:4d} {1:s} {2:f}\n".format(k," ".join(map(str,thisPos)),thisProb ))
        if file:        
            f.close()
    return sampler
Beispiel #6
0
def run_ptmcmc_save(sampler,startPos,nSteps,file,**kwargs):
    '''runs PT MCMC and saves zero temperature chain to file'''
    if not os.path.exists(file):
        f = open(file,"w")
        f.close()

    iStep = 0    
    bar = ProgressBar()
    for pos, prob, like in sampler.sample(startPos,iterations=nSteps,storechain=True,**kwargs):
        bar.render(int(100*iStep/nSteps),'running MCMC')
        iStep += 1
        f = open(file,"a")
        # pos is shape (ntemps, nwalkers, npars)
        # prob is shape (ntemps, nwalkers)
        # loop over all walkers for zero temp and append to file
        zpos = pos[0,...]
        zprob = prob[0,...]
        for k in range(zpos.shape[0]):
            thisPos = zpos[k]
            thisProb = zprob[k]
            f.write("{0:4d} {1:s} {2:f}\n".format(k," ".join(map(str,thisPos)),thisProb ))
    f.close()
    return sampler    
    # white dwarf temp
    twdVals = np.random.normal(loc=args.twd,scale=args.e_twd,size=chainLength)*units.K
    # period
    pVals = np.random.normal(loc=args.p,scale=args.e_p,size=chainLength)*units.d

    # loop over the MCMC chain, calculating system parameters as we go
    
    # table for results
    results = Table(names=('q','Mw','Rw','Mr','Rr','a','Kw','Kr','incl'))
    # need to be a little careful about astropy versions here, since only
    # versions >=1.0 allow quantities in tables
    # function below extracts value from quantity and floats alike
    getval = lambda el: getattr(el,'value',el) 
            
    psolve = partial(solve,baseDir=baseDir)
    data = zip(qVals,dphiVals,rwVals,twdVals,pVals)
    solvedParams = PB.map(psolve,data,multiprocess=True)
    
    print 'Writing out results...'
    # loop over these results and put all the solutions in our results table
    iStep = 0    
    bar = ProgressBar()
    for thisResult in solvedParams:
        bar.render(int(100*iStep/(len(solvedParams))),'Combining data')
        iStep += 1
        if thisResult is not None:
            results.add_row(thisResult)      

    print 'Found solutions for %d percent of samples in MCMC chain' % (100*float(len(results))/float(chainLength))
    results.write('physicalparams.log',format='ascii.commented_header')