44various histogram manipulations.
55"""
66
7- import operator
8- import typing as t
97from collections import abc
10- from functools import reduce
118
129import numpy as np
1310import xarray as xr
@@ -26,22 +23,24 @@ class HistDataArrayAccessor:
2623
2724 .. rubric:: Validity
2825
29- They are some conditions for the accessor to be accessible:
30-
3126 * The coordinates of the bins must be named ``<variable>_bins``.
32- * Each bins coordinates must contain an attribute named `right_edge`, corresponding
33- to the right edge of the last bin.
34- * The array must be named as ``<variable(s)_name>_<histogram or pdf>``. `histogram`
35- if it is *not* normalized, and `pdf` if it is normalized as a probability density
36- function. If the histogram is multi-dimensional, the variables names must be
37- separated by underscores. For instance: ``Temp_Sal_histogram``.
27+ * The array must be named as ``<variable(s)_name>_<histogram or pdf>``. histogram*
28+ *if it is not normalized, and *pdf* if it is normalized as a probability density
29+ *function. If the histogram is multi-dimensional, the variables names must be
30+ *separated by underscores. For instance: ``Temp_Sal_histogram``.
31+
32+ Each bins coordinate may contain attributes:
3833
39- Those conventions are coherent with the output of :func:`.core.histogram`, so if you
40- use this function you should not have to worry.
34+ * ``bin_type``: the class name of the Boost axis type that was used. If not present,
35+ the accessor will assume the bins are regularly spaced and will try to infer the
36+ rightmost edge. * ``right_edge``: the rightmost edge position, only necessary for
37+ Regular and Variable bins. * ``underflow`` and ``overflow``: booleans that indicate
38+ if the corresponding flow bins are present. If not present, will assume no flow
39+ bins.
4140
4241 .. rubric:: Backend for computations
4342
44- Most computations are actually delegated to :class:`scipy.stats.rv_histogram`.
43+ Statistics computations are actually delegated to :class:`scipy.stats.rv_histogram`.
4544 Therefore, it does not support chunking along the bins dimensions (which should not
4645 be a problem in most cases).
4746 """
@@ -107,8 +106,8 @@ def _check_name(self) -> None:
107106 def _check_bins (self ) -> None :
108107 """Check validity of bins.
109108
110- Check the variables names have corresponding bins dimensions, and if the
111- coordinates contain a `right_edge` attribute ( if not try to infer it) .
109+ Check the variables names have corresponding bins dimensions, and infer right
110+ edge if necessary .
112111 """
113112 obj = self ._obj
114113 for var in self .variables :
@@ -134,7 +133,7 @@ def is_normalized(self) -> bool:
134133 return self ._variable_type == "pdf"
135134
136135 def _variable (self , var : str | None ) -> str :
137- """Return variable argument."""
136+ """Sanitize variable argument."""
138137 if var is None :
139138 if len (self .variables ) == 1 :
140139 return self .variables [0 ]
@@ -145,8 +144,16 @@ def _variable(self, var: str | None) -> str:
145144 )
146145 return var
147146
148- """Return bins coordinates for a given variable."""
149147 def bins (self , variable : str | None = None , flow : bool = True ) -> xr .DataArray :
148+ """Return bins coordinates for a given variable.
149+
150+ Parameters
151+ ----------
152+ variable
153+ Can be omitted for 1D histograms.
154+ flow
155+ Remove flow bins if False.
156+ """
150157 variable = self ._variable (variable )
151158 dim = _bins_name (variable )
152159 coord = self ._obj .coords [dim ]
@@ -155,15 +162,36 @@ def bins(self, variable: str | None = None, flow: bool = True) -> xr.DataArray:
155162 return coord
156163
157164 def edges (self , variable : str | None = None , flow : bool = True ) -> xr .DataArray :
165+ """Return the edges of the bins (including the right most edge).
166+
167+ Not supported for bins of the discrete types "IntCategory" and "StrCategory".
168+
169+ Parameters
170+ ----------
171+ variable
172+ Can be omitted for 1D histograms.
173+ flow
174+ Remove flow bins if False.
175+ """
158176 edges = get_edges (self .bins (variable ))
159177 if not flow :
160178 edges = remove_flow (edges )
161179 return edges
162180
163- """Return the edges of the bins (including the right most edge)."""
164181 def centers (self , variable : str | None = None , flow : bool = True ) -> xr .DataArray :
182+ """Return the center of all bins.
183+
184+ Not supported for bin type "StrCategory". IntCategory bins centers are
185+ ``bins+0.5``. The centers of flow bins are the same as their position
186+ (``np.inf`` for instance).
165187
166- """Return the center of all bins."""
188+ Parameters
189+ ----------
190+ variable
191+ Can be omitted for 1D histograms.
192+ flow
193+ Remove flow bins if False.
194+ """
167195 variable = self ._variable (variable )
168196 dim = _bins_name (variable )
169197
@@ -182,19 +210,39 @@ def center(coord):
182210
183211 return self ._apply_to_coord (center , variable , keep_right_edge = True , flow = flow )
184212
185- """Return the width of all bins."""
186213 def widths (self , variable : str | None = None , flow : bool = True ) -> xr .DataArray :
214+ """Return the widths of all bins.
187215
216+ Widths of flow bins and StrCategory are 1.
217+
218+ Parameters
219+ ----------
220+ variable
221+ Can be omitted for 1D histograms.
222+ flow
223+ Remove flow bins if False.
224+ """
188225 widths = get_widths (self .bins (variable ))
189226 if not flow :
190227 widths = remove_flow (widths )
191228 return widths
229+
192230 def areas (
193231 self , variables : abc .Sequence [str ] | None = None , flow : bool = True
194232 ) -> xr .DataArray :
195233 """Return the areas of the bins.
196234
197- The product of the widths of all bins.
235+ The product of the widths of all specified bins.
236+ The areas of points that correspond to a flow bin in at least one dimension is
237+ equal to one.
238+
239+ Parameters
240+ ----------
241+ variables
242+ Variables to include the corresponding bins. If left to None, all variables
243+ are used.
244+ flow
245+ Remove flow bins if False.
198246 """
199247 if variables is None :
200248 variables = self .variables
0 commit comments