def K_matrix(l_max, fudge_factor, inverse=False): K = np.matrix(np.zeros(((l_max + 1)**2, (l_max + 1)**2))) for i in xrange((l_max + 1)**2): l1, m1 = Ylm.i2lm(i) for j in xrange(i, (l_max + 1)**2): l2, m2 = Ylm.i2lm(j) element = K_element(l1, m1, l2, m2, fudge_factor, inverse) K[i, j] = element if j > i: K[j, i] = element # symmetric matrix return K
def computeVisSamples(vislm, k, r, theta, phi): """The reverse function to computeVislm, compute the visibilities for give set of (r, theta, phi) from vislm coefficients, Eq. 9 pf Carozzi 2015 vislm: complex array, spherical wave harmonics visibility coefficients computed from computeVislm() k: [N, 1] float array, wave number, observing frequencies/c (1/meters) r, theta, phi: [Q, N] float arrays of visibility positions transformed from (u,v,w) positions, r (meters) returns: vis [Q, N] complex array of visibilities """ vis = np.zeros(r.shape, dtype='complex') nsbs = r.shape[1] kr = r * k.flatten( ) #compute the radii in wavelengths for each visibility sample lmax = vislm.shape[0] - 1 print 'L:', for l in np.arange(lmax + 1): #increase lmax by 1 to account for starting from 0 print l, #if l==0: # krIdx = np.argwhere(kr == 0.) # vis[krIdx] += vislm[0, 0] * 1. * (.5 * np.sqrt(1. / np.pi)) # continue #TODO: I think l==0 needs to be skipped because it is the auto-correlation which shouldn't go into the cross-correlations #jvVals = np.reshape(sphBj(l, kr.flatten(), autos=True), kr.shape) #compute Bessel function radius values jvVals = np.reshape(sphBj(l, kr.flatten(), autos=False), kr.shape) #compute Bessel function radius values sys.stdout.flush() for m in np.arange(-1 * l, l + 1): vis += vislm[l, l + m] * jvVals * Ylm.Ylm(l, m, phi, theta) #import pylab #pylab.plot(vis[:,0].real, '.') #pylab.show() #exit() return vis
def out(alms1, alms2, N, l_max_max): for l_max in xrange(1, l_max_max + 1): i = Ylm.lm2i(l_max, l_max) chisq = get_chisq(alms1[:i + 1], alms2[:i + 1], N) dof = ( l_max + 1 )**2 - 1 # -1 because there is no information in the ell = 0 mode print "l_max = {0:2d}, chisq = {1:6.2f}, dof = {2:3d}, chisq/dof = {3:4.2f}, p = {4:9.7f}".format( l_max, chisq, dof, chisq / dof, p_value(chisq, l_max))
def almKs(blms, Kinv): # takes the 1/omega inverse l_max_blms = np.sqrt(len(blms)) - 1 assert l_max_blms == int(l_max_blms) assert Kinv.shape[0] == Kinv.shape[1] # is square l_max_K = np.sqrt(Kinv.shape[0]) - 1 assert l_max_K == int(l_max_K) assert l_max_K >= l_max_blms Kinv = Kinv[:len(blms), :len(blms)] # prune down to size almKs = Ylm.get_alms_iso_exact(l_max_blms) # initialize to zero for i in xrange(len(almKs)): for j in xrange(len(almKs)): almKs[i] += Kinv[i, j] * blms[ j] # K is symmetric, so Kinv is symmetric return almKs / (almKs[0] * np.sqrt(4 * np.pi) ) # correct the normalization: a00 = 1/sqrt(4pi)
def computeVislm(lmax, k, r, theta, phi, vis, lmin=0): """Compute the spherical wave harmonics visibility coefficients, Eq. 16 of Carozzi 2015 lmax: positive int, maximum spherical harmonic l number lmin: positive int, minimum spherical harmonic l number, usually 0 k: [N, 1] float array, wave number, observing frequencies/c (1/meters) r, theta, phi: [Q, N] float arrays of visibility positions transformed from (u,v,w) positions, r (meters) vis: [Q, N] complex array, observed visibilities returns: [lmax+1, 2*lmax+1, nfreq] array of coefficients, only partially filled, see for loops in this function """ #vis *= 2. #Treat the conjugate baselines as doubling the non-conjugate visibilities #TODO: include conjugate baselines? nsbs = vis.shape[1] vislm = np.zeros((lmax + 1, 2 * lmax + 1, nsbs), dtype=complex) kr = r * k.flatten( ) #compute the radii in wavelengths for each visibility sample print 'L:', for l in np.arange(lmax + 1): #increase lmax by 1 to account for starting from 0 if l < lmin: continue print l, #jvVals = np.reshape(sphBj(l, kr.flatten(), autos=True), kr.shape) #compute Bessel function radius values jvVals = np.reshape(sphBj(l, kr.flatten(), autos=False), kr.shape) #compute Bessel function radius values sys.stdout.flush() for m in np.arange(-1 * l, l + 1): #Compute visibility spherical harmonic coefficients according to SWHT, i.e. multiply visibility by spherical wave harmonics for each L&M and sum over all baselines. #Note that each non-zero baseline is effectively summed twice in the precedi(In the MNRAS letter image the NZ baselines were only weighted once, i.e. their conjugate baselines were not summed.) #spharmlm = np.repeat(np.conj(Ylm.Ylm(l, m, phi[:, sbIdx:sbIdx+1], theta[:, sbIdx:sbIdx+1])), nsbs, axis=1) #spherical harmonics only needed to be computed once for all baselines, independent of observing frequency spharmlm = np.conj( Ylm.Ylm(l, m, phi, theta) ) #spherical harmonics only needed to be computed once for all baselines, independent of observing frequency, TODO: a slow call vislm[l, l + m] = ((2. * (k.flatten()**2.)) / np.pi) * np.sum( vis * jvVals * spharmlm, axis=0) #sum visibilites of same obs frequency #Average coefficients in freqeuncy, TODO: there is probably something else to do here vislm = np.mean(vislm, axis=2) print 'done' return vislm
def makeHEALPix(coeffs, nside=64): """Make a HEALPix map from SWHT image coefficients, comparable to healpy.alm2map() coeffs: SWHT brightness coefficients nside: int, HEALPix NSIDE """ hpIdx = np.arange(hp.nside2npix(nside)) #create an empty HEALPix map hpmap = np.zeros((hp.nside2npix(nside)), dtype=complex) #HEALPix ids theta, phi = hp.pix2ang(nside, hpIdx) lmax = coeffs.shape[0] - 1 print 'L:', for l in np.arange(lmax + 1): print l, sys.stdout.flush() for m in np.arange(-1 * l, l + 1): hpmap += coeffs[l, l + m] * Ylm.Ylm(l, m, phi, theta) #TODO: a slow call print 'done' return hpmap
def make3Dimage(coeffs, dim=[64, 64]): """Make a 3D sphere from SWHT image coefficients coeffs: SWHT brightness coefficients dim: [int, int] number of steps in theta and phi """ #equal-spaced sample of theta and phi, not ideal as equal pixel (i.e. HEALPIX) [theta, phi] = np.meshgrid(np.linspace(0, np.pi, num=dim[0], endpoint=True), np.linspace(0, 2. * np.pi, num=dim[1], endpoint=True)) img = np.zeros(theta.shape, dtype='complex') lmax = coeffs.shape[0] print 'L:', for l in np.arange(lmax): print l, sys.stdout.flush() for m in np.arange(-1 * l, l + 1): img += coeffs[l, l + m] * Ylm.Ylm(l, m, phi, theta) #TODO: a slow call print 'done' return img, phi, theta #flip theta and phi values
def Pranay1(): w,theta,phi = Ylm.gen_tri_quad(10) x,y,z = Ylm.sph_to_cart(theta,phi) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(x,y,z,s=20*w)
def Pranay3(): w,theta,phi = Ylm.gen_tri_quad(16) x,y,z = Ylm.sph_to_cart(theta,phi) err1, err2 = Ylm.test_quads(x,y,z,w,5) plt.plot(err1) plt.plot(err2)
import Ylm E_min_Auger = 57 E_scale = 1.13 E_min_TA = E_min_Auger * E_scale l_max = 25 Auger_TA_events, ff = Data.load_Auger_TA(E_min_Auger=E_min_Auger, E_scale=E_scale, purge_hotspot=False) Auger_TA_events_purged, ff_purged = Data.load_Auger_TA(E_min_Auger=E_min_Auger, E_scale=E_scale, purge_hotspot=True) alms = Ylm.get_alms_exposure(Auger_TA_events, l_max, ff) alms_purged = Ylm.get_alms_exposure(Auger_TA_events_purged, l_max, ff_purged) Cls = Ylm.get_Cls(alms) Cls_purged = Ylm.get_Cls(alms_purged) plt.plot(xrange(1, l_max + 1), Cls[1:], "b-") # a00 is always 1/sqrt(4pi) #plt.plot(xrange(1, l_max + 1), Cls_purged[1:], "r-", label = r"${\rm TA\ Hotspot\ Subtracted}$") plt.xlabel(r"$\ell$") plt.ylabel(r"$C_\ell$") v = list(plt.axis()) v = [0, l_max + 1, 0, v[3]] plt.axis(v) ells = np.linspace(1, l_max + 1, 100)
def make2Dimage(coeffs, res, px=[64, 64], phs=[0., 0.]): """Make a flat image of a single hemisphere from SWHT image coefficients coeffs: SWHT brightness coefficients px: [int, int], number of pixels, note these are equivalent to the l,m coordinates in FT imaging res: float, resolution of the central pixel in radians phs: [float, float], RA and Dec (radians) position at the center of the image """ start_time = time.time() #start from a regular Cartesian grid lrange = np.linspace( -1. * px[0] * res / 2., px[0] * res / 2., num=px[0], endpoint=True) / ( np.pi / 2.) #m range (-1,1) mrange = np.linspace( -1. * px[1] * res / 2., px[1] * res / 2., num=px[1], endpoint=True) / ( np.pi / 2.) #l range (-1,1) xx, yy = np.meshgrid(lrange, mrange) #xx,yy = np.meshgrid(np.linspace(-1., 1., num=px[0]), np.linspace(-1., 1., num=px[1])) #Full hemisphere, no FoV control img = np.zeros(xx.shape, dtype='complex') #convert to polar positions r = np.sqrt(xx**2. + yy**2.) phi = np.arctan2(yy, xx) # zero out undefined regions of the image where r>0 # overly tedious steps for something that should be much easier to do rflat = r.flatten() phiflat = phi.flatten() maxRcond = r.flatten() > 1 idx = np.argwhere(maxRcond) rflat[idx] = 0. phiflat[idx] = 0. r = np.reshape(rflat, r.shape) phi = np.reshape(phiflat, phi.shape) #convert to unit sphere coordinates thetap = np.arccos( r) - np.pi / 2. #north pole is at 0 in spherical coordinates phip = np.pi - phi #azimuth range [-pi, pi] -> [2pi, 0] #Determine the theta, phi coordinates for a hemisphere at the snapshot zenith X, Y, Z = util.sph2cart(thetap, phip) ra = phs[0] raRotation = np.array([[np.cos(ra), -1. * np.sin(ra), 0.], [np.sin(ra), np.cos(ra), 0.], [0., 0., 1.]]) #rotate about the z-axis dec = np.pi - phs[1] #adjust relative to the north pole at -pi/2 print 'dec', dec, 'phs', phs[1] # TODO: might need to do a transpose to apply the inverse rotation decRotation = np.array([[1., 0., 0.], [0., np.cos(dec), -1. * np.sin(dec)], [0., np.sin(dec), np.cos(dec)]]) #rotate about the x-axis XYZ = np.vstack((X.flatten(), Y.flatten(), Z.flatten())) rotMatrix = np.dot(decRotation, raRotation) XYZ0 = np.dot(rotMatrix, XYZ) #order of rotation is important #XYZ0 = np.dot(np.dot(raRotation, decRotation), XYZ) #order of rotation is important r0, phi0, theta0 = util.cart2sph(XYZ0[0, :], XYZ0[1, :], XYZ0[2, :]) r0 = r0.reshape(thetap.shape) #not used, should all be nearly 1 phi0 = phi0.reshape(thetap.shape) #rotated phi values theta0 = theta0.reshape(thetap.shape) #rotated theta values lmax = coeffs.shape[0] print 'L:', for l in np.arange(lmax): print l, sys.stdout.flush() for m in np.arange(-1 * l, l + 1): img += coeffs[l, l + m] * Ylm.Ylm(l, m, phi0, theta0) #TODO: a slow call print 'done' print 'Run time: %f s' % (time.time() - start_time) return np.ma.array(img, mask=maxRcond)
purge_hotspot = False l_max_max = 10 # load in data Auger_TA_events, ff = Data.load_Auger_TA(E_min_Auger=E_min_Auger, E_scale=E_scale, purge_hotspot=purge_hotspot) N = len(Auger_TA_events) # simulate events without regard to exposure and with exposure Simulated_events = Simulated_Events.gen_iso(len(Auger_TA_events)) Simulated_events_exposure = Simulated_Events.gen_iso_exposure( len(Auger_TA_events), ff) # calculate alms Exact_iso_alms = Ylm.get_alms_iso_exact(l_max_max) Simulated_alms = Ylm.get_alms_iso(Simulated_events, l_max_max) Auger_TA_alms = Ylm.get_alms_exposure(Auger_TA_events, l_max_max, ff) Simulated_alms_exposure = Ylm.get_alms_exposure(Simulated_events_exposure, l_max_max, ff) # calculate chisq's and pvalues, print in a pretty way def out(alms1, alms2, N, l_max_max): for l_max in xrange(1, l_max_max + 1): i = Ylm.lm2i(l_max, l_max) chisq = get_chisq(alms1[:i + 1], alms2[:i + 1], N) dof = ( l_max + 1 )**2 - 1 # -1 because there is no information in the ell = 0 mode print "l_max = {0:2d}, chisq = {1:6.2f}, dof = {2:3d}, chisq/dof = {3:4.2f}, p = {4:9.7f}".format( l_max, chisq, dof, chisq / dof, p_value(chisq, l_max))
import numpy as np import Data import Ylm E_min_Auger = 57 E_scale = 1.13 E_min_TA = E_min_Auger * E_scale l_max = 25 Auger_TA_events, ff = Data.load_Auger_TA(E_min_Auger=E_min_Auger, E_scale=E_scale, purge_hotspot=False) alms = Ylm.get_alms_exposure(Auger_TA_events, l_max, ff) xs = np.empty(len(alms)) for i in xrange(len(alms)): l, m = Ylm.i2lm(i) if l == 0: xs[i] = 0 else: xs[i] = l + 0.2 * m / l plt.plot(xs[1:], alms[1:], "b+") # a00 is always 1/sqrt(4pi) plt.xlabel(r"$\ell+0.2\frac m\ell$") plt.ylabel(r"$a_{\ell m}$") v = list(plt.axis())
import Chisq import warnings warnings.simplefilter("error") E_min_Auger = 53 E_scale = 1.13 purge_hotspot = False l_max = 1 fname = "2MRSEarthmap.txt" events, ff = Data.load_Auger_TA(E_min_Auger=E_min_Auger, E_scale=E_scale, purge_hotspot=purge_hotspot) cplx_alms_Auger_TA = Ylm.get_alms_exposure(events, l_max, ff) real_alms_Auger_TA = Ylm.cplx2real_alms(cplx_alms_Auger_TA) real_alms_2MRS = Ylm.fname2real_alms(fname, l_max) #print real_alms_Auger_TA #print real_alms_2MRS chisq = Chisq.get_chisq(real_alms_Auger_TA, real_alms_2MRS, len(events)) p = Chisq.p_value(chisq, l_max) print chisq, p exit() chisqs = np.zeros(l_max + 1) for l in xrange(1, l_max + 1): i_max = Ylm.lm2i(l, l)