The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.
Example 1
def plot_heatmap(ax, xpoints, ypoints, nbins, title=None, maxcount=None): ''' Plot a heatmap of the given data on on the given axes. ''' # imshow expects y,x for the image, but x,y for the extents, # so we have to manage that here... bins = np.concatenate( (np.arange(0,1.0,1.0/nbins), [1.0]) ) heatmap, yedges, xedges = np.histogram2d(ypoints, xpoints, bins=bins) extent = [xedges[0],xedges[-1], yedges[0], yedges[-1]] # make sure we always show the full extent of the tank and the full extent of the data, # whichever is wider. ax.set_xlim(min(0, xedges[0]), max(1, xedges[-1])) ax.set_ylim(min(0, yedges[0]), max(1, yedges[-1])) if title: ax.set_title(title) if maxcount is not None: norm = Normalize(0, maxcount) else: norm = None return ax.imshow(heatmap, extent=extent, cmap=plt.get_cmap('hot'), origin='lower', interpolation='nearest', norm=norm)
Example 2
def plot_density_map(x, y, xbins, ybins, Nlevels=4, cbar=True, weights=None): Z = np.histogram2d(x, y, bins=(xbins, ybins), weights=weights)[0].astype(float).T # central values lt = get_centers_from_bins(xbins) lm = get_centers_from_bins(ybins) cX, cY = np.meshgrid(lt, lm) X, Y = np.meshgrid(xbins, ybins) im = plt.pcolor(X, Y, Z, cmap=plt.cm.Blues) plt.contour(cX, cY, Z, levels=nice_levels(Z, Nlevels), cmap=plt.cm.Greys_r) if cbar: cb = plt.colorbar(im) else: cb = None plt.xlim(xbins[0], xbins[-1]) plt.ylim(ybins[0], ybins[-1]) try: plt.tight_layout() except Exception as e: print(e) return plt.gca(), cb
Example 3
def shot_heatmap(df,sigma = 1,log=False,player_pic=True,ax=None,cmap='jet'): ''' This function plots a heatmap based on the shot chart. input - dataframe with x and y coordinates. optional - log (default false) plots heatmap in log scale. player (default true) adds player's picture and name if true sigma - the sigma of the Gaussian kernel. In feet (default=1) ''' n,_,_ = np.histogram2d( 0.1*df['LOC_X'].values, 0.1*df['LOC_Y'].values,bins = [500, 500],range = [[-25,25],[-5.25,44.75]]) KDE = ndimage.filters.gaussian_filter(n,10.0*sigma) N = 1.0*KDE/np.sum(KDE) if ax is None: ax = plt.gca(xlim = [30,-30],ylim = [-7,43],xticks=[],yticks=[],aspect=1.0) court(ax,outer_lines=True,color='black',lw=2.0,direction='down') ax.axis('off') if log: ax.imshow(np.rot90(np.log10(N+1)),cmap=cmap,extent=[25.0, -25.0, -5.25, 44.75]) else: ax.imshow(np.rot90(N),cmap=cmap,extent=[25.0, -25.0, -5.25, 44.75]) if player_pic: player_id = df.PLAYER_ID.values[0] pic = players_picture(player_id) ax.imshow(pic,extent=[15,25,30,37.8261]) ax.text(0,-7,'By: Doingthedishes',color='white',horizontalalignment='center',fontsize=20,fontweight='bold')
Example 4
def photonsToFrame(photonposframe,imagesize,background): pixels = imagesize edges = range(0, pixels+1) # HANDLE CASE FOR NO PHOTONS DETECTED AT ALL IN FRAME if photonposframe.size == 0: simframe = _np.zeros((pixels, pixels)) else: xx = photonposframe[:, 0] yy = photonposframe[:, 1] simframe, xedges, yedges = _np.histogram2d(yy, xx, bins=(edges, edges)) simframe = _np.flipud(simframe) # to be consistent with render #simframenoise = noisy(simframe,background,noise) simframenoise = noisy_p(simframe, background) simframenoise[simframenoise > 2**16-1] = 2**16-1 simframeout = _np.round(simframenoise).astype('<u2') return simframeout
Example 5
def mask_locs(self): locs = self.locs[0] steps_x = len(self.xedges) steps_y = len(self.yedges) x_ind = np.floor((locs['x']-self.x_min)/(self.x_max-self.x_min)*steps_x)-1 y_ind = np.floor((locs['y']-self.y_min)/(self.y_max-self.y_min)*steps_y)-1 x_ind = x_ind.astype(int) y_ind = y_ind.astype(int) index = self.mask[y_ind,x_ind].astype(bool) self.index_locs = locs[index] self.index_locs_out = locs[~index] H_new, xedges, yedges = np.histogram2d(self.index_locs['x'], self.index_locs['y'], bins=(self.xedges, self.yedges)) self.H_new = H_new.T # Let each row list bins with common y range. ax4 = self.figure.add_subplot(144, title='Masked image') ax4.imshow(self.H_new, interpolation='nearest', origin='low',extent=[self.xedges[0], self.xedges[-1], self.yedges[0], self.yedges[-1]]) ax4.grid(False) self.mask_exists = 1 self.saveButton.setEnabled(True) self.canvas.draw()
Example 6
def calc_mutual_information(x, y, bins): try: if bins == -1: bins = doane_bin(x) if bins == np.inf: bins = sturges_bin(x) except ValueError: bins = 10.0 # print "bins", bins try: c_xy = np.histogram2d(x, y, bins)[0] mi = metrics.mutual_info_score(None, None, contingency=c_xy) # print "success" except Exception,e: print "error with mi calc", str(e) mi = 0 return mi
Example 7
def signal_gaussian( signal_location=np.array([61, 21])*u.deg, fov_center=np.array([60, 20])*u.deg, width=0.05*u.deg, signal_events=80, bins=[80, 80], fov=4*u.deg, ): # reshape so if signal_events = 1 the array can be indexed in the same way. signal = multivariate_normal.rvs( mean=signal_location.value, cov=width.value, size=signal_events ).reshape(signal_events, 2) r = np.array([fov_center - fov/2, fov_center + fov/2]).T signal_hist, _, _ = np.histogram2d(signal[:, 0], signal[:, 1], bins=bins, range=r) return signal_hist
Example 8
def drawSmoothCatalog(self, catalog, label=None, **kwargs): ax = plt.gca() ra,dec = catalog.ra_dec x, y = sphere2image(self.ra,self.dec,ra,dec) delta_x = self.radius/100. smoothing = 2*delta_x bins = numpy.arange(-self.radius, self.radius + 1.e-10, delta_x) h, xbins, ybins = numpy.histogram2d(x, y, bins=[bins, bins]) blur = nd.filters.gaussian_filter(h.T, smoothing / delta_x) defaults = dict(cmap='gray_r',rasterized=True) kwargs = dict(defaults.items()+kwargs.items()) xx,yy = np.meshgrid(xbins,ybins) im = drawProjImage(xx,yy,blur,coord='C',**kwargs) if label: plt.text(0.05, 0.95, label, fontsize=10, ha='left', va='top', color='k', transform=pylab.gca().transAxes, bbox=dict(facecolor='white', alpha=1., edgecolor='none'))
Example 9
def visualize2D(fig, ax, xs, ys, bins=200, xlabel='x', ylabel='y', xlim=None, ylim=None): H, xedges, yedges = numpy.histogram2d(xs, ys, bins) H = numpy.rot90(H) H = numpy.flipud(H) Hmasked = numpy.ma.masked_where(H == 0, H) ax.pcolormesh(xedges, yedges, Hmasked) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) if xlim is None: xlim = (min(xs), max(xs)) if ylim is None: ylim = (min(ys), max(ys)) ax.set_xlim(*xlim) ax.set_ylim(*ylim) fig.colorbar(pyplot.contourf(Hmasked))
Example 10
def test_zoom(): edges = pixel_edges(jet_size=1, pixel_size=(0.1, 0.1), border_size=0.25) assert_equal(edges[0].shape, (26,)) assert_equal(edges[1].shape, (26,)) image, _, _ = np.histogram2d( np.random.normal(0, 1, 1000), np.random.normal(0, 1, 1000), bins=(edges[0], edges[1])) assert_true(image.sum() > 0) assert_equal(image.shape, (25, 25)) # zooming with factor 1 should not change anything image_zoomed = zoom_image(image, 1, out_width=25) assert_array_almost_equal(image, image_zoomed) assert_raises(ValueError, zoom_image, image, 0.5) # test out_width assert_equal(zoom_image(image, 1, out_width=11).shape, (11, 11)) image_zoomed = zoom_image(image, 2, out_width=25) assert_true(image.sum() < image_zoomed.sum())
Example 11
def heatmap (d, bins=(100, 100), smoothing=1.3, cmap='jet'): def getx (pt): return pt.coords[0][0] def gety (pt): return pt.coords[0][1] x = list(d.geometry.apply(getx)) y = list(d.geometry.apply(gety)) heatmap, xedges, yedges = np.histogram2d(y, x, bins=bins) extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]] # bin edges along the x and y dimensions, ordered # why are we taking log? logheatmap = np.log(heatmap) logheatmap[np.isneginf(logheatmap)] = 0 logheatmap = ndimage.filters.gaussian_filter(logheatmap, smoothing, mode='nearest') return (logheatmap, extent)
Example 12
def DensityHistogram(xypoints, numbins): xpoints = map(lambda (x, y): x, xypoints) ypoints = map(lambda (x, y): y, xypoints) minx, maxx, miny, maxy = min(xpoints), max(xpoints), \ min(ypoints), max(ypoints) xedges = np.arange(minx, maxx, (maxx - minx) / float(numbins)) yedges = np.arange(miny, maxy, (maxy - miny) / float(numbins)) H, xedges, yedges = np.histogram2d(ypoints, xpoints, bins = (xedges, yedges)) fig = plt.figure(figsize=(7, 3)) ax = fig.add_subplot(132) ax.set_title('pcolormesh: exact bin edges') X, Y = np.meshgrid(xedges, yedges) ax.pcolormesh(X, Y, H) ax.set_aspect('equal') #plt.savefig('./output/foo.png', bbox_inches='tight') #plt.show() g = histogram([xpoints, ypoints]) g.save('./output/foo2.png') ## def
Example 13
def addDistributions(self,Tuple): import numpy selidxs=[] ytuple=Tuple[self.nameY] xtuple=Tuple[self.nameX] useonlyoneclass=len(self.classes)==1 and len(self.classes[0])==0 if not useonlyoneclass: labeltuple=Tuple[self.classes] for c in self.classes: selidxs.append(labeltuple[c]>0) else: selidxs=[numpy.zeros(len(xtuple),dtype='int')<1] for i in range(len(self.classes)): tmphist,xe,ye=numpy.histogram2d(xtuple[selidxs[i]],ytuple[selidxs[i]],[self.axisX,self.axisY],normed=True) self.xedges=xe self.yedges=ye if len(self.distributions)==len(self.classes): self.distributions[i]=self.distributions[i]+tmphist else: self.distributions.append(tmphist)
Example 14
def heatmap(self, get_ball_var): heat_values = get_ball_var('ball_position_history') # Generate some test data x = np.random.randn(8873) y = np.random.randn(8873) heatmap, xedges, yedges = np.histogram2d(x, y, bins=50) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] # x = np.random.randn(100000) y = np.random.randn(100000) # print(y) # plt.hist2d(HeatValues[0],HeatValues[1],bins=100); # plt.clf() # plt.imshow(heatmap, extent=extent) # plt.show()
Example 15
def _init_read_free(self): # subverted... interpret self.filename as a numpy array try: FEC = self.filename # FreeEnergyContainer? F = FEC.F except AttributeError: super(DerivedFreeEnergy,self)._init_read_free() return self._midpoints = (FEC.X, FEC.Y) self._X = FEC.X self._Y = FEC.Y self._free_energy = numpy.ma.array(F, mask=numpy.logical_not(numpy.isfinite(F)), fill_value=1000); # reconstruct what input histogram2d would need self._edges = (self._mid2edges(self._X), # NMP bin edges self._mid2edges(self._Y)) # LID bin edges
Example 16
def plotAgainstGFP_hist2d(self): fig1 = pylab.figure(figsize = (20, 15)) print len(self.GFP) for i in xrange(min(len(data.cat), 4)): print len(self.GFP[self.categories == i]) vect = [] pylab.subplot(2,2,i+1) pop = self.GFP[self.categories == i] print "cat", i, "n pop", len(self.GFP[(self.categories == i) & (self.GFP > -np.log(12.5))]) H, xedges, yedges = np.histogram2d(self.angles[self.categories == i], self.GFP[self.categories == i], bins = 10) hist = pylab.hist2d(self.GFP[self.categories == i], self.angles[self.categories == i], bins = 10, cmap = pylab.cm.Reds, normed = True) pylab.clim(0.,0.035) pylab.colorbar() pylab.title(data.cat[i]) pylab.xlabel('GFP score') pylab.ylabel('Angle (degree)') pylab.xlim([-4.2, -1]) pylab.show()
Example 17
def calculate_mutualinformation(x, y, bins): pxy, _, _ = np.histogram2d(x, y, bins) px, _, = np.histogram(x, bins) py, _, = np.histogram(y, bins) pxy = pxy/np.sum(pxy) px = px/np.sum(px) py = py/np.sum(py) pxy = pxy[np.nonzero(pxy)] px = px[np.nonzero(px)] py = py[np.nonzero(py)] hxy = -np.sum(pxy*np.log2(pxy)) hx = -np.sum(px*np.log2(px)) hy = -np.sum(py*np.log2(py)) MI = hx+hy-hxy return MI
Example 18
def test_f32_rounding(self): # gh-4799, check that the rounding of the edges works with float32 x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32) y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32) counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100) assert_equal(counts_hist.sum(), 3.)
Example 19
def edgeplot(self, TT, ps): for ei,X in enumerate(self.edges): (i, j) = X[:2] Ta = TT[i] Tb = TT[j] plt.clf() if len(Ta) > 1000: nbins = 101 ra = np.hstack((Ta.ra, Tb.ra)) dec = np.hstack((Ta.dec, Tb.dec)) H,xe,ye = np.histogram2d(ra, dec, bins=nbins) (matchRA, matchDec, dr,dd) = self.edge_matches(ei, goodonly=True) G,xe,ye = np.histogram2d(matchRA, matchDec, bins=(xe,ye)) assert(G.shape == H.shape) img = antigray(H / H.max()) img[G>0,:] = matplotlib.cm.hot(G[G>0] / H[G>0]) ax = setRadecAxes(xe[0], xe[-1], ye[0], ye[-1]) plt.imshow(img, extent=(min(xe), max(xe), min(ye), max(ye)), aspect='auto', origin='lower', interpolation='nearest') plt.axis(ax) else: self.plotallstars([Ta,Tb]) self.plotmatchedstars(ei) plt.xlabel('RA (deg)') plt.ylabel('Dec (deg)') ps.savefig() # one plot per edge
Example 20
def gen_hist(self, event=None): try: xnum = int(self.x_axis_num.text()) ynum = int(self.y_axis_num.text()) except ValueError: sys.stderr.write('Need axes numbers to be integers\n') return self.hist2d, self.binx, self.biny = np.histogram2d(self.embed[:,xnum], self.embed[:,ynum], bins=100) delx = self.binx[1] - self.binx[0] dely = self.biny[1] - self.biny[0] self.binx = np.insert(self.binx, 0, [self.binx[0]-6*delx, self.binx[0]-delx]) self.binx = np.insert(self.binx, len(self.binx), [self.binx[-1]+delx, self.binx[-1]+6*delx]) self.biny = np.insert(self.biny, 0, [self.biny[0]-6*dely, self.biny[0]-dely]) self.biny = np.insert(self.biny, len(self.biny), [self.biny[-1]+dely, self.biny[-1]+6*dely])
Example 21
def _compute_occupancy(self): x, y = self.trans_func(self._extern, at=self._bst.bin_centers) xmin = self.xbins[0] xmax = self.xbins[-1] ymin = self.ybins[0] ymax = self.ybins[-1] occupancy, _, _ = np.histogram2d(x, y, bins=[self.xbins, self.ybins], range=([[xmin, xmax], [ymin, ymax]])) return occupancy
Example 22
def convert_Qmap_old( img, qx_map, qy_map=None, bins=None, rangeq=None): """Y.G. Nov [email protected] Convert a scattering image to a qmap by giving qx_map and qy_map Return converted qmap, x-coordinates and y-coordinates """ if qy_map is not None: if rangeq is None: qx_min,qx_max = qx_map.min(), qx_map.max() qy_min,qy_max = qy_map.min(), qy_map.max() rangeq = [ [qx_min,qx_max], [qy_min,qy_max] ] if bins is None: bins = qx_map.shape remesh_data, xbins, ybins = np.histogram2d(qx_map.ravel(), qy_map.ravel(), bins=bins, range= rangeq, normed=False, weights= img.ravel() ) else: if rangeq is None: qx_min,qx_max = qx_map.min(), qx_map.max() rangeq = [qx_min,qx_max] if bins is None: bins = qx_map.size else: if isinstance( bins, list): bins = bins[0] print( rangeq, bins ) remesh_data, xbins = np.histogram(qx_map.ravel(), bins=bins, range= rangeq, normed=False, weights= img.ravel() ) ybins=None return remesh_data, xbins, ybins # Mask ################################################################################
Example 23
def test_f32_rounding(self): # gh-4799, check that the rounding of the edges works with float32 x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32) y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32) counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100) assert_equal(counts_hist.sum(), 3.)
Example 24
def from_values(cls, x, y, weights=None, nbins=200, x_name=None, y_name=None): """ This function ... :param x: :param y: :param weights: :param nbins: :param x_name: :param y_name: :return: """ #rBins_F, FBins_r = getRadBins(x, y, 1, weights) #rBins_F[rBins_F > 25] = np.nan rBins_F = None FBins_r = None #print("rBins_F", rBins_F) #print("FBins_r", FBins_r) # Estimate the 2D histogram H, xedges, yedges = np.histogram2d(x, y, bins=nbins, normed=True, weights=weights) # H needs to be rotated and flipped H = np.rot90(H) H = np.flipud(H) # Mask zeros Hmasked = np.ma.masked_where(H == 0, H) # Mask pixels with a value of zero return cls(Hmasked, xedges, yedges, rBins_F, FBins_r, x_name, y_name) # -----------------------------------------------------------------
Example 25
def from_values(cls, x, y, weights=None, nbins=200, x_name=None, y_name=None): """ This function ... :param x: :param y: :param weights: :param nbins: :param x_name: :param y_name: :return: """ #rBins_F, FBins_r = getRadBins(x, y, 1, weights) #rBins_F[rBins_F > 25] = np.nan rBins_F = None FBins_r = None #print("rBins_F", rBins_F) #print("FBins_r", FBins_r) # Estimate the 2D histogram H, xedges, yedges = np.histogram2d(x, y, bins=nbins, normed=True, weights=weights) # H needs to be rotated and flipped H = np.rot90(H) H = np.flipud(H) # Mask zeros Hmasked = np.ma.masked_where(H == 0, H) # Mask pixels with a value of zero return cls(Hmasked, xedges, yedges, rBins_F, FBins_r, x_name, y_name) # -----------------------------------------------------------------
Example 26
def get_hist_node2vec(emb,d,my_min,my_max,definition): # d should be an even integer img_dim = int(np.arange(my_min, my_max+0.05,(my_max+0.05-my_min)/float(definition*(my_max+0.05-my_min))).shape[0]-1) my_bins = np.linspace(my_min,my_max,img_dim) # to have middle bin centered on zero Hs = [] for i in range(0,d,2): H, xedges, yedges = np.histogram2d(x=emb[:,i],y=emb[:,i+1],bins=my_bins, normed=False) Hs.append(H) Hs = np.array(Hs) return Hs
Example 27
def get_hist_node2vec(emb,d,my_min,my_max,definition): # d should be an even integer img_dim = int(np.arange(my_min, my_max+0.05,(my_max+0.05-my_min)/float(definition*(my_max+0.05-my_min))).shape[0]-1) my_bins = np.linspace(my_min,my_max,img_dim) # to have middle bin centered on zero Hs = [] for i in range(0,d,2): H, xedges, yedges = np.histogram2d(x=emb[:,i],y=emb[:,i+1],bins=my_bins, normed=False) Hs.append(H) Hs = np.array(Hs) return Hs
Example 28
def histogram2dstd(data, std = 6, bins = 50): """Create histogram resolving distribution according to std""" ## calculate standard deviations in each direction stds = np.std(data[:,0:-2], axis = 0); means = np.mean(data[:,0:-2], axis = 0); rngs = [[m- std * s, m + std * s] for m,s in itertools.izip(means,stds)]; hist = np.histogram2d(data[:,0], data[:,1], bins = bins, range = rngs); return hist;
Example 29
def mutual_info(x, y, bins=10): counts_xy, bins_x, bins_y = np.histogram2d(x, y, bins=(bins, bins)) counts_x, bins = np.histogram(x, bins=bins) counts_y, bins = np.histogram(y, bins=bins) counts_xy += 1 counts_x += 1 counts_y += 1 P_xy = counts_xy / np.sum(counts_xy, dtype=float) P_x = counts_x / np.sum(counts_x, dtype=float) P_y = counts_y / np.sum(counts_y, dtype=float) I_xy = np.sum(P_xy * np.log2(P_xy / (P_x.reshape(-1, 1) * P_y))) return I_xy / (entropy(counts_x) + entropy(counts_y))
Example 30
def generate_image(self): locs = self.locs[0] self.stepsize = 1/self.oversampling self.xedges = np.arange(self.x_min,self.x_max,self.stepsize) self.yedges = np.arange(self.y_min,self.y_max,self.stepsize) H, xedges, yedges = np.histogram2d(locs['x'], locs['y'], bins=(self.xedges, self.yedges)) H = H.T # Let each row list bins with common y range. self.H = H
Example 31
def getHSHistograms_2D(HSVimage): (Width, Height) = HSVimage.shape[1], HSVimage.shape[0] H, xedges, yedges = numpy.histogram2d(numpy.reshape(HSVimage[:,:,0], Width*Height), numpy.reshape(HSVimage[:,:,1], Width*Height), bins=(range(-1,180, 30), range(-1, 256, 64))) H = H / numpy.sum(H); return (H, xedges, yedges)
Example 32
def test_f32_rounding(self): # gh-4799, check that the rounding of the edges works with float32 x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32) y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32) counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100) assert_equal(counts_hist.sum(), 3.)
Example 33
def compute_histogram(data, bins=36): h, x, y = np.histogram2d(data[data.columns[0]], data[data.columns[1]], bins=bins, normed=True) xc = (x[:-1] + x[1:]) / 2 yc = (y[:-1] + y[1:]) / 2 coords = np.array([(xi, yi) for xi in xc for yi in yc]) theta = coords[:, 0] r = coords[:, 1] return h.flatten(), theta, r
Example 34
def create_cube(df, bins, bin_range): _, x_edges, y_edges = np.histogram2d( df.alt, df.az, bins=bins, range=bin_range) slices = [] N = 100 for df in np.array_split(df, N): H, _, _ = np.histogram2d(df.alt, df.az, bins=[ x_edges, y_edges], range=bin_range) slices.append(H) slices = np.array(slices) return slices
Example 35
def create_cube(self, points): t, alt, az = points.T alt = alt.astype(np.float) az = az.astype(np.float) _, x_edges, y_edges = np.histogram2d( alt, az, bins=self.bins, range=self.bin_range ) slices = [] timestamps = [] for indeces in np.array_split(np.arange(0, len(points)), self.slices_per_cube): timestamps.append(t[indeces][0]) H, _, _ = np.histogram2d( alt[indeces], az[indeces], bins=[x_edges, y_edges], range=self.bin_range) slices.append(H) slices = np.array(slices) timestamps = np.array(timestamps) return timestamps, slices
Example 36
def myplot(x, y, nb=32, xsize=500, ysize=500): heatmap, xedges, yedges = np.histogram2d(x, y, bins=nb) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] return heatmap.T, extent
Example 37
def summarizeMap(mapDataFrame): latlon = mapDataFrame[['meta_latitude','meta_longitude']] latlon = latlon[pd.notnull(latlon['meta_latitude'])] latlon = latlon[pd.notnull(latlon['meta_longitude'])] minLat = np.amin(latlon['meta_latitude']) maxLat = np.amax(latlon['meta_latitude']) minLon = np.amin(latlon['meta_longitude']) maxLon = np.amax(latlon['meta_longitude']) if len(latlon) > 1: latlon_map = np.histogram2d(x=latlon['meta_longitude'],y=latlon['meta_latitude'],bins=[36,18], range=[[minLon, maxLon], [minLat, maxLat]]) else: latlon_map = np.histogram2d(x=[],y=[],bins=[36,18], range=[[-180, 180], [-90, 90]]) #define latlon map color bin info percentiles, countRanges, fillColors = getMapBins(latlon_map[0], num_bins=10) # range should be flexible to rules in DatasetSearchSummary # latlon_map[0] is the lonxlat (XxY) array of counts; latlon_map[1] is the nx/lon bin starts; map[2] ny/lat bin starts lonstepsize = (latlon_map[1][1]-latlon_map[1][0])/2 latstepsize = (latlon_map[2][1]-latlon_map[2][0])/2 maxMapCount = np.amax(latlon_map[0]) map_data = [] for lon_ix,lonbin in enumerate(latlon_map[0]): for lat_ix,latbin in enumerate(lonbin): #[latlon_map[2][ix]+latstepsize for ix,latbin in enumerate(latlon_map[0][0])] lat = latlon_map[2][lat_ix]+latstepsize lon = latlon_map[1][lon_ix]+lonstepsize value = latbin buffer=0.0001 #left-bottom, left-top, right-top, right-bottom, left-bottom polygon = [[lon-lonstepsize+buffer,lat-latstepsize+buffer], [lon-lonstepsize+buffer,lat+latstepsize-buffer], [lon+lonstepsize-buffer,lat+latstepsize-buffer], [lon+lonstepsize-buffer,lat-latstepsize+buffer], [lon-lonstepsize,lat-latstepsize]] bin_ix = np.amax(np.argwhere(np.array(percentiles)<=sp.percentileofscore(latlon_map[0].flatten(), value))) fillColor = fillColors[bin_ix] map_data.append({"lat":lat,"lon":lon,"count":value,"polygon":polygon, "fillColor":fillColor}) map_legend_info = {"ranges":countRanges, "fills":fillColors} return (map_data,map_legend_info) # Query Construction Helpers / Data Retrieval # Based on a rule (field name, comparator and value), add a filter to a query object # TODO add some better documentation here on what each type is
Example 38
def hinton(W, bg='grey', facecolors=('w', 'k')): """Draw a hinton diagram of the matrix W on the current pylab axis Hinton diagrams are a way of visualizing numerical values in a matrix/vector, popular in the neural networks and machine learning literature. The area occupied by a square is proportional to a value's magnitude, and the colour indicates its sign (positive/negative). Example usage: R = np.random.normal(0, 1, (2,1000)) h, ex, ey = np.histogram2d(R[0], R[1], bins=15) hh = h - h.T hinton.hinton(hh) """ M, N = W.shape square_x = np.array([-.5, .5, .5, -.5]) square_y = np.array([-.5, -.5, .5, .5]) ioff = False if plt.isinteractive(): plt.ioff() ioff = True plt.fill([-.5, N - .5, N - .5, - .5], [-.5, -.5, M - .5, M - .5], bg) Wmax = np.abs(W).max() for m, Wrow in enumerate(W): for n, w in enumerate(Wrow): c = plt.signbit(w) and facecolors[1] or facecolors[0] plt.fill(square_x * w / Wmax + n, square_y * w / Wmax + m, c, edgecolor=c) plt.ylim(-0.5, M - 0.5) plt.xlim(-0.5, M - 0.5) if ioff is True: plt.ion() plt.draw_if_interactive()
Example 39
def test_f32_rounding(self): # gh-4799, check that the rounding of the edges works with float32 x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32) y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32) counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100) assert_equal(counts_hist.sum(), 3.)
Example 40
def twoDimensionalHistogram(title, title_x, title_y, z, bins_x, bins_y, lim_x=None, lim_y=None, vmin=None, vmax=None): """ Create a two-dimension histogram plot or binned map. If using the outputs of numpy.histogram2d, remember to transpose the histogram. INPUTS """ pylab.figure() mesh_x, mesh_y = numpy.meshgrid(bins_x, bins_y) if vmin != None and vmin == vmax: pylab.pcolor(mesh_x, mesh_y, z) else: pylab.pcolor(mesh_x, mesh_y, z, vmin=vmin, vmax=vmax) pylab.xlabel(title_x) pylab.ylabel(title_y) pylab.title(title) pylab.colorbar() if lim_x: pylab.xlim(lim_x[0], lim_x[1]) if lim_y: pylab.ylim(lim_y[0], lim_y[1]) ############################################################
Example 41
def drawHessDiagram(self,catalog=None): ax = plt.gca() if not catalog: catalog = self.get_stars() r_peak = self.kernel.extension angsep = ugali.utils.projector.angsep(self.ra, self.dec, catalog.ra, catalog.dec) cut_inner = (angsep < r_peak) cut_annulus = (angsep > 0.5) & (angsep < 1.) # deg mmin, mmax = 16., 24. cmin, cmax = -0.5, 1.0 mbins = np.linspace(mmin, mmax, 150) cbins = np.linspace(cmin, cmax, 150) color = catalog.color[cut_annulus] mag = catalog.mag[cut_annulus] h, xbins, ybins = numpy.histogram2d(color, mag, bins=[cbins,mbins]) blur = nd.filters.gaussian_filter(h.T, 2) kwargs = dict(extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()], cmap='gray_r', aspect='auto', origin='lower', rasterized=True, interpolation='none') ax.imshow(blur, **kwargs) pylab.scatter(catalog.color[cut_inner], catalog.mag[cut_inner], c='red', s=7, edgecolor='none')# label=r'$r < %.2f$ deg'%(r_peak)) ugali.utils.plotting.drawIsochrone(self.isochrone, c='b', zorder=10) ax.set_xlim(-0.5, 1.) ax.set_ylim(24., 16.) plt.xlabel(r'$g - r$') plt.ylabel(r'$g$') plt.xticks([-0.5, 0., 0.5, 1.]) plt.yticks(numpy.arange(mmax - 1., mmin - 1., -1.)) radius_string = (r'${\rm r}<%.1f$ arcmin'%( 60 * r_peak)) pylab.text(0.05, 0.95, radius_string, fontsize=10, ha='left', va='top', color='red', transform=pylab.gca().transAxes, bbox=dict(facecolor='white', alpha=1., edgecolor='none'))
Example 42
def histogram2d(self,distance_modulus=None,delta_mag=0.03,steps=10000): """ Return a 2D histogram the isochrone in mag-mag space. Parameters: ----------- distance_modulus : distance modulus to calculate histogram at delta_mag : magnitude bin size mass_steps : number of steps to sample isochrone at Returns: -------- bins_mag_1 : bin edges for first magnitude bins_mag_2 : bin edges for second magnitude isochrone_pdf : weighted pdf of isochrone in each bin """ if distance_modulus is not None: self.distance_modulus = distance_modulus # Isochrone will be binned, so might as well sample lots of points mass_init,mass_pdf,mass_act,mag_1,mag_2 = self.sample(mass_steps=steps) #logger.warning("Fudging intrinisic dispersion in isochrone.") #mag_1 += np.random.normal(scale=0.02,size=len(mag_1)) #mag_2 += np.random.normal(scale=0.02,size=len(mag_2)) # We cast to np.float32 to save memory bins_mag_1 = np.arange(self.mod+mag_1.min() - (0.5*delta_mag), self.mod+mag_1.max() + (0.5*delta_mag), delta_mag).astype(np.float32) bins_mag_2 = np.arange(self.mod+mag_2.min() - (0.5*delta_mag), self.mod+mag_2.max() + (0.5*delta_mag), delta_mag).astype(np.float32) # ADW: Completeness needs to go in mass_pdf here... isochrone_pdf = np.histogram2d(self.mod + mag_1, self.mod + mag_2, bins=[bins_mag_1, bins_mag_2], weights=mass_pdf)[0].astype(np.float32) return isochrone_pdf, bins_mag_1, bins_mag_2
Example 43
def pixelize(jet_csts, edges, cutoff=0.1): """Return eta-phi histogram of transverse energy deposits. Optionally set all instensities below cutoff to zero. """ image, _, _ = np.histogram2d( jet_csts['eta'], jet_csts['phi'], bins=(edges[0], edges[1]), weights=jet_csts['ET'] * (jet_csts['ET'] > cutoff)) return image
Example 44
def update_data(self): var = getattr(self._sim, self._variable)[:,0:2] mask = None if self._sub_domain: pos = self._sim.positions mask_x = np.logical_or(pos[:, 0] <= self._sub_domain[0][0], pos[:, 0] >= self._sub_domain[0][1]) mask_y = np.logical_or(pos[:, 1] <= self._sub_domain[1][0], pos[:, 1] >= self._sub_domain[1][1]) mask = np.logical_or(mask_x, mask_y) if self._particle_type is not None: if mask is None: mask = (self._sim.types != self._particle_type) else: mask = np.logical_or(mask, (self._sim.types != self._particle_type)) if mask is not None: tiledmask = np.transpose(np.tile(mask, (2, 1))) var = ma.masked_array(var, tiledmask) var = var.compressed() var = var.reshape([len(var)//2, 2]) hist, self._x_edges, self._y_edges = np.histogram2d(var[:, 0], var[:, 1], bins=self._nr_of_bins, range=self._hist_range) if self._window is not None: self._dataHistory.append(hist) if len(self._dataHistory) > self._window: del self._dataHistory[0] self._histogram_array = sum(self._dataHistory) else: self._histogram_array += hist
Example 45
def test_f32_rounding(self): # gh-4799, check that the rounding of the edges works with float32 x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32) y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32) counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100) assert_equal(counts_hist.sum(), 3.)
Example 46
def get_heatmaps(neuron_list, spikes, pos, num_bins=100): """ Gets the 2D heatmaps for firing of a given set of neurons. Parameters ---------- neuron_list : list of ints These will be the indices into the full list of neuron spike times spikes : list Containing nept.SpikeTrain for each neuron. pos : nept.Position Must be 2D. num_bins : int This will specify how the 2D space is broken up, the greater the number the more specific the heatmap will be. The default is set at 100. Returns ------- heatmaps : dict of lists Where the key is the neuron number and the value is the heatmap for that individual neuron. """ if not pos.dimensions == 2: raise ValueError("pos must be two-dimensional") xedges = np.linspace(np.min(pos.x)-2, np.max(pos.x)+2, num_bins+1) yedges = np.linspace(np.min(pos.y)-2, np.max(pos.y)+2, num_bins+1) heatmaps = dict() count = 1 for neuron in neuron_list: field_x = [] field_y = [] for spike in spikes[neuron].time: spike_idx = find_nearest_idx(pos.time, spike) field_x.append(pos.x[spike_idx]) field_y.append(pos.y[spike_idx]) heatmap, out_xedges, out_yedges = np.histogram2d(field_x, field_y, bins=[xedges, yedges]) heatmaps[neuron] = heatmap.T print(str(neuron) + ' of ' + str(len(neuron_list))) count += 1 return heatmaps
Example 47
def test_image(): import matplotlib.pyplot as plt con = config.Config() data = DataGenerator(con) xedges = [_ / 7 for _ in range(-14, 15)] yedges = [_ / 7 for _ in range(-14, 15)] image_data = {} for x, y in data.get_train_data(1): e, v = scipy.linalg.eigh( x.values.reshape((10, 10))) # np.linalg.eig will return the complex data sometimes... for i in range(1, len(v)): new_v = preprocessing.scale(v[i]) for k in range(0, len(new_v), 2): if k not in image_data: image_data[k] = {} image_data[k][0] = [new_v[k]] image_data[k][1] = [new_v[k + 1]] else: image_data[k][0].append(new_v[k]) image_data[k][1].append(new_v[k + 1]) for k in image_data.keys(): H, new_xedges, new_yedges = np.histogram2d(image_data[k][0], image_data[k][1], bins=(xedges, yedges)) print(H) plt.imshow(H, cmap=plt.cm.gray, interpolation='nearest', origin='low', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]]) plt.show()
Example 48
def get_fragment_ends_matrices(self, merged, sv_region, window_size): mats = {} selectors = {"+":"end_pos_{}", "-":"start_pos_{}"} binsx = numpy.arange(sv_region["startx"], sv_region["endx"]+window_size*2, window_size) binsy = numpy.arange(sv_region["starty"], sv_region["endy"]+window_size*2, window_size) for orientationx in "+-": for orientationy in "+-": fx = merged[selectors[orientationx].format("x")].values fy = merged[selectors[orientationy].format("y")].values hist = numpy.histogram2d(fy, fx, (binsy, binsx))[0] mats[orientationx+orientationy] = hist return mats
Example 49
def plot_rc_alpha(ax, snr150=False): nbins_x = 30 nbins_y = 40 ind = indrc if snr150: ind = ind_snr150 H, xedges, yedges = np.histogram2d(metals[ind], alphafe[ind], bins=(nbins_x, nbins_y), normed=True) x_bin_sizes = (xedges[1:] - xedges[:-1]).reshape((1, nbins_x)) y_bin_sizes = (yedges[1:] - yedges[:-1]).reshape((nbins_y, 1)) pdf = (H * np.multiply(x_bin_sizes, y_bin_sizes).T) sig05 = optimize.brentq(find_confidence_interval, 0., 1., args=(pdf, 0.38)) sig1 = optimize.brentq(find_confidence_interval, 0., 1., args=(pdf, 0.68)) sig2 = optimize.brentq(find_confidence_interval, 0., 1., args=(pdf, 0.95)) sig3 = optimize.brentq(find_confidence_interval, 0., 1., args=(pdf, 0.99)) levels = [sig1, sig05, 1.] X = 0.5 * (xedges[1:] + xedges[:-1]) Y = 0.5 * (yedges[1:] + yedges[:-1]) Z = pdf.T ax.contourf(X, Y, Z, levels=levels, origin='lower', colors=('darkgray', 'gray'), zorder=9) ax.contour(X, Y, Z, levels=[levels[0], levels[1]], origin='lower', colors='k', zorder=10) for i in range(nbins_x): for j in range(nbins_y): if Z[j, i] <= sig1 * 1.2: ind_tmp0 = np.where( (metals >= xedges[i]) & (metals < xedges[i+1]) & (alphafe >= yedges[j]) & (alphafe < yedges[j+1]))[0] ind_tmp = np.intersect1d(ind, ind_tmp0) if len(ind_tmp > 0): ax.scatter(metals[ind_tmp], alphafe[ind_tmp], c='k', s=5)
Example 50
def test_contingency_matrix(): labels_a = np.array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3]) labels_b = np.array([1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 3, 1, 3, 3, 3, 2, 2]) C = contingency_matrix(labels_a, labels_b) C2 = np.histogram2d(labels_a, labels_b, bins=(np.arange(1, 5), np.arange(1, 5)))[0] assert_array_almost_equal(C, C2) C = contingency_matrix(labels_a, labels_b, eps=.1) assert_array_almost_equal(C, C2 + .1)