def mu_binning(powerspectrum, los_axis=0, mubins=20, kbins=10, box_dims=None, weights=None, exclude_zero_modes=True, binning='log'): ''' This function is for internal use only. ''' if weights != None: powerspectrum *= weights assert (len(powerspectrum.shape) == 3) k_comp, k = _get_k(powerspectrum, box_dims) mu = _get_mu(k_comp, k, los_axis) #Calculate k values, and make k bins kbins = _get_kbins(kbins, box_dims, k, binning=binning) dk = (kbins[1:] - kbins[:-1]) / 2. n_kbins = len(kbins) - 1 #Exclude k_perp = 0 modes if exclude_zero_modes: good_idx = _get_nonzero_idx(powerspectrum.shape, los_axis) else: good_idx = np.ones_like(powerspectrum) #Make mu bins if isinstance(mubins, int): mubins = np.linspace(-1., 1., mubins + 1) dmu = (mubins[1:] - mubins[:-1]) / 2. n_mubins = len(mubins) - 1 #Remove the zero component from the power spectrum. mu is undefined here powerspectrum[tuple(np.array(powerspectrum.shape) / 2)] = 0. #Bin the data print_msg('Binning data...') outdata = np.zeros((n_mubins, n_kbins)) for ki in range(n_kbins): print_msg('Bin %d of %d' % (ki, n_kbins)) kmin = kbins[ki] kmax = kbins[ki + 1] kidx = get_eval()('(k >= kmin) & (k < kmax)') kidx *= good_idx for i in range(n_mubins): mu_min = mubins[i] mu_max = mubins[i + 1] idx = get_eval()('(mu >= mu_min) & (mu < mu_max) & kidx') outdata[i, ki] = np.mean(powerspectrum[idx]) if weights != None: outdata[i, ki] /= weights[idx].mean() return outdata, mubins[:-1] + dmu, kbins[:-1] + dk
def mu_binning(powerspectrum, los_axis = 0, mubins=20, kbins=10, box_dims=None, weights=None, exclude_zero_modes=True, binning='log'): ''' This function is for internal use only. ''' if weights != None: powerspectrum *= weights assert(len(powerspectrum.shape)==3) k_comp, k = _get_k(powerspectrum, box_dims) mu = _get_mu(k_comp, k, los_axis) #Calculate k values, and make k bins kbins = _get_kbins(kbins, box_dims, k, binning=binning) dk = (kbins[1:]-kbins[:-1])/2. n_kbins = len(kbins)-1 #Exclude k_perp = 0 modes if exclude_zero_modes: good_idx = _get_nonzero_idx(powerspectrum.shape, los_axis) else: good_idx = np.ones_like(powerspectrum) #Make mu bins if isinstance(mubins,int): mubins = np.linspace(-1., 1., mubins+1) dmu = (mubins[1:]-mubins[:-1])/2. n_mubins = len(mubins)-1 #Remove the zero component from the power spectrum. mu is undefined here powerspectrum[tuple(np.array(powerspectrum.shape)/2)] = 0. #Bin the data print_msg('Binning data...') outdata = np.zeros((n_mubins,n_kbins)) for ki in range(n_kbins): print_msg('Bin %d of %d' % (ki, n_kbins)) kmin = kbins[ki] kmax = kbins[ki+1] kidx = get_eval()('(k >= kmin) & (k < kmax)') kidx *= good_idx for i in range(n_mubins): mu_min = mubins[i] mu_max = mubins[i+1] idx = get_eval()('(mu >= mu_min) & (mu < mu_max) & kidx') outdata[i,ki] = np.mean(powerspectrum[idx]) if weights != None: outdata[i,ki] /= weights[idx].mean() return outdata, mubins[:-1]+dmu, kbins[:-1]+dk
def _get_k(input_array, box_dims): ''' Get the k values for input array with given dimensions. Return k components and magnitudes. For internal use. ''' dim = len(input_array.shape) if dim == 1: x = np.arange(len(input_array)) center = x.max()/2. kx = 2.*np.pi*(x-center)/box_dims[0] return [kx], kx elif dim == 2: x,y = np.indices(input_array.shape, dtype='int32') center = np.array([(x.max()-x.min())/2, (y.max()-y.min())/2]) kx = 2.*np.pi * (x-center[0])/box_dims[0] ky = 2.*np.pi * (y-center[1])/box_dims[1] k = np.sqrt(kx**2 + ky**2) return [kx, ky], k elif dim == 3: x,y,z = np.indices(input_array.shape, dtype='int32') center = np.array([(x.max()-x.min())/2, (y.max()-y.min())/2, \ (z.max()-z.min())/2]) kx = 2.*np.pi * (x-center[0])/box_dims[0] ky = 2.*np.pi * (y-center[1])/box_dims[1] kz = 2.*np.pi * (z-center[2])/box_dims[2] k = get_eval()('(kx**2 + ky**2 + kz**2 )**(1./2.)') return [kx,ky,kz], k
def _get_k(input_array, box_dims): ''' Get the k values for input array with given dimensions. Return k components and magnitudes. For internal use. ''' dim = len(input_array.shape) if dim == 1: x = np.arange(len(input_array)) center = x.max() / 2. kx = 2. * np.pi * (x - center) / box_dims[0] return [kx], kx elif dim == 2: x, y = np.indices(input_array.shape, dtype='int32') center = np.array([(x.max() - x.min()) / 2, (y.max() - y.min()) / 2]) kx = 2. * np.pi * (x - center[0]) / box_dims[0] ky = 2. * np.pi * (y - center[1]) / box_dims[1] k = np.sqrt(kx**2 + ky**2) return [kx, ky], k elif dim == 3: x, y, z = np.indices(input_array.shape, dtype='int32') center = np.array([(x.max()-x.min())/2, (y.max()-y.min())/2, \ (z.max()-z.min())/2]) kx = 2. * np.pi * (x - center[0]) / box_dims[0] ky = 2. * np.pi * (y - center[1]) / box_dims[1] kz = 2. * np.pi * (z - center[2]) / box_dims[2] k = get_eval()('(kx**2 + ky**2 + kz**2 )**(1./2.)') return [kx, ky, kz], k