def createGWB(self, A_gwb, gamma_gw= 13./3, seed=None, fit=None): """Great GWB using libstempo.toasim""" if A_gwb!=0: LT.createGWB(self.libs_psrs, A_gwb, gamma_gw, seed=seed) # Fit libstempo psr if fit is not None: for psr in self.libs_psrs: psr.fit(iters=fit) else: pass self.gwb_added = True self.seed = seed
def make_fakes(amp, psrcut): datadir = './data/partim/' ## Get parameter noise dictionary noisedir = os.getcwd() + '/data/noisefiles/' noise_params = {} for filepath in glob.iglob(noisedir + '/*.json'): with open(filepath, 'r') as f: noise_params.update(json.load(f)) # print(noise_params) # import par and tim files into parfiles and timfiles tempopsr = [] print('Importing to libstempo....') for psr in psrcut: parfiles = sorted(glob.glob(datadir + psr.name + '*.par')) timfiles = sorted(glob.glob(datadir + psr.name + '*.tim')) psr = libs.tempopulsar(parfile=parfiles[0], timfile=timfiles[0], maxobs=50000) tempopsr.append(psr) print('adding efac') for psr in tempopsr: LT.make_ideal(psr) LT.add_efac(psr, efac=1.0) print('adding red noise') for psr in tempopsr: # print(psr.name) rednoise_toadd = noise_params[str(psr.name) + '_log10_A'] # print(psr.name) gamma = noise_params[str(psr.name) + '_gamma'] LT.add_rednoise(psr, 10**rednoise_toadd, gamma, components=30) psr.fit() print('adding GWB') LT.createGWB(tempopsr, Amp=amp, gam=13. / 3.) for psr in tempopsr: psr.fit() # save the pulsars pardir = './fakes_gwb/par/' timdir = './fakes_gwb/tim/' if not os.path.exists(pardir): os.makedirs(pardir) os.makedirs(timdir) print('fixing tim files') for psr in tempopsr: psr.savepar(pardir + psr.name + '-sim.par') psr.savetim(timdir + psr.name + '-sim.tim') libs.purgetim(timdir + psr.name + '-sim.tim') # fix the tim files
## Inject fdm. if args.inject_fdm: print("Injecting Fuzzy Dark Matter!") fdm_phase_e = random.random() * 2 * np.pi output_params.write('fdm_phase_e = ' + str(fdm_phase_e) + '\n') for psr in tempopsr: fdm_phase_p = random.random() * 2 * np.pi output_params.write(psr.name + ' fdm_phase_p = ' + str(fdm_phase_p) + '\n') LT.add_fdm(psr, args.fdm_A, args.fdm_f, fdm_phase_e, fdm_phase_p) # Inject a GWB. if args.inject_GWB: print("Injecting Gravitational Wave Background") LT.createGWB(tempopsr, Amp=args.GWB_A, gam=args.gamma) for psr in tempopsr: print(psr) if args.average_epoch: print("Cleaning up design matrix") for i in range(1, 126): psr[f'DMX_{i:04d}'].fit = False for par in ['JUMP1', 'FD1', 'FD2', 'FD3']: psr[par].fit = False dmsum = np.sum(psr.designmatrix(updatebats=False, incoffset=True)**2, axis=0)
LT.add_jitter(p, ecorr=noise_dict[p.name]['ecorrs'][:, 1], flagid='f', flags=noise_dict[p.name]['ecorrs'][:, 0], coarsegrain=1.0 / 86400.0, seed=seed_jitter + ii) except KeyError: pass ## add red noise LT.add_rednoise(p, noise_dict[p.name]['RN_Amp'], noise_dict[p.name]['RN_gamma'], components=30, seed=seed_red + ii) print(ii, p.name) # Create GWB # Takes a list of libstempo pulsar objects as input. LT.createGWB(t2psr, Amp=1.3e-15, gam=13. / 3., seed=seed_gwb_1) LT.createGWB(t2psr, Amp=1.0e-15, gam=7. / 3., seed=seed_gwb_2) psrs = [] for p in t2psr: psrs.append(Pulsar(p)) #Save sim pulsars to a pickle file with open(psr_pickle_file, 'wb') as psrfile: pickle.dump(psrs, psrfile) psrfile.close()
def create_dataset(dataset, Agwb): """ Create simulated dataset using 18 pulsars from NANOGrav 9-year stochastic analysis. Will use 11-year data span and red noise values with white noise values taken from most recent time-to-detection simulations. :param dataset: Name of output dataset. :param Agwb: Amplitude of injected GWB """ print 'Getting pulsar parameters for simulated dataset...' # get simulation data with open('nano9_simdata.json', 'r') as fp: pdict = json.load(fp) # get red noise dictionary with open('nano_red_dict.json', 'r') as fp: red_dict = json.load(fp) # get parfiles parfiles = glob.glob('../data/nano9_stipped_parfiles/*.par') parfiles = [ p for p in parfiles if p.split('/')[-1].split('_')[0] in pdict.keys() ] datadir = '../data/simulated_data/' + dataset if not os.path.exists(datadir): os.makedirs(datadir) print 'Making simulated data in directory ' + datadir psrs = [] for pf in parfiles: pname = pf.split('/')[-1].split('_')[0] psrs.append(LT.fakepulsar(pf, pdict[pname][0], pdict[pname][1])) for psr in psrs: # white noise LT.add_efac(psr) # red noise if pname in red_dict: LT.add_rednoise(psr, red_dict[pname][0], red_dict[pname][1], components=30) # GWB LT.createGWB(psrs, Agwb, 13. / 3., seed=None) for psr in psrs: psr.fit(iters=2) ## Save par and tim files psr.savepar(datadir + '/{0}_optstatsim.par'.format(psr.name)) psr.savetim(datadir + '/{0}_optstatsim.tim'.format(psr.name)) # make the hdf5 file for the simulated dataset print 'Making the hdf5 file for the simulated dataset...' datadir = '../data/simulated_data/' + dataset h5filename = datadir + '/sim.hdf5' os.system('python makeH5file.py \ --pardir {0} --timdir {0} \ --h5File {1}'.format(datadir, h5filename)) print 'Finished!'
seed=seed_jitter + ii) except KeyError: pass ## add red noise LT.add_rednoise(p, noise_dict[p.name]['RN_Amp'], noise_dict[p.name]['RN_gamma'], components=30, seed=seed_red + ii) print(ii, p.name) # Create GWB # Takes a list of libstempo pulsar objects as input. LT.createGWB(t2psr, Amp=1.5e-15, gam=13. / 3., seed=seed_gwb_1) LT.createGWB(t2psr, Amp=9.0e-15, gam=10. / 3., seed=seed_gwb_2) psrs = [] for p in t2psr: psrs.append(Pulsar(p)) # find the maximum time span to set GW frequency sampling selection = Selection(selections.by_backend) tmin = [p.toas.min() for p in psrs] tmax = [p.toas.max() for p in psrs] Tspan = np.max(tmax) - np.min(tmin) ##### parameters and priors #####