Skip to content

Commit 00f90e7

Browse files
committed
Use integers for under/over-flow attributes
otherwise NetCDF will complain
1 parent bf120ab commit 00f90e7

5 files changed

Lines changed: 29 additions & 22 deletions

File tree

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
### 0.2.1
3+
4+
- Fix: under/over flow attributes are int instead of bool to conform with NetCDF
5+
26
## 0.2.0
37

48
- Use common function for normalization in histogramdd and accessor

docs/source/accessor.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ Each bins coordinate may contain attributes:
5353
to infer the rightmost edge.
5454
* ``right_edge``: the rightmost edge position, only necessary for Regular and
5555
Variable bins.
56-
* ``underflow`` and ``overflow``: booleans that indicate if the corresponding
57-
flow bins are present. If not present, will assume no flow bins.
56+
* ``underflow`` and ``overflow``: integers that indicate if the corresponding
57+
flow bins are present (0: not present, 1: present). If not specified, will
58+
assume no flow bins.
59+
60+
.. note::
61+
62+
We use integers instead of booleans to conform with NetCDF attributes.
5863

5964
Those conventions are coherent with the output of
6065
``xarray_histogram.histogram*``, so if you use this package functions you

src/xarray_histogram/accessor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HistDataArrayAccessor:
3636
rightmost edge.
3737
* ``right_edge``: the rightmost edge position, only necessary for Regular and
3838
Variable bins.
39-
* ``underflow`` and ``overflow``: booleans that indicate if the corresponding flow
39+
* ``underflow`` and ``overflow``: integers that indicate if the corresponding flow
4040
bins are present. If not present, will assume no flow bins.
4141
4242
.. rubric:: Backend for computations
@@ -578,6 +578,6 @@ def remove_flow(coord: xr.DataArray) -> xr.DataArray:
578578
overflow = coord.attrs.get("overflow", False)
579579
underflow = coord.attrs.get("underflow", False)
580580
out = coord[slice(1 if underflow else 0, -1 if overflow else None)]
581-
out.attrs["underflow"] = False
582-
out.attrs["overflow"] = True
581+
out.attrs["underflow"] = 0
582+
out.attrs["overflow"] = 0
583583
return out

src/xarray_histogram/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ def get_coord(name: str, ax: bh.axis.Axis, dtype: np.dtype, flow: bool) -> xr.Da
528528
"""
529529
underflow = flow and ax.traits.underflow
530530
overflow = flow and ax.traits.overflow
531-
attrs = dict(bin_type=type(ax).__name__, underflow=underflow, overflow=overflow)
531+
attrs = dict(
532+
bin_type=type(ax).__name__, underflow=int(underflow), overflow=int(overflow)
533+
)
532534

533535
if isinstance(ax, bh.axis.Integer):
534536
if dtype.kind not in "uib":

tests/test_histogram.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def test_regular(self, underflow: bool, overflow: bool):
8787
assert coord.attrs["bin_type"] == "Regular"
8888
assert coord.size == ax.extent
8989
assert coord.attrs["right_edge"] == 1.0
90-
assert coord.attrs["underflow"] is underflow
91-
assert coord.attrs["overflow"] is overflow
90+
assert coord.attrs["underflow"] is int(underflow)
91+
assert coord.attrs["overflow"] is int(overflow)
9292

9393
slc = slice(1 if underflow else 0, -1 if overflow else None)
9494
assert_allclose(coord[slc], ax.edges[:-1])
@@ -110,8 +110,8 @@ def test_regular_int(self, underflow: bool, overflow: bool):
110110
assert coord.attrs["bin_type"] == "Regular"
111111
assert coord.size == ax.extent
112112
assert coord.attrs["right_edge"] == 10.0
113-
assert coord.attrs["underflow"] is underflow
114-
assert coord.attrs["overflow"] is overflow
113+
assert coord.attrs["underflow"] is int(underflow)
114+
assert coord.attrs["overflow"] is int(overflow)
115115

116116
slc = slice(1 if underflow else 0, -1 if overflow else None)
117117
assert_allclose(coord[slc], ax.edges[:-1])
@@ -133,8 +133,8 @@ def test_int(self, underflow: bool, overflow: bool):
133133
assert coord.attrs["bin_type"] == "Integer"
134134
assert coord.size == ax.extent
135135
assert "right_edge" not in coord.attrs
136-
assert coord.attrs["underflow"] is underflow
137-
assert coord.attrs["overflow"] is overflow
136+
assert coord.attrs["underflow"] is int(underflow)
137+
assert coord.attrs["overflow"] is int(overflow)
138138

139139
slc = slice(1 if underflow else 0, -1 if overflow else None)
140140
assert_allclose(coord[slc], ax.edges[:-1])
@@ -153,8 +153,8 @@ def test_intcat(self, overflow: bool):
153153
assert coord.size == 5 if overflow else 4
154154
assert np.isdtype(coord.dtype, kind="signed integer")
155155
assert coord.attrs["bin_type"] == "IntCategory"
156-
assert coord.attrs["underflow"] is False
157-
assert coord.attrs["overflow"] is overflow
156+
assert coord.attrs["underflow"] == 0
157+
assert coord.attrs["overflow"] is int(overflow)
158158
if overflow:
159159
assert_allclose(coord, [2, 5, 8, 7, np.iinfo(coord.dtype).max])
160160
else:
@@ -172,8 +172,8 @@ def test_int_bool(self, underflow: bool, overflow: bool):
172172
assert coord.attrs["bin_type"] == "Integer"
173173
assert coord.size == ax.extent
174174
assert "right_edge" not in coord.attrs
175-
assert coord.attrs["underflow"] is underflow
176-
assert coord.attrs["overflow"] is overflow
175+
assert coord.attrs["underflow"] is int(underflow)
176+
assert coord.attrs["overflow"] is int(overflow)
177177
if underflow or overflow:
178178
assert np.isdtype(coord.dtype, kind="signed integer")
179179
slc = slice(1 if underflow else 0, -1 if overflow else None)
@@ -197,8 +197,8 @@ def test_intcat_bool(self, overflow: bool):
197197
coord = hist["var1_bins"]
198198
assert coord.size == 3 if overflow else 2
199199
assert coord.attrs["bin_type"] == "IntCategory"
200-
assert coord.attrs["underflow"] is False
201-
assert coord.attrs["overflow"] is overflow
200+
assert coord.attrs["underflow"] == 0
201+
assert coord.attrs["overflow"] is int(overflow)
202202
if overflow:
203203
assert np.isdtype(coord.dtype, kind="signed integer")
204204
assert_allclose(coord[:-1], [0, 1])
@@ -515,7 +515,3 @@ def test_partial_dask(self):
515515
ref = get_ref_hist(x, y, axes=axes)
516516

517517
assert_allclose(hist.to_numpy(), ref, atol=1, rtol=1e-6)
518-
519-
520-
class TestFlowBins:
521-
pass

0 commit comments

Comments
 (0)