def testSumWithFilterStringFunctions(self): for i in xrange(11): left, right = self.struct[:i], self.struct[i:] leftSumming = Select("not bool", Sum("double + 1")) rightSumming = Select("not bool", Sum("double + 1")) for _ in left: leftSumming.fill(_) for _ in right: rightSumming.fill(_) self.assertAlmostEqual( leftSumming.cut.sum, sum(_.double + 1 for _ in left if not _.bool)) self.assertAlmostEqual( rightSumming.cut.sum, sum(_.double + 1 for _ in right if not _.bool)) finalResult = leftSumming + rightSumming self.assertAlmostEqual( finalResult.cut.sum, sum(_.double + 1 for _ in self.struct if not _.bool)) self.checkScaling(leftSumming) self.checkScaling(leftSumming.toImmutable()) self.checkJson(leftSumming) self.checkPickle(leftSumming) self.checkName(leftSumming)
def testSumWithWeightingFactorStringFunctions(self): for i in xrange(11): left, right = self.struct[:i], self.struct[i:] leftSumming = Select("int", Sum("double * 2")) rightSumming = Select("int", Sum("double * 2")) for _ in left: leftSumming.fill(_) for _ in right: rightSumming.fill(_) self.assertAlmostEqual( leftSumming.cut.sum, sum(_.double * 2 * _.int for _ in left if _.int > 0)) self.assertAlmostEqual( rightSumming.cut.sum, sum(_.double * 2 * _.int for _ in right if _.int > 0)) finalResult = leftSumming + rightSumming self.assertAlmostEqual( finalResult.cut.sum, sum(_.double * 2 * _.int for _ in self.struct if _.int > 0)) self.checkScaling(leftSumming) self.checkScaling(leftSumming.toImmutable()) self.checkJson(leftSumming) self.checkPickle(leftSumming) self.checkName(leftSumming)
def testSumWithFilter(self): for i in xrange(11): left, right = self.struct[:i], self.struct[i:] leftSumming = Select(lambda x: x.bool, Sum(lambda x: x.double)) rightSumming = Select(lambda x: x.bool, Sum(lambda x: x.double)) for _ in left: leftSumming.fill(_) for _ in right: rightSumming.fill(_) self.assertAlmostEqual(leftSumming.cut.sum, sum(_.double for _ in left if _.bool)) self.assertAlmostEqual(rightSumming.cut.sum, sum(_.double for _ in right if _.bool)) finalResult = leftSumming + rightSumming self.assertAlmostEqual( finalResult.cut.sum, sum(_.double for _ in self.struct if _.bool)) self.checkScaling(leftSumming) self.checkScaling(leftSumming.toImmutable()) self.checkJson(leftSumming) self.checkPickle(leftSumming) self.checkName(leftSumming)
def testSumStringFunctions(self): for i in xrange(11): left, right = self.simple[:i], self.simple[i:] leftSumming = Sum("_ + 1") rightSumming = Sum("datum + 1") for _ in left: leftSumming.fill(_) for _ in right: rightSumming.fill(_) self.assertAlmostEqual(leftSumming.sum, sum(left) + len(left)) self.assertAlmostEqual(rightSumming.sum, sum(right) + len(right)) finalResult = leftSumming + rightSumming self.assertAlmostEqual(finalResult.sum, sum(self.simple) + len(self.simple)) self.checkScaling(leftSumming) self.checkScaling(leftSumming.toImmutable()) self.checkJson(leftSumming) self.checkPickle(leftSumming) self.checkName(leftSumming)
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 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 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 testSum(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("Sum no data", Sum(lambda x: x["empty"]), self.data, Sum(lambda x: x), self.empty) self.compare("Sum noholes", Sum(lambda x: x["noholes"]), self.data, Sum(lambda x: x), self.noholes) self.compare("Sum holes", Sum(lambda x: x["withholes"]), self.data, Sum(lambda x: x), self.withholes)
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 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 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 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)
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)
class TestNumpy(unittest.TestCase): def runTest(self): self.testSum() self.testAverage() self.testDeviate() self.testMinimize() self.testMaximize() self.testBin() self.testBinTrans() self.testBinAverage() self.testBinDeviate() self.testSparselyBin() self.testSparselyBinTrans() self.testSparselyBinAverage() self.testSparselyBinDeviate() self.testCentrallyBin() self.testCentrallyBinTrans() self.testCentrallyBinAverage() self.testCentrallyBinDeviate() self.testCategorize() self.testCategorizeTrans() self.testFractionBin() self.testStackBin() self.testIrregularlyBinBin() self.testSelectBin() self.testLabelBin() self.testUntypedLabelBin() self.testIndexBin() self.testBranchBin() self.testBag() SIZE = 10000 HOLES = 100 data = makeSamples(SIZE, HOLES) empty = data["empty"] positive = data["positive"] boolean = data["boolean"] noholes = data["noholes"] withholes = data["withholes"] withholes2 = data["withholes2"] def twosigfigs(self, number): return round(number, 1 - int(math.floor(math.log10(number)))) def compare(self, name, hnp, npdata, hpy, pydata): import numpy npdata2 = npdata.copy() hnp2 = hnp.copy() hnp3 = hnp.copy() hpy2 = hpy.copy() hpy3 = hpy.copy() startTime = time.time() hnp.fill.numpy(npdata) numpyTime = time.time() - startTime if pydata.dtype != numpy.unicode_: for key in npdata: diff = (npdata[key] != npdata2[key]) & numpy.bitwise_not( numpy.isnan(npdata[key])) & numpy.bitwise_not(numpy.isnan(npdata2[key])) if numpy.any(diff): raise AssertionError("npdata has been modified:\n{0}\n{1}\n{2}\n{3} vs {4}".format(npdata[key], npdata2[key], numpy.nonzero( diff), npdata[key][numpy.nonzero(diff)[0][0]], npdata2[key][numpy.nonzero(diff)[0][0]])) hnp2.fill.numpy(npdata) hnp3.fill.numpy(npdata) hnp3.fill.numpy(npdata) assert (hnp + hnp2) == hnp3 assert (hnp2 + hnp) == hnp3 assert (hnp + hnp.zero()) == hnp2 assert (hnp.zero() + hnp) == hnp2 startTime = time.time() for d in pydata: if isinstance(d, numpy.unicode_): d = str(d) else: d = float(d) hpy.fill(d) pyTime = time.time() - startTime for h in [hpy2, hpy3, hpy3]: for d in pydata: if isinstance(d, numpy.unicode_): d = str(d) else: d = float(d) h.fill(d) assert (hpy + hpy) == hpy3 assert (hpy + hpy2) == hpy3 assert (hpy2 + hpy) == hpy3 assert (hpy + hpy.zero()) == hpy2 assert (hpy.zero() + hpy) == hpy2 hnpj = json.dumps(hnp.toJson()) hpyj = json.dumps(hpy.toJson()) if Factory.fromJson(hnp.toJson()) != Factory.fromJson(hpy.toJson()): raise AssertionError("\n numpy: {0}\npython: {1}".format(hnpj, hpyj)) else: sys.stderr.write("{0:45s} | numpy: {1:.3f}ms python: {2:.3f}ms = {3:g}X speedup\n".format( name, numpyTime*1000, pyTime*1000, self.twosigfigs(pyTime/numpyTime))) assert Factory.fromJson((hnp + hnp2).toJson()) == Factory.fromJson((hpy + hpy2).toJson()) assert Factory.fromJson(hnp3.toJson()) == Factory.fromJson(hpy3.toJson()) # Warmup: apparently, Numpy does some dynamic optimization that needs to warm up... if empty is not None: Sum(lambda x: x["empty"]).fill.numpy(data) Sum(lambda x: x["empty"]).fill.numpy(data) Sum(lambda x: x["empty"]).fill.numpy(data) Sum(lambda x: x["empty"]).fill.numpy(data) Sum(lambda x: x["empty"]).fill.numpy(data) def testSum(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("Sum no data", Sum(lambda x: x["empty"]), self.data, Sum(lambda x: x), self.empty) self.compare("Sum noholes", Sum(lambda x: x["noholes"]), self.data, Sum(lambda x: x), self.noholes) self.compare("Sum holes", Sum(lambda x: x["withholes"]), self.data, Sum(lambda x: x), self.withholes) def testAverage(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("Average no data", Average(lambda x: x["empty"]), self.data, Average(lambda x: x), self.empty) self.compare("Average noholes", Average( lambda x: x["noholes"]), self.data, Average(lambda x: x), self.noholes) self.compare("Average holes", Average(lambda x: x["withholes"]), self.data, Average(lambda x: x), self.withholes) 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) def testMinimize(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("Minimize no data", Minimize( lambda x: x["empty"]), self.data, Minimize(lambda x: x), self.empty) self.compare("Minimize noholes", Minimize( lambda x: x["noholes"]), self.data, Minimize(lambda x: x), self.noholes) self.compare("Minimize holes", Minimize( lambda x: x["withholes"]), self.data, Minimize(lambda x: x), self.withholes) def testMaximize(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("Maximize no data", Maximize( lambda x: x["empty"]), self.data, Maximize(lambda x: x), self.empty) self.compare("Maximize noholes", Maximize( lambda x: x["noholes"]), self.data, Maximize(lambda x: x), self.noholes) self.compare("Maximize holes", Maximize( lambda x: x["withholes"]), self.data, Maximize(lambda x: x), self.withholes) def testBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") for bins in [10, 100]: self.compare("Bin ({0} bins) no data".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["empty"]), self.data, Bin(bins, -3.0, 3.0, lambda x: x), self.empty) self.compare("Bin ({0} bins) noholes".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["noholes"]), self.data, Bin(bins, -3.0, 3.0, lambda x: x), self.noholes) self.compare("Bin ({0} bins) holes".format(bins), Bin( bins, -3.0, 3.0, lambda x: x["withholes"]), self.data, Bin(bins, -3.0, 3.0, lambda x: x), self.withholes) def testBinTrans(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") for bins in [10, 100]: self.compare("BinTrans ({0} bins) no data".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["empty"], Count( lambda x: 0.5*x)), self.data, Bin(bins, -3.0, 3.0, lambda x: x, Count(lambda x: 0.5*x)), self.empty) self.compare("BinTrans ({0} bins) noholes".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["noholes"], Count( lambda x: 0.5*x)), self.data, Bin(bins, -3.0, 3.0, lambda x: x, Count(lambda x: 0.5*x)), self.noholes) self.compare("BinTrans ({0} bins) holes".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["withholes"], Count( lambda x: 0.5*x)), self.data, Bin(bins, -3.0, 3.0, lambda x: x, Count(lambda x: 0.5*x)), self.withholes) def testBinAverage(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") for bins in [10, 100]: self.compare("BinAverage ({0} bins) no data".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["empty"], Average( lambda x: x["empty"])), self.data, Bin(bins, -3.0, 3.0, lambda x: x, Average(lambda x: x)), self.empty) self.compare("BinAverage ({0} bins) noholes".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["noholes"], Average( lambda x: x["noholes"])), self.data, Bin(bins, -3.0, 3.0, lambda x: x, Average(lambda x: x)), self.noholes) self.compare("BinAverage ({0} bins) holes".format(bins), Bin(bins, -3.0, 3.0, lambda x: x["withholes"], Average( lambda x: x["withholes"])), self.data, Bin(bins, -3.0, 3.0, lambda x: x, Average(lambda x: x)), self.withholes) 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) def testSparselyBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("SparselyBin no data", SparselyBin( 0.1, lambda x: x["empty"]), self.data, SparselyBin(0.1, lambda x: x), self.empty) self.compare("SparselyBin noholes", SparselyBin( 0.1, lambda x: x["noholes"]), self.data, SparselyBin(0.1, lambda x: x), self.noholes) self.compare("SparselyBin holes", SparselyBin( 0.1, lambda x: x["withholes"]), self.data, SparselyBin(0.1, lambda x: x), self.withholes) def testSparselyBinTrans(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("SparselyBinTrans no data", SparselyBin(0.1, lambda x: x["empty"], Count( lambda x: 0.5*x)), self.data, SparselyBin(0.1, lambda x: x, Count(lambda x: 0.5*x)), self.empty) self.compare("SparselyBinTrans noholes", SparselyBin(0.1, lambda x: x["noholes"], Count( lambda x: 0.5*x)), self.data, SparselyBin(0.1, lambda x: x, Count(lambda x: 0.5*x)), self.noholes) self.compare("SparselyBinTrans holes", SparselyBin(0.1, lambda x: x["withholes"], Count( lambda x: 0.5*x)), self.data, SparselyBin(0.1, lambda x: x, Count(lambda x: 0.5*x)), self.withholes) def testSparselyBinAverage(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("SparselyBinAverage no data", SparselyBin(0.1, lambda x: x["empty"], Average( lambda x: x["empty"])), self.data, SparselyBin(0.1, lambda x: x, Average(lambda x: x)), self.empty) self.compare("SparselyBinAverage noholes", SparselyBin(0.1, lambda x: x["noholes"], Average( lambda x: x["noholes"])), self.data, SparselyBin(0.1, lambda x: x, Average(lambda x: x)), self.noholes) self.compare("SparselyBinAverage holes", SparselyBin(0.1, lambda x: x["withholes"], Average( lambda x: x["withholes"])), self.data, SparselyBin(0.1, lambda x: x, Average(lambda x: x)), self.withholes) 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) def testCentrallyBin(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("CentrallyBin no data", CentrallyBin( centers, lambda x: x["empty"]), self.data, CentrallyBin(centers, lambda x: x), self.empty) self.compare("CentrallyBin noholes", CentrallyBin( centers, lambda x: x["noholes"]), self.data, CentrallyBin(centers, lambda x: x), self.noholes) self.compare("CentrallyBin holes", CentrallyBin( centers, lambda x: x["withholes"]), self.data, CentrallyBin(centers, lambda x: x), self.withholes) def testCentrallyBinTrans(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("CentrallyBinTrans no data", CentrallyBin(centers, lambda x: x["empty"], Count( lambda x: 0.5*x)), self.data, CentrallyBin(centers, lambda x: x, Count(lambda x: 0.5*x)), self.empty) self.compare("CentrallyBinTrans noholes", CentrallyBin(centers, lambda x: x["noholes"], Count( lambda x: 0.5*x)), self.data, CentrallyBin(centers, lambda x: x, Count(lambda x: 0.5*x)), self.noholes) self.compare("CentrallyBinTrans holes", CentrallyBin(centers, lambda x: x["withholes"], Count( lambda x: 0.5*x)), self.data, CentrallyBin(centers, lambda x: x, Count(lambda x: 0.5*x)), self.withholes) def testCentrallyBinAverage(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("CentrallyBinAverage no data", CentrallyBin(centers, lambda x: x["empty"], Average( lambda x: x["empty"])), self.data, CentrallyBin(centers, lambda x: x, Average(lambda x: x)), self.empty) self.compare("CentrallyBinAverage noholes", CentrallyBin(centers, lambda x: x["noholes"], Average( lambda x: x["noholes"])), self.data, CentrallyBin(centers, lambda x: x, Average(lambda x: x)), self.noholes) self.compare("CentrallyBinAverage holes", CentrallyBin(centers, lambda x: x["withholes"], Average( lambda x: x["withholes"])), self.data, CentrallyBin(centers, lambda x: x, Average(lambda x: x)), self.withholes) 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) def testCategorize(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("Categorize no data", Categorize(lambda x: numpy.array(numpy.floor( x["empty"]), dtype="<U5")), self.data, Categorize(lambda x: x), numpy.array(numpy.floor(self.empty), dtype="<U5")) self.compare("Categorize noholes", Categorize(lambda x: numpy.array(numpy.floor( x["noholes"]), dtype="<U5")), self.data, Categorize(lambda x: x), numpy.array(numpy.floor(self.noholes), dtype="<U5")) self.compare("Categorize holes", Categorize(lambda x: numpy.array(numpy.floor( x["withholes"]), dtype="<U5")), self.data, Categorize(lambda x: x), numpy.array(numpy.floor(self.withholes), dtype="<U5")) def testCategorizeTrans(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("CategorizeTrans no data", Categorize(lambda x: numpy.array(numpy.floor(x["empty"]), dtype="<U5"), Count( lambda x: 0.5*x)), self.data, Categorize(lambda x: x, Count(lambda x: 0.5*x)), numpy.array(numpy.floor(self.empty), dtype="<U5")) self.compare("CategorizeTrans noholes", Categorize(lambda x: numpy.array(numpy.floor(x["noholes"]), dtype="<U5"), Count( lambda x: 0.5*x)), self.data, Categorize(lambda x: x, Count(lambda x: 0.5*x)), numpy.array(numpy.floor(self.noholes), dtype="<U5")) self.compare("CategorizeTrans holes", Categorize(lambda x: numpy.array(numpy.floor(x["withholes"]), dtype="<U5"), Count( lambda x: 0.5*x)), self.data, Categorize(lambda x: x, Count(lambda x: 0.5*x)), numpy.array(numpy.floor(self.withholes), dtype="<U5")) def testFractionBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("FractionBin no data", Fraction(lambda x: x["empty"], Bin( 100, -3.0, 3.0, lambda x: x["empty"])), self.data, Fraction(lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.empty) self.compare("FractionBin noholes", Fraction(lambda x: x["noholes"], Bin( 100, -3.0, 3.0, lambda x: x["noholes"])), self.data, Fraction(lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.noholes) self.compare("FractionBin holes", Fraction(lambda x: x["withholes"], Bin( 100, -3.0, 3.0, lambda x: x["withholes"])), self.data, Fraction(lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.withholes) def testStackBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") cuts = [-3.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 3.0] self.compare("StackBin no data", Stack(cuts, lambda x: x["empty"], Bin( 100, -3.0, 3.0, lambda x: x["empty"])), self.data, Stack(cuts, lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.empty) self.compare("StackBin noholes", Stack(cuts, lambda x: x["noholes"], Bin( 100, -3.0, 3.0, lambda x: x["noholes"])), self.data, Stack(cuts, lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.noholes) self.compare("StackBin holes", Stack(cuts, lambda x: x["withholes"], Bin( 100, -3.0, 3.0, lambda x: x["withholes"])), self.data, Stack(cuts, lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.withholes) def testIrregularlyBinBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") cuts = [-3.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 3.0] self.compare("IrregularlyBinBin no data", IrregularlyBin(cuts, lambda x: x["empty"], Bin( 100, -3.0, 3.0, lambda x: x["empty"])), self.data, IrregularlyBin(cuts, lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.empty) self.compare("IrregularlyBinBin noholes", IrregularlyBin(cuts, lambda x: x["noholes"], Bin( 100, -3.0, 3.0, lambda x: x["noholes"])), self.data, IrregularlyBin(cuts, lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.noholes) self.compare("IrregularlyBinBin holes", IrregularlyBin(cuts, lambda x: x["withholes"], Bin( 100, -3.0, 3.0, lambda x: x["withholes"])), self.data, IrregularlyBin(cuts, lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.withholes) def testSelectBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("SelectBin no data", Select(lambda x: x["empty"], Bin( 100, -3.0, 3.0, lambda x: x["empty"])), self.data, Select(lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.empty) self.compare("SelectBin noholes", Select(lambda x: x["noholes"], Bin( 100, -3.0, 3.0, lambda x: x["noholes"])), self.data, Select(lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.noholes) self.compare("SelectBin holes", Select(lambda x: x["withholes"], Bin( 100, -3.0, 3.0, lambda x: x["withholes"])), self.data, Select(lambda x: x, Bin(100, -3.0, 3.0, lambda x: x)), self.withholes) def testLabelBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("LabelBin no data", Label( x=Bin(100, -3.0, 3.0, lambda x: x["empty"])), self.data, Label(x=Bin(100, -3.0, 3.0, lambda x: x)), self.empty) self.compare("LabelBin noholes", Label( x=Bin(100, -3.0, 3.0, lambda x: x["noholes"])), self.data, Label(x=Bin(100, -3.0, 3.0, lambda x: x)), self.noholes) self.compare("LabelBin holes", Label( x=Bin(100, -3.0, 3.0, lambda x: x["withholes"])), self.data, Label(x=Bin(100, -3.0, 3.0, lambda x: x)), self.withholes) def testUntypedLabelBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("UntypedLabelBin no data", UntypedLabel( x=Bin(100, -3.0, 3.0, lambda x: x["empty"])), self.data, UntypedLabel(x=Bin(100, -3.0, 3.0, lambda x: x)), self.empty) self.compare("UntypedLabelBin noholes", UntypedLabel( x=Bin(100, -3.0, 3.0, lambda x: x["noholes"])), self.data, UntypedLabel(x=Bin(100, -3.0, 3.0, lambda x: x)), self.noholes) self.compare("UntypedLabelBin holes", UntypedLabel(x=Bin( 100, -3.0, 3.0, lambda x: x["withholes"])), self.data, UntypedLabel(x=Bin(100, -3.0, 3.0, lambda x: x)), self.withholes) def testIndexBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("IndexBin no data", Index( Bin(100, -3.0, 3.0, lambda x: x["empty"])), self.data, Index(Bin(100, -3.0, 3.0, lambda x: x)), self.empty) self.compare("IndexBin noholes", Index( Bin(100, -3.0, 3.0, lambda x: x["noholes"])), self.data, Index(Bin(100, -3.0, 3.0, lambda x: x)), self.noholes) self.compare("IndexBin holes", Index( Bin(100, -3.0, 3.0, lambda x: x["withholes"])), self.data, Index(Bin(100, -3.0, 3.0, lambda x: x)), self.withholes) def testBranchBin(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("BranchBin no data", Branch( Bin(100, -3.0, 3.0, lambda x: x["empty"])), self.data, Branch(Bin(100, -3.0, 3.0, lambda x: x)), self.empty) self.compare("BranchBin noholes", Branch( Bin(100, -3.0, 3.0, lambda x: x["noholes"])), self.data, Branch(Bin(100, -3.0, 3.0, lambda x: x)), self.noholes) self.compare("BranchBin holes", Branch( Bin(100, -3.0, 3.0, lambda x: x["withholes"])), self.data, Branch(Bin(100, -3.0, 3.0, lambda x: x)), self.withholes) def testBag(self): with Numpy() as numpy: if numpy is None: return sys.stderr.write("\n") self.compare("Bag no data", Bag(lambda x: x["empty"], "N"), self.data, Bag(lambda x: x, "N"), self.empty) self.compare("Bag noholes", Bag(lambda x: x["noholes"], "N"), self.data, Bag(lambda x: x, "N"), self.noholes) self.compare("Bag holes", Bag(lambda x: x["withholes"], "N"), self.data, Bag(lambda x: x, "N"), self.withholes)