@@ -216,35 +216,23 @@ def test_histogram(self):
216
216
g [...] = np .arange (40 ).reshape (5 , 8 ) + 253.15
217
217
g .override_units ("K" , inplace = True )
218
218
219
+ atol = 1e-15
220
+
219
221
# 1-d histogram
220
222
indices = f .digitize (10 )
221
223
h = cf .histogram (indices )
222
224
self .assertTrue ((h .array == [9 , 7 , 9 , 4 , 5 , 1 , 1 , 1 , 2 , 1 ]).all )
223
225
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 ))
243
230
244
231
# 2-d histogram
245
232
indices_t = g .digitize (5 )
246
233
h = cf .histogram (indices , indices_t )
247
234
self .assertEqual (h .Units , cf .Units ())
235
+ # The -1 values correspond to missing values in h
248
236
self .assertTrue (
249
237
(
250
238
h .array
@@ -260,73 +248,13 @@ def test_histogram(self):
260
248
261
249
h = cf .histogram (indices , indices_t , density = True )
262
250
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
329
255
)
256
+ integral = (h * bin_measures ).sum ()
257
+ self .assertTrue (np .allclose (integral .array , 1 , rtol = 0 , atol = atol ))
330
258
331
259
332
260
if __name__ == "__main__" :
0 commit comments