Example #1
0
def sampler_emcee(D):
    # nlinks: number of iterations for each walker
    # please specify as even
    D['nlinks'] = 100

    # nwalkers: number of interacting chains
    D['nwalkers'] = 200

    # ntemps:
    D['ntemps'] = 5

    # ntune: number of initialization steps to discard
    D['ntune'] = 500

    st = time.time()

    # identify starting positions for chains
    tmp = start_pos(D['ntemps'] * D['nwalkers'], D)
    pos0 = tmp.reshape((D['ntemps'], D['nwalkers'], D['dim']))

    likelihood = D['likelihood']

    # run MCMC for the model of interest
    sampler = PTSampler(ntemps=D['ntemps'],
                        nwalkers=D['nwalkers'],
                        dim=len(D['pname']),
                        logl=likelihood,
                        logp=prior,
                        loglargs=(D, ),
                        logpargs=(D, ))

    for pos, lnprob, lnlike in sampler.sample(pos0, iterations=D['ntune']):
        pass
    burntrace = sampler.chain
    sampler.reset()

    print(burntrace.shape)
    print("burnin completed")

    for pos, lnprob, lnlike in sampler.sample(pos, iterations=D['nlinks']):
        pass
    finaltrace = sampler.chain
    print(finaltrace.shape)

    D['rawtrace'] = np.concatenate((burntrace[0, ...], finaltrace[0, ...]),
                                   axis=1)

    D['sampling_time'] = np.round(time.time() - st, 2)

    st = time.time()

    D['lnZ'], D['dlnZ'] = sampler.thermodynamic_integration_log_evidence()

    elaps = np.round(time.time() - st, 2)
    msg = "model evidence evaluation: " + str(elaps) + " seconds"
    WP(msg, D['wrt_file'])

    return D
Example #2
0
# Load in samples from previous mcmc chain, and run any models
# that weren't generated.
p = np.load(maindir + 'p_frommcmc.npy')

##################################################################
##################### Continue After Burn-in #####################
##################################################################

chainfn = maindir + "chain.dat"
tempsfn = maindir + "temps.dat"
f = open(chainfn, "w")
f.close()
g = open(tempsfn, "w")
g.close()

for p, lnprob, lnlike in sampler.sample(p, iterations=niter, storechain=True):
    position = p[0]
    position_temp = p[1]
    f = open(chainfn, "a")
    g = open(tempsfn, "a")
    for k in range(position.shape[0]):
        f.write("{0:4d} {1:s}\n".format(k, " ".join(str(position[k]))))
        g.write("{0:4d} {1:s}\n".format(k, " ".join(str(position_temp[k]))))
    f.close()
    g.close()

position = p
np.save('p_mcmc', position)

print "MCMC Complete"
samples = sampler.chain
Example #3
0
w0 = np.random.uniform(73.0,80.0,size=(ntemps,nwalkers))
w1 = np.random.uniform(10,20,size=(ntemps,nwalkers))
w2 = np.random.uniform(0.0001,0.001,size=(ntemps,nwalkers))
w3 = np.random.uniform(100.0,3000.0,size=(ntemps,nwalkers))
w4 = np.random.uniform(-2.0,0.0,size=(ntemps,nwalkers))
w5 = np.random.uniform(1.2,1.4,size=(ntemps,nwalkers))
w6 = np.random.uniform(0.4,0.6,size=(ntemps,nwalkers))

p0 = np.dstack((w0,w1,w2,w3,w4,w5,w6))

niter = 400
nburn = np.int(0.2*niter)


for p, lnprob, lnlike in sampler.sample(p0, iterations=nburn):
    pass

sampler.reset()

print 'Burn in complete'


"""
#Parallelize after this step. Look into whether it's better to 
#vary number of walkers or number of steps across different machines.
"""


for p, lnprob, lnlike in sampler.sample(p, lnprob0=lnprob,
                                        lnlike0=lnlike,
Example #4
0
class EMPIRE:
    def __init__(self, stardat, setup):
        assert len(stardat) >= 1, 'stardat has to contain at least 1 file ! !'
        assert len(setup) == 3, 'setup has to be [ntemps, nwalkers, nsteps]'
        #  Setup
        self.cores = multiprocessing.cpu_count()
        self.setup = setup
        self.ntemps, self.nwalkers, self.nsteps = setup
        self.betas = None
        self.MOAV = 1

        self.burn_out = self.nsteps // 2
        self.PM = False
        #  Reading data

        if len(stardat.shape) > 1:
            # RV
            self.rvfiles = stardat[0]

            rvdat = emplib.read_data(self.rvfiles)
            self.time, self.rv, self.err, self.ins = rvdat[0]  # time, radial velocities, error and instrument flag
            self.all_data = rvdat[0]
            self.staract, self.starflag = rvdat[1], rvdat[2]  # time, star activity index and flag
            self.totcornum = rvdat[3]  # quantity if star activity indices

            self.nins = len(self.rvfiles)  # number of instruments autodefined
            self.ndat = len(self.time)  # number of datapoints

            # PM
            self.pmfiles = stardat[1]
            pmdat = emplib.read_data(self.pmfiles)
            self.time_pm, self.rv_pm, self.err_pm, self.ins_pm = pmdat[0]  # just the fname its pm rly
            self.all_data_pm = pmdat[0]
            self.staract_pm, self.starflag_pm = pmdat[1], pmdat[2]  # time, star activity index and flag
            self.totcornum_pm = pmdat[3]  # ?

            self.nins_pm = len(self.pmfiles)
            self.ndat_pm = len(self.time_pm)
            self.MOAV_pm = 1  # for flat model
            self.fsig = 1
            self.f2k = sp.array([0, 0])
            self.PM = True

            self.params_pm = sp.array([1, 2, 3, 4])
            self.lenppm = len(self.params_pm)

            self.PACC_pm = False

        else:  # RV

            self.rvfiles = stardat
            rvdat = emplib.read_data(stardat)
            self.time, self.rv, self.err, self.ins = rvdat[0]  # time, radial velocities, error and instrument flag
            self.all_data = rvdat[0]
            self.staract, self.starflag = rvdat[1], rvdat[2]  # time, star activity index and flag
            self.totcornum = rvdat[3]  # quantity if star activity indices

            self.nins = len(self.rvfiles)  # number of instruments autodefined
            self.ndat = len(self.time)  # number of datapoints

            # PM
            self.time_pm, self.rv_pm, self.err_pm, self.ins_pm = 0., 0., 0., 0.
            self.totcornum_pm = 0.

        #  Statistical Tools
        self.bayes_factor = sp.log(150)  # inside chain comparison (smaller = stricter)
        self.model_comparison = 5
        self.BIC = 5
        self.AIC = 5

        #  Menudencies
        self.thin = 1
        self.PLOT = True
        self.draw_every_n = 1
        self.PNG = True
        self.PDF = False
        self.CORNER = True
        self.HISTOGRAMS = True
        self.starname = self.rvfiles[0].split('_')[0]
        self.MUSIC = False
        self.breakFLAG = False
        self.STARMASS = False
        self.HILL = False
        self.CHECK = False
        self.RAW = False
        '''
        self.CORNER_MASK = True
        self.CORNER_K = True
        self.CORNER_I =
        '''

        # About the search parameters
        self.PACC = False  # parabolic Acceleration

        self.CONSTRAIN = True
        self.eccprior = 0.3
        self.jittprior = 5.0
        self.jittmean = 5.0

        self.sampler = 0.0
        ########################################
        # los IC sirven para los mod_lims? NO!
        # mod_lims sólo acotan el espacio donde buscar, excepto para periodo
        pass


    def mklogfile(self, *args):
        if args:
            theta_max, best_post, sample_sizes = args[0], args[1], args[2]
            sigmas, kplanets, modlims = args[3], args[4], args[5]
            BIC, AIC, alt_res = args[6], args[7], args[8]
            START, residuals = args[9], args[10]



        dayis = dt.date.today()  # This is for the folder name
        def ensure_dir(date='datalogs/'+self.starname+'/'+str(dayis.month)+'.'+str(dayis.day)+'.'+str(dayis.year)[2:]):
            if not os.path.exists(date):
                os.makedirs(date)
                return date
            else:
                if len(date.split('_')) == 2:
                    aux = int(date.split('_')[1]) + 1
                    date = date.split('_')[0]+'_'+str(aux)
                else:
                    date = date + '_1'
            return ensure_dir(date)

        def timer():
            timing = chrono.time() - START
            #insec = sp.array([604800, 86400, 3600, 60])
            weeks, rest0 = timing // 604800, timing % 604800
            days, rest1 = rest0 // 86400, rest0 % 86400
            hours, rest2 = rest1 // 3600, rest1 % 3600
            minutes, seconds = rest2 // 60, rest2 % 60
            if weeks == 0:
                if days == 0:
                    if hours == 0:
                        if minutes == 0:
                            return '%i seconds' % seconds
                        else:
                            return '%i minutes and %i seconds' % (minutes, seconds)
                    else:
                        return '%i hours, %i minutes and %i seconds' % (hours, minutes, seconds)
                else:
                    return '%i days, %i hours, %i minutes and %i seconds' % (days, hours, minutes, seconds)
            else:
                return '%i weeks, %i days, %i hours, %i minutes and %i seconds' % (weeks, days, hours, minutes, seconds)

        def mklogdat(theta, best_post):
            G = 39.5
            days_in_year = 365.242199
            sigmas_hen = sigmas
            logdat = '\nStar Name                         : '+self.starname
            for i in range(self.nins):
                if i==0:
                    logdat += '\nUsed datasets                     : '+self.rvfiles[i]
                else:
                    logdat += '\n                                  : '+self.rvfiles[i]
            logdat += '\n--------------------------------------------------------------------'
            logdat += '\nThe sample sizes are        :    ' + str(sample_sizes)
            logdat += '\nThe maximum posterior is    :    ' + str(best_post)
            logdat += '\nThe BIC is                  :    ' + str(BIC)
            logdat += '\nThe AIC is                  :    ' + str(AIC)
            logdat += '\nThe RMS is                  :    ' + str(sp.sum(residuals**2))
            logdat += '\nThe most probable chain values are as follows...'
            for i in range(kplanets):
                #MP = sp.sqrt(1 - theta[i*5+4] ** 2) * theta[i*5] * theta[i*5+1] ** (1/3.) * STARMASS ** (2/3.) / 203.
                #SMA = (((theta[i*5+1]/365.242199) ** 2) * G * STARMASS0 / (4 * sp.pi ** 2)) ** (1/3.)
                if self.STARMASS:
                    SMA = (((theta[i*5]*24.*3600.)**2.0) / ( (4.0*sp.pi**2.0) / (6.67e-11 * self.STARMASS * 1.99e30) ))**(1./3) / 1.49598e11
                    MP = theta[i*5+1] / ( (28.4/sp.sqrt(1. - theta[i*5+4]**2.)) * (self.STARMASS**(-0.5)) * (SMA**(-0.5)) ) * 317.8

                logdat += '\n--------------------------------------------------------------------'
                logdat += '\nPeriod   '+str(i+1)+'[days] :   ' + str(theta[i*5]) + ' +- ' + str(sigmas_hen[i*5])
                logdat += '\nAmplitude  '+str(i+1)+'[m/s]:   ' + str(theta[i*5+1]) + ' +- ' + str(sigmas_hen[i*5+1])
                logdat += '\nPhase   '+str(i+1)+'        :   ' + str(theta[i*5+2]) + ' +- ' + str(sigmas_hen[i*5+2])
                logdat += '\nLongitude   '+str(i+1)+'    :   ' + str(theta[i*5+3]) + ' +- ' + str(sigmas_hen[i*5+3])
                logdat += '\nEccentricity   '+str(i+1)+' :   ' + str(theta[i*5+4]) + ' +- ' + str(sigmas_hen[i*5+4])
                if self.STARMASS:
                    logdat += '\nMinimum Mass   '+str(i+1)+' :   ' + str(MP)
                    logdat += '\nSemiMajor Axis '+str(i+1)+' :   ' + str(SMA)
            logdat += '\n--------------------------------------------------------------------'
            logdat += '\nAcceleration [m/s/(year)]:'+str(theta[5*kplanets]/(days_in_year*24*60*60)) + ' +- ' + str(sigmas_hen[5*kplanets]/(days_in_year*24*60*60))
            if self.PACC:
                logdat += '\nQuadratic Acceleration [m/s/(year)]:'+str(theta[5*kplanets + self.PACC]/(days_in_year*24*60*60)) + ' +- ' + str(sigmas_hen[5*kplanets + self.PACC]/(days_in_year*24*60*60))

            for i in range(self.nins):
                logdat += '\n--------------------------------------------------------------------'
                logdat += '\nJitter '+str(i+1)+'    [m/s]:   ' + str(theta[5*kplanets + i*2*(self.MOAV+1) + self.PACC + 1]) + ' +- ' + str(sigmas_hen[5*kplanets + i*2*(self.MOAV+1) + self.PACC + 1])
                logdat += '\nOffset '+str(i+1)+'    [m/s]:   ' + str(theta[5*kplanets + i*2*(self.MOAV+1) + self.PACC + 2]) + ' +- ' + str(sigmas_hen[5*kplanets + i*2*(self.MOAV+1) + self.PACC + 2])
                for j in range(self.MOAV):
                    logdat += '\nMA coef '+str(i+1)+'_'+str(j+1)+'        : ' + str(theta[5*kplanets + i*2*(self.MOAV+1) + 2*(j+1) + 1 + self.PACC]) + ' +- ' + str(sigmas_hen[5*kplanets + i*2*(self.MOAV+1) + 2*(j+1) + 1 + self.PACC])
                    logdat += '\nTimescale '+str(i+1)+'_'+str(j+1)+'[days]: ' + str(theta[5*kplanets + i*2*(self.MOAV+1) + 2*(j+1) + 2 + self.PACC]) + ' +- ' + str(sigmas_hen[5*kplanets + i*2*(self.MOAV+1) + 2*(j+1) + 2 + self.PACC])
            for h in range(self.totcornum):
                logdat += '\n--------------------------------------------------------------------'
                logdat += '\nStellar Activity'+str(h+1)+':   ' + str(theta[5*kplanets + self.nins*2*(self.MOAV+1) + self.PACC + 1 + h]) + ' +- ' + str(sigmas_hen[5*kplanets + self.nins*2*(self.MOAV+1) + self.PACC + 1 + h])

            if self.PM:
                if kplanets > 0:
                    for i in range(self.fsig):
                        ndim_rv = 5*kplanets + self.nins*2*(self.MOAV+1) + self.PACC + 1 + self.totcornum
                        logdat += '\n--------------------------------------------------------------------'
                        for ii in range(self.lenppm):  # BATMAN PARAMS
                            logdat += '\nParam_pm'+str(i+1)+str(ii+1)+'[m/s]:   ' + str(theta[ndim_rv + i*self.lenppm + ii]) + ' +- ' + str(sigmas_hen[ndim_rv + i*self.lenppm + ii])

                    logdat += '\n--------------------------------------------------------------------'
                    fdim = ndim_rv + self.lenppm*self.fsig
                '''
                else:
                    logdat += '\nAcceleration [m/s/(year)]:'+str(theta[ndim_rv]/(days_in_year*24*60*60)) + ' +- ' + str(sigmas_hen[ndim_rv]/(days_in_year*24*60*60))
                    for i in range(self.nins_pm):
                        logdat += '\n--------------------------------------------------------------------'
                        logdat += '\nJitter_pm '+str(i+1)+' [m/s]:   ' + str(theta[ndim_rv + i*2*(self.MOAV_pm+1) + self.PACC_pm + 1]) + ' +- ' + str(sigmas_hen[ndim_rv + i*2*(self.MOAV_pm+1) + self.PACC_pm + 1])
                        logdat += '\nOffset_pm '+str(i+1)+' [m/s]:   ' + str(theta[ndim_rv + i*2*(self.MOAV_pm+1) + self.PACC_pm + 2]) + ' +- ' + str(sigmas_hen[ndim_rv + i*2*(self.MOAV_pm+1) + self.PACC_pm + 2])
                        for j in range(self.MOAV_pm):
                            logdat += '\nMA coef '+str(i+1)+'_'+str(j+1)+'        : ' + str(theta[ndim_rv + i*2*(self.MOAV_pm+1) + 2*(j+1) + 1 + self.PACC_pm]) + ' +- ' + str(sigmas_hen[ndim_rv + i*2*(self.MOAV_pm+1) + 2*(j+1) + 1 + self.PACC_pm])
                            logdat += '\nTimescale '+str(i+1)+'_'+str(j+1)+'[days]: ' + str(theta[ndim_rv + i*2*(self.MOAV_pm+1) + 2*(j+1) + 2 + self.PACC_pm]) + ' +- ' + str(sigmas_hen[ndim_rv + i*2*(self.MOAV_pm+1) + 2*(j+1) + 2 + self.PACC_pm])
                '''

            logdat += '\n------------------------------ RV DATA ------------------------------'
            logdat += '\nTemperatures, Walkers, Steps      : '+str((self.ntemps, self.nwalkers, self.nsteps))
            logdat += '\nN Instruments, K planets, N data  : '+str((self.nins, kplanets, self.ndat))
            logdat += '\nNumber of Dimensions              : '+str(5 * kplanets + self.nins*2*(self.MOAV+1) + self.totcornum + self.PACC + 1)
            logdat += '\nN Moving Average                  : '+str(self.MOAV)
            logdat += '\nBeta Detail                       : '+str(self.betas)
            logdat += '\n--------------------------------------------------------------------'
            if self.PM:
                logdat += '\n------------------------------ PM DATA ------------------------------'
                logdat += '\nN Instruments, N signals, N data  : '+str((self.nins_pm, self.fsig, self.ndat_pm))
                if kplanets > 0:
                    ndim_rv = 5*kplanets + self.nins*2*(self.MOAV+1) + self.PACC + 1 + self.totcornum
                    logdat += '\nNumber of Dimensions              : '+str(ndim_rv + self.fsig*self.lenppm)
                else:
                    pass
                #logdat += '\nN Moving Average                  : '+str(self.MOAV_pm)
                #logdat += '\nBeta Detail                       : '+str(self.betas)
                logdat += '\n--------------------------------------------------------------------'


            logdat += '\nRunning Time                      : '+timer()
            print(logdat)
            logdat += '\n -------------------------- ADVANCED --------------------------'
            logdat += '\n raw fit'
            logdat += '\n'+str(theta)
            logdat += '\n raw boundaries'
            logdat += '\n '+ str(modlims)
            logdat += '\n alt_res'
            logdat += '\n '+ str(alt_res)
            return logdat


        name = str(ensure_dir())
        logdat = mklogdat(theta_max, best_post)
        sp.savetxt(name+'/log.dat', sp.array([logdat]), fmt='%100s')
        sp.savetxt(name+'/residuals.dat', sp.c_[self.time, residuals])
        return name


    def instigator(self, chain, post, saveplace, kplanets):
        '''
        Automatically saves chains and posteriors.
        '''

        def mk_header(kplanets):
            h = []
            kepler = ['Period                  ', 'Amplitude               ', 'Phase                   ', 'Longitude               ', 'Eccentricity            ', 'Minimum Mass            ', 'SemiMajor Axis          ']
            telesc = ['Jitter                  ', 'Offset                  ']
            mov_ave = ['MA Coef ', 'Timescale ']

            photo = ['param']

            for i in range(kplanets):
                for item in kepler:
                    h.append(item)
            if self.PACC:
                h.append('Linear Acceleration     ')
                h.append('Quadratic Acceleration  ')
            else:
                h.append('Acceleration            ')
            for j in range(self.nins):
                for item in telesc:
                    h.append(item)
                    for c in range(self.MOAV):
                        h.append(mov_ave[0]+str(c)+'               ')
                        h.append(mov_ave[1]+str(c)+'             ')
            for jj in range(self.totcornum):
                h.append('Stellar Activity'+str(jj))

            if self.PM:
                for k in range(self.nins_pm):
                    for kk in range(self.lenppm):
                        h.append('Photometry Parameter '+str(k)+'_'+str(kk))
            h = ' '.join(h)
            return h

        def savechain(chain):
            for i in range(self.ntemps):
                sp.savetxt(saveplace + '/chain_'+str(i)+'.dat', chain[i], header=mk_header(kplanets))
            pass
        def savepost(post):
            for i in range(self.ntemps):
                sp.savetxt(saveplace + '/posterior_'+str(i)+'.dat', post[i], header=mk_header(kplanets))
            pass
        savechain(chain)
        savepost(post)
        pass


    def alt_results(self, samples, kplanets):
        titles = sp.array(["Period","Amplitude","Longitude", "Phase","Eccentricity", 'Acceleration', 'Jitter', 'Offset', 'MACoefficient', 'MATimescale', 'Stellar Activity'])
        namen = sp.array([])
        ndim = kplanets * 5 + self.nins*2*(self.MOAV+1) + self.totcornum + 1 + self.PACC

        RESU = sp.zeros((ndim, 5))
        for k in range(kplanets):
            namen = sp.append(namen, [titles[i] + '_'+str(k) for i in range(5)])
        namen = sp.append(namen, titles[5])  # for acc
        if self.PACC:
            namen = sp.append(namen, 'Parabolic Acceleration')
        for i in range(self.nins):
            namen = sp.append(namen, [titles[ii] + '_'+str(i+1) for ii in sp.arange(2)+6])
            for c in range(self.MOAV):
                namen = sp.append(namen, [titles[ii] + '_'+str(i+1) + '_'+str(c+1) for ii in sp.arange(2)+8])
        for h in range(self.totcornum):
            namen = sp.append(namen, titles[-1]+'_'+str(h+1))

        if self.PM:
            for g in range(self.nins_pm):
                for gg in range(self.lenppm):
                    namen = sp.append(namen, 'Photometry param'+str(g)+'_'+str(gg+1))

        alt_res = list(map(lambda v: (v[2], v[3]-v[2], v[2]-v[1], v[4]-v[2], v[2]-v[0]),
                      zip(*np.percentile(samples, [2, 16, 50, 84, 98], axis=0))))
        logdat = '\nAlternative results with uncertainties based on the 2nd, 16th, 50th, 84th and 98th percentiles of the samples in the marginalized distributions'
        logdat = '\nFormat is like median +- 1-sigma, +- 2-sigma'
        for res in range(ndim):
            logdat += '\n'+namen[res]+'     : '+str(alt_res[res][0])+' +- '+str(alt_res[res][1:3]) +'    2%   +- '+str(alt_res[res][3:5])
            RESU[res] = sp.percentile(samples, [2, 16, 50, 84, 98], axis=0)[:, res]
        print(logdat)
        return RESU


    def MCMC(self, *args):
        if args:
            #kplan, mod_lims, ins_lims, acc_lims, sigmas_raw, pos0, logl, logp
            kplanets, boundaries, inslims = args[0], args[1], args[2]
            acc_lims, sigmas_raw, pos0 = args[3], args[4], args[5]
            logl, logp = args[6], args[7]
        ndim = 1 + 5 * kplanets + self.nins*2*(self.MOAV+1) + self.totcornum + self.PACC
        print(str(self.PM)), 'self.pm!!'  # PMPMPM
        if kplanets > 0:
            if self.PM:
                pm_lims = args[8]
                ndim += self.lenppm*self.fsig
                print('checkpoint 1')  # PMPMPM
        ndat = len(self.time)
        def starinfo():
            colors = ['red', 'green', 'blue', 'yellow', 'grey', 'magenta', 'cyan', 'white']
            c = sp.random.randint(0,7)
            print(colored('\n    ###############################################', colors[c]))
            print(colored('    #                                             #', colors[c]))
            print(colored('    #                                             #', colors[c]))
            print(colored('    #                 E M P E R 0 R               #', colors[c]))
            print(colored('    #                                             #', colors[c]))
            print(colored('    #                                             #', colors[c]))
            print(colored('    ###############################################', colors[c]))
            print(colored('Exoplanet Mcmc Parallel tEmpering Radial vel0city fitteR', colors[sp.random.randint(0,7)]))
            logdat = '\n\nStar Name                         : '+self.starname
            logdat += '\nTemperatures, Walkers, Steps      : '+str((self.ntemps, self.nwalkers, self.nsteps))
            logdat += '\nN Instruments, K planets, N data  : '+str((self.nins, kplanets, self.ndat))
            if self.PM:
                logdat += '\nN of data for Photometry          : '+str(self.ndat_pm)
            logdat += '\nN Number of Dimensions            : '+str(ndim)
            logdat += '\nN Moving Average                  : '+str(self.MOAV)
            logdat += '\nBeta Detail                       : '+str(self.betas)
            logdat += '\n-----------------------------------------------------'
            print(logdat)
            pass

        starinfo()
        #'''
        #from emperors_library import logp_rv
        print(str(self.PM), ndim, 'self.pm y ndim')  # PMPMPM
        if self.PM:
            if kplanets > 0:
                logp_params = sp.array([sp.array([self.time, kplanets, self.nins, self.MOAV,
                                        self.totcornum, boundaries, inslims, acc_lims,
                                        sigmas_raw, self.eccprior, self.jittprior,
                                        self.jittmean, self.STARMASS, self.HILL,
                                        self.PACC, self.CHECK]),
                               sp.array([self.time_pm, self.fsig, self.lenppm,
                                         self.nins_pm, self.MOAV_pm,
                                         self.totcornum_pm, boundaries, sigmas_raw,
                                         self.PACC_pm])])

                logl_params = sp.array([sp.array([self.time, self.rv, self.err, self.ins,
                                        self.staract, self.starflag, kplanets, self.nins,
                                        self.MOAV, self.totcornum, self.PACC]),
                               sp.array([self.time_pm, self.rv_pm, self.err_pm, self.ins_pm,
                                        self.staract_pm, self.starflag_pm, self.fsig,
                                        self.f2k, self.nins_pm, self.MOAV_pm,
                                        self.totcornum_pm, self.PACC_pm, kplanets])])
                self.sampler = PTSampler(self.ntemps, self.nwalkers, ndim, logl, logp,
                                         loglargs=[empmir.logl_rvpm, logl_params],
                                         logpargs=[empmir.logp_rvpm, logp_params],
                                         threads=self.cores, betas=self.betas)
                #raise ImportError('xd dale al debug mejor')
            else:
                logp_params = sp.array([self.time, kplanets, self.nins, self.MOAV,
                                        self.totcornum, boundaries, inslims, acc_lims,
                                        sigmas_raw, self.eccprior, self.jittprior,
                                        self.jittmean, self.STARMASS, self.HILL,
                                        self.PACC, self.CHECK])
                logl_params = sp.array([self.time, self.rv, self.err, self.ins,
                                        self.staract, self.starflag, kplanets, self.nins,
                                        self.MOAV, self.totcornum, self.PACC])
                self.sampler = PTSampler(self.ntemps, self.nwalkers, ndim, logl, logp,
                                         loglargs=[empmir.logl_rv, logl_params],
                                         logpargs=[empmir.logp_rv, logp_params],
                                         threads=self.cores, betas=self.betas)
            # raise ImportError
        else:
            logp_params = sp.array([self.time, kplanets, self.nins, self.MOAV,
                                    self.totcornum, boundaries, inslims, acc_lims,
                                    sigmas_raw, self.eccprior, self.jittprior,
                                    self.jittmean, self.STARMASS, self.HILL,
                                    self.PACC, self.CHECK])
            logl_params = sp.array([self.time, self.rv, self.err, self.ins,
                                    self.staract, self.starflag, kplanets, self.nins,
                                    self.MOAV, self.totcornum, self.PACC])
            self.sampler = PTSampler(self.ntemps, self.nwalkers, ndim, logl, logp,
                                     loglargs=[empmir.logl_rv, logl_params],
                                     logpargs=[empmir.logp_rv, logp_params],
                                     threads=self.cores, betas=self.betas)
        # RVPM THINGY

            s0 = chrono.time()
            for _ in range(10000):
                empmir.logp_rv(pos0[0][0], logp_params)
            print('________', chrono.time()-s0)

        print('\n --------------------- BURN IN --------------------- \n')

        pbar = tqdm(total=self.burn_out)

        for p, lnprob, lnlike in self.sampler.sample(pos0, iterations=self.burn_out):
            pbar.update(1)
            pass
        pbar.close()

        p0, lnprob0, lnlike0 = p, lnprob, lnlike
        print("\nMean acceptance fraction: {0:.3f}".format(sp.mean(self.sampler.acceptance_fraction)))
        assert sp.mean(self.sampler.acceptance_fraction) != 0, 'Mean acceptance fraction = 0 ! ! !'
        self.sampler.reset()

        print('\n ---------------------- CHAIN ---------------------- \n')
        pbar = tqdm(total=self.nsteps)
        for p, lnprob, lnlike in self.sampler.sample(p0, lnprob0=lnprob0,
                                                     lnlike0=lnlike0,
                                                     iterations=self.nsteps,
                                                     thin=self.thin):
            pbar.update(1)
            pass
        pbar.close()
        #'''

        assert self.sampler.chain.shape == (self.ntemps, self.nwalkers, self.nsteps/self.thin, ndim), 'something really weird happened'
        print("\nMean acceptance fraction: {0:.3f}".format(sp.mean(self.sampler.acceptance_fraction)))

        ln_post = self.sampler.lnprobability

        posteriors = sp.array([ln_post[i].reshape(-1) for i in range(self.ntemps)])
        chains = self.sampler.flatchain
        best_post = posteriors[0] == np.max(posteriors[0])
        #raise ImportError

        thetas_raw = sp.array([chains[i] for i in range(self.ntemps)])
        thetas_hen = sp.array([empmir.henshin(chains[i], kplanets) for i in sp.arange(self.ntemps)])

        ajuste_hen = thetas_hen[0][best_post][0]
        ajuste_raw = thetas_raw[0][best_post][0]

        interesting_loc = sp.array([max(posteriors[temp]) - posteriors[temp] < self.bayes_factor for temp in sp.arange(self.ntemps)])
        interesting_thetas = sp.array([thetas_hen[temp][interesting_loc[temp]] for temp in sp.arange(self.ntemps)])
        thetas_hen = sp.array([thetas_hen[temp] for temp in sp.arange(self.ntemps)])
        interesting_thetas_raw = sp.array([thetas_raw[temp][interesting_loc[temp]] for temp in sp.arange(self.ntemps)])
        interesting_posts = sp.array([posteriors[temp][interesting_loc[temp]] for temp in range(self.ntemps)])
        sigmas = sp.array([ sp.std(interesting_thetas[0][:, i]) for i in range(ndim) ])
        sigmas_raw = sp.array([ sp.std(interesting_thetas_raw[0][:, i]) for i in range(ndim) ])
        #print('sigmas', sigmas)  # for testing
        #print('sigmas_raw', sigmas_raw)
        #print('mod_lims', boundaries)
        print('ALL RIGHT ALL RIGHT ALL RIGHT ALL RIGHT ALL RIGHT ALL RIGHT ALL RIGHT ALL RIGHT ')
        return thetas_raw, ajuste_raw, thetas_hen, ajuste_hen, p, lnprob, lnlike, posteriors, self.sampler.betas, interesting_thetas, interesting_posts, sigmas, sigmas_raw


    def conquer(self, from_k, to_k, logl=logl, logp=logp, BOUND=sp.array([])):
        burn_out = self.burn_out
        assert self.cores >= 1, 'Cores is set to 0 ! !'
        assert self.thin * self.draw_every_n < self.nsteps, 'You are thining way too hard ! !'
        if self.betas is not None:
            assert len(self.betas) == self.ntemps, 'Betas array and ntemps dont match ! !'

        if self.MUSIC:
            mixer.init()
            s = mixer.Sound('mediafiles/imperial_march.wav')
            thybiding = mixer.Sound('mediafiles/swvader04.wav')
            technological_terror = mixer.Sound('mediafiles/technological.wav')
            s.play()

        kplan = from_k
        # for k = 0
        mod_lims = sp.array([])
        acc_lims = sp.array([-1., 1.])
        jitt_limiter = sp.amax(abs(self.rv))
        jitt_lim = 3 * jitt_limiter  # review this
        offs_lim = jitt_limiter  # review this
        ins_lims = sp.array([sp.append(sp.array([0.0001, jitt_lim, -offs_lim, offs_lim]), sp.array([sp.array([-1.0, 1.0, 0.1, 10]) for j in range(self.MOAV)])) for i in range(self.nins)]).reshape(-1)
        sqrta, sqrte = jitt_lim, 1.
        sqrta, sqrte = sqrta ** 0.5, sqrte ** 0.5
        ndim = kplan * 5 + self.nins*2*(self.MOAV+1) + self.totcornum + 1 + self.PACC
        free_lims = sp.array([sp.log(0.1), sp.log(3 * max(self.time)), -sqrta, sqrta, -sqrta, sqrta, -sqrte, sqrte, -sqrte, sqrte])
        sigmas, sigmas_raw = sp.zeros(ndim), sp.zeros(ndim)
        pos0 = 0.
        thetas_hen, ajuste_hen = 0., 0.
        ajuste_raw = sp.array([0])
        oldlogpost = -999999999.
        interesting_thetas, interesting_posts = sp.array([]), sp.array([])
        thetas_raw = sp.array([])
        #####################################################
        #if self.PM:
        #    pm_lims = sp.array([min(self.time_pm), max(self.time_pm), 0.0, 1.0, min(self.rv_pm), max(self.rv_pm), 0.1, 10])
            #                           t0min           t0max      ratiomin ratiomax kamin          ka_max      kr_min   kr_max
        #####################################################
        START = chrono.time()
        while kplan <= to_k:

            mod_lims = sp.array([free_lims for i in range(kplan)]).reshape(-1)
            ins_lims = sp.array([sp.append(sp.array([0.0001, jitt_lim, -offs_lim, offs_lim]), sp.array([sp.array([-1.0, 1.0, 0.1, 10]) for j in range(self.MOAV)])) for i in range(self.nins)]).reshape(-1)
            #if LIL_JITT:
            #    ins_lims = ins_lims = sp.array([sp.append(sp.array([0.0001, 10.0, -offs_lim, offs_lim]), sp.array([sp.array([-1.0, 1.0, 0.1, 10]) for j in range(self.MOAV)])) for i in range(self.nins)]).reshape(-1)
            if kplan > 0:
                if self.PM:
                    ndim += self.fsig*self.lenppm
                    t0min, t0max = min(self.time_pm), max(self.time_pm)
                    pm_lims = sp.array([t0min, t0max, 0.0, 1.0, min(self.rv_pm), max(self.rv_pm), -10, 10])  # makes sense????
                    #                   t0min  t0max ratiomin ratiomax kamin       ka_max       kr_min  kr_max
                    print(pm_lims, pm_lims.shape, 'pm_lims and shape\n\n')  # PMPMPM
                    print(self.fsig*self.lenppm, 'ndim en pm\n\n')  # PMPMPM

                if self.CONSTRAIN and ajuste_raw[0]:
                    constrained = sp.array([])
                    for k in range(kplan - 1):
                        amp = 2.0
                        Pk, Ask, Ack, Sk, Ck = ajuste_raw[k*5:(k+1)*5]
                        Pk_std, Ask_std, Ack_std, Sk_std, Ck_std = sigmas_raw[5*k:5*(k+1)]
                        Ask_std, Ack_std, Sk_std, Ck_std = amp * sp.array([Ask_std, Ack_std, Sk_std, Ck_std])
                        aux = sp.array([Pk-Pk_std, Pk+Pk_std, Ask - Ask_std, Ask + Ask_std, Ack - Ack_std, Ack + Ack_std, Sk - Sk_std, Sk + Sk_std, Ck - Ck_std, Ck + Ck_std])

                        constrained = sp.append(constrained, aux)

                    for nin in range(self.nins):  # only constrains
                        ins_lims[0 + nin*4*(self.MOAV+1)] = 0.0001
                        ins_lims[1 + nin*4*(self.MOAV+1)] = ajuste_raw[(kplan - 1) * 5 + nin*2*(self.MOAV+1) + 1 + self.PACC]

                    mod_lims = sp.append(constrained, free_lims)  # only planets limits

            if len(BOUND) != 0:
                nn = len(BOUND)
                for j in range(len(BOUND[:kplan].reshape(-1))):
                    if BOUND[:kplan].reshape(-1)[j] != -sp.inf:
                        mod_lims[j] = BOUND[:kplan].reshape(-1)[j]
                if nn <= kplan:
                    BOUND = sp.array([])

            #print('ins_lims', ins_lims)  # testing purposes
            #print('mod_lims', mod_lims)
            #print('ajuste_raw', ajuste_raw)

            if self.breakFLAG==True:
                break

            if self.PM:
                if kplan > 0:
                    pos0 = emplib.pt_pos_rvpm(self.setup, kplan, self.nins, mod_lims, ins_lims,
                                              acc_lims, self.MOAV, self.totcornum, self.PACC,
                                              self.fsig, self.lenppm, self.nins_pm, pm_lims)
                    print(pos0, pos0.shape, 'pos0 y shape\n\n')  # PMPMPM
                    thetas_raw, ajuste_raw, thetas_hen, ajuste_hen, p, lnprob, lnlike, posteriors, betas, interesting_thetas, interesting_posts, sigmas, sigmas_raw = self.MCMC(kplan, mod_lims, ins_lims, acc_lims, sigmas_raw, pos0, logl, logp, pm_lims)
                else:
                    pos0 = emplib.pt_pos(self.setup, kplan, self.nins, mod_lims, ins_lims,
                                         acc_lims, self.MOAV, self.totcornum, self.PACC)
                    thetas_raw, ajuste_raw, thetas_hen, ajuste_hen, p, lnprob, lnlike, posteriors, betas, interesting_thetas, interesting_posts, sigmas, sigmas_raw = self.MCMC(kplan, mod_lims, ins_lims, acc_lims, sigmas_raw, pos0, logl, logp)
            else:
                pos0 = emplib.pt_pos(self.setup, kplan, self.nins, mod_lims, ins_lims,
                                     acc_lims, self.MOAV, self.totcornum, self.PACC)
                thetas_raw, ajuste_raw, thetas_hen, ajuste_hen, p, lnprob, lnlike, posteriors, betas, interesting_thetas, interesting_posts, sigmas, sigmas_raw = self.MCMC(kplan, mod_lims, ins_lims, acc_lims, sigmas_raw, pos0, logl, logp)
            chain = thetas_hen
            fit = ajuste_hen
            sample_sizes = sp.array([len(interesting_thetas[i]) for i in range((len(interesting_thetas)))])
            bestlogpost = max(posteriors[0])

            # BIC
            NEW_BIC = sp.log(self.ndat) * ndim - 2 * bestlogpost
            OLD_BIC = sp.log(self.ndat) * ndim - 2 * oldlogpost
            NEW_AIC = 2 * ndim - 2 * bestlogpost
            OLD_AIC = 2 * ndim - 2 * oldlogpost
                # theta, time, kplanets, ins, staract, starflag, kplanets, nins, MOAV, totcornum, PACC

            residuals = empmir.RV_residuals(ajuste_raw, self.rv, self.time, self.ins, self.staract, self.starflag, kplan, self.nins, self.MOAV, self.totcornum, self.PACC)
            alt_res = self.alt_results(interesting_thetas[0], kplan)
            saveplace = self.mklogfile(fit, bestlogpost, sample_sizes, sigmas, kplan, mod_lims, NEW_BIC, NEW_AIC, alt_res, START, residuals)
            self.instigator(interesting_thetas, interesting_posts, saveplace, kplan)
            if self.MUSIC:
                thybiding.play()
            if self.PLOT:
                from emperors_canvas import plot1, plot2
                plug = sp.array([self.setup, kplan, self.nins, self.totcornum,
                                 saveplace, self.MOAV, self.PACC])

                plot2(self.all_data, plug, fit, self.starflag, self.staract,
                      self.ndat)

                plug2 = sp.array([self.HISTOGRAMS, self.CORNER, self.STARMASS,
                                  self.PNG, self.PDF, self.thin, self.draw_every_n])
                plot1(interesting_thetas, interesting_posts, plug, plug2, '0')

                if self.RAW:
                    rawplace = str(saveplace)+'/RAW'
                    os.makedirs(rawplace)
                    self.instigator(thetas_hen, posteriors, rawplace, kplan)
                    plug[4] = rawplace
                    plot1(thetas_hen, posteriors, plug, plug2, '0')

            if OLD_BIC - NEW_BIC < self.BIC:
                print('\nBayes Information Criteria of %.2f requirement not met ! !' % self.BIC)
            if OLD_AIC - NEW_AIC < self.AIC:
                print('\nAkaike Information Criteria of %.2f requirement not met ! !' % self.AIC)
                print(OLD_AIC, NEW_AIC, OLD_AIC - NEW_AIC)

            print('New logpost vs. Old logpost', bestlogpost, oldlogpost, bestlogpost - oldlogpost)
            print('Old BIC vs New BIC', OLD_BIC, NEW_BIC, OLD_BIC - NEW_BIC)
            print('Old AIC vs New AIC', OLD_AIC, NEW_AIC, OLD_AIC - NEW_AIC)

            if bestlogpost - oldlogpost < self.model_comparison:
                print('\nBayes Factor of %.2f requirement not met ! !' % self.model_comparison)
                break

            oldlogpost = bestlogpost
            kplan += 1
        if self.MUSIC:
            technological_terror.play()
        return pos0, chain, fit, thetas_raw, ajuste_raw, mod_lims, posteriors, bestlogpost, interesting_thetas, interesting_posts, sigmas, sigmas_raw
Example #5
0
p0 = np.dstack((w0,w1,w2,w3,w4,w5,w6,w7,w8))
#print p0.shape
niter = 2000
nburn = np.int(0.01*niter)

diskname='HD141569A'
#f = open("burnchain.dat", "w")
#f.close()

######################################################
##################  BURN IN STAGE ####################
######################################################



for p, lnprob, lnlike in sampler.sample(p0, iterations=nburn):
    #samples = sampler.chain[:,:,:].reshape((-1,ndim))
    #fig = triangle.corner(samples, labels=["$i$","$Ho$","$M$","$amax$","$alpha$","$beta$","$w$"])#,truths=[i_true,Ho_true,M_true,amax_true,alpha_true,beta_true,w_true])
    #fig.savefig("burntriangle.png")
    pass
 #   for k in range(nwalkers):
 #       for j in range(ntemps):
 #           f.write('{0} \n'.format(sampler.acceptance_fraction[j,k]))
    

#f.close()

    
print 'Burn in complete'

#pool.close()
        if i == 0:
            x0,y0 = 0,0
        else:
            x0,y0 = dx,dy
        image = imgs[i]
        sigma = sigs[i]
        psf = PSFs[i]
        lp += lensModel.lensFit(None,image,sigma,gals,lenses,srcs,xc+x0,yc+y0,OVRS,verbose=False,psf=psf,csub=1)
    #print lp
    return lp

def logp(X):
    return 0

sampler=PTSampler(ntemps, nwalkers, ndim, lnprob,logp,threads=4)
for p, lnprob, lnlike in sampler.sample(p0, iterations=200):
    pass
sampler.reset()
print 'sampled'
for p, lnprob, lnlike in sampler.sample(p, lnprob0=lnprob,lnlike0=lnlike,iterations=200):
    pass
assert sampler.chain.shape == (ntemps, nwalkers, 200, ndim)
print 'fertig?'
'''
outFile = '/data/ljo31/Lens/J1347/test_emcee'
f = open(outFile,'wb')
cPickle.dump(S.result(),f,2)
f.close()

result = S.result()
lp = result[0]
Example #7
0
        #for result in sampler.sample(pos_end, iterations=10, storechain=False):
	#    position = result[0]
	#    f = open("chain.dat", "a")
	#    for k in range(position.shape[0]):
	#        f.write("{0:4d} {1:s}\n".format(k, " ".join(position[k])))
	#    fc.lose()



	fn = "spec_mcmc_newest_"+str(nspec)+".out"
	f = open(fn, "w")
	f.close()
	print 'now running sampler with '+np.str(niter * nwalkers)+' total steps'
	sys.stdout.flush()
	for pos, prob, rstate in sampler.sample(pos0, prob, state, iterations=niter):
            # Write the current position to a file, one line per walker                                                                                                                                                                                                                
            f = open(fn, "a")
	    f.write("\n".join(["\t".join([str(q) for q in p]) for p in pos]))
            #f.write("\n".join(["\t".join( pos.tolist()[i] +[prob.tolist()[i]] )  for i in range( len(prob) ) ] ) )
	    f.write("\n")
            f.close()

        #pool.close()

	print("Mean acceptance fraction: {0:.3f}".format(np.mean(sampler.acceptance_fraction)))


        #Plotting corner plot that shows the model density  over each posible tuples of parameter projections
	samples = sampler.chain.reshape((-1, ndim))
	if parameters==['NH','Vmax']:
Example #8
0
    #for result in sampler.sample(pos_end, iterations=10, storechain=False):
    #    position = result[0]
    #    f = open("chain.dat", "a")
    #    for k in range(position.shape[0]):
    #        f.write("{0:4d} {1:s}\n".format(k, " ".join(position[k])))
    #    fc.lose()

    fn = "spec_mcmc_newest_" + str(nspec) + ".out"
    f = open(fn, "w")
    f.close()
    print 'now running sampler with ' + np.str(
        niter * nwalkers) + ' total steps'
    sys.stdout.flush()
    for pos, prob, rstate in sampler.sample(pos0,
                                            prob,
                                            state,
                                            iterations=niter):
        # Write the current position to a file, one line per walker
        f = open(fn, "a")
        f.write("\n".join(["\t".join([str(q) for q in p]) for p in pos]))
        #f.write("\n".join(["\t".join( pos.tolist()[i] +[prob.tolist()[i]] )  for i in range( len(prob) ) ] ) )
        f.write("\n")
        f.close()

#pool.close()

    print("Mean acceptance fraction: {0:.3f}".format(
        np.mean(sampler.acceptance_fraction)))

    #Plotting corner plot that shows the model density  over each posible tuples of parameter projections
    samples = sampler.chain.reshape((-1, ndim))
Example #9
0
    def call_mcmc(self):
        """ Start running the emcee code. """

        # Get the data and the intrinsic spin-down values
        y_measured,y_measured_error       = self.params.ACCEL.value,self.params.ACCEL_ERR.value
        y_measured[self.PBDOT_INDS]       = self.params.ACCEL_BINARY[self.PBDOT_INDS].value
        y_measured_error[self.PBDOT_INDS] = self.params.ACCEL_BINARY_ERR[self.PBDOT_INDS].value
        y_measured_var                    = y_measured_error**2
        y_measured_var_div                = (1/y_measured_error**2)
        j_measured                        = (self.params.P2[self.J_INDS].value/self.params.P0[self.J_INDS].value)*const.c.value
        j_measured_var                    = ((self.params.P2_ERR[self.J_INDS].value/self.params.P0[self.J_INDS].value)*const.c.value)**2
        j_measured_var_div                = (1/j_measured_var)
        P0                                = self.params.P0[self.ISO_INDS].value

        # MCMC setup parameters
        nwalkers = int(self.options.nwalker)
        nsteps   = int(self.options.nchain)
        ntemps   = int(self.options.ntemp)
        nthreads = int(self.options.nthread)
        nburn    = int(self.options.nburn)
        nglide   = int(self.options.nglide)
        nthin    = int(self.options.nthin)

        # Get initial guesses for l
        l_guesses       = self.rough_l_guess(y_measured,(nwalkers,self.rsize))
        l_guesses_scale = .1*np.fabs(l_guesses)
        l_signs         = np.sign(l_guesses[0])

        # Boundaries for the flat prior
        lb,ub = self.make_prior_array(l_signs)

        # Initial guesses for walkers
        np.random.seed(0)
        starting_guesses = np.zeros((ntemps,nwalkers,self.ndim))
        for ii in range(ntemps):
            starting_guesses[ii,:,:self.nparam]                = np.random.normal(self.theta_init, .1*self.theta_init, (nwalkers,self.nparam))
            starting_guesses[ii,:,self.zmin_ind:self.zmax_ind] = np.random.normal(l_guesses, l_guesses_scale, (nwalkers,self.rsize))
            starting_guesses[ii,:,self.bmin_ind:self.bmax_ind] = self.bfield_dist(np.log10(self.bmin),np.log10(self.bmax),shape=(nwalkers,self.isosize))*1e8

            # Get the initial velocity guesses if needed
            if self.options.jerkflag:
                v_inits  = np.sqrt(4*np.pi*const.G.value*starting_guesses[ii,:,0]/3.)*starting_guesses[ii,:,1]
                v_inits  = np.repeat(v_inits,self.jsize).reshape(-1,self.jsize)
                j_signs  = np.sign(j_measured)
                v_inits *= j_signs
                v_inits  = np.random.normal(v_inits,.05*np.fabs(v_inits))
                starting_guesses[ii,:,self.vmin_ind:self.vmax_ind] = v_inits

            # Make the black hole mass spaced evenly in log space if needed
            if self.options.bhflag:
                bhmin_log = np.log10(self.options.bhmin)
                bhmax_log = np.log10(self.options.bhmax)
                delta_bh  = (bhmax_log-bhmin_log)
                starting_guesses[ii,:,self.nparam-1] = (10**(bhmin_log+delta_bh*np.random.random((nwalkers,))))*const.M_sun.value

        # Make the argument list to pass to the MCMC handler
        args = [self.rperp,P0,y_measured,y_measured_var_div,j_measured,j_measured_var_div,self.zmin_ind,self.zmax_ind \
                ,self.ISO_INDS,self.PBDOT_INDS,self.rc_grid,self.alpha_grid,self.lookup,l_signs,self.J_INDS \
                ,self.vmin_ind,self.vmax_ind,self.options.jerkflag,self.options.bhflag,lb,ub]

        # Sample the distribution
        try:
            sampler = PTSampler(ntemps, nwalkers, self.ndim, mcmcfnc.log_likelihood, mcmcfnc.log_prior, loglargs=[args], logpargs=[args],threads=nthreads)
        except AssertionError:
            print "Incorrect number of walkers for the given dimensions. Minimum number of walkers needed: %d." %(2*self.ndim)
            exit()

        # Burning in the data
        print "Beginning the burn-in."
        for p, lnprob, lnlike in sampler.sample(starting_guesses, iterations=nburn):
            pass
        sampler.reset()
        print "Finished the burn in. Starting the sampling"

        for p, lnprob, lnlike in sampler.sample(p, lnprob0=lnprob, lnlike0=lnlike, iterations=nsteps, thin=nthin):
            pass

        # Save the array
        np.save(self.options.outfile, sampler.chain)
        np.save('%schain.p.npy' %(self.outdir),p)
        np.save('%schain.lbprob.npy' %(self.outdir),lnprob)
        np.save('%schain.lnlike.npy' %(self.outdir),lnlike)
Example #10
0
# Load in samples from previous mcmc chain, and run any models
# that weren't generated.
p = np.load(maindir+'p_frommcmc.npy')

##################################################################
##################### Continue After Burn-in #####################
##################################################################

chainfn = maindir+"chain.dat"
tempsfn = maindir+"temps.dat"
f = open(chainfn,"w")
f.close()
g = open(tempsfn,"w")
g.close()

for p, lnprob, lnlike in sampler.sample(p, iterations=niter,storechain=True):
    position = p[0]
    position_temp = p[1]
    f = open(chainfn,"a")
    g = open(tempsfn,"a")
    for k in range(position.shape[0]):
        f.write("{0:4d} {1:s}\n".format(k, " ".join(str(position[k]))))
        g.write("{0:4d} {1:s}\n".format(k, " ".join(str(position_temp[k]))))
    f.close()
    g.close()
    
position = p
np.save('p_mcmc',position)
    
print "MCMC Complete"
samples = sampler.chain
Example #11
0
sampler = PTSampler(ntemps, nwalkers, ndim, logl, logp)

# Making the sampling multi-threaded is as simple as adding the threads=Nthreads
# argument to PTSampler. We could have modified the temperature ladder using the
# betas optional argument (which should be an array of beta = 1/T values). The
# pool argument also allows to specify our own pool of worker threads if we
# wanted fine-grained control over the parallelism.

nsteps = 1000

# First, we run the sampler for N/10 burn-in iterations:

print "PT burning in for",nsteps/10,"iterations..."
p0 = np.random.uniform(low=-1.0, high=1.0, size=(ntemps, nwalkers, ndim))
for p, lnprob, lnlike in sampler.sample(p0, iterations=nsteps/10):
    pass
sampler.reset()

# Now we sample for nsteps iterations, recording every 10th sample:

print "PT sampling for",nsteps,"iterations..."
for p, lnprob, lnlike in sampler.sample(p, lnprob0=lnprob,
                                           lnlike0=lnlike,
                                           iterations=nsteps, thin=10):
    pass
    
# The resulting samples (nsteps/thin of them) are stored as the sampler.chain 
# property:

assert sampler.chain.shape == (ntemps, nwalkers, nsteps/10, ndim)
Example #12
0
    dx2 = x - mu2

    return np.logaddexp(-np.dot(dx1, np.dot(sigma1inv, dx1))/2.0,
                        -np.dot(dx2, np.dot(sigma2inv, dx2))/2.0)

# Use a flat prior
def logp(x):
    return 0.0
    
ntemps = 20
nwalkers = 100
ndim = 2

sampler=PTSampler(ntemps, nwalkers, ndim, logl, logp)
p0 = np.random.uniform(low=-1.0, high=1.0, size=(ntemps, nwalkers, ndim))
for p, lnprob, lnlike in sampler.sample(p0, iterations=100):
    pass
sampler.reset()
for p, lnprob, lnlike in sampler.sample(p, lnprob0=lnprob,
                                           lnlike0=lnlike,
                                           iterations=1000, thin=10):
    pass

assert sampler.chain.shape == (ntemps, nwalkers, 100, ndim)


# Chain has shape (ntemps, nwalkers, nsteps, ndim)
# Zero temperature mean:
mu0 = np.mean(np.mean(sampler.chain[0,...], axis=0), axis=0)

# Longest autocorrelation length (over any temperature)
Example #13
0
    def MCMC(self, kplanets, boundaries, inslims, acc_lims, sigmas_raw, pos0,
             logl, logp):

        ndim = 1 + 4 * kplanets + self.nins * 2 * (
            self.MOAV + 1) + self.totcornum + self.PACC
        ndat = len(self.time)

        def starinfo():
            colors = [
                'red', 'green', 'blue', 'yellow', 'grey', 'magenta', 'cyan',
                'white'
            ]
            c = sp.random.randint(0, 7)
            print(
                colored(
                    '\n    ###############################################',
                    colors[c]))
            print(
                colored('    #                                             #',
                        colors[c]))
            print(
                colored('    #                                             #',
                        colors[c]))
            print(
                colored('    #                 E M P E R 0 R               #',
                        colors[c]))
            print(
                colored('    #                                             #',
                        colors[c]))
            print(
                colored('    #                                             #',
                        colors[c]))
            print(
                colored('    ###############################################',
                        colors[c]))
            print(
                colored(
                    'Exoplanet Mcmc Parallel tEmpering Radial vel0city fitteR',
                    colors[sp.random.randint(0, 7)]))
            logdat = '\n\nStar Name                         : ' + self.starname
            logdat += '\nTemperatures, Walkers, Steps      : ' + str(
                (self.ntemps, self.nwalkers, self.nsteps))
            logdat += '\nN Instruments, K planets, N data  : ' + str(
                (self.nins, kplanets, self.ndat))
            logdat += '\nN Number of Dimensions            : ' + str(ndim)
            logdat += '\nN Moving Average                  : ' + str(self.MOAV)
            logdat += '\nBeta Detail                       : ' + str(
                self.betas)
            logdat += '\n-----------------------------------------------------'
            print(logdat)
            pass

        starinfo()
        #'''
        sampler = PTSampler(self.ntemps,
                            self.nwalkers,
                            ndim,
                            logl,
                            logp,
                            loglargs=[
                                self.time, self.rv, self.err, self.ins,
                                self.staract, self.starflag, kplanets,
                                self.nins, self.MOAV, self.totcornum, self.PACC
                            ],
                            logpargs=[
                                self.time, kplanets, self.nins, self.MOAV,
                                self.totcornum, boundaries, inslims, acc_lims,
                                sigmas_raw, self.eccprior, self.jittprior,
                                self.jittmean, self.STARMASS, self.HILL,
                                self.PACC, self.CHECK
                            ],
                            threads=self.cores,
                            betas=self.betas)
        print('\n --------------------- BURN IN --------------------- \n')

        pbar = tqdm(total=self.burn_out)
        for p, lnprob, lnlike in sampler.sample(pos0,
                                                iterations=self.burn_out):
            pbar.update(1)
            pass
        pbar.close()

        p0, lnprob0, lnlike0 = p, lnprob, lnlike
        print("\nMean acceptance fraction: {0:.3f}".format(
            sp.mean(sampler.acceptance_fraction)))
        assert sp.mean(sampler.acceptance_fraction
                       ) != 0, 'Mean acceptance fraction = 0 ! ! !'
        sampler.reset()
        '''


        sampler1 = PTSampler(self.ntemps, self.nwalkers, ndim, logl, logp, loglargs=[self.time, self.rv, self.err, self.ins, self.staract, self.starflag, kplanets, self.nins, self.MOAV, self.totcornum, self.PACC],
                            logpargs=[self.time, kplanets, self.nins, self.MOAV, self.totcornum, boundaries, inslims, acc_lims, sigmas_raw, self.eccprior, self.jittprior, self.jittmean, self.STARMASS, self.HILL, self.PACC, self.CHECK],
                            threads=self.cores, betas=self.betas*0.3)

        print('\n --------------- EXPERIMENTAL BURN IN --------------- \n')

        pbar = tqdm(total=self.burn_out)
        for p, lnprob, lnlike in sampler1.sample(pos0, iterations=self.burn_out // 3):
            pbar.update(1)
            pass
        pbar.close()

        p0, lnprob0, lnlike0 = p, lnprob, lnlike
        print("\nMean acceptance fraction: {0:.3f}".format(sp.mean(sampler1.acceptance_fraction)))
        assert sp.mean(sampler1.acceptance_fraction) != 0, 'Mean acceptance fraction = 0 ! ! !'
        sampler1.reset()

        print('\n ---------------- EXPERIMENTAL CHAIN ---------------- \n')
        sampler = PTSampler(self.ntemps, self.nwalkers, ndim, logl, logp, loglargs=[self.time, self.rv, self.err, self.ins, self.staract, self.starflag, kplanets, self.nins, self.MOAV, self.totcornum, self.PACC],
                            logpargs=[self.time, kplanets, self.nins, self.MOAV, self.totcornum, boundaries, inslims, acc_lims, sigmas_raw, self.eccprior, self.jittprior, self.jittmean, self.STARMASS, self.HILL, self.PACC, self.CHECK],
                            threads=self.cores, betas=self.betas)

        pbar = tqdm(total=self.nsteps)
        for p, lnprob, lnlike in sampler.sample(p0, lnprob0=lnprob0,
                                                   lnlike0=lnlike0,
                                                   iterations=self.nsteps,
                                                   thin=self.thin):
            pbar.update(1)
            pass
        pbar.close()



        '''
        print('\n ---------------------- CHAIN ---------------------- \n')
        pbar = tqdm(total=self.nsteps)
        for p, lnprob, lnlike in sampler.sample(p0,
                                                lnprob0=lnprob0,
                                                lnlike0=lnlike0,
                                                iterations=self.nsteps,
                                                thin=self.thin):
            pbar.update(1)
            pass
        pbar.close()
        #'''

        assert sampler.chain.shape == (self.ntemps, self.nwalkers,
                                       self.nsteps / self.thin,
                                       ndim), 'something really weird happened'
        print("\nMean acceptance fraction: {0:.3f}".format(
            sp.mean(sampler.acceptance_fraction)))

        ln_post = sampler.lnprobability

        posteriors = sp.array(
            [ln_post[i].reshape(-1) for i in range(self.ntemps)])
        chains = sampler.flatchain
        best_post = posteriors[0] == np.max(posteriors[0])

        thetas_raw = sp.array([chains[i] for i in range(self.ntemps)])
        thetas_hen = sp.array([
            empmir.henshin_PM(chains[i], kplanets) for i in range(self.ntemps)
        ])

        ajuste_hen = thetas_hen[0][best_post][0]
        ajuste_raw = thetas_raw[0][best_post][0]

        interesting_loc = sp.array([
            max(posteriors[temp]) - posteriors[temp] < self.bayes_factor
            for temp in sp.arange(self.ntemps)
        ])
        interesting_thetas = sp.array([
            thetas_hen[temp][interesting_loc[temp]]
            for temp in sp.arange(self.ntemps)
        ])
        interesting_thetas_raw = sp.array([
            thetas_raw[temp][interesting_loc[temp]]
            for temp in sp.arange(self.ntemps)
        ])
        interesting_posts = sp.array([
            posteriors[temp][interesting_loc[temp]]
            for temp in range(self.ntemps)
        ])
        sigmas = sp.array(
            [sp.std(interesting_thetas[0][:, i]) for i in range(ndim)])
        sigmas_raw = sp.array(
            [sp.std(interesting_thetas_raw[0][:, i]) for i in range(ndim)])
        #print('sigmas', sigmas)  # for testing
        #print('sigmas_raw', sigmas_raw)
        #print('mod_lims', boundaries)
        print ajuste_raw, ajuste_hen
        return thetas_raw, ajuste_raw, thetas_hen, ajuste_hen, p, lnprob, lnlike, posteriors, sampler.betas, interesting_thetas, interesting_posts, sigmas, sigmas_raw
Example #14
0
def mc_main(s_ident,
            ntemps,
            nwalkers,
            niter,
            nburn,
            nthin,
            nthreads,
            mcdata,
            mcmod,
            partemp=True,
            mc_a=2.,
            init_samples_fn=None,
            write_model=False,
            plot=False,
            save=False):

    start = time.ctime()
    time_start_secs = time.time()

    print("\nSTART TIME: " + start)

    # TEMP!!!
    # For now, emcee v3.* offers only an EnsembleSampler, so force that mode
    # if such a version is detected.
    if (ntemps > 1) and (emcee_version_major >= 3):
        ntemps = 1
        partemp = False
        emcee_v3_msg = "WARNING! FORCED to use EnsembleSampler because emcee v3+ detected. Setting ntemps=1 and partemp=False."
        print("\n" + emcee_v3_msg)
        print(
            "To use a PTSampler, try the ptemcee package (NOT currently compatible with diskmc) or using emcee v2.2.1."
        )
    else:
        emcee_v3_msg = None

    data = mcdata.data
    uncerts = mcdata.uncerts
    data_types = np.array(mcdata.data_types)  # need as nd.array for later
    stars = mcdata.stars

    model_path = os.path.join(
        os.path.abspath(os.path.expanduser(mcmod.model_path)), '')
    log_path = os.path.join(
        os.path.abspath(os.path.expanduser(mcmod.log_path)), '')
    lam = mcmod.lam  # [microns]
    unit_conv = mcmod.unit_conv

    # Sort the parameter names.
    # NOTE: this must be an array (can't be a list).
    pkeys_all = np.array(sorted(mcmod.pkeys))

    # Create log file.
    mcmc_log_fn = os.path.join(log_path, '%s_mcmc_log.txt' % s_ident)
    mcmc_log = open(mcmc_log_fn, 'w')

    # FIX ME!!! Need to handle this specific case better.
    # Make phi map specifically for conversion of Stokes to radial Stokes.
    yy, xx = np.mgrid[:data[0].shape[0], :data[0].shape[1]]
    phi_stokes = np.arctan2(yy - stars[0][0], xx - stars[0][1])

    # Bin data by factors specified in mcdata.bin_facts list.
    # Do nothing if mcdata.bin_facts is None or its elements are 1.
    if mcdata.bin_facts is None:
        mcdata.bin_facts = len(data) * [1]

    data_orig = []
    uncerts_orig = []
    stars_orig = []
    for ii, bin_fact in enumerate(mcdata.bin_facts):
        if bin_fact not in [1, None]:
            # Store the original data as backup.
            data_orig.append(data[ii].copy())
            uncerts_orig.append(uncerts[ii].copy())
            # Bin data, uncertainties, and mask by interpolation.
            datum_binned = zoom(np.nan_to_num(data_orig[ii].data),
                                1. / bin_fact) * bin_fact
            uncert_binned = zoom(np.nan_to_num(uncerts_orig[ii]),
                                 1. / bin_fact,
                                 order=1) * bin_fact
            # FIX ME!!! Interpolating the mask may not work perfectly. Linear interpolation (order=1)
            # is best so far.
            try:
                mask_binned = zoom(np.nan_to_num(data_orig[ii].mask),
                                   1. / bin_fact,
                                   order=1)
            except:
                mask_binned = False

            stars_orig.append(stars[ii].copy())
            star_binned = stars_orig[ii] / int(bin_fact)

            # radii_binned = make_radii(datum_binned, star_binned)

            # mask_fit = np.ones(datum_binned.shape).astype(bool)
            # mask_fit[star_binned[0]-int(hw_y/bin_fact):star_binned[0]+int(hw_y/bin_fact)+1, star_binned[1]-int(hw_x/bin_fact):star_binned[1]+int(hw_x/bin_fact)+1] = False
            # FIX ME!!! Need to specify this inner region mask or happens automatically?
            # mask_fit[radii_binned < r_fit/int(bin_fact)] = True

            data[ii] = np.ma.masked_array(datum_binned, mask=mask_binned)
            uncerts[ii] = uncert_binned
            stars[ii] = star_binned

    ####################################
    # ------ INITIALIZE WALKERS ------ #
    # This will be done using a uniform distribution drawn from
    # plims_lib (first option if not None) or a Gaussian distribution
    # drawn from pmeans_lib and psigmas_lib (if plims_lib == None).

    ndim = len(pkeys_all)

    print(
        "\nNtemps = %d, Ndim = %d, Nwalkers = %d, Nstep = %d, Nburn = %d, Nthreads = %d"
        % (ntemps, ndim, nwalkers, niter, nburn, nthreads))

    # Sort parameters for walker initialization.
    if mcmod.plims_lib is not None:
        plims_sorted = np.array(
            [val for (key, val) in sorted(mcmod.plims_lib.items())])
        init_type = 'uniform'
    elif mcmod.pmeans_lib is not None:
        pmeans_sorted = [
            val for (key, val) in sorted(mcmod.pmeans_lib.items())
        ]
        psigmas_sorted = [
            val for (key, val) in sorted(mcmod.psigmas_lib.items())
        ]
        init_type = 'gaussian'

    # Make the array of initial walker positions p0.
    if init_samples_fn is None:
        if partemp:
            if init_type == 'uniform':
                # Uniform initialization between priors.
                p0 = np.random.uniform(low=plims_sorted[:, 0],
                                       high=plims_sorted[:, 1],
                                       size=(ntemps, nwalkers, ndim))
            elif init_type == 'gaussian':
                # Gaussian ball initialization.
                p0 = np.random.normal(loc=pmeans_sorted,
                                      scale=psigmas_sorted,
                                      size=(ntemps, nwalkers, ndim))
        else:
            if init_type == 'uniform':
                # Uniform initialization between priors.
                p0 = np.random.uniform(low=plims_sorted[:, 0],
                                       high=plims_sorted[:, 1],
                                       size=(nwalkers, ndim))
            elif init_type == 'gaussian':
                # Gaussian ball initialization.
                p0 = np.random.normal(loc=pmeans_sorted,
                                      scale=psigmas_sorted,
                                      size=(nwalkers, ndim))
        print("Walker initialization = " + init_type)
    else:
        init_type == 'sampled'
        init_step_ind = -1
        init_samples = hickle.load(os.path.join(log_path, init_samples_fn))
        if partemp:
            p0 = init_samples['_chain'][:, :, init_step_ind, :]
            lnprob_init = init_samples['_lnprob'][:, :, init_step_ind]
            lnlike_init = init_samples['_lnlikelihood'][:, :, init_step_ind]
        else:
            p0 = init_samples['_chain'][:, init_step_ind, :]
            lnprob_init = init_samples['_lnprob'][:, init_step_ind]
            lnlike_init = init_samples['_lnlikelihood'][:, init_step_ind]
        print(
            "\nLoaded init_samples from %s.\nWalkers will start from those final positions."
            % init_samples_fn)

    # Try to get the MCFOST version number being used.
    try:
        output = subprocess.check_output("mcfost -v", shell=True)
        mcf_version = output.split('\n')[0].split(' ')[-1]
    except:
        mcf_version = 'unknown'
    print("Running MCFOST version %s" % mcf_version)
    print("Running diskmc version %s" % __version__)

    log_preamble = [
        '|---MCMC LOG---|\n\n',
        '%s' % s_ident,
        '\nLOG DATE: ' + dt.date.isoformat(dt.date.today()),
        '\nJOB START: ' + start,
        '\nNPROCESSORS: %d\n' % nthreads,
        '\ndiskmc version: %s' % __version__,
        '\nMCFOST version: %s' % mcf_version,
        '\nMCFOST PARFILE: ',
        mcmod.parfile,
        '\n\nMCMC PARAMS: Ndim: %d, Nwalkers: %d, Nburn: %d, Niter: %d, Nthin: %d, Nthreads: %d'
        % (ndim, nwalkers, nburn, niter, nthin, nthreads),
        '\nPARALLEL-TEMPERED?: ' + str(partemp),
        ' , Ntemps: %d' % ntemps,
        '\nINITIALIZATION: ',
        init_type,
        '\na = %.2f' % mc_a,
        '\nWavelength = %s microns' % str(lam),
        '\n',
    ]
    mcmc_log.writelines(log_preamble)
    if emcee_v3_msg is not None:
        mcmc_log.writelines('\n{}\n'.format(emcee_v3_msg))
    mcmc_log.close()

    # Create emcee sampler instances: parallel-tempered or ensemble.
    if partemp:
        # Instantiate parallel-tempered sampler.
        # Pass data_I, uncertainty_I, and parfile as arguments to emcee sampler.
        sampler = PTSampler(
            ntemps,
            nwalkers,
            ndim,
            mc_lnlike,
            mc_lnprior,
            a=mc_a,
            loglargs=[
                pkeys_all, data, uncerts, data_types, mcmod.mod_bin_factor,
                phi_stokes, mcmod.parfile, model_path, unit_conv, mcmod.priors,
                mcmod.scatlight, mcmod.fullimg, mcmod.sed, mcmod.dustprops,
                lam, partemp, ndim, write_model, s_ident, mcdata.algo_I,
                mcmod.modfm
            ],
            logpargs=[pkeys_all, mcmod.priors],
            threads=nthreads)
        #pool=pool)

    else:
        # Instantiate ensemble sampler.
        if emcee_version_major >= 3:
            # Put backend setup here for some future use.
            # backend_log_name = os.path.join(log_path, '{}_mcmc_full_sampler.h5'.format(s_ident))
            # backend = emcee.backends.HDFBackend(backend_log_name, name='mcmc')
            # backend.reset(nwalkers, ndim)
            #
            # vars(backend)['pkeys_all'] = pkeys_all
            backend = None

            from multiprocessing import Pool
            pool = Pool()

            sampler = EnsembleSampler(
                nwalkers,
                ndim,
                mc_lnlike,
                a=mc_a,
                args=[
                    pkeys_all, data, uncerts, data_types, mcmod.mod_bin_factor,
                    phi_stokes, mcmod.parfile, model_path, unit_conv,
                    mcmod.priors, mcmod.scatlight, mcmod.fullimg, mcmod.sed,
                    mcmod.dustprops, lam, partemp, ndim, write_model, s_ident,
                    mcdata.algo_I, mcmod.modfm
                ],
                pool=pool,
                backend=backend)
            # Add some items to sampler that don't exist in emcee >2.2.1.
            vars(sampler)['_chain'] = np.array([])
            vars(sampler)['_lnprob'] = np.array([])
        else:
            sampler = EnsembleSampler(
                nwalkers,
                ndim,
                mc_lnlike,
                a=mc_a,
                args=[
                    pkeys_all, data, uncerts, data_types, mcmod.mod_bin_factor,
                    phi_stokes, mcmod.parfile, model_path, unit_conv,
                    mcmod.priors, mcmod.scatlight, mcmod.fullimg, mcmod.sed,
                    mcmod.dustprops, lam, partemp, ndim, write_model, s_ident,
                    mcdata.algo_I, mcmod.modfm
                ],
                threads=nthreads)

    # Insert pkeys and priors into the sampler dict for later use.
    # Force '|S' string dtype to avoid unicode (which doesn't hickle well).
    vars(sampler)['pkeys_all'] = pkeys_all.astype('S')
    vars(sampler)['priors'] = mcmod.priors.copy()

    # List of items to delete from the sampler dict that may not hickle well during
    # logging. Mix of emcee v2 and v3, Ensemble, and PTSampler items.
    sampler_keys_trim = [
        'pool', 'lnprobfn', 'log_prob_fn', 'runtime_sortingfn', 'logl', 'logp',
        'logpkwargs', 'loglkwargs', 'args', 'kwargs', '_random', '_moves',
        '_previous_state', 'backend'
    ]

    ###############################
    # ------ BURN-IN PHASE ------ #
    if nburn > 0:
        print("\nBURN-IN START...\n")
        for bb, (pburn, lnprob_burn,
                 lnlike_burn) in enumerate(sampler.sample(p0,
                                                          iterations=nburn)):
            # Print progress every 25%.
            if bb in [nburn // 4, nburn // 2, 3 * nburn // 4]:
                print("PROCESSING ITERATION %d; BURN-IN %.1f%% COMPLETE..." %
                      (bb, 100 * float(bb) / nburn))
            pass

        # Print burn-in autocorrelation time and acceptance fraction.
        try:
            max_acl_burn = np.nanmax(
                sampler.acor)  # fails if too few iterations
        except:
            max_acl_burn = -1.
        print("Largest Burn-in Autocorrelation Time = %.1f" % max_acl_burn)
        if partemp:
            print("Mean, Median Burn-in Acceptance Fractions: %.2f, %.2f" %
                  (np.mean(sampler.acceptance_fraction[0]),
                   np.median(sampler.acceptance_fraction[0])))
        else:
            print("Mean, Median Burn-in Acceptance Fractions: %.2f, %.2f" %
                  (np.mean(sampler.acceptance_fraction),
                   np.median(sampler.acceptance_fraction)))

        # Pause interactively between burn-in and main phase.
        # Comment this out if you don't want the script to pause here.
        # pdb.set_trace()

        # Reset the sampler chains and lnprobability after burn-in.
        sampler.reset()

        print("BURN-IN COMPLETE!")

    elif (nburn == 0) & (init_samples_fn is not None):
        print(
            "\nWalkers initialized from file and no burn-in samples requested."
        )
        sampler.reset()
        pburn = p0
        lnprob_burn = None  #lnprob_init
        lnlike_burn = None  #lnlike_init
    else:
        print("\nNo burn-in samples requested.")
        pburn = p0
        lnprob_burn = None
        lnlike_burn = None

    ############################
    # ------ MAIN PHASE ------ #

    print("\nMAIN-PHASE MCMC START...\n")
    if partemp:
        for nn, (pp, lnprob, lnlike) in enumerate(
                sampler.sample(pburn,
                               lnprob0=lnprob_burn,
                               lnlike0=lnlike_burn,
                               iterations=niter)):
            # Print progress every 25%.
            if nn in [niter // 4, niter // 2, 3 * niter // 4]:
                print("Processing iteration %d; MCMC %.1f%% complete..." %
                      (nn, 100 * float(nn) / niter))
                if emcee_version_major < 3:
                    # Log the full sampler or chain (all temperatures) every so often.
                    log_message = log_sampler(sampler, sampler_keys_trim,
                                              log_path, s_ident, nn)
    else:
        for nn, (pp, lnprob, lnlike) in enumerate(
                sampler.sample(pburn, lnprob_burn, iterations=niter)):
            # Print progress every 25%.
            if nn in [niter // 4, niter // 2, 3 * niter // 4]:
                print("Processing iteration %d; MCMC %.1f%% complete..." %
                      (nn, 100 * float(nn) / niter))
                # Log the full sampler or chain (all temperatures) every so often.
                log_message = log_sampler(sampler, sampler_keys_trim, log_path,
                                          s_ident, nn)

    print('\nMCMC RUN COMPLETE!\n')

    ##################################
    # ------ RESULTS HANDLING ------ #
    # Log the sampler output and chains. Also get the max and median likelihood
    # parameter values and save models for them.

    # If possible, save the whole sampler to an HDF5 log file (could be large).
    # If that fails because hickle won't handle something in the sampler,
    # try to pickle it instead. If that still fails, just log the sampler chains.
    # if emcee_version_major < 3:
    log_message = log_sampler(sampler, sampler_keys_trim, log_path, s_ident,
                              'FINAL')

    # Chain has shape (ntemps, nwalkers, nsteps/nthin, ndim).
    if partemp:
        assert sampler.chain.shape == (ntemps, nwalkers, niter / nthin, ndim)
    else:
        assert sampler.chain.shape == (nwalkers, niter / nthin, ndim)

    # Re-open the text log for additional info.
    mcmc_log = open(mcmc_log_fn, 'a')
    mcmc_log.writelines([
        '\n' + log_message,
        '\n\n|--- RESULTS FOR ALL ITERATIONS (NO BURN-IN EXCLUDED) ---|', '\n'
    ])

    # If multiple temperatures, take zero temperature walkers because only they
    # sample the posterior distribution.
    # ch has dimensions [nwalkers, nstep, ndim] and excludes burn-in samples.
    if partemp:
        ch = sampler.chain[0, :, :, :]  # zeroth temperature chain only
        samples = ch[:, :, :].reshape((-1, ndim))
        lnprob_out = sampler.lnprobability[0]  # zero-temp chi-squareds
        # Median acceptance fraction of zero temp walkers.
        print("\nMean, Median Acceptance Fractions (zeroth temp): %.2f, %.2f" %
              (np.mean(sampler.acceptance_fraction[0]),
               np.median(sampler.acceptance_fraction[0])))
        mcmc_log.writelines(
            '\nMean, Median Acceptance Fractions (zeroth temp): %.2f, %.2f' %
            (np.mean(sampler.acceptance_fraction[0]),
             np.median(sampler.acceptance_fraction[0])))
    else:
        ch = sampler.chain
        samples = ch[:, :, :].reshape((-1, ndim))
        lnprob_out = sampler.lnprobability  # all chi-squareds
        # Median acceptance fraction of all walkers.
        print("\nMean, Median Acceptance Fractions: %.2f, %.2f" %
              (np.mean(sampler.acceptance_fraction),
               np.median(sampler.acceptance_fraction)))
        mcmc_log.writelines('\nMean, Median Acceptance Fractions: %.2f, %.2f' %
                            (np.mean(sampler.acceptance_fraction),
                             np.median(sampler.acceptance_fraction)))

    # Renormalize mass_fraction values so sum to 1.0 (more intuitive).
    wh_pops = [ind for ind, key in enumerate(pkeys_all) if 'dust_pop' in key]
    samples_orig = samples.copy()
    samples[:, wh_pops] /= np.reshape(np.sum(samples_orig[:, wh_pops], axis=1),
                                      (samples_orig.shape[0], 1))

    # Haven't implemented blobs handling yet.
    blobs = None

    # Print zero temp main-phase autocorrelation time and acceptance fraction.
    try:
        max_acl = np.nanmax(sampler.acor)
        if partemp:
            acor_T0 = sampler.acor[0]
        else:
            acor_T0 = sampler.acor
    except:
        max_acl = -1.
        acor_T0 = -1.
    print("Largest Main Autocorrelation Time = %.1f" % max_acl)

    # Max likelihood params values.
    ind_lk_max = np.where(lnprob_out == lnprob_out.max())
    lk_max = np.e**lnprob_out.max()
    params_ml_mcmc = dict(zip(pkeys_all, ch[ind_lk_max][0]))
    params_ml_mcmc_sorted = [
        val for (key, val) in sorted(params_ml_mcmc.items())
    ]

    # Get median values (50th percentile) and 1-sigma (68%) confidence intervals
    # for each parameter (in order +, -).
    params_med_mcmc = list(
        map(lambda vv: (vv[1], vv[2] - vv[1], vv[1] - vv[0]),
            zip(*np.percentile(samples, [16, 50, 84], axis=0))))

    print("\nMax-Likelihood Param Values:")
    mcmc_log.writelines('\n\nMAX-LIKELIHOOD PARAM VALUES:')
    for kk, key in enumerate(pkeys_all):
        print(key + ' = %.3e' % params_ml_mcmc[key])
        mcmc_log.writelines('\n%s = %.3e' % (key, params_ml_mcmc[key]))

    print(
        "\n50%-Likelihood Param Values (50th percentile +/- 1 sigma (i.e., 34%):"
    )
    mcmc_log.writelines(
        '\n\n50%-LIKELIHOOD PARAM VALUES (50th percentile +/- 1 sigma (i.e., 34%):'
    )
    for kk, key in enumerate(pkeys_all):
        print(key + ' = %.3f +/- %.3f/%.3f' %
              (params_med_mcmc[kk][0], params_med_mcmc[kk][1],
               params_med_mcmc[kk][2]))
        mcmc_log.writelines('\n%s = %.3f +/- %.3f/%.3f' %
                            (key, params_med_mcmc[kk][0],
                             params_med_mcmc[kk][1], params_med_mcmc[kk][2]))

    # Construct max- and med-likelihood models.
    print("\nConstructing 'best-fit' models...")
    mod_idents = ['maxlk', 'medlk']
    params_50th_mcmc = np.array(params_med_mcmc)[:, 0]

    for mm, pl in enumerate([params_ml_mcmc_sorted, params_50th_mcmc]):
        pl_dict = dict()
        pl_dict.update(zip(pkeys_all, pl))

        # Name for model and its directory.
        try:
            fnstring = "%s_mcmc_%s_%s%.3e_%s%.3e_%s%.3e" % \
                       (s_ident, mod_idents[mm], pkeys_all[0], pl_dict[pkeys_all[0]], pkeys_all[1], pl_dict[pkeys_all[1]],
                        pkeys_all[2], pl_dict[pkeys_all[2]])
        except:
            fnstring = "%s_mcmc_%s_%s%.5e" % \
                       (s_ident, mod_idents[mm], pkeys_all[0], pl_dict[pkeys_all[0]])

        # Make the MCFOST model.
        make_mcfmod(pkeys_all,
                    pl_dict,
                    mcmod.parfile,
                    model_path,
                    s_ident,
                    fnstring,
                    lam=lam,
                    scatlight=mcmod.scatlight,
                    fullimg=mcmod.fullimg)

        # Calculate Chi2 for images.
        chi2s = chi2_morph(
            os.path.join(model_path, fnstring, 'data_%s' % str(lam)), data,
            uncerts, data_types, mcmod.mod_bin_factor, phi_stokes, unit_conv)
        # Calculate reduced Chi2 for images.
        chi2_reds = np.array([
            chi2s[ii] /
            (np.where(np.isfinite(data[ii].filled(np.nan)))[0].size - ndim)
            for ii in range(len(data))
        ])
        chi2_red_total = np.sum(chi2_reds)

        if mm == 0:
            lk_type = 'Max-Likelihood'
            # print('\nMax-Likelihood total chi2_red: %.3e | SED Cushing G: %.3e , I chi2_red: %.3f , Qr chi2_red: %.3f' % (chi2_red_total, G_mm, chi2_red_I, chi2_red_Qr))
            # mcmc_log.writelines('\n\nMax-Likelihood total chi2_red: %.3e | SED Cushing G: %.3e , I chi2_red: %.3f , Qr chi2_red: %.3f' % (chi2_red_total, G_mm, chi2_red_I, chi2_red_Qr))
        elif mm == 1:
            lk_type = '50%-Likelihood'
            # print('50%%-Likelihood total chi2_red: %.3e | SED Cushing G: %.3e , I chi2_red: %.3f , Qr chi2_red: %.3f' % (chi2_red_total, G_mm, chi2_red_I, chi2_red_Qr))
            # mcmc_log.writelines('\n50%%-Likelihood total chi2_red: %.3e | SED Cushing G: %.3e , I chi2_red: %.3f , Qr chi2_red: %.3f' % (chi2_red_total, G_mm, chi2_red_I, chi2_red_Qr))
        # print('%s total chi2_red: %.3e | SED Cushing G: %.3e , I chi2_red: %.3f , Qr chi2_red: %.3f' % (lk_type, chi2_red_total, G_mm, chi2_red_I, chi2_red_Qr))
        # mcmc_log.writelines('\n%s total chi2_red: %.3e | SED Cushing G: %.3e , I chi2_red: %.3f , Qr chi2_red: %.3f' % (lk_type, chi2_red_total, G_mm, chi2_red_I, chi2_red_Qr))
        print('%s total chi2_red: %.3e' % (lk_type, chi2_red_total))
        print("individual chi2_red's: " +
              len(chi2_reds) * "%.3e | " % tuple(chi2_reds))
        mcmc_log.writelines('\n\n%s total chi2_red: %.3e' %
                            (lk_type, chi2_red_total))
        mcmc_log.writelines("\nindividual chi2_red's: " +
                            len(chi2_reds) * "%.3e | " % tuple(chi2_reds))

        # Make image, sed, and/or dust properties models for maxlk and medlk.
        try:
            os.chdir(os.path.join(model_path, fnstring))
            # Make the dust properties at proper wavelength.
            # NOTE: This must come before the image calculation at the same
            # wavelength, otherwise MCFOST automatically deletes the image directory!
            subprocess.call('rm -rf data_' + str(lam), shell=True)
            subprocess.call(
                'mcfost ' + fnstring +
                '.para -dust_prop -op %s >> dustmcfostout.txt' % lam,
                shell=True)
            time.sleep(1)
            # # Delete the (mostly) empty data_[lam] directory after dust_prop step.
            # subprocess.call('rm -rf data_'+str(lam), shell=True)
            # Make the SED model.
            subprocess.call('mcfost ' + fnstring +
                            '.para -rt >> sedmcfostout.txt',
                            shell=True)
            # Make the image models (thermal + scattered-light) at demanded wavelength.
            subprocess.call('mcfost ' + fnstring + '.para -img ' + str(lam) +
                            ' -rt2 >> imagemcfostout.txt',
                            shell=True)
            time.sleep(1)
            print("Saved image and dust properties models.")
        except:
            print("Failed to save image and dust properties models.")

    # Plot and save maxlk and medlk models.
    try:
        # Placeholder for plotting functions.
        pass
        print("Max and Median Likelihood models made, plotted, and saved.\n")
    except:
        print(
            "Max and Median Likelihood models made and saved but plotting failed.\n"
        )

    time_end_secs = time.time()
    time_elapsed_secs = time_end_secs - time_start_secs  # [seconds]
    print("END TIME: " + time.ctime())
    print("ELAPSED TIME: %.2f minutes = %.2f hours" %
          (time_elapsed_secs / 60., time_elapsed_secs / 3600.))

    mcmc_log.writelines(
        ['\n\nSTART TIME - END TIME: ', start, ' - ',
         time.ctime()])
    mcmc_log.writelines([
        '\nELAPSED TIME: %.2f minutes = %.2f hours' %
        (time_elapsed_secs / 60., time_elapsed_secs / 3600.)
    ])
    mcmc_log.writelines('\n\nSUCCESSFUL EXIT')
    mcmc_log.close()

    # Close MPI pool.
    try:
        pool.close()
        print("\nPool closed")
    except:
        print("\nNo Pool to close.")

    print("\nmc_main function finished\n")

    # # Pause interactively before finishing script.
    # pdb.set_trace()

    return
Example #15
0
def start_sampler(data, recovery_prob,  burnin, niter, verbose,  contact_daylist, max_recovery_time, nsick_param, diagnosis_lag=False, null_comparison=False,  **kwargs3):
	r"""Sampling performed using emcee """

	parameter_estimate=None
	##############################################################################
	
	G_raw, health_data, node_health, nodelist, true_value,  time_min, time_max, seed_date =data		
	######################################
	### Set number of parameters to estimate
	######################################
	ndim_base = 2
	if recovery_prob: ndim_base += nsick_param
	ndim = ndim_base+nsick_param
	
	####################### 
	##Adjust temperature ladder
	#######################
	betas = np.linspace(0, -2, 15)
	betas = 10**(np.sort(betas)[::-1])
	ntemps = 15
	
	########################################### 
	###set starting positions for the walker
	#############################################
	nwalkers = max(50, 2*ndim) # number of MCMC walkers
	starting_guess = np.zeros((ntemps, nwalkers, ndim))
	##starting guess for beta  
	starting_guess[:, :, 0] = np.random.uniform(low = 0, high = 10, size=(ntemps, nwalkers))
	##start epsilon close to zero
	epsilons = np.random.power(4, size = (ntemps, nwalkers))
	starting_guess[:, :, 1] = 1-epsilons
	if diagnosis_lag:
		starting_guess[:, :, 2: ] = np.random.uniform(low = 0.001, high = 1,size=(ntemps, nwalkers, ndim-2))
		
		
	################################################################################
	##calculating infection date and infection strength outside loglik to speed up #
	##computations
	################################################################################
	if not diagnosis_lag:		
		infection_date = [(node, time1) for node in node_health if node_health[node].has_key(1) for (time1,time2) in node_health[node][1]]
		infection_date = sorted(infection_date)
		infected_strength = {network:{node:{time: calculate_infected_strength(node, time, health_data, G_raw[network]) for time in range(time_min, time_max+1)} for node in nodelist} for network in G_raw}
		pool = None
		threads = 1
		
		
	else: 
		infection_date = None
		infected_strength=None	
		threads = 8
		

	healthy_nodelist = return_healthy_nodelist(node_health)	
	################################################################################
	if threads>1:
		
		sampler = PTSampler(ntemps=ntemps, nwalkers=nwalkers, dim=ndim, betas=betas, logl=log_likelihood, logp=log_prior, a = 1.5,  loglargs=(data, infection_date, infected_strength, healthy_nodelist, null_comparison, diagnosis_lag,  recovery_prob, nsick_param, contact_daylist, max_recovery_time, parameter_estimate), logpargs=(null_comparison, diagnosis_lag, nsick_param, recovery_prob, parameter_estimate), threads=threads) 

	if threads<=1:
		sampler = PTSampler(ntemps=ntemps, nwalkers=nwalkers, dim=ndim, betas=betas, logl=log_likelihood, logp=log_prior, a = 1.5,  loglargs=(data, infection_date, infected_strength, healthy_nodelist, null_comparison, diagnosis_lag,  recovery_prob, nsick_param, contact_daylist, max_recovery_time, parameter_estimate), logpargs=(null_comparison, diagnosis_lag, nsick_param, recovery_prob, parameter_estimate)) 

	#Run user-specified burnin
	print ("burn in......")
	for i, (p, lnprob, lnlike) in enumerate(sampler.sample(starting_guess, iterations = burnin)): 
		if verbose:print("burnin progress..."), (100 * float(i) / burnin)
		else: pass

	sampler.reset()
	#################################
	print ("sampling........")
	nthin = 5
	for i, (p, lnprob, lnlike) in enumerate(sampler.sample(p, lnprob0 = lnprob,  lnlike0= lnlike, iterations= niter, thin= nthin)):  
		if verbose:print("sampling progress"), (100 * float(i) / niter)
		else: pass


	
	##############################
	#The resulting samples are stored as the sampler.chain property:
	assert sampler.chain.shape == (ntemps, nwalkers, niter/nthin, ndim)

	return sampler