def compute_fg(self): self.find_lines_in_bin() #this function is actualy phi(l)dL i.e. the number of sources in the luminosity bin centred on L def number_density(log_ll_star, log_phi, alpha): phi_star = 10**(log_phi) phi = np.log(10) * phi_star * np.exp(-(10**(log_ll_star))) * ( (10**log_ll_star)**(alpha + 1)) * self.delta_log_llstar return phi def comov_vol(z, delta_z, omega_pix): return ( omega_pix * ((cosmo.angular_diameter_distance(z).value)**2) * (sc.c / cosmo.H(0).value) * (1 / np.sqrt((0.31 * ((1 + z)**3)) + (0.69))) * ((1 + z)**2) * delta_z) ###how to deal with delta z... nlines = self.data.shape[0] self.L_tot = np.zeros((nlines, self.npix)) self.ave_Ns = np.zeros((nlines, len(self.log_ll_star))) for h in range( nlines ): #here you find the average Ns for each lum bin for each line for i in range(len(self.log_ll_star)): central_l = self.log_ll_star[i] + (self.delta_log_llstar / 2 ) # check math here self.ave_Ns[h, i] = number_density( central_l, float(self.data[h, 5]), float(self.data[h, 3]) ) * ( cosmo.differential_comoving_volume(float( self.data[h, 1])).value * self.omega_pix ) #comov_vol(float(self.data[h,1]),float(self.data[h,2]),self.omega_pix) # this should not be negative.... uh oh # here we are populating every pixel with every line in every luminosity bin. for h in range(nlines): for j in range( self.npix ): # this is when you populate all the pixels with sources. for i in range(len(self.log_ll_star)): #pick a luminosity pls numsources = np.random.poisson(self.ave_Ns[ h, i]) #find the number of sources in that (L,z) # central_l = (self.ll_star[i])+(self.delta_l/2) # central llstar of that bin # L_s = 10**(float(self.data[h,4])) self.L_tot[h, j] += (10**( float(self.log_ll_star[i]) + float(self.data[h, 4]) )) * float( numsources ) #luminosity of one (L,z) bin here taking L to be (L/L*)(L*) return self.L_tot
def box_pyramid_pspec(cube, angular_extent, r_mpc, Zs, kz = None, Nkbins=100, error=False, cosmo=False, return_dV=False): """ Estimate the power spectrum in a "pyramid" appoximation: angular_extent = in radians, the width of the box For each r_mpc, calculate the width of the projected region from the angular diameter distance. In the radial direction, calculate discrete fourier transform. In tangential directions, do FFT. """ Zs.sort() r_mpc.sort() Nx, Ny, Nz = cube.shape D_mpc = WMAP9.comoving_transverse_distance(Zs).to("Mpc").value L = D_mpc * angular_extent # Transverse lengths in Mpc if cosmo: kfact=2*np.pi # dV = (Nz) = (L/Nx) * (L/Ny) * np.diff( pixel_size = angular_extent**2/(Nx * Ny) dz_redshift = np.diff(Zs)[0] # Assume uniform redshift interval dV = WMAP9.differential_comoving_volume(Zs).to("Mpc3/sr").value * dz_redshift * pixel_size Lz = r_mpc[-1] - r_mpc[0] pfact = 1/(L**2 * Lz) else: kfact=1 dV = 1 pfact = 1/float(Nx*Ny*Nz) if kz is None: dz = np.abs(r_mpc[-1] - r_mpc[0])/float(Nz) #Mean spacing kz = np.fft.fftfreq(Nz,d=dz) * kfact else: assert kz.size == r_mpc.size ## DFT in radial direction M = np.exp(np.pi * 2 * (-1j) * np.outer(kz,r_mpc) ) _c = np.apply_along_axis(lambda x: np.dot(M,x),2, cube) ## 2D FFT in the transverse directions _c = np.fft.fft2(_c,axes=(0,1)) _c = _c * dV ## kx and ky kx = np.array([np.fft.fftfreq(Nx,d=l/Nx) for l in L])*kfact ky = np.array([np.fft.fftfreq(Ny,d=l/Ny) for l in L])*kfact kx = np.moveaxis(np.tile(kx[:,:,np.newaxis],Ny),[1,2],[0,1]) ky = np.moveaxis(np.tile(ky[:,:,np.newaxis],Nx),[1,2],[1,0]) kz = np.tile(kz[np.newaxis,:],Nx*Ny).reshape(Nx,Ny,Nz) pk3d = np.abs(_c)**2 * pfact results = bin_1d(pk3d,(kx,ky,kz),Nkbins=Nkbins,error=error) if return_dV: return results, dV return results
def cosmodVol(redshift, WMAP9=False, H0=70.0, Om0=0.30, Planck15=False): """ Get the Differential Comoving Volume at redshift=z. This is simply a wrapper of astropy.cosmology The input redsfhit can be an array """ if WMAP9: from astropy.cosmology import WMAP9 as cosmo elif Planck15: from astropy.cosmology import Planck15 as cosmo else: from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=H0, Om0=Om0) dv = cosmo.differential_comoving_volume(redshift) return dv.value
def cosmodVol(redshift, WMAP9=False, H0=70.0, Om0=0.30, Planck15=False): """ Get the Differential Comoving Volume at redshift=z. This is simply a wrapper of astropy.cosmology The input redsfhit can be an array """ if WMAP9: from astropy.cosmology import WMAP9 as cosmo elif Planck15: from astropy.cosmology import Planck15 as cosmo else: from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=H0, Om0=Om0) dv = cosmo.differential_comoving_volume(redshift) return dv.value
def comoving_voxel_volume(z, dnu, omega): """ From z, dnu, and pixel size (omega), get voxel volume. dnu = MHz Omega = sr """ if isinstance(z, np.ndarray): if isinstance(omega, np.ndarray): z, omega = np.meshgrid(z, omega) elif isinstance(dnu, np.ndarray): z, dnu = np.meshgrid(z, dnu) elif isinstance(dnu, np.ndarray) and isinstance(omega, np.ndarray): dnu, omega = np.meshgrid(dnu, omega) nu0 = 1420. / (z + 1) - dnu / 2. nu1 = nu0 + dnu dz = 1420. * (1 / nu0 - 1 / nu1) vol = cosmo.differential_comoving_volume(z).value * dz * omega return vol
def astro_redshifts(min_z, max_z, nsamples): '''Sample the redshifts for sources, with redshift independent rate, using standard cosmology Parameters ---------- min_z: float Minimum redshift max_z: float Maximum redshift nsamples: int Number of samples Returns ------- z_astro: array nsamples of redshift, between min_z, max_z, by standard cosmology ''' dz, fac = 0.001, 3.0 # use interpolation instead of directly estimating all the pdfz for rndz V = quad(contracted_dVdc, 0., max_z)[0] zbins = np.arange(min_z, max_z + dz / 2., dz) zcenter = (zbins[:-1] + zbins[1:]) / 2 pdfz = cosmo.differential_comoving_volume(zcenter).value / (1 + zcenter) / V int_pdf = interp1d(zcenter, pdfz, bounds_error=False, fill_value=0) rndz = np.random.uniform(min_z, max_z, int(fac * nsamples)) pdf_zs = int_pdf(rndz) maxpdf = max(pdf_zs) rndn = np.random.uniform(0, 1, int(fac * nsamples)) * maxpdf diff = pdf_zs - rndn idx = np.where(diff > 0) z_astro = rndz[idx] np.random.shuffle(z_astro) z_astro.resize(nsamples) return z_astro
def __init__(self, **kwargs): """Initialize module.""" super(Arbitrary, self).__init__(**kwargs) self._filename = kwargs.get(self.key('filename'), None) """Input data from file""" #Put the axis label (X,Y) on the first row of file df = pd.read_csv(self._filename, header = 0) """Data treatment""" #Ignore this part if x and y are ready to fit. df.Y = 4 * np.pi * 10 ** df.Y * cosmo.differential_comoving_volume(df.X) """Generating distribution shape and normalizing factor""" self._min_value = self._min_value if self._min_value else x.min() self._max_value = self._max_value if self._max_value else x.max() self._f = interpolate.interp1d(df.X, df.Y, kind = 'linear', fill_value="extrapolate") norm_factor = quad(self._f, self._min_value, self._max_value)[0] """Generating icdf""" self._cdf = [] self._x = np.linspace(self._min_value, self._max_value, 100) for i in self._x: integral = quad(self._f, 0.1, i)[0] self._cdf.append(integral / norm_factor) self._icdf = interpolate.interp1d(self._cdf, self._x, kind = 'linear', fill_value = 'extrapolate')
def contracted_dVdc(z): #Return the time-dilated differential comoving volume return cosmo.differential_comoving_volume(z).value / (1 + z)
def dVdz(z): return cosmo.differential_comoving_volume(z).value