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