def test_shell_pspec_dft(): """ Take a gaussian shell and confirm its power spectrum using shell_project_pspec. """ select_radius = 10. #degrees N_sections = 10 Nside = 256 Npix = 12 * Nside**2 Omega = 4 * np.pi / float(Npix) Nfreq = 100 freqs = np.linspace(167.0, 177.0, Nfreq) dnu = np.diff(freqs)[0] Z = 1420 / freqs - 1. sig = 2.0 mu = 0.0 shell = np.random.normal(mu, sig, (Npix, Nfreq)) dV0 = comoving_voxel_volume(Z[Nfreq / 2], dnu, Omega) for fi in range(Nfreq): dV = comoving_voxel_volume(Z[fi], dnu, Omega) shell[:, fi] = np.random.normal(mu, sig * np.sqrt(dV0 / dV), Npix) kbins, pk, errs = pspec_funcs.shell_project_pspec(shell, Nside, select_radius, freqs=freqs, Nkbins=100, N_sections=N_sections, cosmo=True, method='dft', error=True) tol = np.sqrt(np.var(pk)) print 'Tolerance: ', tol print 'Error: ', np.mean(pk / (sig**2 * dV0)) nt.assert_true(np.isclose(np.mean(pk), sig**2 * dV0, atol=tol))
def compare_averages_shell_pspec_dft(): """ Take a gaussian shell and confirm its power spectrum using shell_project_pspec. """ import pylab as pl select_radius = 5. #degrees Nside = 256 Npix = 12 * Nside**2 Omega = 4 * np.pi / float(Npix) Nfreq = 100 freqs = np.linspace(167.0, 177.0, Nfreq) dnu = np.diff(freqs)[0] Z = 1420 / freqs - 1. sig = 2.0 mu = 0.0 shell = np.random.normal(mu, sig, (Npix, Nfreq)) dV = comoving_voxel_volume(Z[Nfreq / 2], dnu, Omega) variances = [] pks = [] fig, (ax0, ax1, ax2) = pl.subplots(nrows=1, ncols=3) steps = range(10, 110, 10) for n in steps: Nkbins = 100 kbins, pk = pspec_funcs.shell_project_pspec(shell, Nside, select_radius, freqs=freqs, Nkbins=Nkbins, N_sections=n, cosmo=True, method='dft', error=False) variances.append(np.var(pk[0:Nkbins - 5])) pks.append(pk) ax0.plot(kbins, pk, label=str(n)) ax0.axhline(y=dV * sig**2) ax0.legend() ax1.plot(steps, variances) ax2.imshow(np.log10(np.array(pks)[:, 0:Nkbins - 5]), aspect='auto') pl.show()
def compare_selection_radii_shell_pspec_dft(): import pylab as pl N_sections = 20 Nside = 256 Npix = 12 * Nside**2 Omega = 4 * np.pi / float(Npix) Nfreq = 100 freqs = np.linspace(167.0, 177.0, Nfreq) dnu = np.diff(freqs)[0] Z = 1420 / freqs - 1. sig = 2.0 mu = 0.0 shell = np.random.normal(mu, sig, (Npix, Nfreq)) dV = comoving_voxel_volume(Z[Nfreq / 2], dnu, Omega) variances = [] pks = [] means = [] fig, (ax0, ax1, ax2) = pl.subplots(nrows=1, ncols=3) steps = np.linspace(2, 20, 20) for s in steps: Nkbins = 100 kbins, pk = pspec_funcs.shell_project_pspec(shell, Nside, s, freqs=freqs, Nkbins=Nkbins, N_sections=N_sections, cosmo=True, method='dft', error=False) variances.append(np.var(pk[0:Nkbins - 5])) pks.append(pk) means.append(np.mean(pk[0:Nkbins - 5])) ax0.plot(kbins, pk, label=u'{:0.2f}°'.format(s)) ax0.axhline(y=dV * sig**2) ax0.legend() ax1.plot(steps, means) im = ax2.imshow(np.log10(np.array(pks)[:, 0:Nkbins - 5]), aspect='auto') fig.colorbar(im, ax=ax2) pl.show()
def test_orthoslant_project(): Nside = 256 Npix = 12 * Nside**2 Omega = 4 * np.pi / float(Npix) Nfreq = 100 freqs = np.linspace(167.0, 177.0, Nfreq) dnu = np.diff(freqs)[0] Z = 1420 / freqs - 1. sig = 2.0 mu = 0.0 shell = np.random.normal(mu, sig, (Npix, Nfreq)) center = [np.sqrt(2) / 2., np.sqrt(2) / 2., 0] ## For orientation tests, replace with a single point at center # import healpy # cind = healpy.vec2pix(Nside, *center) # shell *= 0.0 # shell[cind] += 10 orthogrid = pspec_funcs.orthoslant_project(shell, center, 10, degrees=True) Nx, Ny, Nz = orthogrid.shape Lx = comoving_transverse_length(Z[Nfreq / 2], Omega) * Nx Ly = Lx Lz = comoving_radial_length(Z[Nfreq / 2], dnu) * Nz dV = comoving_voxel_volume(Z[Nfreq / 2], dnu, Omega) r_mpc = WMAP9.comoving_distance(Z).to("Mpc").value kbins, pk = pspec_funcs.box_dft_pspec(orthogrid, [Lx, Ly, Lz], r_mpc=r_mpc, cosmo=True) tol = np.sqrt(np.var(pk)) nt.assert_true(np.allclose(np.mean(pk), sig**2 * dV, atol=tol))
# Frequency freqs = np.linspace(1e8, 1.3e8, Nfreqs) #30 MHz Nfreqs = freqs.size # Shells Nside = args.Nside Npix = 12*Nside**2 sig = args.sigma Nskies = args.Nskies #shell0 = np.zeros((Nskies,Npix,Nfreqs), dtype=float) shell0 = utils.mparray((Nskies, Npix, Nfreqs), dtype=float) #shell0 = np.random.normal(0.0, sig, (Nskies, Npix, Nfreqs)) dnu = np.diff(freqs)[0]/1e6 om = 4*np.pi/float(Npix) Zs = 1420e6/freqs - 1 dV0 = comoving_voxel_volume(Zs[Nfreqs/2], dnu, om) print("Making skies") for fi in range(Nfreqs): dV = comoving_voxel_volume(Zs[fi], dnu, om) s = sig * np.sqrt(dV0/dV) shell0[:,:,fi] = np.random.normal(0.0, s, (Nskies, Npix)) #Make observatories visibs = [] #fwhms = [2.5, 5.0, 10.0, 20.0, 25.0, 30.0] #fwhms = [35, 40, 45.0, 50.0, 55.0, 60.0] fwhm = args.fwhm sigma = fwhm/2.355 obs = visibility.observatory(latitude, longitude, array=[bl], freqs=freqs) obs.set_fov(fov) obs.set_pointings(time_arr)
def compare_selection_radii_shell_pspec_dft(): N_sections=10 Nside=256 Npix = 12 * Nside**2 Omega = 4*np.pi/float(Npix) Nfreq = 100 freqs = np.linspace(167.0, 177.0, Nfreq) dnu = np.diff(freqs)[0] Z = 1420/freqs - 1. sig = 2.0 mu = 0.0 shell = np.random.normal(mu, sig, (Npix, Nfreq)) dV = comoving_voxel_volume(Z[Nfreq/2], dnu, Omega) variances = [] pks = [] means = [] gs = gridspec.GridSpec(2, 3) fig = pl.figure() ax0 = pl.subplot(gs[0, 0:2]) ax1 = pl.subplot(gs[1, 0]) ax3 = pl.subplot(gs[1, 1]) ax2 = pl.subplot(gs[:, 2]) steps = np.linspace(2,20,20) vmin,vmax = min(steps),max(steps) normalize = mcolors.Normalize(vmin=vmin, vmax=vmax) colormap = cm.viridis for s in steps: Nkbins = 100 kbins, pk = pspec_funcs.shell_project_pspec(shell, Nside, s, freqs=freqs, Nkbins=Nkbins, N_sections=N_sections, cosmo=True, method='dft', error=False) variances.append(np.var(pk[0:Nkbins-5])) pks.append(pk) means.append(np.mean(pk[0:Nkbins-5])) ax0.plot(kbins, pk, label=u'{:0.2f}°'.format(s), color=colormap(normalize(s))) ax0.axhline(y=dV*sig**2) # ax0.legend(ncol=2,loc=3) scalarmappable = cm.ScalarMappable(norm=normalize, cmap=colormap) scalarmappable.set_array(steps) fig.colorbar(scalarmappable,label=r'Selection radius', ax=ax0) ax0.set_ylabel(r"P(k) [mK$^2$ Mpc$^{3}]$") ax0.set_xlabel(r"k [Mpc$^{-1}]$") ax1.plot(steps, np.array(variances), label="Variance") ax1.set_ylabel(r"Variance(P(k)) [mK$^4$ Mpc$^{6}]$") ax1.set_xlabel(u"Selection radius (degrees)") ax3.plot(steps, means, label="Mean") ax3.set_ylabel(r"Mean(P(k)) [mK$^2$ Mpc$^{3}]$") ax3.set_xlabel(u"Selection radius (degrees)") ax1.legend() ax3.legend() ax1.xaxis.set_major_formatter(FormatStrFormatter(u'%0.2f°')) ax3.xaxis.set_major_formatter(FormatStrFormatter(u'%0.2f°')) im = ax2.imshow(np.array(pks)[:,0:Nkbins-5], aspect='auto', norm=mcolors.LogNorm()) fig.colorbar(im, ax=ax2) pl.show()
def compare_averages_shell_pspec_dft(): """ Take a gaussian shell and confirm its power spectrum using shell_project_pspec. """ select_radius = 5. #degrees Nside=256 Npix = 12 * Nside**2 Omega = 4*np.pi/float(Npix) Nfreq = 100 freqs = np.linspace(167.0, 177.0, Nfreq) dnu = np.diff(freqs)[0] Z = 1420/freqs - 1. sig = 2.0 mu = 0.0 shell = np.random.normal(mu, sig, (Npix, Nfreq)) dV = comoving_voxel_volume(Z[Nfreq/2], dnu, Omega) variances = [] means = [] pks = [] gs = gridspec.GridSpec(2, 3) fig = pl.figure() ax0 = pl.subplot(gs[0, 0:2]) ax1 = pl.subplot(gs[1, 0]) ax3 = pl.subplot(gs[1, 1]) ax2 = pl.subplot(gs[:, 2]) steps = range(10,110,10) vmin,vmax = min(steps),max(steps) normalize = mcolors.Normalize(vmin=vmin, vmax=vmax) colormap = cm.viridis for n in steps: Nkbins = 100 kbins, pk = pspec_funcs.shell_project_pspec(shell, Nside, select_radius, freqs=freqs, Nkbins=Nkbins, N_sections=n, cosmo=True, method='dft', error=False) variances.append(np.var(pk[0:Nkbins-5])) means.append(np.mean(pk[0:Nkbins-5])) pks.append(pk) ax0.plot(kbins, pk, label=str(n), color=colormap(normalize(n))) ax0.axhline(y=dV*sig**2, color='k', lw=2.0) # ax0.legend() scalarmappable = cm.ScalarMappable(norm=normalize, cmap=colormap) scalarmappable.set_array(steps) fig.colorbar(scalarmappable,label=r'Number of snapshots', ax=ax0) ax0.set_ylabel(r"P(k) [mK$^2$ Mpc$^{3}]$") ax0.set_xlabel(r"k [Mpc$^{-1}]$") ax1.plot(steps, np.array(variances), label="Variance") ax1.set_ylabel(r"Variance(P(k)) [mK$^4$ Mpc$^{6}]$") ax1.set_xlabel(u"Number of 5° snapshots") ax3.plot(steps, means, label="Mean") ax3.set_ylabel(r"Mean(P(k)) [mK$^2$ Mpc$^{3}]$") ax3.set_xlabel(u"Number of 5° snapshots") ax1.legend() ax3.legend() im = ax2.imshow(np.array(pks)[:,0:Nkbins-5], aspect='auto')#, norm=mcolors.LogNorm()) fig.colorbar(im, ax=ax2) print('Fractional deviation: ', np.mean(np.abs(pk - dV*sig**2))) pl.show()
# bins = np.average(bins, axis=0) # cs = np.average(cs, axis=0) save_dict['covar_data'] = cs time_arr = np.average(bins, axis=0) version = data_cfg['root'].split('/')[-1] save_dict['times'] = time_arr save_results(save_dict, data_cfg, out_cfg) # Get a reference line, if available if 'Nside' in catalog_cfg: if 'sigma' in catalog_cfg: nside = catalog_cfg['Nside'] sigma = catalog_cfg['sigma'] om = 4 * np.pi / (12. * nside**2) dnu = np.diff(freq_obs)[0] / 1e6 #MHz ref_line = comoving_voxel_volume(Zmean, dnu, om) * (sigma)**2 if 'furlanetto_flat' in catalog_cfg: f = readsav('/users/alanman/FHD/catalog_data/eor_power_1d.idlsave') maxpow = max(f['power']) ref_line = maxpow / 0.7**3 #Furlanetto's curve includes little-h if 'npz' in catalog_cfg: # Setting reference level to the most averaged spectrum in an npz file. f = np.load(catalog_cfg['npz']) try: ref_avg_mode = f['avg_mode'] except KeyError: ref_avg_mode = 'over_k' ref_avgs = f['avgs'] if ref_avg_mode == 'over_k': print ref_avgs.shape