def give_me_maps_d1_modified(nus, nubreak, covmap, delta_b, nside, fix_temp=None, nside_index=256): maps_dust = np.ones(((len(nus), 3, 12 * nside**2))) * hp.UNSEEN ind = np.where(covmap > 0)[0] sky = pysm3.Sky(nside=nside, preset_strings=['d1']) maps_dust = sky.get_emission(353 * u.GHz, None) * utils.bandpass_unit_conversion( 353 * u.GHz, None, u.uK_CMB) map_index = np.array(sky.components[0].mbb_index) if fix_temp is not None: sky.components[0].mbb_temperature = fix_temp map_temperature = np.array( np.ones(12 * nside**2) * sky.components[0].mbb_temperature) else: map_temperature = np.array(sky.components[0].mbb_temperature) if nside_index != 256: map_temperature = hp.pixelfunc.ud_grade(map_temperature, nside_index) map_index = hp.pixelfunc.ud_grade(map_index, nside_index) map_temperature = hp.pixelfunc.ud_grade(map_temperature, 256) map_index = hp.pixelfunc.ud_grade(map_index, 256) #hp.mollview(map_temperature, sub=(1, 2, 1)) #hp.mollview(map_index, sub=(1, 2, 2)) #print(map_index.shape) # Evaluation of Mixing Matrix for 2 beta model comp2b = [fgbuster.component_model.Dust_2b(nu0=353)] A2b = fgbuster.MixingMatrix(*comp2b) A2b_ev = A2b.evaluator(nus) new_dust_map = np.ones(((len(nus), 3, 12 * nside**2))) * hp.UNSEEN for i in ind: A2b_maxL = A2b_ev([ np.array(map_index)[i] - delta_b, np.array(map_index)[i] + delta_b, nubreak, np.array(map_temperature)[i] ]) for j in range(len(nus)): new_dust_map[j, :, i] = A2b_maxL[j, 0] * maps_dust[:, i] return new_dust_map, [map_index, map_temperature]
def create_dust_with_model2beta(maps_353GHz, nside, nus, betad0, betad1, nubreak, temp, break_width): comp2b = [ fgbuster.component_model.Dust_2b(nu0=353, break_width=break_width) ] A2b = fgbuster.MixingMatrix(*comp2b) A2b_ev = A2b.evaluator(nus) A2b_maxL = A2b_ev([betad0, betad1, nubreak, temp]) new_dust_map = np.zeros( ((len(nus), maps_353GHz.shape[0], maps_353GHz.shape[1]))) for i in range(len(nus)): #print('i = ', i) new_dust_map[i] = A2b_maxL[i, 0] * maps_353GHz return new_dust_map
def create_sync(nside, nus): # Create 353 GHz dust maps sky = pysm3.Sky(nside=nside, preset_strings=['s0']) maps_70GHz = sky.get_emission(70 * u.GHz, None) * utils.bandpass_unit_conversion( 70 * u.GHz, None, u.uK_CMB) comp = [fgbuster.component_model.Synchrotron(nu0=70)] A = fgbuster.MixingMatrix(*comp) A_ev = A.evaluator(nus) A_maxL = A_ev(np.array([-3])) new_sync_map = np.zeros(((len(nus), 3, 12 * nside**2))) for i in range(len(nus)): new_sync_map[i] = A_maxL[i, 0] * maps_70GHz return new_sync_map
def apply_fgb(inmaps, freqs, fwhms, verbose=True, apodize=0, plot_apo=False, apocut=False, apotype='C1', coverage_recut=None, coverage=None, resol_correction=True, ref_fwhm=0.5, alm_space=False, plot_separated=False, center=None, add_title='', plot_residuals=False, truth=None, alm_maps=False, apply_to_unconvolved=False): ### FGB Configuration instrument = fgb.get_instrument('Qubic') instrument.frequency = freqs instrument.fwhm = fwhms components = [fgb.Dust(150., temp=20.), fgb.CMB()] ### Check good pixels pixok = inmaps[0, 0, :] != hp.UNSEEN nside = hp.npix2nside(len(inmaps[0, 0, :])) if apodize != 0: mymask = pixok.astype(float) nmt = nam.Namaster(mymask, 40, 400, 30, aposize=apodize, apotype=apotype) apodized_mask = nmt.get_apodized_mask() if plot_apo: hp.gnomview(apodized_mask, title='Apodized Mask {} deg.'.format(apodize), reso=15, rot=center) maps = inmaps * apodized_mask maps[:, :, ~pixok] = hp.UNSEEN else: maps = inmaps.copy() apodized_mask = np.ones(12 * nside**2) ### Data to feed FGB: if alm_space: if verbose: print( '\nFBG in alm-space with resol_correction={} and ref_resol={}'. format(resol_correction, ref_fwhm)) mydata = get_alm_maps(maps, fwhms, ref_fwhm=ref_fwhm, resol_correction=resol_correction, verbose=verbose) if (truth is not None) & ~apply_to_unconvolved: if verbose: print('\nNow reconvolving truth to ref_fwhm = {}'.format( ref_fwhm)) mytruth = [ hp.smoothing(truth[0], fwhm=np.deg2rad(ref_fwhm), pol=True, verbose=False), hp.smoothing(truth[1], fwhm=np.deg2rad(ref_fwhm), pol=True, verbose=False) ] space = ' (alm based)' else: if verbose: print( '\nFBG in pixel-space with resol_correction={} and ref_resol={}' .format(resol_correction, ref_fwhm)) if resol_correction: if verbose: print('\nNow reconvolving input maps to ref_fwhm = {}'.format( ref_fwhm)) mydata = reconvolve(maps, fwhms, ref_fwhm, verbose=verbose) if (truth is not None): if verbose: print( '\nNow reconvolving truth (dust and CMB) to ref_fwhm = {}' .format(ref_fwhm)) mytruth = [ hp.smoothing(truth[0], fwhm=np.deg2rad(ref_fwhm), pol=True, verbose=False), hp.smoothing(truth[1], fwhm=np.deg2rad(ref_fwhm), pol=True, verbose=False) ] space = ' (Pixel based - Reconv.)' else: mydata = maps if (truth is not None): mytruth = truth.copy() space = ' (Pixel based - No Reconv.)' if coverage_recut is not None: if verbose: print('Applying coverage recut to {}'.format(coverage_recut)) fidregion = (coverage > (coverage_recut * np.max(coverage))) mydata[..., ~fidregion] = hp.UNSEEN mapregions = np.zeros(12 * nside**2) + hp.UNSEEN mapregions[pixok] = 1 mapregions[fidregion] = 2 if verbose: hp.gnomview(mapregions, rot=center, reso=15, title='Fiducial region: {}'.format(coverage_recut)) show() if (apodize != 0) & (apocut == True): fidregion = apodized_mask == 1 mydata[..., ~fidregion] = hp.UNSEEN ### FGB itself if verbose: print('Starting FGBuster in s') r = separate(components, instrument, mydata, print_option=verbose) if verbose: print('Resulting beta: {}'.format(r.x[0])) ### Resulting separated maps if alm_space: if alm_maps: ### Directly use the output from FGB => not very accurate because of alm-transform near the edges of the patch almdustrec = r.s[0, :, :] print('ALM', np.shape(almdustrec)) dustrec = hp.alm2map( almdustrec[..., ::2] + almdustrec[..., 1::2] * 1j, nside) dustrec[:, ~pixok] = hp.UNSEEN almcmbrec = r.s[1, :, :] cmbrec = hp.alm2map( almcmbrec[..., ::2] + almcmbrec[..., 1::2] * 1j, nside) cmbrec[:, ~pixok] = hp.UNSEEN else: ### Instead we use the fitted beta and recalculate the component maps in pixel space ### This avoids inaccuracy of the alm transformation near the edges of the patch A = fgb.MixingMatrix(*components) print(' >>> building s = Wd in pixel space') A_ev = A.evaluator(instrument.frequency) A_maxL = A_ev(r.x) print('A_maxl', np.shape(A_maxL)) if apply_to_unconvolved: # We apply the mixing matrix to maps each at its own resolution... # this allows to keep the effecgive resolution as good as possible, but surely the bell is no # longer Gaussian. It is the combnation of various resolutions with weird weights. themaps = maps.copy() # Now we calculate the effective Bell Bl_each = [] for fw in fwhms: Bl_gauss_fwhm = hp.gauss_beam(np.radians(fw), lmax=2 * nside + 1) Bl_each.append(Bl_gauss_fwhm) plot(Bl_gauss_fwhm, label='FWHM {:4.2}'.format(fw)) Bl_each = np.array(Bl_each) ### Compute W matrix (from Josquin) invN = np.diag(np.ones(len(fwhms))) inv_AtNA = np.linalg.inv(A_maxL.T.dot(invN).dot(A_maxL)) W = inv_AtNA.dot(A_maxL.T).dot(invN) Bl_eff_Dust = W.dot(Bl_each)[0] #1 is for the Dust Bl_eff_CMB = W.dot(Bl_each)[1] #1 is for the CMB component # print('W matrix') # print(W) # print('A_maxL matrix') # print(A_maxL) # print('Bl_each:',np.shape(Bl_each)) Bl_eff_Dust_new = np.zeros(2 * nside + 1) Bl_eff_CMB_new = np.zeros(2 * nside + 1) ones_comp = np.ones(2) #number of components for i in range(2 * nside + 1): blunknown = W.dot(Bl_each[:, i] * np.dot(A_maxL, ones_comp)) Bl_eff_Dust_new[i] = blunknown[0] Bl_eff_CMB_new[i] = blunknown[1] plot(Bl_eff_CMB, ':', label='Effective Bl CMB: W.Bl', lw=3) plot(Bl_eff_Dust, ':', label='Effective Bl Dust: W.Bl', lw=3) plot(Bl_eff_CMB_new, '--', label='Effective Bl CMB NEW: W.B.A.1', lw=3) plot(Bl_eff_Dust_new, '--', label='Effective Bl Dust NEW: W.B.A.1', lw=3) legend() # We need to smooth the truth with this Bell if (truth is not None): if verbose: print( '\nNow reconvolving truth (dust and CMB) with effective Bell' ) mytruth = [ hp.smoothing(truth[0], beam_window=Bl_eff_Dust_new, pol=True, verbose=False), hp.smoothing(truth[1], beam_window=Bl_eff_CMB_new, pol=True, verbose=False) ] else: # We will apply the mixing matrix to maps at a chosen reeference resolution # this might not bee the most optimal although it is simple in the sens that the components maps resolution # is what we have chosen and is gaussian. themaps = reconvolve(maps, fwhms, ref_fwhm, verbose=verbose) components_bell = hp.gauss_beam(np.radians(ref_fwhm), lmax=2 * nside + 1) themaps[themaps == hp.UNSEEN] = 0 ### Needed to comment the two lines below as prewhiten_factors was None #prewhiten_factors = fgb.separation_recipes._get_prewhiten_factors(instrument, themaps.shape, nside) #invN = np.zeros(prewhiten_factors.shape+prewhiten_factors.shape[-1:]) r.s = fgb.algebra.Wd(A_maxL, themaps.T) #, invN=invN) r.s = np.swapaxes(r.s, -1, 0) dustrec = r.s[0, :, :] dustrec[:, ~pixok] = hp.UNSEEN cmbrec = r.s[1, :, :] cmbrec[:, ~pixok] = hp.UNSEEN else: dustrec = r.s[0, :, :] cmbrec = r.s[1, :, :] if plot_separated: display_maps( dustrec, bigtitle=r'$\beta=${0:7.6f} - Reconstructed Dust'.format(r.x[0]) + space, rot=center, figsize=(16, 7)) display_maps( cmbrec, bigtitle=r'$\beta=${0:7.6f} - Reconstructed CMB'.format(r.x[0]) + space, rot=center, figsize=(16, 7)) if truth: resid_dust = dustrec - mytruth[0] * apodized_mask resid_dust[:, ~pixok] = hp.UNSEEN resid_cmb = cmbrec - mytruth[1] * apodized_mask resid_cmb[:, ~pixok] = hp.UNSEEN if coverage_recut: resid_cmb[:, ~fidregion] = hp.UNSEEN resid_dust[:, ~fidregion] = hp.UNSEEN pixok = fidregion.copy() if (apodize != 0) & (apocut == True): resid_cmb[:, ~fidregion] = hp.UNSEEN resid_dust[:, ~fidregion] = hp.UNSEEN pixok = fidregion.copy() sigs_dust = np.std(resid_dust[:, pixok], axis=1) sigs_cmb = np.std(resid_cmb[:, pixok], axis=1) if plot_residuals: # display_maps(mytruth[0], bigtitle=r'$\beta=${0:7.6f} Input Dust'.format(r.x[0])+space, # rot=center, figsize=(16, 7), add_rms=True) # display_maps(mytruth[1], bigtitle=r'$\beta=${0:7.6f} - Input CMB'.format(r.x[0])+space, # rot=center, figsize=(16, 7), add_rms=True) display_maps(mytruth[0], bigtitle='Truth Dust Reconvolved', rot=center, figsize=(16, 7), add_rms=True, unseen=~pixok) display_maps(mytruth[1], bigtitle='Truth CMB Reconvolved', rot=center, figsize=(16, 7), add_rms=True, unseen=~pixok) display_maps( resid_dust, bigtitle=r'$\beta=${0:7.6f} Residuals Dust'.format(r.x[0]) + space, rot=center, figsize=(16, 7), add_rms=True) display_maps( resid_cmb, bigtitle=r'$\beta=${0:7.6f} - Residuals CMB'.format(r.x[0]) + space, rot=center, figsize=(16, 7), add_rms=True) figure() suptitle(r'$\beta=${0:7.6f} - Residuals:'.format(r.x[0]) + space, fontsize=30, y=1.05) for i in range(3): subplot(1, 3, i + 1) hist(resid_dust[i, pixok], range=[-5 * sigs_dust[i], 5 * sigs_dust[i]], bins=100, alpha=0.5, color='b', label='Dust: RMS={:4.2g}'.format(sigs_dust[i]), density=True) hist(resid_cmb[i, pixok], range=[-5 * sigs_cmb[i], 5 * sigs_cmb[i]], bins=100, alpha=0.5, color='r', label='CMB: RMS={:4.2g}'.format(sigs_cmb[i]), density=True) title('Residuals Stokes {}'.format(stk[i])) legend() tight_layout() if truth: return r.x[ 0], dustrec, cmbrec, sigs_dust, sigs_cmb, resid_dust, resid_cmb, mytruth[ 0], mytruth[1] else: return r.x[0], dustrec, cmbrec
def _RUN_MC(self, N, fit, x0, covmap, beta=[], nside_param=0, fixcmb=True, noiseless=False): maskpix = np.zeros(12 * 256**2) pixok = covmap > 0 maskpix[pixok] = 1 Namaster = nam.Namaster(maskpix, lmin=21, lmax=355, delta_ell=35) comp, _ = self._get_comp_instr(nu0=145, fit=fit, x0=x0) instr = self._get_instr_nsamples(N_SAMPLE_BAND=10) inputs = self.newfg.copy() clBB = np.zeros((N, 9)) clres = np.zeros((N, 9)) components = np.zeros((N, 3, 3, np.sum(pixok))) if nside_param != 0: beta = np.zeros( (N, fgbuster.MixingMatrix(*comp).n_param, 12 * 256**2)) else: beta = np.zeros((N, fgbuster.MixingMatrix(*comp).n_param)) print('********** Separation ************') for i in range(N): if fixcmb: seed = 42 else: seed = np.random.randint(1000000) print(seed) cmb = self._get_cmb(self.covmap, seed) if self.nside != 256: newcmb = np.zeros((self.newfg.shape[0], self.newfg.shape[0], 12 * self.nside**2)) for i in range(self.fg.shape[0]): newcmb[i] = hp.pixelfunc.ud_grade(cmb[i], self.nside) else: newcmb = cmb.copy() data = inputs + newcmb.copy() print(data.shape) r1, components1 = self._separation(comp, instr, data, self.covmap, beta, nside_param, fit, x0, noiseless) r2, components2 = self._separation(comp, instr, data, self.covmap, beta, nside_param, fit, x0, noiseless) beta[i] = r1.copy() components1[:, :, ~pixok] = 0 components2[:, :, ~pixok] = 0 components[i] = components1[:, :, pixok].copy() leff, clBB[i] = self._get_clBB(components1[0], components2[0], Namaster) #res1=cmb[0]-components1[0] #res2=cmb[0]-components2[0] #res1[:, ~pixok]=0 #res2[:, ~pixok]=0 #leff, clres[i]=self._get_clBB(res1, res2, Namaster) #print(cmb[0, 1, pixok]-components1[0, 1, pixok]) return leff, clBB, beta, components