A robot calibration report groups probe errors by commanded position. If the sweep includes the exact end of the measured travel, that last observation can change the mean for the final range. A report that summarizes errors with SciPy and derives counts in a separate step should use the same interval rules in both places.
Test the boundaries with a small sweep
This fixture pairs positions in millimeters with signed errors in micrometers. The edges 0, 5, and 10 describe two bins. The first includes 0 and stops just before 5. The second includes both 5 and 10. The positions at -1 and 11 represent rows outside the intended sweep. Their deliberately large errors make it easy to detect accidental inclusion.
import numpy as np
from scipy.stats import binned_statistic
position_mm = np.array([-1, 0, 4, 5, 8, 9, 10, 11])
error_um = np.array([900, 10, 30, 20, 40, 60, 80, 1000])
edges = np.array([0, 5, 10])
mean, actual_edges, assignment = binned_statistic(
position_mm, error_um, statistic="mean", bins=edges,
)
count, count_edges = np.histogram(position_mm, bins=edges)
assert np.array_equal(actual_edges, count_edges)
assert np.allclose(mean, [20, 50])
assert count.tolist() == [2, 4]
assert assignment.tolist() == [0, 1, 1, 2, 2, 2, 2, 3]
SciPy 1.18.1 and NumPy 2.5.5 produced those results. The point at 5 mm goes into the second bin. The point at 10 mm also goes into that bin and contributes its 80 micrometer error to the mean of 50 micrometers. Excluding that one observation would change the mean to 40 micrometers and the count to three. The low and high overflow rows appear in the assignment array as 0 and 3, outside the two interior bins numbered 1 and 2.
Reconcile the counts before exporting
Keep the actual edges with the result so a later report can distinguish the final closed interval from the earlier intervals. Record overflow counts alongside the in-range counts. In this fixture there are six in-range positions and two out-of-range positions, which accounts for all eight input rows. If the report filters rows for missing error measurements, apply the same row selection to both the mean and its companion count. Otherwise a valid position with a missing error can appear in the count without supplying a valid error to the statistic.
Add a test row at each edge and a row outside each end. Use the same units for the data and for the stored bin edges. For measurements near a boundary, define any rounding or tolerance in the calibration method and preserve the original values for review.
Sources
SciPy binned_statistic reference for assignment and interval endpoints
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binned_statistic.html
NumPy histogram reference for counts and the final closed interval
https://numpy.org/doc/stable/reference/generated/numpy.histogram.html