Histogramming, grouping, and binning#

Overview#

Histogramming (see sc.hist), grouping (using sc.groupby), and binning (see Binned data) all serve similar but slightly different purposes. Picking the optimal one of the three for a particular application may yield more natural code and better performance. Let us start by an example. Consider a table of scattered measurements:

[1]:
import numpy as np
import scipp as sc

N = 5000
values = 10 * np.random.rand(N)
table = sc.DataArray(
    data=sc.array(
        dims=['position'], unit=sc.units.counts, values=values, variances=values
    ),
    coords={
        'x': sc.array(dims=['position'], unit='m', values=np.random.rand(N)),
        'y': sc.array(dims=['position'], unit='m', values=np.random.rand(N)),
    },
)
table.values *= 1.0 / np.exp(5.0 * table.coords['x'].values)
sc.table(table['position', :5])
[1]:
CoordinatesData
x [m]y [m] [counts]
0.3540.1661.398±2.861
0.1930.7781.418±1.927
0.1060.3895.479±3.049
0.8990.9820.000±0.211
0.0270.3697.959±3.018

We may now be interested in the total intensity (counts) as a function of 'x'. There are three ways to do this:

[2]:
xbins = sc.linspace('x', 0, 1, num=40, unit='m')
ds = sc.Dataset(
    {
        'histogram': table.hist(x=xbins),
        'groupby': table.groupby('x', bins=xbins).sum('position'),
        'bin': table.bin(x=xbins).bins.sum(),
    }
)
ds.plot()
[2]:
../../_images/user-guide_binned-data_histogramming-grouping-and-binning_3_0.svg

In the above plot we can only see a single line, since the three solutions yield exactly the same result (neglecting floating-point rounding errors):

  • hist sorts data points into ‘x’ bins, summing immediately.

  • groupby groups by ‘x’ and then sums (on-the-fly) all data points falling in the same ‘x’ bin.

  • bin sorts data points into ‘x’ bins. Summing all rows in a bin yields the same result as grouping and summing directly.

So in this case we get equivalent results, but the application areas differ, as described in more detail in the following sections.

Histogramming#

scipp.hist directly sums the data and is efficient. Limitations are:

  • When histogramming in more than one dimension, the implementation uses sc.bin internally, which may be less efficient and uses more memory.

  • Can only apply “sum” or “nansum” to accumulate into a bin. scipp.nanhist is currently implemented differently and uses sc.bin internally. It therefore uses more memory and may be less efficient.

We can also histogram binned data (since binning preserves the 'y' coord), to create 2-D (or N-D) histograms:

[3]:
binned = table.bin(x=xbins)
hist = binned.hist(y=30)
hist.plot()
[3]:
../../_images/user-guide_binned-data_histogramming-grouping-and-binning_5_0.svg
[4]:
hist
[4]:
Show/Hide data repr Show/Hide attributes
scipp.DataArray (20.09 KB)
    • x: 39
    • y: 30
    • x
      (x [bin-edge])
      float64
      m
      0.0, 0.026, ..., 0.974, 1.0
      Values:
      array([0. , 0.02564103, 0.05128205, 0.07692308, 0.1025641 , 0.12820513, 0.15384615, 0.17948718, 0.20512821, 0.23076923, 0.25641026, 0.28205128, 0.30769231, 0.33333333, 0.35897436, 0.38461538, 0.41025641, 0.43589744, 0.46153846, 0.48717949, 0.51282051, 0.53846154, 0.56410256, 0.58974359, 0.61538462, 0.64102564, 0.66666667, 0.69230769, 0.71794872, 0.74358974, 0.76923077, 0.79487179, 0.82051282, 0.84615385, 0.87179487, 0.8974359 , 0.92307692, 0.94871795, 0.97435897, 1. ])
    • y
      (y [bin-edge])
      float64
      m
      5.469e-05, 0.033, ..., 0.966, 1.000
      Values:
      array([5.46866083e-05, 3.33798341e-02, 6.67049816e-02, 1.00030129e-01, 1.33355277e-01, 1.66680424e-01, 2.00005572e-01, 2.33330719e-01, 2.66655867e-01, 2.99981014e-01, 3.33306162e-01, 3.66631309e-01, 3.99956457e-01, 4.33281604e-01, 4.66606752e-01, 4.99931899e-01, 5.33257047e-01, 5.66582194e-01, 5.99907342e-01, 6.33232489e-01, 6.66557637e-01, 6.99882784e-01, 7.33207932e-01, 7.66533079e-01, 7.99858227e-01, 8.33183374e-01, 8.66508522e-01, 8.99833669e-01, 9.33158817e-01, 9.66483964e-01, 9.99809112e-01])
    • (x, y)
      float64
      counts
      25.892, 25.279, ..., 0.297, 0.099
      σ = 5.295, 5.180, ..., 6.480, 3.735
      Values:
      array([[25.89248991, 25.27923351, 31.00970362, ..., 41.72445952, 6.02805214, 30.7027413 ], [10.48241628, 13.78280101, 8.08930581, ..., 10.17775037, 32.12292972, 13.1569997 ], [ 2.05669309, 19.97652573, 15.69071933, ..., 3.83414367, 18.51394442, 12.6915688 ], ..., [ 0.12107013, 0.14580317, 0.15641146, ..., 0.19431067, 0.23221217, 0.14670408], [ 0.26313962, 0.08665933, 0.16385021, ..., 0.18476415, 0.09803213, 0. ], [ 0.09643831, 0.24489284, 0.20106459, ..., 0.382861 , 0.29650272, 0.09898298]])

      Variances (σ²):
      array([[28.04223671, 26.8317314 , 33.24224979, ..., 44.40416095, 6.47727702, 32.8621606 ], [13.25572563, 17.13030787, 10.27898266, ..., 12.73897097, 39.32951854, 16.07716791], [ 2.69603584, 28.14993241, 21.55967974, ..., 5.35189495, 24.58797524, 17.44119991], ..., [12.83274617, 16.10290935, 17.02339672, ..., 20.56355216, 24.76329692, 16.56010983], [31.96533047, 10.80976355, 20.10368003, ..., 23.12863058, 11.98807571, 0. ], [13.84635738, 33.97350236, 27.90305274, ..., 52.74655453, 41.99197956, 13.95321442]])

Another capability of hist is to histogram a dimension that has previously been binned with a different or higher resolution, i.e. different bin edges. Compare to the plot of the initial example:

[5]:
binned = table.bin(x=xbins)
binned.hist(x=100).plot()
[5]:
../../_images/user-guide_binned-data_histogramming-grouping-and-binning_8_0.svg

Grouping#

groupby is more flexible in terms of operations than can be applied and may be the go-to solution when a quick one-liner is required. Limitations are:

  • Can only group along a single dimension.

  • Works best for small to medium-sized data, or if data is already mostly sorted along the grouping dimension. Slow if millions of small input slices contribute to each group.

groupby can also operate on binned data, combining bin contents by concatenation:

[6]:
binned = table.bin(x=xbins)
binned.coords['param'] = sc.array(
    dims=['x'], values=(np.random.random(39) * 4).astype(np.int32)
)
grouped = binned.groupby('param').concat('x')
grouped
[6]:
Show/Hide data repr Show/Hide attributes
scipp.DataArray (158.70 KB)
    • param: 4
    • param
      (param)
      int32
      𝟙
      0, 1, 2, 3
      Values:
      array([0, 1, 2, 3], dtype=int32)
    • (param)
      DataArrayView
      binned data [len=1154, len=1281, len=1121, len=1444]
      dim='position',
      content=DataArray(
                dims=(position: 5000),
                data=float64[counts],
                coords={'x':float64[m], 'y':float64[m]})

Each output bin is a combination of multiple input bins:

[7]:
grouped.values[0]
[7]:
Show/Hide data repr Show/Hide attributes
scipp.DataArray (37.31 KB out of 157.50 KB)
    • position: 1154
    • x
      (position)
      float64
      m
      0.073, 0.057, ..., 0.815, 0.795
      Values:
      array([0.072956 , 0.05681999, 0.07425508, ..., 0.79935098, 0.81529023, 0.79541251])
    • y
      (position)
      float64
      m
      0.492, 0.703, ..., 0.782, 0.200
      Values:
      array([0.49200701, 0.70342672, 0.68170126, ..., 0.32811215, 0.78202112, 0.19999294])
    • (position)
      float64
      counts
      5.659, 4.843, ..., 0.114, 0.165
      σ = 2.855, 2.537, ..., 2.589, 2.969
      Values:
      array([5.65898822, 4.8429825 , 3.80499156, ..., 0.12343776, 0.11371671, 0.16522541])

      Variances (σ²):
      array([8.15005848, 6.43422065, 5.51564808, ..., 6.71763861, 6.70200194, 8.81643812])

Binning#

scipp.bin actually reorders data and meta data such that all data contributing to a bin is in a contiguous block. Binning along multiple dimensions is supported. Of the three options it is the only solution that supports modifying data in the grouped/binned layout. A variety of operations on such binned data is available. Limitations are:

  • Requires copying and reordering the input data and can thus become expensive.

In the above example the 'y' information is dropped by hist and groupby, but bin preserves it:

[8]:
binned = table.bin(x=xbins)
binned.values[0]
[8]:
Show/Hide data repr Show/Hide attributes
scipp.DataArray (5.81 KB out of 157.50 KB)
    • position: 146
    • x
      (position)
      float64
      m
      0.017, 0.003, ..., 0.007, 0.010
      Values:
      array([0.01720217, 0.00310975, 0.01180276, 0.00936337, 0.00595237, 0.00850423, 0.00270481, 0.01351875, 0.00514329, 0.00629665, 0.01403017, 0.02430262, 0.02421085, 0.00281341, 0.02536831, 0.00982768, 0.02177614, 0.00412636, 0.02516333, 0.00348511, 0.01117117, 0.00374133, 0.0072283 , 0.00572948, 0.00622856, 0.00652611, 0.00383292, 0.01843262, 0.00377603, 0.01308352, 0.01457236, 0.01596243, 0.00683355, 0.02256774, 0.00637885, 0.02287875, 0.01231905, 0.02109659, 0.00939635, 0.02118235, 0.00573414, 0.01931737, 0.00707789, 0.01586108, 0.01948408, 0.00268859, 0.01436142, 0.00437867, 0.0100306 , 0.01292847, 0.0238043 , 0.02273308, 0.02037339, 0.00178348, 0.00016306, 0.00947859, 0.02304741, 0.01590287, 0.01223724, 0.01016693, 0.00801388, 0.01619989, 0.00617763, 0.0220336 , 0.00025312, 0.00052437, 0.00663527, 0.00056475, 0.02478097, 0.01693429, 0.00783947, 0.01331714, 0.00350454, 0.0135763 , 0.00191845, 0.02334239, 0.01051465, 0.02292939, 0.01122023, 0.00677631, 0.02094014, 0.00193583, 0.0044514 , 0.00297925, 0.00517244, 0.00107385, 0.01159401, 0.01359855, 0.01149437, 0.0230815 , 0.0115497 , 0.01701406, 0.02059565, 0.02115183, 0.00755142, 0.01778729, 0.00098741, 0.01216435, 0.01457314, 0.01499713, 0.00730885, 0.01240914, 0.0073206 , 0.00112246, 0.02442966, 0.02337218, 0.00378566, 0.02216977, 0.01028248, 0.01221492, 0.01565491, 0.00911096, 0.02168469, 0.01909049, 0.02412764, 0.00336965, 0.01674963, 0.01415608, 0.01402822, 0.02546174, 0.02122413, 0.01437526, 0.02531688, 0.00378879, 0.00514728, 0.01739441, 0.00575877, 0.0134989 , 0.01620537, 0.01785738, 0.01983875, 0.01694272, 0.00854757, 0.02033267, 0.01669727, 0.01557464, 0.02403922, 0.02455641, 0.00511415, 0.00506292, 0.01185888, 0.00100797, 0.00531293, 0.00280948, 0.00654103, 0.01017281])
    • y
      (position)
      float64
      m
      0.150, 0.697, ..., 0.805, 0.017
      Values:
      array([1.49936519e-01, 6.97208818e-01, 7.10798343e-01, 8.88147230e-01, 3.00879066e-01, 8.77998018e-01, 9.17918212e-01, 4.03804179e-01, 4.18842604e-02, 5.74969982e-02, 6.47394708e-02, 6.44705613e-02, 7.26967924e-01, 8.90111627e-01, 7.40923585e-01, 5.15946105e-01, 4.06350331e-01, 3.86853335e-02, 3.59927673e-01, 8.82176867e-01, 1.00512609e-01, 4.63042368e-01, 3.28454930e-01, 4.77103323e-01, 3.04063537e-01, 7.13418603e-01, 1.19406864e-01, 3.64484676e-01, 2.24301773e-01, 1.45171052e-01, 8.72048030e-01, 2.73663242e-01, 6.56430519e-01, 6.50538646e-01, 4.10929157e-01, 6.44967229e-01, 6.53836679e-01, 1.42724984e-01, 2.71672983e-01, 2.05983030e-01, 6.76480829e-01, 4.80058145e-01, 5.81052338e-01, 7.65468301e-01, 3.69754892e-01, 7.52892963e-01, 1.58268562e-01, 2.14092948e-01, 1.16582889e-01, 9.70967526e-01, 7.13213926e-01, 2.98757246e-01, 5.32639227e-02, 2.61368718e-01, 5.43624858e-01, 1.98642248e-01, 9.69016028e-02, 3.43987879e-01, 1.76545091e-01, 7.91411534e-01, 4.09678785e-01, 1.72870903e-01, 7.15502262e-02, 5.58527264e-01, 4.46146496e-01, 2.80024314e-01, 4.18843822e-01, 7.72122401e-01, 7.69377606e-01, 5.30153293e-01, 7.22613883e-02, 8.50900945e-01, 1.04954302e-01, 3.38898246e-01, 8.68378004e-01, 5.73427297e-01, 7.99863373e-01, 1.06549155e-01, 9.16290713e-01, 6.75258961e-01, 9.11749586e-01, 1.78829795e-01, 7.17839720e-01, 9.03299346e-01, 5.76406782e-01, 8.13494729e-01, 2.26140978e-02, 6.78731760e-01, 9.78547987e-01, 3.34344929e-01, 7.27757775e-01, 3.75175607e-01, 2.09020970e-01, 1.95621024e-01, 3.59880613e-01, 2.22102421e-01, 2.87678950e-01, 1.19945799e-01, 1.98828761e-01, 4.96888633e-01, 9.98630463e-02, 3.23397085e-01, 9.76874801e-01, 8.37002887e-01, 9.29347970e-01, 3.79134158e-01, 1.43796292e-01, 1.30469926e-02, 7.34301710e-01, 3.29175478e-01, 8.55976105e-01, 5.56258282e-02, 3.98275957e-01, 2.59427946e-04, 3.47172897e-01, 8.65415753e-01, 1.99185712e-01, 9.09387846e-02, 6.85633978e-01, 4.59842667e-01, 9.80966731e-01, 9.63385977e-01, 2.71358292e-01, 2.70861114e-01, 8.72144944e-01, 7.47474455e-01, 3.29765489e-01, 7.30266473e-01, 3.48835125e-01, 4.16868432e-02, 8.43033369e-01, 4.52500288e-01, 2.31962396e-01, 9.93076029e-02, 2.51004543e-01, 5.47601779e-01, 5.25984449e-02, 8.16766243e-01, 4.21694156e-01, 2.99731237e-01, 9.30580193e-01, 2.88588969e-01, 1.78713297e-01, 9.06865395e-01, 8.04563256e-01, 1.74342107e-02])
    • (position)
      float64
      counts
      5.074, 4.051, ..., 1.326, 7.927
      σ = 2.351, 2.028, ..., 1.170, 2.888
      Values:
      array([5.07360899, 4.05092484, 6.25227585, 6.52904149, 6.73733277, 1.33176877, 9.55637137, 3.49716485, 6.92462947, 1.15008592, 1.98772089, 3.05147064, 3.68522372, 2.38032424, 4.79145148, 0.18789618, 7.31372482, 2.81641186, 6.20959009, 9.1479853 , 1.35490944, 9.03914153, 4.11788296, 1.3307971 , 2.37690371, 5.35853247, 8.40270594, 4.44283438, 8.64965968, 1.60226734, 8.1848133 , 0.93285381, 0.73662371, 4.54326776, 5.08320068, 5.66581001, 0.28201728, 3.43993422, 9.40490373, 1.03053898, 8.78490073, 4.02503454, 0.07895026, 3.99683737, 0.38940375, 4.68506348, 6.60174759, 0.24447581, 1.9513705 , 7.74874399, 4.28961723, 1.4798804 , 0.61146972, 3.92844284, 5.83827537, 6.62913401, 7.29943461, 0.83526263, 7.68293913, 6.50323897, 7.77684186, 6.43579297, 6.75971992, 5.02490745, 5.04556209, 1.83720658, 7.65893726, 3.72976286, 1.32205061, 7.79919818, 6.72666599, 2.545673 , 1.37650982, 6.72483229, 7.82530194, 5.45880191, 8.78915286, 3.50366723, 8.76464638, 4.28596954, 8.48309321, 1.33823784, 7.4596663 , 2.72480464, 1.43043918, 5.39908392, 4.75922549, 1.5985196 , 5.29031756, 3.43885203, 1.27837283, 7.63887296, 0.14985518, 6.88025404, 3.02966954, 1.3932514 , 9.24829002, 7.93831108, 8.88924899, 4.0599293 , 2.97251594, 2.20296874, 8.68469377, 3.79288373, 5.87998597, 3.34296485, 6.24851212, 7.58033971, 8.65428429, 6.28259544, 8.61563545, 4.83908696, 8.2874727 , 5.62560752, 7.03099626, 7.35668426, 1.71299264, 0.78926632, 8.28634182, 2.75538788, 8.97898599, 6.02805214, 1.25180654, 0.49275565, 3.45132845, 8.50922047, 4.64745864, 3.67717467, 3.14270779, 1.48871629, 8.44765 , 0.84387999, 7.29664135, 6.46210084, 7.60728231, 1.29188723, 2.40964175, 1.62908368, 0.27425364, 1.94139538, 4.54397035, 5.07277305, 6.40549307, 1.7715876 , 1.32574179, 7.92731719])

      Variances (σ²):
      array([5.52931122, 4.11440382, 6.63235099, 6.84197869, 6.94086209, 1.38961832, 9.68649028, 3.7417236 , 7.10501582, 1.18687037, 2.13216851, 3.44573331, 4.15946095, 2.41404501, 5.43943357, 0.1973597 , 8.1550177 , 2.87512308, 7.042136 , 9.30879088, 1.43274251, 9.20982486, 4.26943147, 1.36947233, 2.45209182, 5.53626824, 8.5652936 , 4.87176188, 8.81451787, 1.71058827, 8.80343707, 1.01035864, 0.76222742, 5.08596717, 5.2479388 , 6.35246924, 0.29993434, 3.82261707, 9.8573068 , 1.14567469, 9.04041547, 4.4331942 , 0.08179429, 4.32671587, 0.42924897, 4.74846981, 7.09323507, 0.24988723, 2.05173327, 8.26618505, 4.8317998 , 1.65802421, 0.67704138, 3.963631 , 5.84303731, 6.95087218, 8.19098325, 0.90438989, 8.16770853, 6.84237587, 8.0947824 , 6.97878285, 6.9717732 , 5.61013652, 5.05195179, 1.84202982, 7.91729473, 3.74030971, 1.49643985, 8.4883309 , 6.99556928, 2.72094911, 1.40084258, 7.19717444, 7.90072529, 6.13457801, 9.26358913, 3.92928378, 9.27040745, 4.433673 , 9.41944203, 1.35125374, 7.62755771, 2.7656978 , 1.46791597, 5.4281509 , 5.04327167, 1.71098752, 5.60326857, 3.85952919, 1.35437018, 8.31715597, 0.16610957, 7.64777459, 3.14624814, 1.52283936, 9.29406217, 8.43611842, 9.56115229, 4.37607058, 3.08315356, 2.34398293, 9.00846918, 3.81423031, 6.64392355, 3.75736898, 6.36791231, 8.46895284, 9.11085803, 6.67826193, 9.31711637, 5.06462879, 9.23655084, 6.18904869, 7.93248717, 7.48168144, 1.86263111, 0.84715545, 8.88842331, 3.1294802 , 9.9842378 , 6.47727702, 1.42073184, 0.50217936, 3.54130617, 9.28242112, 4.783222 , 3.93393112, 3.40795243, 1.62775389, 9.3285728 , 0.91848348, 7.61524387, 7.15361505, 8.26965129, 1.39651144, 2.71739626, 1.84190384, 0.28135693, 1.99116838, 4.82155044, 5.09840358, 6.57793317, 1.79664939, 1.36981717, 8.3409635 ])

If we omit the call to bins.sum in the original example, we can subsequently apply another histogramming or binning operation to the data:

[9]:
binned = binned.bin(y=100)
binned
[9]:
Show/Hide data repr Show/Hide attributes
scipp.DataArray (220.91 KB)
    • x: 39
    • y: 100
    • x
      (x [bin-edge])
      float64
      m
      0.0, 0.026, ..., 0.974, 1.0
      Values:
      array([0. , 0.02564103, 0.05128205, 0.07692308, 0.1025641 , 0.12820513, 0.15384615, 0.17948718, 0.20512821, 0.23076923, 0.25641026, 0.28205128, 0.30769231, 0.33333333, 0.35897436, 0.38461538, 0.41025641, 0.43589744, 0.46153846, 0.48717949, 0.51282051, 0.53846154, 0.56410256, 0.58974359, 0.61538462, 0.64102564, 0.66666667, 0.69230769, 0.71794872, 0.74358974, 0.76923077, 0.79487179, 0.82051282, 0.84615385, 0.87179487, 0.8974359 , 0.92307692, 0.94871795, 0.97435897, 1. ])
    • y
      (y [bin-edge])
      float64
      m
      5.469e-05, 0.010, ..., 0.990, 1.000
      Values:
      array([5.46866083e-05, 1.00522309e-02, 2.00497751e-02, 3.00473194e-02, 4.00448636e-02, 5.00424079e-02, 6.00399521e-02, 7.00374964e-02, 8.00350406e-02, 9.00325849e-02, 1.00030129e-01, 1.10027673e-01, 1.20025218e-01, 1.30022762e-01, 1.40020306e-01, 1.50017850e-01, 1.60015395e-01, 1.70012939e-01, 1.80010483e-01, 1.90008027e-01, 2.00005572e-01, 2.10003116e-01, 2.20000660e-01, 2.29998204e-01, 2.39995749e-01, 2.49993293e-01, 2.59990837e-01, 2.69988381e-01, 2.79985926e-01, 2.89983470e-01, 2.99981014e-01, 3.09978558e-01, 3.19976103e-01, 3.29973647e-01, 3.39971191e-01, 3.49968735e-01, 3.59966280e-01, 3.69963824e-01, 3.79961368e-01, 3.89958912e-01, 3.99956457e-01, 4.09954001e-01, 4.19951545e-01, 4.29949089e-01, 4.39946634e-01, 4.49944178e-01, 4.59941722e-01, 4.69939266e-01, 4.79936811e-01, 4.89934355e-01, 4.99931899e-01, 5.09929443e-01, 5.19926988e-01, 5.29924532e-01, 5.39922076e-01, 5.49919620e-01, 5.59917165e-01, 5.69914709e-01, 5.79912253e-01, 5.89909797e-01, 5.99907342e-01, 6.09904886e-01, 6.19902430e-01, 6.29899974e-01, 6.39897519e-01, 6.49895063e-01, 6.59892607e-01, 6.69890151e-01, 6.79887696e-01, 6.89885240e-01, 6.99882784e-01, 7.09880328e-01, 7.19877873e-01, 7.29875417e-01, 7.39872961e-01, 7.49870505e-01, 7.59868050e-01, 7.69865594e-01, 7.79863138e-01, 7.89860682e-01, 7.99858227e-01, 8.09855771e-01, 8.19853315e-01, 8.29850859e-01, 8.39848404e-01, 8.49845948e-01, 8.59843492e-01, 8.69841036e-01, 8.79838581e-01, 8.89836125e-01, 8.99833669e-01, 9.09831213e-01, 9.19828758e-01, 9.29826302e-01, 9.39823846e-01, 9.49821390e-01, 9.59818935e-01, 9.69816479e-01, 9.79814023e-01, 9.89811567e-01, 9.99809112e-01])
    • (x, y)
      DataArrayView
      binned data [len=1, len=2, ..., len=0, len=0]
      dim='position',
      content=DataArray(
                dims=(position: 5000),
                data=float64[counts],
                coords={'x':float64[m], 'y':float64[m]})

As in the 1-D example above, summing the bins is equivalent to histogramming binned data:

[10]:
binned.bins.sum().plot()
[10]:
../../_images/user-guide_binned-data_histogramming-grouping-and-binning_18_0.svg