Exemplo n.º 1
0
def mcmc(Chain, runtime, MarkovChain, mixingtime=1000, stepsize=1, seed=0 ):
    pb = progressBar(maxValue = runtime + mixingtime)
    cwd=os.getcwd()
    tmpdir = cwd+'/.'+uniquename()
    if not tmpdir == None:
        if os.path.exists(tmpdir):
            os.chdir(tmpdir)
        else:
            os.mkdir(tmpdir)
            os.chdir(tmpdir)
    os.system('cp %s/%s %s/%s' % (cwd, parfile, tmpdir, parfile))
    os.system('cp %s/%s %s/%s' % (cwd, pulsefile, tmpdir, pulsefile))
    motifile(toafile, cwd, tmpdir)
    touchparfile(parfile, NITS=1)
    pf = PARfile(parfile)
    #chisq, dof = tempofit(parfile, toafile = toafile, pulsefile = pulsefile)
    pf.matrix(toafile)
    pf.freezeall()
    #pf.thawall('JUMP_')
    #pf.write()
    #plist = [x for x in pf.manifest if x in pf.parameters.keys() ]
    #dit = {'BEST':[pf.__dict__[p][0] for p in plist] + [ chisq], 'parfile':pf.parfile, 'parameters':plist + [ 'chisq']}
    #pickle.dump(dit, open('%s/bestpar.p' % cwd, 'w', 0), protocol=2)
    p0 = probcal(pf)
    pmax = p0
    ThisChain = []
    c = 0
    randomlist = uniform(0,1,size=runtime)
    while c <= mixingtime + runtime - 1:
        c+=1
        npf = pf.randomnew(stepsize=stepsize)
        #randomnew(npf, stepsize) #only use this for 1713
        p1 = probcal(npf)
        if c % 30 == 0:pb(c)
        if c > mixingtime:
            t = randomlist[c-mixingtime-1]
            if t < exp(p1-p0):
                Chain.Chain.append([npf.__dict__[p][0] for p in plist] + [ npf.chisq])
                pf = npf
                p0 = p1
                if p1 > pmax:
                    pmax = p1
                    bestpar['BEST'] = [npf.__dict__[p][0] for p in plist] + [ npf.chisq]
                    pickle.dump(bestpar, open('%s/bestpar.p' % cwd, 'wb', 0), protocol=2)
            else:
                Chain.Chain.append([pf.__dict__[p][0] for p in plist] + [ npf.chisq])

            if c % (100+(seed%100)) == 0:
                MarkovChain.extend(Chain.Chain)
                ThisChain.extend(Chain.Chain)
                Chain.Chain = [] #empty the list
                #try:
                TC = np.array(ThisChain)
                dit = {'Chain':TC}
                pid = str(os.getpid())
                try: 
                    os.remove(cwd+'/MChain.p'+pid)
                except:pass
                f = open(cwd+'/MChain.p'+pid, 'wb', 0)
                pickle.dump(dit, f, protocol=2)
                f.flush()
                f.close()
                del dit
                del TC

    MarkovChain.extend(Chain.Chain)
    os.chdir(cwd)
Exemplo n.º 2
0
    print options

    parfile = options.parfile
    toafile = options.toafile
    pulsefile = options.pulsefile
    steps = options.steps
    mixing = options.mixing
    rseed = options.seed
    stepsize = options.stepsize
    px = options.paral
    pf =PARfile(parfile)
    #pf = model(parfile)
    pf.freezeall()
    pf.thawall('JUMP_')
    pf.write('mcmc.par')
    touchparfile('mcmc.par', NITS=1)
    chisq, dof = tempofit('mcmc.par', toafile = toafile, pulsefile = pulsefile)
    #pf.tempofit(TOAfile(toafile), pulsefile = pulsefile)
    smallestchisq = chisq
    #print 'smallestchisq', smallestchisq, dof

    cwd=os.getcwd()
    plist = [x for x in pf.manifest if x in pf.parameters.keys() if not x.startswith('DMX') and not x.startswith('JUMP')]

    bestpar = {'BEST':[pf.__dict__[p][0] for p in plist] + [chisq], 'parfile':pf.parfile, 'parameters':plist + ['chisq']}
    pickle.dump(bestpar, open('%s/bestpar.p' % cwd, 'w', 0), protocol=2)
    



    def run(argv):