def test_lm2ind(): lm2inddic = {(6, 4):25, (0, 0):0, (10, 0):55} for key, value in lm2inddic.items(): yield ok_, almmod.lm2ind(key) == value yield ok_, almmod.ind2lm(value) == key #Test m-major ordering as well: lm2inddic = {(3, 2):10, (2, 1):6} for key, value in lm2inddic.items(): yield ok_, almmod.lm2ind(key, lmmax=(4, 4), ordering='m-major') == value yield ok_, almmod.ind2lm(value, lmmax=(4, 4), ordering='m-major') == key lm2inddic = {(5, 4):23, (0, 0):0, (6, 6):27} for key, value in lm2inddic.items(): yield ok_, almmod.lm2ind(key, lmmax=(6, 6), ordering='m-major') == value yield ok_, almmod.ind2lm(value, lmmax=(6, 6), ordering='m-major') == key #Test conversion of alms between m-major and l-major ordering lmax = 4 mmax = 4 nels = lmax * (lmax + 1) // 2 + mmax + 1 alms = shaperange_cplx((nels,)) ad = almmod.AlmData(lmax, alms=alms) ad.switchordering() #l2mdic = {13 : 286, 3593 : 2016, 348 : 1957} l2mdic = {6 : 3, 11 : 8} for key, value in l2mdic.items(): yield eq_, int(ad.alms[value].real), key lmax = 5 mmax = 5 nels = lmax * (lmax + 1) // 2 + mmax + 1 alms = shaperange_cplx((nels,)) ad = almmod.AlmData(lmax, alms=alms) ad.switchordering() l2mdic = {5 : 11, 14 : 18, 20 : 20} for key, value in l2mdic.items(): yield eq_, int(ad.alms[value].real), key ls = () for l in range(lmax + 1): for m in range(l + 1): ind = almmod.lm2ind((l, m), (lmax, lmax), ordering='l-major') if ind in ls: raise AssertionError() ls += (ind,)
def alm2ps(ad): if ad.pol_axis is not None: if ad.pol_axis < ad.ind_axis: shape = list(ad.alms.shape[:ad.pol_axis] + (6,) + ad.alms.shape[ad.pol_axis+1:ad.ind_axis] + (ad.lmax + 1,) + ad.alms.shape[ad.ind_axis + 1:]) else: shape = list(ad.alms.shape[:ad.ind_axis] + (ad.lmax + 1,) + ad.alms.shape[ad.ind_axis+1:ad.pol_axis] + (6,) + ad.alms.shape[ad.pol_axis + 1:]) cd = almmod.ClData(ad.lmax, cls = np.zeros(shape), spec_axis=ad.pol_axis, spectra='all') else: shape = list(ad.alms.shape[:ad.ind_axis] + (ad.lmax + 1,) + ad.alms.shape[ad.ind_axis + 1:]) cd = almmod.ClData(ad.lmax, cls=np.zeros(shape)) if cd.spectra != ['TT']: raise NotImplementedError if cd.spectra == ['TT']: if ad.ordering == 'l-major': for l in range(ad.lmax + 1): sl = getslice(cd.cls, cd.cl_axis, l) ind1 = almmod.lm2ind((l, 0), lmmax=(ad.lmax, ad.mmax), \ ordering=ad.ordering) ind2 = almmod.lm2ind((l, min(l, ad.mmax)), \ lmmax=(ad.lmax, ad.mmax), ordering=ad.ordering) asl = list(getslice(ad.alms, ad.ind_axis, ind1)) cd.cls[sl] += ad.alms[asl] ** 2 asl[ad.ind_axis] = slice(ind1 + 1, ind2 + 1) cd.cls[sl] += 2 * np.sum((ad.alms[asl] * \ ad.alms[asl].conjugate()).real) cd.cls[sl] = cd.cls[sl] / (2 * l + 1) else: for l in range(ad.lmax + 1): sl = getslice(cd.cls, cd.cl_axis, l) for m in range(min(l, ad.mmax) + 1): asl = getslice(ad.alms, ad.ind_axis, almmod.lm2ind((l, m), \ lmmax=(ad.lmax, ad.mmax), ordering=ad.ordering)) if m == 0: cd.cls[sl] += ad.alms[asl] ** 2 else: cd.cls[sl] += 2 * (ad.alms[asl] * \ ad.alms[asl].conjugate()).real cd.cls[sl] = cd.cls[sl] / (2 * l + 1) return cd
def test_operators(): ad = almmod.AlmData(lmax=lmax, alms=alms) ad2 = almmod.AlmData(lmax=lmax, alms=alms) yield ok_, np.all(ad.alms + ad2.alms == (ad + ad2).alms) yield ok_, np.all(ad.alms + ad.alms == (ad + ad).alms) yield ok_, np.all(ad.alms * ad2.alms == (ad * ad2).alms) yield ok_, np.all(ad.alms - ad2.alms == (ad - ad2).alms) ad.alms = ad.alms + 1 ad2.alms = ad.alms + 1 yield ok_, np.all(ad.alms / ad2.alms == (ad / ad2).alms) nalms = shaperange_cplx((nels, 2)) ad = almmod.AlmData(lmax=lmax, alms=nalms) for i in range(2): yield ok_, np.all(ad[:, i].alms == ad.alms[:, i]) yield eq_, ad[:, 0].alms.shape, (nels,) # yield eq_, ad.alms[67], ad[67] # ad[87] = 245.23 # yield eq_, ad[87], 245.23 beam = cls bd = beammod.BeamData(lmax=lmax, beam=beam) ad = almmod.AlmData(lmax=lmax, alms=alms) res = np.zeros(alms.shape, dtype=np.complex) for l in range(lmax + 1): for m in range(l + 1): ind = almmod.lm2ind((l, m), (lmax, lmax), ordering='l-major') res[ind] = alms[ind] * beam[l] yield ok_, np.all((ad * bd).alms == res) nalms = shaperange_cplx((nels, 3)) beam = shaperange((lmax + 1, 3)) ad = almmod.AlmData(lmax=lmax, alms=nalms, pol_axis=1) bd = beammod.BeamData(lmax=lmax, beam=beam, pol_axis=1) res = np.zeros(nalms.shape, dtype=np.complex) for l in range(lmax + 1): for m in range(l + 1): ind = almmod.lm2ind((l, m), (lmax, lmax), ordering='l-major') res[ind, :] = nalms[ind, :] * beam[l, :] yield ok_, np.all((ad * bd).alms == res) nalms = shaperange_cplx((3, nels)) beam = shaperange((3, lmax + 1)) ad = almmod.AlmData(lmax=lmax, alms=nalms, pol_axis=0) bd = beammod.BeamData(lmax=lmax, beam=beam, pol_axis=0) res = np.zeros(nalms.shape, dtype=np.complex) for l in range(lmax + 1): for m in range(l + 1): ind = almmod.lm2ind((l, m), (lmax, lmax), ordering='l-major') res[:, ind] = nalms[:, ind] * beam[:, l] yield ok_, np.all((ad * bd).alms == res) nalms = shaperange_cplx((nels,)) beam = shaperange((lmax + 1,)) + 50 bd = beammod.BeamData(lmax=lmax, beam=beam) ad = almmod.AlmData(lmax=lmax, alms=nalms) res = np.zeros(nalms.shape, dtype=np.complex) for l in range(lmax + 1): for m in range(l + 1): ind = almmod.lm2ind((l, m), (lmax, lmax), ordering='l-major') res[ind] = nalms[ind] / beam[l] # There seem to be some numerical inconsistencies here, so omitting this test for now # yield ok_, np.all((ad / bd).alms == res) nalms = shaperange_cplx((nels, 3)) beam = shaperange((lmax + 1, 3)) + 50 ad = almmod.AlmData(lmax=lmax, alms=nalms, pol_axis=1) bd = beammod.BeamData(lmax=lmax, beam=beam, pol_axis=1) res = np.zeros(nalms.shape, dtype=np.complex) for l in range(lmax + 1): for m in range(l + 1): ind = almmod.lm2ind((l, m), (lmax, lmax), ordering='l-major') res[ind, :] = nalms[ind, :] / beam[l, :] yield ok_, np.all((ad / bd).alms == res) nalms = shaperange_cplx((3, nels)) beam = shaperange((3, lmax + 1)) + 50 ad = almmod.AlmData(lmax=lmax, alms=nalms, pol_axis=0) bd = beammod.BeamData(lmax=lmax, beam=beam, pol_axis=0) res = np.zeros(nalms.shape, dtype=np.complex) for l in range(lmax + 1): for m in range(l + 1): ind = almmod.lm2ind((l, m), (lmax, lmax), ordering='l-major') res[:, ind] = nalms[:, ind] / beam[:, l] yield ok_, np.all((ad / bd).alms == res)