Exemplo n.º 1
0
    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 ##