コード例 #1
0
ファイル: test_named.py プロジェクト: scikit-hep/hist
def test_named_access():
    """
    Test named access -- whether NamedHist bins can be accessed.
    """

    h = NamedHist(axis.Regular(10, -5, 5, name="X", label="x [units]")).fill(
        X=np.random.normal(size=1000)
    )

    assert h[6] == h[bh.loc(1)] == h[1j] == h[0j + 1] == h[-3j + 4] == h[bh.loc(1, 0)]
    h[6] = h[bh.loc(1)] = h[1j] = h[0j + 1] = h[-3j + 4] = h[bh.loc(1, 0)] = 0

    h = NamedHist(
        axis.Regular(50, -5, 5, name="Norm", label="normal distribution"),
        axis.Regular(50, -5, 5, name="Unif", label="uniform distribution"),
        axis.StrCategory(["hi", "hello"], name="Greet"),
        axis.Boolean(name="Yes"),
        axis.Integer(0, 1000, name="Int"),
    ).fill(
        Norm=np.random.normal(size=1000),
        Unif=np.random.uniform(size=1000),
        Greet=["hi"] * 800 + ["hello"] * 200,
        Yes=[True] * 600 + [False] * 400,
        Int=np.ones(1000),
    )

    assert h[0j, -0j + 2, "hi", True, 1]

    # mismatch dimension
    with pytest.raises(Exception):
        h[0j, -0j + 2, "hi", True]
コード例 #2
0
ファイル: test_named.py プロジェクト: scikit-hep/hist
def test_named_density():
    """
    Test named density -- whether NamedHist density work properly.
    """

    for data in range(10, 20, 10):
        h = NamedHist(axis.Regular(10, -3, 3, name="x")).fill(x=np.random.randn(data))
        assert pytest.approx(sum(h.density()), 2) == pytest.approx(10 / 6, 2)
コード例 #3
0
def test_basic_usage():
    h = NamedHist(axis.Regular(
        10, 0, 1,
        name="x"))  # NamedHist should require axis.Regular to have a name set

    h.fill([0.35, 0.35, 0.45])  # Fill should be keyword only, with the names

    # Optional if you want these to fail
    assert h[2] == 0
    assert h[3] == 2
    assert h[4] == 1
    assert h[5] == 0
コード例 #4
0
ファイル: test_named.py プロジェクト: scikit-hep/hist
def test_named_axestuple():
    """
    Test named axes tuple -- whether NamedHist axes tuple work properly.
    """

    h = NamedHist(
        axis.Regular(20, 0, 12, name="A"),
        axis.Regular(10, 1, 3, name="B", label="Beta"),
        axis.Regular(15, 3, 5, name="C"),
        axis.Regular(5, 3, 2, name="D", label="Axis 3"),
    )

    assert h.axes.name == ("A", "B", "C", "D")
    assert h.axes.label == ("A", "Beta", "C", "Axis 3")

    assert h.axes[0].size == 20
    assert h.axes["A"].size == 20

    assert h.axes[1].size == 10
    assert h.axes["B"].size == 10

    assert h.axes[2].size == 15

    assert h.axes[:2].size == (20, 10)
    assert h.axes["A":"B"].size == (20,)
    assert h.axes[:"B"].size == (20,)
    assert h.axes["B":].size == (10, 15, 5)
コード例 #5
0
ファイル: test_plot.py プロジェクト: cranmer/hist
def test_ratiolike_str_alias(str_alias, use_likelihood):
    """
    Test str alias for callable in plot_ratio and plot_pull
    """

    np.random.seed(42)

    h = NamedHist(
        axis.Regular(50,
                     -4,
                     4,
                     name="S",
                     label="s [units]",
                     underflow=False,
                     overflow=False)).fill(S=np.random.normal(size=10))

    assert h.plot_ratio(str_alias, likelihood=use_likelihood)
    assert h.plot_pull(str_alias, likelihood=use_likelihood)
コード例 #6
0
def test_basic_usage():
    '''
        Test basic usage -- whether NamedHist are properly derived from\
        boost-histogram and whether it can be filled by names.
    '''

    # Test normal NamedHist
    h = NamedHist(axis.Regular(10, 0, 1, name="x"))
    h.fill(x=[0.35, 0.35, 0.45])

    assert h[2] == 0
    assert h[3] == 2
    assert h[4] == 1
    assert h[5] == 0

    assert h[{0: 2}] == 0
    assert h[{0: 3}] == 2
    assert h[{0: 4}] == 1
    assert h[{0: 5}] == 0

    assert h[{'x': 2}] == 0
    assert h[{'x': 3}] == 2
    assert h[{'x': 4}] == 1
    assert h[{'x': 5}] == 0

    # Test multi-axis NamedHist
    h = NamedHist(axis.Regular(10, 0, 1, name="x"),
                  axis.Regular(10, 0, 1, name="y"), axis.Integer(0,
                                                                 2,
                                                                 name="z"))

    h.fill(x=[0.35, 0.35, 0.35, 0.45, 0.55, 0.55, 0.55],
           y=[0.35, 0.35, 0.45, 0.45, 0.45, 0.45, 0.45],
           z=[0, 0, 1, 1, 1, 1, 1])

    z_one_only = h[{'z': bh.loc(1)}]
    assert z_one_only[{'x': 3, 'y': 4}] == 1
    assert z_one_only[{'x': 4, 'y': 4}] == 1
    assert z_one_only[{'x': 5, 'y': 4}] == 3
    assert z_one_only[{'x': 5, 'y': 5}] == 0
    assert z_one_only[{'x': 6, 'y': 5}] == 0

    assert z_one_only[3, 4] == 1
    assert z_one_only[4, 4] == 1
    assert z_one_only[5, 4] == 3
    assert z_one_only[5, 5] == 0
    assert z_one_only[6, 5] == 0
コード例 #7
0
ファイル: test_named.py プロジェクト: scikit-hep/hist
def test_named_hist_proxy():
    """
    Test named hist proxy -- whether NamedHist hist proxy works properly.
    """

    h = NamedHist.new.Reg(10, 0, 1, name="x").Double().fill(x=[0.5, 0.5])
    assert h[0.5j] == 2

    assert type(h) is NamedHist

    with pytest.raises(AttributeError):
        NamedHist().new

    h = (
        NamedHist.new.Reg(10, 0, 1, name="x")
        .Reg(10, 0, 1, name="y")
        .Double()
        .fill(x=[0.5, 0.5], y=[0.2, 0.6])
    )

    assert h[0.5j, 0.2j] == 1
    assert h[bh.loc(0.5), bh.loc(0.6)] == 1

    h = NamedHist.new.Bool(name="x").Double().fill(x=[True, True])
    assert h[bh.loc(True)] == 2

    h = (
        NamedHist.new.Bool(name="x")
        .Bool(name="y")
        .Double()
        .fill(x=[True, True], y=[True, False])
    )

    assert h[True, True] == 1
    assert h[True, False] == 1

    h = NamedHist.new.Var(range(10), name="x").Double().fill(x=[5, 5])
    assert h[5j] == 2

    h = (
        NamedHist.new.Var(range(10), name="x")
        .Var(range(10), name="y")
        .Double()
        .fill(x=[5, 5], y=[2, 6])
    )

    assert h[5j, 2j] == 1
    assert h[bh.loc(5), bh.loc(6)] == 1

    h = NamedHist.new.Int(0, 10, name="x").Double().fill(x=[5, 5])
    assert h[5j] == 2

    h = (
        NamedHist.new.Int(0, 10, name="x")
        .Int(0, 10, name="y")
        .Double()
        .fill(x=[5, 5], y=[2, 6])
    )

    assert h[5j, 2j] == 1
    assert h[bh.loc(5), bh.loc(6)] == 1

    h = NamedHist.new.IntCat(range(10), name="x").Double().fill(x=[5, 5])
    assert h[5j] == 2

    h = (
        NamedHist.new.IntCat(range(10), name="x")
        .IntCat(range(10), name="y")
        .Double()
        .fill(x=[5, 5], y=[2, 6])
    )

    assert h[5j, 2j] == 1
    assert h[bh.loc(5), bh.loc(6)] == 1

    h = NamedHist.new.StrCat("TF", name="x").Double().fill(x=["T", "T"])
    assert h["T"] == 2

    h = (
        NamedHist.new.StrCat("TF", name="x")
        .StrCat("TF", name="y")
        .Double()
        .fill(x=["T", "T"], y=["T", "F"])
    )

    assert h["T", "T"] == 1
    assert h["T", "F"] == 1
コード例 #8
0
ファイル: test_named.py プロジェクト: scikit-hep/hist
def test_named_index_access():
    """
    Test named index access -- whether NamedHist can be accessed by index.
    """

    h = NamedHist(
        axis.Regular(10, -5, 5, name="Ones"),
        axis.Regular(10, -5, 5, name="Twos"),
        axis.StrCategory(["hi", "hello"], name="Greet"),
        axis.Boolean(name="Yes"),
        axis.Integer(0, 10, name="Int"),
    ).fill(
        Ones=np.ones(10),
        Twos=np.ones(10) * 2,
        Greet=["hi"] * 8 + ["hello"] * 2,
        Yes=[True] * 6 + [False] * 4,
        Int=np.ones(10),
    )

    assert h[1j, 2j, "hi", True, 1] == 6
    assert (
        h[
            {
                "Ones": 6,
                "Twos": 7,
                "Greet": bh.loc("hi"),
                "Yes": bh.loc(True),
                "Int": bh.loc(1),
            }
        ]
        == 6
    )
    assert h[0j + 1, -2j + 4, "hi", True, 1] == 6
    assert (
        h[
            {
                "Ones": bh.loc(1, 0),
                "Twos": bh.loc(3, -1),
                "Greet": "hi",
                "Yes": True,
                "Int": 1,
            }
        ]
        == 6
    )

    with pytest.raises(Exception):
        h[0 : bh.loc(1, 0), 1 : bh.loc(3, -1), 2:"hi", 3:True, 4:1]

    with pytest.raises(Exception):
        h[0 : bh.loc(1, 0), 1 : bh.loc(3, -1), "Greet":"hi", 3:True, 4:1]

    assert h[0:10:2j, 0:5:5j, "hello", False, 5]
    assert len(h[::2j, 0:5, :, :, :].axes[1]) == 5
    assert len(h[:, 0:5, :, :, :].axes[1]) == 5

    # wrong loc shortcut
    with pytest.raises(Exception):
        h[0.5, 1 / 2, "hi", True, 1]

    with pytest.raises(Exception):
        h[0.5 + 1j, 1 / 2 + 1j, "hi", True, 1]

    # wrong rebin shortcut
    with pytest.raises(Exception):
        h[0:10:0.2j, 0:5:0.5j, "hello", False, 5]

    with pytest.raises(Exception):
        h[0 : 10 : 1 + 2j, 0 : 5 : 1 + 5j, "hello", False, 5]

    with pytest.raises(Exception):
        h[0:10:20j, 0:5:10j, "hello", False, 5]
コード例 #9
0
ファイル: test_named.py プロジェクト: scikit-hep/hist
def test_named_project():
    """
    Test named project -- whether NamedHist can be projected properly.
    """

    h = NamedHist(
        axis.Regular(
            50, -5, 5, name="A", label="a [units]", underflow=False, overflow=False
        ),
        axis.Boolean(name="B", label="b [units]"),
        axis.Variable(range(11), name="C", label="c [units]"),
        axis.Integer(0, 10, name="D", label="d [units]"),
        axis.IntCategory(range(10), name="E", label="e [units]"),
        axis.StrCategory("FT", name="F", label="f [units]"),
    )

    # via names
    assert h.project()
    assert h.project("A", "B")
    assert h.project("A", "B", "C", "D", "E", "F")

    h = NamedHist(
        axis.Regular(
            50, -5, 5, name="A", label="a [units]", underflow=False, overflow=False
        ),
        axis.Boolean(name="B", label="b [units]"),
        axis.Variable(range(11), name="C", label="c [units]"),
        axis.Integer(0, 10, name="D", label="d [units]"),
        axis.IntCategory(range(10), name="E", label="e [units]"),
        axis.StrCategory("FT", name="F", label="f [units]"),
    )

    # via indices
    with pytest.raises(Exception):
        h.project(0, 1)
    with pytest.raises(Exception):
        h.project(0, 1, 2, 3, 4, 5)

    # duplicated
    with pytest.raises(Exception):
        h.project(0, 0)

    with pytest.raises(Exception):
        h.project("A", "A")

    # wrong/mixed types
    with pytest.raises(Exception):
        h.project(2, "A")

    with pytest.raises(Exception):
        h.project(True, "A")

    # cannot found
    with pytest.raises(Exception):
        h.project(-1, 9)

    with pytest.raises(Exception):
        h.project("G", "H")
コード例 #10
0
ファイル: test_named.py プロジェクト: scikit-hep/hist
def test_named_init():
    """
    Test named init -- whether NamedHist can be properly initialized.
    """

    # basic
    h = NamedHist(
        axis.Regular(10, 0, 1, name="x"), axis.Regular(10, 0, 1, name="y")
    ).fill(x=[0.35, 0.35, 0.45], y=[0.35, 0.35, 0.45])

    for idx in range(10):
        if idx == 3:
            assert h[idx, idx] == 2
            assert h[{"x": idx, "y": idx}] == 2
        elif idx == 4:
            assert h[idx, idx] == 1
        else:
            assert h[idx, idx] == 0
        with pytest.raises(Exception):
            h[{0: idx, 1: idx}]
    # with named axes
    assert NamedHist(
        axis.Regular(50, -3, 3, name="x"), axis.Regular(50, -3, 3, name="y")
    ).fill(x=np.random.randn(10), y=np.random.randn(10))

    assert NamedHist(axis.Boolean(name="x"), axis.Boolean(name="y")).fill(
        y=[True, False, True], x=[True, False, True]
    )

    assert NamedHist(
        axis.Variable(range(-3, 3), name="x"), axis.Variable(range(-3, 3), name="y")
    ).fill(x=np.random.randn(10), y=np.random.randn(10))

    assert NamedHist(axis.Integer(-3, 3, name="x"), axis.Integer(-3, 3, name="y")).fill(
        x=np.random.randn(10), y=np.random.randn(10)
    )

    assert NamedHist(
        axis.IntCategory(range(-3, 3), name="x"),
        axis.IntCategory(range(-3, 3), name="y"),
    ).fill(x=np.random.randn(10), y=np.random.randn(10))

    assert NamedHist(
        axis.StrCategory(["F", "T"], name="x"), axis.StrCategory("FT", name="y")
    ).fill(y=["T", "F", "T"], x=["T", "F", "T"])

    # cannot access via index
    h = NamedHist(axis.Regular(10, 0, 1, name="x")).fill(x=[0.35, 0.35, 0.45])

    for idx in range(10):
        with pytest.raises(Exception):
            h[{0: idx}]

    # with no-named axes
    with pytest.raises(Exception):
        NamedHist(axis.Regular(50, -3, 3), axis.Regular(50, -3, 3)).fill(
            x=np.random.randn(10), y=np.random.randn(10)
        )

    with pytest.raises(Exception):
        NamedHist(axis.Boolean(), axis.Boolean()).fill(
            y=[True, False, True], x=[True, False, True]
        )

    with pytest.raises(Exception):
        NamedHist(axis.Variable(range(-3, 3)), axis.Variable(range(-3, 3))).fill(
            x=np.random.randn(10), y=np.random.randn(10)
        )

    with pytest.raises(Exception):
        NamedHist(axis.Integer(-3, 3), axis.Integer(-3, 3)).fill(
            x=np.random.randn(10), y=np.random.randn(10)
        )

    with pytest.raises(Exception):
        NamedHist(
            axis.IntCategory(range(-3, 3)),
            axis.IntCategory(range(-3, 3)),
        ).fill(x=np.random.randn(10), y=np.random.randn(10))

    with pytest.raises(Exception):
        NamedHist(axis.StrCategory(["F", "T"]), axis.StrCategory("FT")).fill(
            y=["T", "F", "T"], x=["T", "F", "T"]
        )

    # with duplicated names
    with pytest.raises(Exception):
        NamedHist(axis.Regular(50, -3, 3, name="x"), axis.Regular(50, -3, 3, name="x"))

    with pytest.raises(Exception):
        NamedHist(axis.Boolean(name="y"), axis.Boolean(name="y"))

    with pytest.raises(Exception):
        NamedHist(
            axis.Variable(range(-3, 3), name="x"), axis.Variable(range(-3, 3), name="x")
        )

    with pytest.raises(Exception):
        NamedHist(axis.Integer(-3, 3, name="x"), axis.Integer(-3, 3, name="x"))

    with pytest.raises(Exception):
        NamedHist(
            axis.IntCategory(range(-3, 3), name="x"),
            axis.IntCategory(range(-3, 3), name="x"),
        )

    with pytest.raises(Exception):
        NamedHist(
            axis.StrCategory("TF", name="y"), axis.StrCategory(["T", "F"], name="y")
        )
コード例 #11
0
def test_stack_constructor_fails():
    # Don't allow construction directly from axes with no Histograms
    with pytest.raises(Exception):
        assert Stack(reg_ax)

    with pytest.raises(Exception):
        assert Stack(reg_ax, reg_ax, reg_ax)

    # not allow to construct stack with different-type but same-type-axis histograms
    with pytest.raises(Exception):
        Stack(reg_hist, named_reg_hist)
    with pytest.raises(Exception):
        assert Stack(boo_hist, named_boo_hist)
    with pytest.raises(Exception):
        Stack(var_hist, named_var_hist)
    with pytest.raises(Exception):
        Stack(int_hist, named_int_hist)
    with pytest.raises(Exception):
        Stack(int_cat_hist, named_int_cat_hist)
    with pytest.raises(Exception):
        Stack(str_cat_hist, named_str_cat_hist)

    # not allow to construct stack with same-type but different-type-axis histograms
    with pytest.raises(Exception):
        Stack(reg_hist, boo_hist, var_hist)
    with pytest.raises(Exception):
        Stack(int_hist, int_cat_hist, str_cat_hist)

    # allow to construct stack with 2d histograms
    s1 = Stack(reg_hist_2d, reg_hist_2d, reg_hist_2d)
    s2 = Stack(boo_hist_2d, boo_hist_2d, boo_hist_2d)
    s3 = Stack(var_hist_2d, var_hist_2d, var_hist_2d)
    s4 = Stack(int_hist_2d, int_hist_2d, int_hist_2d)
    s5 = Stack(int_cat_hist_2d, int_cat_hist_2d, int_cat_hist_2d)
    s6 = Stack(str_cat_hist_2d, str_cat_hist_2d, str_cat_hist_2d)

    assert s1.axes == reg_hist_2d.axes
    assert s2.axes == boo_hist_2d.axes
    assert s3.axes == var_hist_2d.axes
    assert s4.axes == int_hist_2d.axes
    assert s5.axes == int_cat_hist_2d.axes
    assert s6.axes == str_cat_hist_2d.axes

    # not allow to construct stack with different ndim
    with pytest.raises(Exception):
        Stack(reg_hist, reg_hist_2d)
    with pytest.raises(Exception):
        Stack(boo_hist, boo_hist_2d)
    with pytest.raises(Exception):
        Stack(var_hist, var_hist_2d)
    with pytest.raises(Exception):
        Stack(int_hist, int_hist_2d)
    with pytest.raises(Exception):
        Stack(int_cat_hist, int_cat_hist_2d)
    with pytest.raises(Exception):
        Stack(str_cat_hist, str_cat_hist_2d)

    # not allow to struct stack from histograms with different axes
    with pytest.raises(Exception):
        NamedHist(named_reg_ax, axis.Regular(10, 0, 1,
                                             name="X")).stack("A", "X")
    with pytest.raises(Exception):
        NamedHist(named_boo_ax, axis.Boolean(name="X")).stack("B", "X")
    with pytest.raises(Exception):
        NamedHist(named_var_ax, axis.Variable(range(-3, 3),
                                              name="X")).stack("C", "X")
    with pytest.raises(Exception):
        NamedHist(named_int_ax, axis.Integer(-3, 3, name="X")).stack("D", "X")
    with pytest.raises(Exception):
        NamedHist(named_int_cat_ax,
                  axis.IntCategory(range(-3, 3), name="X")).stack("E", "X")
    with pytest.raises(Exception):
        NamedHist(named_str_cat_ax,
                  axis.StrCategory(["F", "T"], name="X")).stack("F", "X")
コード例 #12
0
ファイル: test_plot.py プロジェクト: cranmer/hist
def test_named_plot_pull():
    """
    Test named plot_pull -- whether 1d-NamedHist can be plotted pull properly.
    """

    np.random.seed(42)

    h = NamedHist(
        axis.Regular(50,
                     -4,
                     4,
                     name="S",
                     label="s [units]",
                     underflow=False,
                     overflow=False)).fill(S=np.random.normal(size=10))

    def pdf(x, a=1 / np.sqrt(2 * np.pi), x0=0, sigma=1, offset=0):
        return a * np.exp(-((x - x0)**2) / (2 * sigma**2)) + offset

    assert h.plot_pull(
        pdf,
        eb_ecolor="crimson",
        eb_mfc="crimson",
        eb_mec="crimson",
        eb_fmt="o",
        eb_ms=6,
        eb_capsize=1,
        eb_capthick=2,
        eb_alpha=0.8,
        fp_c="chocolate",
        fp_ls="-",
        fp_lw=3,
        fp_alpha=1.0,
        bar_fc="orange",
        pp_num=6,
        pp_fc="orange",
        pp_alpha=0.618,
        pp_ec=None,
    )

    pdf_str = "a * np.exp(-((x - x0) ** 2) / (2 * sigma ** 2)) + offset"

    assert h.plot_pull(pdf_str)

    # dimension error
    hh = NamedHist(
        axis.Regular(50,
                     -4,
                     4,
                     name="X",
                     label="s [units]",
                     underflow=False,
                     overflow=False),
        axis.Regular(50,
                     -4,
                     4,
                     name="Y",
                     label="s [units]",
                     underflow=False,
                     overflow=False),
    ).fill(X=np.random.normal(size=10), Y=np.random.normal(size=10))

    with pytest.raises(Exception):
        hh.plot_pull(pdf)

    # no eval-able variable
    with pytest.raises(Exception):
        h.plot_pull("1")

    with pytest.raises(Exception):
        h.plot_pull(1)

    with pytest.raises(Exception):
        h.plot_pull(0.1)

    with pytest.raises(Exception):
        h.plot_pull((1, 2))

    with pytest.raises(Exception):
        h.plot_pull([1, 2])

    with pytest.raises(Exception):
        h.plot_pull({"a": 1})
    plt.close("all")

    # wrong kwargs names
    with pytest.raises(Exception):
        h.plot_pull(pdf, abc="crimson", xyz="crimson")

    with pytest.raises(Exception):
        h.plot_pull(pdf, ecolor="crimson", mfc="crimson")

    # not disabled params
    h.plot_pull(pdf, eb_label="value")

    h.plot_pull(pdf, fp_label="value")

    h.plot_pull(pdf, ub_label="value")

    h.plot_pull(pdf, bar_label="value")

    h.plot_pull(pdf, pp_label="value")

    # disabled params
    with pytest.raises(Exception):
        h.plot_pull(pdf, bar_width="value")

    # wrong kwargs types
    with pytest.raises(Exception):
        h.plot_pull(pdf, eb_ecolor=1.0, eb_mfc=1.0)  # kwargs should be str

    plt.close("all")
コード例 #13
0
ファイル: test_plot.py プロジェクト: cranmer/hist
def test_named_plot():
    """
    Test named plot -- whether NamedHist can be plotted properly.
    """

    h = NamedHist(
        axis.Regular(50,
                     -5,
                     5,
                     name="A",
                     label="a [units]",
                     underflow=False,
                     overflow=False), ).fill(A=np.random.normal(size=10))

    assert h.plot(color="green", ls="--", lw=3)

    h = NamedHist(
        axis.Regular(50,
                     -5,
                     5,
                     name="A",
                     label="a [units]",
                     underflow=False,
                     overflow=False),
        axis.Regular(50,
                     -4,
                     4,
                     name="B",
                     label="b [units]",
                     underflow=False,
                     overflow=False),
    ).fill(B=np.random.normal(size=10), A=np.random.normal(size=10))

    assert h.plot(cmap="cividis")
    plt.close("all")

    # dimension error
    h = NamedHist(
        axis.Regular(50,
                     -5,
                     5,
                     name="A",
                     label="a [units]",
                     underflow=False,
                     overflow=False),
        axis.Regular(50,
                     -4,
                     4,
                     name="B",
                     label="b [units]",
                     underflow=False,
                     overflow=False),
        axis.Regular(50,
                     -4,
                     4,
                     name="C",
                     label="c [units]",
                     underflow=False,
                     overflow=False),
    ).fill(
        A=np.random.normal(size=10),
        B=np.random.normal(size=10),
        C=np.random.normal(size=10),
    )

    with pytest.raises(Exception):
        h.plot()

    # wrong kwargs names
    with pytest.raises(Exception):
        h.project("A").plot(abc="red")

    with pytest.raises(Exception):
        h.project("A", "C").plot(abc="red")

    # wrong kwargs type
    with pytest.raises(Exception):
        h.project("B").plot(ls="red")

    with pytest.raises(Exception):
        h.project("A", "C").plot(cmap=0.1)

    plt.close("all")
コード例 #14
0
ファイル: test_plot.py プロジェクト: cranmer/hist
def test_named_plot2d_full():
    """
    Test named plot2d_full -- whether 2d-NamedHist can be fully plotted properly.
    """

    h = NamedHist(
        axis.Regular(50,
                     -5,
                     5,
                     name="A",
                     label="a [units]",
                     underflow=False,
                     overflow=False),
        axis.Regular(50,
                     -4,
                     4,
                     name="B",
                     label="b [units]",
                     underflow=False,
                     overflow=False),
    ).fill(B=np.random.normal(size=10), A=np.random.normal(size=10))

    assert h.plot2d_full(
        main_cmap="cividis",
        top_ls="--",
        top_color="orange",
        top_lw=2,
        side_ls="-.",
        side_lw=1,
        side_color="steelblue",
    )
    plt.close("all")

    # dimension error
    h = NamedHist(
        axis.Regular(50,
                     -5,
                     5,
                     name="A",
                     label="a [units]",
                     underflow=False,
                     overflow=False),
        axis.Regular(50,
                     -4,
                     4,
                     name="B",
                     label="b [units]",
                     underflow=False,
                     overflow=False),
    ).fill(B=np.random.normal(size=10), A=np.random.normal(size=10))

    with pytest.raises(Exception):
        h.project("A").plot2d_full()

    # wrong kwargs names
    with pytest.raises(Exception):
        h.plot2d_full(abc="red")

    with pytest.raises(Exception):
        h.plot2d_full(color="red")

    # wrong kwargs type
    with pytest.raises(Exception):
        h.plot2d_full(main_cmap=0.1, side_lw="autumn")

    plt.close("all")
コード例 #15
0
ファイル: test_named.py プロジェクト: aribalam/MyHist
def test_basic_usage():

    # Check if axis without name raises an error
    with pytest.raises(KeyError):
        h_named = NamedHist(
            axis.Regular(10, 0, 1, name="x"),
            axis.Regular(10, 0, 1)
        )

    h_named = NamedHist(
        axis.Regular(10, 0, 1, name="x"),
        axis.Regular(10, 0, 1, name="y")
    )  # NamedHist should require axis.Regular to have a name set

    # Check if filling without keyword raises error
    with pytest.raises(ValueError):
        h_named.fill([0.35, 0.35, 0.45], y=[0.65, 0.75, 0.85])

    h_named.fill(x=[0.35, 0.35, 0.45], y=[5, 10, 14])  # Fill should be keyword only, with the names

    h_normal = Hist(
        axis.Regular(10, 0, 1, name="x"),
        axis.Regular(10, 5, 15, name="y")
    )
    h_normal.fill([0.35, 0.35, 0.45], [0.65, 0.75, 0.85])

    assert (h_named.view() == h_normal.view()).all()

    h = NamedHist(
        axis.Regular(10, 0, 1, name='x')
    )

    h.fill(x=[0.35, 0.35, 0.45])

    # Example of a test that should be made to pass:
    assert h[{'x': 2}] == 0  # Should work
    assert h[{'x': 3}] == 2  # Should work
    assert h[{'x': 4}] == 1  # Should work
    assert h[{'x': 5}] == 0  # Should work

    # Additional Test cases on indexing

    h2 = h_normal[{0: slice(1, 5, None), 1: slice(None, 5, None)}]
    h3 = h_named[{'y': slice(None, 5, None), 'x': slice(1, 5, None)}]

    # Check if indexing by axis name works correctly
    assert (h2.view() == h3.view()).all()

    h2 = h_normal[{0: 3}]
    h3 = h_named[{'x': 3}]

    # Check if indexing works correctly
    assert (h2.view() == h3.view()).all()

    h2 = h_normal[{0: loc(0.35)}]
    h3 = h_normal[loc(0.35), :]
    h4 = h_named[{'x': loc(0.35)}]

    # Checking if indexing with loc() works correctly
    assert (h2.view() == h3.view()).all()
    assert (h3.view() == h4.view()).all()

    h2 = h_normal[{1: slice(None, None, sum)}]
    h3 = h_named[{'y': slice(None, None, sum)}]

    # Check if indexing with sum works correctly
    assert (h2.view() == h3.view()).all()
コード例 #16
0
ファイル: test_named.py プロジェクト: scikit-hep/hist
def test_named_fill():
    """
    Test named fill -- whether NamedHist can be properly filled.
    """

    # Regular
    h = NamedHist(
        axis.Regular(10, 0, 1, name="x"),
        axis.Regular(10, 0, 1, name="y"),
        axis.Regular(2, 0, 2, name="z"),
    ).fill(
        x=[0.35, 0.35, 0.35, 0.45, 0.55, 0.55, 0.55],
        y=[0.35, 0.35, 0.45, 0.45, 0.45, 0.45, 0.45],
        z=[0, 0, 1, 1, 1, 1, 1],
    )

    z_one_only = h[{"z": bh.loc(1)}]
    for idx_x in range(10):
        for idx_y in range(10):
            if idx_x == 3 and idx_y == 4 or idx_x == 4 and idx_y == 4:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 1
                )
            elif idx_x == 5 and idx_y == 4:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 3
                )
            else:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 0
                )

    # Boolean
    h = NamedHist(
        axis.Boolean(name="x"),
        axis.Boolean(name="y"),
        axis.Boolean(name="z"),
    ).fill(
        x=[True, True, True, True, True, False, True],
        y=[False, True, True, False, False, True, False],
        z=[False, False, True, True, True, True, True],
    )

    z_one_only = h[{"z": bh.loc(True)}]
    assert z_one_only[False, False] == z_one_only[{"x": False, "y": False}] == 0
    assert z_one_only[False, True] == z_one_only[{"x": False, "y": True}] == 1
    assert z_one_only[True, False] == z_one_only[{"x": True, "y": False}] == 3
    assert z_one_only[True, True] == z_one_only[{"x": True, "y": True}] == 1

    # Variable
    h = NamedHist(
        axis.Variable(range(11), name="x"),
        axis.Variable(range(11), name="y"),
        axis.Variable(range(3), name="z"),
    ).fill(
        x=[3.5, 3.5, 3.5, 4.5, 5.5, 5.5, 5.5],
        y=[3.5, 3.5, 4.5, 4.5, 4.5, 4.5, 4.5],
        z=[0, 0, 1, 1, 1, 1, 1],
    )

    z_one_only = h[{"z": bh.loc(1)}]
    for idx_x in range(10):
        for idx_y in range(10):
            if idx_x == 3 and idx_y == 4 or idx_x == 4 and idx_y == 4:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 1
                )
            elif idx_x == 5 and idx_y == 4:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 3
                )
            else:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 0
                )

    # Integer
    h = NamedHist(
        axis.Integer(0, 10, name="x"),
        axis.Integer(0, 10, name="y"),
        axis.Integer(0, 2, name="z"),
    ).fill(
        x=[3.5, 3.5, 3.5, 4.5, 5.5, 5.5, 5.5],
        y=[3.5, 3.5, 4.5, 4.5, 4.5, 4.5, 4.5],
        z=[0, 0, 1, 1, 1, 1, 1],
    )

    z_one_only = h[{"z": bh.loc(1)}]
    for idx_x in range(10):
        for idx_y in range(10):
            if idx_x == 3 and idx_y == 4 or idx_x == 4 and idx_y == 4:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 1
                )
            elif idx_x == 5 and idx_y == 4:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 3
                )
            else:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 0
                )

    # IntCategory
    h = NamedHist(
        axis.IntCategory(range(10), name="x"),
        axis.IntCategory(range(10), name="y"),
        axis.IntCategory(range(2), name="z"),
    ).fill(
        x=[3.5, 3.5, 3.5, 4.5, 5.5, 5.5, 5.5],
        y=[3.5, 3.5, 4.5, 4.5, 4.5, 4.5, 4.5],
        z=[0, 0, 1, 1, 1, 1, 1],
    )

    z_one_only = h[{"z": bh.loc(1)}]
    for idx_x in range(10):
        for idx_y in range(10):
            if idx_x == 3 and idx_y == 4 or idx_x == 4 and idx_y == 4:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 1
                )
            elif idx_x == 5 and idx_y == 4:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 3
                )
            else:
                assert (
                    z_one_only[idx_x, idx_y]
                    == z_one_only[{"x": idx_x, "y": idx_y}]
                    == 0
                )

    # StrCategory
    h = NamedHist(
        axis.StrCategory("FT", name="x"),
        axis.StrCategory(list("FT"), name="y"),
        axis.StrCategory(["F", "T"], name="z"),
    ).fill(
        x=["T", "T", "T", "T", "T", "F", "T"],
        y=["F", "T", "T", "F", "F", "T", "F"],
        z=["F", "F", "T", "T", "T", "T", "T"],
    )

    z_one_only = h[{"z": bh.loc("T")}]
    assert z_one_only[bh.loc("F"), bh.loc("F")] == 0
    assert z_one_only[bh.loc("F"), bh.loc("T")] == 1
    assert z_one_only[bh.loc("T"), bh.loc("F")] == 3
    assert z_one_only[bh.loc("T"), bh.loc("T")] == 1

    # without names
    with pytest.raises(Exception):
        NamedHist(
            axis.Regular(50, -3, 3, name="x"), axis.Regular(50, -3, 3, name="y")
        ).fill(np.random.randn(10), np.random.randn(10))

    with pytest.raises(Exception):
        NamedHist(axis.Boolean(name="x"), axis.Boolean(name="y")).fill(
            [True, False, True], [True, False, True]
        )

    with pytest.raises(Exception):
        NamedHist(
            axis.Variable(range(-3, 3), name="x"), axis.Variable(range(-3, 3), name="y")
        ).fill(np.random.randn(10), np.random.randn(10))

    with pytest.raises(Exception):
        NamedHist(axis.Integer(-3, 3, name="x"), axis.Integer(-3, 3, name="y")).fill(
            np.random.randn(10), np.random.randn(10)
        )

    with pytest.raises(Exception):
        NamedHist(
            axis.IntCategory(range(-3, 3), name="x"),
            axis.IntCategory(range(-3, 3), name="y"),
        ).fill(np.random.randn(10), np.random.randn(10))

    with pytest.raises(Exception):
        NamedHist(
            axis.StrCategory(["F", "T"], name="x"), axis.StrCategory("FT", name="y")
        ).fill(["T", "F", "T"], ["T", "F", "T"])

    # wrong names
    with pytest.raises(Exception):
        NamedHist(
            axis.Regular(50, -3, 3, name="x"), axis.Regular(50, -3, 3, name="y")
        ).fill(x=np.random.randn(10), z=np.random.randn(10))

    with pytest.raises(Exception):
        NamedHist(axis.Boolean(name="x"), axis.Boolean(name="y")).fill(
            y=[True, False, True], z=[True, False, True]
        )

    with pytest.raises(Exception):
        NamedHist(
            axis.Variable(range(-3, 3), name="x"), axis.Variable(range(-3, 3), name="y")
        ).fill(z=np.random.randn(10), x=np.random.randn(10))

    with pytest.raises(Exception):
        NamedHist(axis.Integer(-3, 3, name="x"), axis.Integer(-3, 3, name="y")).fill(
            x=np.random.randn(10), z=np.random.randn(10)
        )

    with pytest.raises(Exception):
        NamedHist(
            axis.IntCategory(range(-3, 3), name="x"),
            axis.IntCategory(range(-3, 3), name="y"),
        ).fill(y=np.random.randn(10), z=np.random.randn(10))

    with pytest.raises(Exception):
        NamedHist(
            axis.StrCategory(["F", "T"], name="x"), axis.StrCategory("FT", name="y")
        ).fill(z=["T", "F", "T"], x=["T", "F", "T"])

    h = NamedHist(
        axis.Regular(
            50, -4, 4, name="X", label="s [units]", underflow=False, overflow=False
        )
    ).fill(X=np.random.normal(size=10))
コード例 #17
0
reg_hist = Hist(reg_ax).fill(np.random.randn(10))
boo_hist = Hist(boo_ax).fill([True, False, True])
var_hist = Hist(var_ax).fill(np.random.randn(10))
int_hist = Hist(int_ax).fill(np.random.randn(10))
int_cat_hist = Hist(int_cat_ax).fill(np.random.randn(10))
str_cat_hist = Hist(str_cat_ax).fill(["T", "F", "T"])

named_reg_ax = axis.Regular(10, 0, 1, name="A")
named_boo_ax = axis.Boolean(name="B")
named_var_ax = axis.Variable(range(-3, 3), name="C")
named_int_ax = axis.Integer(-3, 3, name="D")
named_int_cat_ax = axis.IntCategory(range(-3, 3), name="E")
named_str_cat_ax = axis.StrCategory(["F", "T"], name="F")

named_reg_hist = NamedHist(named_reg_ax).fill(A=np.random.randn(10))
named_boo_hist = NamedHist(named_boo_ax).fill(B=[True, False, True])
named_var_hist = NamedHist(named_var_ax).fill(C=np.random.randn(10))
named_int_hist = NamedHist(named_int_ax).fill(D=np.random.randn(10))
named_int_cat_hist = NamedHist(named_int_cat_ax).fill(E=np.random.randn(10))
named_str_cat_hist = NamedHist(named_str_cat_ax).fill(F=["T", "F", "T"])

reg_hist_2d = Hist(reg_ax, reg_ax).fill(np.random.randn(10),
                                        np.random.randn(10))

boo_hist_2d = Hist(boo_ax, boo_ax).fill([True, False, True],
                                        [True, False, True])
var_hist_2d = Hist(var_ax, var_ax).fill(np.random.randn(10),
                                        np.random.randn(10))
int_hist_2d = Hist(int_ax, int_ax).fill(np.random.randn(10),
                                        np.random.randn(10))