Ejemplo n.º 1
0
def mass(z, zo):
  s = c * stattools.Sbi(z) / (1+zo)
  # using z=0 as the cosmological redshift
  if '--z=0' in sys.argv:
    zo = 0
  m = scalings.sigma(s, zo)
  r = conversions.rsph(m, zo, ref='200c', unit='kpc')
  return s, m, r
Ejemplo n.º 2
0
def run(center, min_Nperbin=15, min_binsize=250,
        maingap=500, maxgap=1000, sigma=False, version='1'):
  #center = centers[i]
  id = center[0]
  xo = center[1]
  yo = center[2]
  zo = center[3]

  j = close(xo, yo, zo, ra, dec, z)
  r = astCoords.calcAngSepDeg(xo, yo, ra[j], dec[j])
  r = scipy.array([cosmology.dProj(zo, ri, input_unit='deg', unit='Mpc') \
                   for ri in r])
  if '2' in version:
      r *= (1 + zo)
  r *= 1e3
  outplot = get_plotname(version, id)
  #output = 'phase%s/plots/v%s/rv/halo-%d.png' %(phase, version, id)
  m = members.sgapper(r, z[j], zo=zo, min_Nperbin=min_Nperbin,
                      maingap=maingap, maxgap=maxgap, sigma=sigma,
                      verbose=False, debug=False, plot_output=outplot,
                      full_output=True)
  m, binsize, nit, incut = m
  Nm = len(m)

  zo = stattools.Cbi(z[j[m]])
  s, m200, r200 = mass(z[j[m]], zo)
  mo = m[r[m] <= r200]
  n200 = -1
  it = []
  while n200 != len(mo):
    try:
      n200 = len(mo)
      s, m200, r200 = mass(z[j[mo]], zo)
      #zo = stattools.Cbi(z[j[mo]])
      mo = m[r[m] <= r200]
      # should maybe find a more sophisticated solution by, say, averaging
      # all items from one of these "cycles"
      if r200 in it:
        break
      it.append(r200)
    except ZeroDivisionError:
      print 'broke'
      break

  if n200 > 15:
    stats = (stattools.Cbi, stattools.Sbi)
    z_err, s_err = stattools.bootstrap(stats, z[j[incut]],
                                       n_obj=Nm, n_samples=1000)
    s_err = c * s_err / (1+zo)
  else:
    z_err = scipy.std(z) / scipy.sqrt(Nm)
    s_err = c/(1+zo) * z_err / scipy.sqrt(2)
  m200, m200_err = scalings.sigma(s, zo, s_err, z_err,
                                  separate_errors=True)
  r200 = conversions.rsph(m200, zo)

  # to make it their format
  m200_err = [scipy.log10(1+me/m200) for me in m200_err]

  try:
    #if n200 > 
    line = '%4d  %.3f  %.3f  %6d  %.1e  %4d  %.1f  %4d  %.2f  %.2f  %4d' \
           %(id, xo*deg2rad, yo*deg2rad, round(c*zo, 0), m200,
             round(s, 0), r200/1e3, n200, m200_err[0], m200_err[1], Nm)
  except TypeError:
    line = '%4d  %.3f  %.3f  %6d     -1      -1   -1    -1    -1    -1  %4d' \
           %(id, xo*deg2rad, yo*deg2rad, round(c*zo, 0), Nm)
  print line, '%4.1f' %max(r[m]/1e3)
  out = open(output, 'a')
  print >>out, line
  out.close()

  write_members(id, galid[j[m]], galid[j[mo]])
  return m200
Ejemplo n.º 3
0
def M200(r, z, ntot=0, errors=False, converge=True, verbose=False):
  """
  ntot is to be used in the bootstrap resampling
  """
  c = 299792.458
  def _mass(z, zo):
    s = c * stattools.Sbi(z) / (1 + zo)
    m = scalings.sigma(s, zo)
    r200 = conversions.rsph(m, zo, ref='200c', unit='kpc')
    return s, m, r200

  zo = stattools.Cbi(z)
  s, m, r200 = _mass(z, zo)
  if verbose:
    print 'iteration 0:'
    print '  sigma = %d km/s' %int(s)
    print '  m200 = %.1e' %m
    print '  r200 = %d' %int(r200)
    print '  Ngal = %d' %len(r)
  j = scipy.arange(len(r), dtype=int)
  if converge:
    jo = j[r < r200]
    n200 = 0
    it = []
    while len(jo) != n200:
      try:
        n200 = len(jo)
        s, m, r200 = _mass(z[jo], zo)
        if verbose:
          print 'iteration %d:' %(len(it)+1)
          try:
            print '  sigma = %d km/s' %int(s)
            print '  m200 = %.1e' %m
            print '  r200 = %d' %int(r200)
            print '  n200 = %d' %n200
          except ValueError:
            print '  Measurement failed'
        jo = j[r < r200]
        if r200 in it:
          break
        it.append(r200)
      except ZeroDivisionError:
        break
  else:
    n200 = len(r)
  if n200 == 0:
    return zo, -1, (-1, (-1,-1)), -1, -1
  if errors:
    if n200 > 15:
      stats = (stattools.Cbi, stattools.Sbi)
      z_err, s_err = stattools.bootstrap(stats, z, ntot)
      s_err *= c / (1 + zo)
    else:
      z_err = scipy.std(z) / scipy.sqrt(n200)
      s_err = c/(1+zo) * z_err / scipy.sqrt(2)
    m = scalings.sigma(s, zo, ds=s_err, dz=z_err,
                       scaling='evrard08', separate_errors=True)
    r = conversions.rsph(m[0], zo, ref='200c', unit='kpc')
    merr = [scipy.log10(1+me/m[0]) for me in m[1]]
    m = (m[0], merr)
  else:
    m = scalings.sigma(s, zo)
    r = conversions.rsph(m, zo, ref='200c', unit='kpc')
  return zo, s, m, r, n200
Ejemplo n.º 4
0
 def _mass(z, zo):
   s = c * stattools.Sbi(z) / (1 + zo)
   m = scalings.sigma(s, zo)
   r200 = conversions.rsph(m, zo, ref='200c', unit='kpc')
   return s, m, r200
Ejemplo n.º 5
0
def run(catalog='HOD2_SW'):
    print catalog
    halofile = os.path.join('Phase_2_input_catalogues',
                            '{0}_input_halo_data_M200c.txt'.format(catalog))
    truehalos = readfile.dict(halofile)
    truehalos['logm200'] = truehalos.pop('m200')
    truehalos['m200'] = 10**truehalos['logm200']
    truehalos['z'] = truehalos['v_res'] / c
    truehalos['m200hz'] = cosmology.E(truehalos['z']) * truehalos['m200']
    Nhalo = len(truehalos['halo-id'])
    galfile = os.path.join('Phase_2_input_catalogues',
                           '{0}_input_galaxy_data.txt'.format(catalog))
    truegals = readfile.dict(galfile)
    Ngals = len(truegals)
    s = [
        stattools.Sbi(truegals['v_res'][truegals['halo-id'] == i])
        for i in xrange(1, Nhalo + 1)
    ]
    truehalos['sigma'] = scipy.array(s) / (1 + truehalos['z'])
    # write this out
    output = '{0}_input_halo_data_M200c_sigma.txt'.format(catalog)
    out = open(output, 'w')
    print >> out, '# halo-id  m200  ra  dec  z  vdisp'
    for i in xrange(Nhalo):
        print >>out, '%d  %.3e  %.9f  %.9f  %.6f  %7.2f' \
                     %(truehalos['halo-id'][i], truehalos['m200'][i],
                       truehalos['ra'][i], truehalos['dec'][i],
                       truehalos['z'][i], truehalos['sigma'][i])
    out.close()
    print 'Saved to', output

    # fit scaling relation
    j = scipy.arange(Nhalo)
    #j = j[truehalos['m200hz'] >= 1e14]
    Mo = int(round(scipy.median(truehalos['m200'][j]), 0))
    So = int(round(scipy.median(truehalos['sigma'][j]), 0))
    s = scipy.logspace(2, 3.3, 100)
    t = scipy.logspace(13, 15.5, 100)
    #Amle, Bmle, Smle = lnr.mle(truehalos['sigma']/So, truehalos['m200'],
    #logify=True)
    Amle, Bmle, Smle = lnr.mle(truehalos['m200hz'][j] / Mo,
                               truehalos['sigma'][j],
                               logify=True)
    print Amle, Bmle, Smle
    #A, B, Sint = lnr.mcmc(truehalos['sigma']/So, truehalos['m200'],
    #nsteps=2000, logify=True, output=(50,16,84))
    A, B, Sint = lnr.mcmc(truehalos['m200hz'][j] / Mo,
                          truehalos['sigma'][j],
                          nsteps=2000,
                          logify=True,
                          output=(50, 16, 84))
    print A
    print B
    print Sint
    #pylab.plot(truehalos['sigma'], truehalos['m200'], 'k.')
    pylab.plot(truehalos['m200hz'], truehalos['sigma'], 'k.')
    if len(j) < Nhalo:
        pylab.plot(truehalos['m200hz'][j], truehalos['sigma'][j], 'b.')
    #bestfit = '$S_0=%d$\n$A=%.3f$\n$B=%.3f$' %(So, A, B)
    #bestfit = r'$S_0=%d\,\mathrm{km\,s^{-1}}$' %(So)
    #bestfit += '\n'
    #bestfit += r'$A=%.3f_{-%.4f}^{+%.4f}$' %(A[0], A[0]-A[1], A[2]-A[0])
    #bestfit += '\n'
    bestfit = r'$B=%.3f_{-%.3f}^{+%.3f}$' % (B[0], B[0] - B[1], B[2] - B[0])
    pylab.plot(t, 10**A[0] * (t / Mo)**B[0], 'r-', lw=2)  #, label=bestfit)
    #Slabel = r'$\sigma_{M|S}=%.3f_{-%.3f}^{+%.3f}$' \
    #%(Sint[0], Sint[0]-Sint[1], Sint[2]-Sint[0])
    Slabel = r'$\sigma_{S|M}=%.3f_{-%.3f}^{+%.3f}$' \
             %(Sint[0], Sint[0]-Sint[1], Sint[2]-Sint[0])
    #pylab.plot(t, (1-Sint[0]) * 10**A[0] * (t/Mo)**B[0], 'r--')
    #pylab.plot(t, (1+Sint[0]) * 10**A[0] * (t/Mo)**B[0], 'r--')#,
    #label=Slabel)
    pylab.plot(scalings.sigma(s, 0, scaling='evrard08'),
               s,
               'c-',
               lw=2,
               label='Evrard+08')
    pylab.plot(scalings.sigma(s, 0, scaling='munari13dm'),
               s,
               '-',
               color='orange',
               lw=2,
               label='Munari+13 DM')
    pylab.plot(scalings.sigma(s, 0, scaling='munari13sub'),
               s,
               '--',
               color='orange',
               lw=2,
               label='Munari+13 sub')
    pylab.plot(scalings.sigma(s, 0, scaling='munari13gal'),
               s,
               ':',
               color='orange',
               lw=2,
               label='Munari+13 gal')
    if catalog[-2:] == 'SW':
        depth = 'shallow'
    elif catalog[-2:] == 'DN':
        depth = 'deep'
    msg = '{0} {1}'.format(catalog[:4], depth)
    pylab.annotate(msg,
                   xy=(0.04, 0.96),
                   xycoords='axes fraction',
                   ha='left',
                   va='top')
    l = pylab.legend(loc='lower right')
    l.get_frame().set_alpha(0)
    pylab.ylim(80, 2000)
    pylab.xlim(1e13, 2e15)
    return