Skip to content

Commit

Permalink
Flat parametrize test args
Browse files Browse the repository at this point in the history
  • Loading branch information
kddubey committed Nov 1, 2024
1 parent 3abd478 commit ab0aa93
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 49 deletions.
6 changes: 3 additions & 3 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ abstract: >-
Completion After Prompt Probability. Make your LLM make a
choice
license: Apache-2.0
commit: a46dd22
version: 0.9.0
date-released: '2024-02-01'
commit: ' 3abd478'
version: 0.9.5
date-released: '2024-10-28'
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2023-2023 Kush Dubey
Copyright 2023-2024 Kush Dubey

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = "CAPPr"
copyright = "2023, kddubey"
author = "kddubey"
copyright = "2023-2024, Kush Dubey"
author = "Kush Dubey"

version = __version__
release = version
Expand Down
73 changes: 30 additions & 43 deletions tests/utils/test_utils_classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,26 @@ def test_agg_log_probs():
_ = classify.agg_log_probs([[[[0, 1]]]])


@pytest.mark.parametrize("likelihoods", ([[4, 1], [1, 4]],))
@pytest.mark.parametrize("prior", (None, [1 / 2, 1 / 2], [1 / 3, 2 / 3]))
@pytest.mark.parametrize("normalize", (True, False))
def test_posterior_prob_2d(likelihoods, prior, normalize):
# TODO: clean this up
@pytest.mark.parametrize(
"likelihoods, prior, normalize, expected",
[
# Case 1: Prior = None
([[4, 1], [1, 4]], None, True, [[4 / 5, 1 / 5], [1 / 5, 4 / 5]]),
([[4, 1], [1, 4]], None, False, [[4, 1], [1, 4]]),
# Case 2: Prior = [1 / 2, 1 / 2]
([[4, 1], [1, 4]], [1 / 2, 1 / 2], True, [[4 / 5, 1 / 5], [1 / 5, 4 / 5]]),
([[4, 1], [1, 4]], [1 / 2, 1 / 2], False, [[2, 0.5], [0.5, 2]]),
# Case 3: Prior = [1 / 3, 2 / 3]
([[4, 1], [1, 4]], [1 / 3, 2 / 3], True, [[2 / 3, 1 / 3], [1 / 9, 8 / 9]]),
([[4, 1], [1, 4]], [1 / 3, 2 / 3], False, [[4 / 3, 2 / 3], [1 / 3, 8 / 3]]),
],
)
def test_posterior_prob_2d(likelihoods, prior, normalize, expected):
posteriors = classify.posterior_prob(
likelihoods, axis=1, prior=prior, normalize=normalize
)
if prior == [1 / 2, 1 / 2]:
if normalize:
assert np.all(np.isclose(posteriors, [[4 / 5, 1 / 5], [1 / 5, 4 / 5]]))
else:
assert np.all(posteriors == np.array(likelihoods) / 2)
elif prior is None:
if normalize:
assert np.all(np.isclose(posteriors, [[4 / 5, 1 / 5], [1 / 5, 4 / 5]]))
else:
assert np.all(posteriors == likelihoods)
elif prior == [1 / 3, 2 / 3]:
if normalize:
assert np.all(np.isclose(posteriors, [[2 / 3, 1 / 3], [1 / 9, 8 / 9]]))
else:
assert np.all(np.isclose(posteriors, [[4 / 3, 2 / 3], [1 / 3, 8 / 3]]))
else:
raise ValueError("nooo")
# Using np.allclose for floating-point precision comparison
assert np.allclose(posteriors, expected)


def test_posterior_prob_2d_or_mixed_prior():
Expand Down Expand Up @@ -147,27 +142,19 @@ def test_posterior_prob_2d_or_mixed_prior():
_ = classify.posterior_prob(likelihoods, axis=1, normalize=[True, True, False])


@pytest.mark.parametrize("likelihoods", (np.array([4, 1]),))
@pytest.mark.parametrize("prior", (None, [1 / 2, 1 / 2], [1 / 3, 2 / 3]))
@pytest.mark.parametrize("normalize", (True, False))
def test_posterior_prob_1d(likelihoods, prior, normalize):
@pytest.mark.parametrize(
"likelihoods, prior, normalize, expected",
[
(np.array([4, 1]), None, True, [4 / 5, 1 / 5]),
(np.array([4, 1]), None, False, [4, 1]),
(np.array([4, 1]), [1 / 2, 1 / 2], True, [4 / 5, 1 / 5]),
(np.array([4, 1]), [1 / 2, 1 / 2], False, [2, 0.5]),
(np.array([4, 1]), [1 / 3, 2 / 3], True, [2 / 3, 1 / 3]),
(np.array([4, 1]), [1 / 3, 2 / 3], False, [4 / 3, 2 / 3]),
],
)
def test_posterior_prob_1d_(likelihoods, prior, normalize, expected):
posteriors = classify.posterior_prob(
likelihoods, axis=0, prior=prior, normalize=normalize
)
if prior == [1 / 2, 1 / 2]: # hard-coded b/c idk how to engineer tests
if normalize:
assert np.all(np.isclose(posteriors, [4 / 5, 1 / 5]))
else:
assert np.all(posteriors == np.array(likelihoods) / 2)
elif prior is None:
if normalize:
assert np.all(np.isclose(posteriors, [4 / 5, 1 / 5]))
else:
assert np.all(posteriors == likelihoods)
elif prior == [1 / 3, 2 / 3]:
if normalize:
assert np.all(np.isclose(posteriors, [2 / 3, 1 / 3]))
else:
assert np.all(np.isclose(posteriors, [4 / 3, 2 / 3]))
else:
raise ValueError("nooo")
assert np.allclose(posteriors, expected)

0 comments on commit ab0aa93

Please sign in to comment.