Skip to content

Commit 6374ba3

Browse files
authored
docs: mention stacked setting for histograms (#649)
1 parent d82f1d8 commit 6374ba3

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

docs/usage/accumulators.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,8 @@ Most of the accumulators (except Sum) support a View. This is what is returned f
159159
a histogram when ``.view()`` is requested. This is a structured NumPy ndarray, with a few small
160160
additions to make them easier to work with. Like a NumPy recarray, you can access the fields with
161161
attributes; you can even access (but not set) computed attributes like ``.variance``. A view will
162-
also return an accumulator instance if you select a single item.
162+
also return an accumulator instance if you select a single item. You can set a view's contents
163+
with a stacked array, and each item in the stack will be used for the (computed) values that a
164+
normal constructor would take. For example, WeighedMean can take an array with a final
165+
dimension four long, with ``sum_of_weights``, ``sum_of_weights_squared``, ``value``, and ``variance``
166+
elements, even though several of these values are computed from the internal representation.

docs/usage/histogram.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,20 @@ The primary values from a histogram are always available as ``.values()``. The v
2424
Views
2525
^^^^^
2626

27-
While Histograms do conform to the Python buffer protocol, the best way to get access to the raw contents of a histogram as a NumPy array is with ``.view()``. This way you can optionally pass ``flow=True`` to get the flow bins, and if you have an accumulator storage, you will get a View, which is a slightly augmented ndarrray subclass (see :ref:`usage-accumulators`).
27+
While Histograms do conform to the Python buffer protocol, the best way to get access to the raw contents of a histogram as a NumPy array is with ``.view()``. This way you can optionally pass ``flow=True`` to get the flow bins, and if you have an accumulator storage, you will get a View, which is a slightly augmented ndarrray subclass (see :ref:`usage-accumulators`). Views support setting as well for non-computed properties; you can use an expression like this to set the values of an accumulator storage:
2828

29+
.. code:: python3
30+
31+
h.view().value = values
32+
33+
34+
You can also used stacked arrays (N+1 dimensional) to set a histogram's contents. This is especially useful if you need to set a computed value, like variance on a Mean/WeightedMean storage, which cannot be set using the above method:
35+
36+
.. code:: python3
37+
38+
h[...] = np.stack([values, variances], axis=-1)
39+
40+
If you leave endpoints off (such as with ``...`` above), then you can match the size with or without flow bins.
2941

3042
Operations
3143
^^^^^^^^^^

0 commit comments

Comments
 (0)