def test_setup_sampler(dmx_psrs, caplog):
    m2a = models.model_2a(dmx_psrs, noisedict=noise_dict)
    samp = sampler.setup_sampler(m2a, outdir=outdir, human='tester')
    assert hasattr(samp, "sample")
    paramfile = os.path.join(outdir, "pars.txt")
    assert os.path.isfile(paramfile)
    with open(paramfile, "r") as f:
        params = [line.rstrip('\n') for line in f]
    for ptapar, filepar in zip(m2a.param_names, params):
        assert ptapar == filepar
示例#2
0
    # pta_gw = signal_base.PTA(gw_models)

    # # delta_common=0.,
    # ptas = {0:pta_crn,
    #         1:pta_gw}

    pta_crn.set_default_params(noise)

    with open(args.pta_pkl, 'wb') as fout:
        cloudpickle.dump(pta_crn, fout)

groups = sampler.get_parameter_groups(pta_crn)
groups.extend(sampler.get_psr_groups(pta_crn))
Sampler = sampler.setup_sampler(pta_crn,
                                outdir=args.outdir,
                                resume=True,
                                empirical_distr=args.emp_distr,
                                groups=groups)


def draw_from_gw_gamma_prior(self, x, iter, beta):

    q = x.copy()
    lqxy = 0

    # draw parameter from signal model
    signal_name = [
        par for par in self.pnames if ('gw' in par and 'gamma' in par)
    ][0]
    idx = list(self.pnames).index(signal_name)
    param = self.params[idx]
示例#3
0
    if args.gfl:
        vary_rn = False
    else:
        vary_rn = True
    pta = models.model_singlepsr_noise(psr, red_var=vary_rn,
                                       psd=args.psd, Tspan=args.tspan,
                                       components=args.nfreqs,
                                       factorized_like=args.gfl,
                                       gw_components=args.n_gwbfreqs,
                                       fact_like_logmin=-14.2,
                                       fact_like_logmax=-1.2,
                                       is_wideband=args.wideband)
    emp_dist_path = args.emp_distr.replace('PSR_NAME',psr.name)

Sampler = sampler.setup_sampler(pta=pta,
                                outdir=Outdir,
                                empirical_distr = emp_dist_path,
                                resume=True)
if args.gfl:
    freqs = bys.get_freqs(pta, signal_id='gw')
else:
    freqs = bys.get_freqs(pta, signal_id='red_noise')
    
np.savetxt(Outdir+'achrom_freqs.txt', freqs)

x0 = np.hstack(p.sample() for p in pta.params)

Sampler.sample(x0, Niter=args.niter, burn=100000,
               writeHotChains=args.writeHotChains)

c0 = Core(chaindir=Outdir)
c0.set_rn_freqs(freq_path=Outdir+'/achrom_freqs.txt')
示例#4
0
def gwb_ul(psrs_cut, num_points):
    # find the maximum time span to set GW frequency sampling
    tmin = [p.toas.min() for p in psrs_cut]
    tmax = [p.toas.max() for p in psrs_cut]
    Tspan = np.max(tmax) - np.min(tmin)
    # define selection by observing backend
    selection = selections.Selection(selections.by_backend)
    # white noise parameters
    # we set these ourselves so we know the most likely values!
    efac = parameter.Constant(1)
    # quad = parameter.Constant(0)
    # ecorr = parameter.Constant(0)

    # red noise parameters
    log10_A = parameter.LinearExp(-20, -11)
    gamma = parameter.Uniform(0, 7)

    # GW parameters (initialize with names here to use parameters in common across pulsars)
    log10_A_gw = parameter.LinearExp(-18, -12)('log10_A_gw')
    gamma_gw = parameter.Constant(4.33)('gamma_gw')
    # white noise
    ef = white_signals.MeasurementNoise(efac=efac, selection=selection)
    # eq = white_signals.EquadNoise(log10_equad=equad, selection=selection)
    # ec = white_signals.EcorrKernelNoise(log10_ecorr=ecorr, selection=selection)

    # red noise (powerlaw with 30 frequencies)
    pl = utils.powerlaw(log10_A=log10_A, gamma=gamma)
    rn = gp_signals.FourierBasisGP(spectrum=pl, components=30, Tspan=Tspan)

    # gwb (no spatial correlations)
    cpl = utils.powerlaw(log10_A=log10_A_gw, gamma=gamma_gw)
    gw = gp_signals.FourierBasisGP(spectrum=cpl,
                                   components=30,
                                   Tspan=Tspan,
                                   name='gw')

    # timing model
    tm = gp_signals.TimingModel(
        use_svd=True)  # stabilizing timing model design matrix with SVD
    s = ef + rn + gw + tm

    # intialize PTA
    models = []

    for p in psrs_cut:
        models.append(s(p))

    pta = signal_base.PTA(models)
    outDir = './chains/psrs/{0}'.format(psrs_cut[0].name)
    sample = sampler.setup_sampler(pta, outdir=outDir)
    x0 = np.hstack([p.sample() for p in pta.params])

    # sampler for N steps
    N = int(
        num_points)  # normally, we would use 5e6 samples (this will save time)
    sample.sample(
        x0,
        N,
        SCAMweight=30,
        AMweight=15,
        DEweight=50,
    )

    chain = np.loadtxt(os.path.join(outDir, 'chain_1.txt'))
    pars = np.loadtxt(outDir + '/pars.txt', dtype=np.unicode_)
    ind = list(pars).index('log10_A_gw')

    UL, unc = model_utils.ul(chain[:, ind])
    return UL, unc
示例#5
0
gamma = parameter.Constant()
plaw_pr = gp_priors.powerlaw(log10_A=log10_A, gamma=gamma)
plaw = gp_signals.FourierBasisGP(plaw_pr, components=30, Tspan=Tspan)
rn = gp_signals.FourierBasisGP(fs,
                               components=30,
                               Tspan=Tspan,
                               name='excess_noise')

m = tm + wn + plaw + rn
if args.gwb_on:
    gw_log10_A = parameter.Constant('gw_log10_A')
    gw_gamma = parameter.Constant(4.3333)('gw_gamma')
    gw_pr = gp_priors.powerlaw(log10_A=gw_log10_A, gamma=gw_gamma)
    gwb = gp_signals.FourierBasisGP(gw_pr,
                                    components=args.n_gwbfreqs,
                                    Tspan=Tspan)
    m += gwb

pta = signal_base.PTA(m(psr))
pta.set_default_params(noise)
x0 = np.hstack(p.sample() for p in pta.params)
samp = sampler.setup_sampler(pta,
                             outdir=args.outdir + f'/{psr.name}/',
                             resume=False)

N = args.niter
samp.sample(x0, Niter=N, burn=200000)

c0 = Core(chaindir=args.outdir + f'/{psr.name}/')
c0.save(args.corepath + f'{psr.name}.core')
示例#6
0
                                     ecorr_drop=kdrop,
                                     ecorr_thresh=kthresh,
                                     selection=ng_backend)

tm = gp_signals.TimingModel()

log10_A = parameter.Uniform(-18,-12)
gamma = parameter.Uniform(0,7)
plaw = dropout.dropout_powerlaw(log10_A=log10_A,gamma=gamma,k_drop=kdrop,)
rn  = gp_signals.FourierBasisGP(plaw, components=30)

m = tm + ef + eq + ec + rn

pta = signal_base.PTA([m(psr)])

x0 = np.hstack(p.sample() for p in pta.params)

# sampler for N steps
N = args.niter

if psr.name in ['B1937+21','J1713+0747']:
    N *= 2

samp = sampler.setup_sampler(pta,
                             outdir=args.outdir+f'{psr.name}/',
                             resume=True)

samp.sample(x0, N, SCAMweight=30, AMweight=15, DEweight=50, burn=int(N//4),
               writeHotChains=args.writeHotChains,
               hotChain=args.hot_chain)
示例#7
0
    red_components=args.nfreqs,
    dm_components=args.nfreqs,
    noisedict=noise,
    rn_psrs=rn_psrs,
    gamma_common=args.gamma_gw,
    delta_common=0.,
    upper_limit=False,
    bayesephem=args.bayes_ephem,
    be_type='setIII',
    dm_var=True,
    dm_type='gp',
    dm_psd='powerlaw',
)

Sampler = sampler.setup_sampler(pta_gw,
                                outdir=args.outdir,
                                resume=True,
                                empirical_distr=args.emp_distr)

try:
    achrom_freqs = get_freqs(pta_gw, signal_id='gw')
    np.savetxt(args.outdir + 'achrom_rn_freqs.txt', achrom_freqs, fmt='%.18e')
except:
    pass

x0 = np.hstack(p.sample() for p in pta_gw.params)
Sampler.sample(x0,
               args.niter,
               SCAMweight=30,
               AMweight=15,
               DEweight=30,
               burn=300000,
示例#8
0
        dm_var=True,
        dm_type='gp',
        dm_psd='powerlaw',
        dm_annual=False,
        white_vary=False,
        gequad=False,
        dm_chrom=False,
        dmchrom_psd='powerlaw',
        dmchrom_idx=4,
        red_select=None,
        red_breakflat=False,
        red_breakflat_fq=None,
        coefficients=False,
    )
else:
    raise NotImplementedError('Please add this model to the script.')

Sampler = sampler.setup_sampler(pta=pta,
                                outdir=Outdir,
                                empirical_distr=args.emp_distr,
                                resume=True)
try:
    freqs = bys.get_freqs(pta, signal_id='gw')
    np.savetxt(Outdir + 'achrom_freqs.txt', freqs)
except:
    pass

x0 = np.hstack(p.sample() for p in pta.params)

Sampler.sample(x0, Niter=args.niter)