Exemple #1
0
def projmap_median(df, col, minmax=None, **kwargs):
    rdd = df.select("ipix", col).na.drop().rdd.map(lambda r: (r[0], r[1]))
    map_rdd = rdd.groupByKey().mapValues(tuple).mapValues(np.median)
    #collect renvoie un eliste de tuples
    #back to pandas
    map_p = map_rdd.map(lambda r: (r[0], float(r[1]))).toDF().toPandas()
    val = np.array(map_p._2.values)
    mu = val.mean()
    sig = val.std()
    if minmax == None:
        minmax = (mu - 2 * sig, mu + 2 * sig)

    print("N={} \nmean={} \nsigma={}\nmin={}\nmax={}".format(
        val.size, mu, sig, min(val), max(val)))
    skyMap = np.full(hp.nside2npix(nside), hp.UNSEEN)
    skyMap[map_p._1.values] = val
    hp.gnomview(skyMap,
                nest=nest,
                reso=reso,
                min=minmax[0],
                max=minmax[1],
                title="median(" + col + ")",
                **kwargs)
    plt.show()
    return skyMap
Exemple #2
0
def densitymap(df, minmax=None):
    df_map = df.select("ipix").groupBy("ipix").count()
    df_map = df_map.withColumn("density",
                               df_map['count'] / pixarea).drop("count")

    #back to python world
    map_p = df_map.toPandas()
    A = map_p.index.size * pixarea / 3600
    print("map area={} deg2".format(A))
    #statistics per pixel
    var = 'density'
    s = df_map.describe([var])
    s.show()
    r = s.select(var).take(3)
    N = int(r[0][0])
    mu = float(r[1][0])
    sig = float(r[2][0])
    map_p = df_map.toPandas()
    #now data is reduced create the healpy map
    skyMap = np.full(hp.nside2npix(nside), hp.UNSEEN)
    skyMap[map_p['ipix'].values] = map_p[var].values

    if minmax == None:
        minmax = (np.max([0, mu - 2 * sig]), mu + 2 * sig)
    hp.gnomview(skyMap,
                rot=[55, -29.8],
                reso=reso,
                min=minmax[0],
                max=minmax[1],
                title=r"$density/arcmin^2$")
    plt.show()
def runChips(useCamera=False):
    import numpy as np
    import lsst.sims.maf.slicers as slicers
    import lsst.sims.maf.metrics as metrics
    import lsst.sims.maf.metricBundles as metricBundles
    import lsst.sims.maf.db as db
    from lsst.sims.maf.plots import PlotHandler
    import matplotlib.pylab as plt
    import healpy as hp


    print 'Camera setting = ', useCamera

    database = 'enigma_1189_sqlite.db'
    sqlWhere = 'filter = "r" and night < 800 and fieldRA < %f and fieldDec > %f and fieldDec < 0' % (np.radians(15), np.radians(-15))
    opsdb = db.OpsimDatabase(database)
    outDir = 'Camera'
    resultsDb = db.ResultsDb(outDir=outDir)

    nside=512
    tag = 'F'
    if useCamera:
        tag='T'
    metric = metrics.CountMetric('expMJD', metricName='chipgap_%s'%tag)

    slicer = slicers.HealpixSlicer(nside=nside, useCamera=useCamera)
    bundle1 = metricBundles.MetricBundle(metric,slicer,sqlWhere)

    bg = metricBundles.MetricBundleGroup({0:bundle1},opsdb, outDir=outDir, resultsDb=resultsDb)
    bg.runAll()
    hp.gnomview(bundle1.metricValues, xsize=800,ysize=800, rot=(7,-7,0), unit='Count', min=1)
    plt.savefig(outDir+'/fig'+tag+'.png')
Exemple #4
0
def display(map, cov, msg, sub):
    for i, (kind, lim) in enumerate(zip('IQU', [200, 10, 10])):
        map_ = map[..., i].copy()
        mask = cov == 0
        map_[mask] = np.nan
        hp.gnomview(map_, rot=center, reso=5, xsize=400, min=-lim, max=lim,
                    title=msg + ' ' + kind, sub=(3, 3, 3 * (sub-1) + i+1))
def plot_sky_projection_healpy_simple_zoom(Sliced_Halo_data,Output_Para):
 
    rc('font',family='serif')
    fdir  = './Output/plots/HEALPixZoom/'
 
    nside = Output_Para.nside

    Sl_n  = len(Sliced_Halo_data)
    Z_max = max(Sliced_Halo_data[Sl_n-1].Z_red[:])
    ques  = True

    while (ques):
      k = Read_Integer_Input("We have %i redshift slice  which one you want exctract the halo ? "%Sl_n)
      k = int(k) - 1
      while(k > (Sl_n-1) or k < 0 ):
        print "Invalid Number."
        k = Read_Integer_Input("Please choose an integer between 1 and %i : "%Sl_n)
        k = int(k) - 1

      pix     = zeros(12*nside**2)
      n       = len(Sliced_Halo_data[k].RA[:])
      if (n == 0):
         print "There is no halo in this piece of redshift, please choos another one ..."
         raw_input("Press enter to continue ... ")
         break 

      for i in range(n):
        j    = hp.ang2pix(nside,DtoR*(90.0-Sliced_Halo_data[k].DEC[i]),DtoR*(Sliced_Halo_data[k].RA[i]))
        pix[j] += 10.0**Sliced_Halo_data[k].lgFx[i]
             
      halo_n = Read_Integer_Input("We have %i halos in this slice which one you want to exctract ? "%n)
      halo_n = halo_n - 1
      while(halo_n > (n-1) or halo_n < 0 ):
        print "Invalid Number."
        halo_n = Read_Integer_Input("Please choose an integer between 1 and %i : "%n)
        halo_n = halo_n - 1
 
      clf()
      hp.gnomview(pix, fig = 1 ,rot=(Sliced_Halo_data[k].RA[halo_n],Sliced_Halo_data[k].DEC[halo_n],0.0) , xsize=Output_Para.xsize , reso=Output_Para.resolution , degree=Output_Para.resol_degree)
      hp.graticule()
      show()
      close()

      save_ques = Read_YN_Input("Do you want to save its picture (please enter Y, y, N, or n)? ")
      if save_ques :
        rc('text',usetex=True)
        clf()
        hp.gnomview(pix, fig=1 , rot=(Sliced_Halo_data[k].RA[halo_n],Sliced_Halo_data[k].DEC[halo_n],0.0) , xsize=Output_Para.xsize , reso=Output_Para.resolution , degree=Output_Para.resol_degree)
        hp.graticule()
        fname = 'sky_projection_HEALPix_Simple_%i_%i_%i.pdf'%(nside,(halo_n+1),(k+1))
        title(r'Redshift is between %0.2f and %0.2f'%(Sliced_Halo_data[k].z_min,Sliced_Halo_data[k].z_max),fontsize = 20)
        print 'Saving plot', fname
#        savefig(fdir+fname,bbox_inches='tight')
        savefig(fdir+fname)
        rc('text',usetex=False)
        close()

      ques = False

    return 0
Exemple #6
0
def projmap_stddev(df, col, minmax=None, dohist=True, **kwargs):
    df_map = df.select(col,
                       "ipix").na.drop().groupBy("ipix").agg(F.stddev(col))
    #statistics per pixel
    var = df_map.columns[-1]
    s = df_map.describe([var])
    s.show()
    r = s.select(var).take(3)
    N = int(r[0][0])
    mu = float(r[1][0])
    sig = float(r[2][0])
    map_p = df_map.toPandas()

    #now data is reduced create the healpy map
    skyMap = np.full(hp.nside2npix(nside), hp.UNSEEN)
    skyMap[map_p['ipix'].values] = map_p[var].values

    if minmax == None:
        minmax = (mu - 2 * sig, mu + 2 * sig)

    if dohist:
        plt.hist(map_p[var].values, bins=80, range=minmax)
        plt.xlabel(var)

    hp.gnomview(skyMap,
                nest=nest,
                reso=reso,
                min=minmax[0],
                max=minmax[1],
                title=var,
                **kwargs)
    plt.show()
    return skyMap
def compute_hitmaps():
    x0 += 1.
    hitmap = reorganize_map(A * x0, hp_pixs, npix, nside, pol,
                            'data/hitmap_ra23_1day.fits')
    zeros = np.where(hitmap[0] == 0.)[0]

    hitmap[0][zeros] = hp.UNSEEN
    hp.gnomview(hitmap[0],
                rot=[-13.45, -32.09],
                xsize=600,
                title='hits',
                min=0)
    x0 = np.array([0, 0, 1] * npix)
    hitmap = reorganize_map(A * x0, hp_pixs, npix, nside, pol,
                            'data/hitmap_ra23_1day.fits')

    zeros = np.where(hitmap[0] == 0.)[0]
    hitmap[0][zeros] = hp.UNSEEN
    hitmap[1][zeros] = hp.UNSEEN
    hitmap[2][zeros] = hp.UNSEEN

    #hp.gnomview(hitmap[0],rot=[-13.45,-32.09],xsize=600,title='hits',min=0,sub=131)
    #hp.gnomview(hitmap[1],rot=[-13.45,-32.09],xsize=600,title='hits',sub=132)
    hp.gnomview(hitmap[2],
                rot=[-13.45, -32.09],
                xsize=600,
                title='hits',
                sub=133)
Exemple #8
0
 def __call__(self, *pars):
     idetector = self.idetector
     angspeed = pars[0]
     deltaaz = pars[1]
     nsweepsel = int(pars[2])
     decrange = pars[3]
     decspeed = pars[4]
     angspeedpsi = pars[5]
     maxpsi = pars[6]
     cov = get_coverage_onedet(idetector=idetector, angspeed=angspeed,
                               delta_az=deltaaz, angspeed_psi=angspeedpsi,
                               maxpsi=maxpsi, nsweeps_el=nsweepsel,
                               duration=self.duration, ts=self.ts,
                               decrange=decrange, decspeed=decspeed, recenter=self.recenter)
     cov = cov / np.max(cov)
     eta = np.sum(cov) / np.sum(cov**2)
     self.cov = cov
     if self.costdef is 'stddev':
         cost = np.std(cov[cov != 0])
     elif self.costdef is 'eta':
         cost = eta
     elif self.costdef is 'omega':
         cost = np.sum(cov)
         
     if self.doplot:
         clf()
         hp.gnomview(cov,rot=[racenter,deccenter], reso=5, xsize=400, min=0, max=np.max(cov),
                     title='Coverage: '+self.costdef+'='+str(cost),sub=(1,1,1),coord=['G','C'])
         draw()
     print (angspeed, nsweepsel, decrange, decspeed, cost)
     return cost
def plot_zoom(scan,
              ra,
              dec,
              title,
              reso=3,
              var="pVal",
              range=[0, 6],
              cmap=None):
    if cmap is None:
        pdf_palette = sns.color_palette("Blues", 500)
        cmap = mpl.colors.ListedColormap(pdf_palette)
    hp.gnomview(scan,
                rot=(np.degrees(ra), np.degrees(dec), 0),
                cmap=cmap,
                max=max(scan),
                reso=reso,
                title=title,
                notext=True,
                cbar=False
                #unit=r""
                )

    plt.plot(4.95 / 3. * reso * np.radians([-1, 1, 1, -1, -1]),
             4.95 / 3. * reso * np.radians([1, 1, -1, -1, 1]),
             color="k",
             ls="-",
             lw=3)
    hp.graticule(verbose=False)
    plot_labels(dec, ra, reso)
Exemple #10
0
def plot_model1(qu, QU, mod, R_mod, mask, lab=''):
    plt.figure()
    plt.title('Data vs model {}'.format(lab))
    plt.scatter(qu[0, mask], QU[0, mask], c='grey', marker='^', label='data')
    plt.scatter(qu[1, mask], QU[1, mask], c='skyblue', marker='^')
    plt.scatter(qu[0, mask], mod[0, mask], c='k', marker='.', label='model')
    plt.scatter(qu[1, mask], mod[1, mask], c='b', marker='.')

    plt.legend()
    plt.xlabel(r'$q, u$')
    plt.ylabel(r'$Q, U$ [MJy/sr]')
    plt.title('Plot data vs model')
    plt.savefig('Figures/Sampling/model_vs_data_sampler.png')

    # Residuals:
    res = residual(QU, mod, mask)
    print(np.shape(res))
    hp.gnomview(res[0,:], title=r'Residuals, $\frac{Q_s - Q_m}{|Q_s|}$',\
                cmap='bwr', min=-1, max=1, rot=[104,22.2], xsize=150)
    hp.graticule()
    plt.savefig('Figures/Sampling/Q_residuals.png')
    hp.gnomview(res[1,:], title=r'Residuals, $\frac{U_s - U_m}{|U_s|}$',\
                cmap='bwr', min=-1, max=1, rot=[104, 22.2], xsize=150)
    hp.graticule()
    plt.savefig('Figures/Sampling/U_residuals.png')
Exemple #11
0
def projmap(df, col, minmax=None):
    df_map = df.select(col, "ipix").na.drop().groupBy("ipix").avg(col)
    #statistics per pixel
    var = df_map.columns[-1]
    s = df_map.describe([var])
    s.show()
    r = s.select(var).take(3)
    N = int(r[0][0])
    mu = float(r[1][0])
    sig = float(r[2][0])
    map_p = df_map.toPandas()
    #now data is reduced create the healpy map
    skyMap = np.full(hp.nside2npix(nside), hp.UNSEEN)
    skyMap[map_p['ipix'].values] = map_p[var].values

    if minmax == None:
        minmax = (np.max([0, mu - 2 * sig]), mu + 2 * sig)
    hp.gnomview(skyMap,
                rot=[55, -29.8],
                reso=reso,
                min=minmax[0],
                max=minmax[1],
                title=var)
    plt.show()
    return skyMap
Exemple #12
0
def plot_zoom_sky(Output_Para,pix,pix_bool):
    rc('font',family='serif')
    from XCat_Objects import DtoR
    if (pix_bool):
       lon = Read_Float_Input("Please enter long (-180<long<180) : ")
       lat = Read_Float_Input("Please enter lati (-90<long<90) : ")
       rot = Read_Float_Input("Please enter rot (0<rot<360) : ")
       res = Read_Float_Input("Please enter resolution (in arcmin) : ")
    else:
       print "Please, first load a HEALPix map file ."
       raw_input("Press enter to continue ... ")
       return 0


    plt.clf()
    hp.gnomview(pix, fig = 1, rot=(lon,lat,rot),reso = res)
    hp.graticule()
    plt.show()
    plt.close()

    save_ques = Read_YN_Input("Do you want to save its picture (please enter Y, y, N, or n)? ")
    if save_ques :
       rc('text',usetex=True)
       plt.clf()
       hp.gnomview(pix, fig = 1, rot=(lon,lat,rot),reso = res)
       hp.graticule()
       print 'Saving plot', fname
       plt.savefig(fdir+fname+'_zoom.pdf',bbox_inches='tight')
#       plt.savefig(fdir+fname+'.pdf')
       rc('text',usetex=False)
       plt.close()
def plot_Single_Halo_Surface_Brightness(Halo_data,Input_Para,Output_Para):

    from XCat_Objects               import DtoR, RtoD
    rc('font',family='serif')
    fdir  = './Output/plots/HEALPixSurfaceBrightness/'

    nside = Output_Para.nside

    Sl_n  = len(Halo_data)
    k = Read_Integer_Input("We have %i redshift slice  which one you want exctract the halo ? "%Sl_n)
    k = int(k) - 1
    while(k > (Sl_n-1) or k < 0 ):
      print "Invalid Number."
      k = Read_Integer_Input("Please choose an integer between 1 and %i : "%Sl_n)
      k = int(k) - 1
    n       = len(Halo_data[k].RA[:])
    if (n == 0):
       print "There is no halo in this piece of redshift, please choos another one ..."
       raw_input("Press enter to continue ... ")
       return 

    halo_n = Read_Integer_Input("We have %i halos in this slice which one you want to exctract ? "%n)
    halo_n = halo_n - 1

    from XCat_Objects               import Halo_Brightness_Surface_Sample_Object
    Halo_Sample = Halo_Brightness_Surface_Sample_Object(Halo_data[k],Input_Para,Output_Para,halo_n)

    pix     = zeros(12*nside**2)
    for i in range(len(Halo_Sample.sRA[:])):
       j    = hp.ang2pix(nside,DtoR*(90.0-Halo_Sample.sDEC[i]),DtoR*(Halo_Sample.sRA[i]))
       pix[j] += Halo_Sample.sSB[i]
    if (Output_Para.log_scale):
       pix_min = max(pix[:])/10.0**4
       pix = log10((pix+pix_min)*4.0*pi/(12.0*nside**2))
    else:
       pix = pix/(12.0*nside**2)
    plt.clf()
    hp.gnomview(pix, fig=1 ,rot=(Halo_Sample.RA,Halo_Sample.DEC,0.0), xsize=Output_Para.xsize , reso=Output_Para.resolution , degree=Output_Para.resol_degree)
    hp.graticule()
    plt.show()
    plt.close()

    save_ques = Read_YN_Input("Do you want to save its picture (please enter Y, y, N, or n)? ")
    if save_ques :
      rc('text',usetex=True)
      plt.clf()
      hp.gnomview(pix, fig=1 , rot=(Halo_Sample.RA,Halo_Sample.DEC,0.0) , xsize=Output_Para.xsize , reso=Output_Para.resolution , degree=Output_Para.resol_degree)
      hp.graticule()
      if (Output_Para.log_scale):
        fname = 'sky_projection_HEALPix_Surface_Brightness_Profile_log_%i_%i_%i.pdf'%(nside,(halo_n+1),(k+1))
      else:
        fname = 'sky_projection_HEALPix_Surface_Brightness_Profile_%i_%i_%i.pdf'%(nside,(halo_n+1),(k+1))
      plt.title(r'Surface Brightness Profile',fontsize = 20)
      print 'Saving plot', fname
#      savefig(fdir+fname,bbox_inches='tight')
      plt.savefig(fdir+fname)
      rc('text',usetex=False)
      plt.close()
Exemple #14
0
def display_maps(inmaps,
                 bigtitle=None,
                 mytitle='',
                 figsize=(16, 10),
                 nsig=3,
                 rot=None,
                 reso=15,
                 moll=False,
                 add_rms=False,
                 force_rng=None,
                 unseen=None,
                 freqs=None):
    rc('figure', figsize=figsize)
    figure()
    if bigtitle is not None:
        suptitle(bigtitle, fontsize=30, y=1.05)
    sh = np.shape(inmaps)
    if len(sh) == 2:
        maps = np.reshape(inmaps.copy(), (1, sh[0], sh[1]))
    else:
        maps = inmaps.copy()
    if unseen is not None:
        maps[:, :, unseen] = hp.UNSEEN
    nf = maps.shape[0]
    nstk = maps.shape[1]
    mypixok = (maps[0, 0, :] != hp.UNSEEN) & (maps[0, 0, :] != 0)
    for i in range(nf):
        for j in range(nstk):
            ss = np.std(maps[0, j, mypixok])
            if freqs is None:
                nuprint = i
            else:
                nuprint = freqs[i]
            thetitle = mytitle + ' {} nu={:5.1f}'.format(stk[j], nuprint)
            if force_rng is None:
                mini = -nsig * ss
                maxi = nsig * ss
            else:
                mini = -force_rng[j]
                maxi = force_rng[j]
            if add_rms:
                thetitle += ' RMS={0:5.2g}'.format(ss)
            if moll:
                hp.mollview(maps[i, j, :],
                            sub=(nf, 3, 3 * i + j + 1),
                            min=mini,
                            max=maxi,
                            title=thetitle)
            else:
                hp.gnomview(maps[i, j, :],
                            sub=(nf, 3, 3 * i + j + 1),
                            min=mini,
                            max=maxi,
                            title=thetitle,
                            rot=rot,
                            reso=reso)
    tight_layout()
 def plot(self, filter, ra=None, dec=None):
     """
     Plot zoomed zeropoint shiftmap for chosen filter centered on (ra, dec) given in degrees,
     or simply Mollweide all-sky map by default.
     """
     if ra is not None and dec is not None:
         healpy.gnomview(self.zeropoint_shiftmap.data.field(filter), rot=(ra, dec, 0))
     else:
         healpy.mollview(self.zeropoint_shiftmap.data.field(filter))
def display(x, title, min=None, max=None):
    x = x.copy()
    x[~mask] = np.nan
    hp.gnomview(x,
                rot=center,
                reso=5,
                xsize=600,
                min=min,
                max=max,
                title=title)
Exemple #17
0
 def plot(self, filter, ra=None, dec=None):
     '''
     Plot zoomed zeropoint shiftmap for chosen filter centered on (ra, dec) given in degrees,
     or simply Mollweide all-sky map by default.
     '''
     if ra is not None and dec is not None:
         healpy.gnomview(self.zeropoint_shiftmap.data.field(filter),
                         rot=(ra, dec, 0))
     else:
         healpy.mollview(self.zeropoint_shiftmap.data.field(filter))
def display(x, title, lim=200):
    x = x.copy()
    x[~mask] = np.nan
    hp.gnomview(x,
                rot=center_gal,
                reso=5,
                xsize=600,
                min=-lim,
                max=lim,
                title=title)
Exemple #19
0
def GoGetStackMixedAKARI(qso, skymap, fluxmap, mask, npix, noise=None, extras_names=None, rnd=False):
	results = {}
	results['maps'] = []

	if noise is not None:
		results['noise'] = []

	# Remember that x refers to axis=0 and y refers to axis=1 -> MAP[y,x]
	x, y = fluxmap.w.wcs_world2pix(qso.RA, qso.DEC, 0) # 0 because numpy arrays start from 0
	good_idx = (~np.isnan(x)) & (~np.isnan(y))
	x = x[good_idx]
	y = y[good_idx]

	coord = SkyCoord(ra=qso.RA[good_idx], dec=qso.DEC[good_idx], unit='deg').transform_to('galactic')
	l = coord.l.value
	b = coord.b.value

	# extras = {}
	# for name in extras_names:
	# 	extras[name] = qso[name][good_idx].values

	if not rnd:
		for i in xrange(len(x)):
			cutmask = GetCutout(mask, (x[i],y[i]), npix=25)

			if np.mean(cutmask) == 1:
				cutmask = hp.gnomview(skymap, rot=[l[i],b[i]], reso=0.5, xsize=2*npix, ysize=2*npix, return_projected_map=True)
				pl.close()
				if cutmask.all() < 100.:
					results['maps'].append(cutmask.data)
				else:
					pass
			else:
				pass
	else:
		print 'BITCHES!'
		print len(x)
		for i in xrange(len(x)):
			cutmask = GetCutout(mask, (x[i],y[i]), npix=25)

			if np.mean(cutmask) == 1:
				cutmask = hp.gnomview(skymap, rot=[l[i]+0.0055,b[i]+0.0055], reso=0.5, xsize=2*npix, ysize=2*npix, return_projected_map=True)
				pl.close()
				if cutmask.all() < 100.:
					results['maps'].append(cutmask.data)
				else:
					pass
			else:
				pass


	results['maps'] = np.asarray(results['maps'])

	return results
Exemple #20
0
def show_lines(maps, nums, min=None, max=None):
    sh = np.shape(maps)
    nl = sh[0]
    for l in range(nl):
        for i in range(4):
            hp.gnomview(maps[l, i, :],
                        reso=10,
                        min=min,
                        max=max,
                        sub=(nl, 4, l * 4 + i + 1),
                        title=nums[l, i])
    tight_layout()
Exemple #21
0
def plot_zoom(ind, LLH=False, reso=1., cmap=None, draw_contour=True, ax=None):
    """Plot skymap of an alert event
    
    Args:
        ind (int): Alert event index
        LLH (bool, default=False): plot LLH vs. scaled probs.
    """
    skymap, header = hp.read_map(skymap_files[ind], h=True, verbose=False,
        dtype=None)
    header = {name: val for name, val in header}
    nside = hp.get_nside(skymap)
    area = np.count_nonzero(skymap < 64.2) * hp.nside2pixarea(nside) * 180.**2. / (np.pi**2.)
    reso *= int(np.sqrt(area))
    reso = np.max([reso, 1.])
    original_LLH = None
    if not LLH:
        original_LLH = np.copy(skymap)
        skymap = np.exp(-1. * skymap / 2.)
        skymap = np.where(skymap > 1e-20, skymap, 0.0)
        skymap = skymap / np.sum(skymap) 
    ra = np.radians(header['RA'])
    dec = np.radians(header['DEC'])
    title = skymap_files[ind][l_ind:r_ind].replace('Run', 'Run ').replace('_', ', Event ')
    if cmap is None:
        pdf_palette = sns.color_palette("Blues", 500)
        cmap = mpl.colors.ListedColormap(pdf_palette)
    max_color = 100 if LLH else max(skymap)
    hp.gnomview(skymap, rot=(np.degrees(ra), np.degrees(dec), 0),
                    cmap=cmap,
                    max=max_color,
                    min=0,
                    reso=reso,
                    title=title,
                    notext=True,
                    cbar=False
                    #unit=r""
                    )

    plt.plot(4.95/3.*reso*np.radians([-1, 1, 1, -1, -1]), 4.95/3.*reso*np.radians([1, 1, -1, -1, 1]), color="k", ls="-", lw=3)
    hp.graticule(verbose=False)
    plot_labels(dec, ra, reso)
    original_LLH = skymap if original_LLH is None else original_LLH
    con_nside = 256 if area < 5. else 128
    if draw_contour:
        sample_points = np.array(hp.pix2ang(nside,np.arange(len(original_LLH)))).T
        msk = original_LLH < 110.
        contours = plot_contours(None, original_LLH[msk], 
            sample_points[msk], levels=[22.2, 64.2]) #, nside = con_nside)
        for contour in np.array(contours).T:
            hp.projplot(contour[0],contour[1],linewidth=1.5,c='k')
    col_label=r'$-2\Delta \mathrm{LLH}$' if LLH else "Prob."
    plot_color_bar(cmap = cmap, labels = [0, max_color], col_label = col_label)
Exemple #22
0
def make_plots(maprec, dust_coeffs):
    fig, axes = mp.subplots(len(dust_coeffs), 4, figsize=(12, 15))
    fig.tight_layout()
    for i, d in enumerate(dust_coeffs):
        for l in range(4):
            mp.axes(axes[i, l])
            hp.gnomview(maprec[i][l][:, 0],
                        rot=[-45, -60],
                        xsize=2000,
                        hold=True,
                        title="{}".format(d))

    mp.show(block=False)
Exemple #23
0
    def all_components(map_list, ifreq, rot, reso, title_list, nb_component=3):

        Stokes = ['I', 'Q', 'U']

        plt.figure()
        for istk in range(3):
            for i in range(nb_component):
                hp.gnomview(map_list[i][ifreq, istk, :],
                            rot=rot,
                            reso=reso,
                            sub=(3, nb_component,
                                 nb_component * istk + (i + 1)),
                            title=Stokes[istk] + ' - ' + title_list[i])
def display(map, cov, msg, sub):
    for i, (kind, lim) in enumerate(zip('IQU', [200, 10, 10])):
        map_ = map[..., i].copy()
        mask = cov == 0
        map_[mask] = np.nan
        hp.gnomview(map_,
                    rot=center,
                    reso=5,
                    xsize=400,
                    min=-lim,
                    max=lim,
                    title=msg + ' ' + kind,
                    sub=(3, 3, 3 * (sub - 1) + i + 1))
Exemple #25
0
def selectcenter(hpmap,
                 center,
                 delta=3,
                 nside=256,
                 nest=False,
                 threshold=3,
                 displaycenters=False,
                 doplot=False):

    #return the pixel of the central peak
    npix = 12 * nside**2
    centerarr = [
        center,
        center - delta * np.array([1, 0]),
        #center - 2 * delta * np.array([1,0]),
        center + delta * np.array([1, 0]),
        #center + 2 * delta * np.array([1,0]),
        center - delta * np.array([0, 1]),
        #center - 2 * delta * np.array([0,1]),
        center + delta * np.array([0, 1])
    ]
    #center + 2 * delta * np.array([0,1])]

    fullvec = hp.pix2vec(nside, range(0, npix), nest=nest)
    relmaxpx = np.zeros((len(centerarr), ))
    px = np.zeros((len(centerarr), ), dtype=int)

    for j, icenter in enumerate(centerarr):
        ivec = hp.ang2vec(np.deg2rad(icenter[0]), np.deg2rad(icenter[1]))
        imaskpx = np.rad2deg(np.arccos(np.dot(ivec, fullvec))) < threshold
        imaskidx = np.where(imaskpx == True)[0]
        relmaxpx[j] = np.max(hpmap[imaskpx])
        px[j] = imaskidx[np.argmax(hpmap[imaskpx])]

    indxmax = np.argmax(relmaxpx)
    pixmax, newcenter = px[indxmax], centerarr[indxmax]
    if doplot:
        hp.gnomview(hpmap,
                    reso=12,
                    rot=np.array([90, 0]) - newcenter,
                    nest=nest)
        if displaycenters:
            for each in centerarr:
                hp.projscatter(np.deg2rad(each), marker='+', color='r')
        hp.projscatter(hp.pix2ang(256, pixmax, nest=nest),
                       marker='+',
                       color='r')

    return pixmax, newcenter
Exemple #26
0
def draw_maglim_pixel(skymap,**kwargs):
    nside = healpy.npix2nside(len(skymap))
    pix = np.where(skymap > 0)
    if len(pix[0]) == 0:
        logger.warn("No maglims found")
        return

    ra,dec = pix2ang(nside,pix)
    ra_center,dec_center = np.median(ra),np.median(dec)

    vmin,vmax = maglim_range(skymap)

    kwargs.setdefault('rot',(ra_center, dec_center, 0.))
    kwargs.setdefault('min',vmin)
    kwargs.setdefault('max',vmax)
    healpy.gnomview(skymap,**kwargs)
Exemple #27
0
def random_map(masked_kappa, reso=4.5, size=256, verbose=False):
    """
    This takes a masked healpix map and returns a projected patch
    """

    while 1 == 1:
        proposal = hp.gnomview(masked_kappa,
                               rot=[
                                   np.random.uniform(0, 90.),
                                   np.random.uniform(0, 90.),
                                   np.random.uniform(0, 180)
                               ],
                               title='example',
                               reso=reso,
                               xsize=size,
                               ysize=size,
                               flip='geo',
                               return_projected_map=True)
        plt.close()
        #         print(proposal.shape)
        if len(np.where((proposal.mask).flatten() == True)[0]) > 1:
            if verbose == True:
                print('boundary error')
        else:
            if verbose == True:
                print('boundaries good')
            break

    return proposal.data
Exemple #28
0
def GoGetStackHealpix(x, y, skymap, mask, npix, noise=None, extras=None, z=None, rnd=True):
	results = {}
	results['maps'] = []

	if noise is not None:
		results['noise'] = []

	if extras is not None:
		for name in extras.iterkeys():
			results[name] = []

 	# embed()
 	coord = SkyCoord(ra=x, dec=y, unit='deg').transform_to('galactic')
 	l = coord.l.value
 	b = coord.b.value

 	if not rnd:
		for i in xrange(len(x)):
			cutmask = hp.gnomview(mask, rot=[l[i],b[i]], reso=0.5, xsize=2*npix, ysize=2*npix, return_projected_map=True)
			pl.close()

			if cutmask.all() == 1.:
				stamp = hp.gnomview(skymap, rot=[l[i],b[i]], reso=0.5, xsize=2*npix, ysize=2*npix, return_projected_map=True)
				results['maps'].append(stamp.data)
				pl.close()
			else:
				pass

				if extras is not None:
					for name in extras.iterkeys():
						results[name].append(extras[name][i])
	else:
		print 'BITCHES!!!'
		for i in xrange(len(x)):
			cutmask = hp.gnomview(skymap, rot=[l[i],b[i]+0.5], reso=0.5, xsize=2*npix, ysize=2*npix, return_projected_map=True)
			pl.close()

			if cutmask.all() < 10.:
				results['maps'].append(cutmask.data)
			else:
				pass

				if extras is not None:
					for name in extras.iterkeys():
						results[name].append(extras[name][i])

	return results
Exemple #29
0
def display(input, msg, iplot=1, reso=5, Trange=[100, 5, 5], xsize=800, subx=3, suby=3):
    out = []
    for i, (kind, lim) in enumerate(zip('IQU', Trange)):
        map = input[..., i]
        out += [hp.gnomview(map, rot=center, reso=reso, xsize=xsize, min=-lim,
                            max=lim, title=msg + ' ' + kind,
                            sub=(suby, subx, iplot + i), return_projected_map=True)]
    return out
Exemple #30
0
def main():

    nb_comp = 2
    NSIDE = 2048
    F_matrix = []

    for i in range(nb_comp):
        F_matrix.append(Total_matrix[i])

    print 'F_matrix : %s' % F_matrix

    truncated_maps = library.get_truncated_maps(NSIDE)
    mask = hp.read_map(setup.out_files_path + 'mask.fits')

    for i in range(nb_comp):

        #if i == 0:
        if True:

            print 'Computing component %s' % i

            e_vector = list()

            for j in range(nb_comp):
                e_vector.append(0)

            e_vector[i] = 1.

            S = library.compute_S_matrix(truncated_maps, F_matrix, e_vector)
            # hp.mollview(S[i], title="Component %s" % i, norm='hist')

            if i == 0:
                hp.gnomview(S, rot=[0, 90])
                hp.mollzoom(S, norm='hist')
                hp.write_map(
                    setup.out_files_path + "ILC_SZ_comp_" + str(NSIDE) +
                    ".fits", S)

            S = np.ma.array(S, mask=(mask == 0), fill_value=hp.UNSEEN)
            hp.mollview(S, title="Component_%s" % i, norm='hist')
            library.save_figure('Component_%s' % i, sub_folder='figures/ILC')
            del S

    plt.show()

    return 0
def display(input, msg, iplot=1):
    out = []
    for i, (kind, lim) in enumerate(zip('IQU', [50, 5, 5])):
        map = input[..., i]
        out += [hp.gnomview(map, rot=center, reso=5, xsize=800, min=-lim,
                            max=lim, title=msg + ' ' + kind,
                            sub=(3, 3, iplot + i), return_projected_map=True)]
    return out
def plot_gnom(map, lon, lat, label):
    """
    Plotting function viewing in gnomonic projection.

    Parameters:
    -----------
    - map, array. The map to plot.
    - lon, scalar. mean position in longitude
    - lat, scalar. mean position in latitude
    - label, string.
    Return:
    -------
    """
    path = 'Figures/tomography/'
    hp.gnomview(map, title='Polarization {}'.format(label), rot=[lon,90-lat,180],\
                flip='geo', xsize=100)
    hp.graticule()
    plt.savefig(path + '{}.png'.format(label))
def compute_hitmaps():
        x0+=1.
        hitmap= reorganize_map(A*x0,hp_pixs,npix,nside,pol,'data/hitmap_ra23_1day.fits')
        zeros=np.where(hitmap[0] == 0. )[0]

        hitmap[0][zeros]=hp.UNSEEN
        hp.gnomview(hitmap[0],rot=[-13.45,-32.09],xsize=600,title='hits',min=0)
        x0=np.array([0,0,1]*npix)
        hitmap= reorganize_map(A*x0,hp_pixs,npix,nside,pol,'data/hitmap_ra23_1day.fits')

        zeros=np.where(hitmap[0] == 0. )[0]
        hitmap[0][zeros]=hp.UNSEEN
        hitmap[1][zeros]=hp.UNSEEN
        hitmap[2][zeros]=hp.UNSEEN

        #hp.gnomview(hitmap[0],rot=[-13.45,-32.09],xsize=600,title='hits',min=0,sub=131)
        #hp.gnomview(hitmap[1],rot=[-13.45,-32.09],xsize=600,title='hits',sub=132)
        hp.gnomview(hitmap[2],rot=[-13.45,-32.09],xsize=600,title='hits',sub=133)
Exemple #34
0
def map_temp(data, temp, mask, pol, delta, Nside=256):
    map1 = np.full(12 * Nside**2, hp.UNSEEN)
    map2 = np.full(12 * Nside**2, hp.UNSEEN)

    map1[mask] = data
    map2[mask] = temp

    hp.gnomview(map1, title='{} polarisation'.format(pol),\
                unit=r'$\mu K_{cmb}$', rot=[104.1,22.225], xsize=100,\
                min=np.min(map1[mask]), max=np.max(map1[mask]))
    hp.graticule()
    plt.savefig('Figures/{}_map.png'.format(pol))

    hp.gnomview(map2, title='{} template shift {} deg'.format(pol, delta),\
                unit=r'$\mu K_{cmb}$', rot=[104.1,22.225], xsize=100,\
                min=np.min(map1[mask]), max=np.max(map1[mask]))
    hp.graticule()
    plt.savefig('Figures/{}_map_temp_{}.png'.format(pol, delta))
Exemple #35
0
def plot_residuals(a, temp, f_map, Nside, mask, f1, f2=353, pol='P'):
    Npix = hp.nside2npix(Nside)
    map = np.full(Npix, hp.UNSEEN)

    map[mask] = (f_map - a * temp) / np.abs(
        f_map)  #((f_map - a*temp)/np.abs(f_map))**2
    lon, lat = 104.1, 22.225
    #print(f_map)
    #print(a*temp)
    #print(map[mask])
    hp.gnomview(map, title=r'Residuals {}GHz: $({}-a*{}_{{temp}})/|{}|$'.\
                format(f1,pol,pol,pol), rot=[lon,lat], xsize=100,\
                unit=r'$\mu K_{{CMB}}$', cmap='jet')
    #    hp.gnomview(map, title=r'Residuals {}GHz: $(({}-a*{}_{{temp}})/|{}|)^2$'.\
    #                format(f1,pol,pol,pol), rot=[lon,lat], xsize=100, cmap='jet')
    hp.graticule()
    plt.savefig('Figures/template/{}_residuals_{}vs{}template_{}.png'.\
                format(pol,f1,f2,Nside))
Exemple #36
0
def projmap_mean(df, col, minmax=None, nsig=2, dohist=True, **kwargs):
    df_map = df.select(col, "ipix").na.drop().groupBy("ipix").avg(col)
    #statistics per pixel
    var = df_map.columns[-1]
    s = df_map.describe([var])
    s.show()
    r = s.select(var).take(3)
    N = int(r[0][0])
    mu = float(r[1][0])
    sig = float(r[2][0])
    map_p = df_map.toPandas()

    #now data is reducd create the healpy map
    skyMap = np.full(hp.nside2npix(nside), hp.UNSEEN)
    skyMap[map_p['ipix'].values] = map_p[var].values

    if minmax == None:
        minmax = (mu - nsig * sig, mu + nsig * sig)

    hp.gnomview(skyMap,
                reso=reso,
                min=minmax[0],
                max=minmax[1],
                nest=nest,
                title=var,
                **kwargs)

    if dohist:
        plt.figure()
        plt.hist(map_p[var].values, bins=80, range=minmax)
        plt.xlabel(var)
        stat = [
            r"$N={:d}$".format(N), r"$\mu={:g}$".format(mu),
            r"$\sigma={:g}$".format(np.sqrt(sig))
        ]
        ax = plt.gca()
        plt.text(0.7,
                 0.8,
                 "\n".join(stat),
                 horizontalalignment='center',
                 transform=ax.transAxes)

    plt.show()
    return skyMap
Exemple #37
0
def plot_filters_gnomonic(filters, order=10, ind=0, title='Filter {}->{}', graticule=False):
    """Plot all filters in a filterbank in Gnomonic projection."""
    nside = hp.npix2nside(filters.G.N)
    reso = hp.pixelfunc.nside2resol(nside=nside, arcmin=True) * order / 100
    rot = hp.pix2ang(nside=nside, ipix=ind, nest=True, lonlat=True)

    maps = filters.localize(ind, order=order)

    nrows, ncols = filters.n_features_in, filters.n_features_out

    if maps.shape[0] == filters.G.N:
        # FIXME: old signal shape when not using Chebyshev filters.
        shape = (nrows, ncols, filters.G.N)
        maps = maps.T.reshape(shape)
    else:
        if nrows == 1:
            maps = np.expand_dims(maps, 0)
        if ncols == 1:
            maps = np.expand_dims(maps, 1)

    # Plot everything.
    # fig, axes = plt.subplots(nrows, ncols, figsize=(17, 17/ncols*nrows),
    #                          squeeze=False, sharex='col', sharey='row')
    cm = plt.cm.seismic
    cm.set_under('w')
    a = max(abs(maps.min()), maps.max())
    ymin, ymax = -a,a
    for row in range(nrows):
        for col in range(ncols):
            map = maps[row, col, :]
            hp.gnomview(map.flatten(), nest=True, rot=rot, reso=reso, sub=(nrows, ncols, col+row*ncols+1),
                    title=title.format(row, col), notext=True,  min=ymin, max=ymax, cbar=False, cmap=cm,
                    margins=[0.003,0.003,0.003,0.003],)
            # if row == nrows - 1:
            #     #axes[row, col].xaxis.set_ticks_position('top')
            #     #axes[row, col].invert_yaxis()
            #     axes[row, col].set_xlabel('out map {}'.format(col))
            # if col == 0:
            #     axes[row, col].set_ylabel('in map {}'.format(row))
    # fig.suptitle('Gnomoinc view of the {} filters in the filterbank'.format(filters.n_filters))#, y=0.90)
    # return fig
    if graticule:
        with utils.HiddenPrints():
            hp.graticule(verbose=False)
def run_test():
    nside = 512
    center = map(angles.from_degrees, [-92.0, -1.0])
    scale = 1.0
    fwhm = angles.from_arcmin(16.0)
    radius = angles.from_degrees(3.0)
    hit_map, signal_map = _generate_beam_map(nside, center, scale, fwhm, radius)

    guess_center = map(angles.from_degrees, [-91.5, -1.1])
    guess_fwhm = angles.from_arcmin(24.0)
    fit = fit_map(hit_map, signal_map, guess_center, guess_fwhm)
    print fit_string(fit)

    signal_map[hit_map == 0] = pylab.nan
    xsize = 1000
    reso_arcmin = angles.to_arcmin(radius*2.5) / float(xsize)
    #healpy.mollview(signal_map)
    healpy.gnomview(signal_map, rot=map(angles.to_degrees, center), xsize=xsize, reso=reso_arcmin)
    pylab.show()
Exemple #39
0
def test():
	fname = '/Users/bl/Dropbox/Projects/Quicksip/data/SVA1_COADD_ASTROM_PSF_INFO.fits'
	#fname = '/Users/bl/Dropbox/Projects/Quicksip/data/Y1A1_IMAGEINFO_and_COADDINFO.fits'
	pixoffset = 10
	hdulist = pyfits.open(fname)
	tbdata = hdulist[1].data
	hdulist.close()
	nside = 1024
	ratiores = 4
	treemap = HealTree(nside)
	#results = pool.map(treemap.addElem, [imagedata for imagedata in tbdata])
	print tbdata.dtype
	#ind = np.ndarray([0])
	ind = np.where( tbdata['band'] == 'i' )
	import numpy.random
	ind = numpy.random.choice(ind[0], 1 )
	print 'Number of images :', len(ind)
	hpxmap = np.zeros(hp.nside2npix(nside))
	ras_c = []
	decs_c = []
	for i, propertyArray in enumerate(tbdata[ind]):
	    ras_c.append(propertyArray['RA'])
	    decs_c.append(propertyArray['DEC'])
	plt.figure()
	for i, propertyArray in enumerate(tbdata[ind]):
	    print i
	    propertyArray.dtype = tbdata.dtype
	    listpix, weights, thetas_c, phis_c, listpix_sup = computeHPXpix_sequ_new(nside, propertyArray, pixoffset=pixoffset, ratiores=ratiores)
	    #listpix2, weights2, thetas_c2, phis_c2 = computeHPXpix_sequ(nside, propertyArray, pixoffset=pixoffset, ratiores=ratiores)
	    hpxmap = np.zeros(hp.nside2npix(nside))
	    hpxmap[listpix] = weights
	    hpxmap_sup = np.zeros(hp.nside2npix(ratiores*nside))
	    hpxmap_sup[listpix_sup] = 1.0
	    listpix_hi, weights_hi, thetas_c_hi, phis_c_hi, superind_hi = computeHPXpix_sequ_new(ratiores*nside, propertyArray, pixoffset=pixoffset, ratiores=1)
	    hpxmap_hi = np.zeros(hp.nside2npix(ratiores*nside))
	    hpxmap_hi[listpix_hi] = weights_hi
	    hpxmap_hitolo = hp.ud_grade(hpxmap_hi, nside)
	    print 'valid hpxmap_hi', np.where(hpxmap_hi > 0)[0]
	    print 'hpxmap', zip(np.where(hpxmap > 0)[0], hpxmap[hpxmap > 0])
	    print 'hpxmap_sup', zip(np.where(hpxmap_sup > 0)[0], hpxmap_sup[hpxmap_sup > 0])
	    print 'hpxmap_hitolo', zip(np.where(hpxmap_hitolo > 0)[0], hpxmap_hitolo[hpxmap_hitolo > 0])
	    hp.gnomview(hpxmap_hi, title='hpxmap_hi', rot=[propertyArray['RA'], propertyArray['DEC']], reso=0.2)
	    hp.gnomview(hpxmap_sup, title='hpxmap_sup', rot=[propertyArray['RA'], propertyArray['DEC']], reso=0.2)
	    hp.gnomview(hpxmap_hitolo, title='hpxmap_hitolo', rot=[propertyArray['RA'], propertyArray['DEC']], reso=0.2)
	    hp.gnomview(hpxmap, title='hpxmap', rot=[propertyArray['RA'], propertyArray['DEC']], reso=0.2)
	    #plt.plot(phis_c, thetas_c)
	    thetas, phis = hp.pix2ang(nside, listpix)
	    #plt.scatter(phis, thetas, color='red', marker='o', s=50*weights)
	    #plt.scatter(propertyArray['RA']*np.pi/180, np.pi/2 - propertyArray['DEC']*np.pi/180)
	    #plt.text(propertyArray['RA']*np.pi/180, np.pi/2 - propertyArray['DEC']*np.pi/180, str(i))
	plt.show()
	stop
Exemple #40
0
    def plot_zoom_from_map(self,
                           ind,
                           reso=1.,
                           cmap=None,
                           draw_contour=True,
                           ax=None,
                           col_label=r'$\log_{10}$(prob.)'):
        s = self.cascade_info['skymap'][ind]
        nside = hp.get_nside(s)
        ra, dec = self.loc_of_map(ind)
        title = f"Run {self.cascade_info['run'][ind]}, " \
            + f"Event {self.cascade_info['event'][ind]}"
        if cmap is None:
            pdf_palette = sns.color_palette("Blues", 500)
            cmap = mpl.colors.ListedColormap(pdf_palette)
        skymap = np.log10(s)
        #min_color = np.min([0., 2.*max_color])
        max_color = max(skymap)
        min_color = max_color - 4.
        hp.gnomview(skymap,
                    rot=(np.degrees(ra), np.degrees(dec), 0),
                    cmap=cmap,
                    max=max_color,
                    min=min_color,
                    reso=reso,
                    title=title,
                    notext=True,
                    cbar=False
                    #unit=r""
                    )

        plt.plot(4.95 / 3. * reso * np.radians([-1, 1, 1, -1, -1]),
                 4.95 / 3. * reso * np.radians([1, 1, -1, -1, 1]),
                 color="k",
                 ls="-",
                 lw=3)
        hp.graticule(verbose=False)
        self.plot_labels(dec, ra, reso)
        self.plot_color_bar(cmap=cmap,
                            labels=[min_color, max_color],
                            col_label=col_label)
Exemple #41
0
def runcannon():

    comp=['therm','cmb','synch']

    ds,test_label=getTdataset(components=comp)

    md = model.CannonModel(2)
    md.fit(ds)
    
    # Starting guesses and bounds
    #sg = test_label
    sg = np.mean(test_label, axis=0)

    lb = np.min(test_label, axis=0)
    ub = np.max(test_label, axis=0)
    err,chi2 = md.infer_labels(ds, starting_guess=sg, bounds=(lb,ub))

    # Get predictions
    md.infer_spectra(ds)
    
    if ds.test_flux.shape == ds.tr_flux.shape:
        prefix = 'betaCOM_TdCOM_sm1deg_' 
        freq = 353
        freqind = np.where(ds.wl==freq)[0]
        fn = get_pysm_fname(freq,comp,prefix=prefix)
        x = hp.read_map(fn,field=(0,1,2))
        hmap1 = np.sqrt(x[1]**2 + x[2]**2)
        hmap2 = dc(hmap1)
        hmap2[:] = 0
        hmap2[ds.test_ID] = md.model_spectra[:,freqind]

        #ds.diagnostics_1to1()

        close('all')
        map1 = hp.gnomview(hmap1,rot=(0,12),return_projected_map=True)
        map2 = hp.gnomview(hmap2,rot=(0,12),return_projected_map=True)
    
    return ds,md,test_label
Exemple #42
0
def make_cutouts_from_catalog(catalog, hpix_map, reso_arcmin=0.5, nside=61):
    add_healpix_coordinates(catalog)
    import healpy as hp
    lon_deg = catalog['ra']
    lat_deg = catalog['dec']
    ncl = len(catalog['ra'])
    cutouts = np.zeros((ncl, nside, nside))
    for i, lon, lat in zip(range(ncl), lon_deg, lat_deg):
        print '%i/%i'%(i,ncl)
        pl.clf()
        cutout = hp.gnomview(hpix_map, rot=(lon, lat, 0), fig=1,
                             reso=reso_arcmin, xsize=nside, ysize=nside,
                             return_projected_map=True)
        cutouts[i, :, :] = cutout
    return cutouts
def display(x, title):
    x = x.copy()
    x[~mask] = np.nan
    hp.gnomview(x, rot=center, reso=5, xsize=600, min=-200, max=200,
                title=title)
Exemple #44
0
sbideal = inst[idet].get_synthbeam(scene)[0]
sbnew = inst[idet].get_synthbeam(scene, external_A=external_A)[0]
sbideal_int = inst[idet].get_synthbeam(scene, detector_integrate=nint)[0]
sbnew_int = inst[idet].get_synthbeam(scene, external_A=external_A, detector_integrate=nint)[0]

sbnew *= np.sum(np.nan_to_num(sbideal))/np.sum(np.nan_to_num(sbnew))
sbnew_int *= np.sum(np.nan_to_num(sbideal_int))/np.sum(np.nan_to_num(sbnew_int))

blm_ideal = hp.anafast(sbideal)
blm_sim = hp.anafast(sbnew)
blm_ideal_int = hp.anafast(sbideal_int)
blm_sim_int = hp.anafast(sbnew_int)

clf()
mini=-25
hp.gnomview(np.log10(sbideal_int/np.max(sbideal_int))*10, rot=[0,90], reso=5, sub=(2,2,1),
            title='Ideal - with Pix. Int.', min=mini,max=0, unit='dB')
hp.gnomview(np.log10(sbnew_int/np.max(sbnew_int))*10, rot=[0,90], reso=5, sub=(2,2,2),
            title='Sim - with Pix. Int.',min=mini, max=0, unit='dB')
subplot(2,1,2)
plot(blm_sim / blm_ideal, label='Sim / ideal')
plot(blm_sim_int / blm_ideal_int, label='Sim / ideal Int')
ylim(0.5,1.5)
plot(np.ones(len(blm_sim_int)),'k:')
xlim(0,300)
legend()

xx = linspace(0,199,200)*5.
map = hp.gnomview(sbideal_int/np.max(sbideal_int), rot=[0,90], reso=5, return_projected_map=True)
plot(xx,map[100,:])

Exemple #45
0
def run_final_mcmc( model=None,
	dir = '/global/scratch2/sd/dpietrob/DM-haze/data/maps/ns0128/',
	mfile = 'south_haze_ptsrc_mask_ns0128.fits', # mask used for the region fit
	sig_scaling = 8,
	haze_fit_mask = 'fit_mask_p-20_20_t-10-35_ns0128.fits',
	fix_cmb   = True,
	fix_ff    = False, # Does/does not assign freefree parameter
	inc_disc  = False,
	## inc_bubbles  = True,
	## :DP May 2015 to get Col.4 of Tab.3 running with no bubbles
	inc_bubbles  = False,
	inc_mono  = True,
	inc_dipo  = True,
	plot_gnom = False,
	seed=0,
	regression = 0,
	tag = '',
	nsamples = 750000
	 ):

	if model == None:
		info_message( 'ERROR: dm_model not provided. Stop' )
		sys.exit()

	tag = tag + '_' + model + '_2stp_'+str(sig_scaling).zfill(2)+'x'
	tag = tag + '_' + str(seed)
	print tag

	print model

# --- Templates converted to mK_CMB
	fname = dir + 'haze_model_54_'+model+'_ns0128_K_norm.fits'
	if not os.path.isfile(fname):
		info_message( ' >>> making DM model templates...' )
	  	make_dmhaze_templates( model=model )
#
#
#
# --- Making a more suitable mask ---
# 30 deg radius from [0,-22.5]
# radius = -1
# if False:
# 	m = hp.read_map( dir + 'south_haze_ptsrc_mask_ns0128.fits' )
# 	# hp.mollview(m)
# 	print np.sum(m)/len(m)
# 
# 	x,y,z = hp.ang2vec(np.pi/2+22.5/180.*np.pi ,0.)
# 	d = make_disc(128, radius, direction=[x,y,z])
# 	# hp.mollview(d)
# 	# hp.mollview(d*m)
# 	print np.sum(m*d)/len(m)
# 
# 	hp.write_map( dir + 'south'+str(radius)+'_haze_ptsrc_mask_ns0128.fits', d*m*ptsrc*ptsrc1 )
# 	hp.gnomview( d*m*ptsrc*ptsrc1, rot=[0,-22.5], reso=25, min=-1, max=1)
# 	hp.graticule(dpar=5,dmer=5)
# 	# hp.gnomview( ptsrc, rot=[0,-22.5], reso=25, min=-1, max=1)
# 	# hp.graticule(dpar=5,dmer=5)
# 
# 	m = hp.read_map( dir + 'fit_mask_p-35_35_t-10-35_ns0128.fits' )
# 	# hp.mollview(m)
# 	print np.sum(m)/len(m)
# 
# 	pl.show()
# # sys.exit()
# 
# if radius>0:
# 	tag = 'southern'+str(radius)	
# 	mfile = 'south'+str(radius)+'_haze_ptsrc_mask_ns0128.fits'
# else:
# 	tag = 'southern'
# 	mfile = 'south_haze_ptsrc_mask_ns0128.fits'
# 
# # tag = tag + '_2stp'
# # tag = tag + '_2stp_10x'
# sig_scaling = 10
# 
# tag = tag + '_2stp_6freq_fxFF_'+str(sig_scaling).zfill(2)+'x'

	mask = hp.read_map( dir + mfile )
	mask_npix = np.sum( mask )
	fsky = np.sum( mask ) / len(mask)
	gpix = np.array( (mask == 1.) )
	bpix = np.array( (mask == 0.) )

	# --- Define haze flux computation region
	d = hp.read_map( dir + haze_fit_mask )
	haze_region = (mask*d == 1)
	not_haze_region = (mask*d == 0)

	# --- What to do 
	find_minimum = True
	run_mcmc     = True
	# ------
	plot_min_sed = False
	do_write     = False

	# ------ Run MCMC ------
	# setup  = Data( DMmodel=model, maskfile='ebvh_mask_ns0128.fits', raw_sync=True )
	# setup  = Data( DMmodel=model, maskfile=mfile, raw_sync=True )
	setup  = Data( DMmodel=model, maskfile=mfile, raw_sync=True )
	data   = setup.data
	gmodel = setup.gmodel

	freq_min = OrderedDict()
	# --- Set parameter values from full sky for some components ---
	if find_minimum:
		info_message( 'Running regression' )

		fx_mnpnt = OrderedDict()
		fx_mnpnt_er = OrderedDict()

		mnpnt = OrderedDict()
		mnpnt_er = OrderedDict()

		chisq = []

		fx_c2 = 0.
		c2 = 0.

		flux = {}
		cfreq = data.cfreq
		mKt2cgs = conversionfactor( cfreq, option='mKthermo2cgs' )
			
		haze_flux = []
		haze_flux_er = []
		haze_templ_flux = []
		haze_templ_flux_er = []

		# --- Find minimum for each frequency
		for ifreq, freq in enumerate( data.frequencies ):

			h = getattr( gmodel.haze, 'f'+freq )

			# --- Full run for starting point determination
			# Thin disc component removed: chisq gets worse, but global fit does
			# not change drmatically.
			# tfiles = [ gmodel.galaxy.cmb.filename, gmodel.galaxy.dust.filename, gmodel.galaxy.freefree.filename, 
			tfiles = [ gmodel.galaxy.cmb.filename, gmodel.galaxy.dust.filename,
				gmodel.galaxy.freefree.filename, 
				gmodel.galaxy.sync.filename, h.filename ]
			icmb = 0
			idust = 1
			iff = 2
			isync = 3
			ihaze = 4

			if inc_mono:
				tfiles.append( dir+'map_monopole_ns0128.fits' )
				imono = len(tfiles)-1
			if inc_dipo:
				tfiles.append( dir+'map_xdipole_ns0128.fits' )
				idipox = len(tfiles)-1

				tfiles.append( dir+'map_ydipole_ns0128.fits' )
				idipoy = len(tfiles)-1

				tfiles.append( dir+'map_zdipole_ns0128.fits' )
				idipoz = len(tfiles)-1
			if inc_disc:
				tfiles.append( gmodel.galaxy.disc.filename )
				idisc = len(tfiles)-1
			if inc_bubbles:
				tfiles.append( gmodel.galaxy.bubbles.filename )
				ibubbles = len(tfiles)-1

			map = getattr( data, 'f'+freq )
			r = map_regression( map.imap.map, tfiles, rms=map.irms.map,
				maskfile=dir+'ebvh_mask_ns0128.fits', return_res=True, return_chi2=True )
				# maskfile=dir+'map_monopole_ns0128.fits', return_res=True, return_chi2=True )
			chisq.append( str("%.3f" % r[3]) )
			fx_c2 += r[3]
			
			# hp.mollview( r[2], title='FS '+freq, min=-0.3, max=0.3)
			
			fx_mnpnt['f'+freq+'_cmb'] = r[0][icmb]
			fx_mnpnt['f'+freq+'_dust'] = r[0][idust]
			fx_mnpnt['f'+freq+'_freefree'] = r[0][iff]
			fx_mnpnt['f'+freq+'_sync'] = r[0][isync]
			fx_mnpnt['f'+freq+'_haze'] = r[0][ihaze]
			if inc_mono:
				fx_mnpnt['f'+freq+'_monopole'] = r[0][imono]
			if inc_dipo:
				fx_mnpnt['f'+freq+'_dipole_x'] = r[0][idipox]
				fx_mnpnt['f'+freq+'_dipole_y'] = r[0][idipoy]
				fx_mnpnt['f'+freq+'_dipole_z'] = r[0][idipoz]
			if inc_disc:
				fx_mnpnt['f'+freq+'_disc'] = r[0][idisc]
			if inc_bubbles:
				fx_mnpnt['f'+freq+'_bubbles'] = r[0][ibubbles]

			fx_mnpnt_er['f'+freq+'_cmb'] = r[1][icmb]
			fx_mnpnt_er['f'+freq+'_dust'] = r[1][idust]
			fx_mnpnt_er['f'+freq+'_freefree'] = r[1][iff]
			fx_mnpnt_er['f'+freq+'_sync'] = r[1][isync]
			fx_mnpnt_er['f'+freq+'_haze'] = r[1][ihaze]
			if inc_mono:
				fx_mnpnt_er['f'+freq+'_monopole'] = r[1][imono]
			if inc_dipo:
				fx_mnpnt_er['f'+freq+'_dipole_x'] = r[1][idipox]
				fx_mnpnt_er['f'+freq+'_dipole_z'] = r[1][idipoz]
				fx_mnpnt_er['f'+freq+'_dipole_y'] = r[1][idipoy]
			if inc_disc:
				fx_mnpnt_er['f'+freq+'_disc'] = r[1][idisc]
			if inc_bubbles:
				fx_mnpnt_er['f'+freq+'_bubbles'] = r[1][ibubbles]

			# --- Remove monopole, dipole and CMB from the map
			if inc_mono:
				flat_comp = r[0][imono]
			if inc_dipo:
				flat_comp += gmodel.galaxy.dipole_x.map * r[0][idipox] + \
				gmodel.galaxy.dipole_y.map * r[0][idipoy] + \
				gmodel.galaxy.dipole_z.map * r[0][idipoz]
			if fix_cmb:
				flat_comp += gmodel.galaxy.cmb.map * r[0][icmb]
				# gmodel.galaxy.freefree.map * r[0][2]

			# --- Run on small patch after flat component removed
			tfiles = [ gmodel.galaxy.dust.filename,
				gmodel.galaxy.sync.filename,
				h.filename,
				gmodel.galaxy.freefree.filename ]

			if inc_disc:
				tfiles.append( gmodel.galaxy.disc.filename )
				idisc = len( tfiles ) -1
				
			if inc_bubbles:
				tfiles.append( gmodel.galaxy.bubbles.filename )
				ibubbles = len( tfiles ) -1
				
			if not fix_cmb:
				tfiles.append( gmodel.galaxy.cmb.filename )
				icmb = len( tfiles ) -1

			#if inc_mono:
			#	tfiles.append( dir+'map_monopole_ns0128.fits' )
			#	imono = len( tfiles ) -1

			# map = getattr( data, 'f'+freq )
			# --- Setting up the smaller mask in the southern region
			imap = getattr( map, 'imap' )
			setattr(imap, 'map', map.imap.map-flat_comp )
			setattr(map, 'imap', imap )
			
			r = map_regression( map.imap.map, tfiles, rms=map.irms.map,
				maskfile=dir+mfile, return_res=True, return_chi2=True )
			chisq.append( str("%.3f" % r[3]) )
			c2 += r[3]

			# --- Plot gnomview projections
			if plot_gnom:
				ccc = map.imap.map
				ccc[ bpix] = -1.6375e30
				hp.gnomview( ccc, title=freq, rot=[0,-22.5], reso=25 )
				hp.graticule(dpar=5,dmer=5)

				if freq == '070':
					ccc = gmodel.galaxy.cmb.map
					ccc[ bpix] = -1.6375e30
					hp.gnomview( ccc, min=-0.3, max=0.3, title='CMB', rot=[0,-22.5], reso=25 )
					hp.graticule(dpar=5,dmer=5)

				if freq == 'K':
					ccc = gmodel.galaxy.sync.map
					ccc[ bpix] = -1.6375e30
					hp.gnomview( ccc, title='Sync', rot=[0,-22.5], reso=25 )
					hp.graticule(dpar=5,dmer=5)

					ccc = gmodel.galaxy.dust.map
					ccc[ bpix] = -1.6375e30
					hp.gnomview( ccc, title='Dust', rot=[0,-22.5], reso=25 )
					hp.graticule(dpar=5,dmer=5)

					ccc = gmodel.galaxy.freefree.map
					ccc[ bpix] = -1.6375e30
					hp.gnomview( ccc, title='FF', rot=[0,-22.5], reso=25 )
					hp.graticule(dpar=5,dmer=5)

				ccc = r[2]*mask
				ccc[ bpix] = -1.6375e30
				hp.gnomview( ccc, min=-0.05, max=0.05, title='Residuals: '+freq,
					rot=[0,-22.5], reso=25 )
				hp.graticule(dpar=5,dmer=5)

				ccc = (r[2]+r[0][2]*h.map)*mask
				ccc[ bpix] = -1.6375e30
				hp.gnomview( ccc, title='Haze: '+freq, rot=[0,-22.5], reso=25 )
				hp.graticule(dpar=5,dmer=5)

				# ccc = (r[0][2]*h.map)*mask
				# ccc[ bpix] = -1.6375e30
				# hp.gnomview( ccc, min=0., title='Haze template: '+freq, rot=[0,-22.5], reso=25 )
				# hp.graticule(dpar=5,dmer=5)
				pl.show()
			
			# --- Flux with residuals
			# haze_flux.append( np.mean( (r[2] + r[0][2]*h.map )[gpix] ) * mKt2cgs[ifreq] )
			haze_flux.append( np.mean( (r[2] + r[0][2]*h.map )[haze_region] ) * mKt2cgs[ifreq] )
			# haze_flux_er.append( np.std( (r[2] + r[0][2]*h.map)[gpix] ) * mKt2cgs[ifreq] )
			# --- Trying to be independent of the haze template
			# haze_flux_er.append( np.std( r[2][gpix] ) * mKt2cgs[ifreq] )
			haze_flux_er.append( np.sqrt( np.var( r[2][haze_region] ) + (haze_flux[-1]*r[1][2])**2)* mKt2cgs[ifreq] )

			freq_min[ freq ] = r

			mnpnt['f'+freq+'_dust'] = r[0][0]
			mnpnt['f'+freq+'_sync'] = r[0][1]
			mnpnt['f'+freq+'_haze'] = r[0][2]
			mnpnt['f'+freq+'_freefree'] = r[0][3]
			if inc_disc:
				mnpnt['f'+freq+'_disc'] = r[0][idisc]
			if inc_bubbles:
				mnpnt['f'+freq+'_bubbles'] = r[0][ibubbles]
			if not fix_cmb:
				mnpnt['f'+freq+'_cmb'] = r[0][icmb]
			#if inc_mono:
			#	mnpnt['f'+freq+'_monopole'] = r[0][imono]

			mnpnt_er['f'+freq+'_dust'] = r[1][0]
			mnpnt_er['f'+freq+'_sync'] = r[1][1]
			mnpnt_er['f'+freq+'_haze'] = r[1][2]
			mnpnt_er['f'+freq+'_freefree'] = r[1][3]
			if inc_disc:
				mnpnt_er['f'+freq+'_disc'] = r[1][idisc]
			if inc_bubbles:
				mnpnt_er['f'+freq+'_bubbles'] = r[1][ibubbles]
			if not fix_cmb:
				mnpnt_er['f'+freq+'_cmb'] = r[1][icmb]
			#if inc_mono:
			#	mnpnt_er['f'+freq+'_monopole'] = r[1][imono]

		flux[model+'_haze'] = ( np.array( haze_flux )*1.e20,
			np.array( haze_flux_er )*1.e20 )

		if do_write:
			figtag = '../sed_dm_' + model + '_2step_' + tag
			if fix_cmb:
				figtag = figtag + '_fixCMB'
			if not inc_disc:
				figtag = figtag + '_noDISC'
			if inc_mono:
				figtag = figtag + '_MONO'

			file = open(figtag + '.txt', 'w')
			file.write( '# cfreq, res_haze, res_haze err\n' )
			for ifreq,freq in enumerate(data.cfreq):
				# print ifreq, freq
				file.write( '%f \t %f \t %f \n' %(freq, flux[model+'_haze'][0][ifreq],
					flux[model+'_haze'][1][ifreq] ) )
			file.close()
		# print flux

		info_message(' total chi2 = '+str(c2)+','+str(c2*np.sum(mask)))

		sed = get_sed( mnpnt )
		sed_er = get_sed( mnpnt_er )

		fx_sed = get_sed( fx_mnpnt )
		fx_sed_er = get_sed( fx_mnpnt_er )
	
		if plot_min_sed:
			f = data.cfreq
			cv = conversionfactor( f, option='antenna2thermo' )
			fig = plt.figure(30+imodel,(18,7.5), dpi=80)

			# --- SED plot based on regression coefficients
			ax = plt.subplot(121)
	
			plt.errorbar( f, sed['dust'], yerr=sed_er['dust'], label='dust', color='b', fmt='s:' )
			plt.errorbar( f, sed['sync'], yerr=sed_er['sync'], label='sync', color='g', fmt='s:' )
			plt.errorbar( f, sed['haze'], yerr=sed_er['haze'], label='haze', color='m', fmt='s:' )
			plt.errorbar( f, sed['freefree'], yerr=sed_er['freefree'], label='freefree', color='r', fmt='s:' )
			if not fix_cmb:
				plt.errorbar( f, sed['cmb'], yerr=sed_er['cmb'], label='cmb', color='k', fmt='s:' )
			if inc_disc:
				plt.errorbar( f, sed['disc'], yerr=sed_er['disc'], label='disc', color='y', fmt='s:' )

			plt.errorbar( f, fx_sed['cmb'], yerr=fx_sed_er['cmb'], label='cmb FS', color='k', fmt='.-' )
			plt.errorbar( f, fx_sed['dust'], yerr=fx_sed_er['dust'], label='dust FS', color='b', fmt='.-' )
			plt.errorbar( f, fx_sed['sync'], yerr=fx_sed_er['sync'], label='sync FS', color='g', fmt='.-' )
			plt.errorbar( f, fx_sed['haze'], yerr=fx_sed_er['haze'], label='haze FS', color='m', fmt='.-' )
			plt.errorbar( f, fx_sed['freefree'], yerr=fx_sed_er['freefree'], label='freefree FS', color='r', fmt='.-' )
			if inc_disc:
				plt.errorbar( f, fx_sed['disc'], yerr=fx_sed_er['disc'], label='disc FS', color='y', fmt='s-' )

			plt.plot( f, (f/f[0])**(-3.1)*cv*fx_sed['sync'][0], ':', label='-3.1', color='g' )
			plt.plot( f, (f/f[0])**(-2.15)*cv*fx_sed['freefree'][0], ':', label='-2.15', color='r' )
			plt.plot( f, f*0.+sed['haze'][0], ':', color='m', label='same amplitude' )

			plt.title('Dark matter model: '+model)
			plt.xlabel(r'$\nu$ [GHz]')
			plt.ylabel('Template amplitudes')

			ax.set_yscale('log')
			ax.set_xscale('log')
			ax.set_ylim([5e-3,10])
			ax.set_xlim([8,100])
			plt.legend( loc='upper left',prop={'size':10})

			# --- Monopole dipole plot
# 			ax = plt.subplot(122)
# 	
# 			plt.errorbar( f, sed['monopole'], yerr=sed_er['monopole'], label='monopole', color='k' )
# 			plt.errorbar( f, sed['dipole_x'], yerr=sed_er['dipole_x'], label='dipole_x', color='b' )
# 			plt.errorbar( f, sed['dipole_y'], yerr=sed_er['dipole_y'], label='dipole_y', color='r' )
# 			plt.errorbar( f, sed['dipole_z'], yerr=sed_er['dipole_z'], label='dipole_z', color='m' )
# 			plt.plot( f, f*0., '--', color='g' )
# 			plt.errorbar( f, sed_noh['monopole'], yerr=sed_er_noh['monopole'], label='monopole no-haze', color='k', fmt='--' )
# 			plt.errorbar( f, sed_noh['dipole_x'], yerr=sed_er_noh['dipole_x'], label='dipole_x no-haze', color='b', fmt='--' )
# 			plt.errorbar( f, sed_noh['dipole_y'], yerr=sed_er_noh['dipole_y'], label='dipole_y no-haze', color='r', fmt='--' )
# 			plt.errorbar( f, sed_noh['dipole_z'], yerr=sed_er_noh['dipole_z'], label='dipole_z no-haze', color='m', fmt='--' )
# 
# 			plt.errorbar( f, sed_nod['monopole'], yerr=sed_er_nod['monopole'], label='monopole no-disc', color='k', fmt='s-' )
# 			plt.errorbar( f, sed_nod['dipole_x'], yerr=sed_er_nod['dipole_x'], label='dipole_x no-disc', color='b', fmt='s-' )
# 			plt.errorbar( f, sed_nod['dipole_y'], yerr=sed_er_nod['dipole_y'], label='dipole_y no-disc', color='r', fmt='s-' )
# 			plt.errorbar( f, sed_nod['dipole_z'], yerr=sed_er_nod['dipole_z'], label='dipole_z no-disc', color='m', fmt='s-' )
# 
# 			plt.title(r'$^{\rm red}\chi^2=$'+', '.join(chisq)+r'$\pm0.0028$'+' vs \n'+', '.join(chisq_noh)+' vs \n'+', '.join(chisq_nod), fontsize=12)
# 			# plt.title('Dark matter model: '+model+' - Southern region')
# 			plt.xlabel(r'$\nu$ [GHz]')
# 			plt.ylabel(r'Mono/dipole amplitudes [mK_CMB]')
# 
# 			ax.set_xlim([10,100])
# 			ax.set_xscale('log')
# 			hd = np.reshape([sed['monopole'],sed['dipole_x'],sed['dipole_y'],sed['dipole_z'],sed_noh['monopole'],sed_noh['dipole_x'],sed_noh['dipole_y'],sed_noh['dipole_z']], 8*len(sed['monopole']) )
# 			thrs = 1.1 * np.max( abs( hd ) )
# 			ax.set_ylim([-thrs,thrs])
# 			plt.legend( loc='lower right',prop={'size':10})

			# --- SED plot based on average flux in the region.
			# Mind, this is region dependent!!!
			ax = plt.subplot(122)
			plt.errorbar( f, flux[model+'_haze'][0], yerr=flux[model+'_haze'][1], label='Haze flux', color='m', fmt='s-' )
			plt.plot( f, f*0., '--', color='k')

			ax.set_ylim([-.75,2])
			ax.set_xlim([8,100])
			plt.xlabel(r'$\nu$ [GHz]')
			plt.ylabel(r'Haze flux [erg/(cm$^2$ s Hz sr)] $\times 10^{20}$')
			plt.legend( loc='upper left',prop={'size':10})
			ax.annotate(', '.join(data.frequencies), xy=(10, -0.5) )
			ax.annotate(', '.join([ str("%0.3f" %s) for s in flux[model+'_haze'][0] ]), xy=(10, -0.6) )
			ax.annotate(r'$\pm$'+', '.join([ str("%0.3f" %s) for s in flux[model+'_haze'][1] ]), xy=(10, -0.7) )

			figtag = '../sed_dm_'+model+'_2step_'+tag
			if fix_cmb:
				figtag = figtag + '_fixCMB'
			if not inc_disc:
				figtag = figtag + '_noDISC'
			if inc_mono:
				figtag = figtag + '_MONO'

			fig.savefig(figtag + '.png')

			pl.show()

		# --- Setting starting point
		p = OrderedDict()

		limits = {}
		limits['dust'] = (0.,11.)
		limits['freefree'] = (0.,50)
		limits['sync'] = (0.,50)
		limits['cmb'] = (0.97,1.03)
		# limits['monopole'] = (-0.06,0.06)
		# limits['disc'] = (0,100)
		# limits['monopole'] = (-0.06,0.06)
		limits['disc'] = (0.,1)
		limits['bubbles'] = (0.,10.) #This is actually the Haze, not DM

		# sig_scaling = 6.
		# p['global_haze'] = {'center_value':np.min( sed['haze'] ), 'sigma':np.min(sed_er['haze'])/sig_scaling, 'min':0., 'max':1000}
		p['global_haze'] = {'center_value':sed['haze'][0], 'sigma':np.min(sed_er['haze'])/sig_scaling, 'min':0., 'max':1000.}

		for ifreq, freq in enumerate( data.frequencies ):
			m = getattr( data, 'f'+freq )
			# hp.mollview( m.imap.map )
		
			r = freq_min[ freq ]
			if not fix_cmb:
				p['f'+freq+'_cmb'] = {'center_value':np.max( np.array([0.,sed['cmb'][ifreq]])), 'sigma':np.min(sed_er['cmb'])/sig_scaling, 'min':limits['cmb'][0], 'max':limits['cmb'][1]}
			p['f'+freq+'_dust'] = {'center_value':np.max( np.array([0.,sed['dust'][ifreq]])), 'sigma':np.min(sed_er['dust'])/sig_scaling, 'min':limits['dust'][0], 'max':limits['dust'][1]}
			if not fix_ff:
				p['f'+freq+'_freefree'] = {'center_value':np.max( np.array([0.,sed['freefree'][ifreq]])), 'sigma':np.min(sed_er['freefree'])/sig_scaling, 'min':limits['freefree'][0], 'max':limits['freefree'][1]}
			p['f'+freq+'_sync'] = {'center_value':np.max( np.array([0.,sed['sync'][ifreq]])), 'sigma':np.min(sed_er['sync'])/sig_scaling, 'min':limits['sync'][0], 'max':limits['sync'][1]}
			# p['f'+freq+'_monopole'] = {'center_value':r[0][5], 'sigma':np.min(sed_er['monopole'])/sig_scaling, 'min':limits['monopole'][0], 'max':limits['monopole'][1]}
			# p['f'+freq+'_dipole_x'] = {'center_value':r[0][6], 'sigma':np.min(sed_er['dipole_x'])/sig_scaling, 'min':limits['monopole'][0], 'max':limits['monopole'][1]}
			# p['f'+freq+'_dipole_y'] = {'center_value':r[0][7], 'sigma':np.min(sed_er['dipole_y'])/sig_scaling, 'min':limits['monopole'][0], 'max':limits['monopole'][1]}
			# p['f'+freq+'_dipole_z'] = {'center_value':r[0][8], 'sigma':np.min(sed_er['dipole_z'])/sig_scaling, 'min':limits['monopole'][0], 'max':limits['monopole'][1]}
			if inc_disc:
				p['f'+freq+'_disc'] = {'center_value':np.max( np.array([0.,r[0][9]])), 'sigma':np.min(sed_er['disc'])/sig_scaling, 'min':limits['disc'][0], 'max':limits['disc'][1]}
			if inc_bubbles:
				p['f'+freq+'_bubbles'] = {'center_value':np.max( np.array([0.,sed['bubbles'][ifreq]])), 'sigma':np.min(sed_er['bubbles'])/sig_scaling, 'min':limits['bubbles'][0], 'max':limits['bubbles'][1]}

		ppp = {}
		for key in p.keys():
			print key, '\t', p[key]['center_value'], '\t', p[key]['sigma']
			ppp[key] = p[key]['center_value']

		info_message( 'Starting point chi2: '+str(setup.fg_chisq(ppp) ) )

	# sys.exit()
	# ------ Run MCMC ------
	mcmc = MCMC()
	if run_mcmc:
		bf, ch = mcmc.sampler( p, setup.fg_chisq, nsamples=nsamples, seed=seed, output_tag=tag,
			temperature=1., accept_first=False, update_covmat=False )
Exemple #46
0
clf()
xn,yn,dxn,dyn=pyquad.profile(ang.flatten(),cormc.flatten(),0.001,50,200,plot=True,dispersion=False)

clf()
imshow(abs(cormc),interpolation='nearest',vmin=0,vmax=0.2)
title('Map Correlation Matrix')
colorbar()



clf()
errorbar(xn,yn,xerr=dxn,yerr=dyn,fmt='bo')
from Homogeneity import SplineFitting
spl=SplineFitting.MySplineFitting(xn,yn,dyn,100)
#plot(xn,spl(xn),'r',lw=3)
plot(xn,xn*0,'k--')
ylabel('Noise Correlation Matrix')
xlabel('Angle [degrees]')




sol2 = pcg(P_packed.T * P_packed, P_packed.T(tod*0+1), M=DiagonalOperator(1/coverage[~mask]), disp=True)
errmap = unpack(sol2['x'])
hp.gnomview(errmap,rot=[0,90],reso=14)
mm=np.zeros(12*nside**2)
mm[maskok]=sqrt(diag(covmc))
hp.gnomview(mm,rot=[0,90],reso=14)


Exemple #47
0
# Produce the Time-Ordered data
tod = H(input_map)

# map-making
coverage = P.T(np.ones_like(tod))
mask = coverage < 10
P.matrix.pack(mask)
P_packed = ProjectionInMemoryOperator(P.matrix)
unpack = UnpackOperator(mask)
solution = pcg(P_packed.T * P_packed, P_packed.T(tod), M=DiagonalOperator(1/coverage[~mask]), disp=True)
output_map = unpack(solution['x'])

# some display
output_map[mask] = np.nan
hp.gnomview(input_map, rot=[0,90], reso=0.3, xsize=600,title='Input Map')
hp.gnomview(output_map, rot=[0,90], reso=0.3, xsize=600,title='Output Map')

# get theta
iprings=np.arange(12*nside**2)
vecs=hp.pix2vec(int(nside),iprings[~mask])
vec0=hp.pix2vec(int(nside),0)
angles=np.arccos(np.dot(np.transpose(vec0),vecs))
themap=output_map[~mask]
clf()
plot(angles*180/np.pi,themap,'b.')
xlim(0,10)
yscale('log')

from Homogeneity import fitting
Exemple #48
0
themapi[mask]=0
themapi[~mask]+=np.random.randn(len(themapi[~mask]))*signoise
themapi[~mask]-=np.mean(themapi[~mask])
themapq=mapq.copy()
themapq[mask]=0
themapq[~mask]+=np.random.randn(len(themapq[~mask]))*signoise
themapq[~mask]-=np.mean(themapq[~mask])
themapu=mapu.copy()
themapu[mask]=0
themapu[~mask]+=np.random.randn(len(themapu[~mask]))*signoise
themapu[~mask]-=np.mean(themapu[~mask])
themaps=[themapq,themapu]

hp.mollview(mapq,rot=[0,90],title='Q')
hp.mollview(mapu,rot=[0,90],title='U')
hp.gnomview(themaps[0],rot=[0,90],reso=10,title='Q')
hp.gnomview(themaps[1],rot=[0,90],reso=10,title='U')

covmap=np.identity(len(ipok)*len(themaps))*signoise**2
guess=[0,binspec[:,1],binspec[:,2],binspec[:,3],0]
specout,error,invfisher,lk,num,ds_dcb,conv=qml.qml(themaps,mask,covmap,ellbins,fwhmrad,guess,ds_dcb,spectra,cholesky=True,temp=False,polar=True,plot=True,itmax=20)




#### TEB
ds_dcb=0
nbpixok=2000
mask=(np.arange(12*nside**2) >= nbpixok)
maskok=~mask
ip=np.arange(12*nside**2)

#kSZ_cat_file = 'data/FlenderS_sims/catalog_fullsky_zsm1_subsample.dat'
kSZ_cat_file = 'data/FlenderS_sims/catalog_fullsky_zsm1_subsample_mass_2e14.dat'
kSZ_sim_file = 'data/FlenderS_sims/ksz_mod3_R13.fits.gz'
kSZ_cat = np.loadtxt(kSZ_cat_file, usecols = [0,1,3,6,8])
kSZ_ra, kSZ_dec, kSZ_M200, kSZ_z, kSZ_vlos = zip(*kSZ_cat)

kSZ = H.read_map('data/FlenderS_sims/ksz_mod3_R13.fits.gz')

sys.exit()
kSZ_dic = {}
reqd_res_arcmins = 0.5
boxsize = 50 #arcmins
xs = ys = boxsize / reqd_res_arcmins #50 X 50 arcmin box
for cnt,(r, d, M, z, v) in enumerate(kSZ_cat):
	keyname = (round(r,4), round(d,4), round(M/1e14,3), round(z,3), round(v,3))
	print keyname, cnt
	MAP = H.gnomview(kSZ, rot = [r,d], reso=reqd_res_arcmins,xsize=xs,ysize=ys, coord='C', return_projected_map = 1)
	kSZ_dic[keyname] = MAP
	imshow(MAP);colorbar();show()#;sys.exit()

	#if cnt > 10:
	#	sys.exit(0)
sys.exit(0)
#op_file = 'data/FlenderS_sims/ksz_mod3_R13_extracted_M200_box50am_2.7e14_z_0.6_0.8.pkl.gz'
op_file = 'data/FlenderS_sims/ksz_mod3_R13_extracted_M200_box50am_2e14_z_0.6_0.8.pkl.gz'
pickle.dump(kSZ_dic, gzip.open(op_file,'wb'), protocol = 2)


# Combine CMB and dust. As output we have N 3-component maps of sky.
x0 = cmb_plus_dust(cmb, dust, Nbbands, nus)

# Simulate the TOD. Here we use Nf frequencies over the 150 GHz band
TOD, maps_convolved = a.get_observation(x0)

# Reconstruct CMB. Use Nsb sub-bands
maps_recon = a.tod2map(TOD, tol=tol)

# Get coverage maps. This returns Nf coverage maps
cov = a.get_coverage()

# We need coverages for Nsb sub-bands
cov = np.array([cov[(nus > nus_edge[i]) * (nus < nus_edge[i+1])].mean(axis=0) for i in xrange(Nsb)])

_max = [300, 5, 5]
for iband, (inp, rec, c) in enumerate(zip(maps_convolved, maps_recon, cov)):
    mp.figure(iband + 1)
    for i, (inp_, rec_, iqu) in enumerate(zip(inp.T, rec.T, 'IQU')):
        inp_[c < c.max() * 0.01] = hp.UNSEEN
        rec_[c < c.max() * 0.01] = hp.UNSEEN
        diff = inp_ - rec_
        diff[c < c.max() * 0.01] = hp.UNSEEN
        hp.gnomview(inp_, rot=center_gal, reso=5, xsize=700, fig=1,
            sub=(3, 3, i + 1), min=-_max[i], max=_max[i], title='Input convolved, {}, {:.0f} GHz'.format(iqu, nus[iband]))
        hp.gnomview(rec_, rot=center_gal, reso=5, xsize=700, fig=1,
            sub=(3, 3, i + 4), min=-_max[i], max=_max[i], title='Recon, {}, {:.0f} GHz'.format(iqu, nus[iband]))
        hp.gnomview(diff, rot=center_gal, reso=5, xsize=700, fig=1,
            sub=(3, 3, i+7), min=-_max[i], max=_max[i], title='Diff, {}, {:.0f} GHz'.format(iqu, nus[iband]))

mp.show()
import numpy as np
import healpy as hp
import matplotlib.pylab as plt

import glob

files = glob.glob('Chips/*.npz')

for fname in files:
    data = np.load(fname)

    mapv = data['metricValues']
    mapv[data['mask']] = hp.UNSEEN

    cbarFormat='%i'

    hp.gnomview(mapv, rot=(5.5,-5,0), xsize=300, ysize=300, title='',cbar=False)

    ax = plt.gca()
    im = ax.get_images()[0]
    cb = plt.colorbar(im, shrink=0.75, aspect=25, orientation='horizontal',
                      extend='both', extendrect=True, format=cbarFormat)

    cb.set_label('Number of Observations')

    cb.solids.set_edgecolor("face")

    plt.savefig(fname[:-4]+'_reproject.pdf')
def plot_Pixel_Halos_Surface_Brightness(Halo_data,Input_Para,Output_Para):

    from XCat_Objects               import DtoR, RtoD
    rc('font',family='serif')
    fdir  = './Output/plots/HEALPixSurfaceBrightness/'

    nside = Output_Para.nside

    Sl_n  = len(Halo_data)

    from XCat_Objects  import Halo_Brightness_Surface_Sample_Object

    for k in range(Sl_n):

      pix     = zeros(12*nside**2)

      if (len(Halo_data[k].RA[:]) == 0):
         pass 
      else:
         for halo_n in range(len(Halo_data[k].RA[:])):
            Halo_Sample     = Halo_Brightness_Surface_Sample_Object(Halo_data[k],Input_Para,Output_Para,halo_n)
            for i in range(len(Halo_Sample.sRA[:])):
               j    = hp.ang2pix(nside,DtoR*(90.0-Halo_Sample.sDEC[i]),DtoR*(Halo_Sample.sRA[i]))
               pix[j] += Halo_Sample.sSB[i]
         if (Output_Para.log_scale):
            pix_min = max(pix[:])/10.0**4
            pix = log10((pix+pix_min)*4.0*pi/(12.0*nside**2))
         else:
            pix = pix/(12.0*nside**2)
         plt.clf()
         hp.gnomview(pix, fig=1 , rot=(Halo_Sample.RA,Halo_Sample.DEC,0.0) , xsize=Output_Para.xsize , reso=Output_Para.resolution , degree=Output_Para.resol_degree)
         hp.graticule()
         plt.show()
         plt.close()

         save_ques = Read_YN_Input("Do you want to save the plot (please enter Y, y, N, or n)? ")
         if save_ques :
           rc('text',usetex=True)
           plt.clf()
           hp.gnomview(pix, fig = 1 ,rot=(Halo_Sample.RA,Halo_Sample.DEC,0.0),xsize = Output_Para.xsize)
           hp.graticule()
           if (Output_Para.log_scale):
              fname = 'Surface_Brightness_Profile_log_%i_%i.pdf'%(nside,(k+1))
           else:
              fname = 'Surface_Brightness_Profile_%i_%i.pdf'%(nside,(k+1))
           plt.title(r'Surface Brightness Profile at redshift between %0.3f and %0.3f'%(Halo_data[k].z_min,Halo_data[k].z_max),fontsize = 20)
           print 'Saving plot', fname
#         savefig(fdir+fname,bbox_inches='tight')
           plt.savefig(fdir+fname)
           rc('text',usetex=False)
           plt.close()

         save_ques = Read_YN_Input("Do you want to save the map data (please enter Y, y, N, or n)? ")
         if save_ques :
           if (Output_Para.log_scale):
              fname = 'Surface_Brightness_Profile_log_HEALPix_map_%i_%i.txt'%(nside,(k+1))
           else:
              fname = 'Surface_Brightness_Profile_HEALPix_map_%i_%i.txt'%(nside,(k+1))
           print 'Saving data', fname
           f = open(fdir+fname,'w')
           f.write("#   Pix_Value \n")
           savetxt(f, array([pix]).T)
           f.close()
###############

reso = 10


close("all")

Q = figure(1)
Q.canvas.set_window_title("Q polarization")
[
    (
        hp.gnomview(
            (x0_convolved[i, :, 1]).T,
            sub=(Nbbands, 3, 3 * i + 1),
            title="convolved",
            rot=center,
            reso=reso,
            min=-3,
            max=3,
        ),
        hp.gnomview(
            (maps[i, :, 1]).T, sub=(Nbbands, 3, 3 * i + 2), title="reconstructed", rot=center, reso=reso, min=-3, max=3
        ),
        hp.gnomview(
            (res[i, :, 1]).T, sub=(Nbbands, 3, 3 * i + 3), title="residual", rot=center, reso=reso, min=-1, max=1
        ),
    )
    for i in range(Nbbands)
]

Exemple #54
0
# compute apodization mask
nside = 256
lmin = 20
lmax = 2*nside-1
delta_ell = 20
racenter = 0.0
deccenter = -57.0
maxang = 20.

center = equ2gal(racenter, deccenter)
veccenter = hp.ang2vec(pi/2-np.radians(center[1]), np.radians(center[0]))
vecpix = hp.pix2vec(nside, np.arange(12*nside**2))
cosang = np.dot(veccenter, vecpix)
maskok = np.degrees(np.arccos(cosang)) < maxang
maskmap = apodize_mask(maskok, 5)
hp.gnomview(maskmap, rot=[racenter, deccenter], coord=['G', 'C'], reso=15)


# Xpol estimation through MC
np.random.seed(0)
xpol = Xpol(maskmap, lmin, lmax, delta_ell)
ell_binned = xpol.ell_binned
spectra_binned = xpol.bin_spectra(spectra)
nbins = len(ell_binned)

nbmc = 100
allclsout = np.zeros((nbmc, 6, nbins))
allcls = np.zeros((nbmc, 6, lmax+1))
bar = progress_bar(nbmc)
for i in np.arange(nbmc):
    maps = hp.synfast(spectra, nside, fwhm=0, pixwin=True, new=True,
Exemple #55
0
healpy.mollview(m, min=0, max=m.max(), title='Mollview RING', nest=False)
show()

#! http://lambda.gsfc.nasa.gov/data/map/dr4/skymaps/7yr/raw/wmap_band_imap_r9_7yr_W_v4.fits

filename = 'wmap_band_imap_r9_7yr_W_v4.fits'
#filename = '/global/scratch/sd/planck/user/zonca/healpytut/wmap_band_imap_r9_7yr_W_v4.fits'
m = healpy.read_map(filename) #by default converts to RING!!
healpy.mollview(m, title='Histogram equalized', nest=False, norm='hist')
show()
m = healpy.read_map(filename, nest=True) #keeps nested
healpy.mollview(m, coord=['G','E'], title='Linear scale', unit='mK', nest=True, min=-1,max=1, xsize=2000) #xsize increases resolution
healpy.graticule()
show()

healpy.gnomview(m, rot=[0,0.3], title='Linear scale', unit='mK', format='%.2g', nest=True)
show()

print(healpy.fit_dipole(m, gal_cut=20)) # degrees

#!Smoothing
#!~~~~~~~~~

m_smoothed = healpy.smoothing(m, fwhm=60, arcmin=True)
healpy.mollview(m_smoothed, min=-1, max=1, title='Map smoothed 1 deg')

#!Rotator
#!~~~~~~~

rot = healpy.Rotator(coord=['G','E'])
theta_gal, phi_gal = np.pi/2., 0.
Exemple #56
0
        idx = numpy.abs(distance_modulus_array - distance_modulus).argmin()
        print title, distance_modulus, distance_modulus_array[idx]

        if target['coord'] == 'CEL':
            glon, glat = ugali.utils.projector.celToGal(target['lon'],target['lat'])
        elif target['coord'] == 'GAL':
            glon, glat = target['lon'],target['lat']
        else:
            raise Exception('...')

        plt.ioff()
        fig = plt.figure(figsize=(6,6))
        xsize = 1000
        reso = 60. * 2. * target['radius'] / xsize # Deg to arcmin

        label_kwargs = dict(xy=(0.05,0.05),xycoords='axes fraction', xytext=(0, 0), 
                            textcoords='offset points',ha='left', va='bottom',size=10,
                            bbox={'boxstyle':"round",'fc':'1'}, zorder=10)

        slices = [(ts,'TS'), (stellar,'Stellar'), (frac,'Fraction'), (richness,'Richness')]
        map = blank
        for i,(data,label) in enumerate(slices):
            map[pix] = data[idx] if data.ndim > 1 else data
            img = healpy.gnomview(map, rot=[glon, glat], xsize=xsize, reso=reso,
                            title='',sub=[2,2,i+1],notext=True)
            fig.gca().annotate(label,**label_kwargs)

        fig.suptitle(title,bbox={'boxstyle':"round",'fc':'1'}, zorder=10)
        plt.draw()
        plt.savefig(name+'_results.png',bbox_inches='tight')
#hp.gnomview(mapdust-mm, coord='GE', title='Planck Dust Map', rot=[racenter,deccenter], fig=1, reso=25,min=0,max=1e-4)
hp.mollview(mapdust-mm, coord='GE', title='Planck Dust Map', rot=[racenter,deccenter], fig=2, min=0,max=1e-4)
hp.projplot(pi/2-np.radians(deccenter), np.radians(racenter), 'kx')
hp.projplot(pi/2-np.radians(deccirc), np.radians(racirc), 'k')
hp.projplot(pi/2-np.radians(deccirc2), np.radians(racirc2), 'k')
hp.projplot(pi/2-np.radians(deccirc3), np.radians(racirc3), 'k')
hp.projplot(pi/2-np.radians(-45), np.radians(4*15+40/60+12/3600), 'ko')
hp.projplot(pi/2-np.radians(-30/60), np.radians(11*15+453/60+0/3600), 'ko')
hp.projplot(pi/2 - np.radians(-32-48/60), np.radians(23*15+1/60+48/3600), 'ko')
bicepalpha = np.ravel([np.zeros(10)-60, np.linspace(-60, 60, 10), np.zeros(10)+60, np.linspace(60, -60, 10)])
bicepdelta = np.ravel([np.linspace(-70, -45, 10),np.zeros(10)-45, np.linspace(-45, -70, 10),np.zeros(10)-70])
hp.projplot(pi/2-np.radians(bicepdelta), np.radians(bicepalpha), 'k--')

# Initial 143 GHz map 
clf()
hp.gnomview(init143, coord='GE', title='Planck 143 GHz Map', rot=[racenter,deccenter], fig=2, reso=25,min=-5e-4,max=5e-4)
hp.projplot(pi/2-np.radians(deccenter), np.radians(racenter), 'kx')
hp.projplot(pi/2-np.radians(deccirc), np.radians(racirc), 'k')
hp.projplot(pi/2-np.radians(deccirc2), np.radians(racirc2), 'k')
hp.projplot(pi/2-np.radians(deccirc3), np.radians(racirc3), 'k')
hp.projplot(pi/2-np.radians(-45), np.radians(4*15+40/60+12/3600), 'ko')
hp.projplot(pi/2-np.radians(-30/60), np.radians(11*15+453/60+0/3600), 'ko')
hp.projplot(pi/2 - np.radians(-32-48/60), np.radians(23*15+1/60+48/3600), 'ko')
bicepalpha = np.ravel([np.zeros(10)-60, np.linspace(-60, 60, 10), np.zeros(10)+60, np.linspace(60, -60, 10)])
bicepdelta = np.ravel([np.linspace(-70, -45, 10),np.zeros(10)-45, np.linspace(-45, -70, 10),np.zeros(10)-70])
hp.projplot(pi/2-np.radians(bicepdelta), np.radians(bicepalpha), 'k--')


##################### DUST/CMB mollview
mp.clf()
mn = -1
def display(x, title, lim=200):
    x = x.copy()
    x[~mask] = np.nan
    hp.gnomview(x, rot=center_gal, reso=5, xsize=600, min=-lim, max=lim,
                title=title)
Exemple #59
0
def gnomview_template(template, rm):
    hp.gnomview(template*1e6,rot=(8.+np.median(rm['phi_gal'])*180./np.pi, np.median(rm['b_gal'])*180./np.pi, 0), xsize=1000, ysize=1000,min=-2,max=2,reso=0.5, unit='uK')
Exemple #60
0
def display(input, msg,center=False,lim=[200, 10, 10],reso=5,mask=None):
    for i, (kind, lim) in enumerate(zip('IQU', lim)):
        map = input[..., i].copy()
        if mask is not None: map[mask]=np.nan
        hp.gnomview(map, rot=center, reso=reso, xsize=400, min=-lim, max=lim,
                    title=msg + ' ' + kind, sub=(1, 3, i+1))