Skip to content

Commit f3ad202

Browse files
committed
dev
1 parent f8c7575 commit f3ad202

File tree

2 files changed

+15
-86
lines changed

2 files changed

+15
-86
lines changed

cf/maths.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ def histogram(*digitized, density=False):
308308

309309
if density:
310310
# Get the measure of each bin. This is the outer product of
311-
# the cell sizes of each dimension coordinate construct.
311+
# the cell sizes of each dimension coordinate construct (the
312+
# dimension coordinate constructs define the bins).
312313
data_axes = out.get_data_axes()
313314
dim = out.dimension_coordinate(filter_by_axis=(data_axes[0],))
314315
bin_measures = dim.cellsize

cf/test/test_Maths.py

Lines changed: 13 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -216,35 +216,23 @@ def test_histogram(self):
216216
g[...] = np.arange(40).reshape(5, 8) + 253.15
217217
g.override_units("K", inplace=True)
218218

219+
atol = 1e-15
220+
219221
# 1-d histogram
220222
indices = f.digitize(10)
221223
h = cf.histogram(indices)
222224
self.assertTrue((h.array == [9, 7, 9, 4, 5, 1, 1, 1, 2, 1]).all)
223225
h = cf.histogram(indices, density=True)
224-
self.assertTrue(
225-
(
226-
h.array
227-
== [
228-
15.734265734265733,
229-
12.237762237762242,
230-
15.734265734265733,
231-
6.9930069930069925,
232-
8.74125874125874,
233-
1.748251748251749,
234-
1.7482517482517475,
235-
1.748251748251749,
236-
3.496503496503498,
237-
1.748251748251744,
238-
]
239-
).all
240-
)
241-
integral = (h * h.dimension_coordinate().cellsize).sum()
242-
self.assertEqual(integral.array, 1)
226+
# Check that integral is 1
227+
bin_measures = h.dimension_coordinate().cellsize
228+
integral = (h * bin_measures).sum()
229+
self.assertTrue(np.allclose(integral.array, 1, rtol=0, atol=atol))
243230

244231
# 2-d histogram
245232
indices_t = g.digitize(5)
246233
h = cf.histogram(indices, indices_t)
247234
self.assertEqual(h.Units, cf.Units())
235+
# The -1 values correspond to missing values in h
248236
self.assertTrue(
249237
(
250238
h.array
@@ -260,73 +248,13 @@ def test_histogram(self):
260248

261249
h = cf.histogram(indices, indices_t, density=True)
262250
self.assertEqual(h.Units, cf.Units())
263-
self.assertTrue(
264-
(
265-
h.array
266-
== [
267-
[
268-
0.6724045185583661,
269-
0.6724045185583664,
270-
0.44826967903891074,
271-
-1,
272-
-1,
273-
-1,
274-
-1,
275-
-1,
276-
-1,
277-
-1,
278-
],
279-
[
280-
0.22413483951945457,
281-
0.2241348395194546,
282-
0.44826967903890913,
283-
0.22413483951945457,
284-
0.6724045185583637,
285-
-1,
286-
-1,
287-
-1,
288-
-1,
289-
-1,
290-
],
291-
[
292-
0.22413483951945457,
293-
-1,
294-
-1,
295-
0.22413483951945457,
296-
-1,
297-
0.2241348395194547,
298-
0.22413483951945448,
299-
0.2241348395194547,
300-
0.4482696790389094,
301-
0.224134839519454,
302-
],
303-
[
304-
0.4482696790389124,
305-
0.22413483951945626,
306-
0.2241348395194562,
307-
0.4482696790389124,
308-
0.4482696790389124,
309-
-1,
310-
-1,
311-
-1,
312-
-1,
313-
-1,
314-
],
315-
[
316-
0.4482696790389059,
317-
0.44826967903890597,
318-
0.8965393580778118,
319-
-1,
320-
-1,
321-
-1,
322-
-1,
323-
-1,
324-
-1,
325-
-1,
326-
],
327-
]
328-
).all()
251+
# Check that integral is 1
252+
bin_measures = h.dimension_coordinate("air_temperature").cellsize
253+
bin_measures.outerproduct(
254+
h.dimension_coordinate("specific_humidity").cellsize, inplace=True
329255
)
256+
integral = (h * bin_measures).sum()
257+
self.assertTrue(np.allclose(integral.array, 1, rtol=0, atol=atol))
330258

331259

332260
if __name__ == "__main__":

0 commit comments

Comments
 (0)