Ejemplo n.º 1
0
    def rebinby(self, axis, factor):
        if not isinstance(factor, (numbers.Integral, numpy.integer)) or factor <= 0:
            raise TypeError("factor must be a positive integer")

        if not isinstance(axis, histbook.axis.Axis):
            expr = histbook.expr.Expr.parse(axis, defs=self._defs)
            for x in self._group + self._fixed:
                if isinstance(x, histbook.axis.RebinFactor) and expr == x._parsed:
                    axis = x
                    break

        for index, x in enumerate(self._group + self._fixed):
            if isinstance(x, histbook.axis.RebinFactor) and axis == x:
                axis = x
                break
        else:
            raise IndexError("no such rebinnable axis: {0}".format(axis))

        if isinstance(axis, histbook.axis.GroupAxis):
            newaxis, newcontent = axis._rebinsplit(factor, self._content, index)
        else:
            newaxis, newcontent = axis._rebinsplit(factor, self._content, index - len(self._group))

        outaxis = [newaxis if i == index else x for i, x in enumerate(self._group + self._fixed + self._profile)]
        out = self.__class__(*outaxis, weight=self._weight, defs=self._defs)
        out._content = newcontent
        return out
Ejemplo n.º 2
0
    def rebinby(self, axis, factor):
        """
        Reduce the number of bins by an approximate ``factor`` by combining existing bins.

        Parameters
        ----------
        axis : :py:class:`Axis <histbook.axis.Axis>`, algebraic expression (string), or index position (integer)
            the axis to rebin

        factor : positive integer
            number of bins to combine into a single bin (inexact if the number of bins in ``axis`` is not an exact multiple of ``factor``)

        Returns
        -------
        :py:class:`Hist <histbook.hist.Hist>`
            a rebinned histogram.
        """
        if not isinstance(factor,
                          (numbers.Integral, numpy.integer)) or factor <= 0:
            raise TypeError("factor must be a positive integer")

        if not isinstance(axis, histbook.axis.Axis):
            expr = histbook.expr.Expr.parse(axis, defs=self._defs)
            for x in self._group + self._fixed:
                if isinstance(
                        x, histbook.axis._RebinFactor) and expr == x._parsed:
                    axis = x
                    break

        for index, x in enumerate(self._group + self._fixed):
            if isinstance(x, histbook.axis._RebinFactor) and axis == x:
                axis = x
                break
        else:
            raise IndexError("no such rebinnable axis: {0}".format(axis))

        if isinstance(axis, histbook.axis.GroupAxis):
            newaxis, newcontent = axis._rebinsplit(factor, self._content,
                                                   index)
        else:
            newaxis, newcontent = axis._rebinsplit(factor, self._content,
                                                   index - len(self._group))

        outaxis = [
            newaxis if i == index else x
            for i, x in enumerate(self._group + self._fixed + self._profile)
        ]
        out = self.__class__(*outaxis,
                             weight=self._weightoriginal,
                             filter=self._filteroriginal,
                             defs=dict(self._defs),
                             attachment=dict(self._attachment))
        out._content = newcontent
        return out
Ejemplo n.º 3
0
    def rebin(self, axis, edges):
        """
        Reduce the number of bins by combining existing bins at a specified set of ``edges``.

        Parameters
        ----------
        axis : :py:class:`Axis <histbook.axis.Axis>`, algebraic expression (string), or index position (integer)
            the axis to rebin

        edges : iterable of numbers
            new bin edges; must be a subset of existing bin edges

        Returns
        -------
        :py:class:`Hist <histbook.hist.Hist>`
            a rebinned histogram.
        """
        if not isinstance(axis, histbook.axis.Axis):
            expr = histbook.expr.Expr.parse(axis, defs=self._defs)
            for x in self._group + self._fixed:
                if isinstance(x,
                              histbook.axis._RebinSplit) and expr == x._parsed:
                    axis = x
                    break

        for index, x in enumerate(self._group + self._fixed):
            if isinstance(x, histbook.axis._RebinSplit) and axis == x:
                axis = x
                break
        else:
            raise IndexError("no such rebinnable axis: {0}".format(axis))

        if isinstance(axis, histbook.axis.GroupAxis):
            newaxis, newcontent = axis._rebinsplit(edges, self._content, index)
        else:
            newaxis, newcontent = axis._rebinsplit(edges, self._content,
                                                   index - len(self._group))

        outaxis = [
            newaxis if i == index else x
            for i, x in enumerate(self._group + self._fixed + self._profile)
        ]
        out = self.__class__(*outaxis,
                             weight=self._weightoriginal,
                             filter=self._filteroriginal,
                             defs=dict(self._defs),
                             attachment=dict(self._attachment))
        out._content = newcontent
        return out
Ejemplo n.º 4
0
    def rebin(self, axis, edges):
        if not isinstance(axis, histbook.axis.Axis):
            expr = histbook.expr.Expr.parse(axis, defs=self._defs)
            for x in self._group + self._fixed:
                if isinstance(x, histbook.axis.RebinSplit) and expr == x._parsed:
                    axis = x
                    break

        for index, x in enumerate(self._group + self._fixed):
            if isinstance(x, histbook.axis.RebinSplit) and axis == x:
                axis = x
                break
        else:
            raise IndexError("no such rebinnable axis: {0}".format(axis))

        if isinstance(axis, histbook.axis.GroupAxis):
            newaxis, newcontent = axis._rebinsplit(edges, self._content, index)
        else:
            newaxis, newcontent = axis._rebinsplit(edges, self._content, index - len(self._group))

        outaxis = [newaxis if i == index else x for i, x in enumerate(self._group + self._fixed + self._profile)]
        out = self.__class__(*outaxis, weight=self._weight, defs=self._defs)
        out._content = newcontent
        return out