Skip to content

Commit

Permalink
test: Add backend support to testing
Browse files Browse the repository at this point in the history
* Adding tensorflow tests are very slow
  • Loading branch information
matthewfeickert committed Apr 18, 2024
1 parent bd10ae3 commit e815612
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions tests/test_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def check_uniform_type(in_list):
)


def test_toms748_scan(tmp_path, hypotest_args):
def test_toms748_scan(backend, tmp_path, hypotest_args):
"""
Test the upper limit toms748 scan returns the correct structure and values
"""
Expand Down Expand Up @@ -53,11 +53,12 @@ def test_toms748_scan(tmp_path, hypotest_args):
for i in range(5)
]
)
assert observed_cls == pytest.approx(0.05)
# FIXME: Remove float cast after TensorFlow support removed

Check notice on line 56 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L56

unresolved comment '# FIXME: Remove float cast after TensorFlow support removed' (C100)
assert float(observed_cls) == pytest.approx(0.05)
assert expected_cls == pytest.approx(0.05)


def test_toms748_scan_bounds_extension(hypotest_args):
def test_toms748_scan_bounds_extension(backend, hypotest_args):
"""
Test the upper limit toms748 scan bounds can correctly extend to bracket the CLs level
"""
Expand All @@ -72,18 +73,20 @@ def test_toms748_scan_bounds_extension(hypotest_args):
data, model, 3, 5, rtol=1e-8
)

assert observed_limit == pytest.approx(observed_limit_default)
# FIXME: Remove float cast after TensorFlow support removed

Check notice on line 76 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L76

unresolved comment '# FIXME: Remove float cast after TensorFlow support removed' (C100)
assert float(observed_limit) == pytest.approx(observed_limit_default)
assert np.allclose(np.asarray(expected_limits), np.asarray(expected_limits_default))

# Force bounds_up to expand
observed_limit, expected_limits = pyhf.infer.intervals.upper_limits.toms748_scan(
data, model, 0, 1, rtol=1e-8
)
assert observed_limit == pytest.approx(observed_limit_default)
# FIXME: Remove float cast after TensorFlow support removed

Check notice on line 84 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L84

unresolved comment '# FIXME: Remove float cast after TensorFlow support removed' (C100)
assert float(observed_limit) == pytest.approx(observed_limit_default)
assert np.allclose(np.asarray(expected_limits), np.asarray(expected_limits_default))


def test_upper_limit_against_auto(hypotest_args):
def test_upper_limit_against_auto(backend, hypotest_args):
"""
Test upper_limit linear scan and toms748_scan return similar results
"""
Expand All @@ -97,11 +100,13 @@ def test_upper_limit_against_auto(hypotest_args):
)
obs_linear, exp_linear = results_linear
# Can't expect these to be much closer given the low granularity of the linear scan
assert obs_auto == pytest.approx(obs_linear, abs=0.1)
# FIXME: Remove float cast after TensorFlow support removed

Check notice on line 103 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L103

unresolved comment '# FIXME: Remove float cast after TensorFlow support removed' (C100)
assert float(obs_auto) == pytest.approx(obs_linear, abs=0.1)
assert np.allclose(exp_auto, exp_linear, atol=0.1)


def test_upper_limit(hypotest_args):
@pytest.mark.skip_numpy_minuit
def test_upper_limit(backend, hypotest_args):
"""
Check that the default return structure of pyhf.infer.hypotest is as expected
"""
Expand All @@ -110,22 +115,27 @@ def test_upper_limit(hypotest_args):
results = pyhf.infer.intervals.upper_limits.upper_limit(data, model, scan=scan)
assert len(results) == 2
observed_limit, expected_limits = results
assert observed_limit == pytest.approx(1.0262704738584554)
assert expected_limits == pytest.approx(
[0.65765653, 0.87999725, 1.12453992, 1.50243428, 2.09232927]
# FIXME: Remove float cast after TensorFlow support removed

Check notice on line 118 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L118

unresolved comment '# FIXME: Remove float cast after TensorFlow support removed' (C100)
assert float(observed_limit) == pytest.approx(1.0262704738584554)
# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed

Check notice on line 120 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L120

unresolved comment '# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed' (C100)
assert np.allclose(
expected_limits, [0.65765653, 0.87999725, 1.12453992, 1.50243428, 2.09232927]
)

# tighter relative tolerance needed for macos
results = pyhf.infer.intervals.upper_limits.upper_limit(data, model, rtol=1e-6)
assert len(results) == 2
observed_limit, expected_limits = results
assert observed_limit == pytest.approx(1.01156939)
assert expected_limits == pytest.approx(
[0.55988001, 0.75702336, 1.06234693, 1.50116923, 2.05078596]
# FIXME: Remove float cast after TensorFlow support removed

Check notice on line 129 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L129

unresolved comment '# FIXME: Remove float cast after TensorFlow support removed' (C100)
assert float(observed_limit) == pytest.approx(1.01156939)
# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed

Check notice on line 131 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L131

unresolved comment '# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed' (C100)
assert np.allclose(
expected_limits, [0.55988001, 0.75702336, 1.06234693, 1.50116923, 2.05078596]
)


def test_upper_limit_with_kwargs(hypotest_args):
@pytest.mark.skip_numpy_minuit
def test_upper_limit_with_kwargs(backend, hypotest_args):
"""
Check that the default return structure of pyhf.infer.hypotest is as expected
"""
Expand All @@ -136,9 +146,11 @@ def test_upper_limit_with_kwargs(hypotest_args):
)
assert len(results) == 2
observed_limit, expected_limits = results
assert observed_limit == pytest.approx(1.0262704738584554)
assert expected_limits == pytest.approx(
[0.65765653, 0.87999725, 1.12453992, 1.50243428, 2.09232927]
# FIXME: Remove float cast after TensorFlow support removed

Check notice on line 149 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L149

unresolved comment '# FIXME: Remove float cast after TensorFlow support removed' (C100)
assert float(observed_limit) == pytest.approx(1.0262704738584554)
# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed

Check notice on line 151 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L151

unresolved comment '# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed' (C100)
assert np.allclose(
expected_limits, [0.65765653, 0.87999725, 1.12453992, 1.50243428, 2.09232927]
)

# linear_grid_scan
Expand All @@ -147,9 +159,11 @@ def test_upper_limit_with_kwargs(hypotest_args):
)
assert len(results) == 3
observed_limit, expected_limits, (_scan, point_results) = results
assert observed_limit == pytest.approx(1.0262704738584554)
assert expected_limits == pytest.approx(
[0.65765653, 0.87999725, 1.12453992, 1.50243428, 2.09232927]
# FIXME: Remove float cast after TensorFlow support removed

Check notice on line 162 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L162

unresolved comment '# FIXME: Remove float cast after TensorFlow support removed' (C100)
assert float(observed_limit) == pytest.approx(1.0262704738584554)
# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed

Check notice on line 164 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L164

unresolved comment '# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed' (C100)
assert np.allclose(
expected_limits, [0.65765653, 0.87999725, 1.12453992, 1.50243428, 2.09232927]
)
assert _scan.tolist() == scan.tolist()
assert len(_scan) == len(point_results)
Expand All @@ -160,9 +174,11 @@ def test_upper_limit_with_kwargs(hypotest_args):
)
assert len(results) == 3
observed_limit, expected_limits, (_scan, point_results) = results
assert observed_limit == pytest.approx(1.01156939)
assert expected_limits == pytest.approx(
[0.55988001, 0.75702336, 1.06234693, 1.50116923, 2.05078596]
# FIXME: Remove float cast after TensorFlow support removed

Check notice on line 177 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L177

unresolved comment '# FIXME: Remove float cast after TensorFlow support removed' (C100)
assert float(observed_limit) == pytest.approx(1.01156939)
# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed

Check notice on line 179 in tests/test_infer.py

View check run for this annotation

codefactor.io / CodeFactor

tests/test_infer.py#L179

unresolved comment '# FIXME: Can use expected_limits == pytest.approx([...]) after TensorFlow support removed' (C100)
assert np.allclose(
expected_limits, [0.55988001, 0.75702336, 1.06234693, 1.50116923, 2.05078596]
)


Expand Down

0 comments on commit e815612

Please sign in to comment.