def get_lf(lf, sid, bins, use_smooth_maps=False, use_binvol2 = False): # Bin data. This is only for visualisation and to compare # with reported binned values. m = lf.M1450[lf.sid==sid] #print 'Originally have {:d} quasars'.format(np.size(m)) #print 'Should have {:d} quasars'.format(np.size(m[m<-24.0])) ms = (bins[1:]+bins[:-1])*0.5 limit = np.max(ms) m = m[m<-24.0] # print 'bins=', bins #print 'limit=', limit #print 'Have {:d} quasars'.format(np.size(m)) if use_smooth_maps: selmaps = [x for x in smoothmaps if x.sid == sid] else: selmaps = [x for x in lf.maps if x.sid == sid] v1 = np.array([totBinVol(lf, x, bins, selmaps, use_binvol2=use_binvol2) for x in m]) v1_nonzero = v1[np.where(v1>0.0)] m = m[np.where(v1>0.0)] h = np.histogram(m, bins=bins, weights=1.0/(v1_nonzero)) nums = h[0] mags = (h[1][:-1] + h[1][1:])*0.5 dmags = np.diff(h[1])*0.5 left = mags - h[1][:-1] right = h[1][1:] - mags phi = nums logphi = np.log10(phi) # cMpc^-3 mag^-1 # Calculate errorbars on our binned LF. These have been estimated # using Equations 1 and 2 of Gehrels 1986 (ApJ 303 336), as # implemented in astropy.stats.poisson_conf_interval. The # interval='frequentist-confidence' option to that astropy function is # exactly equal to the Gehrels formulas, although the documentation # does not say so. n = np.histogram(m, bins=bins)[0] nlims = pci(n,interval='frequentist-confidence') nlims *= phi/n uperr = np.log10(nlims[1]) - logphi downerr = logphi - np.log10(nlims[0]) # for a in zip(mags, n): # print '{:.2f} {:d}'.format(a[0], a[1]) return mags, left, right, logphi, uperr, downerr
def get_lf(lf, sid, z_plot, special='None'): # Bin data. This is only for visualisation and to compare # with reported binned values. m = lf.M1450[lf.sid == sid] selmaps = [x for x in lf.maps if x.sid == sid] if sid == 6: # Glikman's sample needs wider bins. bins = np.array([-26.0, -25.0, -24.0, -23.0, -22.0, -21]) elif sid == 7: bins = np.array([-23.5, -21.5, -20.5, -19.5, -18.5]) elif sid == 10 or sid == 18: bins = np.arange(-30.9, -17.3, 1.8) elif special == 'croom_comparison': # These M1450 bins result in the Mgz2 bins of Croom09. The # 1.23 converts between the two magnitudes (Eqn B8 of Ross13). bins = np.arange(-30, -19.5, 0.5) + 1.23 elif special == 'croom_comparison_Mgz2': # These Mgz2 bins of Croom09. bins = np.arange(-30, -19.5, 0.5) else: bins = np.arange(-30.9, -17.3, 0.6) v1 = np.array([totBinVol(lf, x, bins, selmaps) for x in m]) v1_nonzero = v1[np.where(v1 > 0.0)] m = m[np.where(v1 > 0.0)] h = np.histogram(m, bins=bins, weights=1.0 / (v1_nonzero)) nums = h[0] mags = (h[1][:-1] + h[1][1:]) * 0.5 dmags = np.diff(h[1]) * 0.5 left = mags - h[1][:-1] right = h[1][1:] - mags phi = nums logphi = np.log10(phi) # cMpc^-3 mag^-1 # print 'sid=', sid # print 'mags=', mags # print 'nums=', nums # print 'total=', np.sum(nums) # Calculate errorbars on our binned LF. These have been estimated # using Equations 1 and 2 of Gehrels 1986 (ApJ 303 336), as # implemented in astropy.stats.poisson_conf_interval. The # interval='frequentist-confidence' option to that astropy function is # exactly equal to the Gehrels formulas, although the documentation # does not say so. n = np.histogram(m, bins=bins)[0] nlims = pci(n, interval='frequentist-confidence') nlims *= phi / n uperr = np.log10(nlims[1]) - logphi downerr = logphi - np.log10(nlims[0]) return mags, left, right, logphi, uperr, downerr
def get_lf_sample(lf, sid, z_plot): # Bin data. This is only for visualisation and to compare # with reported binned values. m = lf.M1450[lf.sid == sid] n = m.size mlims = (m.min(), m.max()) theta = np.median(lf.samples, axis=0) m = lfsample(theta, n, mlims) selmaps = [x for x in lf.maps if x.sid == sid] if sid == 6: # Glikman's sample needs wider bins. bins = np.array([-26.0, -25.0, -24.0, -23.0, -22.0, -21]) elif sid == 7: bins = np.array([-23.5, -21.5, -20.5, -19.5, -18.5]) elif sid == 10: bins = np.arange(-30.9, -17.3, 1.8) else: bins = np.arange(-30.9, -17.3, 0.6) v1 = np.array([totBinVol(lf, x, bins, selmaps) for x in m]) v1_nonzero = v1[np.where(v1 > 0.0)] m = m[np.where(v1 > 0.0)] h = np.histogram(m, bins=bins, weights=1.0 / (v1_nonzero)) nums = h[0] mags = (h[1][:-1] + h[1][1:]) * 0.5 dmags = np.diff(h[1]) * 0.5 left = mags - h[1][:-1] right = h[1][1:] - mags phi = nums logphi = np.log10(phi) # cMpc^-3 mag^-1 # Calculate errorbars on our binned LF. These have been estimated # using Equations 1 and 2 of Gehrels 1986 (ApJ 303 336), as # implemented in astropy.stats.poisson_conf_interval. The # interval='frequentist-confidence' option to that astropy function is # exactly equal to the Gehrels formulas, although the documentation # does not say so. n = np.histogram(m, bins=bins)[0] nlims = pci(n, interval='frequentist-confidence') nlims *= phi / n uperr = np.log10(nlims[1]) - logphi downerr = logphi - np.log10(nlims[0]) return mags, left, right, logphi, uperr, downerr
def get_lf(zrange, bins): z, m, p = np.loadtxt('Data/r13miz2_sample.dat', usecols=(1, 2, 3), unpack=True) select = ((z >= zrange[0]) & (z < zrange[1])) m = m[select] p = p[select] area = 2236.0 # deg^2 dz = 0.05 dm = 0.1 zsel, msel, psel = np.loadtxt('Data/r13miz2_selfunc.dat', usecols=(1, 2, 3), unpack=True) vol = volume(zsel, area) * dz psel[(zsel < zrange[0]) | (zsel >= zrange[1])] = 0.0 zv = np.linspace(zrange[0], zrange[1], 50) dzv = np.diff(zv)[0] v = volume(zv, area) * dzv totvol = np.sum(v) v1 = np.array( [binvol(x, zrange, bins, msel, psel, vol, zsel, totvol) for x in m]) v1_nonzero = v1[np.where(v1 > 0.0)] m = m[np.where(v1 > 0.0)] h = np.histogram(m, bins=bins, weights=1.0 / (v1_nonzero)) nums = h[0] mags = (h[1][:-1] + h[1][1:]) * 0.5 dmags = np.diff(h[1]) * 0.5 left = mags - h[1][:-1] right = h[1][1:] - mags phi = nums logphi = np.log10(phi) # cMpc^-3 mag^-1 n = np.histogram(m, bins=bins)[0] nlims = pci(n, interval='frequentist-confidence') nlims *= phi / n uperr = np.log10(nlims[1]) - logphi downerr = logphi - np.log10(nlims[0]) return mags, left, right, logphi, uperr, downerr
def get_lf(lf, z_plot, sids): sample_selection = np.in1d(lf.sid, sids) m = lf.M1450[sample_selection] p = lf.p[sample_selection] sid = lf.sid[sample_selection] m = m[p!=0.0] p = p[p!=0.0] sid = sid[p!=0.0] # DR7 area = 6248.0 # deg^2 dz = 0.05 dm = 0.1 zsel, msel, psel = np.loadtxt('Data/mcgreer13_dr7selfunc.dat', usecols=(1,2,3), unpack=True) vol = volume(zsel, area)*dz # def volm(m, msel, psel, vsel): # total_vol = 0.0 # bincount = 0 # bincount_pnonzero = 0 # dm = abs(msel[1]-msel[0]) # for i in xrange(msel.size): # if (m >= msel[i]-dm) and (m < msel[i]+dm): # bincount += 1 # if psel[i] > 0.0: # bincount_pnonzero += 1 # # total_vol += vsel[i]*psel[i] # total_vol += vsel[i] # Should this be vsel[i]*dm? # return total_vol def volm(m, msel, psel, vsel): total_vol = 0.0 bins = np.array([-28.23, -27.73, -27.23, -26.73, -26.23, -25.73]) idx = np.searchsorted(bins, m, side='right') mlow = bins[idx-1] mhigh = bins[idx] dm = abs(msel[1]-msel[0]) n = int(abs(mhigh-mlow)/dm)+1 for i in xrange(msel.size): if (mlow >= msel[i]-dm) and (mlow < msel[i]+dm): for j in range(n): try: total_vol += vsel[i+j]*psel[i+j]*dm except(IndexError): total_vol += 0.0 return total_vol v1 = np.array([volm(x, msel, psel, vol) for x in m]) bins = [-28.23, -27.73, -27.23, -26.73, -26.23, -25.73] # h = np.histogram(m,bins=bins,weights=1.0/(p*v1)) h = np.histogram(m,bins=bins,weights=1.0/v1) print m[v1==0.0] print p[v1==0.0] # print volm(-26.73, msel, psel, vol, pd=True) print '----' # # Stripe 82 # area = 235.0 # deg^2 # dz = 0.05 # dm = 0.1 # zsel, msel, psel = np.loadtxt('Data/mcgreer13_s82selfunc.dat', usecols=(1,2,3), unpack=True) # vol = volume(zsel, area)*dz # mask = np.ones(len(M1450), dtype=bool) # mask[dr7] = False # v2 = np.array([volm(x, msel, psel, vol) for x in m[mask]]) # v = np.where(mask, v2, v1) # bins = [-26.0, -25.0, -24.0, -23.0, -22.0, -21] # h = np.histogram(m[1:],bins=bins,weights=1.0/(p[1:]*v[1:])) nums = h[0] mags = (h[1][:-1] + h[1][1:])*0.5 dmags = np.diff(h[1])*0.5 left = mags - h[1][:-1] right = h[1][1:] - mags phi = nums/np.diff(h[1]) logphi = np.log10(phi) # cMpc^-3 mag^-1 # Calculate errorbars on our binned LF. These have been estimated # using Equations 1 and 2 of Gehrels 1986 (ApJ 303 336), as # implemented in astropy.stats.poisson_conf_interval. The # interval='frequentist-confidence' option to that astropy function is # exactly equal to the Gehrels formulas, although the documentation # does not say so. n = np.histogram(m,bins=bins)[0] print n nlims = pci(n,interval='frequentist-confidence') nlims *= phi/n uperr = np.log10(nlims[1]) - logphi downerr = logphi - np.log10(nlims[0]) print mags print logphi return mags, left, right, logphi, uperr, downerr
def get_lf(zrange, bins, old=True): if old: z, m, p = np.loadtxt('Data/glikman11qso.dat', usecols=(1, 2, 3), unpack=True) else: z, m, p = np.loadtxt('Data/glikman11debug.dat', usecols=(1, 2, 3), unpack=True) select = ((z >= zrange[0]) & (z < zrange[1])) m = m[select] p = p[select] area = 1.71 # deg^2 dz = 0.02 dm = 0.05 if old: zsel, msel, psel = np.loadtxt('Data/glikman11_selfunc_ndwfs_old.dat', usecols=(1, 2, 3), unpack=True) else: zsel, msel, psel = np.loadtxt('Data/glikman11_selfunc_ndwfs.dat', usecols=(1, 2, 3), unpack=True) vol = volume(zsel, area) * dz psel[(zsel < zrange[0]) | (zsel >= zrange[1])] = 0.0 # m[:12] because only those qsos are from NDWFS v1 = np.array( [binvol(x, zrange, bins, msel, psel, vol, zsel) for x in m[:12]]) area = 2.05 # deg^2 if old: zsel, msel, psel = np.loadtxt('Data/glikman11_selfunc_dls_old.dat', usecols=(1, 2, 3), unpack=True) else: zsel, msel, psel = np.loadtxt('Data/glikman11_selfunc_dls.dat', usecols=(1, 2, 3), unpack=True) vol = volume(zsel, area) * dz psel[(zsel < zrange[0]) | (zsel >= zrange[1])] = 0.0 # m[12:] because only those qsos are from DLS v2 = np.array( [binvol(x, zrange, bins, msel, psel, vol, zsel) for x in m[12:]]) v = np.concatenate((v1, v2)) print v.size v_nonzero = v[np.where(v > 0.0)] print v_nonzero.size m = m[np.where(v > 0.0)] h = np.histogram(m, bins=bins, weights=1.0 / (v_nonzero)) nums = h[0] mags = (h[1][:-1] + h[1][1:]) * 0.5 dmags = np.diff(h[1]) * 0.5 left = mags - h[1][:-1] right = h[1][1:] - mags phi = nums logphi = np.log10(phi) # cMpc^-3 mag^-1 n = np.histogram(m, bins=bins)[0] print n nlims = pci(n, interval='frequentist-confidence') nlims *= phi / n uperr = np.log10(nlims[1]) - logphi downerr = logphi - np.log10(nlims[0]) mags, b, c = bs(m, m, bins=bins) left = mags - b[:-1] right = b[1:] - mags return mags, left, right, logphi, uperr, downerr
def rhoqso3(lfs, mag_threshold, bins, **kwargs): zs = [] rhos = [] uerr = [] lerr = [] for x in lfs: z = (x.zlims[0]+x.zlims[1])/2 sids = np.unique(x.sid) logphis = [] for sid in sids: mags, left, right, logphi, uperr, downerr = get_lf(x, sid, bins, **kwargs) logphis.append(10.0**logphi) Nbins = np.size(mags) Nsamples = len(logphis) print('Nbins=', Nbins) print('Nsamples=', Nsamples) total_phi = np.zeros(Nbins) for i in range(Nbins): vals = [] nonzero_counter = 0 for j in range(Nsamples): if logphis[j][i] > 0.0: nonzero_counter += 1 vals.append(logphis[j][i]) if nonzero_counter > 0: total_phi[i] = np.mean(np.array(vals)) # print i, total_phi[i] # N = len(logphis) # if N > 2: # print 'We have a problem: N > 2!!!' # if N > 1: # total_phi = logphis[0] # for ns in range(1,N): # for i, p in enumerate(logphis[ns]): # if total_phi[i] == 0.0: # total_phi[i] = total_phi[i] + p # elif p > 0.0: # total_phi[i] = (total_phi[i] + p)/2 # logphi = total_phi # else: # logphi = np.sum(logphis, axis=0) logphi = total_phi #row1 = logphis[0] #row2 = logphis[1] # logphi = np.sum(logphis, axis=0) # #logphi = total_phi # logphi_test = np.mean(logphis, axis=0) #for phivalue in zip(row1, row2, logphi): # print '{:.2e} {:.2e} {:.2e}'.format(*phivalue) #print logphi dm = left + right phidm = logphi * dm intphidm = np.cumsum(phidm) # for omega in zip(mags, logphi): # print '{:.2f} {:.2e}'.format(omega[0], omega[1]) # print 'mags=', mags # print 'logphi=', logphi # assert(np.all(np.diff(mags) > 0)) # rho = np.interp(mag_threshold, mags, intphidm) rho = intphidm[-1] nqso = np.size(x.M1450[x.M1450 < mag_threshold]) if nqso > 0: nlims = pci(nqso,interval='frequentist-confidence') nlims *= rho/nqso uperr = nlims[1] - rho downerr = rho - nlims[0] else: uperr = 0.0 downerr = 0.0 if np.max(mags[logphi > 0.0]) < mag_threshold: # not reaching threshold rho = 0.0 uperr = 0.0 downerr = 0.0 rhos.append(rho) zs.append(z) uerr.append(uperr) lerr.append(downerr) return zs, rhos, uerr, lerr
def get_lf(lf, z_plot, sids): sample_selection = np.in1d(lf.sid, sids) m = lf.M1450[sample_selection] p = lf.p[sample_selection] sid = lf.sid[sample_selection] print m print np.sort(m) m = m[p!=0.0] p = p[p!=0.0] sid = sid[p!=0.0] # Jiang 2008 area = 260.0 # deg^2 dz = 0.025 dm = 0.1 zsel, msel, psel = np.loadtxt('Data/jiang08_sel.dat', usecols=(1,2,3), unpack=True) vol = volume(zsel, area)*dz # def volm(mag, magsel, probsel, volsel, zsel): # # Below, np.isclose() selects the tile to which this magnitude # # belongs at each redshift. We then select the tiles for # # which selection probability is non-zero (probsel[t]>0.0) and # # sum the volume of all those tiles. # t = np.isclose(magsel, mag, rtol=0.0, atol=dm*0.5) # rs = zsel[t][probsel[t]>0.0] # vl = volsel[t][probsel[t]>0.0] # return np.trapz(vl, rs) # # return np.sum(volsel[t][probsel[t]>0.0], dtype=np.float64) # def volm(mag, magsel, probsel, volsel, zsel): # # Below, np.isclose() selects the tile to which this magnitude # # belongs at each redshift. We then select the tiles for # # which selection probability is non-zero (probsel[t]>0.0) and # # sum the volume of all those tiles. # t = np.isclose(magsel, mag, rtol=0.0, atol=dm*0.5) # return np.sum(volsel[t][probsel[t]>0.0], dtype=np.float64) # def volm(mag, magsel, probsel, volsel, zsel): # # Below, np.isclose() selects the tile to which this magnitude # # belongs at each redshift. We then select the tiles for # # which selection probability is non-zero (probsel[t]>0.0) and # # sum the volume of all those tiles. # t = np.isclose(magsel, mag, rtol=0.0, atol=dm*0.5) # return np.sum(volsel[t]*probsel[t], dtype=np.float64) bins = [-26.25, -25.35, -24.85] def volm(mag, magsel, probsel, volsel, zsel): # Below, np.isclose() selects the tile to which this magnitude # belongs at each redshift. We then select the tiles for # which selection probability is non-zero (probsel[t]>0.0) and # sum the volume of all those tiles. bn = np.searchsorted(bins, mag) t = ((magsel < bins[1]) & (mag >= bins[0])) return np.sum(volsel[t]*probsel[t], dtype=np.float64) m1 = np.concatenate((m[:2],m[3:6])) v1 = np.array([volm(x, msel, psel, vol, zsel) for x in m1]) print 'v1=', v1 # Jiang 2009 area = 195.0 # deg^2 dz = 0.025 dm = 0.1 zsel, msel, psel = np.loadtxt('Data/jiang09_sel.dat', usecols=(1,2,3), unpack=True) vol = volume(zsel, area)*dz v2 = np.array([volm(x, msel, psel, vol, zsel) for x in m[6:]]) v = np.concatenate((v1, v2)) bins = [-26.25, -25.35, -24.85] # h = np.histogram(m,bins=bins,weights=1.0/(p*v)) # h = np.histogram(np.concatenate((m[:2],m[3:])),bins=bins,weights=1.0/(np.concatenate((p[:2],p[3:]))*v)) h = np.histogram(np.concatenate((m[:2],m[3:])),bins=bins,weights=1.0/v) nums = h[0] mags = (h[1][:-1] + h[1][1:])*0.5 dmags = np.diff(h[1])*0.5 left = mags - h[1][:-1] right = h[1][1:] - mags phi = nums/np.diff(h[1]) logphi = np.log10(phi) # cMpc^-3 mag^-1 # Calculate errorbars on our binned LF. These have been estimated # using Equations 1 and 2 of Gehrels 1986 (ApJ 303 336), as # implemented in astropy.stats.poisson_conf_interval. The # interval='frequentist-confidence' option to that astropy function is # exactly equal to the Gehrels formulas, although the documentation # does not say so. n = np.histogram(m,bins=bins)[0] nlims = pci(n,interval='frequentist-confidence') nlims *= phi/n uperr = np.log10(nlims[1]) - logphi downerr = logphi - np.log10(nlims[0]) # mags, b, c = bs(m, m, bins=bins) # left = mags - b[:-1] # right = b[1:] - mags print mags print phi return mags, left, right, logphi, uperr, downerr