|
1 | 1 | """
|
2 | 2 | Tests for the entropy function
|
3 | 3 | """
|
4 |
| - |
| 4 | +import pytest |
5 | 5 | import numpy as np
|
6 |
| -import unittest |
7 | 6 |
|
8 | 7 | from entropy import entropy
|
9 | 8 |
|
10 | 9 |
|
11 |
| -class TestEntropy(unittest.TestCase): |
12 |
| - |
13 |
| - def test_smoke(self): |
14 |
| - """ |
15 |
| - Simple smoke test to make sure function runs. |
16 |
| - """ |
17 |
| - entropy([1]) |
| 10 | +def test_smoke(): |
| 11 | + """ |
| 12 | + Simple smoke test to make sure function runs. |
| 13 | + """ |
| 14 | + entropy([1]) |
| 15 | + return |
| 16 | + |
| 17 | +def test_args_dont_sum_to_1(): |
| 18 | + """ |
| 19 | + Edge test to make sure the function throws a ValueError |
| 20 | + when the input probabilities do not sum to one. |
| 21 | + """ |
| 22 | + with pytest.raises( |
| 23 | + ValueError, match="The list of input probabilities does not sum to 1" |
| 24 | + ): |
| 25 | + entropy([.9, .9]) |
| 26 | + return |
| 27 | + |
| 28 | +def test_args_out_of_range(): |
| 29 | + """ |
| 30 | + Edge tst to make sure the function throws a ValueError |
| 31 | + when the input probabilities are < 0 or > 1. |
| 32 | + """ |
| 33 | + with pytest.raises(ValueError, match="At least one input is out of range"): |
| 34 | + entropy([-1, 2]) |
| 35 | + return |
| 36 | + |
| 37 | +def test_four_equal_likelihood_states(): |
| 38 | + """ |
| 39 | + One shot test using the known case of four states with |
| 40 | + equal likelihood of occurrence. Should return 2 bits. |
| 41 | + """ |
| 42 | + np.testing.assert_allclose(entropy([0.25, 0.25, 0.25, 0.25]), 2.) |
| 43 | + return |
| 44 | + |
| 45 | +def test_equal_probability(): |
| 46 | + """ |
| 47 | + Pattern test using the known relationship of equal probabilities |
| 48 | + and predefined result. |
| 49 | + """ |
| 50 | + def test(count): |
| 51 | + prob = 1.0/count |
| 52 | + ps = np.repeat(prob, count) |
| 53 | + assert np.isclose(entropy(ps), -np.log2(prob)) |
18 | 54 | return
|
19 | 55 |
|
20 |
| - def test_args_dont_sum_to_1(self): |
21 |
| - """ |
22 |
| - Edge test to make sure the function throws a ValueError |
23 |
| - when the input probabilities do not sum to one. |
24 |
| - """ |
25 |
| - with self.assertRaises(ValueError): |
26 |
| - entropy([.9, .9]) |
27 |
| - return |
28 |
| - |
29 |
| - def test_args_out_of_range(self): |
30 |
| - """ |
31 |
| - Edge tst to make sure the function throws a ValueError |
32 |
| - when the input probabilities are < 0 or > 1. |
33 |
| - """ |
34 |
| - with self.assertRaises(ValueError): |
35 |
| - entropy([-1, 2]) |
36 |
| - return |
37 |
| - |
38 |
| - def test_four_equal_likelihood_states(self): |
39 |
| - """ |
40 |
| - One shot test using the known case of four states with |
41 |
| - equal likelihood of occurrence. Should return 2 bits. |
42 |
| - """ |
43 |
| - assert np.isclose(entropy([0.25, 0.25, 0.25, 0.25]), 2.) |
44 |
| - return |
45 |
| - |
46 |
| - def test_equal_probability(self): |
47 |
| - """ |
48 |
| - Pattern test using the known relationship of equal probabilities |
49 |
| - and predefined result. |
50 |
| - """ |
51 |
| - def test(count): |
52 |
| - prob = 1.0/n |
53 |
| - ps = np.repeat(prob, n) |
54 |
| - assert np.isclose(entropy(ps), -np.log2(prob)) |
55 |
| - return |
56 |
| - |
57 |
| - # run the test for a large number of iterations |
58 |
| - for n in range(10, 100000, 10000): |
59 |
| - test(n) |
60 |
| - return |
| 56 | + # run the test for a large number of iterations |
| 57 | + for count in range(10, 100000, 10000): |
| 58 | + test(count) |
| 59 | + return |
0 commit comments