A robot test report often needs one row per run even when some runs contribute no samples. A telemetry export can store the samples in a single array and put the boundaries of each run in a separate offset table. That format saves padding, but the meaning of repeated offsets matters. If the run boundaries are passed directly to NumPy's reduceat, a repeated offset can produce a value from the next run where the report expects an empty one.

The example uses small integer event counts from invented runs. The results below were checked in NumPy 2.5.2. The same storage layout can hold many other kinds of data, but the appropriate treatment for an empty group depends on the quantity being reported.

Read the boundaries as slices

The five sample values are 4, 7, 1, 9, and 3. The four offsets are 0, 2, 2, and 5. To locate a run, use its offset as the start and the next offset as the exclusive stop. The first run owns positions zero and one. The second owns an empty slice because its start and stop both equal two. The third owns the remaining three values.

import numpy as np

values = np.array([4, 7, 1, 9, 3], dtype=np.int64)

offsets = np.array([0, 2, 2, 5], dtype=np.int64)

assert values[0:2].tolist() == [4, 7]

assert values[2:2].size == 0

assert values[2:5].tolist() == [1, 9, 3]

For event counts, the expected sums are 11, 0, and 13. It is still useful to retain a separate sample count for each run. An empty run and a measured run whose observations sum to zero both have a total of zero. The counts let the report preserve that distinction.

Why reduceat produces a plausible wrong result

NumPy's reduceat normally reduces between adjacent start indices, with the last start extending to the end of the selected axis. There is a documented exception when a start is at or beyond the next start. In that case, the result for that position is the sample at the start index. The fixture demonstrates what happens when the offset table contains a repeated boundary.

raw = np.add.reduceat(values, offsets[:-1])

assert raw.tolist() == [11, 1, 13]

The middle 1 comes from values[2]. That same sample is included in the total for the third run. The repeated start therefore causes a contribution to appear twice, even though the output has the correct number of rows. If a fourth run with no samples is appended, its start is 5, which is outside the five-element sample array. Passing the starts without filtering then raises an index error. The offset is a valid exclusive endpoint of a slice, which is a different role from naming an element.

Keep the empty rows when reducing nonempty runs

Check that the offset table starts at zero, ends at the size of the flat array, and never decreases. These conditions describe a complete partition of one buffer. The difference between adjacent offsets gives the number of observations in each run. Select the starts for runs with positive counts, reduce at those starts, and return the results to their original positions.

offsets = np.array([0, 2, 2, 5, 5], dtype=np.int64)

lengths = np.diff(offsets)

nonempty = lengths > 0

totals = np.zeros(len(lengths), dtype=np.int64)

if nonempty.any():

starts = offsets[:-1][nonempty]

totals[nonempty] = np.add.reduceat(values, starts, dtype=np.int64)

assert lengths.tolist() == [2, 0, 3, 0]

assert totals.tolist() == [11, 0, 13, 0]

Removing a repeated start does not remove any data in a complete partition. The next retained start is also the end of the preceding nonempty interval. Keeping the output indexed by the original run order ensures that the fourth row still belongs to the fourth run. Keep the run IDs alongside the offset table when joining the results to test metadata.

Cross-check with cumulative sums

A prefix sum offers an independent calculation for the integer example. Prefix position zero holds zero. Every later position holds the sum of the samples preceding that boundary. The total for a run is the difference between the two prefix values at its endpoints. Equal endpoints produce a difference of zero.

prefix = np.r_[0, np.cumsum(values, dtype=np.int64)]

reconstructed = prefix[offsets[1:]] - prefix[offsets[:-1]]

assert np.array_equal(reconstructed, totals)

This method creates an additional array with one cumulative value per sample plus the initial zero. Choose an accumulator whose range accommodates the cumulative total. NumPy documents modular arithmetic for integer cumulative sums, so overflow can wrap without an error. With floating-point measurements, subtracting nearby, large cumulative totals can lose precision for a small run. Checking against direct slice sums on representative data helps determine whether that matters.

For an event report, export the sample count, total, and run ID together. A mean of an empty run needs an explicit missing-result convention. A run skipped before data acquisition may also need a separate status from a completed run that simply produced no events. The offset table encodes how many values belong to each run, while acquisition status comes from the run record.

A regression fixture can put empty runs at the beginning, middle, and end of a batch and include a batch with no samples at all. Check that the number and order of run IDs survive aggregation and compare each total against its direct slice. These cases exercise the repeated boundary and the endpoint that equals the size of the sample array.

Sources checked November 28, 2026

NumPy, numpy.ufunc.reduceat

https://numpy.org/doc/stable/reference/generated/numpy.ufunc.reduceat.html

NumPy, numpy.cumsum

https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html

Illustration. AI-generated concept showing an empty sample tray in the middle of a three-run sequence.