def test_add_2d_w(self): for uoflow in (False, True): h = histogram(integer(-1, 2, uoflow=uoflow), regular(4, -2, 2, uoflow=uoflow)) h.fill(-1, -2) h.fill(-1, -1) h.fill(0, 0) h.fill(0, 1) h.fill(1, 0) h.fill(3, -1) h.fill(0, -3) m = [[1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] h2 = histogram(integer(-1, 2, uoflow=uoflow), regular(4, -2, 2, uoflow=uoflow)) h2.fill(0, 0, weight=0) h2 += h h2 += h h += h self.assertEqual(h, h2) for i in range(-uoflow, len(h.axis(0)) + uoflow): for j in range(-uoflow, len(h.axis(1)) + uoflow): self.assertEqual(h.value(i, j), 2 * m[i][j]) self.assertEqual(h.variance(i, j), 2 * m[i][j])
def test_init(self): histogram() histogram(integer(-1, 1)) with self.assertRaises(TypeError): histogram(1) with self.assertRaises(TypeError): histogram("bla") with self.assertRaises(TypeError): histogram([]) with self.assertRaises(TypeError): histogram(regular) with self.assertRaises(TypeError): histogram(regular()) with self.assertRaises(TypeError): histogram([integer(-1, 1)]) with self.assertRaises(RuntimeError): histogram(integer(-1, 1), unknown_keyword="nh") h = histogram(integer(-1, 2)) self.assertEqual(h.dim, 1) self.assertEqual(h.axis(0), integer(-1, 2)) self.assertEqual(h.axis(0).shape, 5) self.assertEqual(histogram(integer(-1, 2, uoflow=False)).axis(0).shape, 3) self.assertNotEqual(h, histogram(regular(1, -1, 1))) self.assertNotEqual(h, histogram(integer(-1, 2))) self.assertNotEqual(h, histogram(integer(-1, 1, label="ia")))
def compare_3d(n, distrib): if distrib == 0: r = np.random.rand(n) else: r = 0.3 * np.random.randn(n) r = r.reshape(3, n / 3) best_numpy = float("infinity") best_boost = float("infinity") for k in xrange(50): t = timer() w, xe = np.histogramdd(r.T, bins=(100, 100, 100), range=((0.0, 1.0), (0.0, 1.0), (0.0, 1.0))) t = timer() - t best_numpy = min(t, best_numpy) h = histogram(regular(100, 0, 1), regular(100, 0, 1), regular(100, 0, 1)) t = timer() h.fill(r[0], r[1], r[2]) t = timer() - t best_boost = min(t, best_boost) assert (np.all(w == np.array(h)[:-2, :-2, :-2])) print "py:numpy %.3f" % best_numpy print "py:hd_sd %.3f" % best_boost
def test_pickle_0(self): a = histogram(category(0, 1, 2), integer(0, 20, label='ia'), regular(20, 0.0, 20.0, uoflow=False), variable(0.0, 1.0, 2.0), circular(4, label='pa')) for i in range(len(a.axis(0))): a.fill(i, 0, 0, 0, 0) for j in range(len(a.axis(1))): a.fill(i, j, 0, 0, 0) for k in range(len(a.axis(2))): a.fill(i, j, k, 0, 0) for l in range(len(a.axis(3))): a.fill(i, j, k, l, 0) for m in range(len(a.axis(4))): a.fill(i, j, k, l, m * 0.5 * pi) io = BytesIO() pickle.dump(a, io) io.seek(0) b = pickle.load(io) self.assertNotEqual(id(a), id(b)) self.assertEqual(a.dim, b.dim) self.assertEqual(a.axis(0), b.axis(0)) self.assertEqual(a.axis(1), b.axis(1)) self.assertEqual(a.axis(2), b.axis(2)) self.assertEqual(a.axis(3), b.axis(3)) self.assertEqual(a.axis(4), b.axis(4)) self.assertEqual(a.sum, b.sum) self.assertEqual(a, b)
def test_fill_with_numpy_array_0(self): ar = lambda *args: numpy.array(args, dtype=float) a = histogram(integer(0, 3, uoflow=False)) a.fill(ar(-1, 0, 1, 2, 1)) a.fill((4, -1, 0, 1, 2)) self.assertEqual(a.value(0), 2) self.assertEqual(a.value(1), 3) self.assertEqual(a.value(2), 2) with self.assertRaises(ValueError): a.fill(numpy.empty((2, 2))) with self.assertRaises(ValueError): a.fill(numpy.empty(2), 1) with self.assertRaises(ValueError): a.fill("abc") a = histogram(integer(0, 2, uoflow=False), regular(2, 0, 2, uoflow=False)) a.fill(ar(-1, 0, 1), ar(-1., 1., 0.1)) self.assertEqual(a.value(0, 0), 0) self.assertEqual(a.value(0, 1), 1) self.assertEqual(a.value(1, 0), 1) self.assertEqual(a.value(1, 1), 0) with self.assertRaises(ValueError): a.fill(ar(1, 2, 3)) a = histogram(integer(0, 3, uoflow=False)) a.fill(ar(0, 0, 1, 2, 1, 0, 2, 2)) self.assertEqual(a.value(0), 3) self.assertEqual(a.value(1), 2) self.assertEqual(a.value(2), 3)
def test_pickle_1(self): a = histogram(category(0, 1, 2), integer(0, 3, label='ia'), regular(4, 0.0, 4.0, uoflow=False), variable(0.0, 1.0, 2.0)) for i in range(len(a.axis(0))): a.fill(i, 0, 0, 0, weight=3) for j in range(len(a.axis(1))): a.fill(i, j, 0, 0, weight=10) for k in range(len(a.axis(2))): a.fill(i, j, k, 0, weight=2) for l in range(len(a.axis(3))): a.fill(i, j, k, l, weight=5) io = BytesIO() pickle.dump(a, io) io.seek(0) b = pickle.load(io) self.assertNotEqual(id(a), id(b)) self.assertEqual(a.dim, b.dim) self.assertEqual(a.axis(0), b.axis(0)) self.assertEqual(a.axis(1), b.axis(1)) self.assertEqual(a.axis(2), b.axis(2)) self.assertEqual(a.axis(3), b.axis(3)) self.assertEqual(a.sum, b.sum) self.assertEqual(a, b)
def test_getitem(self): v = [1.0, 1.25, 1.5, 1.75, 2.0] a = regular(4, 1.0, 2.0) for i in range(4): self.assertAlmostEqual(a[i][0], v[i]) self.assertAlmostEqual(a[i][1], v[i+1]) self.assertEqual(a[-1][0], -float("infinity")) self.assertEqual(a[4][1], float("infinity"))
def test_overflow(self): h = histogram(*[regular(1, 0, 1) for i in range(50)]) with self.assertRaises(RuntimeError): h.fill(*range(50)) with self.assertRaises(RuntimeError): h.value(*range(50)) with self.assertRaises(RuntimeError): h.variance(*range(50))
def test(self): axes = (regular(10, 0, 1), integer(0, 1)) h = histogram(*axes) for i, a in enumerate(axes): self.assertEqual(h.axis(i), a) with self.assertRaises(IndexError): h.axis(2) self.assertEqual(h.axis(-1), axes[-1]) self.assertEqual(h.axis(-2), axes[-2]) with self.assertRaises(IndexError): h.axis(-3)
def test_index(self): a = regular(4, 1.0, 2.0) self.assertEqual(a.index(-1), -1) self.assertEqual(a.index(0.99), -1) self.assertEqual(a.index(1.0), 0) self.assertEqual(a.index(1.249), 0) self.assertEqual(a.index(1.250), 1) self.assertEqual(a.index(1.499), 1) self.assertEqual(a.index(1.500), 2) self.assertEqual(a.index(1.749), 2) self.assertEqual(a.index(1.750), 3) self.assertEqual(a.index(1.999), 3) self.assertEqual(a.index(2.000), 4) self.assertEqual(a.index(20), 4)
def test_numpy_conversion_6(self): a = integer(0, 2) b = regular(2, 0, 2) c = variable(0, 1, 2) ref = numpy.array((0., 1., 2.)) self.assertTrue(numpy.all(numpy.array(a) == ref)) self.assertTrue(numpy.all(numpy.array(b) == ref)) self.assertTrue(numpy.all(numpy.array(c) == ref)) d = circular(4) ref = numpy.array((0., 0.5*pi, pi, 1.5*pi, 2.0*pi)) self.assertTrue(numpy.all(numpy.array(d) == ref)) e = category(1, 2) ref = numpy.array((1, 2)) self.assertTrue(numpy.all(numpy.array(e) == ref))
def test_out_of_range(self): h = histogram(regular(3, 0, 1)) h.fill(-1) h.fill(2) self.assertEqual(h.value(-1), 1) self.assertEqual(h.value(3), 1) with self.assertRaises(IndexError): h.value(-2) with self.assertRaises(IndexError): h.value(4) with self.assertRaises(IndexError): h.variance(-2) with self.assertRaises(IndexError): h.variance(4)
def test_fill_with_numpy_array_1(self): ar = lambda *args: numpy.array(args, dtype=float) a = histogram(integer(0, 3, uoflow=True)) v = ar(-1, 0, 1, 2, 3, 4) w = ar( 2, 3, 4, 5, 6, 7) a.fill(v, weight=w) a.fill((0, 1), weight=(2, 3)) self.assertEqual(a.value(-1), 2) self.assertEqual(a.value(0), 5) self.assertEqual(a.value(1), 7) self.assertEqual(a.value(2), 5) self.assertEqual(a.variance(-1), 4) self.assertEqual(a.variance(0), 13) self.assertEqual(a.variance(1), 25) self.assertEqual(a.variance(2), 25) a.fill((1, 2), weight=1) a.fill(0, weight=(1, 2)) self.assertEqual(a.value(0), 8) self.assertEqual(a.value(1), 8) self.assertEqual(a.value(2), 6) with self.assertRaises(RuntimeError): a.fill((1, 2), foo=(1, 1)) with self.assertRaises(ValueError): a.fill((1, 2), weight=(1,)) with self.assertRaises(ValueError): a.fill((1, 2), weight="ab") with self.assertRaises(RuntimeError): a.fill((1, 2), weight=(1, 1), foo=1) with self.assertRaises(ValueError): a.fill((1, 2), weight=([1, 1], [2, 2])) a = histogram(integer(0, 2, uoflow=False), regular(2, 0, 2, uoflow=False)) a.fill((-1, 0, 1), (-1, 1, 0.1)) self.assertEqual(a.value(0, 0), 0) self.assertEqual(a.value(0, 1), 1) self.assertEqual(a.value(1, 0), 1) self.assertEqual(a.value(1, 1), 0) a = histogram(integer(0, 3, uoflow=False)) a.fill((0, 0, 1, 2)) a.fill((1, 0, 2, 2)) self.assertEqual(a.value(0), 3) self.assertEqual(a.value(1), 2) self.assertEqual(a.value(2), 3)
def test_operators(self): h = histogram(integer(0, 2)) h.fill(0) h += h self.assertEqual(h.value(0), 2) self.assertEqual(h.variance(0), 2) self.assertEqual(h.value(1), 0) h *= 2 self.assertEqual(h.value(0), 4) self.assertEqual(h.variance(0), 8) self.assertEqual(h.value(1), 0) self.assertEqual((h + h).value(0), (2 * h).value(0)) self.assertEqual((h + h).value(0), (h * 2).value(0)) self.assertNotEqual((h + h).variance(0), (2 * h).variance(0)) self.assertNotEqual((h + h).variance(0), (h * 2).variance(0)) h2 = histogram(regular(2, 0, 2)) with self.assertRaises(RuntimeError): h + h2
def compare_1d(n, distrib): if distrib == 0: r = np.random.rand(n) else: r = 0.5 + 0.3 * np.random.randn(n) best_numpy = float("infinity") best_boost = float("infinity") for k in xrange(50): t = timer() w, xe = np.histogram(r, bins=100, range=(0.0, 1.0)) t = timer() - t best_numpy = min(t, best_numpy) h = histogram(regular(100, 0, 1)) t = timer() h.fill(r) t = timer() - t best_boost = min(t, best_boost) assert (np.all(w == np.array(h)[:-2])) print "py:numpy %.3f" % best_numpy print "py:hd_sd %.3f" % best_boost
def test_fill_2d(self): for uoflow in (False, True): h = histogram(integer(-1, 2, uoflow=uoflow), regular(4, -2, 2, uoflow=uoflow)) h.fill(-1, -2) h.fill(-1, -1) h.fill(0, 0) h.fill(0, 1) h.fill(1, 0) h.fill(3, -1) h.fill(0, -3) with self.assertRaises(Exception): h.fill(1) with self.assertRaises(Exception): h.fill(1, 2, 3) m = [[1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] for i in range(-uoflow, len(h.axis(0)) + uoflow): for j in range(-uoflow, len(h.axis(1)) + uoflow): self.assertEqual(h.value(i, j), m[i][j])
def test_iter(self): v = [1.0, 1.25, 1.5, 1.75, 2.0] a = regular(4, 1.0, 2.0) self.assertAlmostEqual([x[0] for x in a], v[:-1]) self.assertAlmostEqual([x[1] for x in a], v[1:])
def test_repr(self): h = histogram(regular(10, 0, 1), integer(0, 1)) h2 = eval(repr(h)) self.assertEqual(h, h2)
def test_len(self): a = regular(4, 1.0, 2.0) self.assertEqual(len(a), 4)
def test_init(self): regular(1, 1.0, 2.0) regular(1, 1.0, 2.0, label="ra") regular(1, 1.0, 2.0, uoflow=False) regular(1, 1.0, 2.0, label="ra", uoflow=False) regular_log(1, 1.0, 2.0) regular_sqrt(1, 1.0, 2.0) regular_cos(1, 0.5, 1.0) regular_pow(1, 1.0, 2.0, 1.5) with self.assertRaises(TypeError): regular() with self.assertRaises(TypeError): regular(1) with self.assertRaises(TypeError): regular(1, 1.0) with self.assertRaises(RuntimeError): regular(0, 1.0, 2.0) with self.assertRaises(TypeError): regular("1", 1.0, 2.0) with self.assertRaises(Exception): regular(-1, 1.0, 2.0) with self.assertRaises(RuntimeError): regular(1, 2.0, 1.0) with self.assertRaises(TypeError): regular(1, 1.0, 2.0, label=0) with self.assertRaises(TypeError): regular(1, 1.0, 2.0, label="ra", uoflow="True") with self.assertRaises(TypeError): regular(1, 1.0, 2.0, bad_keyword="ra") with self.assertRaises(TypeError): regular_pow(1, 1.0, 2.0) a = regular(4, 1.0, 2.0) self.assertEqual(a, regular(4, 1.0, 2.0)) self.assertNotEqual(a, regular(3, 1.0, 2.0)) self.assertNotEqual(a, regular(4, 1.1, 2.0)) self.assertNotEqual(a, regular(4, 1.0, 2.1))
def test_add_2d_bad(self): a = histogram(integer(-1, 1)) b = histogram(regular(3, -1, 1)) with self.assertRaises(RuntimeError): a += b