def _get_rootH(self, _type, i, j): """ Return square root R of matrix H = 0.5(Cov^-1 Cl + (Cov^-1 Cl)^t) i.e. R R^t = H. (no beam) :param type: :return: """ assert i < len(_type) and j < len(_type), (i, j, _type) if i > j: return self._get_rootH(_type, j, i) fname = self.lib_dir + '/%s_rootH_%s%s.npy' % (_type, i, j) if not os.path.exists(fname): Cinv = SM.get_Pmat(_type, self.lib_alm, self.cls_len, cl_transf=self.cl_transf, cls_noise=self.cls_noise, inverse=True) R = np.zeros_like(Cinv) for _i in range(len(_type)): for _j in range(len(_type)): for _k in range(0, len(_type)): R[:, _i, _j] += Cinv[:, i, _k] * SM.get_unlPmat_ij(_type, self.lib_alm, self.cls_len, _k, j) del Cinv R = 0.5 * (R + np.swapaxes(R, 1, 2)) # symmetrisation without changing diagonal for _ell in range(self.lib_alm.ellmin, self.lib_alm.ellmax + 1): w, v = np.linalg.eigh(R[_ell, :, :]) assert np.all(w >= 0.), w R_ell = np.dot(v, np.dot(np.diag(np.sqrt(w)), v.T)) assert np.allclose(np.dot(R_ell, R_ell.transpose()), R[_ell, :, :]) R[_ell, :, :] = np.copy(R_ell) for _i in range(len(_type)): for _j in range(_i, len(_type)): _fname = self.lib_dir + '/%s_rootH_%s%s.npy' % (_type, _i, _j) np.save(_fname, R[:, _i, _j]) print "Cached :", _fname return np.load(fname)
def _apply_signal(self, _type, alms, use_Pool=0): assert alms.shape == self._datalms_shape(_type), (alms.shape, self._datalms_shape(_type)) t = timer(_timed, prefix=__name__, suffix=' _apply signal') t.checkpoint("just started") tempalms = np.empty(self._skyalms_shape(_type), dtype=complex) ret = np.empty_like(alms) for _i in range(len(_type)): tempalms[_i] = self._mask_alm(alms[_i], forward=False) tempalms = self._apply_beams(_type, tempalms) t.checkpoint(("masked alms and applied beams")) if use_Pool <= -100: import mllens_GPU.apply_GPU as apply_GPU f = self.f f_inv = self.f_inv apply_GPU.apply_FDxiDtFt_GPU_inplace(_type, self.lib_skyalm, self.lib_skyalm, tempalms, f, f_inv, self.cls_unl) tempalms = self._apply_beams(_type, tempalms) for _i in range(len(_type)): ret[_i] = self._mask_alm(tempalms[_i], forward=True) return ret + self.apply_noise(_type, alms) for _i in range(len(_type)): # Lens with inverse and mult with determinant magnification. tempalms[_i] = self.f_inv.lens_alm(self.lib_skyalm, tempalms[_i], mult_magn=True, use_Pool=use_Pool) # NB : 7 new full sky alms for TQU in this routine - > 4 GB total for full sky lmax_sky = 6000. t.checkpoint("backward lens + det magn") skyalms = np.zeros_like(tempalms) for j in range(len(_type)): for i in range(len(_type)): skyalms[i] += get_unlPmat_ij(_type, self.lib_skyalm, self.cls_unl, i, j) * tempalms[j] del tempalms t.checkpoint("mult with Punl mat ") for i in range(len(_type)): # Lens with forward displacement skyalms[i] = self.f.lens_alm(self.lib_skyalm, skyalms[i], use_Pool=use_Pool) t.checkpoint("Forward lensing mat ") skyalms = self._apply_beams(_type, skyalms) t.checkpoint("Beams") for _i in range(len(_type)): ret[_i] = self._mask_alm(skyalms[_i], forward=True) t.checkpoint("masking") return ret
def get_W2(_type, i, j): ret = SM.get_unlPmat_ij(_type, self.lib_alm, self.cls_len, i, 0) * self._get_Pmatinv(_type, 0, j) for _k in range(1, len(_type)): ret += SM.get_unlPmat_ij(_type, self.lib_alm, self.cls_len, i, _k) * self._get_Pmatinv(_type, _k, j) _cl = self.cl_transf[0:self.lib_alm.ellmax + 1] ** 2 return ret * _cl[self.lib_alm.reduced_ellmat()]
def get_W2(_type, i, j): ret = SM.get_unlPmat_ij(_type, self.lib_alm, self.cls_len, i, 0) * self._get_rootPmatinv(_type, 0, j) for _k in range(1, len(_type)): ret += SM.get_unlPmat_ij(_type, self.lib_alm, self.cls_len, i, _k) * self._get_rootPmatinv(_type, _k, j) return ret * self.cl_transf[self.lib_alm.reduced_ellmat()]