def get_hess(obs_mags_cols, synth_phot, hess_xedges, hess_yedges): """ Hess diagram of observed minus best match synthetic cluster. """ # 2D histogram of the observed cluster. cl_histo = np.histogram2d( *obs_mags_cols, bins=[hess_xedges, hess_yedges])[0] # 2D histogram of the synthetic cluster. syn_histo = np.histogram2d(*synth_phot, bins=[hess_xedges, hess_yedges])[0] # Grid for pcolormesh. hess_x, hess_y = np.meshgrid(hess_xedges, hess_yedges) # Hess diagram: observed minus synthetic. hess_diag = np.array([]) if syn_histo.size: hess_diag = cl_histo - syn_histo if hess_diag.size: HD = np.rot90(hess_diag) HD = np.flipud(HD) else: HD = np.array([]) if not HD.size: print(" WARNING: the Hess diagram could no be obtained.") return hess_x, hess_y, HD
def plot_roc2(trans_w, trans_g, xax_index, yax_index, nComps=0, doCombinedFitting=True): wx = get_vals(trans_w, xax_index) gx = get_vals(trans_g, xax_index) wy = get_vals(trans_w, yax_index) gy = get_vals(trans_g, yax_index) low_x = min(np.min(wx), np.min(gx)) low_y = min(np.min(wy), np.min(gy)) high_x = max(np.max(wx), np.max(gx)) high_y = max(np.max(wy), np.max(gy)) nbins = max(5, min(30,int(math.sqrt(gy.shape[0]/100.0)))) h_w, xedges, yedges = np.histogram2d(wx, wy, bins=(nbins, nbins), range=((low_x, high_x),(low_y, high_y))) h_g, xedges, yedges = np.histogram2d(gx, gy, bins=(nbins, nbins), range=((low_x, high_x),(low_y, high_y))) simulated_ll = eff_eff_pair(h_w, h_g) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(np.linspace(0,1,10), np.linspace(0,1,10), 'k-') ax.plot(simulated_ll [:,1], simulated_ll [:,0], 'go', label='Simulated LHood') ax.set_xlabel('Background Efficiency', size='x-large') ax.set_ylabel('Signal Efficiency', size='x-large') ax.set_ylim(-.1, 1.4) ax.set_xlim(-.1, 1.1) plt.suptitle('ROC for Index=%s & %s' % \ (makeTitle(xax_index,nComps,doCombinedFitting), makeTitle(yax_index,nComps,doCombinedFitting))) ax.grid() plt.legend()
def get_smoothed_map(prob_map,ebinsLT,czbinsLT,ebinsSM,czbinsSM): ''' Downsamples a map by averaging over the look up table bins whose bin center is within the new (coarser) binning. DOES NOT assume that the new (SM) binning is divisible by the old (LT) binning. The algorithm is that a new histogram is created from the entirety of the data in the Lookup Table. NOTATION: LT - "lookup table" (finely binned) SM - "smoothed" binning ''' ecenLT = get_bin_centers(ebinsLT) czcenLT = get_bin_centers(czbinsLT) elist = [] czlist = [] weight_list = [] for ie,egy in enumerate(ecenLT): for icz,cz in enumerate(czcenLT): czlist.append(cz) elist.append(egy) weight_list.append(prob_map[ie][icz]) map_sum_wts = np.histogram2d(elist,czlist,weights=weight_list, bins=[ebinsSM,czbinsSM])[0] map_num = np.histogram2d(elist,czlist,bins=[ebinsSM,czbinsSM])[0] return np.divide(map_sum_wts,map_num)
def f3d_to_fcylavg(f, x, y, z, bins=None, log=False): """Cylindrically average a function defined on a 3D grid Parameters ---------- f : x : y : z : bins : Number of bins, in the form (ks, kpar) Returns ------- """ if bins == None: # Count the maximum number of gridpts outward from origin in x, y, or z, then subtract 1 bins_prp = max([ max( np.count_nonzero(a>0), np.count_nonzero(a<0) ) for a in (x,y) ]) - 1 bins_par = max( np.count_nonzero(z>0), np.count_nonzero(z<0) ) - 1. bins = (bins_prp, bins_par) # Get (r,z) cylindrical bin edges from (x,y,z) cartesian grid rcylbins = xyz_to_rcylbins(x,y,z, bins=bins, log=log) xx,yy,zz = np.meshgrid(x, y, z, indexing='ij') rr = np.sqrt( xx**2 + yy**2 ) # 3D grid of r (cylindrical radius from origin) nn = np.histogram2d(rr.ravel(), zz.ravel(), bins=rcylbins)[0] # Number of cells used to compute average favg= np.histogram2d(rr.ravel(), zz.ravel(), bins=rcylbins, weights=f.ravel())[0] / nn rmid_prp = _bin_midpoints(rcylbins[0]) rmid_par = _bin_midpoints(rcylbins[1]) return rmid_prp, rmid_par, favg
def test_simple(self): x = array( [0.41702200, 0.72032449, 1.1437481e-4, 0.302332573, 0.146755891]) y = array( [0.09233859, 0.18626021, 0.34556073, 0.39676747, 0.53881673]) xedges = np.linspace(0, 1, 10) yedges = np.linspace(0, 1, 10) H = histogram2d(x, y, (xedges, yedges))[0] answer = array( [[0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]) assert_array_equal(H.T, answer) H = histogram2d(x, y, xedges)[0] assert_array_equal(H.T, answer) H, xedges, yedges = histogram2d(list(range(10)), list(range(10))) assert_array_equal(H, eye(10, 10)) assert_array_equal(xedges, np.linspace(0, 9, 11)) assert_array_equal(yedges, np.linspace(0, 9, 11))
def LogHistPlot(self, save=None, binstep=0.2, figsize=(8, 8), ax=None, inds=None): """Plots a density histogram.""" if figsize is not None and ax is None: plt.figure(figsize=figsize) if ax is None: ax = plt.subplot(111) ax.set_axis_bgcolor('black') dr = np.array([self.__data[0].min(), self.__data[1].min(), self.__data[0].max(), self.__data[1].max()]) dr = np.hstack((np.floor(dr[:2]), np.ceil(dr[2:]))) if inds is None: n, xb, yb = np.histogram2d( self.__data[0], self.__data[1], bins=(np.arange(dr[0], dr[2], binstep), np.arange(dr[1], dr[3], binstep))) else: n, xb, yb = np.histogram2d( self.__data[0][inds], self.__data[1][inds], bins=(np.arange(dr[0], dr[2], binstep), np.arange(dr[1], dr[3], binstep))) rateMasked = np.ma.array(n, mask=(n <= 0)) cmap = plt.cm.RdBu_r cmap.set_bad('k') plt.imshow(np.ma.log10(rateMasked).T, cmap=cmap, extent=[xb.min(), xb.max(), yb.min(), yb.max()], interpolation='none', origin='lower') plt.axis('equal') plt.xlim((xb.min(), xb.max())) plt.ylim((yb.min(), yb.max())) if save is not None: plt.savefig(save) return ax
def test_binparameter_combination(self): x = array( [0, 0.09207008, 0.64575234, 0.12875982, 0.47390599, 0.59944483, 1]) y = array( [0, 0.14344267, 0.48988575, 0.30558665, 0.44700682, 0.15886423, 1]) edges = (0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1) H, xe, ye = histogram2d(x, y, (edges, 4)) answer = array( [[ 2., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 1.]]) assert_array_equal(H, answer) assert_array_equal(ye, array([0., 0.25, 0.5, 0.75, 1])) H, xe, ye = histogram2d(x, y, (4, edges)) answer = array( [[ 1., 1., 0., 1., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 1., 0., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]]) assert_array_equal(H, answer) assert_array_equal(xe, array([0., 0.25, 0.5, 0.75, 1]))
def main(mask='out/200607/*nc4'): dset = da.read_nc(mask, ['lon', 'lat', 'cloudpts'], axis='tai_time') cloudy = 1 * (dset['cloudpts'] > 0) h, xx, yy = np.histogram2d(dset['lon'].values, dset['lat'].values, bins=[lonbins, latbins], weights=cloudy) nprof, xx, yy = np.histogram2d(dset['lon'].values, dset['lat'].values, bins=[lonbins, latbins]) map_show(lonbins, latbins, h, nprof)
def radprof3D(field, xx, yy, z, center, nbins): """ compute radial profile from the 'center' point for 3D field assumes square, periodic domain, given by x,y. """ #nx = len(xx[:,0]) #ny = len(yy[:,0]) nz = z.size width = xx[0,-1] delx = np.abs(xx - center[0]) dely = np.abs(yy - center[1]) delx[delx > width/2] = width - delx[delx > width/2] dely[dely > width/2] = width - dely[dely > width/2] r = np.sqrt(np.power(delx, 2) + np.power(dely, 2)) r = r.ravel() field = field.reshape(nz, r.size) #r = np.repeat(r[np.newaxis,:], z.size, axis=0) rr, zz = np.meshgrid(r, z) fieldsums, redges, zedges = np.histogram2d(rr.ravel(), zz.ravel(), bins=nbins, weights=field.ravel()) nr, redges, zedges = np.histogram2d(rr.ravel(), zz.ravel(), bins=nbins) fieldmeans = fieldsums/nr return (redges, zedges, fieldmeans)
def bin_an_array_2d(input_array, radius_array_x, radius_array_y, bins_x, bins_y): """Bin the points in an array by radius (n-dim) Parameters ---------- input_array: np.ndarray array over which to bin radius_array_x: np.ndarray array of the x-vectors at each point in the series radius_array_y: np.ndarray array of the y-vectors at each point in the series bins_x and bins_y: np.ndarray the bins in x and y """ radius_flat_x = radius_array_x.flatten() radius_flat_y = radius_array_y.flatten() arr_flat = input_array.flatten() counts_histo = np.histogram2d(radius_flat_x, radius_flat_y, bins=(bins_x, bins_y))[0] binsum_histo = np.histogram2d(radius_flat_x, radius_flat_y, bins=(bins_x, bins_y), weights=arr_flat)[0] binavg = binsum_histo / counts_histo.astype(float) return counts_histo, binavg
def plot_predicted_vs_observed_pks(obs_score, pred_score, ofname): plt.figure() heatmap, xedges, yedges = numpy.histogram2d( rankdata(-obs_score, method='ordinal'), rankdata(pred_score, method='ordinal'), bins=20) heatmap, xedges, yedges = numpy.histogram2d( numpy.clip(-numpy.log(1+obs_score), -0.1, 0), numpy.clip(numpy.log(1+pred_score), 0, 0.1), bins=100) #heatmap, xedges, yedges = numpy.histogram2d( # numpy.clip(-numpy.log(1+data['ATAC_mean']), -0.1, 0), # numpy.clip(numpy.log(y), 0, 0.1), # bins=100) #heatmap, xedges, yedges = numpy.histogram2d( # rankdata(-data['ATAC_mean'], method='average'), # rankdata(obs_score, method='average'), # bins=20) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] plt.clf() plt.imshow(heatmap, extent=extent) #plt.scatter(rankdata(obs_score, method='ordinal'), # rankdata(pred_score, method='ordinal')) plt.savefig(ofname) plt.close() return
def create_contingency_table ( data, dico, x, y, z ): # détermination de la taille de z size_z = 1 offset_z = np.zeros ( len ( z ) ) j = 0 for i in z: offset_z[j] = size_z size_z *= len ( dico[i] ) j += 1 # création du tableau de contingence res = np.zeros ( size_z, dtype = object ) # remplissage du tableau de contingence if size_z != 1: z_values = np.apply_along_axis ( lambda val_z : val_z.dot ( offset_z ), 1, data[z,:].T ) i = 0 while i < size_z: indices, = np.where ( z_values == i ) a,b,c = np.histogram2d ( data[x,indices], data[y,indices], bins = [ len ( dico[x] ), len (dico[y] ) ] ) res[i] = ( indices.size, a ) i += 1 else: a,b,c = np.histogram2d ( data[x,:], data[y,:], bins = [ len ( dico[x] ), len (dico[y] ) ] ) res[0] = ( data.shape[1], a ) return res
def cached(data): cols = datatable.get_cols(*markers) if not range: fixed_range = [ min(cols[0]), min(cols[1]), max(cols[0]), max(cols[1])] else: fixed_range = range hist, data.x_edges, data.y_edges = np.histogram2d( cols[0], cols[1], [ np.r_[fixed_range[0]:fixed_range[2]:no_bins], np.r_[fixed_range[1]:fixed_range[3]:no_bins]]) data.final_hist = np.sign(np.subtract( np.clip(np.abs(hist), min_cells_per_bin, np.inf), min_cells_per_bin)) if color_marker: data.is_colored = True weights = datatable.get_cols(color_marker)[0] weighted_hist, x_edges, y_edges = np.histogram2d( cols[0], cols[1], [ np.r_[fixed_range[0]:fixed_range[2]:no_bins], np.r_[fixed_range[1]:fixed_range[3]:no_bins]], None, False, weights) data.colored_hist = np.multiply( np.true_divide(weighted_hist, hist), data.final_hist) else: data.is_colored = False
def Get_TProfile(ar1, ar2, nbBin1, we2): ''' Create a plot similar to a ROOT TProfile ''' number, axisX, axisY = numpy.histogram2d(ar1, ar2, (nbBin1,1)) weight, axisX, axisY = numpy.histogram2d(ar1, ar2, (nbBin1,1), weights=we2) mean, axisX, axisY = numpy.histogram2d(ar1, ar2, (nbBin1,1), weights=we2*ar2) err, axisX, axisY = numpy.histogram2d(ar1, ar2, (nbBin1,1), weights=we2*(ar2**2.)) ### find the axis X axisX = numpy.array([ axisX[i]+(axisX[i+1]-axisX[i])/2. for i in range(0,axisX.size-1) ]) ### Get only not empty bins bool_number = (number[:,0]>1) axisX = axisX[bool_number] number = number[:,0][bool_number] weight = weight[:,0][bool_number] mean = mean[:,0][bool_number] err = err[:,0][bool_number] mean = mean/weight err = numpy.sqrt((err/weight-mean**2.)/number) return axisX, mean, err, number
def bootstrap_estimate(RA, DEC, gamma, RAbins, DECbins, Nbootstrap=1000): N = len(gamma) gamma_sum = 0 gamma2_sum = 0 for i in range(Nbootstrap): ind = np.random.randint(N, size=N) RAi = RA[ind] DECi = DEC[ind] gammai = gamma[ind] count = np.histogram2d(RAi, DECi, (RAbins, DECbins)) shear = np.histogram2d(RAi, DECi, (RAbins, DECbins), weights=gammai) mask = (count == 0) count[mask] = 1 shear /= count gamma_sum += shear gamma2_sum += abs(shear) ** 2 gamma_sum /= Nbootstrap gamma2_sum /= Nbootstrap d2gamma = gamma2_sum - abs(gamma_sum ** 2)
def get_fluxmap(self,eners, local_coords, resolution): # Bottom: xs_bot = N.linspace(-self._half_dims[0,0], self._half_dims[0,0], resolution+1) ys_bot = N.linspace(-self._half_dims[1,0], self.extr_center[1]-self.extr_half_dims[1,0], resolution+1) eners_bot = N.histogram2d(local_coords[0], local_coords[1], bins=[xs_bot, ys_bot], weights=eners)[0] dxs_bot = N.tile(N.abs(xs_bot[1:]-xs_bot[:-1]), (len(ys_bot)-1,1)) dys_bot = N.tile(N.abs(ys_bot[1:]-ys_bot[:-1]), (len(xs_bot)-1,1)).T areas_bot = dxs_bot*dys_bot flux_bot = eners_bot/areas_bot # Middle left: xs_mid_left = N.linspace(-self._half_dims[0,0], self.extr_center[0]-self.extr_half_dims[0,0], resolution+1) ys_mid_left = N.linspace(self.extr_center[1]-self.extr_half_dims[1,0], self.extr_center[1]+self.extr_half_dims[1,0], resolution+1) eners_mid_left = N.histogram2d(local_coords[0], local_coords[1], bins=[xs_mid_left, ys_mid_left], weights=eners)[0] dxs_mid_left = N.tile(N.abs(xs_mid_left[1:]-xs_mid_left[:-1]), (len(ys_mid_left)-1,1)) dys_mid_left = N.tile(N.abs(ys_mid_left[1:]-ys_mid_left[:-1]), (len(xs_mid_left)-1,1)).T areas_mid_left = dxs_mid_left*dys_mid_left flux_mid_left = eners_mid_left/areas_mid_left # Middle right: xs_mid_right = N.linspace(self.extr_center[0]+self.extr_half_dims[0,0], self._half_dims[0,0], resolution+1) ys_mid_right = N.linspace(self.extr_center[1]-self.extr_half_dims[1,0], self.extr_center[1]+self.extr_half_dims[1,0], resolution+1) eners_mid_right = N.histogram2d(local_coords[0], local_coords[1], bins=[xs_mid_right, ys_mid_right], weights=eners)[0] dxs_mid_right = N.tile(N.abs(xs_mid_right[1:]-xs_mid_right[:-1]), (len(ys_mid_right)-1,1)) dys_mid_right = N.tile(N.abs(ys_mid_right[1:]-ys_mid_right[:-1]), (len(xs_mid_right)-1,1)).T areas_mid_right = dxs_mid_right*dys_mid_right flux_mid_right = eners_mid_right/areas_mid_right # Top: xs_top = N.linspace(-self._half_dims[0,0], self._half_dims[0,0], resolution+1) ys_top = N.linspace(self.extr_center[1]+self.extr_half_dims[1,0], self._half_dims[1,0], resolution+1) eners_top = N.histogram2d(local_coords[0], local_coords[1], bins=[xs_top, ys_top], weights=eners)[0] dxs_top = N.tile(N.abs(xs_top[1:]-xs_top[:-1]), (len(ys_top)-1,1)) dys_top = N.tile(N.abs(ys_top[1:]-ys_top[:-1]), (len(xs_top)-1,1)).T areas_top = dxs_top*dys_top flux_top = eners_top/areas_top return N.hstack(flux_bot), N.hstack(flux_mid_left), N.hstack(flux_mid_right), N.hstack(flux_top)
def basic_imshow(ax,xfield,yfield1,yfield2,plot_type='holding',xkey='temp'): if plot_type=='emis': yfield1,yfield2,xfield = yfield1.flat,yfield2.flat,xfield.flat idx1 = np.where(yfield1 > 1.)[0] yfield1,yfield2,xfield = yfield1[idx1],yfield2[idx1],xfield[idx1] idx2 = np.where(yfield2 > 1.)[0] yfield1,yfield2,xfield = yfield1[idx2],yfield2[idx2],xfield[idx2] ion_frac = np.log10(yfield1/yfield2) if plot_type=='coldens': yfield1,yfield2,xfield = yfield1.flat,yfield2.flat,xfield.flat idx1 = np.where(np.log10(yfield1) > 12.)[0] yfield1,yfield2,xfield = yfield1[idx1],yfield2[idx1],xfield[idx1] idx2 = np.where(np.log10(yfield2) > 12.)[0] yfield1,yfield2,xfield = yfield1[idx2],yfield2[idx2],xfield[idx2] ion_frac = np.log10(yfield1/yfield2) else: ion_frac = yfield1/yfield2 xfield = xfield.flat ion_frac = np.log10(ion_frac.flat) if xkey == 'temp': hist,xedges,yedges = np.histogram2d(np.log10(xfield),ion_frac,range=[[4,6.5],[-3,3]],bins=100) extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]] ax.imshow(np.log10(hist.T),origin='lower',extent=[4,6.5,-3,3],interpolation='nearest',cmap='jet',vmin=-3.,vmax=3.) if xkey == 'dens': hist,xedges,yedges = np.histogram2d(np.log10(xfield),ion_frac,range=[[-5,-1],[-4,2]],bins=100) extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]] ax.imshow(np.log10(hist.T),origin='lower',extent=[-5,-1,-4,2],interpolation='nearest',cmap='jet',vmin=-3.,vmax=3.) if xkey == 'NH': hist,xedges,yedges = np.histogram2d(np.log10(xfield),ion_frac,range=[[18,22],[-6,3]],bins=100) extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]] ax.imshow(np.log10(hist.T),origin='lower',extent=[18,22,-6,3],interpolation='nearest',cmap='jet',vmin=-3.,vmax=3.) ax.set_aspect('auto') return
def plot_correlations(filenames, limit=None): DataFrame = pd.DataFrame() index = 0 for fileName in filenames: with pd.get_store(fileName, 'r') as store: tempDataFrame = pd.DataFrame({'Event': store.Hits.Event[:15000], 'Row' + str(index): store.Hits.Row[:15000]}) tempDataFrame = tempDataFrame.set_index('Event') DataFrame = tempDataFrame.join(DataFrame) DataFrame = DataFrame.dropna() index += 1 del tempDataFrame DataFrame["index"] = DataFrame.index DataFrame.drop_duplicates(take_last=True, inplace=True) del DataFrame["index"] correlationNames = ('Row') index = 0 for corName in correlationNames: for colName in itertools.permutations(DataFrame.filter(regex=corName), 2): if(corName == 'Col'): heatmap, xedges, yedges = np.histogram2d(DataFrame[colName[0]], DataFrame[colName[1]], bins=(80, 80), range=[[1, 80], [1, 80]]) else: heatmap, xedges, yedges = np.histogram2d(DataFrame[colName[0]], DataFrame[colName[1]], bins=(336, 336), range=[[1, 336], [1, 336]]) extent = [yedges[0] - 0.5, yedges[-1] + 0.5, xedges[-1] + 0.5, xedges[0] - 0.5] cmap = cm.get_cmap('hot', 40) fig = Figure() FigureCanvas(fig) ax = fig.add_subplot(111) ax.imshow(heatmap, extent=extent, cmap=cmap, interpolation='nearest') ax.invert_yaxis() ax.set_xlabel(colName[0]) ax.set_ylabel(colName[1]) ax.set_title('Correlation plot(' + corName + ')') fig.savefig(colName[0] + '_' + colName[1] + '.pdf') index += 1
def main(): x = np.random.normal(0, 50, 50000) y = np.random.normal(0, 15, 50000) ranges = ([(-100, 0), (-50, 0)], [(0, 100), (-50, 0)], [(-100, 0), (0, 50)], [(0, 100), (0, 50)]) types = ('reverse_bw', 'bw', 'reverse_bw', 'bw') bitmaps = (True, True, False, False) plot = Plot() for r, t, b in zip(ranges, types, bitmaps): n, xbins, ybins = np.histogram2d(x, y, bins=15, range=r) plot.histogram2d(n, xbins, ybins, type=t, bitmap=b) plot.save('histogram2d') plot = MultiPlot(2, 2, width=r'.5\linewidth') subplot_idxs = [(1, 0), (1, 1), (0, 0), (0, 1)] for idx, r, t, b in zip(subplot_idxs, ranges, types, bitmaps): p = plot.get_subplot_at(*idx) n, xbins, ybins = np.histogram2d(x, y, bins=15, range=r) p.histogram2d(n, xbins, ybins, type=t, bitmap=b) plot.show_yticklabels_for_all([(1, 0), (0, 1)]) plot.show_xticklabels_for_all([(1, 0), (0, 1)]) plot.save('multi_histogram2d')
def sample_agency_magnitude_pairs(data, xbins, ybins, number_samples=1): """ """ keys = data.keys() n_data = len(data[keys[0]]) if not number_samples or (number_samples == 1): # Only one sample, return simple histogram #print xbins, ybins return np.histogram2d(np.around(data[keys[0]], 2), np.around(data[keys[2]], 2), bins=[xbins, ybins])[0] elif (np.max(data[keys[1]]) < 1E-15) and (np.max(data[keys[3]]) < 1E-15): # No uncertainty on magnitudes return np.histogram2d(np.around(data[keys[0]], 2), np.around(data[keys[2]], 2), bins=[xbins, ybins])[0] else: counter = np.zeros([len(xbins) - 1, len(ybins) - 1]) for i in xrange(number_samples): # Sample data sets data_x = data[keys[0]] + data[keys[1]] * np.random.normal(0., 1., n_data) data_y = data[keys[2]] + data[keys[3]] * np.random.normal(0., 1., n_data) counter += np.histogram2d(data_x, data_y, bins=[xbins, ybins])[0] return counter / float(number_samples)
def plot_weighted_2d(x,y,weights,disc,nbins,f_out): # Create some random numbers # Plot data plt.plot(x,y,'.r') plt.xlabel('x') plt.ylabel('y') # Estimate the 2D histogram H1, xedges, yedges = np.histogram2d(x,y,bins=nbins) H, xedges, yedges = np.histogram2d(x,y,bins=nbins,weights = weights) H1[H==0],H[H==0] = 1,1 H = H/H1 # 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 # Plot 2D histogram using pcolor fig2 = plt.figure() plt.pcolormesh(xedges,yedges,Hmasked) plt.xlabel('$\\Delta$ R(e-$\\gamma$)',fontsize = 20) plt.ylabel('$\\gamma$ Energy in MeV',fontsize = 20) plt.title('$\\epsilon$(disc > %.2f),NEvents = %.2E' %(disc,len(x)),fontsize=20,loc='left') cbar = plt.colorbar() cbar.ax.set_ylabel('Counts') plt.savefig('%s.png' %(f_out)) output = open('%s.pkl' %(f_out), 'wb') pkl.dump({'eff_tuple':(xedges,yedges,Hmasked)},output)
def contourplot(x,y,wgt=None,ax=None,\ nxbins=25,nybins=25,flist=[0.683,0.954], linestyle='-',color='k',linewidths=3): """ Input optional weight to make a weighted 2d histogram. flist is fraction of points you want to enclose. """ if wgt is None: H, xedges, yedges = np.histogram2d(x,y,[nxbins,nybins]) else: H, xedges, yedges = np.histogram2d(x,y,[nxbins,nybins],weights=wgt) Hflip = np.zeros([nybins,nxbins]) # for some idiotic reason, contour progam bins are flipped. xcen = (xedges[1:] + xedges[:-1])*0.5 ycen = (yedges[1:] + yedges[:-1])*0.5 for x in range(nxbins): for y in range(nybins): Hflip[y,x] = H[x,y] clist = getclevels(Hflip,flist) if ax is None: f = plt.figure() ax = f.add_subplot(111) ax.contour(xcen,ycen,Hflip,levels=clist,linestyles=linestyle,colors=color,linewidths=linewidths) return f, ax else: ax.contour(xcen,ycen,Hflip,levels=clist,linestyles=linestyle,colors=color,linewidths=linewidths)
def conn_tests(solver, save_format): print '>>>', datetime.now(), 'Begin conn tests' solver.net.set_phase_test() solver.test_nets[0].share_with(solver.net) net = solver.test_nets[0] save_dir = save_format.format(solver.iter) os.mkdir(save_dir) hist = np.zeros((2, 2)) for fname in fnames_val: net.forward() im = Image.fromarray(net.blobs['left-conn'].data[0].argmax(0) .astype(np.uint8), mode='P') im.save(os.path.join(save_dir, fname + '-L.png')) im = Image.fromarray(net.blobs['top-conn'].data[0].argmax(0) .astype(np.uint8), mode='P') im.save(os.path.join(save_dir, fname + '-T.png')) h, _ , _ = np.histogram2d(net.blobs['left-gt'].data[0, 0].flatten(), net.blobs['left-conn'].data[0].argmax(0).flatten(), bins = 2, range=[[0, 2], [0, 2]]) hist += h h, _ , _ = np.histogram2d(net.blobs['top-gt'].data[0, 0].flatten(), net.blobs['top-conn'].data[0].argmax(0).flatten(), bins = 2, range=[[0, 2], [0, 2]]) hist += h print '>>>', datetime.now(), 'Iteration', solver.iter, 'overall accuracy', \ np.diag(hist).sum() / hist.sum() print '>>>', datetime.now(), 'Iteration', solver.iter, 'total IU', \ hist[1, 1] / (hist[0, 1] + hist[1, 0] + hist[1, 1]) solver.net.set_phase_train()
def dualHistogram2D(X1,Y1,X2,Y2,res=16,title1='Pairs',title2='Isolated',xLabel= "Proj. Dis (Mpc) binned",yLabel= "dv (km/s) binned"): hist1,xedges1, yedges1= numpy.histogram2d(X1,Y1,bins=res) hist2,xedges2, yedges2= numpy.histogram2d(X2,Y2,bins=res) #print xedges1," ",yedges1 #create bar graphs fig = plt.figure() #set ranges #extent1 = [yedges1[0], yedges1[-1], xedges1[-1], xedges1[0]] plot1 = fig.add_subplot(121) im1 = plot1.imshow(hist1,interpolation='nearest') plot1.set_ylabel(yLabel) plot1.set_xlabel(xLabel) plot1.set_title(title1) fig.colorbar(im1) #set ranges extent2 = [yedges2[0], yedges2[-1], xedges2[-1], xedges2[0]] plot2 = fig.add_subplot(122) im2 = plot2.imshow(hist2,interpolation='nearest') plot2.set_ylabel(yLabel) plot2.set_xlabel(xLabel) plot2.set_title(title2) fig.colorbar(im2) plt.show()
def get_corr_map(coo1, coo2, skypos, skyrange): imsz = imagetools.deg2pix(skypos, skyrange, 0.001) count = np.zeros(imsz) print(imsz) co_rel = np.array([[0,0]]) len1 = coo1.shape[0] len2 = coo2.shape[0] print(len1,len2) wcs = imagetools.define_wcs(skypos,skyrange,width=False,height=False,verbose=0,pixsz=0.001) if len2>len1: for i in range(len2): print(i) co_rel = np.concatenate((co_rel, np.roll(coo2, i, axis=0)[0:len1,:]-coo1), axis = 0) if (i+1)%200 == 0: foc = wcs.sip_pix2foc(wcs.wcs_world2pix(co_rel[1:],1),1) H,xedges,yedges=np.histogram2d(foc[:,1]-0.5, foc[:,0]-0.5,\ bins=imsz, range=([ [0,imsz[0]],[0,imsz[1]] ])) count += H co_rel = np.array([[0, 0]]) else: for i in range(len1): print(i) co_rel = np.concatenate((co_rel, coo2-np.roll(coo1, i, axis=0)[0:len2,:]), axis = 0) if (i+1)%200 == 0: foc = wcs.sip_pix2foc(wcs.wcs_world2pix(co_rel[1:],1),1) H,xedges,yedges=np.histogram2d(foc[:,1]-0.5, foc[:,0]-0.5,\ bins=imsz, range=([ [0,imsz[0]],[0,imsz[1]] ])) count += H co_rel = np.array([[0, 0]]) if co_rel.shape[0]>1: foc = wcs.sip_pix2foc(wcs.wcs_world2pix(co_rel[1:],1),1) H,xedges,yedges=np.histogram2d(foc[:,1]-0.5, foc[:,0]-0.5,\ bins=imsz, range=([ [0,imsz[0]],[0,imsz[1]] ])) count += H return count
def plot2D(self,i,j): fig, axes = plt.subplots(1, 1) array = [] labels=[] x = [] y = [] self.LastChain.setSample(0) while self.LastChain.getNextPoint(): x.append( self.Parameters.getVariable(j).getValue() ) y.append( self.Parameters.getVariable(i).getValue() ) nbins = 50 H, xedges, yedges = np.histogram2d(x,y,bins=nbins) while(H.max()>100): nbins = nbins + 1 H, xedges, yedges = np.histogram2d(x,y,bins=nbins) H = np.rot90(H) H = np.flipud(H) Hmasked = np.ma.masked_where(H==0,H) axes.pcolormesh(xedges,yedges,Hmasked) axes.locator_params(nbins = 3) axes.set_xlabel("x") axes.set_ylabel("y") fig.show()
def hist2d(x,y,nBinsX,nBinsY,rngX=None,rngY=None): if rngX == None and rngY == None: h2d, xp, yp = np.histogram2d(y,x,bins=(nBinsY,nBinsX), normed = True) else: h2d, xp, yp = np.histogram2d(y,x,bins=(nBinsY,nBinsX),range=[rngY,rngX], normed = True) extent = [yp[0],yp[-1],xp[0],xp[-1]] return h2d, extent
def contourplotgeneric(vx,vy,wgt=None,ax=None,wgtopt=1,\ nxbins=25,nybins=25,flist=[0.683,0.954], linestyle='-',color='k',linewidths=3): """ Set wgtopt = 1 to weight the points according to the field 'weight' in the chain. Set wgtopt = 0 to weight the points equally. flist is fraction of chain you want to enclose. """ if wgt is None: wgtopt = 0 if wgtopt == 1: H, xedges, yedges = np.histogram2d(vx,vy,[nxbins,nybins],weights=wgt) else: H, xedges, yedges = np.histogram2d(vx,vy,[nxbins,nybins]) Hflip = np.zeros([nybins,nxbins]) # for some idiotic reason, contour progam bins are flipped. xcen = (xedges[1:] + xedges[:-1])*0.5 ycen = (yedges[1:] + yedges[:-1])*0.5 for x in range(nxbins): for y in range(nybins): Hflip[y,x] = H[x,y] clist = getclevels(Hflip,flist) if ax is None: f = plt.figure() ax = f.add_subplot(111) ax.contour(xcen,ycen,Hflip,levels=clist,linestyles=linestyle,colors=color,linewidths=linewidths) return f, ax else: ax.contour(xcen,ycen,Hflip,levels=clist,linestyles=linestyle,colors=color,linewidths=linewidths)
def computeDensityFieldForHaloNumber(self, path_to_DF, path_to_RS, outputFile, gridSize=2048, subgridSize = 256 ) : """ Extracts the distribution of quantity 'name' out of all snapshots of the Multidark simulation. :param path_to_DF: path to the density field file :param path_to_RS: path to the rockstar halo catalog file :param outputFile: where the histogram iswritten :param bins: binning scheme to compute the historgram. :param gridSize: grid size from the density field :param subgridSize: grid size to compute histograms on and write outputs. """ #In each cell, average the number of counts in the RS file : Ncen Nsat dL = 1000./gridSize NperBatch = subgridSize**3 Ntotal = gridSize**3 Nratio = Ntotal / NperBatch hf=open(path_to_RS,'r') DEFINE x, y, z, c_o_s, REWRITE halos of interest ? f=open(path_to_DF,'r') out = n.empty( (subgridSize**3, 3) ) count = 0 countOut = 0 for kk in n.arange(gridSize): for jj in n.arange(gridSize): for ii in n.arange(gridSize): sel =( x > dL*(0.5 + ii) ) & ( x < dL*(0.5 + ii + 1) ) & ( y > dL*(0.5 + jj) ) & ( y < dL*(0.5 + jj + 1) ) & ( x > dL*(0.5 + kz) ) & ( z < dL*(0.5 + kk + 1) ) Nhalos = len(sel.nonzero()[0]) selCen = (sel) & (CONDITION_CEN) NCentrals = len(selCen.nonzero()[0]) deltaValue = n.fromfile(f,dtype="float64",count=1) out[count] = n.array([deltaValue, Nhalos, Ncentrals]) if count == subgridSize**3 : dataAB_tot = n.histogram2d(n.log10(out.T[0]), n.log10(out.T[1]) ,bins = [binsA,binsB]) dataAB_cen = n.histogram2d(n.log10(out.T[0]), n.log10(out.T[2]) ,bins = [binsA,binsB]) f = open(outputFile + "_" +str(countOut)+".pkl" ,'w') cPickle.dump([binsA,binsB,dataAB_tot, dataAB_cen],f) f.close() out = n.empty( (subgridSize**3, 3) ) countOut +=1 count += 1 #GATHER RESULTS fileList = glob.glob(outputFile + "_*.pkl") out_all = n.empty( (Nratio, len(binsA)-1, len(binsB)-1) ) out_cen = n.empty( (Nratio, len(binsA)-1, len(binsB)-1) ) for ii, el in enumerate(fileList): f = open(el ,'r') binsA,binsB,dataAB_tot, dataAB_cen = cPickle.dump([binsA,binsB,dataAB_tot, dataAB_cen],f) f.close() out_all[ii] = dataAB_tot out_cen[ii] = dataAB_cen f = open(outputFile + "_all.pkl" ,'w') cPickle.dump([binsA,binsB,n.sum(out_all, xis=0), n.sum(out_cen, xis=0)],f) f.close()
def convert_3dps_to_2dps(self, k_edges_p, k_edges_v): k_bin_x = 2. * np.pi * np.fft.fftshift(np.fft.fftfreq(self.boxshape[0], self.boxunit)) k_bin_y = 2. * np.pi * np.fft.fftshift(np.fft.fftfreq(self.boxshape[1], self.boxunit)) k_bin_z = 2. * np.pi * np.fft.fftshift(np.fft.fftfreq(self.boxshape[2], self.boxunit)) k_bin_p = np.abs(k_bin_x) k_bin_v = np.sqrt( (k_bin_y**2)[:,None] + (k_bin_z**2)[None,:] ) k_bin_2d = np.zeros(shape=(2,)+ k_bin_p.shape + k_bin_v.shape) k_bin_2d[0] = k_bin_p[:, None, None] k_bin_2d[1] = k_bin_v[None, :, :] ones_weight = np.ones_like(self.ps_3d) kn_2d, xedges, yedges = np.histogram2d(k_bin_2d[0].flatten(), k_bin_2d[1].flatten(), (k_edges_p, k_edges_v), weights=ones_weight.flatten()) ps_2d, xedges, yedges = np.histogram2d(k_bin_2d[0].flatten(), k_bin_2d[1].flatten(), (k_edges_p, k_edges_v), weights=self.ps_3d.flatten()) kn_2d[kn_2d==0] = np.inf ps_2d /= kn_2d kn_2d[kn_2d==np.inf] = 0 self.kn_2d = kn_2d self.ps_2d = ps_2d
def make_lookup(self, size_max, bins=[10, 10]): """ Create lookup tables for the weighting of the telescopes Parameters ---------- size_max : dictionary Dict with the maximum size values for the LUT for each telescope type bins : tuple or list Number of bins in size and width to length ratio Returns ------- lookup: dictionary For each camera ID one tuple containing a numpy array with the histogram and the look up table. """ lookup = {} for cam in self.data.keys(): # make sure that entries are valid self.data[cam] = self.data[cam][np.isfinite( self.data[cam]).all(axis=1)] xbins = np.logspace(1, np.log10(size_max[cam]), bins[0] + 1) ybins = np.linspace(0, 1, bins[1] + 1) hist = np.histogram2d(self.data[cam][:, 1], self.data[cam][:, 2], [xbins, ybins]) # code snippet from numpy.histogramdd to calculate the bin number # each sample falls into. This is used in histogram2d above as well # but as those required numbers are not written out it is # reacalculated right here. sample = np.array([self.data[cam][:, 1], self.data[cam][:, 2]]).T D = sample.shape[1] edges = np.array([xbins, ybins]) dedges = D * [None] Ncount = {} for i in np.arange(D): Ncount[i] = np.digitize(sample[:, i], edges[i]) for i in np.arange(D): dedges[i] = np.diff(edges[i]) # Rounding precision mindiff = dedges[i].min() if not np.isinf(mindiff): decimal = int(-np.log10(mindiff)) + 6 # Find which points are on the rightmost edge. not_smaller_than_edge = (sample[:, i] >= edges[i][-1]) on_edge = (np.around(sample[:, i], decimal) == np.around( edges[i][-1], decimal)) # Shift these points one bin to the left. Ncount[i][np.nonzero(on_edge & not_smaller_than_edge)[0]] -= 1 sum_dca2 = np.zeros([len(xbins), len(ybins)]) for i, dca2 in enumerate(self.data[cam][:, 0]): sum_dca2[Ncount[0][i] - 1, Ncount[1][i] - 1] += dca2 dca2_means = sum_dca2[:-1, :-1] / hist[0] # add to the histogram self.lookup[cam] = np.array(hist + (np.reshape(dca2_means, bins), ))
def run_simulation( gamma_boost, show ): """ Run a simulation with a relativistic electron bunch crosses a laser Parameters ---------- gamma_boost: float The Lorentz factor of the frame in which the simulation is carried out. show: bool Whether to show a plot of the angular distribution """ # Boosted frame boost = BoostConverter(gamma_boost) # The simulation timestep diag_period = 100 N_step = 101 # Number of iterations to perform # Calculate timestep to resolve the interaction with enough points laser_duration_boosted, = boost.copropag_length( [laser_duration], beta_object=-1 ) bunch_sigma_z_boosted, = boost.copropag_length( [bunch_sigma_z], beta_object=1 ) dt = (4*laser_duration_boosted + bunch_sigma_z_boosted/c)/N_step # Initialize the simulation object zmax, zmin = boost.copropag_length( [zmax_lab, zmin_lab], beta_object=1. ) sim = Simulation( Nz, zmax, Nr, rmax, Nm, dt, dens_func=None, zmin=zmin, boundaries={'z':'periodic', 'r':'reflective'}, use_cuda=use_cuda ) # Remove particles that were previously created sim.ptcl = [] print( 'Initialized simulation' ) # Add electron bunch (automatically converted to boosted-frame) add_elec_bunch_gaussian( sim, sig_r=1.e-6, sig_z=bunch_sigma_z, n_emit=0., gamma0=gamma_bunch_mean, sig_gamma=gamma_bunch_rms, Q=Q_bunch, N=N_bunch, tf=0.0, zf=0.5*(zmax+zmin), boost=boost ) elec = sim.ptcl[0] print( 'Initialized electron bunch' ) # Add a photon species photons = sim.add_new_species( q=0, m=0 ) print( 'Initialized photons' ) # Activate Compton scattering for electrons of the bunch elec.activate_compton( target_species=photons, laser_energy=laser_energy, laser_wavelength=laser_wavelength, laser_waist=laser_waist, laser_ctau=laser_ctau, laser_initial_z0=laser_initial_z0, ratio_w_electron_photon=50, boost=boost ) print( 'Activated Compton' ) # Add diagnostics if write_hdf5: sim.diags = [ ParticleDiagnostic( diag_period, species={'electrons': elec, 'photons': photons}, comm=sim.comm ) ] # Get initial total momentum initial_total_elec_px = (elec.w*elec.ux).sum() * m_e * c initial_total_elec_py = (elec.w*elec.uy).sum() * m_e * c initial_total_elec_pz = (elec.w*elec.uz).sum() * m_e * c ### Run the simulation for species in sim.ptcl: species.send_particles_to_gpu() for i_step in range( N_step ): for species in sim.ptcl: species.push_x( 0.5*sim.dt ) elec.handle_elementary_processes( sim.time + 0.5*sim.dt ) for species in sim.ptcl: species.push_x( 0.5*sim.dt ) # Increment time and run diagnostics sim.time += sim.dt sim.iteration += 1 for diag in sim.diags: diag.write( sim.iteration ) # Print fraction of photons produced if i_step%10 == 0: for species in sim.ptcl: species.receive_particles_from_gpu() simulated_frac = photons.w.sum()/elec.w.sum() for species in sim.ptcl: species.send_particles_to_gpu() print( 'Iteration %d: Photon fraction per electron = %f' \ %(i_step, simulated_frac) ) for species in sim.ptcl: species.receive_particles_from_gpu() # Check estimation of photon fraction check_photon_fraction( simulated_frac ) # Check conservation of momentum (is only conserved ) if elec.compton_scatterer.ratio_w_electron_photon == 1: check_momentum_conservation( gamma_boost, photons, elec, initial_total_elec_px, initial_total_elec_py, initial_total_elec_pz ) # Transform the photon momenta back into the lab frame photon_u = 1./photons.inv_gamma photon_lab_pz = boost.gamma0*( photons.uz + boost.beta0*photon_u ) photon_lab_p = boost.gamma0*( photon_u + boost.beta0*photons.uz ) # Plot the scaled angle and frequency if show: import matplotlib.pyplot as plt # Bin the photons on a grid in frequency and angle freq_min = 0.5 freq_max = 1.2 N_freq = 500 gammatheta_min = 0. gammatheta_max = 1. N_gammatheta = 100 hist_range = [[freq_min, freq_max], [gammatheta_min, gammatheta_max]] extent = [freq_min, freq_max, gammatheta_min, gammatheta_max] fundamental_frequency = 4*gamma_bunch_mean**2*c/laser_wavelength photon_scaled_freq = photon_lab_p*c / (h*fundamental_frequency) gamma_theta = gamma_bunch_mean * np.arccos(photon_lab_pz/photon_lab_p) grid, freq_bins, gammatheta_bins = np.histogram2d( photon_scaled_freq, gamma_theta, weights=photons.w, range=hist_range, bins=[ N_freq, N_gammatheta ] ) # Normalize by solid angle, frequency and number of photons dw = (freq_bins[1]-freq_bins[0]) * 2*np.pi * fundamental_frequency dtheta = ( gammatheta_bins[1]-gammatheta_bins[0] )/gamma_bunch_mean domega = 2.*np.pi*np.sin( gammatheta_bins/gamma_bunch_mean )*dtheta grid /= dw * domega[np.newaxis, 1:] * elec.w.sum() grid = np.where( grid==0, np.nan, grid ) plt.imshow( grid.T, origin='lower', extent=extent, cmap='gist_earth', aspect='auto', vmax=1.8e-16 ) plt.title('Particles, $d^2N/d\omega \,d\Omega$') plt.xlabel('Scaled energy ($\omega/4\gamma^2\omega_\ell$)') plt.ylabel(r'$\gamma \theta$' ) plt.colorbar() # Plot theory plt.plot( 1./( 1 + gammatheta_bins**2), gammatheta_bins, color='r' ) plt.show() plt.clf()
def plot_prob(tipo='', chutes_modelo=chutes_modelo, b=None, save=False): '''Plot da probabilidade de marcar de acordo com o ângulo do chute utilizando todos os dados''' #Histograma de duas dimensões: eixo x e y do chute sem considerar os pênaltis H_Chute = np.histogram2d( chutes_modelo[~chutes_modelo['subEventoNome'].isin(['Penalty'])]['X'], chutes_modelo[~chutes_modelo['subEventoNome'].isin(['Penalty'])]['Y'], bins=50, range=[[0, 100], [0, 100]]) gols = chutes_modelo[~chutes_modelo['subEventoNome'].isin(['Penalty']) & chutes_modelo['Gol'] == 1] H_Gol = np.histogram2d(gols['X'], gols['Y'], bins=50, range=[[0, 100], [0, 100]]) #Probabilidade de marcar de acordo com o ângulo e a distância do chute em relação ao gol #Total de chutes por ângulo total_chutes_por_angulo = np.histogram(chutes_modelo['Angulo'] * 180 / np.pi, bins=40, range=[0, 150]) #Total de gols por ângulo total_gols_por_angulo = np.histogram(gols['Angulo'] * 180 / np.pi, bins=40, range=[0, 150]) #Probabilidade de marcar o gol de acordo com o ângulo do chute #-> (gols marcados | angulo) / (chutes feitos | angulo) prob_gol_angulo = np.divide(total_gols_por_angulo[0], total_chutes_por_angulo[0]) #Calculo de angulo medio angulo = total_chutes_por_angulo[1] angulo_medio = (angulo[:-1] + angulo[1:]) / 2 #Total de chutes por distância total_chutes_por_dist = np.histogram(chutes_modelo['distanciaGol'], bins=40, range=[0, 70]) #Total de gols por distância total_gols_por_dist = np.histogram(gols['distanciaGol'], bins=40, range=[0, 70]) #Probabilidade de gol por distância prob_gol_dist = np.divide(total_gols_por_dist[0], total_chutes_por_dist[0]) #Calculo de distancia media distancia = total_chutes_por_dist[1] dist_media = (distancia[:-1] + distancia[1:]) / 2 #Valores de ângulos inventados x = np.arange(150, step=0.1) #Modelo likelihood utilizando amostra dos dados if tipo == 'likelihood': b = [3, -3] #parâmetros y = 1 / (1 + np.exp(b[0] + b[1] * x * np.pi / 180) ) #Calculo de y dado x e os coeficientes xG = 1 / (1 + np.exp(b[0] + b[1] * chutes_modelo['Angulo'])) chutes_modelo = chutes_modelo.assign(xG=xG) chutes_40 = chutes_modelo.sample(n=40, random_state=42) fig, ax = plt.subplots(num=1) ax.plot(chutes_40['Angulo'] * 180 / np.pi, chutes_40['Gol'], linestyle='none', marker='.', markerSize=12, color='black') ax.plot(x, y, linestyle='solid', color='black') ax.plot(x, 1 - y, linestyle='solid', color='black') loglikelihood = 0 for item, chute in chutes_40.iterrows(): ang = chute['Angulo'] * 180 / np.pi if chute['Gol'] == 1: loglikelihood = loglikelihood + np.log(chute['xG']) ax.plot([ang, ang], [chute['Gol'], chute['xG']], color='red') else: loglikelihood = loglikelihood + np.log(1 - chute['xG']) ax.plot([ang, ang], [chute['Gol'], 1 - chute['xG']], color='blue') ax.set_ylabel('Gol marcado') ax.set_xlabel("Ângulo do chute (graus)") ax.set_yticklabels(['Não', 'Sim']) plt.ylim((-0.05, 1.05)) plt.xlim((0, 80)) plt.text(45, 0.2, 'Log-likelihood:') plt.text(45, 0.1, str(loglikelihood)) ax.set_yticks([0, 1]) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) if save: fig.savefig('Imagens/LikelihoodExemplo.pdf', dpi=None, bbox_inches="tight") elif tipo == 'likelihood otimizado angulo': xGprob = 1 / (1 + np.exp(b[0] + b[1] * angulo_medio * np.pi / 180)) fig, ax = plt.subplots(num=1) ax.plot(angulo_medio, prob_gol_angulo, linestyle='none', marker='.', markerSize=12, color='black') ax.plot(angulo_medio, xGprob, linestyle='solid', color='blue') ax.set_ylabel('Probabilidade de marcar') ax.set_xlabel('Ângulo do chute (graus)') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) if save: fig.savefig('Imagens/AjusteProbabilidadeDeGolAngulo.pdf', dpi=None, bbox_inches="tight") elif tipo == 'likelihood otimizado distancia': if len(b) == 2: #predição com distância media somente xGprob = 1 / (1 + np.exp(b[0] + b[1] * dist_media)) else: #predição com distância e distância ao quadrado xGprob = 1 / (1 + np.exp(b[0] + b[1] * dist_media + b[2] * pow(dist_media, 2))) fig, ax = plt.subplots(num=1) ax.plot(dist_media, prob_gol_dist, linestyle='none', marker='.', markerSize=12, color='black') ax.plot(dist_media, xGprob, linestyle='solid', color='black') ax.set_ylabel('Probabilidade de marcar') ax.set_xlabel('Distância do gol (metros)') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) if save: fig.savefig('Imagens/ProbabilidadeDeMarcarDistancia.pdf', dpi=None, bbox_inches="tight") else: fig, ax = plt.subplots(num=2) ax.plot(angulo_medio, prob_gol_angulo, linestyle='none', marker='.', markerSize=12, color='black') ax.set_ylabel('Probabilidade de marcar') ax.set_xlabel("Ângulo do chute (graus)") ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) if save: fig.savefig('Imagens/ProbabilidadeDeGolPorAngulo.pdf', dpi=None, bbox_inches="tight") #Fita uma reta linear aos dados if tipo == 'linear': b = [-0.05, 1 / 125] # parâmetros-> Interseção e Declive y = b[0] + b[1] * x #Calculo de y dado x e os parâmetros ax.plot(x, y, linestyle='solid', color='black') #plota reta linear if save: fig.savefig('Imagens/RegressaoLinearExemplo.pdf', dpi=None, bbox_inches="tight") #Fita uma curva sigmoide aos dados if tipo == 'sigmoide': b = [3, -3] #parâmetros y = 1 / (1 + np.exp(b[0] + b[1] * x * np.pi / 180) ) #Calculo de y (probabilidade) dado x e os coeficientes ax.plot(x, y, linestyle='solid', color='black') #plota curva sigmoide if save: fig.savefig('Imagens/SigmoideExemplo.pdf', dpi=None, bbox_inches="tight")
def angular(d, phi=None, e=None, **kw): ''' Make the angular plot. Call form 1: angular(s, phi, e, kw...) Arguments: s -- the charges. phi -- the angles of ejection. e -- energies of each charge. Call form 2 angular(d, kw...) Arguments: d -- pext data, a structured array from the lspreader. Keyword Arugments: phi -- If a string, read this array of recs as the angles. If an array, these are the angles. e -- If not None, use these as energies over d['KE'] energy_units -- Set the energy units. Options are eV, KeV, MeV, GeV, and auto. auto chooses the unit based on the max energy. energy_scale -- Set the energy scale. Not required, but you can hack it from the default due to energy_unit if you wish. toMeV -- Scale to the MeV scale from energy scale. Not required, but you can hack it from energy_unit if you wish. max_e -- Maximum energy, if 'auto', bin automatically. e_step -- Set the steps of the radius contours. min_q -- Minimum charge. max_q -- Maximum charge. angle_range -- Only bin and plot these angles. Does not affect angle binning, bins are still over (-pi,pi) angle_bins -- Set the number of angle bins. energy_bins -- Set the number of energy bins. colorbar -- If true, plot the colorbar. cmap -- use the colormap cmap. clabel -- Set the colorbar label. labels -- Set the angular labels. If not a list, if 'default', use default. If 'tdefault', use default for theta. (See defaults dict); normalize -- Subdivide charges by the bin weights. fig -- If set, use this figure, Otherwise, make a new figure. ax -- If set, use this axis. Otherwise, make a new axis. ltitle -- Make a plot on the top left. rtitle -- Make a plot on the top right. log_q -- log10 the charges. rgridopts -- pass a dictionary that sets details for the rgrid labels. Options for this dict are: angle -- angle of labels. size -- text side. color -- grid color. invert -- invert the rgrid colors. oap -- Plot this apex angle if not None as the oap collection angle. efficiency -- calculate and display the conversion efficiency in the oap angle. A dict of options passed to totalKE and laserE (I only, not E_0). See help for totalKE and laserE. F -- Multiply charges by a factor. dict_return -- Have the return value be a convenient dictionary instead of god knows what it currently is (a tuple of tuple of stuff). ''' #reckon the call form getkw = mk_getkw(kw, defaults) if type(d) == np.ndarray and len(d.dtype) > 0: structd = True e = np.copy(d['KE']) if not phi: phi = 'phi' phi = np.copy(d[phi]) s = np.abs(d['q']) * 1e6 else: structd = False if phi is None or e is None: raise ValueError("Either phi and s were not passed. See help.") s = d eunits = getkw('energy_units') if eunits == 'auto': pw = np.log10(np.max(e)) if pw < 3: eunits = 'eV' elif pw <= 5: eunits = 'KeV' elif pw <= 9: eunits = 'MeV' else: eunits = 'GeV' if test(kw, 'angle_range'): mnang, mxang = kw['angle_range'] if mxang > np.pi: good = np.logical_and(phi >= mnang, phi <= np.pi) good |= np.logical_and(phi >= -np.pi, phi <= -(mxang - np.pi)) else: good = np.logical_and(phi >= mnang, phi <= mxang) phi = phi[good] e = e[good] s = s[good] if structd: d = d[good] getunitkw = mk_getkw(kw, unit_defaults[eunits]) if test(kw, 'F'): s *= kw['F'] phi_spacing = getkw('angle_bins') E_spacing = getkw('energy_bins') e /= getunitkw('energy_scale') maxE = getunitkw('max_e') Estep = getunitkw('e_step') if maxE == 'max': maxE = np.max(e) elif maxE == 'round' or maxE == 'auto': mxe = np.max(e) tenpow = np.floor(np.log10(mxe)) mantissa = np.floor(mxe / (10**tenpow)) maxE = 10**tenpow * (int(mxe / (10**tenpow)) + 1) Estep = 10**tenpow if mantissa > 6: Estep = 6 * 10**tenpow if test(kw, 'e_step'): Estep = kw['e_step'] maxQ = getkw('max_q') minQ = getkw('min_q') if test(kw, "normalize"): s /= maxE / E_spacing * 2 * np.pi / phi_spacing s *= getunitkw('toMeV') clabel = getkw('clabel') cmap = getkw('cmap') phi_bins = np.linspace(-np.pi, np.pi, phi_spacing + 1) E_bins = np.linspace(0, maxE, E_spacing + 1) PHI, E = np.mgrid[-np.pi:np.pi:phi_spacing * 1j, 0:maxE:E_spacing * 1j] S, _, _ = np.histogram2d(phi, e, bins=(phi_bins, E_bins), weights=s) if test(kw, 'fig'): fig = kw['fig'] else: fig = plt.figure(1, facecolor=(1, 1, 1)) if test(kw, 'ax'): ax = kw['ax'] else: ax = plt.subplot(projection='polar', facecolor='white') norm = matplotlib.colors.LogNorm() if test(kw, 'log_q') else None ax.set_rmax(maxE) surf = plt.pcolormesh(PHI, E, S, norm=norm, cmap=cmap, vmin=minQ, vmax=maxQ) if test(kw, 'colorbar'): c = fig.colorbar(surf, pad=0.1) c.set_label(clabel) #making radial guides. rgrids only works for plt.polar calls #making rgrid if test(kw, 'rgridopts'): ropts = kw['rgridopts'] else: ropts = dict() getrkw = mk_getkw(ropts, rgrid_defaults) if test(ropts, 'unit'): runit = ropts['unit'] else: runit = eunits rangle = getrkw('angle') rsize = getrkw('size') gridc = getrkw('color') if test(ropts, 'invert'): c1, c2 = "w", "black" else: c1, c2 = "black", "w" full_phi = np.linspace(0.0, 2 * np.pi, 100) rlabels = np.arange(0.0, maxE, Estep)[1:] for i in rlabels: plt.plot(full_phi, np.ones(full_phi.shape) * i, c=gridc, alpha=0.9, lw=1, ls='--') ax.set_theta_zero_location('N') ax.patch.set_alpha(0.0) ax.set_facecolor('red') rlabel_str = '{} ' + runit #text outlines. _, ts = plt.rgrids(rlabels, labels=map(rlabel_str.format, rlabels), angle=rangle) for t in ts: t.set_path_effects( [pe.Stroke(linewidth=1.5, foreground=c2), pe.Normal()]) t.set_size(rsize) t.set_color(c1) if test(kw, 'oap'): oap = kw['oap'] / 2 * np.pi / 180 maxt = oap + np.pi mint = np.pi - oap maxr = maxE * .99 if test(kw, 'efficiency') and structd: defeff = dict(I=3e18, w=None, T=None, l=None, ecut=0, anglecut=None) effd = sd(defeff, **kw['efficiency']) if effd['ecut'] == 'wilks': effd['ecut'] = ( np.sqrt(1 + a0(effd['I'], l=effd['l'] * 1e2)**2 / 2.0) - 1.0) * effd['massE'] dim = effd['dim'] LE = laserE(I=effd['I'], w=effd['w'], T=effd['T'], dim=dim) KE, good = totalKE(d, ecut=effd['ecut'], anglecut=(oap / np.pi * 180, dim), return_bools=True) minr = effd['ecut'] / getunitkw('energy_scale') totalq = np.abs(d['q'][good]).sum() * 1e6 def texs(f, l=2): tenpow = int(np.floor(np.log10(f))) nfmt = "{{:0.{}f}}".format(l) + "\\cdot 10^{{{}}}" return nfmt.format(f / 10**tenpow, tenpow) fig.text(0.01, 0.04, "Efficiency:\n$\\frac{{{}J}}{{{}J}}$=${}$".format( texs(KE, l=1), texs(LE, l=1), texs(KE / LE)), fontdict=dict(fontsize=20)) fig.text(0.65, 0.05, "$Q_{{tot}}={} ${}".format( texs(totalq), "pC" if dim == "3D" else "pC/cm"), fontdict=dict(fontsize=20)) fig.text(0.6, 0.92, "I = ${}$ W/cm$^2$".format(texs(effd['I'], l=1)), fontdict=dict(fontsize=20)) else: minr = 0.12 / getunitkw('toMeV') ths = np.linspace(mint, maxt, 20) rs = np.linspace(minr, maxr, 20) mkline = lambda a, b: plt.plot( a, b, c=(0.2, 0.2, 0.2), ls='-', alpha=0.5) mkline(ths, np.ones(ths.shape) * minr) mkline(mint * np.ones(ths.shape), rs) mkline(maxt * np.ones(ths.shape), rs) if test(kw, 'labels'): if kw['labels'] == 'default': labels = defaults['labels'] elif kw['labels'] == 'tdefault': labels = defaults['tlabels'] else: labels = kw['labels'] ax.set_xticks(np.pi / 180 * np.linspace(0, 360, len(labels), endpoint=False)) ax.set_xticklabels(labels) if test(kw, 'ltitle'): if len(kw['ltitle']) <= 4: ax.set_title(kw['ltitle'], loc='left', fontdict={'fontsize': 24}) else: ax.text(np.pi / 4 + 0.145, maxE + Estep * 2.4, kw['ltitle'], fontdict={'fontsize': 24}) if test(kw, 'rtitle'): if '\n' in kw['rtitle']: fig.text(0.60, 0.875, kw['rtitle'], fontdict={'fontsize': 22}) else: plt.title(kw['rtitle'], loc='right', fontdict={'fontsize': 22}) if test(kw, 'dict_return'): return dict( surf=surf, ax=ax, fig=fig, phi_bins=phi_bins, E_bins=E_bins, phi=phi, e=e, s=s, eunits=eunits, ) else: return (surf, ax, fig, (phi_bins, E_bins), (phi, e, s))
def hist_and_plot(data, title, yvar='DWRxk', xvar='DWRkw', xlabel='DWR Ka W [dB]', ylabel='DWR X Ka [dB]', xlim=[-5, 20], ylim=[-5, 20], lognorm=True, vminmax=None, savename='auto3f', inverty=False, figax=None, stats=None, bins=100, density=False, CFAD=False, cmap='viridis'): dataclean = data[[xvar, yvar]].dropna() hst, xedge, yedge = np.histogram2d(dataclean[xvar], dataclean[yvar], bins=bins) hst = hst.T hst[hst==0] = np.nan if density: #xBinW = xedge[1:]-xedge[:-1] #yBinW = yedge[1:]-yedge[:-1] #hst = hst/xBinW/yBinW[:,np.newaxis] hst = 100.*hst/np.nansum(hst) if CFAD: hst = 100.*hst/np.nansum(hst,axis=1)[:,np.newaxis] xcenter = (xedge[:-1] + xedge[1:])*0.5 ycenter = (yedge[:-1] + yedge[1:])*0.5 if figax is None: fig, ax = plt.subplots(1,1) else: fig, ax = figax if lognorm: norm = colors.LogNorm(vmin=np.nanmin(hst[np.nonzero(hst)]), vmax=np.nanmax(hst)) if vminmax is not None: norm = colors.LogNorm(vmin=vminmax[0], vmax=vminmax[1]) hst[hst<0.1*vminmax[0]] = np.nan mesh = ax.pcolormesh(xcenter, ycenter, hst[:], cmap=cmap, norm=norm) else: if vminmax is None: mesh = ax.pcolormesh(xcenter, ycenter, hst[:], cmap='jet') else: mesh = ax.pcolormesh(xcenter, ycenter, hst[:], cmap='jet', vmin=vminmax[0], vmax=vminmax[1]) ax.set_xlim(xlim) ax.set_ylim(ylim) clabel='counts' if CFAD or density: clabel='relative frequency [%]' if stats is not None: bins = pd.cut(data[yvar], yedge) groups = data.groupby(bins)[xvar] lines = [] labels = [] if 'median' in stats: l = ax.plot(groups.median(), ycenter, label='median', lw=2, ls='-.', c='k') lines.append(l[0]) labels.append('median') if 'mean' in stats: l = ax.plot(groups.mean(), ycenter, label='mean', lw=2, ls='--', c='r' ) lines.append(l[0]) labels.append('mean') if 'quartile' in stats: l = ax.plot(groups.quantile(0.25), ycenter, label='.25 quantile', c='k') ax.plot(groups.quantile(0.75), ycenter, label='.75 quantile', ls='-', c='k') lines.append(l[0]) labels.append('quartiles') if 'decile' in stats: l = ax.plot(groups.quantile(0.10), ycenter, label='.10 decile', ls=':', c='k') ax.plot(groups.quantile(0.90), ycenter, label='.90 decile', ls=':', c='k') lines.append(l[0]) labels.append('deciles') ax.legend(lines, labels, framealpha=0.95) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.1) cb = plt.colorbar(mesh, cax=cax, extend='max', label=clabel) #cb = plt.colorbar(mesh, ax=ax, extend='max', label=clabel) ax.set_title(title) if xlabel is not None: ax.set_xlabel(xlabel) if ylabel is not None: ax.set_ylabel(ylabel) ax.grid() if inverty: ax.invert_yaxis() if savename == 'auto3f': fig.savefig('triple_frequency/'+'_'.join(title.split())+addlabel+'.png') elif savename is None: pass else: fig.savefig(savename) return hst, xcenter, ycenter, cb, xedge, yedge
def main(): cm = plt.get_cmap('Blues') try: sns.set_style("white") except: pass #cm.set_gamma(0.2) # name = sys.argv[1] outname = name.split('.')[0] # data = np.loadtxt(name) res = makepdfs(data[:,1], data[:,5]) plt.close('all') plt.hist(res[0], weights=res[1], bins=31, histtype='step', lw=4, color='k') plt.xlim(0.,3.) #plt.plot(res[0], res[1], c='k', lw=2) plt.xlabel('IMF Slope', fontsize=20) plt.ylabel('Probability', fontsize=20) plt.savefig(outname+'_marginal_gamma.png', dpi=300, bbox_inches='tight') res2 = make2dpdfs(data[:,1], data[:,3], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('Log(Age)') plt.xlabel('IMF Slope') cb.set_label('Probability') plt.savefig(outname+'_joint_gamma_age.png', dpi=300, bbox_inches='tight') plt.figure() plt.hist(res2[0], weights = res2[2]) res2 = make2dpdfs(data[:,1], data[:,0], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('Av') plt.xlabel('IMF Slope') cb.set_label('Probability') plt.savefig(outname+'_joint_gamma_av.png', dpi=300, bbox_inches='tight') plt.figure() plt.hist(res2[0], weights = res2[2]) res2 = make2dpdfs(data[:,0], data[:,3], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('Log(Age)') plt.xlabel('Av') cb.set_label('Probability') plt.savefig(outname+'_joint_av_age.png', dpi=300, bbox_inches='tight') res2 = make2dpdfs(data[:,1], data[:,8], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('BF') plt.xlabel('IMF Slope') cb.set_label('Probability') plt.savefig(outname+'_joint_bf_gamma.png', dpi=300, bbox_inches='tight') res2 = make2dpdfs(data[:,0], data[:,9], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('dAv') plt.xlabel('Av') cb.set_label('Probability') plt.savefig(outname+'_joint_av_dav.png', dpi=300, bbox_inches='tight') res2 = make2dpdfs(data[:,8], data[:,9], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('dAv') plt.xlabel('BF') cb.set_label('Probability') plt.savefig(outname+'_joint_bf_dav.png', dpi=300, bbox_inches='tight') #print(res[1]) plt.show()
print('Coriolis term done.') #diffusive term diff_u = param['nu_B'] * LLu.dot(u) diff_v = param['nu_B'] * LLv.dot(v) diff_term = np.sqrt(IuT.dot(diff_u**2) + IvT.dot(diff_v**2)) del diff_u, diff_v print('Diffusion term done.') # actual numbers Re = np.log10(adv_term / diff_term).flatten() Ro = np.log10(adv_term / cor_term).flatten() del adv_term, cor_term, diff_term print('Non dim numbers computed.') # histogram ReRoH, Re_edges, Ro_edges = np.histogram2d(Re, Ro, 200) print('ReRo histogram done.') del Re, Ro Re_mid = Re_edges[:-1] + np.diff(Re_edges) / 2. Ro_mid = Ro_edges[:-1] + np.diff(Ro_edges) / 2. ## STORING dic = dict() all_var2export = ['ReRoH', 'Re_mid', 'Ro_mid', 'Re_edges', 'Ro_edges'] for v in all_var2export: exec('dic[v] =' + v) np.save(runpath + '/analysis/ReRo_hist.npy', dic)
def show_it( fig, filename, frame_start=None, frame_stop=None, ): results = result_utils.get_results(filename, mode='r') reconstructor = flydra_core.reconstruct.Reconstructor(results) camn2cam_id, cam_id2camns = result_utils.get_caminfo_dicts(results) data2d = results.root.data2d_distorted # make sure we have 2d data table print 'reading frames...' frames = data2d.read(field='frame') print 'OK' if frame_start is not None: print 'selecting frames after start' after_start = numpy.nonzero(frames >= frame_start)[0] else: after_start = None if frame_stop is not None: print 'selecting frames before stop' before_stop = numpy.nonzero(frames <= frame_stop)[0] else: before_stop = None print 'finding all frames' if after_start is not None and before_stop is not None: use_idxs = numpy.intersect1d(after_start, before_stop) elif after_start is not None: use_idxs = after_start elif before_stop is not None: use_idxs = before_stop else: use_idxs = numpy.arange(data2d.nrows) # OK, we have data coords, plot print 'reading cameras' frames = frames[use_idxs] camns = data2d.read(field='camn') camns = camns[use_idxs] unique_camns = numpy.unique1d(camns) unique_cam_ids = list( sets.Set([camn2cam_id[camn] for camn in unique_camns])) unique_cam_ids.sort() print '%d cameras with data' % (len(unique_cam_ids), ) if len(unique_cam_ids) == 1: n_rows = 1 n_cols = 1 else: n_rows = 2 n_cols = 3 subplot_by_cam_id = {} for i, cam_id in enumerate(unique_cam_ids): ax = auto_subplot(fig, i, n_rows=n_rows, n_cols=n_cols) ax.text( 0.5, 0.95, '%s: %s' % (cam_id, str(cam_id2camns[cam_id])), horizontalalignment='center', verticalalignment='top', transform=ax.transAxes, ) ## ax.set_xticks([]) ## ax.set_yticks([]) subplot_by_cam_id[cam_id] = ax cdict = { 'red': ((0, 0, 0), (1, 1, 1)), 'green': ((0, 0, 0), (0, 0, 0)), 'blue': ((0, 0, 0), (1, 1, 1)), } # black2magenta = matplotlib.colors.LinearSegmentedColormap('black2magenta',cdict,256) for camn in unique_camns: cam_id = camn2cam_id[camn] ax = subplot_by_cam_id[cam_id] this_camn_idxs = use_idxs[camns == camn] w, h = reconstructor.get_resolution(cam_id) xbins = numpy.linspace(0, w, 40) ybins = numpy.linspace(0, h, 30) xs = data2d.read_coordinates(this_camn_idxs, field='x') ys = data2d.read_coordinates(this_camn_idxs, field='y') hist, xedges, yedges = numpy.histogram2d(xs, ys, bins=(xbins, ybins)) hist = numpy.ma.masked_where(hist == 0, hist) im = getattr(results.root.images, cam_id) ax.imshow(im, origin='lower', extent=[0, w - 1, 0, h - 1], cmap=pylab.cm.gray) pcolor_im = ax.pcolor( xbins, ybins, hist.T, alpha=0.5, ) # cmap = black2magenta ) #fig.colorbar( pcolor_im, cax ) res = reconstructor.get_resolution(cam_id) ax.set_xlim([0, res[0]]) #ax.set_ylim([0,res[1]]) ax.set_ylim([res[1], 0]) if 1: return # Old code below: # Do same as above for Kalman-filtered data if kalman_filename is None: return kresults = PT.open_file(kalman_filename, mode='r') kobs = kresults.root.ML_estimates kframes = kobs.read(field='frame') if frame_start is not None: k_after_start = numpy.nonzero(kframes >= frame_start)[0] #k_after_start = kobs.read_coordinates(idxs) #k_after_start = kobs.get_where_list( # 'frame>=frame_start') else: k_after_start = None if frame_stop is not None: k_before_stop = numpy.nonzero(kframes <= frame_stop)[0] #k_before_stop = kobs.read_coordinates(idxs) #k_before_stop = kobs.get_where_list( # 'frame<=frame_stop') else: k_before_stop = None if k_after_start is not None and k_before_stop is not None: k_use_idxs = numpy.intersect1d(k_after_start, k_before_stop) elif k_after_start is not None: k_use_idxs = k_after_start elif k_before_stop is not None: k_use_idxs = k_before_stop else: k_use_idxs = numpy.arange(kobs.nrows) obj_ids = kobs.read(field='obj_id')[k_use_idxs] #obj_ids = kobs.read_coordinates( k_use_idxs, # field='obj_id') obs_2d_idxs = kobs.read(field='obs_2d_idx')[k_use_idxs] #obs_2d_idxs = kobs.read_coordinates( k_use_idxs, # field='obs_2d_idx') kframes = kframes[k_use_idxs] #kobs.read_coordinates( k_use_idxs, # field='frame') kobs_2d = kresults.root.ML_estimates_2d_idxs xys_by_obj_id = {} for obj_id, kframe, obs_2d_idx in zip(obj_ids, kframes, obs_2d_idxs): obs_2d_idx_find = int( obs_2d_idx) # XXX grr, why can't pytables do this? obj_id_save = int(obj_id) # convert from possible numpy scalar xys_by_cam_id = xys_by_obj_id.setdefault(obj_id_save, {}) kobs_2d_data = kobs_2d.read(start=obs_2d_idx_find, stop=obs_2d_idx_find + 1) assert len(kobs_2d_data) == 1 kobs_2d_data = kobs_2d_data[0] this_camns = kobs_2d_data[0::2] this_camn_idxs = kobs_2d_data[1::2] this_use_idxs = use_idxs[frames == kframe] if debugADS: print print kframe, '===============' print 'this_use_idxs', this_use_idxs d2d = data2d.read_coordinates(this_use_idxs) if debugADS: print 'd2d ---------------' for row in d2d: print row for this_camn, this_camn_idx in zip(this_camns, this_camn_idxs): this_idxs_tmp = numpy.nonzero(d2d['camn'] == this_camn)[0] this_camn_d2d = d2d[d2d['camn'] == this_camn] found = False for this_row in this_camn_d2d: # XXX could be sped up if this_row['frame_pt_idx'] == this_camn_idx: found = True break if not found: if 0: print 'WARNING:point not found in data!?' continue else: raise RuntimeError('point not found in data!?') #this_row = this_camn_d2d[this_camn_idx] this_cam_id = camn2cam_id[this_camn] xys = xys_by_cam_id.setdefault(this_cam_id, ([], [])) xys[0].append(this_row['x']) xys[1].append(this_row['y']) for obj_id in xys_by_obj_id: xys_by_cam_id = xys_by_obj_id[obj_id] for cam_id, (xs, ys) in xys_by_cam_id.iteritems(): ax = subplot_by_cam_id[cam_id] if 0: ax.plot(xs, ys, label='obs: %d' % obj_id) else: ax.plot(xs, ys, 'x-', label='obs: %d' % obj_id) ax.text(xs[0], ys[0], '%d:' % (obj_id, )) ax.text(xs[-1], ys[-1], ':%d' % (obj_id, )) for cam_id in subplot_by_cam_id.keys(): ax = subplot_by_cam_id[cam_id] ax.legend() print 'note: could/should also plot re-projection of Kalman filtered/smoothed data'
im1 = im[yi:yi + W, xi:xi + H, 2] # extract subimage figure(1) plt.imshow(im1, cmap="gray") im2 = im[yi:yi + W, xi:xi + H, 0] # extract subimage figure(2) plt.imshow(im2, cmap="gray") im1_flat = im1.flatten() im2_flat = im2.flatten() hst1 = np.histogram(im1_flat, bins=np.linspace(0, 256, bins + 1))[0] hst2 = np.histogram(im2_flat, bins=np.linspace(0, 256, bins + 1))[0] hst12 = np.histogram2d( im1_flat, im2_flat, bins=[np.linspace(0, 256, bins + 1), np.linspace(0, 256, bins + 1)])[0] hst1 = hst1 / (W * H) hst2 = hst2 / (W * H) hst12 = hst12 / (W * H) mi = 0 for i in range(256): for j in range(256): if hst1[i] > 0 and hst2[j] > 0 and hst12[i][j] > 0: mi += hst12[i][j] * math.log(hst12[i][j] / (hst1[i] * hst2[j]), 2) vi = 0 for i in range(256): for j in range(256):
# Do Correction for Position Data from CameraTransform import CameraTransform CT = CameraTransform(14, [17, 9], [4608, 2592], observer_height=31., angel_to_horizon=(np.pi / 2. - 0.24) * 180 / np.pi) orth_x, orth_y, orth_z = CT.transCamToWorld(Positions_Img.T[:2], Z=0.525) # Calculate Histogramms cx = cy = 2 times = np.asarray(sorted([i.timestamp for i in db.getImages()])) scale = 1. / (cx * cy) / ((times[-1] - times[0]).seconds / 3600.) hist, binx, biny = np.histogram2d(orth_x, orth_y, bins=[ int(max(orth_x) - min(orth_x)) / cx, int(max(orth_y) - min(orth_y)) / cy ], range=[[min(orth_x), max(orth_x)], [min(orth_y), max(orth_y)]]) hist *= scale hist[hist == 0.] = np.nan cx = cy = 6 hist_img, binx_img, biny_img = np.histogram2d(Positions_Img.T[0], Positions_Img.T[1], bins=[4608 / cx, 2592 / cy], range=[[0, 4608], [0, 2592]]) hist_img *= scale * cx * cy hist_img[hist_img == 0.] = np.nan
def iou_metric(y_true_in, y_pred_in, print_table=False): labels = y_true_in y_pred = y_pred_in true_objects = 2 pred_objects = 2 # if all zeros, original code generate wrong bins [-0.5 0 0.5], temp1 = np.histogram2d(labels.flatten(), y_pred.flatten(), bins=([0,0.5,1], [0,0.5, 1])) # temp1 = np.histogram2d(labels.flatten(), y_pred.flatten(), bins=(true_objects, pred_objects)) #print(temp1) intersection = temp1[0] #print("temp2 = ",temp1[1]) #print(intersection.shape) # print(intersection) # Compute areas (needed for finding the union between all objects) #print(np.histogram(labels, bins = true_objects)) area_true = np.histogram(labels,bins=[0,0.5,1])[0] #print("area_true = ",area_true) area_pred = np.histogram(y_pred, bins=[0,0.5,1])[0] area_true = np.expand_dims(area_true, -1) area_pred = np.expand_dims(area_pred, 0) # Compute union union = area_true + area_pred - intersection # Exclude background from the analysis intersection = intersection[1:,1:] intersection[intersection == 0] = 1e-9 union = union[1:,1:] union[union == 0] = 1e-9 # Compute the intersection over union iou = intersection / union # Precision helper function def precision_at(threshold, iou): matches = iou > threshold true_positives = np.sum(matches, axis=1) == 1 # Correct objects false_positives = np.sum(matches, axis=0) == 0 # Missed objects false_negatives = np.sum(matches, axis=1) == 0 # Extra objects tp, fp, fn = np.sum(true_positives), np.sum(false_positives), np.sum(false_negatives) return tp, fp, fn # Loop over IoU thresholds prec = [] if print_table: print("Thresh\tTP\tFP\tFN\tPrec.") for t in np.arange(0.5, 1.0, 0.05): tp, fp, fn = precision_at(t, iou) if (tp + fp + fn) > 0: p = tp / (tp + fp + fn) else: p = 0 if print_table: print("{:1.3f}\t{}\t{}\t{}\t{:1.3f}".format(t, tp, fp, fn, p)) prec.append(p) if print_table: print("AP\t-\t-\t-\t{:1.3f}".format(np.mean(prec))) return np.mean(prec)
if RVS == True: if ivel == 0: vvals = vrots_obs elif ivel == 1: vvals = vrads_obs else: vvals = vzs_obs else: if ivel == 0: vvals = vlons_obs else: vvals = vlats_obs # 2D histogram H, xedges, yedges = np.histogram2d(dists_obs, vvals, \ bins=(ngridx, ngridy), \ range=(drange, vrange[ivel])) # set x-axis (Rgal) is axis=1 H = H.T # normalised by column # print ' hist = ',H # print ' np column = ',np.sum(H, axis=0) H[:, np.sum(H, axis=0) < nsmin] = 0.0 H[:, np.sum(H, axis=0)>=nsmin] = H[:, np.sum(H, axis=0)>=nsmin] \ / np.sum(H[:, np.sum(H, axis=0)>=nsmin], axis=0) # print ' normalised hist = ',H # plt.imshow(H, interpolation='gaussian', origin='lower', aspect='auto', \ # extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]]) # plt.colorbar(im) # plt.show()
def visualise_orientational_distribution(self, axes_to_return=None, cbar=True): """ Creates a plot of the orientational distribution of the unit cells. :param axes_to_return: if None, print to screen, otherwise, requires 3 axes objects, and will return them. :param cbar: boolean to specify if a color bar should be used. """ import matplotlib.pyplot as plt import matplotlib.patheffects as patheffects from mpl_toolkits.basemap import Basemap import scipy.ndimage as ndi def cart2sph(x, y, z): # cctbx (+z to source, y to ceiling) to # lab frame (+x to source, z to ceiling) z, x, y = x, y, z dxy = np.sqrt(x ** 2 + y ** 2) r = np.sqrt(dxy ** 2 + z ** 2) theta = np.arctan2(y, x) phi = np.arctan2(z, dxy) # angle of the z axis relative to xy plane theta, phi = np.rad2deg([theta, phi]) return theta % 360, phi, r def xy_lat_lon_from_orientation(orientation_array, axis_id): logger.debug("axis_id: {}".format(axis_id)) dist = math.sqrt(orientation_array[axis_id][0] ** 2 + orientation_array[axis_id][1] ** 2 + orientation_array[axis_id][2] ** 2) flon, flat, bla = cart2sph(orientation_array[axis_id][0] / dist, orientation_array[axis_id][1] / dist, orientation_array[axis_id][2] / dist) x, y = euler_map(flon, flat) return x, y, flon, flat orientations = [flex.vec3_double(flex.double( image.orientation.direct_matrix())) for image in self.members] space_groups = [image.orientation.unit_cell().lattice_symmetry_group() for image in self.members] # Now do all the plotting if axes_to_return is None: plt.figure(figsize=(10, 14)) axes_to_return = [plt.subplot2grid((3, 1), (0, 0)), plt.subplot2grid((3, 1), (1, 0)), plt.subplot2grid((3, 1), (2, 0))] show_image = True else: assert len(axes_to_return) == 3, "If using axes option, must hand" \ " 3 axes to function." show_image = False axis_ids = [0, 1, 2] labels = ["a", "b", "c"] for ax, axis_id, label in zip(axes_to_return, axis_ids, labels): # Lists of x,y,lat,long for the master orientation, and for all # symmetry mates. x_coords = [] y_coords = [] lon = [] lat = [] sym_x_coords = [] sym_y_coords = [] sym_lon = [] sym_lat = [] euler_map = Basemap(projection='eck4', lon_0=0, ax=ax) for orientation, point_group_type in zip(orientations, space_groups): # Get position of main spots. main_x, main_y, main_lon, main_lat \ = xy_lat_lon_from_orientation(list(orientation), axis_id) x_coords.append(main_x) y_coords.append(main_y) lon.append(main_lon) lat.append(main_lat) # Get position of symetry mates symmetry_operations = list(point_group_type.smx())[1:] for mx in symmetry_operations: rotated_orientation = list(mx.r().as_double() * orientation) # <-- # should make sense if orientation was a vector, not clear what is # going on since orientation is a matrix. Or, make some test cases # with 'orientation' and see if the behave as desired. sym_x, sym_y, sym_lo, sym_la \ = xy_lat_lon_from_orientation(rotated_orientation, axis_id) #assert (sym_x, sym_y) != (main_x, main_y) sym_x_coords.append(sym_x) sym_y_coords.append(sym_y) sym_lon.append(sym_lo) sym_lat.append(sym_la) # Plot each image as a yellow sphere logger.debug(len(x_coords)) euler_map.plot(x_coords, y_coords, 'oy', markersize=4, markeredgewidth=0.5) # Plot the symetry mates as black crosses #euler_map.plot(sym_x_coords, sym_y_coords, 'kx') # Use a histogram to bin the data in lattitude/longitude space, smooth it, # then plot this as a contourmap. This is for all the symetry-related # copies #density_hist = np.histogram2d(lat + sym_lat, lon + sym_lon, # bins=[range(-90, 91), range(0, 361)]) # No symmetry mates until we can verify what the cctbx libs are doing density_hist = np.histogram2d(lat, lon, bins=[list(range(-90, 91)), list(range(0, 361))]) smoothed = ndi.gaussian_filter(density_hist[0], (15, 15), mode='wrap') local_intensity = [] x_for_plot = [] y_for_plot = [] for _lat in range(0, 180): for _lon in range(0, 360): _x, _y = euler_map(density_hist[2][_lon], density_hist[1][_lat]) x_for_plot.append(_x) y_for_plot.append(_y) local_intensity.append(smoothed[_lat, _lon]) cs = euler_map.contourf(np.array(x_for_plot), np.array(y_for_plot), np.array(local_intensity), tri=True) # Pretty up graph if cbar: _cbar = plt.colorbar(cs, ax=ax) _cbar.ax.set_ylabel('spot density [AU]') middle = euler_map(0, 0) path_effect = [patheffects.withStroke(linewidth=3, foreground="w")] euler_map.plot(middle[0], middle[1], 'o', markersize=10, mfc='none') euler_map.plot(middle[0], middle[1], 'x', markersize=8) ax.annotate("beam", xy=(0.52, 0.52), xycoords='axes fraction', size='medium', path_effects=path_effect) euler_map.drawmeridians(np.arange(0, 360, 60), labels=[0, 0, 1, 0], fontsize=10) euler_map.drawparallels(np.arange(-90, 90, 30), labels=[1, 0, 0, 0], fontsize=10) ax.annotate(label, xy=(-0.05, 0.9), xycoords='axes fraction', size='x-large', weight='demi') if show_image: plt.show() return axes_to_return
def main(): #Part to load coordinate file topfile = sys.argv[1] trjfile = sys.argv[2] outfile = open(sys.argv[3], 'w') aname1 = input("atom names for donors? ex) Nm1 Nm2 \n") aname2 = input("atom names for hydrogens? ex) Hm1 Hm2 Hm3 Hm4 \n") aname3 = input("atom names for acceptors? ex) CL \n") rrange = input( "minimum, maximum H-A distance in nanometers? ex) 0.10 0.50 \n") rsplit = rrange.split() rmin, rmax = float(rsplit[0]), float(rsplit[1]) rbin = float(input("r bin size? ex) 0.002 \n")) abin = float(input("cosine(theta) bin size? ex) 0.02 \n")) rnbin, anbin = int((rmax - rmin) / rbin), int(2 / abin) #mi12=input("What is the residue number index interval of MSD calculation? ex) 0 299 \n") #mi12=mi12.split() #mi1,mi2=int(mi12[0]),int(mi12[1]) tskip = int( input("Once in how many frames do you want to take? ex) 10 \n")) teq = int( input( "How many initial frames do you want to cut as equilibration? ex) 5000 \n" )) start_time = timeit.default_timer() #input 1 : load surf traj. (big file) traj = md.load(trjfile, top=topfile) traj = traj[teq::tskip] topology = traj.topology nstep = traj.n_frames nmon = topology.n_residues if nstep >= 100: #if there're many frames.. nfrag = 200 else: nfrag = 1 elapsed = timeit.default_timer() - start_time print('finished trajectory loading {}'.format(elapsed)) print(nstep, nmon) #prepare 2dbins for hydrogen-acceptor distance and H-bond angle #sdbin,count=numpy.zeros(nstep),numpy.zeros(nstep) #bin & stat weight of dr^2(t) ave #make atom indices list (before filtering too far pairs) #should avoid intramolecular atomic pair asplit1, asplit2, asplit3 = aname1.split(), aname2.split(), aname3.split() text1, text2, text3 = '', '', '' for word in asplit1: text1 += 'name ' + word + ' or ' for word in asplit2: text2 += 'name ' + word + ' or ' for word in asplit3: text3 += 'name ' + word + ' or ' text1, text2, text3 = text1[:-4], text2[:-4], text3[:-4] seld = topology.select(text1) selh = topology.select(text2) sela = topology.select(text3) n_atomd, n_atomh, n_atoma = len(seld), len(selh), len(sela) print(n_atomd, n_atomh, n_atoma) dhpairs = [] for j in topology.bonds: if j[0].index in selh and j[1].index in seld: dhpairs.append([j[1].index, j[0].index]) elif j[0].index in seld and j[1].index in selh: dhpairs.append([j[0].index, j[1].index]) fulllist_angles = [] for row in dhpairs: for i in sela: if topology.atom(row[1]).residue != topology.atom(i).residue: extrow = row.copy() extrow.append(i) fulllist_angles.append(extrow) #list_dist=numpy.array(list_dist) fulllist_angles = numpy.array(fulllist_angles) n_angles_full = len(fulllist_angles) print(" full list # angles = {} ".format(n_angles_full)) for ifrag in range( nfrag): #loop of fragmental calculation, to save memory. blength = int(nstep / nfrag) bstart, bend = ifrag * blength, (ifrag + 1) * blength fragtraj = traj[bstart:bend] #calculate distances between hydrogen and acceptors, angle dist = (md.compute_distances(fragtraj, fulllist_angles[:, [1, 2]])).flatten() #index_thres=numpy.where(full_dist<=rmax)[0] #dist=numpy.array(full_dist[index_thres]) #print(dist) #list_angles=fulllist_angles[index_thres,:] ## calculate angles and distances angl = (md.compute_angles(fragtraj, fulllist_angles)).flatten() #n_angles=len(list_angles) #print(" list # distances(within threshold) = {}".format(len(dist))) #print(" list # angles = {}".format(n_angles)) # recalculate NaN values of angles import copy for nan_item in numpy.argwhere(numpy.isnan(angl)).reshape(-1): i_frame = int(nan_item / n_angles_full) i_angle = nan_item % n_angles_full #print(" Nan at {} th frame and {} th angle".format(i_frame,i_angle)) #print(" <-- {} th atoms".format(list_angles[i_angle])) i_abc = fulllist_angles[i_angle] a = traj.xyz[i_frame][i_abc[0]] b = traj.xyz[i_frame][i_abc[1]] c = traj.xyz[i_frame][i_abc[2]] print(" <-- position: \n {} \n {} \n {}".format(a, b, c)) ba = a - b bc = c - b cosine_angle = numpy.dot( ba, bc) / (numpy.linalg.norm(ba) * numpy.linalg.norm(bc)) print(" distance= {}".format(numpy.linalg.norm(bc))) angle = numpy.arccos(cosine_angle) print(" get correct value from NaN, {} (rad) {} (deg)".format( angle, angle * 180.0 / numpy.pi)) angl[nan_item] = copy.copy(angle) cosangl = numpy.cos(angl) #print(cosangl) #printing section - should regard gnuplot pm3d-compatible format. #hold x. increment y. when a full cycle of y range ends, make an empty line. #histogram counts_2d, edge_r, edge_cosa = numpy.histogram2d(dist, cosangl, bins=[rnbin, anbin], range=[[rmin, rmax], [-1.0, 1.0]]) #volume in each radial shell vol = numpy.power(edge_r[1:], 3) - numpy.power(edge_r[:-1], 3) vol *= 4 / 3.0 * numpy.pi # Average number density box_vol = numpy.average(fragtraj.unitcell_volumes) density = n_angles_full / box_vol rdf_2d = (counts_2d * anbin / nstep) / (density * vol[:, None]) if ifrag == 0: totrdf_2d = numpy.copy(rdf_2d) else: totrdf_2d += rdf_2d elapsed = timeit.default_timer() - start_time print('finished fragment {} time {}'.format(ifrag, elapsed)) for i in range(rnbin): for j in range(anbin): xval, yval = rmin + rbin * i, -1.0 + abin * j outfile.write('{:11.4f} {:11.4f} {:11.4f}\n'.format( xval, yval, totrdf_2d[i][j])) outfile.write('\n') elapsed = timeit.default_timer() - start_time print('finished job {}'.format(elapsed)) #numpy.savetxt(outfile,numpy.transpose(rdf_2d), \ #header='x = distance [{},{}], y= cos angle [{},{}]' \ #.format(rmin,rmax,-1.0,1.0),fmt='%f',comments='# ') #numpy.save(outfile,numpy.transpose(rdf_2d)) #print(rdf_2d) outfile.close()
indd = n_templates >= depth_min aft = aft[indd] ind = ind[indd] n_templates = n_templates[indd] ts = patient.times[ind] # Collect counts for i in xrange(aft.shape[0] - 1): for j in xrange(i + 1, aft.shape[0]): dij = j - i if ((ts[j] - ts[i]) > dt[1]) or ((ts[j] - ts[i]) < dt[0]): continue hist += np.histogram2d(aft[i].ravel(), aft[j].ravel(), bins=[pp.binsx, pp.binsy])[0] if use_save: from hivwholeseq.patients.filenames import get_propagator_filename if pnames is None: fn_out = get_propagator_filename(['all'], fragments, dt) else: fn_out = get_propagator_filename(pnames, fragments, dt) # NOTE: do NOT make the call below recursive by default if not os.path.isdir(os.path.dirname(fn_out)): os.mkdir(os.path.dirname(fn_out)) d_out = {'HIV_final_frequency': pp.binsyc, 'HIV_initial_frequency': pp.binsxc, 'HIV_prop': hist,
data = pd.read_excel(Location) from pandas.plotting import scatter_matrix scatter_matrix(data, alpha=0.4, figsize=(6, 6), diagonal='hist') correls = data.corr('pearson') from sklearn.metrics import mutual_info_score bins = 10 x = data['X_rand'] y = data['Y_X_no_lineal'] c_xy = np.histogram2d(x, y, bins)[0] mi = mutual_info_score(None, None, contingency=c_xy) / np.log(2) mi_matrix = np.zeros(correls.shape) for i, column1 in enumerate(data): for j, column2 in enumerate(data): x = data[column1] y = data[column2] c_xy = np.histogram2d(x, y, bins)[0] mi = mutual_info_score(None, None, contingency=c_xy) / np.log(2) mi_matrix[i][j] = mi mi_matrix = pd.DataFrame(data=mi_matrix, columns=correls.columns.values, index=correls.index.values)
def q_fit(out, sacn): #out='/scratch/dw1519/galex/co/co32_1-10_new' co_list = [] info_list = [] co_files = sorted(glob.glob(out + "/[0-9]*.npy"), key=get_key) info_files = sorted(glob.glob(out + "/info_*"), key=get_key_info) ''' for i in range(0, 27): co = np.load(out+'/radec/{0}.npy'.format((i+1)*100))/36000./800/0.001666*2400 info = np.load(out+'/info_{0}.npy'.format((i+1)*100))[1:] ''' for cf, inf in zip(co_files, info_files): co = np.load(cf)[1:] / 36000. / 800 / 0.001666 * 2400 info = np.load(inf)[1:] co_list.append(co) info_list.append(info) co = np.concatenate(co_list, axis=0) info = np.concatenate(info_list, axis=0) mask1 = (co[:, 1] >= -15) & (co[:, 1] <= 15) mask2 = (co[:, 0] >= -15) & (co[:, 0] <= 15) centroids = [] for q in range(0, 32): print('q={0}'.format(q)) mask3 = info[:, 2] == q hist_size = [100, 100] H,xedges,yedges=np.histogram2d(co[mask1&mask2&mask3,1], co[mask1&mask2&mask3,0],\ bins=hist_size, range=([ [-15,15],[-15,15] ])) data = H.byteswap(True).newbyteorder() data = data.copy(order='C') data = data.byteswap(True).newbyteorder() t = 50 c_data = c3.find_centroid(data, t) if c_data is not None: cy, cx, max_value, flux = c_data centroids.append([ -15. + cx * 30. / hist_size[0], -15. + cy * 30. / hist_size[0] ]) else: centroids.append([0, 0]) np.save(out + '/centroids_q.npy', centroids) q = np.arange(32) centroids = np.array(centroids) plt.plot(q, centroids[:, 0], '.k') plt.xlabel('q') plt.ylabel(r'mean $\Delta x$ [pixel]') plt.title(r'scan{0:>5}'.format(scan)) plt.savefig(out + '/q_cx.png', dpi=190) plt.clf() plt.plot(q, centroids[:, 1], '.k') plt.xlabel('q') plt.ylabel(r'mean $\Delta y$ [pixel]') plt.title(r'scan{0:>5}'.format(scan)) plt.savefig(out + '/q_cy.png', dpi=190) plt.clf() ''' c = np.load(out+'/centroids.npy') ya = np.arange(30)+2 slope, intercept, r_value, p_value, std_err = stats.linregress(ya, c[:,1]) print(slope, intercept, r_value, p_value, std_err) np.save(out+'/q_fit.npy', [slope, intercept, r_value, p_value, std_err]) ''' #q hist mask = (co[:, 0] >= -15) & (co[:, 0] <= 15) & (info[:, 2] < 24) imsz = [24, 60] H,xedges,yedges=np.histogram2d(info[mask,2], co[mask,0],\ bins=imsz) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal',\ extent=[-15, 15, 0, 24], origin='lower', norm=LogNorm()) q = np.arange(32) plt.plot(centroids[:, 0], q, 'or') plt.xlim(-15, 15) plt.ylim(0, 24) plt.title('scan{0:>5}'.format(scan)) plt.xlabel(r'$\Delta x$ [pixel]') plt.ylabel('q') plt.savefig(out + '/q-x_tot_fit.png', dpi=190) plt.clf() mask = (co[:, 1] >= -15) & (co[:, 1] <= 15) & (info[:, 2] < 24) imsz = [24, 60] H,xedges,yedges=np.histogram2d(info[mask,2], co[mask,1],\ bins=imsz) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal',\ extent=[-15, 15, 0, 24], origin='lower', norm=LogNorm()) plt.plot(centroids[:, 1], q, 'or') plt.xlim(-15, 15) plt.ylim(0, 24) plt.title('scan{0:>5}'.format(scan)) plt.xlabel(r'$\Delta y$ [pixel]') plt.ylabel('q') plt.savefig(out + '/q-y_tot_fit.png', dpi=190) plt.clf()
def orientation_analysis(): """ Demonstrates how to determine and visualize orientation of structures. """ # Create test image with given main fibre direction main_azimuthal = np.pi/4 main_polar = np.pi/2 img, true_orientations = generate_orientation_image(main_azimuthal, main_polar) # Save it for later visualization pi.writetif(img, output_file('cylinders')) # Calculate orientation of cylinders # Note that we need to convert the input image to float32 # format as it is used as an output image, too. # Additionally we need images to store the azimuthal and polar # orientation angles. pi.convert(img, ImageDataType.FLOAT32) azimuthal = pi.newimage(ImageDataType.FLOAT32) polar = pi.newimage(ImageDataType.FLOAT32) pi.cylinderorientation(img, azimuthal, polar, 1, 1) # Now img has been replaced with 'orientation energy' pi.writetif(img, output_file('cylinders_energy')) # Make a color-coded visualization of the orientations r = pi.newimage() g = pi.newimage() b = pi.newimage() pi.mainorientationcolor(img, azimuthal, polar, main_azimuthal, main_polar, r, g, b) #pi.axelssoncolor(img, azimuthal, polar, r, g, b) # This is another possibility if main orientation is not available. pi.writeraw(r, g, b, output_file('cylinders_main_orientation_color')) # Make orientation histogram. # Energy is used as weight hist = pi.newimage(ImageDataType.FLOAT32) bins1 = pi.newimage(ImageDataType.FLOAT32) bins2 = pi.newimage(ImageDataType.FLOAT32) pi.whist2(azimuthal, -np.pi, np.pi, 20, polar, 0, np.pi, 10, img, hist, bins1, bins2) # 20 azimuthal angle bins, 10 polar angle bins # Make a plot that compares the true orientation distribution to the estimated one import matplotlib.pyplot as plt fig = plt.figure(figsize=(4.5, 6)) # First plot the true orientations plt.subplot(2, 1, 1) # Convert directions to polar coordinates using the same convention that pi2 uses azs = [] pols = [] for dir in true_orientations: x = dir[0] y = dir[1] z = dir[2] r = np.sqrt(x * x + y * y + z * z) azimuthal = np.arctan2(y, x) polar = np.arccos(z / r) azs.append(azimuthal) pols.append(polar) # Calculate orientation histogram using the NumPy method hst, xedges, yedges = np.histogram2d(azs, pols, range=[[-np.pi, np.pi], [0, np.pi]], bins=[20, 10]) # Plot the histogram pltimg = plt.imshow(hst.transpose(), extent=(xedges[0], xedges[-1], yedges[0], yedges[-1])) cbar = fig.colorbar(pltimg) plt.xlabel('Azimuthal angle [rad]') plt.ylabel('Polar angle [rad]') plt.title('True distribution of cylinder orientations') # Now plot the histogram estimated from the image plt.subplot(2, 1, 2) pltimg = plt.imshow(hist.get_data(), extent=(-np.pi, np.pi, 0, np.pi)) cbar = fig.colorbar(pltimg) plt.xlabel('Azimuthal angle [rad]') plt.ylabel('Polar angle [rad]') plt.title('Distribution estimated from the image') # Show and print the figure plt.tight_layout() plt.show(block=False) plt.savefig(output_file('bivariate_histogram_comparison.png'))
if False: out = '/home/dw1519/dw1519/galex/plots/co32-10/radec' co_list = [] info_list = [] for i in range(0, 28): co = np.load(out + '/{0}.npy'.format( (i + 1) * 100)) / 36000. / 800 / 0.001666 * 2400 info = np.load(out + '/info_{0}.npy'.format((i + 1) * 100))[1:] co_list.append(co) info_list.append(info) co = np.concatenate(co_list, axis=0) info = np.concatenate(info_list, axis=0) mask = (co[:, 0] >= -15) & (co[:, 0] <= 15) & (info[:, 2] < 24) imsz = [24, 60] H,xedges,yedges=np.histogram2d(info[mask,2], co[mask,0],\ bins=imsz) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal',\ extent=[-15, 15, 0, 24], origin='lower', norm=LogNorm()) ''' plt.scatter(co[:,1], info[1:,1], s=0.5, alpha=0.005) plt.xlim(-15,40) plt.title('scan{0:>5} t={1}-{2}s'.format(239, i*50, (i+1)*50)) plt.xlabel(r'$\Delta y$ [pixel]') plt.ylabel('YA') ''' #ya = np.arange(30)+2 #plt.plot(-0.0281724222131*ya+0.489247048825, ya, '-r') #239 #plt.plot(-0.0426715749118*ya+0.714434337023, ya, '-r') #32 #plt.title('scan{0:>5} t={1}-{2}s'.format(239, i*50, (i+1)*50)) #plt.title(r'scan{0:>5} $\Delta y$=-0.043YA+0.714'.format(32))
data_z_error_up.append(plot.GetErrorYhigh(j)) data_z_error_down.append(plot.GetErrorYlow(j)) # Create a figure instance fig = plt.figure(1, figsize=(7, 7), dpi=300) # Create an axes instance ax = fig.add_subplot(111) # ax.margins(0.1, 0.1) pt_binning = np.asarray(pt_binnings[key.GetName()]) # Bin data (simply reshape the data_z array z, _, _ = np.histogram2d(data_x, data_y, weights=data_z, bins=(pt_binning, eta_binning)) z_error_up, _, _ = np.histogram2d(data_x, data_y, weights=data_z_error_up, bins=(pt_binning, eta_binning)) z_error_down, _, _ = np.histogram2d(data_x, data_y, weights=data_z_error_down, bins=(pt_binning, eta_binning)) cax = ax.pcolormesh(pt_binning, eta_binning, z.T, cmap='viridis') fig.colorbar(cax) ax.set_ylim([eta_binning[0], eta_binning[-1]]) # ax.set_xlim([max(1, pt_binning[0]), pt_binning[-1]])
def view_lightcurve(self, start: float = -10, stop: float = 20.0, dt: float = 1.0, use_binner: bool = False, use_echans_start: int = 0, use_echans_stop: int = -1) -> plt.Figure: # type: (float, float, float, bool) -> None """ :param start: :param stop: :param dt: :param use_binner: """ # validate echan mask input if not isinstance(use_echans_start, int): log.error(f"The use_echans_start variable must be a integer." f" Input is {use_echans_start}.") raise AssertionError() if not np.abs(use_echans_start) < self.n_channels: log.error( f"The use_echans_start variable must be" f"between {(-1)*(self.n_channels-1)} and {self.n_channels-1}." f" Input is {use_echans_start}.") raise AssertionError() if not isinstance(use_echans_stop, int): log.error(f"The use_echans_stop variable must be a integer." f" Input is {use_echans_stop}.") raise AssertionError() if not np.abs(use_echans_stop) < self.n_channels: log.error( f"The use_echans_stop variable must be" f"between {(-1)*(self.n_channels-1)} and {self.n_channels-1}." f" Input is {use_echans_start}.") raise AssertionError() if use_echans_start < 0: use_echans_start = self.n_channels + use_echans_start if use_echans_stop < 0: use_echans_stop = self.n_channels + use_echans_stop if not use_echans_stop >= use_echans_start: log.error(f"The use_echans_stop variable must be larger" f" or equal than the use_echans_start variable" f" Input is use_echans_start: {use_echans_start}" f" > use_echans_stop: {use_echans_stop}") raise AssertionError() # get echan bins echan_bins = np.arange(use_echans_start, use_echans_stop + 2, 1) - 0.5 if use_binner: # we will use the binner object to bin the # light curve and ignore the normal linear binning bins = self.bins.time_edges # perhaps we want to look a little before or after the binner if start < bins[0]: pre_bins = np.arange(start, bins[0], dt).tolist()[:-1] pre_bins.extend(bins) bins = pre_bins if stop > bins[-1]: post_bins = np.arange(bins[-1], stop, dt) bins.extend(post_bins[1:]) else: # otherwise, just use regular linear binning bins = np.arange(start, stop + dt, dt) cnts, bins, _ = np.histogram2d(self.arrival_times, self.measurement, bins=(bins, echan_bins)) cnts = np.sum(cnts, axis=1) time_bins = np.array([[bins[i], bins[i + 1]] for i in range(len(bins) - 1)]) # width = np.diff(bins) width = [] # now we want to get the estimated background from the polynomial fit if self.poly_fit_exists: # we will store the bkg rate for each time bin bkg = [] for j, tb in enumerate(time_bins): # zero out the bkg tmpbkg = 0.0 # we will use the exposure for the width this_width = self.exposure_over_interval(tb[0], tb[1]) # sum up the counts over this interval for poly in self.polynomials[use_echans_start:use_echans_stop + 1]: tmpbkg += poly.integral(tb[0], tb[1]) # capture the exposure width.append(this_width) # capture the bkg *rate* bkg.append(old_div(tmpbkg, this_width)) else: bkg = None for j, tb in enumerate(time_bins): this_width = self.exposure_over_interval(tb[0], tb[1]) width.append(this_width) width = np.array(width) # pass all this to the light curve plotter if self.time_intervals is not None: selection = self.time_intervals.bin_stack else: selection = None if self.poly_intervals is not None: bkg_selection = self.poly_intervals.bin_stack else: bkg_selection = None return binned_light_curve_plot( time_bins=time_bins, cnts=cnts, width=width, bkg=bkg, selection=selection, bkg_selections=bkg_selection, )
def meta_plot(out, scan): #out='/scratch/dw1519/galex/co/co32_1-10_new' co_list = [] info_list = [] co_files = sorted(glob.glob(out + "/[0-9]*.npy"), key=get_key) info_files = sorted(glob.glob(out + "/info_*"), key=get_key_info) print len(co_files) print len(info_files) ''' for i in range(0, 27): co = np.load(out+'/radec/{0}.npy'.format((i+1)*100))/36000./800/0.001666*2400 info = np.load(out+'/info_{0}.npy'.format((i+1)*100))[1:] ''' for cf, inf in zip(co_files, info_files): co = np.load(cf)[1:] / 36000. / 800 / 0.001666 * 2400 info = np.load(inf)[1:] co_list.append(co) info_list.append(info) co = np.concatenate(co_list, axis=0) info = np.concatenate(info_list, axis=0) print co.shape print info.shape #ya hist mask = (co[:, 1] >= -15) & (co[:, 1] <= 40) imsz = [32, 110] H,xedges,yedges=np.histogram2d(info[mask,1], co[mask,1],\ bins=imsz) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal',\ extent=[-15, 40, 0, 32], origin='lower', norm=LogNorm()) ''' plt.scatter(co[:,1], info[1:,1], s=0.5, alpha=0.005) plt.xlim(-15,40) plt.title('scan{0:>5} t={1}-{2}s'.format(239, i*50, (i+1)*50)) plt.xlabel(r'$\Delta y$ [pixel]') plt.ylabel('YA') ''' #ya = np.arange(30)+2 #plt.plot(-0.0281724222131*ya+0.489247048825, ya, '-r') #239 #plt.plot(-0.0426715749118*ya+0.714434337023, ya, '-r') #32 #plt.title('scan{0:>5} t={1}-{2}s'.format(239, i*50, (i+1)*50)) #plt.title(r'scan{0:>5} $\Delta y$=-0.043YA+0.714'.format(32)) plt.xlim(-15, 40) plt.ylim(0, 32) plt.title('scan{0:>5}'.format(scan)) plt.xlabel(r'$\Delta y$ [pixel]') plt.ylabel('YA') #plt.savefig(out+'/xya_{0}.png'.format((i+1)*100), dpi=190) plt.savefig(out + '/ya-y_tot.png', dpi=190) plt.clf() mask = (co[:, 0] >= -15) & (co[:, 0] <= 15) imsz = [32, 60] H,xedges,yedges=np.histogram2d(info[mask,1], co[mask,0],\ bins=imsz) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal',\ extent=[-15, 15, 0, 32], origin='lower', norm=LogNorm()) plt.xlim(-15, 15) plt.ylim(0, 32) plt.title('scan{0:>5}'.format(scan)) plt.xlabel(r'$\Delta x$ [pixel]') plt.ylabel('YA') plt.savefig(out + '/ya-x_tot.png', dpi=190) plt.clf() #q hist mask = (co[:, 0] >= -15) & (co[:, 0] <= 15) & (info[:, 2] < 24) imsz = [24, 60] H,xedges,yedges=np.histogram2d(info[mask,2], co[mask,0],\ bins=imsz) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal',\ extent=[-15, 15, 0, 24], origin='lower', norm=LogNorm()) plt.xlim(-15, 15) plt.ylim(0, 24) plt.title('scan{0:>5}'.format(scan)) plt.xlabel(r'$\Delta x$ [pixel]') plt.ylabel('q') plt.savefig(out + '/q-x_tot.png', dpi=190) plt.clf() mask = (co[:, 1] >= -15) & (co[:, 1] <= 15) & (info[:, 2] < 24) imsz = [24, 60] H,xedges,yedges=np.histogram2d(info[mask,2], co[mask,1],\ bins=imsz) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal',\ extent=[-15, 15, 0, 24], origin='lower', norm=LogNorm()) plt.xlim(-15, 15) plt.ylim(0, 24) plt.title('scan{0:>5}'.format(scan)) plt.xlabel(r'$\Delta y$ [pixel]') plt.ylabel('q') plt.savefig(out + '/q-y_tot.png', dpi=190) plt.clf() #xa hist mask = (co[:, 1] >= -15) & (co[:, 1] <= 15) imsz = [32, 60] H,xedges,yedges=np.histogram2d(info[mask,0], co[mask,1],\ bins=imsz) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal',\ extent=[-15, 15, 0, 32], origin='lower', norm=LogNorm()) plt.xlim(-15, 15) plt.ylim(0, 32) plt.title('scan{0:>5}'.format(scan)) plt.xlabel(r'$\Delta y$ [pixel]') plt.ylabel('XA') plt.savefig(out + '/xa-y_tot.png', dpi=190) plt.clf() mask = (co[:, 0] >= -15) & (co[:, 0] <= 15) imsz = [32, 60] H,xedges,yedges=np.histogram2d(info[mask,0], co[mask,0],\ bins=imsz) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal',\ extent=[-15, 15, 0, 32], origin='lower', norm=LogNorm()) plt.xlim(-15, 15) plt.ylim(0, 32) plt.title('scan{0:>5}'.format(scan)) plt.xlabel(r'$\Delta x$ [pixel]') plt.ylabel('XA') plt.savefig(out + '/xa-x_tot.png', dpi=190) plt.clf()
def histogram_scatter(ax, datatable, markers, range=None, color_marker=None, min_cells_per_bin=1, no_bins_x=256j, no_bins_y=256j, interpolation='nearest'): """ Draws a 2d histogram of the given markers in the given range. If a bin has more than min_cells_per_bin, than it is colored black. This gives the same effect as a scatter plot. range is a tuple in the form: (min_dim_1, min_dim_2, max_dim_1, max_dim_2). You can also color each bin according to the average value for another dim, among the cells in the bin. This is done using the color_marker parameter. """ cols = datatable.get_cols(*markers) if not range: fixed_range = [min(cols[0]), min(cols[1]), max(cols[0]), max(cols[1])] else: fixed_range = range hist, x_edges, y_edges = np.histogram2d(cols[0], cols[1], [ np.r_[fixed_range[0]:fixed_range[2]:no_bins_x], np.r_[fixed_range[1]:fixed_range[3]:no_bins_y] ]) final_hist = np.sign( np.subtract(np.clip(np.abs(hist), min_cells_per_bin, np.inf), min_cells_per_bin)) if color_marker: weights = datatable.get_cols(color_marker)[0] weighted_hist, x_edges, y_edges = np.histogram2d( cols[0], cols[1], [ np.r_[fixed_range[0]:fixed_range[2]:no_bins_x], np.r_[fixed_range[1]:fixed_range[3]:no_bins_y] ], None, False, weights) averages = np.true_divide(weighted_hist, hist) averages[np.isnan(averages)] = np.NaN averages[final_hist == 0] = np.NaN colored_hist = averages data_to_draw = colored_hist cmap = cm.jet else: data_to_draw = final_hist cmap = cm.gist_yarg extent = [x_edges[0], x_edges[-1], y_edges[0], y_edges[-1]] image = ax.imshow(data_to_draw.T, extent=extent, cmap=cmap, origin='lower', interpolation=interpolation) ax.set_xlabel(str(markers[0]) + ' ', size='x-small') ax.set_ylabel(str(markers[1]) + ' ', size='x-small') ax.figure.subplots_adjust(bottom=0.15) cbar = ax.figure.colorbar(image) if color_marker: cbar.set_label(color_marker, fontsize='xx-small') vals, legend = datatable.get_legend(color_marker) if legend: cbar.set_ticks(vals) cbar.ax.set_yticklabels(legend) #label = cbar.get_label() #label.set_fontsize('xx-small') ax.set_aspect('auto') return hist.T, extent
inj_chi *= np.cos(inj_arr["inclination"]) smallest_match = match.min() from matplotlib.figure import Figure from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas name, ext = os.path.splitext(os.path.basename(fname)) # # binned, weighted (effective) fitting factor plots # fig = Figure() ax = fig.add_subplot(111) vals, xedges, yedges = np.histogram2d(inj_arr["mass1"], inj_arr["mass2"], bins=(10,10), weights= match**3 * inj_sigmasq**(3./2)) norm, xedges, yedges = np.histogram2d(inj_arr["mass1"], inj_arr["mass2"], bins=(10,10), weights=inj_sigmasq**(3./2)) vals = ( vals / np.array(norm + 1e-10, dtype="float") )**(1./3) x, y= np.meshgrid(xedges, yedges) coll = ax.pcolor(x, y, vals.T, vmin=0.9*vals[vals>0].min(), vmax=1) ax.set_xlabel("$m_1$ ($M_\odot$)") ax.set_ylabel("$m_2$ ($M_\odot$)") ax.set_xticks((xedges[:-1] + xedges[1:])/2) ax.set_yticks((yedges[:-1] + yedges[1:])/2) ax.set_xticklabels(["%.1f" % round(k,1) for k in ax.get_xticks()]) ax.set_yticklabels(["%.1f" % round(k,1) for k in ax.get_yticks()]) ax.set_xlim([xedges.min(), xedges.max()]) ax.set_ylim([yedges.min(), yedges.max()]) fig.colorbar(coll, ax=ax).set_label("Effective Fitting Factor") ax.grid(True) canvas = FigureCanvas(fig)
def make_occupancy_hist(cols, rows, ncols=80, nrows=336): hist, xedges, yedges = np.histogram2d(rows, cols, bins=(nrows, ncols), range=[[1, nrows], [1, ncols]]) # extent = [yedges[0] - 0.5, yedges[-1] + 0.5, xedges[-1] + 0.5, xedges[0] - 0.5] return np.ma.masked_equal(hist, 0) # , extent
for flavor in range(flavD): for matter in range(mattD): for current in range(currD): # define the bins. These will be used for both the x and y axes nBins = 30 # bins= np.linspace(10**-4, 1.0, nBins) bins = np.logspace(-4, 0, nBins) # this is needed to put the colorbar on a log scale from matplotlib.colors import LogNorm # creat the plot title if use_weights: normed_hist = np.histogram2d( LepI_bjx[flavor][matter][current], LepI_bjy[flavor][matter][current], bins, weights=LepI_weights[flavor][matter][current])[0] # normed_hist */ else: # calculate the Lepton Injector histogram hist_details = np.histogram2d( LepI_bjx[flavor][matter][current], LepI_bjy[flavor][matter][current], bins) # normalize the bins, and assign them a new name normed_hist = np.array(hist_details[0]) / len( LepI_bjx[flavor][matter][current]) # use the matplotlib pcolormesh function to creat the histogram # note - pcolormesh expects the data as (Y,X), and so I take the transpose of hte normalized histogram
def plotContour(filename, source=False, particle='all'): df = pd.read_hdf(filename, keys='procdf') if particle == 'all': x = np.array(df['x']) y = np.array(df['y']) z = np.array(df['z']) energy = np.array(df['energy'] * 1000) plot_title = 'Spot Size, $^{241}$Am 10$^7$ Primaries, all energies' elif particle == 'alpha': alpha_df = df.loc[df.energy > 5] x = np.array(alpha_df['x']) y = np.array(alpha_df['y']) z = np.array(alpha_df['z']) energy = np.array(alpha_df['energy'] * 1000) plot_title = 'Spot Size, $^{241}$Am 10$^7$ Primaries, Energy $>$ 5 MeV' elif particle == 'gamma': gamma_df = df.loc[(df.energy > .04) & (df.energy < 0.08)] x = np.array(gamma_df['x']) y = np.array(gamma_df['y']) z = np.array(gamma_df['z']) energy = np.array(gamma_df['energy'] * 1000) plot_title = 'Spot Size, $^{241}$Am 10$^7$ Primaries, 60 kev $<$ Energy $<$ 80 keV' else: print('specify particle type!') exit() # fig, ax = plt.subplots(ncols=3, figsize=(16,8)) fig, ax = plt.subplots(figsize=(10, 8)) nbins = 100 counts, xbins, ybins = np.histogram2d(x, y, bins=nbins, normed=True) # hist = ax[0].hist2d(x, y, bins=nbins, cmap='plasma', normed=True) hist = ax.hist2d(x, y, bins=nbins, cmap='plasma', normed=True) # plt.scatter(x, y, c=energy, s=1, cmap='plasma') # cb = plt.colorbar() # cb.set_label("Energy (keV)", ha = 'right', va='center', rotation=270, fontsize=14) # cb.ax.tick_params(labelsize=12) # ax[0].set_xlim(-10,10) # ax[0].set_ylim(9,19) # ax[0].set_xlabel('x position (mm)', fontsize=14) # ax[0].set_ylabel('y position (mm)', fontsize=14) # ax[0].set_title('Histogram of data- 100 bins', fontsize=14) # ax.set_xlim(-10,10) # ax.set_ylim(-10, 10) # # ax.set_ylim(9,19) # ax.set_xlabel('x position (mm)', fontsize=14) # ax.set_ylabel('y position (mm)', fontsize=14) # ax.set_title('Histogram of data- 100 bins', fontsize=14) # CB2 = plt.colorbar(hist[3], shrink=0.8, extend='both') # xi, yi, zi, bw, score = kde2D(x, y, bins=500, optimize_bw=True) # xi, yi, zi, bw, score = kde2D(x, y, bandwidth=0.25, bins=500, optimize_bw=False) # x_score = np.linspace(0, len(score), 200) # fig, ax = plt.subplots(figsize=(10,8)) # plt.plot(score) # ax.hist(score, bins=100) # plt.show() # exit() # kdeColor = ax[1].pcolormesh(xi, yi, zi, cmap='plasma') # ax[1].pcolormesh(xi, yi, norm_zi.reshape(xi.shape), cmap='plasma') # ax[1].set_xlim(-10,10) # ax[1].set_ylim(9,19) # ax[1].set_xlabel('x position (mm)', fontsize=14) # ax[1].set_ylabel('y position (mm)', fontsize=14) # ax[1].set_title('KDE-smoothed \n Bandwidth = %.2f' % bw, fontsize=14) # CB1 = plt.colorbar(kdeColor, shrink=0.8, extend='both') # levels = [0.1] # contour_hist = ax[2].contour(counts.T,extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()],cmap='plasma') # CS = ax[2].contour(xi, yi, zi, cmap='plasma') # ax[2].clabel(CS, fmt = '%.2f', fontsize=14) # CB = plt.colorbar(CS, shrink=0.8, extend='both') # ax[2].clabel(contour_hist, fmt = '%.2f', fontsize=20) # CB = plt.colorbar(contour_hist, shrink=0.8, extend='both') # ax[2].set_xlim(-10,10) # ax[2].set_ylim(9,19) # ax[2].set_xlabel('x position (mm)', fontsize=14) # ax[2].set_ylabel('y position (mm)', fontsize=14) # ax[2].set_title('Contour plot from KDE', fontsize=14) # ax[2].set_title('Contour plot from histogram', fontsize=14) # CB = plt.colorbar(contour_hist, shrink=0.8, extend='both') # ax[2].clabel(contour_hist, fmt = '%.2f', fontsize=20) # plt.xlim(-40,40) # plt.ylim(-40,40) # ax[0].set_xlabel('x position (mm)', fontsize=16) # ax[0].set_ylabel('y position (mm)', fontsize=16) # plt.setp(ax[0].get_xticklabels(), fontsize=14) # plt.setp(ax[0].get_yticklabels(), fontsize=14) # plt.title(plot_title, fontsize=16) plt.show()
def plot_beam(v,beam,plot_XY=False,plot_spect=True,**imshowargs): p_arrays_init = cox.make_beam_sliced(beam, \ div_chirp=0.5*(beam['E'][0]+beam['E'][1])) if plot_spect: fig, (ax1,ax2) = plt.subplots(1,2,figsize=(10,4)) else: fig, ax1 = plt.subplots(1,1,figsize=(5,5)) if plot_XY: fig2,(ax3,ax4) = plt.subplots(1,2,figsize=(10,4)) p_arrays = oclt.deepcopy(p_arrays_init) QuadGradients1 = oclt.deepcopy(QuadGradients) QuadGradients1['QAP1'] *= v.children[0].value QuadGradients1['QAP2'] *= v.children[1].value QuadGradients1['QAP3'] *= v.children[2].value Drifts1 = oclt.deepcopy(Drifts) Drifts1['LPWA-QAP1'] = v.children[3].value*1e-3 Drifts1['QAP3-IMG1'] = v.children[4].value*1e-2 p_arrays,outputs = cox.make_shot(p_arrays, \ QuadGradients=QuadGradients1,Drifts=Drifts1,stop_key='IMG1',) p_arrays = cox.aligh_slices(p_arrays, \ QuadGradients=QuadGradients1,Drifts=Drifts1,stop_key='IMG1') xx = np.hstack(([p_array.x()*1e3 for p_array in p_arrays])) yy = np.hstack(([p_array.y()*1e3 for p_array in p_arrays])) zz = np.hstack(([-p_array.tau()*1e3 for p_array in p_arrays])) px = np.hstack(([p_array.px()*1e3 for p_array in p_arrays])) py = np.hstack(([p_array.py()*1e3 for p_array in p_arrays])) ee = np.hstack(([(p_array.p()+1)*p_array.E*1e3 for p_array in p_arrays])) rr = np.sqrt(xx**2+yy**2) h,x,y = np.histogram2d(xx,yy,bins=800,range=[[-0.1,0.1],[-0.1,0.1]]) h = medfilt2d(np.abs(h),5) pl = ax1.imshow(h.T,aspect='auto', \ extent=(x.min(),x.max(),y.min(),y.max()),origin='lower',**imshowargs) ax1.set_xlabel('X (mm)') ax1.set_ylabel('Y (mm)') hx = h[:,h.shape[1]/2-1:h.shape[1]/2+2].mean(-1) hy = h[h.shape[0]/2-1:h.shape[0]/2+2,:].mean(0) hx -= hx.min() hat_ind = np.nonzero(hx>0.5*hx.max())[0] sx_fwhm = x[hat_ind[-1]] - x[hat_ind[0]] hy -= hy.min() hat_ind = np.nonzero(hy>0.5*hy.max())[0] sy_fwhm = y[hat_ind[-1]] - y[hat_ind[0]] if plot_spect: h,x,y = np.histogram2d(ee,rr,bins=400, \ range=[[20,250],[0,0.15]]) h = medfilt2d(np.abs(h),5) pl = ax2.imshow(h.T,\ aspect='auto',extent=(x.min(),x.max(),y.min(),y.max()), \ origin='lower',**imshowargs) ax2.set_xlabel('Electron energy (MeV)') ax2.set_ylabel('Radius (mm)') if plot_XY: h,x,y = np.histogram2d(ee,xx,bins=400, \ range=[[20,250],[-3,3]]) pl = ax3.imshow(medfilt2d(np.abs(h),5).T,\ aspect='auto',extent=(x.min(),x.max(),y.min(),y.max()), \ origin='lower',**imshowargs) h,x,y = np.histogram2d(ee,yy,bins=400, \ range=[[20,250],[-3,3]]) pl = ax4.imshow(medfilt2d(np.abs(h),5).T,\ aspect='auto',extent=(x.min(),x.max(),y.min(),y.max()), \ origin='lower',**imshowargs) ax3.set_ylabel('X (mm)') ax4.set_ylabel('Y (mm)') for ax in (ax3,ax4,): ax.set_xlabel('Electron energy (MeV)') part_select_mask = (rr<100e-3) part_select_ind = np.nonzero(part_select_mask)[0] num_parts_full = rr.shape[0] num_parts_select = part_select_mask.sum() select_frac = 100.*num_parts_select/num_parts_full sx_rms = xx[part_select_ind].std() sy_rms = yy[part_select_ind].std() sz = zz[part_select_ind].std() ox = px[part_select_ind].std() oy = py[part_select_ind].std() e_cent = ee[part_select_ind].mean() se = ee[part_select_ind].std()/e_cent print('\nFraction in r<100 um {0:g}%'.format( \ select_frac ) ) print( \ "Sx={0:g} ({1:g}), Sy={2:g} ({3:g}), Sz={4:g} [um] RMS (FWHM)\n" \ .format(sx_rms*1e3,sx_fwhm*1e3,sy_rms*1e3,sy_fwhm*1e3,sz*1e3) \ +"Ox={0:g}, Oy={1:g}" \ .format(ox,oy) \ + " [mrad]\ne={0:g} [MeV], de={1:g}%"\ .format(e_cent,se*100) ) out_dat = np.vstack((xx,yy,zz,px,py,ee )) return out_dat
# make plot using hexbin fig = plt.figure(figsize=(12, 5)) ax = fig.add_subplot(121) CS = m.hexbin(x, y, C=z, gridsize=bins, cmap=plt.cm.jet) # draw coastlines, lat/lon lines. m.drawcoastlines() m.drawparallels(np.arange(0, 81, 20)) m.drawmeridians(np.arange(-180, 181, 60)) m.colorbar() # draw colorbar plt.title('hexbin demo') # use histogram2d instead of hexbin. ax = fig.add_subplot(122) # remove points outside projection limb. bincount, xedges, yedges = np.histogram2d(x, y, bins=bins) mask = bincount == 0 # reset zero values to one to avoid divide-by-zero bincount = np.where(bincount == 0, 1, bincount) H, xedges, yedges = np.histogram2d(x, y, bins=bins, weights=z) H = np.ma.masked_where(mask, H / bincount) # set color of masked values to axes background (hexbin does this by default) palette = plt.cm.jet palette.set_bad(ax.get_axis_bgcolor(), 1.0) CS = m.pcolormesh(xedges, yedges, H.T, shading='flat', cmap=palette) # draw coastlines, lat/lon lines. m.drawcoastlines() m.drawparallels(np.arange(0, 81, 20)) m.drawmeridians(np.arange(-180, 181, 60)) m.colorbar() # draw colorbar plt.title('histogram2d demo')
def old_plotContour(filename, source=False, particle='all'): df = pd.read_hdf(filename, keys='procdf') if particle == 'all': x = np.array(df['x']) y = np.array(df['y']) z = np.array(df['z']) energy = np.array(df['energy'] * 1000) plot_title = 'Spot Size, $^{241}$Am 10$^7$ Primaries, all energies' elif particle == 'alpha': alpha_df = df.loc[df.energy > 5] x = np.array(alpha_df['x']) y = np.array(alpha_df['y']) z = np.array(alpha_df['z']) energy = np.array(alpha_df['energy'] * 1000) plot_title = 'Spot Size, $^{241}$Am 10$^7$ Primaries, Energy $>$ 5 MeV' elif particle == 'gamma': gamma_df = df.loc[(df.energy > .04) & (df.energy < 0.08)] x = np.array(gamma_df['x']) y = np.array(gamma_df['y']) z = np.array(gamma_df['z']) energy = np.array(gamma_df['energy'] * 1000) plot_title = 'Spot Size, $^{241}$Am 10$^7$ Primaries, 60 kev $<$ Energy $<$ 80 keV' else: print('specify particle type!') exit() fig, ax = plt.subplots(ncols=3) nbins = 10 counts, xbins, ybins = np.histogram2d(x, y, bins=nbins, normed=True) ax[0].hist2d(x, y, bins=nbins, cmap='plasma', normed=True) # plt.scatter(x, y, c=energy, s=1, cmap='plasma') # cb = plt.colorbar() # cb.set_label("Energy (keV)", ha = 'right', va='center', rotation=270, fontsize=14) # cb.ax.tick_params(labelsize=12) ax[0].set_xlim(-10, 10) ax[0].set_ylim(9, 19) ax[0].set_xlabel('x position (mm)', fontsize=14) ax[0].set_ylabel('y position (mm)', fontsize=14) ax[0].set_title('2D histogam- 10 bins', fontsize=14) # k_arr = np.column_stack((x,y)) # k = kde.gaussian_kde(k_arr.T) xi, yi = np.mgrid[x.min():x.max():nbins * 1j, y.min():y.max():nbins * 1j] # zi = k(np.vstack([xi.flatten(), yi.flatten()])) positions = np.vstack([xi.flatten(), yi.flatten()]) values = np.vstack([x, y]) kernel = kde.gaussian_kde(values) zi = np.reshape(kernel(positions).T, xi.shape) print(np.sum(zi)) scale = len(x) / np.sum(zi) zi *= scale # print(np.sum(counts)) # print(np.min(zi), np.max(zi)) # exit() # norm = np.linalg.norm(zi) # norm_zi = zi/norm print(zi) # print(xi.flatten()) exit() # exit() # ax[1].pcolormesh(xi, yi, zi.reshape(xi.shape), cmap='plasma') ax[1].pcolormesh(xi, yi, zi, cmap='plasma') # ax[1].pcolormesh(xi, yi, norm_zi.reshape(xi.shape), cmap='plasma') ax[1].set_xlim(-10, 10) ax[1].set_ylim(9, 19) ax[1].set_xlabel('x position (mm)', fontsize=14) ax[1].set_ylabel('y position (mm)', fontsize=14) ax[1].set_title('KDE-smoothed', fontsize=14) levels = [0.1] contour_hist = ax[2].contour( counts.T, extent=[xbins.min(), xbins.max(), ybins.min(), ybins.max()], cmap='plasma') # CS = ax[2].contour(xi, yi, zi.reshape(xi.shape), cmap='plasma') # CS = ax[2].contour(xi, yi, zi, cmap='plasma') # CSF = ax[2].contourf(xi, yi, norm_zi.reshape(xi.shape), cmap='plasma') # CSF = ax[2].contourf(xi, yi, zi.reshape(xi.shape), cmap='plasma') # plt.clabel(CS, fmt = '%2.1d', colors = 'k', fontsize=14) # ax[2].clabel(CS, fmt = '%.2f', fontsize=20) # CB = plt.colorbar(CS, shrink=0.8, extend='both') # ax[2].clabel(contour_hist, fmt = '%.2f', fontsize=20) CB = plt.colorbar(contour_hist, shrink=0.8, extend='both') ax[2].set_xlim(-10, 10) ax[2].set_ylim(9, 19) ax[2].set_xlabel('x position (mm)', fontsize=14) ax[2].set_ylabel('y position (mm)', fontsize=14) # ax[2].set_title('Contour plot from KDE', fontsize=14) ax[2].set_title('Contour plot from histogram', fontsize=14) # CB = plt.colorbar(contour_hist, shrink=0.8, extend='both') # ax[2].clabel(contour_hist, fmt = '%.2f', fontsize=20) # plt.xlim(-40,40) # plt.ylim(-40,40) # ax[0].set_xlabel('x position (mm)', fontsize=16) # ax[0].set_ylabel('y position (mm)', fontsize=16) # plt.setp(ax[0].get_xticklabels(), fontsize=14) # plt.setp(ax[0].get_yticklabels(), fontsize=14) # plt.title(plot_title, fontsize=16) plt.show() if source == True: source_df = pd.read_hdf(filename, keys='sourcePV_df') sourceEnergy = np.array(source_df['energy'] * 1000) x_source = np.array(source_df['x']) print(len(x_source))