def testSparselyBin(self): one = SparselyBin(1.0, named("something", lambda x: x)) for _ in self.simple: one.fill(_) self.assertEqual([(i, v.entries) for i, v in sorted(one.bins.items())], [(-5, 1.0), (-3, 1.0), (-2, 2.0), (0, 2.0), (1, 1.0), (2, 1.0), (3, 1.0), (7, 1.0)]) self.assertEqual(one.numFilled, 8) self.assertEqual(one.num, 13) self.assertEqual(one.low, -5.0) self.assertEqual(one.high, 8.0) self.checkScaling(one) self.checkScaling(one.toImmutable()) self.checkJson(one) self.checkPickle(one) self.checkName(one) two = SparselyBin(1.0, named("something", lambda x: x), Sum(named("elsie", lambda x: x))) for _ in self.simple: two.fill(_) self.checkScaling(two) self.checkScaling(two.toImmutable()) self.checkJson(two) self.checkPickle(two) self.checkName(two)
def testMaximize(self): for i in xrange(11): left, right = self.simple[:i], self.simple[i:] leftMaximizing = Maximize(named("something", lambda x: x)) rightMaximizing = Maximize(named("something", lambda x: x)) for _ in left: leftMaximizing.fill(_) for _ in right: rightMaximizing.fill(_) if len(left) > 0: self.assertAlmostEqual(leftMaximizing.max, max(left)) else: self.assertTrue(math.isnan(leftMaximizing.max)) if len(right) > 0: self.assertAlmostEqual(rightMaximizing.max, max(right)) else: self.assertTrue(math.isnan(rightMaximizing.max)) finalResult = leftMaximizing + rightMaximizing self.assertAlmostEqual(finalResult.max, max(self.simple)) self.checkScaling(leftMaximizing) self.checkScaling(leftMaximizing.toImmutable()) self.checkJson(leftMaximizing) self.checkPickle(leftMaximizing) self.checkName(leftMaximizing)
def testBinWithSum(self): one = Bin(5, -3.0, 7.0, named("xaxis", lambda x: x), Sum(named("yaxis", lambda x: 10.0)), Sum(lambda x: 10.0), Sum(lambda x: 10.0), Sum(lambda x: 10.0)) for _ in self.simple: one.fill(_) self.assertEqual(list(map(lambda _: _.sum, one.values)), [30.0, 20.0, 20.0, 10.0, 0.0]) self.assertEqual(one.underflow.sum, 10.0) self.assertEqual(one.overflow.sum, 10.0) self.assertEqual(one.nanflow.sum, 0.0) two = Select( lambda x: x.bool, Bin(5, -3.0, 7.0, lambda x: x.double, Sum(lambda x: 10.0), Sum(lambda x: 10.0), Sum(lambda x: 10.0), Sum(lambda x: 10.0))) for _ in self.struct: two.fill(_) self.assertEqual(list(map(lambda _: _.sum, two.cut.values)), [20.0, 10.0, 10.0, 10.0, 0.0]) self.assertEqual(two.cut.underflow.sum, 0.0) self.assertEqual(two.cut.overflow.sum, 0.0) self.assertEqual(two.cut.nanflow.sum, 0.0) self.checkScaling(one) self.checkScaling(one.toImmutable()) self.checkJson(one) self.checkPickle(one) self.checkName(one) self.checkScaling(two) self.checkScaling(two.toImmutable()) self.checkJson(two) self.checkPickle(two) self.checkName(two)
def testCategorize(self): categorizing = Categorize(named("something", lambda x: x.string[0])) for _ in self.struct: categorizing.fill(_) self.assertEqual( dict((k, v.entries) for k, v in categorizing.binsMap.items()), { "n": 1.0, "e": 1.0, "t": 3.0, "s": 2.0, "f": 2.0, "o": 1.0 }) self.checkScaling(categorizing) self.checkScaling(categorizing.toImmutable()) self.checkJson(categorizing) self.checkPickle(categorizing) self.checkName(categorizing) categorizing2 = Categorize(named("something", lambda x: x.string[0]), Sum(named("elsie", lambda x: x.double))) for _ in self.struct: categorizing2.fill(_) self.checkScaling(categorizing2) self.checkScaling(categorizing2.toImmutable()) self.checkJson(categorizing2) self.checkPickle(categorizing2) self.checkName(categorizing2)
def testCountWithFilter(self): for i in xrange(11): left, right = self.simple[:i], self.simple[i:] leftCounting = Select(named("something", lambda x: x > 0.0), Count()) rightCounting = Select(named("something", lambda x: x > 0.0), Count()) for _ in left: leftCounting.fill(_) for _ in right: rightCounting.fill(_) self.assertEqual(leftCounting.cut.entries, len(list(filter(lambda x: x > 0.0, left)))) self.assertEqual(rightCounting.cut.entries, len(list(filter(lambda x: x > 0.0, right)))) finalResult = leftCounting + rightCounting self.assertEqual(finalResult.cut.entries, len(list(filter(lambda x: x > 0.0, self.simple)))) self.checkScaling(leftCounting) self.checkScaling(leftCounting.toImmutable()) self.checkJson(leftCounting) self.checkPickle(leftCounting) self.checkName(leftCounting)
def testFractionSum(self): fracking = Fraction(named("something", lambda x: x > 0.0), Sum(named("elsie", lambda x: x))) for _ in self.simple: fracking.fill(_) self.assertAlmostEqual(fracking.numerator.sum, 14.5) self.assertAlmostEqual(fracking.denominator.sum, 3.3) self.checkScaling(fracking) self.checkScaling(fracking.toImmutable()) self.checkJson(fracking) self.checkPickle(fracking) self.checkName(fracking)
def testIrregularlyBinSum(self): partitioning = IrregularlyBin([0.0, 2.0, 4.0, 6.0, 8.0], named("something", lambda x: x), Sum(named("elsie", lambda x: x))) for _ in self.simple: partitioning.fill(_) self.assertAlmostEqual(partitioning.bins[0][1].sum, -11.2) self.assertAlmostEqual(partitioning.bins[1][1].sum, 1.6) self.checkScaling(partitioning) self.checkScaling(partitioning.toImmutable()) self.checkJson(partitioning) self.checkPickle(partitioning) self.checkName(partitioning)
def testBin(self): one = Bin(5, -3.0, 7.0, named("xaxis", lambda x: x)) for _ in self.simple: one.fill(_) self.assertEqual(list(map(lambda _: _.entries, one.values)), [3.0, 2.0, 2.0, 1.0, 0.0]) self.assertEqual(one.underflow.entries, 1.0) self.assertEqual(one.overflow.entries, 1.0) self.assertEqual(one.nanflow.entries, 0.0) two = Select(lambda x: x.bool, Bin(5, -3.0, 7.0, lambda x: x.double)) for _ in self.struct: two.fill(_) self.assertEqual(list(map(lambda _: _.entries, two.cut.values)), [2.0, 1.0, 1.0, 1.0, 0.0]) self.assertEqual(two.cut.underflow.entries, 0.0) self.assertEqual(two.cut.overflow.entries, 0.0) self.assertEqual(two.cut.nanflow.entries, 0.0) self.checkScaling(one) self.checkScaling(one.toImmutable()) self.checkJson(one) self.checkPickle(one) self.checkName(one) self.checkScaling(two) self.checkScaling(two.toImmutable()) self.checkJson(two) self.checkPickle(two) self.checkName(two)
def testBag(self): one = Bag(named("something", lambda x: x), "N") for _ in self.simple: one.fill(_) self.assertEqual( one.values, { 7.3: 1.0, 2.2: 1.0, -1.7: 1.0, -4.7: 1.0, 0.0: 2.0, -1.8: 1.0, -3.0: 1.0, 1.6: 1.0, 3.4: 1.0 }) two = Bag(lambda x: (x, x), "N2") for _ in self.simple: two.fill(_) self.assertEqual( two.values, { (7.3, 7.3): 1.0, (2.2, 2.2): 1.0, (-1.7, -1.7): 1.0, (-4.7, -4.7): 1.0, (0.0, 0.0): 2.0, (-1.8, -1.8): 1.0, (-3.0, -3.0): 1.0, (1.6, 1.6): 1.0, (3.4, 3.4): 1.0 }) three = Bag(lambda x: x.string[0], "S") for _ in self.struct: three.fill(_) self.assertEqual(three.values, { "n": 1.0, "e": 1.0, "t": 3.0, "s": 2.0, "f": 2.0, "o": 1.0 }) self.checkScaling(one) self.checkScaling(one.toImmutable()) self.checkJson(one) self.checkPickle(one) self.checkName(one) self.checkScaling(two) self.checkScaling(two.toImmutable()) self.checkJson(two) self.checkPickle(two) self.checkName(two) self.checkScaling(three) self.checkScaling(three.toImmutable()) self.checkJson(three) self.checkPickle(three) self.checkName(three)
def testStackWithSum(self): stacking = Stack([0.0, 2.0, 4.0, 6.0, 8.0], named("something", lambda x: x), Sum(named("elsie", lambda x: x))) for _ in self.simple: stacking.fill(_) self.assertEqual([(k, v.entries) for k, v in stacking.bins], [(float("-inf"), 10.0), (0.0, 6.0), (2.0, 3.0), (4.0, 1.0), (6.0, 1.0), (8.0, 0.0)]) self.checkScaling(stacking) self.checkScaling(stacking.toImmutable()) self.checkJson(stacking) self.checkPickle(stacking) self.checkName(stacking)
def testFraction(self): fracking = Fraction(named("something", lambda x: x > 0.0), Count()) for _ in self.simple: fracking.fill(_) self.assertEqual(fracking.numerator.entries, 4.0) self.assertEqual(fracking.denominator.entries, 10.0) self.checkScaling(fracking) self.checkScaling(fracking.toImmutable()) self.checkJson(fracking) self.checkPickle(fracking) self.checkName(fracking)
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)
def testSum(self): for i in xrange(11): left, right = self.simple[:i], self.simple[i:] leftSumming = Sum(named("something", lambda x: x)) rightSumming = Sum(named("something", lambda x: x)) for _ in left: leftSumming.fill(_) for _ in right: rightSumming.fill(_) self.assertAlmostEqual(leftSumming.sum, sum(left)) self.assertAlmostEqual(rightSumming.sum, sum(right)) finalResult = leftSumming + rightSumming self.assertAlmostEqual(finalResult.sum, sum(self.simple)) self.checkScaling(leftSumming) self.checkScaling(leftSumming.toImmutable()) self.checkJson(leftSumming) self.checkPickle(leftSumming) self.checkName(leftSumming)
def testIrregularlyBin(self): partitioning = IrregularlyBin([0.0, 2.0, 4.0, 6.0, 8.0], named("something", lambda x: x), Count()) for _ in self.simple: partitioning.fill(_) self.assertEqual([(k, v.entries) for k, v in partitioning.bins], [(float("-inf"), 4.0), (0.0, 3.0), (2.0, 2.0), (4.0, 0.0), (6.0, 1.0), (8.0, 0.0)]) self.checkScaling(partitioning) self.checkScaling(partitioning.toImmutable()) self.checkJson(partitioning) self.checkPickle(partitioning) self.checkName(partitioning)
def testCentrallyBin(self): one = CentrallyBin([-3.0, -1.0, 0.0, 1.0, 3.0, 10.0], named("something", lambda x: x)) self.assertEqual(one.center(1.5), 1.0) self.assertEqual(one.neighbors(1.0), (0.0, 3.0)) self.assertEqual(one.neighbors(10.0), (3.0, None)) self.assertEqual(one.range(-3.0), (float("-inf"), -2.0)) self.assertEqual(one.range(-1.0), (-2.0, -0.5)) self.assertEqual(one.range(0.0), (-0.5, 0.5)) self.assertEqual(one.range(10.0), (6.5, float("inf"))) for _ in self.simple: one.fill(_) self.assertEqual([(c, v.entries) for c, v in one.bins], [(-3.0, 2.0), (-1.0, 2.0), (0.0, 2.0), (1.0, 1.0), (3.0, 2.0), (10.0, 1.0)]) self.checkScaling(one) self.checkScaling(one.toImmutable()) self.checkJson(one) self.checkPickle(one) self.checkName(one) two = CentrallyBin([-3.0, -1.0, 0.0, 1.0, 3.0, 10.0], named("something", lambda x: x), Sum(named("elsie", lambda x: x))) self.checkScaling(two) self.checkScaling(two.toImmutable()) self.checkJson(two) self.checkPickle(two) self.checkName(two)
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)