def test_usage_bh(): h = bhc.histogram(bhc.axis.regular(10, 0, 1), bhc.axis.str_category(["one", "two"])) assert h.axis(0) == bhc.axis.regular(10, 0, 1) assert h.axis(1) == bhc.axis.str_category(["one", "two"]) h(0, "one") assert h.at(0, 0) == 1.0
def test_slice_and_rebin(): from boost_histogram.cpp.algorithm import reduce, slice_and_rebin h = bhc.histogram(bhc.axis.regular(5, 0, 5)) np.asarray(h)[:] = 1 hs = reduce(h, slice_and_rebin(0, 1, 3, 2)) assert hs.axis(0) == bhc.axis.regular(1, 1, 3) assert_array_equal(hs, [2]) hs2 = reduce(h, slice_and_rebin(1, 3, 2)) assert hs == hs2
def test_convert_bh(): h = bhc.histogram( bh.Histogram(bh.axis.Regular(10, 0, 1), bh.axis.StrCategory(["one", "two"]))) assert hasattr(h, "axis") assert not hasattr(h, "axes") h = bh.Histogram(h) # Current warning workaround. Enable when removed: # assert not hasattr(h, "axis") assert hasattr(h, "axes")
def test_rebin(): from boost_histogram.cpp.algorithm import reduce, rebin h = bhc.histogram(bhc.axis.regular(4, 1, 5)) np.asarray(h)[:] = 1 assert_array_equal(h, [1, 1, 1, 1]) hs = reduce(h, rebin(0, 4)) assert hs.axis(0) == bhc.axis.regular(1, 1, 5) assert_array_equal(hs, [4]) hs2 = reduce(h, rebin(4)) assert hs == hs2
def test_str(): # Mixing bh/bhc is fine; histogram correctly always returns matching axis, # axis returns matching transform, etc. h = bhc.histogram(bh.axis.Regular(4, 0, 4)) assert repr(str(h)) == repr( """ +--------------------------------------------------------------+ [-inf, 0) 0 | | [ 0, 1) 0 | | [ 1, 2) 0 | | [ 2, 3) 0 | | [ 3, 4) 0 | | [ 4, inf) 0 | | +--------------------------------------------------------------+""" )
def test_crop(): from boost_histogram.cpp.algorithm import reduce, crop h = bhc.histogram(bhc.axis.regular(4, 1, 5)) np.asarray(h)[:] = 1 hs = reduce(h, crop(0, 2, 3)) assert hs.axis(0) == bhc.axis.regular(1, 2, 3) assert_array_equal(hs, [1]) assert hs.at(-1) == 0 assert hs.at(1) == 0 hs2 = reduce(h, crop(2, 3)) assert hs == hs2
def test_slice(mode): from boost_histogram.cpp.algorithm import reduce, slice h = bhc.histogram(bhc.axis.regular(4, 1, 5)) np.asarray(h)[:] = 1 assert_array_equal(h, [1, 1, 1, 1]) hs = reduce(h, slice(0, 1, 2, mode=mode)) assert hs.axis(0) == bhc.axis.regular(1, 2, 3) assert_array_equal(hs, [1]) assert hs.at(-1) == (1 if mode == slice_mode.shrink else 0) assert hs.at(1) == (2 if mode == slice_mode.shrink else 0) hs2 = reduce(h, slice(1, 2, mode=mode)) assert hs == hs2
def test_shrink_rebin_1d(): h = bhc.histogram(bhc.axis.regular(20, 0, 4)) h.fill(1.1) hs = bhc.algorithm.reduce(h, bhc.algorithm.shrink_and_rebin(0, 1, 3, 2)) assert_array_equal(hs, [1, 0, 0, 0, 0])
def test_storage_repr(): h = bhc.histogram(bhc.axis.regular(10, 0, 1)) assert repr(h._storage_type()) == "double()" assert repr( h._storage_type) == "<class 'boost_histogram.cpp.storage.double'>"
def test_repr(): h = bhc.histogram(bh.axis.Regular(4, 0, 4)) assert (repr(h) == """histogram( regular(4, 0, 4, metadata="None", options=underflow | overflow), storage=double())""")