def test_2d_mean_unicode(self): x = self.x y = self.y v = self.v stat1, binx1, biny1, bc = binned_statistic_2d(x, y, v, u("mean"), bins=5) stat2, binx2, biny2, bc = binned_statistic_2d(x, y, v, np.mean, bins=5) assert_allclose(stat1, stat2) assert_allclose(binx1, binx2) assert_allclose(biny1, biny2)
def test_2d_mean_unicode(self): x = self.x y = self.y v = self.v stat1, binx1, biny1, bc = binned_statistic_2d(x, y, v, u('mean'), bins=5) stat2, binx2, biny2, bc = binned_statistic_2d(x, y, v, np.mean, bins=5) assert_array_almost_equal(stat1, stat2) assert_array_almost_equal(binx1, binx2) assert_array_almost_equal(biny1, biny2)
def test_2d_std(self): x = self.x y = self.y v = self.v stat1, binx1, biny1, bc = binned_statistic_2d(x, y, v, 'std', bins=5) stat2, binx2, biny2, bc = binned_statistic_2d(x, y, v, np.std, bins=5) assert_array_almost_equal(stat1, stat2) assert_array_almost_equal(binx1, binx2) assert_array_almost_equal(biny1, biny2)
def test_2d_std(self): x = self.x y = self.y v = self.v stat1, binx1, biny1, bc = binned_statistic_2d(x, y, v, "std", bins=5) stat2, binx2, biny2, bc = binned_statistic_2d(x, y, v, np.std, bins=5) assert_allclose(stat1, stat2) assert_allclose(binx1, binx2) assert_allclose(biny1, biny2)
def test_2d_max(self): x = self.x y = self.y v = self.v stat1, binx1, biny1, bc = binned_statistic_2d(x, y, v, 'max', bins=5) stat2, binx2, biny2, bc = binned_statistic_2d(x, y, v, np.max, bins=5) assert_allclose(stat1, stat2) assert_allclose(binx1, binx2) assert_allclose(biny1, biny2)
def test_2d_multi_values(self): x = self.x y = self.y v = self.v w = self.w stat1v, binx1v, biny1v, bc1v = binned_statistic_2d(x, y, v, "mean", bins=8) stat1w, binx1w, biny1w, bc1w = binned_statistic_2d(x, y, w, "mean", bins=8) stat2, binx2, biny2, bc2 = binned_statistic_2d(x, y, [v, w], "mean", bins=8) assert_allclose(stat2[0], stat1v) assert_allclose(stat2[1], stat1w) assert_allclose(binx1v, binx2) assert_allclose(biny1w, biny2) assert_allclose(bc1v, bc2)
def mkHist(ax,x,y,xlabel,ylabel): numbins = 50 stat = 'count' if 'z' in ylabel: binrange = [[-3,3],[0,6]] else: binrange = [[-3,3],[-3,3]] h,xedges,yedges,binnumber = st.binned_statistic_2d(x,y,y, statistic=stat, bins=numbins, range=binrange) h = np.rot90(h) h = np.flipud(h) h[np.isnan(h)] = 0.0 h = np.ma.masked_where(h==0,h) h = np.log10(h) mesh = ax.pcolormesh(xedges,yedges,h) ax.set_xlim(binrange[0]) ax.set_ylim(binrange[1]) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) cbar = plt.colorbar(mesh,ax=ax,use_gridspec=True) return h,xedges,yedges,binnumber
def main(output_prefix, output_extension, *band_names): data = np.loadtxt(stdin.buffer, unpack=True) validate_columns(data, band_names) RA, DEC = data[:2] for band_data, band_name in zip(data[2:], band_names): fig = plt.figure() ax = fig.add_subplot(111, aspect='equal', adjustable='box') # count the number of stars in each bin MAGii, RAi, DECi, _ = binned_statistic_2d(RA, DEC, band_data, bins=20, statistic='count') RAii, DECii = np.meshgrid(RAi[:-1], DECi[:-1]) cn = ax.contourf(RAii, DECii, MAGii, 20) ax.set_xlabel(r"Right Ascension (degrees)") ax.set_ylabel(r"Declination (degrees)") ax.locator_params(axis="x", tight=True, nbins=5) cbar = fig.colorbar(cn) cbar.set_label("Star Count") fig.savefig(output_prefix + band_name + "." + output_extension) plt.close(fig)
def sector_average(data_x, data_y, data_z, n_bins_angle=100, n_bins_radius=50): ''' ''' angle = np.arctan2(data_y, data_x) angle = np.rad2deg(angle) # make it integer from 0 to 360 angle = np.round(angle).astype(int) + 180 # radius for every pixel radius = np.linalg.norm(np.column_stack((data_x, data_y)), axis=1) # normalize data to 1 data_z = (data_z - data_z.min()) / (data_z.max() - data_z.min()) H, xedges, yedges, binnumber = stats.binned_statistic_2d(angle, radius, data_z, bins=[n_bins_angle, n_bins_radius], statistic='mean') xedges_width = (xedges[1] - xedges[0]) xedges_center = xedges[1:] - xedges_width / 2 yedges_width = (yedges[1] - yedges[0]) yedges_center = yedges[1:] - yedges_width / 2 return H, xedges_center, yedges_center
def sector_average(data_x, data_y, data_z, n_bins_angle=100, n_bins_radius=50, max_radius = np.inf): radius = np.linalg.norm(np.column_stack((data_x, data_y)), axis=1) angle = np.arctan2(data_y, data_x) angle = np.rad2deg(angle) # make it integer from 0 to 360 angle = np.round(angle).astype(int) + 180 angle = angle[radius <= max_radius] # radius for every pixel # normalize data to 1 data_z = (data_z - data_z.min()) / (data_z.max() - data_z.min()) data_z =data_z[radius <= max_radius] radius = radius[radius <= max_radius] H_orig, xedges, yedges, binnumber = stats.binned_statistic_2d(angle, radius, data_z, bins=[n_bins_angle, n_bins_radius], statistic='mean') xedges_width = (xedges[1] - xedges[0]) xedges_center = xedges[1:] - xedges_width / 2 yedges_width = (yedges[1] - yedges[0]) yedges_center = yedges[1:] - yedges_width / 2 H = np.tile(H_orig, (2,1)) xedges_center = np.linspace(np.amin(xedges_center), 2*np.amax(xedges_center), 2*len(xedges_center)) return H, H_orig, xedges_center, yedges_center
def main(ra_filename, dec_filename, residual_filename, output_prefix, ext, *bands): ra = np.loadtxt(ra_filename) dec = np.loadtxt(dec_filename) residuals = np.loadtxt(residual_filename, unpack=True) for band, res in zip(bands, residuals): fig = plt.figure() ax = fig.add_subplot(111, aspect='equal', adjustable='box') resid_grid, ra_bin, dec_bin, _ = binned_statistic_2d(ra, dec, res, bins=20, statistic=np.std) ra_grid, dec_grid = np.meshgrid(ra_bin[:-1], dec_bin[:-1]) cn = ax.contourf(ra_grid, dec_grid, resid_grid, 20) ax.set_xlabel(r"Right Ascension (degrees)") ax.set_ylabel(r"Declination (degrees)") ax.locator_params(axis="x", tight=True, nbins=5) cbar = fig.colorbar(cn) cbar.set_label("Depth (mag std)") fig.savefig("{}{}.{}".format(output_prefix, band, ext)) plt.close(fig)
def mkHist(ax,x,y,z,cbarLabel,stat,binrange): numbins = 50 h,xedges,yedges,binnumber = st.binned_statistic_2d(x,y,z, statistic=stat, bins=numbins, range=binrange) h = np.rot90(h) h = np.flipud(h) h[np.isnan(h)] = 0.0 h = np.ma.masked_where(h==0,h) h = np.log10(h) if cbarLabel=='SNII': mesh = ax.pcolormesh(xedges,yedges,h,vmin=-7,vmax=-1) else: mesh = ax.pcolormesh(xedges,yedges,h) ax.set_xlim(binrange[0]) ax.set_ylim(binrange[1]) ax.set_xlabel('Rho [Rvir]') ax.set_ylabel('Z [Rvir]') cbar = plt.colorbar(mesh,ax=ax,use_gridspec=True, format=ticker.FuncFormatter(fmt)) cbar.ax.get_yaxis().labelpad = 20 cbar.ax.set_ylabel('{0:s} {1:s}'.format(stat,cbarLabel),rotation=270,fontsize=12)
def mkHisto(ax,x,y,z,xlabel,ylabel,clabel='SNII'): numbins = 50 stat = 'median' if 'z' in ylabel: binrange = [[-3,3],[0,6]] else: binrange = [[-3,3],[-3,3]] h,xedges,yedges,binnumber = st.binned_statistic_2d(x,y,y, statistic=stat,bins=numbins,range=binrange) h = np.rot90(h) h = np.flipud(h) h[np.isnan(h)] = 0.0 h = np.ma.masked_where(h==0,h) h = np.log10(h) mesh = ax.pcolormesh(xedges,yedges,h) ax.set_xlim(binrange[0]) ax.set_ylim(binrange[1]) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) cbar = plt.colorbar(mesh,ax=ax,use_gridspec=True, format=ticker.FuncFormatter(fmt))
def _centroidCalcContour(self): ''' Creates the contours based off of centroids using binned_statistic_2d ''' CSs = [] for (x, y, values) in zip(self.xs, self.ys, self.centralities): if not x: continue centrality, yedges, xedges, binNumber = sps.binned_statistic_2d(y, x, values, statistic='mean', bins=self.binSize, range=[[np.min(y), np.max(y)], [np.min(x), np.max(x)]]) for i in range(len(centrality)): centrality[i] = np.nan_to_num(centrality[i]) centrality = spn.filters.gaussian_filter(centrality, 2) extent = [xedges.min(), xedges.max(), yedges.min(), yedges.max()] smoothH = spn.zoom(centrality, 4) smoothH[smoothH < 0] = 0 CSs.append(plt.contour(smoothH, extent=extent)) return CSs
def EnergyMap(self, coord, Nx, Ny): x, y, z = zip(*coord) z = [element**self.mag for element in z] bs = binned_statistic_2d(x, y, z, statistic='mean', bins=[Nx, Ny]) return bs
def counts_per_region(x, y): tmp = stats.binned_statistic_2d(x, y, None, 'count', bins=[long_borders_list, lat_borders_list], expand_binnumbers=True) return (tmp.statistic.reshape((1, 2500)))
def test_2d_result_attributes(self): x = self.x y = self.y v = self.v res = binned_statistic_2d(x, y, v, 'count', bins=5) attributes = ('statistic', 'x_edge', 'y_edge', 'binnumber') check_named_results(res, attributes)
def augment_points(self, augmented_lidar_cam_coords): """ Converts (x,y,z,r,class) to (x,y,z,r,class,xc,yc,zc,xp,zp) """ points_in_xrange = (-40 < augmented_lidar_cam_coords[:,0]) & (augmented_lidar_cam_coords[:,0] < 40) points_in_zrange = (0 < augmented_lidar_cam_coords[:,2]) & (augmented_lidar_cam_coords[:,2] < 70.4) augmented_lidar_cam_coords = augmented_lidar_cam_coords[points_in_xrange & points_in_zrange] new_aug_lidar_cam_coords = np.zeros((augmented_lidar_cam_coords.shape[0], 10)) new_aug_lidar_cam_coords[:, :5] = augmented_lidar_cam_coords xedges = np.linspace(-40,40,501, dtype=np.float32) zedges = np.linspace(80, 0 ,501,dtype=np.float32) #80 first because a point 80m from ego car is in top row of bev img (row 0) x = augmented_lidar_cam_coords[:,0] # left/right y = augmented_lidar_cam_coords[:,1] #y in cam coords (+ is down, - is up) z = augmented_lidar_cam_coords[:,2] #front/back x_inds = np.digitize(x, xedges).reshape(-1,1) - 1 # subtract 1 to get 0 based indexing z_inds = np.digitize(z, zedges).reshape(-1,1) - 1 # idx into rows of bev img bin_idxs = np.hstack((z_inds, x_inds)) # z first because it corresponds to rows of the bev ret_x = stats.binned_statistic_2d(z, x, x, 'mean', bins=[np.flip(zedges),xedges]) # mean of x vals of points in each bin ret_y = stats.binned_statistic_2d(z, x, y, 'mean', bins=[np.flip(zedges),xedges]) ret_z = stats.binned_statistic_2d(z, x, z, 'mean', bins=[np.flip(zedges),xedges]) x_mean = np.flip(ret_x.statistic, axis=0) #since need to flip zedges (row bins) to make function work, need to flip output by rows y_mean = np.flip(ret_y.statistic, axis=0) z_mean = np.flip(ret_z.statistic, axis=0) #mean of all z values in each bev img 'pixel', NaN at cells with no points. x_ctr = np.tile(np.linspace((-40+.08),(40-.08),500), (500,1)) # coord of x center of each bev cell. All cols have same value z_ctr = np.tile(np.linspace((80-.08),(0+.08),500).reshape(-1,1), (1,500)) # all rows have same value #offset of each point from x_mean of pillar, ymean, zmean, x_center, y_center new_aug_lidar_cam_coords[:, 5] = new_aug_lidar_cam_coords[:, 0] - x_mean[bin_idxs[:, 0], bin_idxs[:, 1]] #offset of each point from xmean of pillar new_aug_lidar_cam_coords[:, 6] = new_aug_lidar_cam_coords[:, 1] - y_mean[bin_idxs[:, 0], bin_idxs[:, 1]] #yc new_aug_lidar_cam_coords[:, 7] = new_aug_lidar_cam_coords[:, 2] - z_mean[bin_idxs[:, 0], bin_idxs[:, 1]] #zc new_aug_lidar_cam_coords[:, 8] = new_aug_lidar_cam_coords[:, 0] - x_ctr[bin_idxs[:, 0], bin_idxs[:, 1]] #offset from x center of pillar new_aug_lidar_cam_coords[:, 9] = new_aug_lidar_cam_coords[:, 2] - z_ctr[bin_idxs[:, 0], bin_idxs[:, 1]] #zp H, _, __ = np.histogram2d(z,x, bins=(np.flip(zedges), xedges)) H[H != 0] = 1 num_nonempty_pillars = int(np.flip(H, axis=0).sum()) # pillars containing >= 1 lidar point pillar_idxs = np.unique(bin_idxs, axis=0) #ith element will be bin (row, col of bev img) of that pillar if pillar_idxs.shape[0] > self.max_pillars: np.random.shuffle(pillar_idxs) pillar_idxs = pillar_idxs[:self.max_pillars] return new_aug_lidar_cam_coords, bin_idxs, pillar_idxs
def test_2d_result_attributes(self): x = self.x y = self.y v = self.v res = binned_statistic_2d(x, y, v, "count", bins=5) attributes = ("statistic", "x_edge", "y_edge", "binnumber") check_named_results(res, attributes)
def aggregate_data(year, month, aggreagate_by=''): filename = data_dir + data_file_name.format(year=year, month=month) ndays = calendar.monthrange(year, month)[1] nhours = ndays * 24 date0 = np.datetime64('{year}-{month:02}-01T00:00:00'.format(year=year, month=month)) # Загружаем файл df = pd.read_csv(filename, ',', parse_dates=[1, 2]) # Уберем поездки нулевой длительности df = df[(df.tpep_dropoff_datetime - df.tpep_pickup_datetime) > np.timedelta64(0, 's')] # уберем поездки без пассажиров df = df[df.passenger_count > 0] # уберем поездки c нулевым расстоянием по счетчику df = df[df.trip_distance > 0.0] #отфильтруем так же сразу поездки по координатам df = df[(df.pickup_latitude >= NY[0].lat) & (df.pickup_latitude <= NY[1].lat) & (df.pickup_longitude >= NY[0].long) & (df.pickup_longitude <= NY[1].long)] # Рассортируем по зонам, посчитав общее кооличество поездок из каждой ячейки r = stats.binned_statistic_2d( df.pickup_latitude, df.pickup_longitude, None, 'count', (N, N), [[NY[0].lat, NY[1].lat], [NY[0].long, NY[1].long]], expand_binnumbers=True) # Запишем номер зоны в поле region_id df['region_id'] = map(regid_by_nll, r.binnumber.T) # Округляем время посадки до часов и записываем в новую колонку df['hour'] = df.tpep_pickup_datetime.dt.floor('H') # Построим пустую таблицу с часами по строкам и номером региона по столбцам. agg = pd.DataFrame( index=[date0 + np.timedelta64(1, 'h') * i for i in range(nhours)], columns=range(1, N * N + 1)).fillna(0.0) # Группируем исходную таблицу по часам и номеру региона # Подсчитываем количество объектов в каждой группе # Разворачиваем (unstack) таблицу, превращая иерархический индекс в индексы столбов и строк gb = df.groupby(['hour', 'region_id']) agg2 = gb.size() if aggreagate_by == '' else gb[aggreagate_by].mean() agg2 = agg2.unstack() # Записываем полученные данные в таблицу, которую мы создали выше agg.update(agg2) return agg
def histogram_DCs(dcA, dcB, bin_size, ran_size, temperature, smooth_param, weights=None): if weights == None: #default weights weights = np.ones(np.shape(dcA)[0]) if ran_size == None: z, x, y, slices = stats.binned_statistic_2d(dcA, dcB, weights, bins=bin_size, statistic='sum') else: z, x, y, slices = stats.binned_statistic_2d(dcA, dcB, weights, bins=bin_size, range=np.reshape( ran_size, (2, 2)), statistic='sum') #z,x,y = np.histogram2d(dcA, dcB, bins=[bin_size,bin_size], normed=True) min_prob = np.min(z) zmasked = np.ma.masked_where(z == 0, z) z = np.log(z) z *= (-1.0) z *= kb z *= temperature fe = np.ma.masked_where(z == float("inf"), z) fe = fe - fe.min() if smooth_param == 0: fe_smoothed = fe else: fe_smoothed = smooth2a(fe, smooth_param, smooth_param) return x, y, fe_smoothed, slices
def binned_statistic(self): # return hist, xedges, yedges ret = stats.binned_statistic_2d(self.data[self.col_names[0]], self.data[self.col_names[1]], self.data[self.col_names[2]], agg_stats_func, bins=bins) return ret
def to_velfield(self, filename='snap', lengthX=15, lengthY=15, BINS=512, first_only=False, com=False, parttype='stars', write=True, axes=[0, 1]): """ Write snapshot to a velocity field. Note:This is a pesudo first moment map where each pixel contains the average velocity in the y direction. """ from astropy.io import fits from scipy.stats import binned_statistic_2d pos2 = self.pos[parttype] vel2 = self.vel[parttype] if first_only: com1, com2, gal1id, gal2id = self.center_of_mass(parttype) px2 = pos2[gal1id, axes[0]] py2 = pos2[gal1id, axes[1]] vy2 = vel2[gal1id, axes[1]] if com: px2 -= com1[axes[0]] py2 -= com1[axes[1]] else: px2 = pos2[:, axes[0]] py2 = pos2[:, axes[1]] vy2 = vel2[:, axes[1]] (Z2, xedges, yedges, binnum) = binned_statistic_2d(px2, py2, vy2, statistic='mean', range=[[-lengthX, lengthX], [-lengthY, lengthY]], bins=BINS) hdu = fits.PrimaryHDU() hdu.header['BITPIX'] = -64 hdu.header['NAXIS'] = 2 hdu.header['NAXIS1'] = 512 hdu.header['NAXIS2'] = 512 if write: hdu.data = Z2.T hdu.writeto(filename + '_velfield.fits', clobber=True) else: return Z2, xedges, yedges
def test_2d_multi_values(self): x = self.x y = self.y v = self.v w = self.w stat1v, binx1v, biny1v, bc1v = binned_statistic_2d( x, y, v, 'mean', bins=8) stat1w, binx1w, biny1w, bc1w = binned_statistic_2d( x, y, w, 'mean', bins=8) stat2, binx2, biny2, bc2 = binned_statistic_2d( x, y, [v, w], 'mean', bins=8) assert_allclose(stat2[0], stat1v) assert_allclose(stat2[1], stat1w) assert_allclose(binx1v, binx2) assert_allclose(biny1w, biny2) assert_allclose(bc1v, bc2)
def get_heatmaps(theta, rho, z, theta_bins, z_bins): return [ binned_statistic_2d(theta[i, :, :].flatten(), z[i, :, :].flatten(), rho[i, :, :].flatten(), statistic='mean', bins=[theta_bins, z_bins])[0] for i in range(theta.shape[0]) ]
def fill(self, data, weights=None): if weights is None: weights = np.ones(len(data[0])) if isinstance(weights, list): weights = np.array(weights) self._bin_counts += binned_statistic_2d( x=data[0], y=data[1], values=weights, statistic="sum", bins=self._bin_edges )[0] self._bin_errors_sq += binned_statistic_2d( x=data[0], y=data[1], values=weights ** 2, statistic="sum", bins=self._bin_edges, )[0] self._is_empty = False
def heatmap_fgp(x, y, z, bins=20, title="", cmap=plt.cm.YlOrRd, xlim=(-250, 250), ylim=(422.5, -47.5), facecolor='lightgray', facecolor_alpha=0.4, court_color="black", outer_lines=False, court_lw=0.5, flip_court=False, ax=None, **kwargs): """ Returns an AxesImage object that contains a heatmap of the FG% TODO: Explain parameters """ # Bin the FGA (x, y) and Calculcate the mean number of times shot was # made (z) within each bin # mean is the calculated FG percentage for each bin mean, xedges, yedges, binnumber = binned_statistic_2d(x=x, y=y, values=z, statistic='mean', bins=bins) if ax is None: ax = plt.gca() if not flip_court: ax.set_xlim(xlim) ax.set_ylim(ylim) else: ax.set_xlim(xlim[::-1]) ax.set_ylim(ylim[::-1]) ax.tick_params(labelbottom="off", labelleft="off") ax.set_title(title, fontsize=18) ax.patch.set_facecolor(facecolor) ax.patch.set_alpha(facecolor_alpha) draw_court(ax, color=court_color, lw=court_lw, outer_lines=outer_lines) heatmap = ax.imshow(mean.T, origin='lower', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], interpolation='nearest', cmap=plt.cm.YlOrRd) return heatmap
def test_2d_median(self): x = self.x y = self.y v = self.v stat1, binx1, biny1, bc = binned_statistic_2d(x, y, v, 'median', bins=5) stat2, binx2, biny2, bc = binned_statistic_2d(x, y, v, np.median, bins=5) assert_allclose(stat1, stat2) assert_allclose(binx1, binx2) assert_allclose(biny1, biny2)
def mock_ifu(x1, x2, vel, bins=100, Xrange=None): per = [5, 95] if Xrange is None: range1 = np.percentile(x1, per) range2 = np.percentile(x2, per) Xrange = np.array([range1, range2]) vmap, x1edge, x2edge, binNum = \ binned_statistic_2d(x1, x2, vel, bins=bins, statistic='mean', range=Xrange) return vmap, x1edge, x2edge
def create_heatmap(degree_arrays, network_name, network_title, figure_base): """ For x_degrees, y_degrees pair in degree_arrays, creates and saves a heatmap of the degrees. Parameters ---------- degree_arrays : tuple of numpy arrays (x_degrees, y_degrees) network_name: str network name (string) for naming purposes network_title: str a network-referring title (string) for figures figure_base: str A base path for the heatmap figures. network_name and .pdf extension is added after the figure_base Returns ------- no output, but heatmap figure (as pdf) is saved into the given path """ if degree_arrays: x_degrees, y_degrees = degree_arrays k_min = np.min(degree_arrays) k_max = np.max(degree_arrays) n_bins = k_max - k_min + 1 values = np.zeros(x_degrees.size) statistic = binned_statistic_2d(x=x_degrees, y=y_degrees, bins=n_bins, statistic="count", values=values) fig = plt.figure(figsize=(6, 6)) ax = fig.add_subplot(111) ax.imshow(statistic.statistic, extent=(k_min - 0.5, k_max + 0.5, k_min - 0.5, k_max + 0.5), origin='lower', cmap='hot', interpolation='nearest') ax.set_title(network_title) ax.set_xlabel('Degree $k$') ax.set_ylabel('Degree $k$') add_colorbar(statistic.statistic, cmap='hot') plt.savefig(figure_base + network_name + '.pdf', format='pdf', bbox_inches='tight') print("Heatmap ready!") else: print("Network poorly defined, cannot produce heatmap...")
def buzzard_ks_analysis(bins): import astropy.io.fits as pyfits import scipy.stats as st import scipy.ndimage as ndi import numpy as np path_fits = '/share/splinter/ucapwhi/glimpse_project/scripts/sample_buzzard_rectangle.glimpse.cat.fits' print("Open input file " + path_fits + "...") x = pyfits.open(path_fits) ra = x[1].data.field("ra") dec = x[1].data.field("dec") true_z = x[1].data.field("z") e1 = x[1].data.field("e1") e2 = x[1].data.field("e2") g1 = x[1].data.field("g1") g2 = x[1].data.field("g2") kappa = x[1].data.field("kappa") (binned_g1, x_edge_g1, y_edge_g1, binnumber_g1) = st.binned_statistic_2d(ra, dec, g1, 'mean', bins) (binned_g2, x_edge_g2, y_edge_g2, binnumber_g2) = st.binned_statistic_2d(ra, dec, g2, 'mean', bins) (binned_kappa, x_edge_kappa, y_edge_kappa, binnumber_kappa) = st.binned_statistic_2d(ra, dec, kappa, 'mean', bins) binned_g1 = np.nan_to_num(binned_g1) binned_g2 = np.nan_to_num(binned_g2) binned_kappa = np.nan_to_num(binned_kappa) shear_map = -binned_g1 + binned_g2 * ( 0.0 + 1.0j) # Note the sign conventions used here. ks_kappa = ks(shear_map) real_ks_kappa = np.real(ks_kappa) if False: smoothing_scale_in_pixels = 1 real_ks_kappa = ndi.gaussian_filter(real_ks_kappa, smoothing_scale_in_pixels) binned_kappa = ndi.gaussian_filter(binned_kappa, smoothing_scale_in_pixels) return (binned_kappa, real_ks_kappa)
def test_2d_count(self): x = self.x y = self.y v = self.v count1, binx1, biny1, bc = binned_statistic_2d(x, y, v, "count", bins=5) count2, binx2, biny2 = np.histogram2d(x, y, bins=5) assert_allclose(count1, count2) assert_allclose(binx1, binx2) assert_allclose(biny1, biny2)
def __init__(self, h5file, xvar, yvar, xlimits=None, ylimits=None, xbins=None, ybins=None, lnl_col='-2lnL'): self.h5file = h5file self.xvar = xvar self.yvar = yvar h5 = h5py.File(h5file, 'r') self.n = h5[self.xvar].shape[0] self.chunksize = h5[self.xvar].chunks[0] self.xname = h5[self.xvar].name self.yname = h5[self.yvar].name self.xmin, self.xmax, self.xmean = util.threenum( self.h5file, self.xvar) self.ymin, self.ymax, self.ymean = util.threenum( self.h5file, self.yvar) self.xbins = np.floor(self.n**0.5) if xbins is None else xbins self.ybins = np.floor(self.n**0.5) if ybins is None else ybins self.xnbins = self.xbins if np.isscalar( self.xbins) else self.xbins.shape[0] - 1 self.ynbins = self.ybins if np.isscalar( self.ybins) else self.ybins.shape[0] - 1 self.xlimits = (self.xmin, self.xmax) if xlimits is None else xlimits self.ylimits = (self.ymin, self.ymax) if ylimits is None else ylimits chisq = np.zeros((self.xnbins, self.ynbins)) + 1e100 s = self.chunksize for i in range(0, self.n, s): r = stats.binned_statistic_2d(h5[self.xvar][i:i + s], h5[self.yvar][i:i + s], h5[lnl_col][i:i + s], np.nanmin, bins=[self.xbins, self.ybins], range=[self.xlimits, self.ylimits]) chisq = np.fmin(chisq, r.statistic) chisq = chisq.T self.proflike = np.exp(-(chisq - chisq.min()) / 2.0) self.xbin_edges = r.x_edge self.ybin_edges = r.y_edge self.xcenters = self.xbin_edges[:-1] + np.diff(self.xbin_edges) / 2.0 self.ycenters = self.ybin_edges[:-1] + np.diff(self.ybin_edges) / 2.0 h5.close()
def _create_bins(self): n_spectra = binned_statistic_2d( self.snap_spectra.x, self.snap_spectra.y, np.ones_like(self.snap_spectra.spectrum.flux.transpose(1, 0)), statistic='count', bins=self.bins, expand_binnumbers=True) center_x = (n_spectra.x_edge[1:] + n_spectra.x_edge[:-1]) / 2 center_y = (n_spectra.y_edge[1:] + n_spectra.y_edge[:-1]) / 2 return center_x, center_y, n_spectra
def test_2d_sum(self): x = self.x y = self.y v = self.v sum1, binx1, biny1, bc = binned_statistic_2d(x, y, v, "sum", bins=5) sum2, binx2, biny2 = np.histogram2d(x, y, bins=5, weights=v) assert_allclose(sum1, sum2) assert_allclose(binx1, binx2) assert_allclose(biny1, biny2)
def test_2d_count(self): x = self.x y = self.y v = self.v count1, binx1, biny1, bc = binned_statistic_2d(x, y, v, 'count', bins=5) count2, binx2, biny2 = np.histogram2d(x, y, bins=5) assert_array_almost_equal(count1, count2) assert_array_almost_equal(binx1, binx2) assert_array_almost_equal(biny1, biny2)
def test_2d_sum(self): x = self.x y = self.y v = self.v sum1, binx1, biny1, bc = binned_statistic_2d(x, y, v, 'sum', bins=5) sum2, binx2, biny2 = np.histogram2d(x, y, bins=5, weights=v) assert_array_almost_equal(sum1, sum2) assert_array_almost_equal(binx1, binx2) assert_array_almost_equal(biny1, biny2)
def update_surface_oilfilm_thickness(self): '''The mass of oil is summed within a grid of 20x20 cells covering the oil at a given time. Each oil particle within each cell is given a film thickness as the amount of oil divided by the cell area. ''' from scipy.stats import binned_statistic_2d surface = np.where(self.elements.z == 0)[0] if len(surface) == 0: print('No oil at surface, no film thickness to update') return print('Updating oil film thickness for %s of %s elements at surface' % (len(surface), self.num_elements_active())) meanlon = self.elements.lon[surface].mean() meanlat = self.elements.lat[surface].mean() # Using stereographic coordinates to get regular X and Y psproj = pyproj.Proj('+proj=stere +lat_0=%s +lat_ts=%s +lon_0=%s' % (meanlat, meanlat, meanlon)) X, Y = psproj(self.elements.lon[surface], self.elements.lat[surface]) mass_bin, x_edge, y_edge, binnumber = binned_statistic_2d( X, Y, self.elements.mass_oil[surface], expand_binnumbers=True, statistic='sum', bins=100) bin_area = (x_edge[1] - x_edge[0]) * (y_edge[1] - y_edge[0]) oil_density = 1000 # ok approximation here film_thickness = (mass_bin / oil_density) / bin_area # Postulating min and max film thickness max_thickness = 0.01 # 1 cm min_thickness = 1e-9 # 1 nanometer if film_thickness.max() > max_thickness: print('Warning: decreasing thickness to %sm for %s of %s bins' % (max_thickness, np.sum(film_thickness > max_thickness), film_thickness.size)) film_thickness[film_thickness > max_thickness] = max_thickness num_too_thin = np.sum((film_thickness < min_thickness) & (film_thickness > 0)) if num_too_thin > 0: print('Warning: increasing thickness to %sm for %s of %s bins' % (min_thickness, num_too_thin, film_thickness.size)) film_thickness[film_thickness < min_thickness] = min_thickness # https://github.com/scipy/scipy/issues/7010 binnumber = binnumber - 1 bx = binnumber[0, :] by = binnumber[1, :] # Update thickness self.elements.oil_film_thickness[ surface] = self.elements.oil_film_thickness[surface] * np.nan self.elements.oil_film_thickness[surface] = \ film_thickness[bx, by]
def _new_2d_binning(self): # to have energy on x axis and delay on y axis # Note: the return array from 'stats.binned_statistic_2d' has a swap x and y # axis compared to conventional image data self._a21_heat, _, self._edges2, _ = \ stats.binned_statistic_2d(self._slow1.data(), self._slow2.data(), self._a21.data(), 'mean', [self._n_bins1, self._n_bins2], [self._actual_range1, self._actual_range2]) np.nan_to_num(self._a21_heat, copy=False) self._a21_heat_count, _, _, _ = \ stats.binned_statistic_2d(self._slow1.data(), self._slow2.data(), self._a21.data(), 'count', [self._n_bins1, self._n_bins2], [self._actual_range1, self._actual_range2]) np.nan_to_num(self._a21_heat_count, copy=False)
def xy_plot(x, y, z, x_min, x_max, y_min, y_max, bins, norm, ax): from scipy.stats import binned_statistic_2d cmap = plt.cm.get_cmap('Reds') xbins = np.linspace(x_min,x_max, bins) ybins = np.linspace(y_min,y_max, bins) v = plt.axis() # histogram2d H,xedges,yedges,binnum = binned_statistic_2d(y,x,z,statistic='mean',bins=(xbins,ybins),range = ([y_min,y_max],[x_min,x_max])) # pcolor gci=ax.pcolor(xbins,ybins,H,cmap=cmap, norm=norm) plt.axis('scaled') return gci
def __init__(self, h5file, xvar, yvar, xlimits=None, ylimits=None, xbins=None, ybins=None, post_col='mult'): self.h5file = h5file self.xvar = xvar self.yvar = yvar h5 = h5py.File(h5file, 'r') self.n = h5[self.xvar].shape[0] self.chunksize = h5[self.xvar].chunks[0] self.xname = h5[self.xvar].name self.yname = h5[self.yvar].name self.xmin, self.xmax, self.xmean = util.threenum( self.h5file, self.xvar) self.ymin, self.ymax, self.ymean = util.threenum( self.h5file, self.yvar) self.xbins = np.floor(self.n**0.5) if xbins is None else xbins self.xnbins = self.xbins if np.isscalar( self.xbins) else self.xbins.shape[0] - 1 self.ybins = np.floor(self.n**0.5) if ybins is None else ybins self.ynbins = self.ybins if np.isscalar( self.ybins) else self.ybins.shape[0] - 1 self.xlimits = (self.xmin, self.xmax) if xlimits is None else xlimits self.ylimits = (self.ymin, self.ymax) if ylimits is None else ylimits self.pdf = np.zeros((self.xnbins, self.ynbins)) s = self.chunksize for i in range(0, self.n, s): r = stats.binned_statistic_2d(h5[self.xvar][i:i + s], h5[self.yvar][i:i + s], h5[post_col][i:i + s], 'sum', bins=(self.xbins, self.ybins), range=[self.xlimits, self.ylimits]) self.pdf += r.statistic self.pdf = self.pdf.T self.xbin_edges = r.x_edge self.ybin_edges = r.y_edge self.xcenters = self.xbin_edges[:-1] + np.diff(self.xbin_edges) / 2.0 self.ycenters = self.ybin_edges[:-1] + np.diff(self.ybin_edges) / 2.0 h5.close()
def sampling(self, x, y, z, statistic=[]): #input irregular data - obtained from neural network #N_grid =20 nsample = 1 #1 random sample each time N_grid = self.sample_grid #grid averaging to smooth out huge fluctuations in data if (len(statistic) == 0): statistic, xedges, yedges, binnumber = stats.binned_statistic_2d( x, y, values=z, statistic='mean', range=[[0, 1], [0, 1]], bins=(N_grid, N_grid)) statistic[np.isnan(statistic)] = 0 probDistr = (self.sample_offset / N_grid**2 + (1 - self.sample_offset) * statistic / np.sum(statistic)) xedges = np.linspace(0, 1, num=N_grid + 1) yedges = np.linspace(0, 1, num=N_grid + 1) xcenter = (xedges[1:] + xedges[:-1]) / 2 ycenter = (yedges[1:] + yedges[:-1]) / 2 # generate the set of all x,y pairs represented by the pmf pairs = np.indices( dimensions=(N_grid, N_grid)).T # here are all of the x,y pairs helpindex = np.arange(N_grid**2) # make n random selections from the flattened pmf without replacement # whether you want replacement depends on your application inds = np.random.choice(helpindex, p=probDistr.reshape(-1), size=nsample, replace=True) # inds is the set of n randomly chosen indicies into the flattened dist array... # therefore the random x,y selections # come from selecting the associated elements # from the flattened pairs array selections = pairs.reshape(-1, 2)[inds] dx = xcenter[1] - xcenter[0] dy = ycenter[1] - ycenter[0] xresult = xcenter[selections[:, 1]] + (np.random.rand(nsample) - 0.5) * dx yresult = ycenter[selections[:, 0]] + (np.random.rand(nsample) - 0.5) * dy return (np.transpose([yresult, xresult])), statistic #set values back to [0,1]
def gen_2d_bins(x, y, n): """ Given a series of (x,y) coordinates, bins into a n x n grid """ _, x_edge, y_edge, binnumber_2d = binned_statistic_2d( x, y, x, 'count', bins=np.linspace(0, 430, num=n, endpoint=True), expand_binnumbers=True) binnumber_lin = (binnumber_2d[0] * n) + binnumber_2d[1] return binnumber_2d, binnumber_lin
def sum_on_line_of_sigth(self): flux = np.nan_to_num(self.snap_spectra.spectrum.flux.view(np.ndarray), copy=True) self.binned_stat = binned_statistic_2d(self.snap_spectra.pos[0], self.snap_spectra.pos[1], flux.transpose(1, 0), statistic='sum', bins=self.bins, expand_binnumbers=True) self.datacube = self.binned_stat.statistic.astype( np.float32).transpose(0, 2, 1)
def get_trips(x, y): west = -74.25559 east = -73.70001 south = 40.49612 north = 40.91553 counts = sts.binned_statistic_2d(x, y, None, statistic='count', bins=50, range=[[west, east], [south, north]]) return counts.statistic.ravel()
def test_2d_bincode(self): x = self.x[:20] y = self.y[:20] v = self.v[:20] count1, binx1, biny1, bc = binned_statistic_2d(x, y, v, "count", bins=3) bc2 = np.array([17, 11, 6, 16, 11, 17, 18, 17, 17, 7, 6, 18, 16, 6, 11, 16, 6, 6, 11, 8]) bcount = [(bc == i).sum() for i in np.unique(bc)] assert_allclose(bc, bc2) count1adj = count1[count1.nonzero()] assert_allclose(bcount, count1adj)
def plotMagCorrs(targname, filt, dir='./'): if filt == 'F606W': fils = '_f606w' elif filt == 'F814W': fils = '_f814w' xstr = 'xt1' + fils ystr = 'yt1' + fils magStr = 'magCorr' + fils fileN = np.genfromtxt(dir + 'catWmagCorr.dat', names=True) mean, x_edges, y_edges, _ = stats.binned_statistic_2d(fileN[xstr], fileN[ystr], fileN[magStr], statistic='mean', bins=20, range=[[0, 4096], [0, 4096]]) cm = plt.cm.get_cmap('viridis') # bin_cent_y = (y_edges[1:] + y_edges[:-1]) * 0.5 bin_cent_x = (x_edges[1:] + x_edges[:-1]) * 0.5 # extent = (np.min(bin_cent_x), np.max(bin_cent_x), np.min(bin_cent_y), np.max(bin_cent_y)) fig, ax = plt.subplots(figsize=(6, 6)) # # cax = ax.imshow(mean.T, extent=extent, origin='lower', interpolation='nearest', cmap=cm) cbar = fig.colorbar(cax) title_str = targname + '_' + filt ax.set_title(title_str) plt.savefig(dir + targname + '_' + filt + '_binnedCorr.png', dpi=600, bbox_inches='tight') # plt.show() plt.close() return None
def read_behr_swath(f, swath, bin_lon, bin_lat, CRF_threshold, CF_threshold, CP_threshold, t_window): # Read BEHR variables T = f['Data/'+swath+'/Time_2D'][:] lon = f['Data/'+swath+'/Longitude'][:] lat = f['Data/'+swath+'/Latitude'][:] CRF = f['Data/'+swath+'/CloudRadianceFraction'][:] CP = f['Data/'+swath+'/CloudPressure'][:] CF = f['Data/'+swath+'/CloudFraction'][:] AMFLNOx = f['Data/'+swath+'/BEHRAMFLNOx'][:] LNOx = f['Data/'+swath+'/BEHRColumnAmountLNOxTrop'][:] AMFLNOx_pickering = f['Data/'+swath+'/BEHRAMFLNOx_pickering'][:] LNOx_pickering = f['Data/'+swath+'/BEHRColumnAmountLNOxTrop_pickering'][:] Trop = f['Data/'+swath+'/ColumnAmountNO2Trop'][:] flag = f['Data/'+swath+'/BEHRQualityFlags'][:] # Exclude NaN value T, lon, lat, CRF, CP, CF, AMFLNOx, AMFLNOx_pickering, LNOx, LNOx_pickering, Trop, flag = \ check(T>0, T, lon, lat, CRF, CP, CF, AMFLNOx, AMFLNOx_pickering, LNOx, LNOx_pickering, Trop, flag) # Filter_1.1: CRF and CF filter_CRF = CRF >= CRF_threshold filter_CF = CF >= CF_threshold filter_CP = CP <= CP_threshold T, lon, lat, CRF, CP, AMFLNOx, AMFLNOx_pickering, LNOx, LNOx_pickering, Trop, flag = check(filter_CRF & filter_CF & filter_CP, T, lon, lat, CRF, CP, AMFLNOx, AMFLNOx_pickering, LNOx, LNOx_pickering, Trop, flag) if len(T) == 0: valid_pixels, CRF_bin, CP_bin, AMFLNOx_bin, AMFLNOx_pickering_bin, LNOx_bin, LNOx_pickering_bin, Trop_bin = \ (np.zeros((bin_lon.shape[0]-1, bin_lat.shape[0]-1)) for i in range(8)) else: # Filter_1.2-1.5: quality flags and exclude negative values filter_quality = [not any(x in bad_flags for x in power_find(i)) for i in flag] filter_positive = (LNOx>0) & (LNOx_pickering>0) filter_large = Trop<1E17 filter_nonan = ~np.isnan(AMFLNOx) | ~np.isnan(AMFLNOx_pickering) T, lon, lat, CRF, CP, AMFLNOx, AMFLNOx_pickering, LNOx, LNOx_pickering, flag = check(filter_quality & filter_positive & filter_nonan, T, lon, lat, CRF, CP, AMFLNOx, AMFLNOx_pickering, LNOx, LNOx_pickering, flag) if len(T) == 0: valid_pixels, CRF_bin, CP_bin, AMFLNOx_bin, AMFLNOx_pickering_bin, LNOx_bin, LNOx_pickering_bin, Trop_bin = \ (np.zeros((bin_lon.shape[0]-1, bin_lat.shape[0]-1)) for i in range(8)) else: # Filter_2: Number of pixels meet Filter_1 condition. # This will be used as condition for valid pixels valid_pixels = stats.binned_statistic_2d(lon, lat, None, \ 'count', bins=[bin_lon,bin_lat]).statistic #Bin variables CRF_bin, CP_bin, AMFLNOx_bin, AMFLNOx_pickering_bin, LNOx_bin, LNOx_pickering_bin, Trop_bin\ = bin_mathod(lon, lat, bin_lon, bin_lat, 'mean', CRF, CP, AMFLNOx, AMFLNOx_pickering, LNOx, LNOx_pickering, Trop) return T, lon, lat, valid_pixels, CRF_bin, CP_bin, AMFLNOx_bin, AMFLNOx_pickering_bin, LNOx_bin, LNOx_pickering_bin, Trop_bin
def mkHist(ax,x,y,z,stat,binrange,xlab,ylab,zlab): h, xedges, yedges, binnumber = st.binned_statistic_2d(x,y,z, statistic=stat, bins=50, range=binrange) h = np.rot90(h) h = np.flipud(h) h[np.isnan(h)] = 0.0 h = np.ma.masked_where(h==0,h) h = np.log10(h) mesh = ax.pcolormesh(xedges, yedges, h) ax.set_xlim(binrange[0]) ax.set_ylim(binrange[1]) ax.set_xlabel(xlab) ax.set_ylabel(ylab) cbar = plt.colorbar(mesh,ax=ax,use_gridspec=True) cbar.ax.get_yaxis().labelpad = 20 cbar.ax.set_ylabel(zlab,rotation=270,fontsize=12)
def get_smoothed_map(pvals,evals,czvals,e_coarse_bins,cz_coarse_bins): """Creates a 'smoothed' oscillation probability map with binning given by e_coarse_bins, cz_coarse_bins through the use of the scipy.binned_statistic_2d function. \params: * pvals - array-like object of probability values * evals - array-like object of energy (GeV) values * czvals - array-like object of coszen values * e_coarse_bins - energy bins of final smoothed histogram (probability map) * cz_coarse_bins - coszen bins of final smoothed histogram """ smooth_map = binned_statistic_2d(evals, czvals, pvals, statistic='mean', bins=[e_coarse_bins, cz_coarse_bins])[0] return smooth_map
def energy_dist(data, cuts): # def energy_dist(data, emin=None, emax=None): ratio = np.divide(data['ML_energy'], data['MC_energy'])[cuts] bin_vals = np.arange(-1000, 1015, 15) x = data['MC_x'] x = x[cuts] y = data['MC_y'] y = y[cuts] # if emin is not None and emax is not None: # print('Made it into the mask if statement') # mask = (np.log10(data['MC_energy'])>=emin)*(np.log10(data['MC_energy'])<=emax) # ratio = ratio[mask] # x = x[mask] # y = y[mask] binned_statistic, x_edges, y_edges, binnumber = stats.binned_statistic_2d( x, y, ratio, statistic='median', bins=bin_vals) X, Y = np.meshgrid(x_edges, y_edges) masked_array = np.ma.array(binned_statistic, mask=np.isnan(binned_statistic)) # masked_array = np.ma.masked_less(masked_array,0.25) # cmap = cmaps.plasma cmap = plt.get_cmap('RdBu_r') # cmap = diverge_map(high=('#A00000'),low=('#3F54C0')) cmap.set_bad('w', 1.) # plt.pcolormesh(X, Y, masked_array, cmap=cmap) # plt.pcolormesh(X, Y, masked_array, vmin=0.75, vmax=1.25, cmap=cmap) plt.pcolormesh(X, Y, masked_array, vmin=0.8, vmax=1.2, cmap=cmap) plt.xlabel(r'X [m]') plt.ylabel(r'Y [m]') cb = plt.colorbar(label='$E_{LLH}/E_{MC}$') # itgeo = loadGeometryTxt('IT73_Outline.dat') itgeo = np.load('/data/user/jbourbeau/showerllh/resources/tankpos.npy') itgeo = itgeo.item() itgeo = itgeo['IT73'] plt.scatter(*zip(*itgeo), c = 'white', label = 'Outer IT Tanks', s = 15, linewidth = 0.8, alpha = 0.7) plt.xlim([-1000, 1000]) plt.ylim([-1000, 1000]) plt.title(r'ShowerLLH - IT73 Energy Resolution') outfile = '/home/jbourbeau/public_html/figures/showerLLHstudy/energy_dist.png' checkdir(outfile) plt.savefig(outfile, dpi=300, bbox_inches='tight') plt.close()
def _map(self, param_x, param_y, data=None, method=np.mean, noplot=False, fig=None, ax=None, cax=None, bin_x=50, bin_y=50, cmap="jet", cm_min=None, cm_max=None, axescolor='w', polar=False, showmax=True, **kwargs): """ Return a 2D histogram of the MC chain, showing the walker density per bin """ if param_x not in self.paramstr or param_y not in self.paramstr: print("You must choose param_x and param_y among %s" % self.paramstr) return x = self.chain[param_x] if hasattr(x, 'compressed'): x = x.compressed() y = self.chain[param_y] if hasattr(y, 'compressed'): y = y.compressed() if data is not None: H, bin_y, bin_x = binned_statistic_2d(y, x, data, method, bins=(bin_y, bin_x))[:3] else: H, bin_y, bin_x = np.histogram2d(y, x, bins=(bin_y, bin_x)) H[np.isnan(H)] = np.nanmin(H) maxd = np.unravel_index(np.argmax(H), H.shape) if cm_min is None: cm_min = np.min(H) if cm_max is None: cm_max = np.max(H) cmap, norm, mappable = _core.colorbar(cmap=cmap, cm_min=cm_min, cm_max=cm_max) if noplot: return H, bin_y, bin_x, cmap, norm, mappable if not polar: if fig is None: fig = plt.figure() if ax is None: ax = fig.add_subplot(111) im = mplimageNonUniformImage(ax, cmap=cmap, norm=norm, interpolation='bilinear') arrbin_x = _core.bins_to_array(bin_x) arrbin_y = _core.bins_to_array(bin_y) im.set_data(arrbin_x, arrbin_y, H) ax.images.append(im) if showmax is True: ax.plot(arrbin_x[maxd[1]], arrbin_y[maxd[0]], '^w', ms=7) ax.set_xlim(bin_x[0], bin_x[-1]) ax.set_xlabel(param_x) ax.set_ylim(bin_y[0], bin_y[-1]) ax.set_ylabel(param_y) else: if ax is None: fig, ax = plt.subplots(subplot_kw={'projection':'polar'}) T,R = np.meshgrid(bin_x,bin_y) pax = ax.pcolormesh(T, R, H, cmap=cmap, norm=norm) ax.grid(True) if showmax is True: plt.polar(T[maxd], R[maxd], '^w', ms=7) ax.set_ylim(0, bin_y[-1]) ax.set_title(param_x+' vs '+param_y) ax.grid(True, color=axescolor) ax.tick_params(axis='both', colors=axescolor) fig.colorbar(mappable, cax=cax)
def __init__(self, h5file, xvar, yvar, xlimits=None, ylimits=None, xbins=None, ybins=None, lnl_col='-2lnL'): self.h5file = h5file self.xvar = xvar self.yvar = yvar h5 = h5py.File(h5file, 'r') self.n = h5[self.xvar].shape[0] self.chunksize = h5[self.xvar].chunks[0] self.xname = h5[self.xvar].name self.yname = h5[self.yvar].name self.xmin, self.xmax, self.xmean = util.threenum(self.h5file, self.xvar) self.ymin, self.ymax, self.ymean = util.threenum(self.h5file, self.yvar) self.xbins = np.floor(self.n**0.5) if xbins is None else xbins self.ybins = np.floor(self.n**0.5) if ybins is None else ybins self.xnbins = self.xbins if np.isscalar(self.xbins) else self.xbins.shape[0] -1 self.ynbins = self.ybins if np.isscalar(self.ybins) else self.ybins.shape[0] -1 self.xlimits = (self.xmin, self.xmax) if xlimits is None else xlimits self.ylimits = (self.ymin, self.ymax) if ylimits is None else ylimits chisq = np.zeros((self.xnbins, self.ynbins)) + 1e100 s = self.chunksize for i in range(0, self.n, s): r = stats.binned_statistic_2d(h5[self.xvar][i:i+s], h5[self.yvar][i:i+s], h5[lnl_col][i:i+s], np.nanmin, bins=[self.xbins, self.ybins], range=[self.xlimits, self.ylimits]) chisq = np.fmin(chisq, r.statistic) chisq = chisq.T self.proflike = np.exp(-(chisq - chisq.min())/2.0) self.xbin_edges = r.x_edge self.ybin_edges = r.y_edge self.xcenters = self.xbin_edges[:-1] + np.diff(self.xbin_edges)/2.0 self.ycenters = self.ybin_edges[:-1] + np.diff(self.ybin_edges)/2.0 h5.close()
def mkHist(ax,x,y,z,stat,xlabel,ylabel,lox,hix,loy,hiy): numbins = 500 #stat = 'count' binrange = [[lox,hix],[loy,hiy]] h,xedges,yedges,binnumber = st.binned_statistic_2d(x,y,z, statistic=stat, bins=numbins, range=binrange) h = np.rot90(h) h = np.flipud(h) h[np.isnan(h)] = 0.0 h = np.ma.masked_where(h==0,h) h = np.log10(h) mesh = ax.pcolormesh(xedges,yedges,h,vmin=0) ax.set_xlim(binrange[0]) ax.set_ylim(binrange[1]) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) cbar = plt.colorbar(mesh,ax=ax,use_gridspec=True, format=ticker.FuncFormatter(fmt) ) return h,xedges,yedges,binnumber
def test_2d_binnumbers_unraveled(self): x = self.x y = self.y v = self.v stat, edgesx, bcx = binned_statistic(x, v, "mean", bins=10) stat, edgesy, bcy = binned_statistic(y, v, "mean", bins=10) stat2, edgesx2, edgesy2, bc2 = binned_statistic_2d(x, y, v, "mean", bins=10, expand_binnumbers=True) bcx3 = np.searchsorted(edgesx, x, side="right") bcy3 = np.searchsorted(edgesy, y, side="right") # `numpy.searchsorted` is non-inclusive on right-edge, compensate bcx3[x == x.max()] -= 1 bcy3[y == y.max()] -= 1 assert_allclose(bcx, bc2[0]) assert_allclose(bcy, bc2[1]) assert_allclose(bcx3, bc2[0]) assert_allclose(bcy3, bc2[1])
def mkHist(ax,n,t,z,stat,cbarLabel): numbins = 200 binrange = [[-10,2],[2,8]] h,xedges,yedges,binnumber = st.binned_statistic_2d(n,t,z, statistic=stat,range=binrange, bins=numbins) h = np.rot90(h) h = np.flipud(h) h[np.isnan(h)] = 0.0 h = np.ma.masked_where(h==0,h) h = np.log10(h) mesh = ax.pcolormesh(xedges,yedges,h) ax.set_xlim(binrange[0]) ax.set_ylim(binrange[1]) ax.set_xlabel('Density [cm$^{-3}$]') ax.set_ylabel('Temperature [K]') cbar = plt.colorbar(mesh,ax=ax,use_gridspec=True) cbar.ax.get_yaxis().labelpad = 20 cbar.ax.set_ylabel(cbarLabel,rotation=270,fontsize=12)