Skip to content

Commit 414b354

Browse files
committed
Up docs
1 parent b9cee0a commit 414b354

3 files changed

Lines changed: 37 additions & 30 deletions

File tree

docs/source/accessor.rst

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Accessor
66

77
An :external+xarray:doc:`accessor <internals/extending-xarray>` is provided to
88
ease manipulation and analysis of the histogram outputs. Simply import
9-
:mod:`xarray_histogram.accessor` to register it. It will then be available for
10-
all DataArrays that meet some conditions (see below), under the ``hist``
11-
attribute. It gives access to a number of methods. ::
9+
:mod:`xarray_histogram.accessor` to register it. It will then be available under
10+
the ``hist`` attribute for all DataArrays that meet some conditions (see below).
11+
It gives access to a number of methods. ::
1212

1313
import xarray_histogram as xh
1414
import xarray_histogram.accessor
@@ -17,15 +17,17 @@ attribute. It gives access to a number of methods. ::
1717

1818
h.hist.median()
1919

20-
Operations are vectorized [#vector]_, so that you can apply them to entire
20+
Operations are vectorized so that you can apply them to entire
2121
arrays of histograms. For instance for data defined along time, latitude and
2222
longitude, we can compute one histogram per time-step::
2323

2424
>>> h = xh.histogram(data, dims=["lon", "lat"])
2525
>>> h.hist.median()
2626
will be of dimensions ("time",)
2727

28-
.. [#vector] Computations are automatically vectorized in Python with
28+
.. note::
29+
30+
Computations are automatically vectorized in Python with
2931
:func:`xarray.apply_ufunc`, which is not efficient for a large number of
3032
histograms.
3133

@@ -56,7 +58,7 @@ Each bins coordinate may contain attributes:
5658

5759
Those conventions are coherent with the output of
5860
``xarray_histogram.histogram*``, so if you use this package functions you
59-
should not have to worry. The names of the array and coordinates is also
61+
should not have to worry. The names of the array and coordinates are also
6062
consistent with that of :external+xhistogram:doc:`xhistogram <index>`
6163
(although coordinates attributes will be missing).
6264

@@ -85,11 +87,11 @@ excluded by passing ``flow=False``.
8587
bins. The overflow bins centers are the same as their position (``np.inf`` for
8688
instance).
8789

88-
* :meth:`~.HistDataArrayAccessor.areas` returns the areas of multidimensional
89-
bins. This is the product of the widths of all bins. Only some variable can be
90-
specified. The areas of points that correspond to a flow bin in at least one
91-
dimension is equal to one. For instance for a 2D-histogram with underflow and
92-
overflow bins, all the borders of the 2D array for areas will be equal to 1.
90+
* :meth:`~.HistDataArrayAccessor.areas` returns the areas corresponding to each
91+
histogram point. This is the outer product of the widths of all bins. The
92+
areas of points that correspond to a flow bin in at least one dimension is
93+
equal to one. For instance for a 2D-histogram with underflow and overflow
94+
bins, all the borders of the 2D array for areas will be equal to 1.
9395

9496
To remove flow bins, :meth:`~.HistDataArrayAccessor.remove_flow` will returns a
9597
new histogram DataArray without the flow bins of the given variables (by default
@@ -102,9 +104,9 @@ Bins transform
102104

103105
Arbitrary functions can be applied to bins with
104106
:meth:`~.HistDataArrayAccessor.apply_func`. The result is equivalent to
105-
computing an histogram of ``func(data["variable"])``. The function must
107+
computing the histogram of ``func(data["variable"])``. The function must
106108
transform the N+1 edges given as a DataArray. There is no need to account for
107-
the *right_edge* attribute.
109+
the *right_edge* attribute or flow bins.
108110

109111
For instance, :meth:`~.HistDataArrayAccessor.scale` scales bins by a given
110112
factor. It essential does ``hist.apply_func(lambda edges: edges * factor)``
@@ -115,7 +117,7 @@ Normalization
115117

116118
The histogram can be normalized to a probability density function if not
117119
already, using :meth:`~.HistDataArrayAccessor.normalize`. Note that for a
118-
N-dimensional histogram, this function can normalize only along some variables.
120+
N-dimensional histogram, this function can normalize along only some variables.
119121

120122
The accessor considers the histogram normalized or not given the name of its
121123
DataArray: normalized if named ``<variables>_pdf`` and non-normalized

docs/source/usage.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,21 @@ supported. Instead, use a
5050
</user-guide/axes.rst#regular-axis-transforms>` applied) is more efficient:
5151
it avoids having to use binary search to find in which bin a value falls.
5252

53-
Some basic examples of axis include::
53+
Some examples of axis include::
5454

5555
import boost_histogram.axis as bha
5656

5757
# regular width bins
5858
bha.Regular(200, 0., 10.)
59+
5960
# logarithmically spaced bins (without performance loss)
6061
bha.Regular(200, 1e-3, 10., transform=bha.transform.log)
62+
6163
# integer bins
6264
bha.Integer(0, 20)
65+
6366
# boolean
64-
bha.Integer(0, 2, underflow=False, overflow=True)
67+
bha.Integer(0, 2, underflow=False, overflow=False)
6568

6669
Over/underflow
6770
==============
@@ -81,7 +84,7 @@ coordinates values for the underflow and overflow bins will be set to
8184
Output
8285
======
8386

84-
All three functions return a simple :class:`xarray.DataArray`. Its name is
87+
All three functions return a single :class:`xarray.DataArray`. Its name is
8588
``<variable names separated by underscores>_histogram`` (so for instance
8689
``x_y_histogram``). The bins edges are contained in coordinates named
8790
``<variable>_bins``. The right edge of the last bin is stored in a coordinate

src/xarray_histogram/accessor.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@ class HistDataArrayAccessor:
2424
.. rubric:: Validity
2525
2626
* The coordinates of the bins must be named ``<variable>_bins``.
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``.
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``.
3131
3232
Each bins coordinate may contain attributes:
3333
3434
* ``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.
35+
the accessor will assume the bins are regularly spaced and will try to infer the
36+
rightmost edge.
37+
* ``right_edge``: the rightmost edge position, only necessary for Regular and
38+
Variable bins.
39+
* ``underflow`` and ``overflow``: booleans that indicate if the corresponding flow
40+
bins are present. If not present, will assume no flow bins.
4041
4142
.. rubric:: Backend for computations
4243
@@ -572,10 +573,11 @@ def interval(self, confidence: float, variable: str | None = None) -> xr.Dataset
572573
return output
573574

574575

575-
def remove_flow(x: xr.DataArray) -> xr.DataArray:
576-
overflow = x.attrs.get("overflow", False)
577-
underflow = x.attrs.get("underflow", False)
578-
out = x[slice(1 if underflow else 0, -1 if overflow else None)]
576+
def remove_flow(coord: xr.DataArray) -> xr.DataArray:
577+
"""Remove flow bins from a coordinate."""
578+
overflow = coord.attrs.get("overflow", False)
579+
underflow = coord.attrs.get("underflow", False)
580+
out = coord[slice(1 if underflow else 0, -1 if overflow else None)]
579581
out.attrs["underflow"] = False
580582
out.attrs["overflow"] = True
581583
return out

0 commit comments

Comments
 (0)