Пример #1
0
 def testDeviate(self):
     with Numpy() as numpy:
         if numpy is None:
             return
         sys.stderr.write("\n")
         self.compare("Deviate no data", Deviate(lambda x: x["empty"]), self.data, Deviate(lambda x: x), self.empty)
         self.compare("Deviate noholes", Deviate(
             lambda x: x["noholes"]), self.data, Deviate(lambda x: x), self.noholes)
         self.compare("Deviate holes", Deviate(lambda x: x["withholes"]),
                      self.data, Deviate(lambda x: x), self.withholes)
Пример #2
0
 def testSparselyBinDeviate(self):
     with Numpy() as numpy:
         if numpy is None:
             return
         sys.stderr.write("\n")
         self.compare("SparselyBinDeviate no data", SparselyBin(0.1, lambda x: x["empty"], Deviate(
             lambda x: x["empty"])), self.data, SparselyBin(0.1, lambda x: x, Deviate(lambda x: x)), self.empty)
         self.compare("SparselyBinDeviate noholes", SparselyBin(0.1, lambda x: x["noholes"], Deviate(
             lambda x: x["noholes"])), self.data, SparselyBin(0.1, lambda x: x, Deviate(lambda x: x)), self.noholes)
         self.compare("SparselyBinDeviate holes", SparselyBin(0.1, lambda x: x["withholes"], Deviate(
             lambda x: x["withholes"])), self.data, SparselyBin(0.1, lambda x: x, Deviate(lambda x: x)), self.withholes)
Пример #3
0
    def testDeviateWithWeightingFactor(self):
        for i in xrange(11):
            left, right = self.struct[:i], self.struct[i:]

            leftDeviating = Select(lambda x: x.int,
                                   Deviate(lambda x: x.double))
            rightDeviating = Select(lambda x: x.int,
                                    Deviate(lambda x: x.double))

            for _ in left:
                leftDeviating.fill(_)
            for _ in right:
                rightDeviating.fill(_)

            if sum(map(lambda _: _.int if _.int > 0.0 else 0.0, left)) == 0.0:
                self.assertTrue(math.isnan(leftDeviating.cut.mean))
                self.assertTrue(math.isnan(leftDeviating.cut.variance))
            else:
                self.assertAlmostEqual(
                    leftDeviating.cut.mean,
                    self.meanWeighted(list(map(lambda _: _.double, left)),
                                      list(map(lambda _: _.int, left))))
                self.assertAlmostEqual(
                    leftDeviating.cut.variance,
                    self.varianceWeighted(list(map(lambda _: _.double, left)),
                                          list(map(lambda _: _.int, left))))

            if sum(map(lambda _: _.int if _.int > 0.0 else 0.0, right)) == 0.0:
                self.assertTrue(math.isnan(rightDeviating.cut.mean))
                self.assertTrue(math.isnan(rightDeviating.cut.variance))
            else:
                self.assertAlmostEqual(
                    rightDeviating.cut.mean,
                    self.meanWeighted(list(map(lambda _: _.double, right)),
                                      list(map(lambda _: _.int, right))))
                self.assertAlmostEqual(
                    rightDeviating.cut.variance,
                    self.varianceWeighted(list(map(lambda _: _.double, right)),
                                          list(map(lambda _: _.int, right))))

            finalResult = leftDeviating + rightDeviating

            self.assertAlmostEqual(
                finalResult.cut.variance,
                self.varianceWeighted(
                    list(map(lambda _: _.double, self.struct)),
                    list(map(lambda _: _.int, self.struct))))

            self.checkScaling(leftDeviating)
            self.checkScaling(leftDeviating.toImmutable())
            self.checkJson(leftDeviating)
            self.checkPickle(leftDeviating)
            self.checkName(leftDeviating)
Пример #4
0
    def testBranch(self):
        one = Histogram(5, -3.0, 7.0, lambda x: x)
        two = Count()
        three = Deviate(lambda x: x + 100.0)

        branching = Branch(one, two, three)

        for _ in self.simple:
            branching.fill(_)

        self.assertEqual(branching.i0.numericalValues,
                         [3.0, 2.0, 2.0, 1.0, 0.0])
        self.assertEqual(branching.i0.numericalUnderflow, 1.0)
        self.assertEqual(branching.i0.numericalOverflow, 1.0)
        self.assertEqual(branching.i0.numericalNanflow, 0.0)

        self.assertEqual(branching.i1.entries, 10.0)

        self.assertAlmostEqual(branching.i2.entries, 10.0)
        self.assertAlmostEqual(branching.i2.mean, 100.33)
        self.assertAlmostEqual(branching.i2.variance, 10.8381)

        self.checkScaling(branching)
        self.checkScaling(branching.toImmutable())
        self.checkJson(branching)
        self.checkPickle(branching)
        self.checkName(branching)
Пример #5
0
    def testDeviateWithFilter(self):
        for i in xrange(11):
            left, right = self.struct[:i], self.struct[i:]

            leftDeviating = Select(lambda x: x.bool,
                                   Deviate(lambda x: x.double))
            rightDeviating = Select(lambda x: x.bool,
                                    Deviate(lambda x: x.double))

            for _ in left:
                leftDeviating.fill(_)
            for _ in right:
                rightDeviating.fill(_)

            if len([_.double for _ in left if _.bool]) == 0:
                self.assertTrue(math.isnan(leftDeviating.cut.mean))
                self.assertTrue(math.isnan(leftDeviating.cut.variance))
            else:
                self.assertAlmostEqual(
                    leftDeviating.cut.mean,
                    self.mean([_.double for _ in left if _.bool]))
                self.assertAlmostEqual(
                    leftDeviating.cut.variance,
                    self.variance([_.double for _ in left if _.bool]))

            if len([_.double for _ in right if _.bool]) == 0:
                self.assertTrue(math.isnan(rightDeviating.cut.mean))
                self.assertTrue(math.isnan(rightDeviating.cut.variance))
            else:
                self.assertAlmostEqual(
                    rightDeviating.cut.mean,
                    self.mean([_.double for _ in right if _.bool]))
                self.assertAlmostEqual(
                    rightDeviating.cut.variance,
                    self.variance([_.double for _ in right if _.bool]))

            finalResult = leftDeviating + rightDeviating

            self.assertAlmostEqual(
                finalResult.cut.variance,
                self.variance([_.double for _ in self.struct if _.bool]))

            self.checkScaling(leftDeviating)
            self.checkScaling(leftDeviating.toImmutable())
            self.checkJson(leftDeviating)
            self.checkPickle(leftDeviating)
            self.checkName(leftDeviating)
Пример #6
0
def SparselyProfileErr(binWidth,
                       binnedQuantity,
                       averagedQuantity,
                       selection=unweighted,
                       origin=0.0):
    """Convenience function for creating a physicist's sparsely binned "profile plot," which is a Profile with variances."""
    return Select.ing(
        selection,
        SparselyBin.ing(binWidth, binnedQuantity,
                        Deviate.ing(averagedQuantity), Count.ing(), origin))
Пример #7
0
def ProfileErr(num,
               low,
               high,
               binnedQuantity,
               averagedQuantity,
               selection=unweighted):
    """Convenience function for creating a physicist's "profile plot," which is a Profile with variances."""
    return Select.ing(
        selection,
        Bin.ing(num, low, high, binnedQuantity, Deviate.ing(averagedQuantity)))
Пример #8
0
    def testDeviate(self):
        for i in xrange(11):
            left, right = self.simple[:i], self.simple[i:]

            leftDeviating = Deviate(named("something", lambda x: x))
            rightDeviating = Deviate(named("something", lambda x: x))

            for _ in left:
                leftDeviating.fill(_)
            for _ in right:
                rightDeviating.fill(_)

            if len(left) == 0:
                self.assertTrue(math.isnan(leftDeviating.mean))
                self.assertTrue(math.isnan(leftDeviating.variance))
            else:
                self.assertAlmostEqual(leftDeviating.mean, self.mean(left))
                self.assertAlmostEqual(leftDeviating.variance,
                                       self.variance(left))

            if len(right) == 0:
                self.assertTrue(math.isnan(rightDeviating.mean))
                self.assertTrue(math.isnan(rightDeviating.variance))
            else:
                self.assertAlmostEqual(rightDeviating.mean, self.mean(right))
                self.assertAlmostEqual(rightDeviating.variance,
                                       self.variance(right))

            finalResult = leftDeviating + rightDeviating

            self.assertAlmostEqual(finalResult.variance,
                                   self.variance(self.simple))

            self.checkScaling(leftDeviating)
            self.checkScaling(leftDeviating.toImmutable())
            self.checkJson(leftDeviating)
            self.checkPickle(leftDeviating)
            self.checkName(leftDeviating)
Пример #9
0
    def testUntypedLabelMultipleTypes(self):
        one = Histogram(5, -3.0, 7.0, lambda x: x)
        two = Sum(lambda x: 1.0)
        three = Deviate(named("something", lambda x: x + 100.0))

        mapping = UntypedLabel(one=one, two=two, three=three)

        for _ in self.simple:
            mapping.fill(_)

        self.assertEqual(
            mapping("one").numericalValues, [3.0, 2.0, 2.0, 1.0, 0.0])
        self.assertEqual(mapping("two").sum, 10.0)
        self.assertAlmostEqual(mapping("three").entries, 10.0)
        self.assertAlmostEqual(mapping("three").mean, 100.33)
        self.assertAlmostEqual(mapping("three").variance, 10.8381)

        self.checkScaling(mapping)
        self.checkScaling(mapping.toImmutable())
        self.checkJson(mapping)
        self.checkPickle(mapping)
        self.checkName(mapping)
Пример #10
0
def ProfileErr(num, low, high, binnedQuantity, averagedQuantity):
    """Convenience function for creating a profile plot

    This is a Profile with variances.
    """
    return Bin.ing(num, low, high, binnedQuantity, Deviate.ing(averagedQuantity))
Пример #11
0
def SparselyProfileErr(binWidth, binnedQuantity, averagedQuantity, origin=0.0):
    """Convenience function for creating a sparsely binned profile plot

    This is a Profile with variances.
    """
    return SparselyBin.ing(binWidth, binnedQuantity, Deviate.ing(averagedQuantity), Count.ing(), origin)
def SparselyProfileErr(binWidth, binnedQuantity, averagedQuantity, selection=unweighted, origin=0.0):
    """Convenience function for creating a physicist's sparsely binned "profile plot," which is a Profile with variances."""
    return Select.ing(selection,
        SparselyBin.ing(binWidth, binnedQuantity,
            Deviate.ing(averagedQuantity), Count.ing(), origin))
def ProfileErr(num, low, high, binnedQuantity, averagedQuantity, selection=unweighted):
    """Convenience function for creating a physicist's "profile plot," which is a Profile with variances."""
    return Select.ing(selection,
        Bin.ing(num, low, high, binnedQuantity,
            Deviate.ing(averagedQuantity)))
Пример #14
0
 def testCentrallyBinDeviate(self):
     with Numpy() as numpy:
         if numpy is None:
             return
         sys.stderr.write("\n")
         centers = [-3.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 3.0]
         self.compare("CentrallyBinDeviate no data", CentrallyBin(centers, lambda x: x["empty"], Deviate(
             lambda x: x["empty"])), self.data, CentrallyBin(centers, lambda x: x, Deviate(lambda x: x)), self.empty)
         self.compare("CentrallyBinDeviate noholes", CentrallyBin(centers, lambda x: x["noholes"], Deviate(
             lambda x: x["noholes"])), self.data, CentrallyBin(centers, lambda x: x, Deviate(lambda x: x)), self.noholes)
         self.compare("CentrallyBinDeviate holes", CentrallyBin(centers, lambda x: x["withholes"], Deviate(
             lambda x: x["withholes"])), self.data, CentrallyBin(centers, lambda x: x, Deviate(lambda x: x)), self.withholes)
Пример #15
0
 def testBinDeviate(self):
     with Numpy() as numpy:
         if numpy is None:
             return
         sys.stderr.write("\n")
         for bins in [10, 100]:
             self.compare("BinDeviate ({0} bins) no data".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["empty"], Deviate(
                 lambda x: x["empty"])), self.data, Bin(bins, -3.0, 3.0, lambda x: x, Deviate(lambda x: x)), self.empty)
             self.compare("BinDeviate ({0} bins) noholes".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["noholes"], Deviate(
                 lambda x: x["noholes"])), self.data, Bin(bins, -3.0, 3.0, lambda x: x, Deviate(lambda x: x)), self.noholes)
             self.compare("BinDeviate ({0} bins) holes".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["withholes"], Deviate(
                 lambda x: x["withholes"])), self.data, Bin(bins, -3.0, 3.0, lambda x: x, Deviate(lambda x: x)), self.withholes)