def inside(self, xs, ys, bins=None): xys = np.array([xs,ys]).T verts = self.verts if bins is None: bins = self.bins path = mplpa.Path(verts) if bins is None: mask = path.contains_points(xys) ind = np.nonzero(mask)[0] else: minx,maxx=verts[:,0].min(),verts[:,0].max() miny,maxy=verts[:,1].min(),verts[:,1].max() hh,pos= quick_hist.quick_hist(xys.T, nbins = [bins]*2, range=[[minx,maxx],[miny,maxy]],getPos=True) xbincens = np.linspace(minx,maxx,bins+1,True) ybincens = np.linspace(miny, maxy, bins+1,True) xbincens = .5*(xbincens[1:]+xbincens[:-1]) ybincens = .5*(ybincens[1:]+ybincens[:-1]) xbincens = xbincens[:,None] + ybincens[None,:]*0 ybincens = ybincens[None,:] + xbincens[:,None]*0 xybincens = np.array([xbincens.flatten(),ybincens.flatten()]) mask = path.contains_points(xybincens.T) mask = mask[pos] & (pos>=0) return mask
def inside(self, xs, ys, bins=None): xys = np.array([xs, ys]).T verts = self.verts if bins is None: bins = self.bins path = mplpa.Path(verts) if bins is None: mask = path.contains_points(xys) ind = np.nonzero(mask)[0] else: minx, maxx = verts[:, 0].min(), verts[:, 0].max() miny, maxy = verts[:, 1].min(), verts[:, 1].max() hh, pos = quick_hist.quick_hist(xys.T, nbins=[bins] * 2, range=[[minx, maxx], [miny, maxy]], getPos=True) xbincens = np.linspace(minx, maxx, bins + 1, True) ybincens = np.linspace(miny, maxy, bins + 1, True) xbincens = .5 * (xbincens[1:] + xbincens[:-1]) ybincens = .5 * (ybincens[1:] + ybincens[:-1]) xbincens = xbincens[:, None] + ybincens[None, :] * 0 ybincens = ybincens[None, :] + xbincens[:, None] * 0 xybincens = np.array([xbincens.flatten(), ybincens.flatten()]) mask = path.contains_points(xybincens.T) mask = mask[pos] & (pos >= 0) return mask
def __doit2d(x, y, hi1=None, hi2=None, thresh=None): hhs = {} for curhi in range(hi1, hi2 + 1): hh = quick_hist.quick_hist((x, y), range=((0, 1), (0, 1)), nbins=[2**curhi, 2**curhi]) hhs[curhi] = hh pixcen = [] hh0 = hhs[hi2] * 1 # accumulator of the result area = hh0 * 0 two = 2 poiss = scipy.stats.poisson(thresh) DeepenOrNot = lambda x: random.random() < poiss.cdf(x) #DeepenOrNot = lambda x: x>thresh def doitit(it, i, j): curhhs = hhs[it] if it == hi2: hh[i:i + 2, j:j + 2] = curhhs[i:i + 2, j:j + 2] area[i:i + 2, j:j + 2] = 2**(-it) pixcen.append((i + .5, j + .5)) else: for ii in range(i, i + 2): for jj in range(j, j + 2): curval = curhhs[ii, jj] if DeepenOrNot(curval): doitit(it + 1, ii * two, jj * two) else: dx = 2**(hi2 - it) hh0[ii * dx:(ii + 1) * dx, jj * dx:(jj + 1) * dx] = curval area[ii * dx:(ii + 1) * dx, jj * dx:(jj + 1) * dx] = 2**(-it) pixcen.append( (ii * dx + dx / 2. + 0.5, jj * dx + dx / 2. + 0.5)) n1 = 2**hi1 dn = 2**(hi2 - hi1) for i in range(n1): for j in range(n1): if DeepenOrNot(hhs[hi1][i, j]): doitit(hi1 + 1, i * two, j * two) else: hh0[i * dn:(i + 1) * dn, j * dn:(j + 1) * dn] = hhs[hi1][i, j] area[i * dn:(i + 1) * dn, j * dn:(j + 1) * dn] = 2**(-hi1) pixcen.append((i * dn + dn / 2. + 0.5, j * dn + dn / 2. + 0.5)) area = (2**hi1 * area)**2 # in smallest pixels squared return hh0 * 1. / area, pixcen, area
def __doit2d(x, y, hi1=None, hi2=None, thresh=None): hhs ={} for curhi in range(hi1,hi2+1): hh = quick_hist.quick_hist((x, y), range=((0, 1), (0, 1)), nbins=[2**curhi,2**curhi]) hhs[curhi]=hh pixcen = [] hh0 = hhs[hi2] * 1 # accumulator of the result area = hh0 * 0 two = 2 poiss = scipy.stats.poisson(thresh) DeepenOrNot = lambda x: random.random() < poiss.cdf(x) #DeepenOrNot = lambda x: x>thresh def doitit(it, i, j): curhhs = hhs[it] if it==hi2: hh[i:i+2, j:j+2] = curhhs[i:i+2, j:j+2] area[i:i+2, j:j+2] = 2**(-it) pixcen.append((i+.5,j+.5)) else: for ii in range(i, i+2): for jj in range(j, j+2): curval = curhhs[ii,jj] if DeepenOrNot(curval): doitit(it+1, ii*two, jj*two) else: dx=2**(hi2-it) hh0[ii*dx:(ii+1)*dx, jj*dx:(jj+1)*dx]=curval area[ii*dx:(ii+1)*dx, jj*dx:(jj+1)*dx] = 2**(-it) pixcen.append((ii*dx+dx/2.+0.5,jj*dx+dx/2.+0.5)) n1 = 2**hi1 dn = 2**(hi2-hi1) for i in range(n1): for j in range(n1): if DeepenOrNot(hhs[hi1][i,j]): doitit(hi1+1,i*two,j*two) else: hh0[i*dn:(i+1)*dn, j*dn:(j+1)*dn] = hhs[hi1][i,j] area[i*dn:(i+1)*dn, j*dn:(j+1)*dn] = 2**(-hi1) pixcen.append((i*dn+dn/2.+0.5,j*dn+dn/2.+0.5)) area = (2**hi1*area)**2 # in smallest pixels squared return hh0*1./area,pixcen,area
def __doit1d(x, hi1=None, hi2=None, thresh=None): hhs = {} for curhi in range(hi1, hi2 + 1): hh = quick_hist.quick_hist((x, ), range=((0, 1), ), nbins=[2**curhi]) hhs[curhi] = hh hh0 = hhs[hi2] * 1 # accumulator of the result area = hh0 * 0 two = 2 poiss = scipy.stats.poisson(thresh) DeepenOrNot = lambda x: random.random() < poiss.cdf(x) #DeepenOrNot = lambda x: x>thresh def doitit(it, i): curhhs = hhs[it] if it == hi2: hh[i:i + 2] = curhhs[i:i + 2] area[i:i + 2] = 2**(-it) else: for ii in range(i, i + 2): curval = curhhs[ii] if DeepenOrNot(curval): doitit(it + 1, ii * two) else: dx = 2**(hi2 - it) hh0[ii * dx:(ii + 1) * dx] = curval area[ii * dx:(ii + 1) * dx] = 2**(-it) n1 = 2**hi1 dn = 2**(hi2 - hi1) for i in range(n1): if DeepenOrNot(hhs[hi1][i]): doitit(hi1 + 1, i * two) else: hh0[i * dn:(i + 1) * dn] = hhs[hi1][i] area[i * dn:(i + 1) * dn] = 2**(-hi1) return hh0 * 1. / (2**hi1 * area)
def __doit1d(x, hi1=None, hi2=None, thresh=None): hhs ={} for curhi in range(hi1,hi2+1): hh = quick_hist.quick_hist((x,), range=((0, 1),), nbins=[2**curhi]) hhs[curhi]=hh hh0 = hhs[hi2] * 1 # accumulator of the result area = hh0 * 0 two = 2 poiss = scipy.stats.poisson(thresh) DeepenOrNot = lambda x: random.random() < poiss.cdf(x) #DeepenOrNot = lambda x: x>thresh def doitit(it, i): curhhs = hhs[it] if it==hi2: hh[i:i+2] = curhhs[i:i+2] area[i:i+2] = 2**(-it) else: for ii in range(i, i+2): curval = curhhs[ii] if DeepenOrNot(curval): doitit(it+1, ii*two) else: dx=2**(hi2-it) hh0[ii*dx:(ii+1)*dx]=curval area[ii*dx:(ii+1)*dx] = 2**(-it) n1 = 2**hi1 dn = 2**(hi2-hi1) for i in range(n1): if DeepenOrNot(hhs[hi1][i]): doitit(hi1+1,i*two) else: hh0[i*dn:(i+1)*dn] = hhs[hi1][i] area[i*dn:(i+1)*dn] = 2**(-hi1) return hh0*1./(2**hi1*area)
def inside(xs, ys, verts, bins=None): """ Check if points xs,ys are inside a mask specified by an array of vertices Nx2""" xys = np.array([xs, ys]).T path = mplpa.Path(verts) if bins is None: mask = path.contains_points(xys) ind = np.nonzero(mask)[0] else: if hasattr(bins, '__iter__'): if len(bins) == 2: nbins = bins else: raise Exception( 'The bins parameter must be a scalar or a tuple with 2 elements' ) else: nbins = [bins, bins] minx, maxx = verts[:, 0].min(), verts[:, 0].max() miny, maxy = verts[:, 1].min(), verts[:, 1].max() hh, pos = quick_hist.quick_hist(xys.T, nbins=nbins, range=[[minx, maxx], [miny, maxy]], getPos=True) xbincens = np.linspace(minx, maxx, bins + 1, True) ybincens = np.linspace(miny, maxy, bins + 1, True) xbincens = .5 * (xbincens[1:] + xbincens[:-1]) ybincens = .5 * (ybincens[1:] + ybincens[:-1]) xbincens = xbincens[:, None] + ybincens[None, :] * 0 ybincens = ybincens[None, :] + xbincens[:, None] * 0 xybincens = np.array([xbincens.flatten(), ybincens.flatten()]) mask = path.contains_points(xybincens.T) mask = mask[pos] & (pos >= 0) return mask
def healmap(ras, decs, ramin=0, ramax=360, decmin=-90, decmax=90, nside=64, xflip=False, vmax=None, vmin=None, cmap=plt.cm.gray_r, linewidth=1, weights=None, wrap_angle=None, skip_empty=True, weight_norm=False, rasterized=True): """ Make the 2D histogram of the datapoints on the sky using the Healpix pixels """ plt.ioff() nel = 12 * nside**2 ipix = healpy.ang2pix(nside, numpy.deg2rad(decs)+numpy.pi*0.5, numpy.deg2rad(ras)) hh = quick_hist.quick_hist((ipix,), nbins=[nel], range=[[0, nel]],weights=weights) hhsum = quick_hist.quick_hist((ipix,), nbins=[nel], range=[[0, nel]]) pixarea = (4*numpy.pi*(180/numpy.pi)**2)/nel # in sq. deg hh = hh / pixarea hhsum = hhsum / pixarea xloc = numpy.arange(nel) verts = [bovy_healpy.pix2vert(nside, xx) for xx in xloc] collist = [] fac = 180/numpy.pi if wrap_angle is None: wrap_angle = 2 * numpy.pi else: wrap_angle = numpy.deg2rad(wrap_angle) for ii, v in enumerate(verts): if skip_empty and hhsum[ii] == 0: continue b,a = v.T if (a.max()-a.min())>(numpy.pi): a = ((a + (numpy.pi - a[0])) % (2 * numpy.pi)) - (numpy.pi-a[0]) if a.mean() < 0: a = (a + 2 * numpy.pi) if a.mean() > wrap_angle: a -= 2 * numpy.pi xys = numpy.array([a * fac, b * fac - 90]).T curpoly = matplotlib.patches.Polygon(xys, closed=True) collist.append(curpoly) raranges = [ramin,ramax] if xflip: raranges = raranges[::-1] plot([-1], xr=raranges, yr=[decmin,decmax]) ax = plt.gca() tmppol = matplotlib.patches.Polygon(numpy.array(([0, 360, 360, 0], [-90, -90, 90, 90])).T,closed=True) tmpcoll = matplotlib.collections.PatchCollection([tmppol], linewidths=0.01, edgecolors='black', facecolors='black') tmpcoll.set_array(numpy.array([0])) coll = matplotlib.collections.PatchCollection(collist, linewidths=linewidth) if vmin is None: vmin = 0 if vmax is None: vmax = hh.max() if weight_norm: hh = hh * 1. / (hhsum + 1 * (hhsum == 0)) if skip_empty: hh1 = hh[hhsum > 0] else: hh1 = hh coll.set_array(hh1) coll.set_cmap(cmap) coll.set_clim(vmin, vmax) coll.set_edgecolors(coll.cmap(coll.norm(hh1))) coll.set_rasterized(rasterized) tmpcoll.set_clim(vmin, vmax) ax.add_collection(coll) return coll
def tvhist2d (x, y, xmin=None, xmax=None, ymin=None, ymax=None, vmin=None, vmax=None, bins=[100,100], xtitle="", ytitle="", title=None, noerase=False, weights=None, zlog=False, xflip=False, yflip=False, smooth=None, quick=False, cmap='gray_r', normx=None, normy=None, xlog=False, ylog=False, weight_norm=False, vminfrac=None, vmaxfrac=None, position=None, xaxis_formatter=None,yaxis_formatter=None, bar=False, bar_label='', bar_fraction=0.05, bar_pad=0.05, bar_ticks_locator=None, bar_formatter=None, apply_func = None, zsqrt=False, ret_hist=False, interpolation='nearest', scatter_thresh=None, scatter_opt={}, subplot=None, **kw): """ Plot the 2D histogram of the data Example: >> tvhist2d(xs,ys,bins=[30,30]) >> tvhist2d(xs,ys,0,10,-1,2,bins=[30,30]) Keyword arguments: ----------------- xmin,xmax,ymin,ymax the ranges for x,y where the histogram is constructed. These params can be specified not only as keywords but also as normal arguments >> tvhist2d(xs,ys,xmin=0,ymin=10,ymin=-1,ymax=2,bins=[30,30]) >> tvhist2d(xs,ys,0,10,-1,2,bins=[30,30]) vmin,vmax the ranges of intensities shown on the plot bins the list of two integers specifying how many bins in x,y you want smooth if not None this parameter controls additional smoothing of the 2D histogram xflip, yflip boolean parameters allowing to flip x,y axes normx, normy params controlling the normalization of the histogram along X or Y axes if normx=='sum' then the normalization is done in such way that the sum is the same for each row/column if normx=='max' then the normalization is don in such way that the brightest pixel value will be the same for each row/column weight_norm if True the value in each bin is mean weight of points within the bin bar boolean parameter for switching on/off the plotting of the color-bar bar_pad, bar_fraction padding and fraction for bar placement bar_ticks_locator locator for the tickmarks on the colorbar """ x1 = listToArrFlat(x) y1 = listToArrFlat(y) #ind = numpy.isfinite(x1) & numpy.isfinite(y1) if xmin is None: xmin = numpy.nanmin(x1) if ymin is None: ymin = numpy.nanmin(y1) if xmax is None: xmax = numpy.nanmax(x1) if ymax is None: ymax = numpy.nanmax(y1) range1 = (xmin, xmax, ymin, ymax) if xlog is True: x1=numpy.log10(x1) xmin,xmax=numpy.log10(xmin),numpy.log10(xmax) if ylog is True: y1=numpy.log10(y1) ymin,ymax=numpy.log10(ymin),numpy.log10(ymax) range = [[ymin, ymax],[xmin, xmax]] binsRev = bins[::-1] if not quick: hh, yedges, xedges = scipy.histogram2d(y1, x1, range=range, bins=binsRev, weights=weights) if weight_norm: hh1, yedges, xedges = scipy.histogram2d(y1, x1, range=range, bins=binsRev, weights=None) hh = hh*1./hh1 else: import quick_hist hh = quick_hist.quick_hist((y1, x1), range=range, nbins=binsRev, weights=weights) if weight_norm: hh1 = quick_hist.quick_hist((y1, x1), range=range, nbins=binsRev) hh = hh*1./hh1 if apply_func is not None: hh = apply_func (hh) if normx is not None: if normx == 'sum': hh = hh*1./hh.sum(axis=0)[numpy.newaxis,:]#/numpy.maximum(hh.sum(axis=0),1)[numpy.newaxis,:] elif normx == 'max': hh = hh*1./numpy.max(hh,axis=0)[numpy.newaxis,:] else: raise Exception('unknown normx mode') if normy is not None: if normy == 'sum': hh = hh*1./hh.sum(axis=1)[:,numpy.newaxis]#/numpy.maximum(hh.sum(axis=1),1)[:,numpy.newaxis] elif normy == 'max': hh = hh*1./numpy.max(hh,axis=1)[:,numpy.newaxis] else: raise Exception('unknown normy mode') if scatter_thresh is not None: locx = np.linspace(xmin,xmax, bins[0]+1, True) locy = np.linspace(ymin, ymax, bins[1]+1, True) posx = np.digitize(x1, locx) posy = np.digitize(y1, locy) #select points within the histogram ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1]) hhsub = hh.T[posx[ind] - 1, posy[ind] - 1] # values of the histogram where the points are x1 = x1[ind][hhsub < scatter_thresh] # low density points y1 = y1[ind][hhsub < scatter_thresh] hh[hh < scatter_thresh] = np.nan # fill the areas with low density by NaNs if xflip: range1 = (range1[1], range1[0], range1[2], range1[3]) hh = numpy.fliplr(hh) if yflip: range1 = (range1[0], range1[1], range1[3], range1[2]) hh = numpy.flipud(hh) if subplot is not None: noerase=True plt.subplot(subplot) if not noerase: plt.gcf().clf() if position is not None: mypos=position[:] mypos[2]=position[2]-position[0] mypos[3]=position[3]-position[1] plt.axes(mypos) axis = plt.gca() axis.set_xlabel(xtitle) axis.set_ylabel(ytitle) axis.minorticks_on() if xaxis_formatter is not None: axis.xaxis.set_major_formatter(xaxis_formatter) if yaxis_formatter is not None: axis.yaxis.set_major_formatter(yaxis_formatter) if smooth is not None: hh = scipy.ndimage.filters.gaussian_filter(hh, [smooth, smooth]) if vminfrac is not None and vmin is None: vmin = scipy.stats.scoreatpercentile(hh.flat, 100 * vminfrac) if vmaxfrac is not None and vmax is None: vmax = scipy.stats.scoreatpercentile(hh.flat, 100 * vmaxfrac) if vmin is not None and vmax is not None and vmin>=vmax: warnings.warn("vmin is >= vmax... Resetting their values",RuntimeWarning) vmin=None vmax=None if zlog: norm = matplotlib.colors.LogNorm(vmin=vmin, vmax=vmax) elif zsqrt: norm = matplotlib.colors.SqrtNorm(vmin=vmin, vmax=vmax) else: norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax) axim=plt.imshow(hh, extent=range1, aspect='auto', interpolation=interpolation, cmap=cmap, norm=norm, origin='lower', **kw) if scatter_thresh is not None: if 'ps' not in scatter_opt: scatter_opt=copy.copy(scatter_opt) scatter_opt['ps']=3 oplot(x1,y1,**scatter_opt) if bar: if bar_formatter is None: bar_formatter = matplotlib.ticker.ScalarFormatter() if int(''.join((matplotlib.__version__).split('.')[:2]))>=11: kw={'use_gridspec':True} else: kw={} cb=plt.colorbar(fraction=bar_fraction, pad=bar_pad, norm=axim.norm, ax=axis, format=bar_formatter, ticks=bar_ticks_locator,**kw) cb.set_label(bar_label) if xlog: plt.gca().set_xscale('log') if ylog: plt.gca().set_yscale('log') if title is not None: plt.title(title) if ret_hist: return hh return axim
def healmap(ras, decs, ramin=0, ramax=360, decmin=-90, decmax=90, nside=64, xflip=False, vmax=None, vmin=None, cmap=plt.cm.gray_r, linewidth=1, weights=None, wrap_angle=None, skip_empty=True, weight_norm=False, rasterized=True): """ Make the 2D histogram of the datapoints on the sky using the Healpix pixels """ plt.ioff() nel = 12 * nside**2 ipix = healpy.ang2pix(nside, numpy.deg2rad(decs) + numpy.pi * 0.5, numpy.deg2rad(ras)) hh = quick_hist.quick_hist((ipix, ), nbins=[nel], range=[[0, nel]], weights=weights) hhsum = quick_hist.quick_hist((ipix, ), nbins=[nel], range=[[0, nel]]) pixarea = (4 * numpy.pi * (180 / numpy.pi)**2) / nel # in sq. deg hh = hh / pixarea hhsum = hhsum / pixarea xloc = numpy.arange(nel) verts = [bovy_healpy.pix2vert(nside, xx) for xx in xloc] collist = [] fac = 180 / numpy.pi if wrap_angle is None: wrap_angle = 2 * numpy.pi else: wrap_angle = numpy.deg2rad(wrap_angle) for ii, v in enumerate(verts): if skip_empty and hhsum[ii] == 0: continue b, a = v.T if (a.max() - a.min()) > (numpy.pi): a = ((a + (numpy.pi - a[0])) % (2 * numpy.pi)) - (numpy.pi - a[0]) if a.mean() < 0: a = (a + 2 * numpy.pi) if a.mean() > wrap_angle: a -= 2 * numpy.pi xys = numpy.array([a * fac, b * fac - 90]).T curpoly = matplotlib.patches.Polygon(xys, closed=True) collist.append(curpoly) raranges = [ramin, ramax] if xflip: raranges = raranges[::-1] plot([-1], xr=raranges, yr=[decmin, decmax]) ax = plt.gca() tmppol = matplotlib.patches.Polygon(numpy.array( ([0, 360, 360, 0], [-90, -90, 90, 90])).T, closed=True) tmpcoll = matplotlib.collections.PatchCollection([tmppol], linewidths=0.01, edgecolors='black', facecolors='black') tmpcoll.set_array(numpy.array([0])) coll = matplotlib.collections.PatchCollection(collist, linewidths=linewidth) if vmin is None: vmin = 0 if vmax is None: vmax = hh.max() if weight_norm: hh = hh * 1. / (hhsum + 1 * (hhsum == 0)) if skip_empty: hh1 = hh[hhsum > 0] else: hh1 = hh coll.set_array(hh1) coll.set_cmap(cmap) coll.set_clim(vmin, vmax) coll.set_edgecolors(coll.cmap(coll.norm(hh1))) coll.set_rasterized(rasterized) tmpcoll.set_clim(vmin, vmax) ax.add_collection(coll) return coll