Beispiel #1
0
def test_model_singlepsr_noise_sw(nodmx_psrs,caplog):
    # caplog.set_level(logging.CRITICAL)
    m=models.model_singlepsr_noise(nodmx_psrs[1], dm_sw_deter=True,
                                   dm_sw_gp=True)
    assert hasattr(m,'get_lnlikelihood')
    x0 = {pname:p.sample() for pname,p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
def test_model_singlepsr_noise_dip_cusp(nodmx_psrs, caplog):
    # caplog.set_level(logging.CRITICAL)
    dip_kwargs = {
        'dm_expdip': True,
        'dmexp_sign': 'negative',
        'num_dmdips': 2,
        'dm_cusp_idx': [2, 4],
        'dm_expdip_tmin': [54700, 57450],
        'dm_expdip_tmax': [54850, 57560],
        'dmdip_seqname': ['1st_ism', '2nd_ism'],
        'dm_cusp': False,
        'dm_cusp_sign': 'negative',
        'dm_cusp_idx': [2, 4],
        'dm_cusp_sym': False,
        'dm_cusp_tmin': None,
        'dm_cusp_tmax': None,
        'num_dm_cusps': 2,
        'dm_dual_cusp': True,
        'dm_dual_cusp_tmin': [54700, 57450],
        'dm_dual_cusp_tmax': [54850, 57560],
    }
    m = models.model_singlepsr_noise(nodmx_psrs[1],
                                     dm_sw_deter=True,
                                     dm_sw_gp=True,
                                     **dip_kwargs)
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
Beispiel #3
0
def test_model_singlepsr_noise_chrom_diag(nodmx_psrs,caplog):
    # caplog.set_level(logging.CRITICAL)
    m=models.model_singlepsr_noise(nodmx_psrs[1], chrom_gp=True,
                                   chrom_gp_kernel='diag')
    assert hasattr(m,'get_lnlikelihood')
    x0 = {pname:p.sample() for pname,p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
def test_model_singlepsr_noise_faclike(nodmx_psrs, caplog):
    # caplog.set_level(logging.CRITICAL)
    # default behaviour
    m=models.model_singlepsr_noise(nodmx_psrs[1],
                                   factorized_like=True, Tspan=10*const.yr)
    m.get_basis()
    assert 'gw_log10_A' in m.param_names
    assert 'J1713+0747_red_noise_log10_A' in m.param_names
    assert m.signals["J1713+0747_gw"]._labels[''][-1] == const.fyr

    # gw but no RN
    m=models.model_singlepsr_noise(nodmx_psrs[1], red_var=False,
                                   factorized_like=True, Tspan=10*const.yr)
    assert hasattr(m, 'get_lnlikelihood')
    assert 'gw_log10_A' in m.param_names
    assert 'J1713+0747_red_noise_log10_A' not in m.param_names
def test_model_singlepsr_fact_like(nodmx_psrs, caplog):
    # caplog.set_level(logging.CRITICAL)
    psr = nodmx_psrs[1]
    Tspan = model_utils.get_tspan([psr])
    m=models.model_singlepsr_noise(psr, chrom_gp=True,
                                   chrom_gp_kernel='diag',
                                   factorized_like=False,
                                   Tspan=Tspan, fact_like_gamma=13./3,
                                   gw_components=5)
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
        dm_expdip_tmin.append(57450)
        dm_expdip_tmax.append(57560)
        dm_expdip_idx.append(4.4)
        dmdip_seqname.append('chrom_2')

    dip_kwargs = {'dm_expdip':True,
                  'dmexp_sign': 'negative',
                  'num_dmdips':num_dips,
                  'dm_expdip_idx':dm_expdip_idx,
                  'dm_expdip_tmin':dm_expdip_tmin,
                  'dm_expdip_tmax':dm_expdip_tmax,
                  'dmdip_seqname':dmdip_seqname}

    kwargs = copy.deepcopy(model_kwargs['5'])
    kwargs.update(dip_kwargs)
    ptas[ii] = model_singlepsr_noise(psr, **kwargs)
    all_kwargs[ii] = kwargs

super_model = HyperModel(ptas)

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

model_params = {}
for ky, pta in ptas.items():
    model_params.update({str(ky): pta.param_names})

with open(args.outdir + '/model_params.json', 'w') as fout:
    json.dump(model_params, fout, sort_keys=True,
              indent=4, separators=(',', ': '))
def test_model_singlepsr_noise(nodmx_psrs, caplog):
    # caplog.set_level(logging.CRITICAL)
    m = models.model_singlepsr_noise(nodmx_psrs[1])
    assert hasattr(m, 'get_lnlikelihood')
Beispiel #8
0
with open(args.model_kwargs_path, 'r') as fin:
    model_kwargs = json.load(fin)

model_labels = [
    ['A', 'NANOGrav'],
    ['B', '1.13 Dip w/ Scatt GP'],
    ['C', 'PPTA'],
]

ptas = {}
all_kwargs = {}

kwargs = copy.deepcopy(model_kwargs['0'])
kwargs.update({'red_var': True, 'psd': 'powerlaw'})
ptas[0] = model_singlepsr_noise(psr, **kwargs)
all_kwargs[0] = kwargs

dip_kwargs = {
    'dm_expdip': True,
    'dmexp_sign': 'negative',
    'num_dmdips': 2,
    'dm_expdip_idx': [2, 1.15],
    'dm_expdip_tmin': [54700, 57450],
    'dm_expdip_tmax': [54850, 57560],
    'dmdip_seqname': ['dm_1', 'pr_1']
}

# kwargs = copy.deepcopy(model_kwargs['0'])
kwargs.update(dip_kwargs)
ptas[1] = model_singlepsr_noise(psr, **kwargs)
Beispiel #9
0
                    'tm_marg': True
                })
            ## Treat all other Adv Noise pulsars the same
            else:
                ### Turn SW model off. Add in stand alone SW model and common process. Return model.
                kwargs.update({
                    'dm_sw_deter': False,
                    'white_vary': False,
                    'extra_sigs': mean_sw,
                    'psr_model': True,
                    'chrom_df': None,
                    'dm_df': None,
                    'tm_marg': True
                })
            ### Load the appropriate single_pulsar_model
            psr_models.append(model_singlepsr_noise(new_psr,
                                                    **kwargs))  #(new_psr))
            final_psrs.append(new_psr)
        # Treat all other DMX pulsars in the standard way
        elif not args.adv_noise_psrs_only:
            s2 = s + blocks.white_noise_block(
                vary=False, inc_ecorr=True, select='backend')
            psr_models.append(s2)  #(psr))
            final_psrs.append(psr)

        print(f'\r{psr.name} Complete.', end='', flush=True)

    crn_models = [(m + cs)(psr) for psr, m in zip(final_psrs, psr_models)]
    # gw_models = [(m + gw)(psr) for psr,m in  zip(final_psrs,psr_models)]

    pta_crn = signal_base.PTA(crn_models)
    # pta_gw = signal_base.PTA(gw_models)
Beispiel #10
0
    m += gp_signals.TimingModel(use_svd=False)
    m += models.red_noise_block(psd=args.psd, prior=prior,
                                components=args.nfreqs, gamma_val=None)
    m += models.common_red_noise_block(gamma_val=13/3., prior=prior,
                                       psd=args.psd, components=args.nfreqs)
    pta = signal_base.PTA(m(psr))

else:
    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)
Beispiel #11
0
# use dm_expdip for J1713+0747
use_dmdip = False
if 'J1713+0747' == psr.name:
    use_dmdip = True

#################
##  pta model  ##
#################
red_psd = 'spectrum' if args.fs else 'powerlaw'
pta = models.model_singlepsr_noise(psr,
                                   tm_svd=True,
                                   white_vary=True,
                                   red_var=True,
                                   psd=red_psd,
                                   components=30,
                                   dm_var=True,
                                   dm_type='gp',
                                   dmgp_kernel='diag',
                                   dm_psd='powerlaw',
                                   dm_annual=True,
                                   dm_expdip=use_dmdip,
                                   upper_limit=False)

outfile = os.path.join(args.outdir, 'params.txt')
with open(outfile, 'w') as f:
    for pname in pta.param_names:
        f.write(pname + '\n')

###############
##  sampler  ##
###############
from enterprise_warp import bilby_warp

# use the tim files from wn_sp_tutorial - tim files within the -pta flag
# do not seem to work with model_singlepsr_noise
parfile = "J0030+0451_NANOGrav_12yv3.gls.par"
timfile = "J0030+0451.tim"

psr = Pulsar(parfile, timfile, drop_t2pulsar=False)

plist = ["F0", "F1"]  # vary F0 and F1

pta = model_singlepsr_noise(
    psr,
    tmparam_list=plist,
    tm_linear=False,  # set to False so F0 and F1 will be varied
    tm_var=True,  # set to True so F0 and F1 will be varied
    components=1,  # make smaller than default to try and speed things up
    tm_marg=False,
    red_var=False,  # don't estimate red noise to speed things up
    white_vary=True,  # estimate white noise (this defaults to True anyway)
)

# run using enterprise_warp to access bilby_mcmc
priors = bilby_warp.get_bilby_prior_dict(pta)
parameters = dict.fromkeys(priors.keys())
likelihood = bilby_warp.PTABilbyLikelihood(pta, parameters)

outdir = "test/"
label = "test_bilby"
bilby.run_sampler(likelihood=likelihood,
                  priors=priors,
                  outdir=outdir,
def test_model_singlepsr_noise_chrom_nondiag(nodmx_psrs, caplog):
    # caplog.set_level(logging.CRITICAL)
    m=models.model_singlepsr_noise(nodmx_psrs[0], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag')
    assert 'J0613-0200_chrom_gp_log10_sigma' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_ell' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_ell2' not in m.param_names
    assert 'J0613-0200_chrom_gp_log10_alpha_wgt' not in m.param_names
    assert 'J0613-0200_chrom_gp_log10_p' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_gam_p' in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[1], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag')
    assert 'J1713+0747_chrom_gp_log10_sigma' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_ell' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_ell2' not in m.param_names
    assert 'J1713+0747_chrom_gp_log10_alpha_wgt' not in m.param_names
    assert 'J1713+0747_chrom_gp_log10_p' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_gam_p' in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[2], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag')
    assert 'J1909-3744_chrom_gp_log10_sigma' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_ell' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_ell2' not in m.param_names
    assert 'J1909-3744_chrom_gp_log10_alpha_wgt' not in m.param_names
    assert 'J1909-3744_chrom_gp_log10_p' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_gam_p' in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[0], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag',
                                   chrom_kernel='periodic_rfband')
    assert 'J0613-0200_chrom_gp_log10_sigma' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_ell' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_ell2' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_alpha_wgt' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_p' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_gam_p' in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[1], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag',
                                   chrom_kernel='periodic_rfband')
    assert 'J1713+0747_chrom_gp_log10_sigma' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_ell' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_ell2' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_alpha_wgt' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_p' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_gam_p' in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[2], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag',
                                   chrom_kernel='periodic_rfband')
    assert 'J1909-3744_chrom_gp_log10_sigma' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_ell' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_ell2' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_alpha_wgt' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_p' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_gam_p' in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[0], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag',
                                   chrom_kernel='sq_exp')
    assert 'J0613-0200_chrom_gp_log10_sigma' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_ell' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_p' not in m.param_names
    assert 'J0613-0200_chrom_gp_log10_gam_p' not in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[1], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag',
                                   chrom_kernel='sq_exp')
    assert 'J1713+0747_chrom_gp_log10_sigma' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_ell' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_p' not in m.param_names
    assert 'J1713+0747_chrom_gp_log10_gam_p' not in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[2], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag',
                                   chrom_kernel='sq_exp')
    assert 'J1909-3744_chrom_gp_log10_sigma' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_ell' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_p' not in m.param_names
    assert 'J1909-3744_chrom_gp_log10_gam_p' not in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[0], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag',
                                   chrom_kernel='sq_exp_rfband')
    assert 'J0613-0200_chrom_gp_log10_sigma' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_ell' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_ell2' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_alpha_wgt' in m.param_names
    assert 'J0613-0200_chrom_gp_log10_p' not in m.param_names
    assert 'J0613-0200_chrom_gp_log10_gam_p' not in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[1], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag',
                                   chrom_kernel='sq_exp_rfband')
    assert 'J1713+0747_chrom_gp_log10_sigma' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_ell' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_ell2' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_alpha_wgt' in m.param_names
    assert 'J1713+0747_chrom_gp_log10_p' not in m.param_names
    assert 'J1713+0747_chrom_gp_log10_gam_p' not in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
    m=models.model_singlepsr_noise(nodmx_psrs[2], dm_var=True,
                                   dm_type=None, chrom_gp=True,
                                   chrom_gp_kernel='nondiag',
                                   chrom_kernel='sq_exp_rfband')
    assert 'J1909-3744_chrom_gp_log10_sigma' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_ell' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_ell2' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_alpha_wgt' in m.param_names
    assert 'J1909-3744_chrom_gp_log10_p' not in m.param_names
    assert 'J1909-3744_chrom_gp_log10_gam_p' not in m.param_names
    assert hasattr(m, 'get_lnlikelihood')
    x0 = {pname: p.sample() for pname, p in zip(m.param_names, m.params)}
    m.get_lnlikelihood(x0)
Beispiel #14
0
# Add to exponential dips for J1713+0747
if args.psr == 'J1713+0747':
    dip_kwargs = {
        'dm_expdip': True,
        'dmexp_sign': 'negative',
        'num_dmdips': 2,
        'dm_expdip_tmin': [54700, 57450],
        'dm_expdip_tmax': [54850, 57560],
        'dmdip_seqname': 'ism'
    }
    for dict in model_kwargs.values():
        dict.update(dip_kwargs)

ptas = {}
for ky, kwargs in model_kwargs.items():
    ptas[int(ky)] = model_singlepsr_noise(psr, **kwargs)

model_labels = [['A', 'powerlaw', 'None', True],
                ['B', 'powerlaw', 'None', False],
                ['C', 'powerlaw', 'sq_exp', True],
                ['D', 'powerlaw', 'sq_exp', False],
                ['E', 'powerlaw', 'periodic', True],
                ['F', 'powerlaw', 'periodic', False]]

super_model = HyperModel(ptas)

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

model_params = {}