Пример #1
0
def test_root_th1f_convert() -> None:
    th = ROOT.TH1F("h1", "h1", 50, -2.5, 2.5)
    th.FillRandom("gaus", 10000)
    h = ensure_plottable_histogram(th)
    assert all(th.GetBinContent(i + 1) == approx(iv) for i, iv in enumerate(h.values()))
    assert all(
        th.GetBinError(i + 1) == approx(ie)
        for i, ie in enumerate(np.sqrt(h.variances()))
    )
Пример #2
0
def get_count_edges(hist):
    """Get bin contents and edges from a compatible histogram."""

    # Make sure we have a PlottableProtocol histogram
    hist = ensure_plottable_histogram(hist)

    # Use the PlottableProtocol
    count = hist.values()
    edges = get_plottable_protocol_bin_edges(hist.axes[0])

    return count, edges
Пример #3
0
def test_from_numpy() -> None:
    hist1 = ((1, 2, 3, 4, 1, 2), (0, 1, 2, 3))

    h = ensure_plottable_histogram(hist1)

    assert h.values() == approx(np.array(hist1[0]))
    assert len(h.axes) == 1
    assert len(h.axes[0]) == 3
    assert h.axes[0][0] == (0, 1)
    assert h.axes[0][1] == (1, 2)
    assert h.axes[0][2] == (2, 3)
Пример #4
0
def test_root_th2f_convert() -> None:
    th = ROOT.TH2F("h2", "h2", 50, -2.5, 2.5, 50, -2.5, 2.5)
    _ = ROOT.TF2("xyg", "xygaus", -2.5, 2.5, -2.5, 2.5)
    th.FillRandom("xyg", 10000)
    h = ensure_plottable_histogram(th)
    assert all(
        th.GetBinContent(i + 1, j + 1) == approx(iv)
        for i, row in enumerate(h.values())
        for j, iv in enumerate(row)
    )
    assert all(
        th.GetBinError(i + 1, j + 1) == approx(ie)
        for i, row in enumerate(np.sqrt(h.variances()))
        for j, ie in enumerate(row)
    )
Пример #5
0
def test_from_bh_str_cat() -> None:
    bh = pytest.importorskip("boost_histogram")
    h1 = bh.Histogram(bh.axis.StrCategory(["hi", "ho"]))
    h1.fill(["hi", "hi", "hi", "ho"])

    h = ensure_plottable_histogram(h1)

    assert h is h1

    assert h.values() == approx(np.array((3, 1)))
    assert len(h.axes) == 1
    assert len(h.axes[0]) == 2

    assert h.axes[0][0] == "hi"
    assert h.axes[0][1] == "ho"
Пример #6
0
def test_from_bh_integer() -> None:
    bh = pytest.importorskip("boost_histogram")
    h1 = bh.Histogram(bh.axis.Integer(1, 6))
    h1[...] = (3, 2, 1, 2, 3)

    h = ensure_plottable_histogram(h1)

    assert h is h1

    assert h.values() == approx(np.array((3, 2, 1, 2, 3)))
    assert len(h.axes) == 1
    assert len(h.axes[0]) == 5
    assert h.axes[0][0] == 1
    assert h.axes[0][1] == 2
    assert h.axes[0][2] == 3
Пример #7
0
def test_from_numpy_2d() -> None:
    np.random.seed(42)
    x = np.random.normal(1, 2, 1000)
    y = np.random.normal(-1, 1, 1000)
    result = np.histogram2d(x, y)

    h = ensure_plottable_histogram(result)

    assert h.values() == approx(result[0])
    assert len(h.axes) == 2
    assert len(h.axes[0]) == 10
    assert h.axes[0][0] == approx(result[1][0:2])
    assert h.axes[0][1] == approx(result[1][1:3])
    assert h.axes[1][0] == approx(result[2][0:2])
    assert h.axes[1][1] == approx(result[2][1:3])