def __init__(self, quantity=identity, value=Count()):
        """Create a Categorize that is capable of being filled and added.

        Parameters:
            quantity (function returning float): computes the quantity of interest from the data.
            value (:doc:`Container <histogrammar.defs.Container>`): generates sub-aggregators to put in each bin.

        Other Parameters:
            entries (float): the number of entries, initially 0.0.
            bins (dict from str to :doc:`Container <histogrammar.defs.Container>`): the map, probably a hashmap, to
            fill with values when their `entries` become non-zero.
        """
        if value is not None and not isinstance(value, Container):
            raise TypeError(
                "value ({0}) must be None or a Container".format(value))
        self.entries = 0.0
        self.quantity = serializable(
            identity(quantity) if isinstance(quantity, str) else quantity)
        self.value = value
        self.bins = {}
        if value is not None:
            self.contentType = value.name
        else:
            self.contentType = "Count"
        super(Categorize, self).__init__()
        self.specialize()
示例#2
0
    def __init__(self, transform=identity):
        """Create a Count that is capable of being filled and added.

        Parameters:
            transform (function from float to float): transforms each weight.

        Other parameters:
            entries (float): the number of entries, initially 0.0.
        """
        self.entries = 0.0
        self.transform = serializable(transform)
        super(Count, self).__init__()
        self.specialize()
示例#3
0
    def __init__(self,
                 centers,
                 quantity=identity,
                 value=Count(),
                 nanflow=Count()):
        """Create a CentrallyBin that is capable of being filled and added.

        Parameters:
            centers (list of float): the centers of all bins
            quantity (function returning float): computes the quantity of interest from the data.
            value (:doc:`Container <histogrammar.defs.Container>`): generates sub-aggregators to put in each bin.
            nanflow (:doc:`Container <histogrammar.defs.Container>`): a sub-aggregator to use for data whose quantity
                is NaN.

        Other parameters:
            entries (float): the number of entries, initially 0.0.
            bins (list of float, :doc:`Container <histogrammar.defs.Container>` pairs): the bin centers and
                sub-aggregators in each bin.
        """

        if not isinstance(centers, (list, tuple)) and not all(
                isinstance(v, (list, tuple)) and len(v) == 2 and isinstance(
                    v[0], numbers.Real) and isinstance(v[1], Container)
                for v in centers):
            raise TypeError(
                "centers ({0}) must be a list of number, Container pairs".
                format(centers))
        if value is not None and not isinstance(value, Container):
            raise TypeError(
                "value ({0}) must be None or a Container".format(value))
        if not isinstance(nanflow, Container):
            raise TypeError(
                "nanflow ({0}) must be a Container".format(nanflow))
        if len(centers) < 2:
            raise ValueError(
                "number of centers ({0}) must be at least two".format(
                    len(centers)))

        self.entries = 0.0
        if value is None:
            self.bins = None
        else:
            self.bins = [(float(x), value.zero()) for x in sorted(centers)]

        self.quantity = serializable(
            identity(quantity) if isinstance(quantity, str) else quantity)
        self.value = value
        self.nanflow = nanflow.copy()

        super(CentrallyBin, self).__init__()
        self.specialize()
示例#4
0
    def __init__(self, quantity=identity):
        """Create a Sum that is capable of being filled and added.

        Parameters:
            quantity (function returning float): computes the quantity of interest from the data.

        Other parameters:
            entries (float): the number of entries, initially 0.0.
            sum (float): the running sum, initially 0.0.
        """
        self.quantity = serializable(identity(quantity) if isinstance(quantity, str) else quantity)
        self.entries = 0.0
        self.sum = 0.0
        super(Sum, self).__init__()
        self.specialize()
示例#5
0
    def __init__(self,
                 binWidth,
                 quantity=identity,
                 value=Count(),
                 nanflow=Count(),
                 origin=0.0):
        """Create a SparselyBin that is capable of being filled and added.

        Parameters:
            binWidth (float): the width of a bin; must be strictly greater than zero.
            quantity (function returning float): computes the quantity of interest from the data.
            value (:doc:`Container <histogrammar.defs.Container>`): generates sub-aggregators to put in each bin.
            nanflow (:doc:`Container <histogrammar.defs.Container>`): a sub-aggregator to use for data whose quantity
                is NaN.
            origin (float): the left edge of the bin whose index is 0.

        Other parameters:
            entries (float): the number of entries, initially 0.0.
            bins (dict from int to :doc:`Container <histogrammar.defs.Container>`): the map, probably a hashmap, to
                fill with values when their `entries` become non-zero.
        """
        if not isinstance(binWidth, numbers.Real):
            raise TypeError("binWidth ({0}) must be a number".format(binWidth))
        if value is not None and not isinstance(value, Container):
            raise TypeError("value ({0}) must be a Container".format(value))
        if not isinstance(nanflow, Container):
            raise TypeError(
                "nanflow ({0}) must be a Container".format(nanflow))
        if not isinstance(origin, numbers.Real):
            raise TypeError("origin ({0}) must be a number".format(origin))
        if binWidth <= 0.0:
            raise ValueError(
                "binWidth ({0}) must be greater than zero".format(binWidth))

        self.binWidth = float(binWidth)
        self.entries = 0.0
        self.quantity = serializable(
            identity(quantity) if isinstance(quantity, str) else quantity)
        self.value = value
        if value is not None:
            self.contentType = value.name
        else:
            self.contentType = "Count"
        self.bins = {}
        self.nanflow = nanflow.copy()
        self.origin = float(origin)
        super(SparselyBin, self).__init__()
        self.specialize()
示例#6
0
    def __init__(self, quantity=identity):
        """Create a Minimize that is capable of being filled and added.

        Parameters:
            quantity (function returning float): computes the quantity of interest from the data.

        Other parameters:
            entries (float): the number of entries, initially 0.0. #
            min (float): the lowest value of the quantity observed, initially NaN.
        """
        self.quantity = serializable(
            identity(quantity) if isinstance(quantity, str) else quantity)
        self.entries = 0.0
        self.min = float("nan")
        super(Minimize, self).__init__()
        self.specialize()
示例#7
0
    def __init__(self,
                 thresholds,
                 quantity=identity,
                 value=Count(),
                 nanflow=Count()):
        """Create a Stack that is capable of being filled and added.

        Parameters:
            thresholds (list of floats): specifies ``N`` cut thresholds, so the Stack will fill ``N + 1`` aggregators,
                each overlapping the last.
            quantity (function returning float): computes the quantity of interest from the data.
            value (:doc:`Container <histogrammar.defs.Container>`): generates sub-aggregators for each bin.
            nanflow (:doc:`Container <histogrammar.defs.Container>`): a sub-aggregator to use for data whose quantity
                is NaN.

        Other parameters:
            entries (float): the number of entries, initially 0.0.
            bins (list of float, :doc:`Container <histogrammar.defs.Container>` pairs): the ``N + 1`` thresholds and
                sub-aggregators. (The first threshold is minus infinity; the rest are the ones specified
                by ``thresholds``).
        """
        if not isinstance(thresholds, (list, tuple)) and not all(
                isinstance(v, (list, tuple)) and len(v) == 2 and isinstance(
                    v[0], numbers.Real) and isinstance(v[1], Container)
                for v in thresholds):
            raise TypeError(
                "thresholds ({0}) must be a list of number, Container pairs".
                format(thresholds))
        if value is not None and not isinstance(value, Container):
            raise TypeError(
                "value ({0}) must be None or a Container".format(value))
        if not isinstance(nanflow, Container):
            raise TypeError(
                "nanflow ({0}) must be a Container".format(nanflow))
        self.entries = 0.0
        self.quantity = serializable(
            identity(quantity) if isinstance(quantity, str) else quantity)
        if value is None:
            self.bins = tuple(thresholds)
        else:
            self.bins = tuple((float(x), value.zero())
                              for x in (float("-inf"), ) + tuple(thresholds))
        self.nanflow = nanflow.copy()
        super(Stack, self).__init__()
        self.specialize()
示例#8
0
    def __init__(self, quantity=identity, range="S"):
        """Create a Bag that is capable of being filled and added.

        Parameters:
            quantity (function returning a float, a tuple of floats, or a str): computes the quantity of interest from
            the data.

        Other parameters:
            entries (float): the number of entries, initially 0.0.
            values (dict from quantity return type to float): the number of entries for each unique item.
            range ("N", "N#" where "#" is a positive integer, or "S"): the data type: number, vector of numbers,
            or string. default is "S".
        """
        self.quantity = serializable(identity(quantity) if isinstance(quantity, str) else quantity)
        self.entries = 0.0
        self.values = {}
        self.range = range
        try:
            self.dimension = int(range[1:])
        except BaseException:
            self.dimension = 0
        super(Bag, self).__init__()
        self.specialize()
示例#9
0
    def __init__(self, quantity=identity, value=Count()):
        """Create a Fraction that is capable of being filled and added.

        Parameters:
            quantity (function returning bool or float): computes the quantity of interest from the data and interprets
                it as a selection (multiplicative factor on weight).
            value (:doc:`Container <histogrammar.defs.Container>`): generates sub-aggregators for the numerator and
                denominator.

        Other parameters:
            entries (float): the number of entries, initially 0.0.
            numerator (:doc:`Container <histogrammar.defs.Container>`): the sub-aggregator of entries that pass
                the selection.
            denominator (:doc:`Container <histogrammar.defs.Container>`): the sub-aggregator of all entries.
        """
        if value is not None and not isinstance(value, Container):
            raise TypeError("value ({0}) must be None or a Container".format(value))
        self.entries = 0.0
        self.quantity = serializable(identity(quantity) if isinstance(quantity, str) else quantity)
        if value is not None:
            self.numerator = value.zero()
            self.denominator = value.zero()
        super(Fraction, self).__init__()
        self.specialize()
示例#10
0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from histogrammar.defs import unweighted
from histogrammar.util import serializable
from histogrammar.primitives.bin import Bin
from histogrammar.primitives.count import Count

def Histogram(num, low, high, quantity, selection=unweighted):
    return Bin(num, low, high, quantity, selection, Count(), Count(), Count(), Count())

Histogram.ed = serializable(lambda low, high, entries, values, underflow, overflow, nanflow:
    Bin(len(values), low, high, None, None, None, underflow, overflow, nanflow))

class HistogramMethods(Bin):
    @property
    def name(self):
        return "Bin"

    @property
    def factory(self):
        return Bin

    @property
    def numericalValues(self):
        return [v.entries for v in self.values]

    @property