def plot(self, num_pointsx=100, num_pointsy=100, num_contours=75, logz=False, show=True): """Plot the spline. Args ---- num_pointsx : int The number of x sampling points to be used to draw the spline. num_pointsy : int The number of y sampling points to be used to draw the spline. num_contours : int The number of contours for the color plot. show : bool, optional If True, `plt.show()` is called at the end, interrupting the flow. """ from GRATools.utils.matplotlib_ import pyplot as plt _x = numpy.logspace(self.xmin(), self.xmax(), num_pointsx) _y = numpy.logspace(self.ymin(), self.ymax(), num_pointsy) _x, _y = numpy.meshgrid(_x, _y) _z = self(_x, _y, grid=False) if logz: from matplotlib.colors import LogNorm _levels = numpy.logspace(-4, numpy.log10(_z.max()), num_contours) contour = plt.contourf(_x, _y, _z, levels=_levels, norm = LogNorm()) else: contour = plt.contourf(_x, _y, _z, num_contours) bar = plt.colorbar() if self.xname is not None: plt.xlabel(self.xlabel()) if self.yname is not None: plt.ylabel(self.ylabel()) if self.zname is not None: bar.set_label(self.zlabel()) if show: plt.show()
def plot(self, num_points=1000, overlay=False, logx=False, logy=False, scale=1., offset=0., show=True, **kwargs): """Plot the spline. Args ---- num_points : int, optional The number of sampling points to be used to draw the spline. overlay : bool, optional If True, the original arrays passed to the spline are overlaid. show : bool, optional If True, `plt.show()` is called at the end, interrupting the flow. """ from GRATools.utils.matplotlib_ import pyplot as plt if not logx: _x = numpy.linspace(self.xmin(), self.xmax(), num_points) else: _x = numpy.logspace(numpy.log10(self.xmin()), numpy.log10(self.xmax()), num_points) _y = scale*self(_x) + offset if overlay: plt.plot(_x, _y, '-', self.x, self.y, 'o', **kwargs) else: plt.plot(_x, _y, '-', **kwargs) if self.xname is not None: plt.xlabel(self.xlabel()) if self.yname is not None: plt.ylabel(self.ylabel()) if logx: plt.gca().set_xscale('log') if logy: plt.gca().set_yscale('log') if show: plt.show()
def get_psf(psf_file, show=False): """Get the PSF from the fits file created by gtpsf """ hdu_list = pf.open(psf_file) _th = np.radians(hdu_list['THETA'].data.field('Theta')) _en = hdu_list['PSF'].data.field('ENERGY') _psf = hdu_list['PSF'].data.field('PSF') fmt = dict(yname='Theta', yunits='rad', xname='Energy', xunits='MeV', zname='PSF') psf_th_e = xInterpolatedBivariateSplineLinear(_en, _th, _psf, **fmt) hdu_list.close() if show == True: for e in _en: psf_th = psf_th_e.vslice(e) fmt = dict(xname='cos(th)', xunits='', yname='psf[cos(th)]',\ yunits='') _psfxsin = xInterpolatedUnivariateSplineLinear(psf_th.x, \ psf_th.y*np.sin(psf_th.x), **fmt) plt.plot(np.degrees(psf_th.x), psf_th.y, label='%.2f'%e) print 'INT(E=%.2f) ='%e, 2*np.pi*_psfxsin.integral(0, \ np.amax(psf_th.x[:-2])) plt.yscale('log') plt.legend() plt.show() return psf_th_e
def mkCl(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Starting Cl analysis...') e_min = data.E_MIN e_max = data.E_MAX in_label = 'fore' out_label = in_label mask_file = data.MASK_FILE cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_polspicecls.txt' % out_label), 'w') for i, (emin, emax) in enumerate(zip(e_min, e_max)): logger.info('Considering bin %.2f - %.2f ...' % (emin, emax)) mask_f = mask_file if type(mask_file) == list: mask_f = mask_file[i] cl_txt.write('ENERGY\t %.2f %.2f\n' % (emin, emax)) l_max = 1000 _l = np.arange(l_max) flux_map_name = in_label + '_%i-%i.fits' % (emin, emax) flux_map_f = os.path.join(GRATOOLS_OUT, 'output_fore/' + flux_map_name) if kwargs['show'] == True: hp.mollview(flux_map_masked.filled(), title='fore map', min=1e-7, max=1e-4, norm='log') plt.show() out_name = '%s_%i-%i' % (out_label, emin, emax) out_folder = os.path.join(GRATOOLS_OUT, 'output_pol') if not os.path.exists(out_folder): os.makedirs(out_folder) pol_dict = data.POLCEPICE_DICT for key in pol_dict: if key == 'clfile': pol_dict[key] = os.path.join(out_folder, '%s_cl.txt' % out_name) if key == 'cl_outmap_file': pol_dict[key] = os.path.join(out_folder, '%s_clraw.txt' % out_name) if key == 'covfileout': pol_dict[key] = os.path.join(out_folder, '%s_cov.fits' % out_name) if key == 'mapfile': pol_dict[key] = flux_map_f if key == 'maskfile': pol_dict[key] = mask_f config_file_name = 'pol_%s' % (out_name) _l, _cl, _cl_err = pol_cl_calculation(pol_dict, config_file_name) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Cl_ERR\t%s\n\n'%str(list(_cl_err)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s' % (os.path.join(GRATOOLS_OUT, '%s_polspicecls.txt' % out_label)))
def main(): """Test module """ plt.figure() leg, lab = ref_igrb_band() igrb, lab2 = ref_igrb_noFGsub() plt.xscale('log') plt.yscale('log') plt.legend([leg, igrb], [lab, lab2]) plt.show()
def main(): """ """ cl_f = 'output/output_pol/mask_check_cl.txt' cov_f = 'output/output_pol/mask_check_cov.fits' l, cl = pol_cl_parse(cl_f) clerr = pol_cov_parse(cov_f) #plt.errorbar(l, cl, fmt='o', markersize=3, elinewidth=1, yerr=clerr) plt.plot(l, cl, 'o', markersize=3) plt.xscale('log') plt.yscale('log') #plt.ylim(-1e-15, 1e-15) plt.show()
def main(): """ crbkg_PSF1_file = os.path.join(GRATOOLS_CONFIG, 'ascii/p8r2_bkg_flux_t8.txt') crbkg_PSF1 = get_crbkg(crbkg_PSF1_file) crbkg_PSF1.plot(show=False) x = crbkg_PSF1.x xx = x**2 print x y1 = crbkg_PSF1.y crbkg_PSF2_file = os.path.join(GRATOOLS_CONFIG, 'ascii/p8r2_bkg_flux_t16.txt') crbkg_PSF2 = get_crbkg(crbkg_PSF2_file) crbkg_PSF2.plot(show=False) y2 = crbkg_PSF2(x) crbkg_PSF3_file = os.path.join(GRATOOLS_CONFIG, 'ascii/p8r2_bkg_flux_t32.txt') crbkg_PSF3 = get_crbkg(crbkg_PSF3_file) crbkg_PSF3.plot(show=False) y3 = crbkg_PSF2(x) y = (y1/x+y2/x+y3/x)*x print y plt.yscale('log') plt.xscale('log') plt.show() fore_files_list = [os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_146647.fits'), os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_200561.fits'), os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_274296.fits'), os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_375138.fits'), os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_513056.fits'), ] e_min, e_max = 524807.00, 575439.00 get_foreground_integral_flux_map(fore_files_list, e_min, e_max)""" from scipy.optimize import curve_fit ClFORE_FILE = os.path.join(GRATOOLS_OUT, 'fore_polspicecls.txt') min1, emax1, clsfore = clfore_parse(ClFORE_FILE) l = np.arange(len(clsfore[0])) clsfore_spline = xInterpolatedUnivariateSplineLinear(l, clsfore[0]) clsfore_spline.plot(show=False) plt.xscale('log') plt.yscale('log') plt.show()
def mkForeCl(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Starting Cl analysis...') in_label = data.IN_LABEL out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL from GRATools.utils.gFTools import get_cl_param cl_param_file = os.path.join( GRATOOLS_OUT, '%s_%s_parameters.txt' % (in_label, binning_label)) _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): mask_file = data.MASK_FILE if type(mask_file) == list: mask_file = mask_file[i] mask = hp.read_map(mask_file) logger.info('Considering bin %.2f - %.2f ...' % (emin, emax)) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n' % (emin, emax, _emean[i])) l_max = 1000 _l = np.arange(l_max) flux_map_name = in_label + '_fore_%i-%i.fits' % (emin, emax) flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FORE, flux_map_name)) flux_map_masked = hp.ma(flux_map) flux_map_masked.mask = np.logical_not(mask) fsky = 1.-(len(np.where(flux_map_masked.filled() == hp.UNSEEN)[0])/\ float(len(flux_map))) if kwargs['show'] == True: hp.mollview(flux_map_masked.filled(), title='f$_{sky}$ = %.3f' % fsky, min=1e-7, max=1e-4, norm='log') plt.show() print 'fsky = ', fsky nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] _cl = hp.sphtfunc.anafast(flux_map_masked.filled(), lmax=l_max-1, \ iter=5) _cl_fit = hp.sphtfunc.anafast(flux_map_masked.filled(), iter=4) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \ %(out_label, binning_label))))
def main(): """Test module """ leg, lab = ref_cp_band() plt.legend() plt.xscale('log') plt.yscale('log') plt.show() plt.figure() leg, lab = ref_igrb_band() igrb, lab2 = ref_igrb_noFGsub() plt.xscale('log') plt.yscale('log') plt.legend([leg, igrb], [lab, lab2]) plt.show()
def mkForeCl(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Starting Cl analysis...') in_label = data.IN_LABEL out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL from GRATools.utils.gFTools import get_cl_param cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' %(in_label, binning_label)) _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): mask_file = data.MASK_FILE if type(mask_file) == list: mask_file = mask_file[i] mask = hp.read_map(mask_file) logger.info('Considering bin %.2f - %.2f ...'%(emin, emax)) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, _emean[i])) l_max= 1000 _l = np.arange(l_max) flux_map_name = in_label+'_fore_%i-%i.fits'%(emin, emax) flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FORE, flux_map_name)) flux_map_masked = hp.ma(flux_map) flux_map_masked.mask = np.logical_not(mask) fsky = 1.-(len(np.where(flux_map_masked.filled() == hp.UNSEEN)[0])/\ float(len(flux_map))) if kwargs['show'] == True: hp.mollview(flux_map_masked.filled(), title='f$_{sky}$ = %.3f'%fsky, min=1e-7, max=1e-4, norm='log') plt.show() print 'fsky = ', fsky nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] _cl = hp.sphtfunc.anafast(flux_map_masked.filled(), lmax=l_max-1, \ iter=5) _cl_fit = hp.sphtfunc.anafast(flux_map_masked.filled(), iter=4) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_forecls.txt' \ %(out_label, binning_label))))
def remove_multipole(map_file_name, lmax=10): """Returns a map obtained from the one given and cleaned for monopole and dipole. map_file_name : str path and name of the map you want to clean for monopole and dipole. lmax : int maximum multiple number upto which perform the removal """ map_file_name_clean = map_file_name.replace('.fits', '_multiclean.fits') flux_map = hp.read_map(map_file_name) alm = hp.sphtfunc.map2alm(flux_map) map_to_remove = hp.sphtfunc.alm2map(alm, hp.npix2nside(len(flux_map))) hp.mollview(map_to_remove) plt.show() new_flux_map = flux_map - map_to_remove hp.write_map(map_file_name_clean, new_flux_map) return map_file_name_clean
def plot(self, num_points=1000, overlay=False, logx=False, logy=False, scale=1., offset=0., show=True, **kwargs): """Plot the spline. Args ---- num_points : int, optional The number of sampling points to be used to draw the spline. overlay : bool, optional If True, the original arrays passed to the spline are overlaid. show : bool, optional If True, `plt.show()` is called at the end, interrupting the flow. """ from GRATools.utils.matplotlib_ import pyplot as plt if not logx: _x = numpy.linspace(self.xmin(), self.xmax(), num_points) else: _x = numpy.logspace(numpy.log10(self.xmin()), numpy.log10(self.xmax()), num_points) _y = scale * self(_x) + offset if overlay: plt.plot(_x, _y, '-', self.x, self.y, 'o', **kwargs) else: plt.plot(_x, _y, '-', **kwargs) if self.xname is not None: plt.xlabel(self.xlabel()) if self.yname is not None: plt.ylabel(self.ylabel()) if logx: plt.gca().set_xscale('log') if logy: plt.gca().set_yscale('log') if show: plt.show()
def plot(self, num_pointsx=100, num_pointsy=100, num_contours=75, logz=False, show=True): """Plot the spline. Args ---- num_pointsx : int The number of x sampling points to be used to draw the spline. num_pointsy : int The number of y sampling points to be used to draw the spline. num_contours : int The number of contours for the color plot. show : bool, optional If True, `plt.show()` is called at the end, interrupting the flow. """ from GRATools.utils.matplotlib_ import pyplot as plt _x = numpy.linspace(self.xmin(), self.xmax(), num_pointsx) _y = numpy.linspace(self.ymin(), self.ymax(), num_pointsy) _x, _y = numpy.meshgrid(_x, _y) _z = self(_x, _y, grid=False) if logz: from matplotlib.colors import LogNorm _levels = numpy.logspace(-4, numpy.log10(_z.max()), num_contours) contour = plt.contourf(_x, _y, _z, levels=_levels, norm=LogNorm()) else: contour = plt.contourf(_x, _y, _z, num_contours) bar = plt.colorbar() if self.xname is not None: plt.xlabel(self.xlabel()) if self.yname is not None: plt.ylabel(self.ylabel()) if self.zname is not None: bar.set_label(self.zlabel()) if show: plt.show()
def main(): """Simple test unit """ from GRATools.utils.gWindowFunc import get_psf_ref psf_ref_file = os.path.join(GRATOOLS_CONFIG, 'ascii/PSF_UCV_PSF1.txt') psf_ref = get_psf_ref(psf_ref_file) energy = np.array([ 743.73, 1340.69, 2208.67, 3692.56, 6416.93, 11151.34, 18370.95, 30713.33, 53373.66, 92752.78, 152802.88, 255462.38, 443942.75 ]) psf_en = psf_ref(energy) psf_min, psf_max = psf_ref.y[5], psf_ref.y[-1] norm_min, norm_max = 1, 0.3 norm = norm_min + psf_en*((norm_max - norm_min)/(psf_max - psf_min)) -\ psf_min*((norm_max - norm_min)/(psf_max - psf_min)) plt.title('Normalization Factor') plt.plot(energy, norm, 'ro--', ms=5, alpha=0.75) #plt.plot((1e-12, 1e-5), (2, 2), '-', color='silver', linewidth=1.0) plt.xlabel('Energy [MeV]') plt.ylabel('Normalization factor') plt.yscale('log') plt.xscale('log') plt.ylim(0.09, 2) plt.show() nside = 512 SRC_CATALOG_FILE = os.path.join(FT_DATA_FOLDER, 'catalogs/gll_psc_v16.fit') bad_pix = mask_src_weighted(SRC_CATALOG_FILE, 10000, nside) bad_pix += mask_gp(30, nside) npix = hp.nside2npix(nside) mask = np.ones(npix) for bpix in np.unique(bad_pix): mask[bpix] = 0 fsky = 1 - (len(np.unique(bad_pix)) / float(npix)) title = 'f$_{sky}$ = %.3f' % fsky hp.mollview(mask, title=title, coord='G') plt.show()
def main(): """Test module """ """ plt.figure(figsize=(10, 7), dpi=80) _l = np.arange(0, 10, 2) for l in _l: pl_th = get_pl_vs_th(l, np.arange(-1, 1, 0.00001)) plt.plot(np.arange(-1, 1, 0.00001), pl_th, '.', label='l = %i'%l) plt.legend() #plt.show() """ #out_wbeam_txt = 'output/Wbeam_P8R2_ULTRACLEANVETO_V6_56.txt' #wb = get_wbeam(out_wbeam_txt) #wb.plot() """ NSIDE = 1 NPIX = hp.nside2npix(NSIDE) iii = np.arange(NPIX) dec, ra = IndexToDeclRa(NSIDE, iii) index = np.where(abs(dec)>10) ra = ra[index] dec = dec[index] #ra = ra #dec = dec plt.figure(figsize=(10, 7), dpi=80) hp.mollview(iii, title="Mollview image RING") plt.figure(figsize=(10, 7), dpi=80) lab, plots = [], [] for i in range(0, len(ra)): print ra[i], dec[i] out_wbeam_txt = 'output/%i_prova.txt'%i psf_file = 'output/%i_prova.fits'%i dict_gtpsf = {'expcube':'/data1/data/FT-files/output/output_gtltcube'+\ '/Allyrs_filtered_gti_ltcube.fits', 'outfile': psf_file, 'irfs': 'P8R2_ULTRACLEANVETO_V6', 'evtype': 3, 'ra': ra[i], 'dec': dec[i], 'emin': 500, 'emax': 600000, 'nenergies': 10, 'thetamax': 30, 'ntheta': 300} from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf) _l = np.arange(0, 1000, 4) psf = get_psf(psf_file) if not os.path.exists(out_wbeam_txt): wb = build_wbeam(psf, _l, out_wbeam_txt) else: wb = get_wbeam(out_wbeam_txt) wb_1GeV = wb.hslice(1000) wl = wb_1GeV.plot(show=False, label='RA:%i, Dec:%i'%(ra[i],dec[i])) #lab.append('%i-%i'%(ra[i],dec[i])) #plots.append(wl) """ out_wbeam_txt = 'output/Wbeam_P8R2_ULTRACLEANVETO_V6_56.txt' wb = get_wbeam(out_wbeam_txt) plt.figure(figsize=(10, 7), dpi=80) _l = np.arange(0, 1000, 4) wb_500MeV = wb.hslice(500) plt.plot(wb_500MeV.x, wb_500MeV.y**2, label='0.5 GeV') wb_5GeV = wb.hslice(5000) plt.plot(wb_5GeV.x, wb_5GeV.y**2, label='5 GeV') wb_50GeV = wb.hslice(50000) plt.plot(wb_50GeV.x, wb_50GeV.y**2, label='50 GeV') wb_100GeV = wb.hslice(100000) plt.plot(wb_100GeV.x, wb_100GeV.y**2, label='100 GeV') wb_300GeV = wb.hslice(300000) plt.plot(wb_300GeV.x, wb_300GeV.y**2, label='300 GeV') plt.legend() plt.xlabel('Energy [MeV]') plt.ylabel('W$^{2}$$_{beam}$') plt.show()
def main(): """Simple test unit """ from scipy.interpolate import interp1d cat_file = os.path.join(GRATOOLS_CONFIG,'catalogs/gll_psc_v16.fit') src_cat = pf.open(cat_file) CAT = src_cat['LAT_Point_Source_Catalog'] SOURCES = CAT.data src_cat.close() flux = np.sort(SOURCES.field('Flux1000')) flux_min, flux_max = min(flux), max(flux) """ rad_min, rad_max = 0.5, 5. RADdeg = rad_min + flux*((rad_max - rad_min)/(flux_max - flux_min)) -\ flux_min*((rad_max - rad_min)/(flux_max - flux_min)) plt.figure(facecolor='white') plt.title('Disk Radius as a function of $\Phi_{src}$') plt.plot(flux, RADdeg, '*-', color='cornflowerblue', linewidth=1, ms=5, alpha=0.2) plt.xlabel('Flux [photon/cm$^{2}$/s]') plt.ylabel('mask disk Radius [$^{\circ}$]') plt.yscale('log') plt.xscale('log') plt.ylim(1.5, 6) plt.grid(which='both', linestyle='--', linewidth=1) """ """ from GRATools.utils.gWindowFunc import get_psf_ref psf_ref_file = os.path.join(GRATOOLS_CONFIG, 'ascii/PSF_UCV_custom.txt') psf_ref = get_psf_ref(psf_ref_file) energy = np.logspace(1, 6, 10) energy = [301.99,524.81,1000.00,2754.23, 8317.64,22908.68,331131.12] lab = ['PSF3','PSF3','PSF1+2+3', 'PSF1+2+3', 'PSF1+2+3','PSF1+2+3', 'PSF1+2+3'] plt.figure(facecolor='white') for i, en in enumerate(energy): r_min = 2*psf_ref(en) r_max = 5*psf_ref(en) print 'R min', r_min, 'R max', r_max print 'F min', flux_min, 'F max', flux_max RADdeg = r_min + np.log10(flux)*((r_max - r_min)/(np.log10(flux_max) - np.log10(flux_min))) -\ np.log10(flux_min)*((r_max - r_min)/(np.log10(flux_max) - np.log10(flux_min))) #plt.figure(facecolor='white') plt.title('Disk Radius as a function of $\Phi_{src}$') plt.plot(np.log10(flux), RADdeg, 'o-', linewidth=1, ms=3, alpha=0.4, label="%.1f GeV (%s)"%(en/1000., lab[i])) plt.xlabel('Flux [photon/cm$^{2}$/s]') plt.ylabel('Mask disk Radius [$^{\circ}$]') #plt.yscale('log') #plt.xscale('log') #plt.ylim(1.5, 6) plt.grid(which='both', linestyle='--', linewidth=1) plt.legend(fontsize=11, loc=2) """ nside = 512 SRC_CATALOG_FILE = os.path.join(GRATOOLS_CONFIG,'catalogs/gll_psc_v16.fit') bad_pix = mask_src_weighted(SRC_CATALOG_FILE, SRC_CATALOG_FILE,8317, nside) bad_pix += mask_bat_gp(2.5e-7, nside) npix = hp.nside2npix(nside) mask = np.ones(npix) for bpix in np.unique(bad_pix): mask[bpix] = 0 fsky = 1-(len(np.unique(bad_pix))/float(npix)) #title = 'Energy = 100 GeV, F$_{sky}$ = %.3f'%(fsky) title = 'F$_{sky}$ = %.3f'%(fsky) hp.mollview(mask, title=title, coord='G', cmap='bone') hp.graticule(color='silver') plt.show()
def mkCl(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Calculating PSF with gtpsf...') dict_gtpsf = data.DICT_GTPSF logger.info('Calculating Wbeam Function...') out_wb_label = data.OUT_W_LABEL mask_label = data.MASK_LABEL out_wb_txt = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt' % out_wb_label) if not os.path.exists(out_wb_txt): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf) from GRATools.utils.gWindowFunc import get_psf psf_file = data.PSF_FILE psf = get_psf(psf_file) _l = np.arange(0, 1000) from GRATools.utils.gWindowFunc import build_wbeam wb = build_wbeam(psf, _l, out_wb_txt) else: from GRATools.utils.gWindowFunc import get_wbeam wb = get_wbeam(out_wb_txt) save_current_figure('Wbeam_%s.png' % out_wb_label, clear=True) logger.info('Starting Cl analysis...') in_label = data.IN_LABEL in_label = in_label + '_' + mask_label out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL mask_file = data.MASK_FILE mask = hp.read_map(mask_file) cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_cls.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...' % (emin, emax)) gamma = data.WEIGHT_SPEC_INDEX Im = (1 / (1 - gamma)) * (emax**(1 - gamma) - emin**(1 - gamma)) / (emax - emin) eweightedmean = np.power(1 / Im, 1 / gamma) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n' % (emin, emax, eweightedmean)) l_max = 1000 _l = np.arange(l_max) wb_en = wb.hslice(eweightedmean)(_l) flux_map_name = in_label + '_flux_%i-%i.fits' % (emin, emax) flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name)) flux_map_masked = hp.ma(flux_map) flux_map_masked.mask = np.logical_not(mask) fsky = 1.-(len(np.where(flux_map_masked.filled() == hp.UNSEEN)[0])/\ float(len(flux_map))) if kwargs['show'] == True: hp.mollview(flux_map_masked.filled(), title='f$_{sky}$ = %.3f' % fsky, min=1e-7, max=1e-4, norm='log') plt.show() print 'fsky = ', fsky nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] _cl = hp.sphtfunc.anafast(flux_map_masked.filled(), lmax=l_max-1, \ iter=5) _cl_fit = hp.sphtfunc.anafast(flux_map_masked.filled(), iter=4) cn_fit = np.average(_cl_fit[-500:-100] / fsky) / len( _cl_fit[-500:-100]) print 'cn fit = ', cn_fit print 'cn poisson = ', _cn[i] cn = _cn[i] wl = wb_en * wpix _cl = (_cl / fsky - cn) / (wl**2) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) _cl_err = np.sqrt(2. / ((2 * _l + 1) * fsky)) * (_cl + (cn / wl**2)) cl_txt.write('Cl_ERR\t%s\n\n'%str(list(_cl_err)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_cls.txt' \ %(out_label, binning_label))))
def fit_fore_src_poisson(fore_map, data_map, srctempl_map, mask_map=None, n1_guess=1., c_guess=1, n2_guess=1., exp=None, smooth=False, show=False): """Performs the poisonian fit, recursively computing the log likelihood (using poisson_likelihood) for a grid of values of fit parameters around the guess. Returns the values of parameters which minimize the log likelihood, togather to the 1-sigma error n1_guess : float initial guess for normalization parameter n2_guess : float initial guess for normalization parameter c_guess : float initial guess for constant parameter fore_map : numpy array helapix map of foreground model data_map : numpy array helapix map of data. It could be either a count map or a flux map. If a counts map is given, an exposure map should be given too. See next parameter. exp : numpy array or None helapix map of the exposure. Should be given if the data map is in counts (beacause foreground map is in flux units by default and it needs to be turned to counts to be fitted). While, If data map is in flux units, do not declare this parameter, which is None by default. smooth : bool not implemented yet... show : bool if true it shows some usefull plot to check if the fit is functioning """ #show=True logger.info('Performing poissonian fit...') norm1_guess = n1_guess norm2_guess = n2_guess igrb_guess = c_guess nside_out = 64 mask = 0. if mask_map is None: logger.info('fit outside default mask: 30deg gp, 2 deg srcs.') mask_f = os.path.join(GRATOOLS_CONFIG, 'fits/Mask64_src2_gp30.fits') mask = hp.read_map(mask_f) else: logger.info('fit outside mask given in config file.') mask = mask_map logger.info('down grade...') fore_repix = np.array(hp.ud_grade(fore_map, nside_out=nside_out)) data_repix = np.array(hp.ud_grade(data_map, nside_out=nside_out, power=-2)) srct_repix = np.array(hp.ud_grade(srctempl_map, nside_out=nside_out)) mask_repix = np.array(hp.ud_grade(mask, nside_out=nside_out,power=-2)) mask_repix[np.where(mask_repix!=np.amax(mask_repix))[0]] = 0 mask_repix[np.where(mask_repix==np.amax(mask_repix))[0]] = 1 _unmask = np.where(mask_repix > 1e-30)[0] logger.info('initial guesses: n1=%.1e, n2=%.1e, c=%.1e'%(norm1_guess, norm2_guess, igrb_guess)) norm1_list = np.linspace(norm1_guess*0.3, norm1_guess*2, 21) norm2_list = np.linspace(norm2_guess*0.1, norm2_guess*10, 21) print norm2_list print norm1_list igrb_list = np.linspace(igrb_guess*0.01, igrb_guess*10., 101) logger.info('Minimization likelihood run1...') lh_list = [] combinations = list(product(norm1_list, norm2_list, igrb_list)) if exp is not None: exposure = exp exposure = np.array(hp.ud_grade(exposure, nside_out=nside_out)) areapix = 4*np.pi/(len(data_repix)) for i,j,k in combinations: lh = poisson_likelihood_2(i, j, k, fore_repix[_unmask], data_repix[_unmask], srct_repix[_unmask], exp=exposure[_unmask], sr=areapix) lh_list.append(lh) else: for i,j,k in combinations: lh = poisson_likelihood_2(i, j, k, fore_repix[_unmask], data_repix[_unmask], srct_repix[_unmask]) lh_list.append(lh) lh_min = np.argmin(np.array(lh_list)) (norm1_min, norm2_min, igrb_min) = combinations[lh_min] logger.info('Run1 results: n1=%.3f n2=%.2e c=%.2e'%(norm1_min, norm2_min, igrb_min)) norm1_list = np.linspace(norm1_min*0.9, norm1_min*1.7, 21) norm2_list = np.linspace(norm2_min*0.5, norm2_min*5, 21) print norm2_list print norm1_list igrb_list = np.linspace(igrb_min*0.5, igrb_min*1.5, 101) logger.info('Minimization likelihood run2...') lh_list = [] combinations = np.array(list(product(norm1_list, norm2_list, igrb_list))) if exp is not None: exposure = exp exposure = np.array(hp.ud_grade(exposure, nside_out=nside_out)) areapix = 4*np.pi/(len(data_repix)) for i,j,k in product(norm1_list, norm2_list, igrb_list): lh = poisson_likelihood_2(i, j, k, fore_repix[_unmask], data_repix[_unmask], srct_repix[_unmask], exp=exposure[_unmask], sr=areapix) lh_list.append(lh) else: for i,j,k in product(norm1_list, norm2_list, igrb_list): lh = poisson_likelihood_2(i, j, k, fore_repix[_unmask], data_repix[_unmask],srct_repix[_unmask]) lh_list.append(lh) lh_list = np.array(lh_list) lh_min = np.argmin(lh_list) (norm1_min, norm2_min, igrb_min) = combinations[lh_min] logger.info('Run2 results: n1=%.3f n2=%.2e c=%.2e'%(norm1_min, norm2_min, igrb_min)) lh_delta = np.array(lh_list)[lh_min]+2.3 index = np.where(np.array(lh_list) < lh_delta)[0] _norm1 = np.array([x[0] for x in combinations[index]]) logger.info('Norm1 err: %.4f - %.4f'%(_norm1[0], _norm1[-1])) n1_err = (_norm1[0], _norm1[-1]) _norm2 = np.array([x[1] for x in combinations[index]]) logger.info('Norm2 err: %.2e - %.2e'%(_norm2[0], _norm2[-1])) n2_err = (_norm2[0], _norm2[-1]) _igrb = np.array([x[2] for x in combinations[index]]) logger.info('Igrb err: %.2e - %.2e'%(np.amin(_igrb), np.amax(_igrb))) igrb_err = (np.amin(_igrb), np.amax(_igrb)) if show == True: n1 = np.array([x[0] for x in combinations]) n2 = np.array([x[1] for x in combinations]) c = np.array([x[2] for x in combinations]) plt.figure(facecolor='white') plt.plot(n2, lh_list, 'o', color='coral', alpha=0.3) plt.plot(norm2_min, lh_list[lh_min] , 'r*') plt.plot([_norm2[0], _norm2[-1]], [lh_delta, lh_delta], 'r-') plt.xlabel('Normalization') plt.ylabel('-Log(Likelihood)') plt.title('norm src') plt.figure(facecolor='white') plt.plot(n1, lh_list, 'o', color='coral', alpha=0.3) plt.plot(norm1_min, lh_list[lh_min] , 'r*') plt.plot([_norm1[0], _norm1[-1]], [lh_delta, lh_delta], 'r-') plt.xlabel('Normalization') plt.ylabel('-Log(Likelihood)') plt.title('norm fore') plt.figure(facecolor='white') plt.plot(c, lh_list, 'o', color='coral', alpha=0.3) plt.plot(igrb_min, lh_list[lh_min] , 'r*') plt.xlabel('Normalization') plt.ylabel('-Log(Likelihood)') plt.show() return norm1_min, norm2_min, igrb_min, n1_err, n2_err, igrb_err
def mkCross(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Calculating PSF with gtpsf...') dict_gtpsf = data.DICT_GTPSF logger.info('Calculating Wbeam Function...') out_wb_label = data.OUT_W_LABEL out_wb_txt = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt'%out_wb_label) if not os.path.exists(out_wb_txt): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf) from GRATools.utils.gWindowFunc import get_psf psf_file = data.PSF_FILE psf = get_psf(psf_file) _l = np.arange(0, 1000) from GRATools.utils.gWindowFunc import build_wbeam wb = build_wbeam(psf, _l, out_wb_txt) else: from GRATools.utils.gWindowFunc import get_wbeam wb = get_wbeam(out_wb_txt) save_current_figure('Wbeam_%s.png'%out_wb_label, clear=True) logger.info('Starting Cl analysis...') in_label1 = data.IN_LABEL1 in_label2 = data.IN_LABEL2 out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL mask_file = data.MASK_FILE mask = hp.read_map(mask_file) cl_param_file1 = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label1, binning_label)) cl_param_file2 = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label2, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file1) _emin2, _emax2, _emean2, _f2, _ferr2, _cn2, _fsky2 = get_cl_param(cl_param_file2) cross_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_cross.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...'%(emin, emax)) gamma = data.WEIGHT_SPEC_INDEX Im = (1/(1-gamma))*(emax**(1-gamma)-emin**(1-gamma))/(emax-emin) eweightedmean = np.power(1/Im, 1/gamma) cross_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, eweightedmean)) l_max= 1000 _l = np.arange(l_max) wb_en = wb.hslice(eweightedmean)(_l) flux_map_name1 = in_label1+'_flux_%i-%i.fits'%(emin, emax) flux_map_name2 = in_label2+'_flux_%i-%i.fits'%(emin, emax) flux_map1 = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name1)) flux_map2 = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name2)) flux_map_masked1 = hp.ma(flux_map1) flux_map_masked1.mask = np.logical_not(mask) flux_map_masked2 = hp.ma(flux_map2) flux_map_masked2.mask = np.logical_not(mask) fsky1 = 1.-(len(np.where(flux_map_masked1.filled() == hp.UNSEEN)[0])/\ float(len(flux_map1))) fsky2 = 1.-(len(np.where(flux_map_masked2.filled() == hp.UNSEEN)[0])/\ float(len(flux_map2))) if kwargs['show'] == True: hp.mollview(flux_map_masked1.filled(), title='f$_{sky}$ = %.3f'%fsky, min=1e-7, max=1e-4, norm='log') plt.show() print 'fsky = ', fsky1, fsky2 fsky = np.sqrt(fsky1*fsky2) nside1 = hp.npix2nside(len(flux_map1)) nside2 = hp.npix2nside(len(flux_map1)) wpix1 = hp.sphtfunc.pixwin(nside1)[:l_max] wpix2 = hp.sphtfunc.pixwin(nside2)[:l_max] _cl_cross = hp.sphtfunc.anafast(flux_map_masked1.filled(), flux_map_masked2.filled(), lmax=l_max-1, \ iter=5) wl2 = wb_en*wb_en*wpix1*wpix2 _cl_cross = (_cl_cross/fsky)/(wl2) cross_txt.write('Cl\t%s\n'%str(list(_cl_cross)).replace('[',''). \ replace(']','').replace(', ', ' ')) _cl_cross_err = np.sqrt(2./((2*_l+1)*fsky))*(_cl_cross) cross_txt.write('Cl_ERR\t%s\n\n'%str(list(_cl_cross_err)).replace('[',''). \ replace(']','').replace(', ', ' ')) cross_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_cross.txt' \ %(out_label, binning_label))))
def fit_foreground_poisson(fore_map, data_map, mask_map=None, n_guess=1., c_guess=0.1,exp=None, smooth=False, show=False): """Performs the poisonian fit, recursively computing the log likelihood (using poisson_likelihood) for a grid of values of fit parameters around the guess. Returns the values of parameters which minimize the log likelihood, togather to the 1-sigma error n_guess : float initial guess for normalization parameter c_guess : float initial guess for constant parameter fore_map : numpy array helapix map of foreground model data_map : numpy array helapix map of data. It could be either a count map or a flux map. If a counts map is given, an exposure map should be given too. See next parameter. exp : numpy array or None helapix map of the exposure. Should be given if the data map is in counts (beacause foreground map is in flux units by default and it needs to be turned to counts to be fitted). While, If data map is in flux units, do not declare this parameter, which is None by default. smooth : bool not implemented yet... show : bool if true it shows some usefull plot to check if the fit is functioning """ #show=True logger.info('Performing poissonian fit...') norm_guess = n_guess igrb_guess = c_guess nside_out = 64 mask = 0. if mask_map is None: logger.info('fit outside default mask: 30deg gp, 2 deg srcs.') mask_f = os.path.join(GRATOOLS_CONFIG, 'fits/Mask64_src2_gp30.fits') mask = hp.read_map(mask_f) else: logger.info('fit outside mask given in config file.') mask = mask_map logger.info('down grade...') fore_repix = np.array(hp.ud_grade(fore_map, nside_out=nside_out)) data_repix = np.array(hp.ud_grade(data_map, nside_out=nside_out, power=-2)) mask_repix = np.array(hp.ud_grade(mask, nside_out=nside_out, power=-2)) mask_repix[np.where(mask_repix!=np.amax(mask_repix))[0]] = 0 mask_repix[np.where(mask_repix==np.amax(mask_repix))[0]] = 1 _unmask = np.where(mask_repix > 1e-30)[0] norm_list = np.linspace(norm_guess*0.3, norm_guess*1.5, 50) igrb_list = np.linspace(igrb_guess*0.01, igrb_guess*10., 200) logger.info('Minimization likelihood run1...') lh_list = [] combinations = list(product(norm_list, igrb_list)) if exp is not None: exposure = exp exposure = np.array(hp.ud_grade(exposure, nside_out=nside_out)) areapix = 4*np.pi/(len(data_repix)) for i,j in product(norm_list, igrb_list): lh = poisson_likelihood(i, j, fore_repix[_unmask], data_repix[_unmask], exp=exposure[_unmask], sr=areapix) lh_list.append(lh) else: for i,j in product(norm_list, igrb_list): lh = poisson_likelihood(i, j, fore_repix[_unmask], data_repix[_unmask]) lh_list.append(lh) lh_min = np.argmin(np.array(lh_list)) (norm_min, igrb_min) = combinations[lh_min] logger.info('Run1 results: n=%.3f c=%.1e'%(norm_min, igrb_min)) norm_list = np.linspace(norm_min*0.7, norm_min*1.2, 51) igrb_list = np.linspace(igrb_min*0.5, igrb_min*1.5, 101) logger.info('Minimization likelihood run2...') lh_list = [] combinations = np.array(list(product(norm_list, igrb_list))) if exp is not None: exposure = exp exposure = np.array(hp.ud_grade(exposure, nside_out=nside_out)) areapix = 4*np.pi/(len(data_repix)) for i,j in product(norm_list, igrb_list): lh = poisson_likelihood(i, j, fore_repix[_unmask], data_repix[_unmask], exp=exposure[_unmask], sr=areapix) lh_list.append(lh) else: for i,j in product(norm_list, igrb_list): lh = poisson_likelihood(i, j, fore_repix[_unmask], data_repix[_unmask]) lh_list.append(lh) lh_list = np.array(lh_list) lh_min = np.argmin(lh_list) (norm_min, igrb_min) = combinations[lh_min] logger.info('Run2 results: n=%.3f c=%e'%(norm_min, igrb_min)) lh_delta = np.array(lh_list)[lh_min]+2.3 index = np.where(np.array(lh_list) < lh_delta)[0] _norm = np.array([x[0] for x in combinations[index]]) logger.info('Norm err: %.4f - %.4f'%(_norm[0], _norm[-1])) _igrb = np.array([x[1] for x in combinations[index]]) logger.info('Igrb err: %.e - %.e'%(np.amin(_igrb), np.amax(_igrb))) if show == True: n = np.array([x[0] for x in combinations]) plt.figure(facecolor='white') plt.plot(n, lh_list, 'o', color='coral', alpha=0.3) plt.plot(norm_min, lh_list[lh_min] , 'r*') plt.plot([_norm[0], _norm[-1]], [lh_delta, lh_delta], 'r-') plt.xlabel('Normalization') plt.ylabel('-Log(Likelihood)') igrb = np.array([x[1] for x in combinations]) plt.figure(facecolor='white') plt.plot(igrb, lh_list, 'o', color='coral', alpha=0.3) plt.plot(igrb_min, lh_list[lh_min] , 'r*') plt.plot([np.amin(_igrb), np.amax(_igrb)], [lh_delta, lh_delta], 'r-') plt.xlabel('Constant') plt.ylabel('-Log(Likelihood)') fig = plt.figure(facecolor='white') z = lh_list zmin = lh_list[lh_min] z.shape = (len(norm_list), len(igrb_list)) ax = fig.add_subplot(111) cax = ax.matshow(z, origin='lower', cmap='Spectral', aspect='auto') plt.xlabel('$C$ $[cm^{-2}s^{-1}sr^{-1}]$') plt.ylabel('$N$') #plt.title('$\Phi_{data}=N\cdot\Phi_{model}+C$') x_ticks = np.linspace(np.amin(igrb_list), np.amax(igrb_list), 6) formatting_function = np.vectorize(lambda f: format(f, '6.1E')) x_ticks = list(formatting_function(x_ticks)) y_ticks = list(np.around(np.linspace(np.amin(norm_list), np.amax(norm_list), 6), decimals=3)) ax.set_yticklabels(['']+y_ticks) ax.set_xticklabels(['']+x_ticks) ax.xaxis.set_ticks_position('bottom') cb = plt.colorbar(cax, format='$%.1e$') cb.set_label('-Log(Likelihood)', rotation=90) norm_min_ind = list(norm_list).index(norm_min) igrb_min_ind = list(igrb_list).index(igrb_min) _norm_ind = [] _igrb_ind = [] for i in range(0, len(index)): _norm_ind.append(list(norm_list).index(_norm[i])) _igrb_ind.append(list(igrb_list).index(_igrb[i])) _norm_ind = np.array(_norm_ind) _igrb_ind = np.array(_igrb_ind) plt.contourf(z, [zmin, zmin+2.3, zmin+4.61, zmin+5.99], colors='w', origin='lower', alpha=0.3) plt.scatter(igrb_min_ind, norm_min_ind, s=45, c='w', marker='+') plt.show() return norm_min, igrb_min, _norm[0], _norm[-1], np.amin(_igrb), \ np.amax(_igrb)
def mkCross(**kwargs): """ """ get_var_from_file(kwargs['config']) lmax = data.LMAX _l = np.arange(0, lmax) gamma = data.WEIGHT_SPEC_INDEX out_label = data.OUT_LABEL logger.info('Calculating energy spectrum...') from GRATools.utils.gWindowFunc import get_powerlaw_spline energy_spec = get_powerlaw_spline(gamma) logger.info('Calculating PSF with gtpsf 1...') dict_gtpsf1 = data.DICT_GTPSF1 logger.info('Calculating Wbeam Function1...') out_wb_label1 = data.OUT_W_LABEL1 out_wb_txt1 = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt'%out_wb_label1) in_label1 = data.IN_LABEL1 if not os.path.exists(out_wb_txt1): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf1) from GRATools.utils.gWindowFunc import get_psf psf_file1 = data.PSF_FILE1 psf1 = get_psf(psf_file1) from GRATools.utils.gWindowFunc import build_wbeam wb1 = build_wbeam(psf1, _l, out_wb_txt1) else: pass logger.info('Calculating PSF with gtpsf 2...') dict_gtpsf2 = data.DICT_GTPSF2 logger.info('Calculating Wbeam Function2...') out_wb_label2 = data.OUT_W_LABEL2 out_wb_txt2 = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt'%out_wb_label2) in_label2 = data.IN_LABEL2 if not os.path.exists(out_wb_txt2): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf2) from GRATools.utils.gWindowFunc import get_psf psf_file2 = data.PSF_FILE2 psf2 = get_psf(psf_file2) from GRATools.utils.gWindowFunc import build_wbeam wb2 = build_wbeam(psf2, _l, out_wb_txt2) else: pass logger.info('Starting Cross analysis...') mask_label1 = data.MASK_LABEL1 mask_label2 = data.MASK_LABEL2 in_label1 = in_label1 + '_' + mask_label1 in_label2 = in_label2 + '_' + mask_label2 binning_label1 = data.BINNING_LABEL1 binning_label2 = data.BINNING_LABEL2 mask_file1 = data.MASK_FILE1 mask_file2 = data.MASK_FILE2 cl_param_file1 = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label1, binning_label1)) cl_param_file2 = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label2, binning_label2)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f1, _ferr1, _cn1, _fsky1 = \ get_cl_param(cl_param_file1) _emin, _emax, _emean, _f2, _ferr2, _cn2, _fsky2 = \ get_cl_param(cl_param_file2) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_polspicecross.txt' \ %(out_label, binning_label1)), 'w') from GRATools.utils.gWindowFunc import get_integral_wbeam for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...'%(emin, emax)) mask_f1 = mask_file1 mask_f2 = mask_file2 if type(mask_file1) == list: mask_f1 = mask_file1[i] if type(mask_file2) == list: mask_f2 = mask_file2[i] mask1 = hp.read_map(mask_f1) mask2 = hp.read_map(mask_f2) wb1_en = get_integral_wbeam(out_wb_txt1, energy_spec, emin, emax) wb2_en = get_integral_wbeam(out_wb_txt2, energy_spec, emin, emax) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, _emean[i])) l_max = lmax _l = np.arange(l_max) wb1_en = wb1_en(_l) wb2_en = wb2_en(_l) flux_map_name1 = in_label1+'_flux_%i-%i.fits'%(emin, emax) flux_map_name2 = in_label2+'_flux_%i-%i.fits'%(emin, emax) flux_map_f1 = os.path.join(GRATOOLS_OUT_FLUX, flux_map_name1) flux_map_f2 = os.path.join(GRATOOLS_OUT_FLUX, flux_map_name2) flux_map_f_mdclean1 = remove_monopole_dipole(flux_map_f1) flux_map_f_mdclean2 = remove_monopole_dipole(flux_map_f2) flux_map1 = hp.read_map(flux_map_f_mdclean1) flux_map2 = hp.read_map(flux_map_f_mdclean2) fsky1 = _fsky1[i] fsky2 = _fsky2[i] cn = 0. if kwargs['show'] == True: hp.mollview(flux_map1*mask1, title='f$_{sky}$ = %.3f'%fsky, min=1e-7, max=1e-4, norm='log') hp.mollview(flux_map2*mask2, title='f$_{sky}$ = %.3f'%fsky, min=1e-7, max=1e-4, norm='log') plt.show() logger.info('fsky1 = %.3f'%fsky1) logger.info('fsky2 = %.3f'%fsky2) nside1 = hp.npix2nside(len(flux_map1)) nside2 = hp.npix2nside(len(flux_map2)) wpix1 = hp.sphtfunc.pixwin(nside1)[:l_max] wpix2 = hp.sphtfunc.pixwin(nside2)[:l_max] out_name = '%s_%i-%i' %(out_label, emin, emax) out_folder = os.path.join(GRATOOLS_OUT, 'output_pol') logger.info('cn poisson = %e'%cn) wl = np.sqrt(wb1_en*wpix1*wb2_en*wpix2) if not os.path.exists(out_folder): os.makedirs(out_folder) pol_dict = data.POLCEPICE_DICT for key in pol_dict: if key == 'clfile': pol_dict[key] = os.path.join(out_folder,'%s_cl.txt'%out_name) if key == 'cl_outmap_file': pol_dict[key] = os.path.join(out_folder,'%s_clraw.txt'%out_name) if key == 'covfileout': pol_dict[key] = os.path.join(out_folder,'%s_cov.fits'%out_name) if key == 'mapfile': pol_dict[key] = flux_map_f_mdclean1 if key == 'maskfile': pol_dict[key] = mask_f1 if key == 'mapfile2': pol_dict[key] = flux_map_f_mdclean2 if key == 'maskfile2': pol_dict[key] = mask_f2 config_file_name = 'pol_%s'%(out_name) if os.path.exists(os.path.join(out_folder,'%s_cl.txt'%out_name)) and \ os.path.exists(os.path.join(out_folder,'%s_cov.fits'%out_name)): logger.info('ATT: Retriving power spectrum...') _l, _cl, _clerr = pol_cl_parse(os.path.join(out_folder, '%s_cl.txt'%out_name), os.path.join(out_folder, '%s_cov.fits'%out_name), raw_corr=(cn, wl), rebin=True) logger.info('... and covariance matrix.') _cov = pol_cov_parse(os.path.join(out_folder, '%s_cov.fits'%out_name), wl_array=wl, rebin=True, show=True) else: _l, _cl, _clerr, _cov = pol_cl_calculation(pol_dict, config_file_name, raw_corr=(cn, wl), rebin=True,show=True) cl_txt.write('multipole\t%s\n'%str(list(_l)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Cl_ERR\t%s\n\n'%str(list(_clerr)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT,'%s_%s_polspicecls.txt' %(out_label, binning_label1))))
def mkCl(**kwargs): """ """ get_var_from_file(kwargs['config']) lmax = data.LMAX _l = np.arange(0, lmax) gamma = data.WEIGHT_SPEC_INDEX out_label = data.OUT_LABEL logger.info('Calculating energy spectrum...') from GRATools.utils.gWindowFunc import get_powerlaw_spline energy_spec = get_powerlaw_spline(gamma) logger.info('Calculating PSF with gtpsf...') dict_gtpsf = data.DICT_GTPSF logger.info('Calculating Wbeam Function...') out_wb_label = data.OUT_W_LABEL out_wb_txt = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt'%out_wb_label) in_label = data.IN_LABEL if not os.path.exists(out_wb_txt): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf) from GRATools.utils.gWindowFunc import get_psf psf_file = data.PSF_FILE psf = get_psf(psf_file) #_l = np.arange(0, 1500) from GRATools.utils.gWindowFunc import build_wbeam wb = build_wbeam(psf, _l, out_wb_txt) else: pass logger.info('Starting Cl analysis...') mask_label = data.MASK_LABEL in_label = in_label + '_' + mask_label binning_label = data.BINNING_LABEL mask_file = data.MASK_FILE cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_polspicecls.txt' \ %(out_label, binning_label)), 'w') monopoles, monopoles_err = [], [] dipol_vectors = [] from GRATools.utils.gWindowFunc import get_integral_wbeam for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...'%(emin, emax)) mask_f = mask_file if type(mask_file) == list: mask_f = mask_file[i] #mask = hp.read_map(mask_f) wb_en = get_integral_wbeam(out_wb_txt, energy_spec, emin, emax) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, _emean[i])) l_max= lmax _l = np.arange(l_max) wb_en = wb_en(_l) # monopole and dipole cleaning from MASKED map, bad value = UNSEEN flux_map_name = in_label+'_fluxmasked_%i-%i.fits'%(emin, emax) flux_map_f = os.path.join(GRATOOLS_OUT_FLUX, flux_map_name) flux_map_f_mdclean, mono, dipo = remove_monopole_dipole(flux_map_f) monopoles.append(mono) mono_err = np.sqrt(2/_fsky[i])*(mono+_cn[i]) monopoles_err.append(mono_err) dipol_vectors.append(dipo) logger.info('Monopole_term = %e +- %e'%(mono, mono_err)) logger.info('Dipole vector = %s' %(str(dipo))) # Up to l=n cleaning from MASKED map (correction for fsky discarded) if kwargs['multiclean'] is not None: flux_map_f_multiclean = remove_multipole(flux_map_f_mdclean, lmax=kwargs['multiclean']) flux_map = hp.read_map(flux_map_f_multiclean) else: flux_map = hp.read_map(flux_map_f_mdclean) fsky = _fsky[i] cn = _cn[i] if kwargs['show'] == True: hp.mollview(flux_map, title='f$_{sky}$ = %.3f'%fsky) plt.show() logger.info('fsky = '%fsky) nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] out_name = '%s_%i-%i' %(out_label, emin, emax) out_folder = os.path.join(GRATOOLS_OUT, 'output_pol') logger.info('cn poisson = %e'%cn) wl = wb_en*wpix if not os.path.exists(out_folder): os.makedirs(out_folder) pol_dict = data.POLCEPICE_DICT for key in pol_dict: if key == 'clfile': pol_dict[key] = os.path.join(out_folder,'%s_cl.txt'%out_name) if key == 'cl_outmap_file': pol_dict[key] = os.path.join(out_folder,'%s_clraw.txt'%out_name) if key == 'covfileout': pol_dict[key] = os.path.join(out_folder,'%s_cov.fits'%out_name) if key == 'mapfile': pol_dict[key] = flux_map_f_mdclean if key == 'maskfile': pol_dict[key] = mask_f config_file_name = 'pol_%s'%(out_name) if os.path.exists(os.path.join(out_folder,'%s_cl.txt'%out_name)) and \ os.path.exists(os.path.join(out_folder,'%s_cov.fits'%out_name)): logger.info('ATT: Retriving power spectrum...') _l, _cl, _clerr = pol_cl_parse(os.path.join(out_folder, '%s_cl.txt'%out_name), os.path.join(out_folder, '%s_cov.fits'%out_name), raw_corr=(cn, wl), rebin=True) logger.info('... and covariance matrix.') _cov = pol_cov_parse(os.path.join(out_folder, '%s_cov.fits'%out_name), wl_array=wl, rebin=True, show=True) else: _l, _cl, _clerr, _cov = pol_cl_calculation(pol_dict, config_file_name, raw_corr=(cn, wl), rebin=True,show=True) cl_txt.write('multipole\t%s\n'%str(list(_l)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Cl_ERR\t%s\n\n'%str(list(_clerr)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Monopoles\t%s\n\n'%str(monopoles).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.write('Monopoles_ERR\t%s\n\n'%str(monopoles_err).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info('Created %s'%(os.path.join(GRATOOLS_OUT,'%s_%s_polspicecls.txt' %(out_label, binning_label))))
def mkCl(**kwargs): """ """ get_var_from_file(kwargs['config']) logger.info('Calculating PSF with gtpsf...') dict_gtpsf = data.DICT_GTPSF logger.info('Calculating Wbeam Function...') out_wb_label = data.OUT_W_LABEL out_wb_txt = os.path.join(GRATOOLS_OUT, 'Wbeam_%s.txt' % out_wb_label) if not os.path.exists(out_wb_txt): from GRATools.utils.ScienceTools_ import gtpsf gtpsf(dict_gtpsf) from GRATools.utils.gWindowFunc import get_psf psf_file = data.PSF_FILE psf = get_psf(psf_file) _l = np.arange(0, 1000) from GRATools.utils.gWindowFunc import build_wbeam wb = build_wbeam(psf, _l, out_wb_txt) else: from GRATools.utils.gWindowFunc import get_wbeam wb = get_wbeam(out_wb_txt) save_current_figure('Wbeam_%s.png' % out_wb_label, clear=True) logger.info('Starting Cl analysis...') in_label = data.IN_LABEL mask_label = data.MASK_LABEL in_label = in_label + '_' + mask_label out_label = data.OUT_LABEL binning_label = data.BINNING_LABEL mask_file = data.MASK_FILE cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \ %(in_label, binning_label)) from GRATools.utils.gFTools import get_cl_param _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file) cl_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_polspicecls.txt' \ %(out_label, binning_label)), 'w') for i, (emin, emax) in enumerate(zip(_emin, _emax)): logger.info('Considering bin %.2f - %.2f ...' % (emin, emax)) mask_f = mask_file if type(mask_file) == list: mask_f = mask_file[i] gamma = data.WEIGHT_SPEC_INDEX Im = (1 / (1 - gamma)) * (emax**(1 - gamma) - emin**(1 - gamma)) / (emax - emin) eweightedmean = np.power(1 / Im, 1 / gamma) cl_txt.write('ENERGY\t %.2f %.2f %.2f\n' % (emin, emax, eweightedmean)) l_max = 1000 _l = np.arange(l_max) wb_en = wb.hslice(eweightedmean)(_l) flux_map_name = in_label + '_flux_%i-%i.fits' % (emin, emax) flux_map_f = os.path.join(GRATOOLS_OUT_FLUX, flux_map_name) flux_map = hp.read_map(flux_map_f) fsky = _fsky[i] if kwargs['show'] == True: hp.mollview(flux_map_masked.filled(), title='f$_{sky}$ = %.3f' % fsky, min=1e-7, max=1e-4, norm='log') plt.show() logger.info('fsky = ' % fsky) nside = hp.npix2nside(len(flux_map)) wpix = hp.sphtfunc.pixwin(nside)[:l_max] out_name = '%s_%i-%i' % (out_label, emin, emax) out_folder = os.path.join(GRATOOLS_OUT, 'output_pol') if not os.path.exists(out_folder): os.makedirs(out_folder) pol_dict = data.POLCEPICE_DICT for key in pol_dict: if key == 'clfile': pol_dict[key] = os.path.join(out_folder, '%s_cl.txt' % out_name) if key == 'cl_outmap_file': pol_dict[key] = os.path.join(out_folder, '%s_clraw.txt' % out_name) if key == 'covfileout': pol_dict[key] = os.path.join(out_folder, '%s_cov.fits' % out_name) if key == 'mapfile': pol_dict[key] = flux_map_f if key == 'maskfile': pol_dict[key] = mask_f config_file_name = 'pol_%s' % (out_name) _l, _cl, _cl_err = pol_cl_calculation(pol_dict, config_file_name) logger.info('cn poisson = %e' % _cn[i]) cn = _cn[i] wl = wb_en * wpix _cl = (_cl[:l_max] - cn) / (wl**2) _cl_err = _cl_err[:l_max] / (wl**2) cl_txt.write('Cl\t%s\n'%str(list(_cl)).replace('[',''). \ replace(']','').replace(', ', ' ')) #_cl_err = np.sqrt(2./((2*_l+1)*fsky))*(_cl+(cn/wl**2)) cl_txt.write('Cl_ERR\t%s\n\n'%str(list(_cl_err)).replace('[',''). \ replace(']','').replace(', ', ' ')) cl_txt.close() logger.info( 'Created %s' % (os.path.join(GRATOOLS_OUT, '%s_%s_polspicecls.txt' % (out_label, binning_label))))
def maps_view(**kwargs): """Viewer interface for healpix maps """ input_file = kwargs['infile'] healpix_maps = hp.read_map(input_file, field=kwargs['field']) if kwargs['smoothing'] is not None: healpix_maps = hp.sphtfunc.smoothing(healpix_maps, fwhm=np.radians(kwargs['smoothing'])) if not os.path.exists(input_file): abort("Map %s not found!"%input_file) t = os.path.basename(input_file) nside_out = kwargs['udgrade'] logger.info('Returning a map with NSIDE=%i'%nside_out) if kwargs['field'] == 0: if kwargs['counts'] == True: if kwargs['applymask'] is not None: mask = hp.read_map(kwargs['applymask']) healpix_maps = healpix_maps*mask healpix_maps = hp.pixelfunc.ud_grade(healpix_maps, nside_out, pess=True, power=-2) else: if kwargs['applymask'] is not None: mask = hp.read_map(kwargs['applymask']) healpix_maps = healpix_maps*mask healpix_maps = hp.pixelfunc.ud_grade(healpix_maps, nside_out, pess=True) healpix_maps[np.where(healpix_maps==0)[0]] = hp.UNSEEN if kwargs['zscale'] == 'optimized': logger.info('Optimizing...') hp.mollview(healpix_maps, title=t.replace('.fits',''), \ coord='G', min=-5e-7, max=1e-7, cmap=cool_cmap) plt.show() elif kwargs['zscale'] == 'log': logger.info('Optimizinglog z scale...') hp.mollview(healpix_maps, title=t.replace('.fits',''), \ coord='G', norm='log', cmap=cool_cmap) plt.show() elif kwargs['zscale'] == 'hist': logger.info('Optimizinglog z scale...') hp.mollview(healpix_maps, title=t.replace('.fits',''), \ coord='G', norm='hist', cmap=cool_cmap) plt.show() else: hp.mollview(healpix_maps, title=t.replace('.fits',''), \ coord='G', cmap=cool_cmap) plt.show() else: for i, maps in enumerate(healpix_maps): healpix_maps = hp.pixelfunc.ud_grade(healpix_maps, nside_out, \ pess=True) if kwargs['zscale'] == 'optimized': logger.info('Optimizing...') hp.mollview(healpix_maps, title=t.replace('.fits',''), \ coord='G', min=-5e-7, max=1e-7, cmap=cool_cmap) plt.show() elif kwargs['zscale'] == 'log': logger.info('Optimizinglog z scale...') hp.mollview(healpix_maps, title=t.replace('.fits',''), \ coord='G', norm='log', cmap=cool_cmap) plt.show() elif kwargs['zscale'] == 'hist': logger.info('Optimizinglog z scale...') hp.mollview(healpix_maps, title=t.replace('.fits',''), \ coord='G', norm='hist', cmap=cool_cmap) plt.show() else: hp.mollview(healpix_maps, title=t.replace('.fits',''), \ coord='G', cmap=cool_cmap) plt.show()