def makeSkyMap(samples, lmax, nside=16, psrs=None, cmap='YlOrBr'): # number of pixels total npix = hp.nside2npix(nside) # initialize theta and phi map coordinantes skypos=[] for ii in range(npix): skypos.append(np.array(hp.pix2ang(nside,ii))) skypos = np.array(skypos) harmvals = PALutils.SetupSkymapPlottingGrid(lmax,skypos) pwr = [] for ii in range(len(samples)): samples_tot = np.append(2.*np.sqrt(np.pi), samples[ii]) gwp = PALutils.GWpower(samples_tot, harmvals) pwr.append(gwp) pwr = np.array(pwr) pwr = np.mean(pwr, axis=0) ax = plt.subplot(111, projection='astro mollweide') ax.grid() plot.outline_text(ax) plot.healpix_heatmap(pwr, cmap=cmap) plt.colorbar(orientation='horizontal') # add pulsars locations if np.all(psrs): ax.plot(psrs[:,0], psrs[:,1], '*', color='w', markersize=6, mew=1, mec='w') return pwr
def init_os_common(dataset, h5file, psrlist, nf, noisedir=None, noVaryNoise=False, incJitterEquad=True, incEquad=True, outputfile=None): model = PALmodels.PTAmodels(h5file, pulsars=psrlist) # make model dictionary, can play with number of frequencies here # Note: Tmax=0 means it will use the largest time span in the array to set the frequency bin size fullmodel = model.makeModelDict(incRedNoise=True, incEquad=incEquad, incJitterEquad=incJitterEquad, likfunc='mark9', incGWB=True, incEphemMixture=False, nfreqs=nf, Tmax=0) # loop through pulsars and fix white noise parameters to values in file if noisedir is not None: for ct, p in enumerate(model.psr): d = np.genfromtxt(noisedir + p.name + '_noise.txt', dtype='S42') pars = d[:,0] vals = np.array([float(d[ii,2]) for ii in range(d.shape[0])]) sigs = [psig for psig in fullmodel['signals'] if psig['pulsarind'] == ct] sigs = PALutils.fixNoiseValues(sigs, vals, pars, bvary=False, verbose=False) # turn back on red-noise parameters (will use later when drawing from posteriors) for sig in fullmodel['signals']: if sig['corr'] == 'single' and sig['stype'] == 'powerlaw': sig['bvary'][1] = True sig['bvary'][0] = True if noVaryNoise: nflags = ['efac', 'equad', 'jitter', 'jitter_equad'] for sig in fullmodel['signals']: if sig['stype'] in nflags: sig['bvary'][0] = False # intialize mdoel model.initModel(fullmodel, memsave=True, write='no') # start parameters off at initial values (i.e the fixed values red in above) p0 = model.initParameters(fixpstart=True) # essentially turn off GWB component (again we will do something different when drawing from full PTA posterior) p0[-2] = -19 # call once to fix white noise xi, rho, sig, Opt, Sig = model.opt_stat_mark9(p0) if outputfile is not None: f = open(outputfile, 'w') f.write('Opt {0}\n'.format(Opt)) f.write('Sig {0}\n'.format(Sig)) f.write('SNR {0}\n'.format(Opt/Sig)) f.close() return model
sig['pstart'])) #for sig in fullmodel['signals']: # if sig['stype'] == 'jitter_equad': # if sig['flagvalue'] == 'J1741+1351-430_ASP': # sig['bvary'][0] = False if args.fixNoise: noisedir = args.noisedir for ct, p in enumerate(model.psr): d = np.genfromtxt(noisedir + p.name + '_noise.txt', dtype='S42') pars = d[:,0] vals = np.array([float(d[ii,1]) for ii in range(d.shape[0])]) sigs = [psig for psig in fullmodel['signals'] if psig['pulsarind'] == ct] sigs = PALutils.fixNoiseValues(sigs, vals, pars, bvary=False, verbose=True) # turn red noise back on if args.fixNoise and args.fixWhite: print('Turning on red noise and only fixing white noise') for sig in fullmodel['signals']: if sig['corr'] == 'single' and sig['stype'] == 'powerlaw': sig['bvary'][1] = True sig['bvary'][0] = True # write JSON file if not args.incTimingModel: model.writeModelToFile(fullmodel, args.outDir + '/model.json') # check for single efacs
separateJitter=False, separateJitterEquadByFreq=True, incEquad=incEquad, incJitter=False, incJitterEquad=incJitterEquad, incGWB=False, nfreqs=50, ndmfreqs=50, Tmax=0, incBWM=incBWM, compression='None', likfunc=likfunc) if args.noisedir is None: noisedir = outdir + '/noisefiles/' else: noisedir = args.noisedir + '/' for ct, p in enumerate(model.psr): d = np.genfromtxt(noisedir + p.name + '_noise.txt', dtype='S42') pars = d[:,0] vals = np.array([float(d[ii,1]) for ii in range(d.shape[0])]) sigs = [psig for psig in fullmodel['signals'] if psig['pulsarind'] == ct] sigs = PALutils.fixNoiseValues(sigs, vals, pars, bvary=False, verbose=False) # turn red noise back on if args.pipeline == 'BWM': print 'Turning on red noise and only fixing white noise' for sig in fullmodel['signals']: if sig['corr'] == 'single' and sig['stype'] == 'powerlaw': sig['bvary'][1] = True sig['bvary'][0] = True model.initModel(fullmodel, memsave=True, fromFile=False, verbose=False, write='no') # set initial parameters p0 = model.initParameters(fixpstart=True, startEfacAtOne=False) if args.pta in ['nanograv5', 'nanograv9']:
def readPulsar(self, psr, psrname, start_time=None, end_time=None): psr.name = str(psrname) psr.base_ephem = str( self.getData(psrname, 'base_ephem', required=False)) # Read the content of the par/tim files in a string psr.parfile_content = str( self.getData(psrname, 'parfile', required=False)) psr.timfile_content = str( self.getData(psrname, 'timfile', required=False)) # Read the timing model parameter descriptions psr.ptmdescription = map(str, self.getData(psrname, 'tmp_name')) psr.ptmpars = np.array(self.getData(psrname, 'tmp_valpost')) psr.ptmparerrs = np.array(self.getData(psrname, 'tmp_errpost')) psr.flags = np.array( map(str, self.getData(psrname, 'efacequad_freq', 'Flags'))) psr.tobsflags = np.array( map(float, self.getData(psrname, 'tobs_all', 'Flags'))) try: psr.bwflags = np.array( map(float, self.getData(psrname, 'bwflags', 'Flags'))) except IOError: psr.bwflags = np.ones(len(psr.flags)) * 4.0 psr.setptmdescription = map(str, self.getData(psrname, 'set_name')) psr.setptmpars = np.array(self.getData(psrname, 'set_valpost')) # add this for frequency dependent terms #TODO: should eventually change psr.flags to a dictionary psr.fflags = np.array( map(str, self.getData(psrname, 'efacequad_freq', 'Flags'))) # Read the position of the pulsar rajind = np.flatnonzero(np.array(psr.setptmdescription) == 'RAJ') decjind = np.flatnonzero(np.array(psr.setptmdescription) == 'DECJ') # look for ecliptic coordinates if len(rajind) == 0 and len(decjind) == 0: #print 'Could not fine RAJ or DECJ. Looking for ecliptic coords...' elongind = np.flatnonzero( np.array(psr.setptmdescription) == 'ELONG') elatind = np.flatnonzero(np.array(psr.setptmdescription) == 'ELAT') elong = np.array(self.getData(psrname, 'set_valpost'))[elongind] elat = np.array(self.getData(psrname, 'set_valpost'))[elatind] # convert via pyephem #print elong, elat try: ec = pyephem.Ecliptic(elong, elat) # check for B name if 'B' in psr.name: epoch = '1950' else: epoch = '2000' eq = pyephem.Equatorial(ec, epoch=epoch) psr.raj = np.double([eq.ra]) psr.decj = np.double([eq.dec]) except TypeError: print 'WARNING: Cannot find sky location coordinates.' \ 'Setting to 0.' psr.raj = np.array([0.0]) psr.decj = np.array([0.0]) else: psr.raj = np.array(self.getData(psrname, 'set_valpost'))[rajind] psr.decj = np.array(self.getData(psrname, 'set_valpost'))[decjind] psr.theta = np.pi / 2 - psr.decj psr.phi = psr.raj # period of pulsar perind = np.flatnonzero(np.array(psr.ptmdescription) == 'F0') psr.period = 1 / np.array(self.getData(psrname, 'tmp_valpost'))[perind] # pulsar distance and uncertainty psr.pdist = np.double(self.getData(psrname, 'pdist')) psr.pdistErr = np.double(self.getData(psrname, 'pdistErr')) # ppdm psr.ppdm = np.array(self.getData(psrname, 'ppdm', 'Flags')) psr.ppdmerr = np.array(self.getData(psrname, 'ppdme', 'Flags')) if np.all(psr.ppdm != 0): dmin = psr.ppdmerr == 0 psr.ppdmerr[dmin] = 1e10 # Obtain residuals, TOAs, etc. psr.toas = np.array(self.getData(psrname, 'TOAs')) psr.toaerrs = np.array(self.getData(psrname, 'toaErr')) psr.residuals = np.array(self.getData(psrname, 'postfitRes')) psr.detresiduals = np.array(self.getData(psrname, 'postfitRes')) psr.freqs = np.array(self.getData(psrname, 'freq')) psr.Mmat = np.array(self.getData(psrname, 'designmatrix')) # filter data based on start and end times if start_time is not None or end_time is not None: if start_time is None: start_time = psr.toas.min() / 86400 if end_time is None: end_time = psr.toas.max() / 86400 print 'Filtering TOAs between {0} and {1} for PSR {2}'.format( start_time, end_time, psr.name) ind = np.logical_and(psr.toas / 86400 >= start_time, psr.toas / 86400 <= end_time) psr.toas = psr.toas[ind] psr.toaerrs = psr.toaerrs[ind] psr.residuals = psr.residuals[ind] psr.detresiduals = psr.detresiduals[ind] psr.freqs = psr.freqs[ind] psr.flags = psr.flags[ind] psr.fflags = psr.fflags[ind] psr.tobsflags = psr.tobsflags[ind] psr.bwflags = psr.bwflags[ind] # design matrix print 'Design Matrix Shape pre-filter:', psr.Mmat.shape psr.Mmat = psr.Mmat[ind, :] dind = psr.Mmat.sum(axis=0) == 0 psr.Mmat = psr.Mmat[:, ~dind] print 'Design Matrix Shape post-filter:', psr.Mmat.shape # fitted parameters psr.ptmdescription = list(np.array(psr.ptmdescription)[~dind]) psr.ptmpars = psr.ptmpars[~dind] psr.ptmparerrs = psr.ptmparerrs[~dind] # get number of epochs (i.e 1 s window) try: (avetoas, aveflags, Umat) = PALutils.exploderMatrixNoSingles(psr.toas, np.array(psr.flags), dt=1) psr.nepoch = len(avetoas) except IndexError: pass
def readPulsar(self, psr, psrname, start_time=None, end_time=None): psr.name = str(psrname) # Read the content of the par/tim files in a string psr.parfile_content = str(self.getData(psrname, 'parfile', required=False)) psr.timfile_content = str(self.getData(psrname, 'timfile', required=False)) # Read the timing model parameter descriptions psr.ptmdescription = map(str, self.getData(psrname, 'tmp_name')) psr.ptmpars = np.array(self.getData(psrname, 'tmp_valpost')) psr.ptmparerrs = np.array(self.getData(psrname, 'tmp_errpost')) psr.flags = np.array(map(str, self.getData(psrname, 'efacequad_freq', 'Flags'))) psr.tobsflags = np.array(map(float, self.getData(psrname, 'tobs_all', 'Flags'))) try: psr.bwflags = np.array(map(float, self.getData(psrname, 'bwflags', 'Flags'))) except IOError: psr.bwflags = np.ones(len(psr.flags)) * 4.0 psr.setptmdescription = map(str, self.getData(psrname, 'set_name')) psr.setptmpars = np.array(self.getData(psrname, 'set_valpost')) # add this for frequency dependent terms #TODO: should eventually change psr.flags to a dictionary psr.fflags = np.array(map(str, self.getData(psrname, 'efacequad_freq', 'Flags'))) # Read the position of the pulsar rajind = np.flatnonzero(np.array(psr.setptmdescription) == 'RAJ') decjind = np.flatnonzero(np.array(psr.setptmdescription) == 'DECJ') # look for ecliptic coordinates if len(rajind) == 0 and len(decjind) == 0: #print 'Could not fine RAJ or DECJ. Looking for ecliptic coords...' elongind = np.flatnonzero(np.array(psr.setptmdescription) == 'ELONG') elatind = np.flatnonzero(np.array(psr.setptmdescription) == 'ELAT') elong = np.array(self.getData(psrname, 'set_valpost'))[elongind] elat = np.array(self.getData(psrname, 'set_valpost'))[elatind] # convert via pyephem #print elong, elat try: ec = ephem.Ecliptic(elong, elat) # check for B name if 'B' in psr.name: epoch = '1950' else: epoch = '2000' eq = ephem.Equatorial(ec, epoch=epoch) psr.raj = np.double([eq.ra]) psr.decj = np.double([eq.dec]) except TypeError: print 'WARNING: Cannot find sky location coordinates.' \ 'Setting to 0.' psr.raj = np.array([0.0]) psr.decj = np.array([0.0]) else: psr.raj = np.array(self.getData(psrname, 'set_valpost'))[rajind] psr.decj = np.array(self.getData(psrname, 'set_valpost'))[decjind] psr.theta = np.pi/2 - psr.decj psr.phi = psr.raj # period of pulsar perind = np.flatnonzero(np.array(psr.ptmdescription) == 'F0') psr.period = 1/np.array(self.getData(psrname, 'tmp_valpost'))[perind] # pulsar distance and uncertainty psr.pdist = np.double(self.getData(psrname, 'pdist')) psr.pdistErr = np.double(self.getData(psrname, 'pdistErr')) # ppdm psr.ppdm = np.array(self.getData(psrname, 'ppdm', 'Flags')) psr.ppdmerr = np.array(self.getData(psrname, 'ppdme', 'Flags')) if np.all(psr.ppdm!=0): dmin = psr.ppdmerr == 0 psr.ppdmerr[dmin] = np.min(psr.ppdmerr[~dmin]) # Obtain residuals, TOAs, etc. psr.toas = np.array(self.getData(psrname, 'TOAs')) psr.toaerrs = np.array(self.getData(psrname, 'toaErr')) psr.residuals = np.array(self.getData(psrname, 'postfitRes')) psr.detresiduals = np.array(self.getData(psrname, 'postfitRes')) psr.freqs = np.array(self.getData(psrname, 'freq')) psr.Mmat = np.array(self.getData(psrname, 'designmatrix')) # filter data based on start and end times if start_time is not None or end_time is not None: if start_time is None: start_time = psr.toas.min()/86400 if end_time is None: end_time = psr.toas.max()/86400 print 'Filtering TOAs between {0} and {1} for PSR {2}'.format( start_time, end_time, psr.name) ind = np.logical_and(psr.toas/86400>=start_time, psr.toas/86400<=end_time) psr.toas = psr.toas[ind] psr.toaerrs = psr.toaerrs[ind] psr.residuals = psr.residuals[ind] psr.detresiduals = psr.detresiduals[ind] psr.freqs = psr.freqs[ind] psr.flags = psr.flags[ind] psr.fflags = psr.fflags[ind] psr.tobsflags = psr.tobsflags[ind] psr.bwflags = psr.bwflags[ind] # design matrix print 'Design Matrix Shape pre-filter:', psr.Mmat.shape psr.Mmat = psr.Mmat[ind,:] dind = psr.Mmat.sum(axis=0) == 0 psr.Mmat = psr.Mmat[:,~dind] print 'Design Matrix Shape post-filter:', psr.Mmat.shape # fitted parameters psr.ptmdescription = list(np.array(psr.ptmdescription)[~dind]) psr.ptmpars = psr.ptmpars[~dind] psr.ptmparerrs = psr.ptmparerrs[~dind] # get number of epochs (i.e 1 s window) try: (avetoas, aveflags, Umat) = PALutils.exploderMatrixNoSingles( psr.toas, np.array(psr.flags), dt=1) psr.nepoch = len(avetoas) except IndexError: pass
sig['pstart']) #for sig in fullmodel['signals']: # if sig['stype'] == 'jitter_equad': # if sig['flagvalue'] == 'J1741+1351-430_ASP': # sig['bvary'][0] = False if args.fixNoise: noisedir = args.noisedir for ct, p in enumerate(model.psr): d = np.genfromtxt(noisedir + p.name + '_noise.txt', dtype='S42') pars = d[:,0] vals = np.array([float(d[ii,1]) for ii in range(d.shape[0])]) sigs = [psig for psig in fullmodel['signals'] if psig['pulsarind'] == ct] sigs = PALutils.fixNoiseValues(sigs, vals, pars, bvary=False, verbose=True) # turn red noise back on if args.fixNoise and args.fixWhite: print 'Turning on red noise and only fixing white noise' for sig in fullmodel['signals']: if sig['corr'] == 'single' and sig['stype'] == 'powerlaw': sig['bvary'][1] = True sig['bvary'][0] = True # write JSON file if not args.incTimingModel: model.writeModelToFile(fullmodel, args.outDir + '/model.json') # check for single efacs