|
2 | 2 |
|
3 | 3 | import numpy as np |
4 | 4 | import pytest |
5 | | -from numpy.testing import assert_allclose |
| 5 | +from numpy.testing import assert_allclose, assert_array_equal |
6 | 6 |
|
7 | 7 | from astropy.stats import ( |
8 | 8 | calculate_bin_edges, |
@@ -138,35 +138,42 @@ def test_histogram_output_knuth(): |
138 | 138 | def test_histogram_output(): |
139 | 139 | rng = np.random.default_rng(0) |
140 | 140 | X = rng.standard_normal(100) |
| 141 | + X_range = X_min, X_max = X.min(), X.max() |
141 | 142 |
|
142 | 143 | counts, bins = histogram(X, bins=10) |
143 | 144 | assert_allclose(counts, [2, 0, 12, 14, 14, 17, 16, 8, 9, 8]) |
144 | 145 |
|
145 | 146 | # fmt: off |
146 | | - assert_allclose(bins, [-2.32503077, -1.89228844, -1.4595461, -1.02680377, -0.59406143, |
| 147 | + assert_allclose(bins, [X_min, -1.89228844, -1.4595461, -1.02680377, -0.59406143, |
147 | 148 | -0.1613191, 0.27142324, 0.70416558, 1.13690791, 1.56965025, |
148 | | - 2.00239258]) |
| 149 | + X_max]) |
149 | 150 | # fmt: on |
150 | 151 |
|
151 | 152 | counts, bins = histogram(X, bins="scott") |
152 | 153 | assert_allclose(counts, [2, 14, 27, 25, 16, 16]) |
153 | 154 |
|
154 | 155 | # fmt: off |
155 | | - assert_allclose(bins, [-2.32503077, -1.59953424, -0.87403771, -0.14854117, 0.57695536, |
156 | | - 1.3024519, 2.02794843]) |
| 156 | + assert_allclose(bins, [X_min, -1.59953424, -0.87403771, -0.14854117, 0.57695536, |
| 157 | + 1.3024519, X_max]) |
157 | 158 | # fmt: on |
158 | 159 |
|
159 | 160 | counts, bins = histogram(X, bins="freedman") |
160 | 161 | assert_allclose(counts, [2, 11, 16, 18, 22, 14, 13, 4]) |
161 | 162 |
|
162 | 163 | # fmt: off |
163 | | - assert_allclose(bins, [-2.32503077, -1.74087192, -1.15671306, -0.5725542, 0.01160465, |
164 | | - 0.59576351, 1.17992237, 1.76408122, 2.34824008], rtol=2e-7) |
| 164 | + assert_allclose(bins, [X_min, -1.74087192, -1.15671306, -0.5725542, 0.01160465, |
| 165 | + 0.59576351, 1.17992237, 1.76408122, X_max], rtol=2e-7) |
165 | 166 | # fmt: on |
166 | 167 |
|
167 | 168 | counts, bins = histogram(X, bins="blocks") |
168 | 169 | assert_allclose(counts, [3, 97]) |
169 | | - assert_allclose(bins, [-2.32503077, -1.37136996, 2.00239258]) |
| 170 | + assert_allclose(bins, [X_min, -1.37136996, X_max]) |
| 171 | + |
| 172 | + |
| 173 | +def test_histogram_empty_data(): |
| 174 | + counts, bins = histogram([], bins=2) |
| 175 | + assert_allclose(counts, [0, 0]) |
| 176 | + assert_array_equal(bins, [0, 0.5, 1.0]) |
170 | 177 |
|
171 | 178 |
|
172 | 179 | def test_histogram_badargs(N=1000, rseed=0): |
|
0 commit comments