Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhassell committed Jul 16, 2024
1 parent f8c7575 commit f3ad202
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 86 deletions.
3 changes: 2 additions & 1 deletion cf/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ def histogram(*digitized, density=False):

if density:
# Get the measure of each bin. This is the outer product of
# the cell sizes of each dimension coordinate construct.
# the cell sizes of each dimension coordinate construct (the
# dimension coordinate constructs define the bins).
data_axes = out.get_data_axes()
dim = out.dimension_coordinate(filter_by_axis=(data_axes[0],))
bin_measures = dim.cellsize
Expand Down
98 changes: 13 additions & 85 deletions cf/test/test_Maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,35 +216,23 @@ def test_histogram(self):
g[...] = np.arange(40).reshape(5, 8) + 253.15
g.override_units("K", inplace=True)

atol = 1e-15

# 1-d histogram
indices = f.digitize(10)
h = cf.histogram(indices)
self.assertTrue((h.array == [9, 7, 9, 4, 5, 1, 1, 1, 2, 1]).all)
h = cf.histogram(indices, density=True)
self.assertTrue(
(
h.array
== [
15.734265734265733,
12.237762237762242,
15.734265734265733,
6.9930069930069925,
8.74125874125874,
1.748251748251749,
1.7482517482517475,
1.748251748251749,
3.496503496503498,
1.748251748251744,
]
).all
)
integral = (h * h.dimension_coordinate().cellsize).sum()
self.assertEqual(integral.array, 1)
# Check that integral is 1
bin_measures = h.dimension_coordinate().cellsize
integral = (h * bin_measures).sum()
self.assertTrue(np.allclose(integral.array, 1, rtol=0, atol=atol))

# 2-d histogram
indices_t = g.digitize(5)
h = cf.histogram(indices, indices_t)
self.assertEqual(h.Units, cf.Units())
# The -1 values correspond to missing values in h
self.assertTrue(
(
h.array
Expand All @@ -260,73 +248,13 @@ def test_histogram(self):

h = cf.histogram(indices, indices_t, density=True)
self.assertEqual(h.Units, cf.Units())
self.assertTrue(
(
h.array
== [
[
0.6724045185583661,
0.6724045185583664,
0.44826967903891074,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
],
[
0.22413483951945457,
0.2241348395194546,
0.44826967903890913,
0.22413483951945457,
0.6724045185583637,
-1,
-1,
-1,
-1,
-1,
],
[
0.22413483951945457,
-1,
-1,
0.22413483951945457,
-1,
0.2241348395194547,
0.22413483951945448,
0.2241348395194547,
0.4482696790389094,
0.224134839519454,
],
[
0.4482696790389124,
0.22413483951945626,
0.2241348395194562,
0.4482696790389124,
0.4482696790389124,
-1,
-1,
-1,
-1,
-1,
],
[
0.4482696790389059,
0.44826967903890597,
0.8965393580778118,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
],
]
).all()
# Check that integral is 1
bin_measures = h.dimension_coordinate("air_temperature").cellsize
bin_measures.outerproduct(
h.dimension_coordinate("specific_humidity").cellsize, inplace=True
)
integral = (h * bin_measures).sum()
self.assertTrue(np.allclose(integral.array, 1, rtol=0, atol=atol))


if __name__ == "__main__":
Expand Down

0 comments on commit f3ad202

Please sign in to comment.