parfile = opts.parfile
  else:
    print >> sys.stderr, "No par file specified!"
    sys.exit(0)

  # get time/date for file creation
  now = datetime.datetime.now()

  # list of amplitude spectral densities, upper limits and spin-down ratios
  # for the IFOs
  asds = []
  sdlist = []

  # read data from par file
  try:
    par = pppu.psr_par(parfile)
  except:
    print >> sys.stderr, "Par file %s could not be opened!" % parfile
    sys.exit(0)

  pname = par['PSRJ']
  if not pname:
    print >> sys.stderr, "No PSRJ value in par file %s" % parfile

    pname = par['PSR']
    if not pname:
      print >> sys.stderr, "No PSR value in par file %s" % parfile
      sys.exit(0)

  # check if heterodyned data exists for this pulsar and each detector
  if not jointonly:
    resultsnode.set_parfile(pfile)
    resultsnode.set_outpath(outdir)

    if mcmcdirs:
      for mcmcdir in mcmcdirs:
        if not os.path.isdir(mcmcdir):
          print("MCMC directory %s not found!" % mcmcdir, file=sys.stderr)
          sys.exit(1)

      resultsnode.set_domcmc()
      resultsnode.set_mcmcdir(mcmcdirs)
    elif nesteddirs:
      resultsnode.set_donested()

      # get pulsar name from par file
      psr = pppu.psr_par(pfile)
      name = None
      try:
        name = psr['PSRJ']
      except:
        try:
          name = psr['PSR']
        except:
          try:
            name = psr['NAME']
          except:
            print("Cannot get pulsar name from par file" % mcmcdir, file=sys.stderr)
            sys.exit(1)

      # if name is one of the standard hardware injection names set hwinj to true
      if not hwinj and 'PULSAR' in name:
Beispiel #3
0
        resultsnode.set_parfile(pfile)
        resultsnode.set_outpath(outdir)

        if mcmcdirs:
            for mcmcdir in mcmcdirs:
                if not os.path.isdir(mcmcdir):
                    print >> sys.stderr, "MCMC directory %s not found!" % mcmcdir
                    sys.exit(1)

            resultsnode.set_domcmc()
            resultsnode.set_mcmcdir(mcmcdirs)
        elif nesteddirs:
            resultsnode.set_donested()

            # get pulsar name from par file
            psr = pppu.psr_par(pfile)
            name = None
            try:
                name = psr['PSRJ']
            except:
                try:
                    name = psr['PSR']
                except:
                    try:
                        name = psr['NAME']
                    except:
                        print >> sys.stderr, "Cannot get pulsar name from par file" % mcmcdir
                        sys.exit(1)

            # if name is one of the standard hardware injection names set hwinj to true
            if not hwinj and 'PULSAR' in name:
\usepackage{amsmath}
% The rotate command for deluxetable does not work in emulateapj, so use a
% landscape mode instead
\usepackage{lscape}

\\begin{document}
""")

  # perform sorting
  names = []
  freqs = []
  ras = []
  decs = []
  for i, parfile in enumerate(param_files):
    try:
      par = pppu.psr_par(parfile)
    except:
      print >> sys.stderr, "Par file %s could not be opened!" % parfile
      # remove from list and move on to next pulsar
      param_files.remove(i)
      continue

    if par['PSRJ']:
      names.append(par['PSRJ'])
    else:
      names.append(None)

    if par['F0']:
      freqs.append(par['F0'])
    else:
      freqs.append(None)
Beispiel #5
0
def pulsarBayesPostProc(
    outdir,
    data,
    upperlimit,
    histbins,
    #nested sampling options
    priorfile,
    parfile,
    ns_Nlive,
    # analysis data
    Bkdata=None,
    ifos=None,
    # pulsar information
    corfile=None  #,
    #Turn on 2D kdes
    #twodkdeplots=False,
    #Turn on R convergence tests
    #RconvergenceTests=False
):
    # check data is input
    if data is None:
        raise RuntimeError('You must specify an input data file')

    # check output directory is input
    if outdir is None:
        raise RuntimeError("You must specify an output directory.")

    # create output dir if it doesn't exist
    if not os.path.isdir(outdir):
        os.makedirs(outdir)

    # list of posteriors for each ifo
    poslist = []

    # loop over ifos and generate posteriors
    for idx, ifo in enumerate(ifos):
        # nested sampling data files for IFO idx
        idat = data[idx]

        # if input format is xml
        votfile = None
        if '.xml' in data[0]:
            peparser = bppu.PEOutputParser('xml')
            commonResultsObj = peparser.parse(idat[0])
            thefile = open(idat[0], 'r')
            votfile = thefile.read()
        else:  # input as standard nested sampling input
            peparser = bppu.PEOutputParser('ns')
            commonResultsObj = peparser.parse(idat,
                                              Nlive=ns_Nlive,
                                              xflag=False)

        # Create an instance of the posterior class using the posterior values
        # loaded from the file.
        pos = bppu.Posterior( commonResultsObj, SimInspiralTableEntry=None, \
                              votfile=votfile )

        # append to list
        poslist.append(pos)

    # read in par file if given
    if parfile is not None:
        par = pppu.psr_par(parfile)

    # convert phi0' and psi' to phi0 and psi
    if 'phi0prime' in pos.names and 'psiprime' in pos.names:
        phi0samps, psisamps = pppu.phipsiconvert( pos['phi0prime'].samples, \
                                                  pos['psiprime'].samples )
        # append phi0 and psi to posterior
        phi0_pos = bppu.OneDPosterior('phi0', phi0samps)
        pos.append(phi0_pos)

        psi_pos = bppu.OneDPosterior('psi', phi0samps)
        pos.append(psi_pos)

    # get parameters to plot from prior file and those with "Errors" in the par
    # file
    plotpars = []
    pri = pppu.psr_prior(priorfile)
    for pars in pppu.float_keys:
        # prior file values have preference over par file values with errors in the
        # nested sampling code
        pars_err = pars + '_ERR'

        if pri[pars] is not None:
            plotpars.append(pars)
        elif par[pars_err] is not None:
            plotpars.append(pars)

    # if h0 exists plot the pdf
    if 'H0' in plotpars:
        # set bounds of h0
        bounds = [0, float("inf")]

        plotFig, ulvals = pppu.plot_posterior_hist(poslist, 'H0'.lower(), ifos,
                                                   bounds, histbins,
                                                   upperlimit)

        # output plots for each IFO
        for i, ifo in enumerate(ifos):
            figname = 'H0'.lower() + '_' + ifo + '.png'
            oneDplotPath = os.path.join(outdir, figname)
            plotFig[i].savefig(oneDplotPath)

    # if the heterodyned data files have been included create time series plots
    # of |B_k| and a amplitude spectral density spectrogram of the data
    if Bkdata is not None:
        Bkfigs, ASDfigs = pppu.plot_Bks_ASDs(Bkdata, ifos)

        # output plots
        for i, ifo in enumerate(ifos):
            figname = 'Bk' + '_' + ifo + '.png'
            Bkplotpath = os.path.join(outdir, figname)
            Bkfigs[i].savefig(Bkplotpath)

            figname = 'ASD' + '_' + ifo + '.png'
            ASDplotpath = os.path.join(outdir, figname)
            ASDfigs[i].savefig(ASDplotpath)
def pulsarBayesPostProc( outdir, data,
                         upperlimit, histbins,
                         #nested sampling options
                         priorfile, parfile, ns_Nlive,
                         # analysis data
                         Bkdata=None, ifos=None,
                         # pulsar information
                         corfile=None #,
                         #Turn on 2D kdes
                         #twodkdeplots=False,
                         #Turn on R convergence tests
                         #RconvergenceTests=False
                       ):
  # check data is input
  if data is None:
    raise RuntimeError('You must specify an input data file')
  
  # check output directory is input
  if outdir is None:
    raise RuntimeError("You must specify an output directory.")
  
  # create output dir if it doesn't exist
  if not os.path.isdir(outdir):
    os.makedirs(outdir)
  
  # list of posteriors for each ifo
  poslist = []
  
  # loop over ifos and generate posteriors
  for idx, ifo in enumerate(ifos):
    # nested sampling data files for IFO idx
    idat = data[idx]
    
    # if input format is xml
    votfile = None
    if '.xml' in data[0]:
      peparser = bppu.PEOutputParser('xml')
      commonResultsObj = peparser.parse(idat[0])
      thefile = open(idat[0],'r')
      votfile = thefile.read()
    else: # input as standard nested sampling input
      peparser = bppu.PEOutputParser('ns')
      commonResultsObj = peparser.parse(idat, Nlive=ns_Nlive, xflag=False)

    # Create an instance of the posterior class using the posterior values
    # loaded from the file.
    pos = bppu.Posterior( commonResultsObj, SimInspiralTableEntry=None, \
                          votfile=votfile )
   
    # append to list
    poslist.append(pos)
  
  # read in par file if given
  if parfile is not None:
    par = pppu.psr_par(parfile)
  
  # convert phi0' and psi' to phi0 and psi
  if 'phi0prime' in pos.names and 'psiprime' in pos.names:
    phi0samps, psisamps = pppu.phipsiconvert( pos['phi0prime'].samples, \
                                              pos['psiprime'].samples )
    # append phi0 and psi to posterior
    phi0_pos = bppu.OneDPosterior('phi0', phi0samps)
    pos.append(phi0_pos)
    
    psi_pos = bppu.OneDPosterior('psi', phi0samps)
    pos.append(psi_pos)
   
  # get parameters to plot from prior file and those with "Errors" in the par
  # file
  plotpars = []
  pri = pppu.psr_prior(priorfile)
  for pars in pppu.float_keys:
    # prior file values have preference over par file values with errors in the
    # nested sampling code
    pars_err = pars+'_ERR'
    
    if pri[pars] is not None:
      plotpars.append(pars)
    elif par[pars_err] is not None:
      plotpars.append(pars)
  
  # if h0 exists plot the pdf
  if 'H0' in plotpars:
    # set bounds of h0
    bounds = [0, float("inf")]
    
    plotFig, ulvals = pppu.plot_posterior_hist( poslist, 'H0'.lower(), ifos, 
                                                bounds, histbins, upperlimit )
    
    # output plots for each IFO
    for i, ifo in enumerate(ifos):
      figname = 'H0'.lower()+'_'+ifo+'.png'
      oneDplotPath = os.path.join(outdir, figname)
      plotFig[i].savefig(oneDplotPath)
      
  # if the heterodyned data files have been included create time series plots
  # of |B_k| and a amplitude spectral density spectrogram of the data
  if Bkdata is not None:
    Bkfigs, ASDfigs = pppu.plot_Bks_ASDs( Bkdata, ifos )
    
    # output plots
    for i, ifo in enumerate(ifos):
      figname = 'Bk'+'_'+ifo+'.png'
      Bkplotpath = os.path.join(outdir, figname)
      Bkfigs[i].savefig(Bkplotpath)
      
      figname = 'ASD'+'_'+ifo+'.png'
      ASDplotpath = os.path.join(outdir, figname)
      ASDfigs[i].savefig(ASDplotpath)