def array2data2D(self, data, weights=None, normed=False, binning=1, reBin=None): """ Convert array of data to internal format - Designed for arrays of raw, un-binned data. - If you pass values here from an existing histogram ('weights' is not None and the 'data' param is just bin centers), it is possible to re-bin this histogram using the 'reBin' keyword """ try: x = data['x'] y = data['y'] except TypeError: x = data[0] y = data[1] data, bins_x, bins_y = np.histogram2d(x, y, bins=binning, normed=normed, weights=weights) results = Hist() results.content = data.flatten() # data is a ndarray (nxbins,nybins) results.bins = {'x': bins_x, 'y': bins_y} xcenter, ycenter = tools.dummy_bins2D(tools.midpoints(bins_x), tools.midpoints(bins_y)) xwidth, ywidth = tools.dummy_bins2D(tools.widths(bins_x), tools.widths(bins_y)) results.center = {'x': xcenter, 'y': ycenter} results.width = {'x': xwidth, 'y': ywidth} results.error = np.sqrt(data) if weights is not None: # scipy.stats to get sumw2 (x,y,weights should have the same shape) results.error = results.sumw2_2D(xdata=x, ydata=y, values=weights) if reBin is not None: # re-binning after making data from array, likely that the user # passed in binned data and wants to re-bin. results.Rebin2D(reBin) if normed: results.normalize() # normalize after re-binning results.xbins = results.bins['x'] results.ybins = results.bins['y'] results.xcenter = results.center['x'] results.ycenter = results.center['y'] results.xwidth = results.width['x'] results.ywidth = results.width['y'] return results ## THE END ##
def array2data(self, data, weights=None, normed=False, binning=1, reBin=None): """ Convert array of data to internal format - Designed for arrays of raw, un-binned data. - If you pass values here from an existing histogram ('weights' is not None and the 'data' param is just bin centers), it is possible to re-bin this histogram using the 'reBin' keyword """ data, bins = np.histogram(data, bins=binning, weights=weights, normed=normed) results = Hist() results.content = data results.bins = bins results.center = tools.midpoints(bins) results.width = tools.widths(bins) results.error = np.sqrt(data) if weights is not None: # numpy digitize to get sumw2 results.error = results.sumw2_1D(xdata=data, values=weights) if reBin is not None: results.Rebin(reBin) if normed: results.normalize() # normalize after re-binning return results
def Rebin(self, reBin): if isinstance(reBin, (int, long)): # merge bins together by some integer factor 'reBin' if reBin <= 0 or self.content.size % reBin: print " WARNING : Cannot re-bin with {0}".format(reBin) print " : Not re-binning the histogram" return else: reBin = np.array( [self.bins[i] for i in range(0, self.bins.size, reBin)]) elif not all((i in self.bins) for i in reBin): print " WARNING : Cannot re-bin with {0}".format(reBin) print " : Not re-binning the histogram" return # re-bin the histogram (by making a new histogram) bin_contents, bin_edges = np.histogram(self.center, bins=reBin, weights=self.content) self.content = bin_contents self.bins = bin_edges self.center = tools.midpoints(bin_edges) self.width = tools.widths(bin_edges) self.error = self.sumw2_1D( xdata=self.center) # use data for re-binning, not bin content return
def hist2data2D(self, histo, reBin=None, normed=False): """Convert ROOT histogram for internal use.""" bin_contents, (xbin_edges, ybin_edges) = histo.numpy() bin_contents = bin_contents.T if len(histo.allvariances) > 0: bin_errors = histo.allvariances[ 1:-1, 1: -1] # variances() doesn't produce correct values in 2D right now else: bin_errors = np.sqrt(bin_contents) xbin_centers, ybin_centers = tools.dummy_bins2D( tools.midpoints(xbin_edges), tools.midpoints(ybin_edges)) xbin_widths, ybin_widths = tools.dummy_bins2D(tools.widths(xbin_edges), tools.widths(ybin_edges)) results = Hist() results.content = bin_contents.flatten() results.error = bin_errors.flatten() results.bins = {'x': xbin_edges, 'y': ybin_edges} results.center = {'x': xbin_centers, 'y': ybin_centers} results.width = {'x': xbin_widths, 'y': ybin_widths} if reBin is not None: results.Rebin2D(reBin) if normed: results.normalize() # Set extra attributes (placeholders for the moment) results.xbins = results.bins['x'] results.ybins = results.bins['y'] results.xcenter = results.center['x'] results.ycenter = results.center['y'] results.xwidth = results.width['x'] results.ywidth = results.width['y'] return results ## THE END ##
def hist2data(self, histo, reBin=None, normed=False): """Convert ROOT histogram for internal use.""" bin_contents, bin_edges = histo.numpy() results = Hist() results.content = bin_contents results.bins = bin_edges results.center = tools.midpoints(bin_edges) results.width = tools.widths(bin_edges) if len(histo.variances) > 0: results.error = histo.variances else: results.error = np.sqrt(bin_contents) if reBin is not None: results.Rebin(reBin) if normed: results.normalize() return results
def Rebin2D(self, reBin): """Re-bin 2D histogram""" xbins = self.bins['x'] ybins = self.bins['y'] nbinsx = len(xbins) - 1 nbinsy = len(ybins) - 1 xrebin = xbins.copy() # default to original binning yrebin = ybins.copy() # default to original binning if isinstance(reBin, (int, long)): # new bins should be merged bin edges if reBin <= 0 or nbinsx % reBin or nbinsy % reBin: print " WARNING : Cannot re-bin with {0}".format(reBin) print " : Not re-binning the histogram" return else: xrebin = np.array( [xbins[i] for i in range(0, nbinsx + 1, reBin)]) yrebin = np.array( [ybins[i] for i in range(0, nbinsy + 1, reBin)]) else: # new bins should be new array bin edges that must match previous binning: # can't re-bin [0,1,4,6] into [0,3,6] try: xrebin = reBin['x'] yrebin = reBin['y'] except TypeError: xrebin = reBin[0] yrebin = reBin[1] if (not all((i in xbins) for i in xrebin)) or (not all( (i in ybins) for i in yrebin)): print " WARNING : Cannot re-bin 2D histogram using {0}".format( reBin) print " : Not re-binning histogram" return # - generate dummy values of the bin centers and pass data as weights xbins, ybins = tools.dummy_bins2D(tools.midpoints(xbins), tools.midpoints(ybins)) rb_hist2d = np.histogram2d(xbins, ybins, bins=[xrebin, yrebin], weights=self.content.tolist()) bin_contents, xbin_edges, ybin_edges = rb_hist2d self.content = bin_contents.flatten() self.bins = {'x': xbin_edges, 'y': ybin_edges} xbin_center, ybin_center = tools.dummy_bins2D( tools.midpoints(xbin_edges), tools.midpoints(ybin_edges)) xbin_width, ybin_width = tools.dummy_bins2D(tools.widths(xbin_edges), tools.widths(ybin_edges)) self.center = {'x': xbin_center, 'y': ybin_center} self.width = {'x': xbin_width, 'y': ybin_width} if _scipy_available: self.error = self.sumw2_2D(xdata=xbins, ydata=ybins) else: self.error = np.sqrt(bin_contents.flatten()) return ## THE END ##