A torque reading can be finite and still be excluded from a summary. A logger might retain a number while marking its sample invalid because the sensor was out of range for calibration, the measurement arrived stale, or the record failed a consistency check. A weighted average needs to preserve that quality decision. If a conversion strips the mask, the flagged value can enter both the numerator and denominator even when there are no missing numbers to trigger a warning.
Keep a finite rejected sample in the test
The synthetic fixture below ran with NumPy 2.5.2. It uses five torque readings in newton-meters and five positive, unitless analysis weights. The middle reading is flagged by the hypothetical quality system. The example leaves it in the stored values so a reviewer can see exactly what the mask does. The weights stand for an explicitly chosen analysis policy and do not describe elapsed time, sensor confidence, or a real test. With the flagged sample excluded, four readings contribute a total weight of six.
import numpy as np
torque_nm = np.array([10., 22., 70., 18., 14.])
weights = np.array([1., 2., 4., 1., 2.])
quality_rejected = np.array([False, False, True, False, False])
masked = np.ma.array(torque_nm, mask=quality_rejected)
mean, used_weight = np.ma.average(masked, weights=weights, returned=True)
base = np.asarray(masked)
unfiltered_mean = np.average(base, weights=weights)
assert np.isclose(mean, 100. / 6.) and used_weight == 6.
assert np.isfinite(base[2]) and np.isclose(unfiltered_mean, 38.)
with_gap = torque_nm.copy()
with_gap[3] = np.nan
masked_gap = np.ma.masked_invalid(
np.ma.array(with_gap, mask=quality_rejected)
)
mean_gap, used_gap = np.ma.average(masked_gap, weights=weights, returned=True)
assert np.ma.getmaskarray(masked_gap).tolist() == [False, False, True, True, False]
assert np.isclose(mean_gap, 82. / 5.) and used_gap == 5.
assert np.ma.is_masked(np.ma.average(np.ma.masked_all(5), weights=weights))
The accepted readings contribute a weighted sum of 100 and a weighted average of about 16.67 newton-meters. Converting the masked array with NumPy's asarray produces a base array and omits the mask. In this fixture, the underlying 70 remains finite and receives a weight of four. Averaging that base array with the original weights returns 38. The conversion succeeded and the calculation returned a finite result. Neither fact shows that the calculation honored the logger's validity decisions.
Distinguish numeric failures from quality failures
A quality mask and a finite-value check answer different questions. The modified example changes one other reading to NaN. Applying masked_invalid to the array keeps the original quality exclusion and marks the new nonfinite observation as excluded. The remaining three samples contribute a weight of five and an average of 16.4 newton-meters. Filling invalid values with zero before averaging would alter the meaning of both the measurement and the denominator. A true zero torque reading can be eligible for an average, whereas a missing measurement should follow the documented exclusion policy.
An entirely masked window needs an explicit reporting path. In this fixture NumPy returns a masked result rather than a real observation. A monitoring report should represent that outcome as unavailable and preserve why. For example, a page can show the number of accepted samples and their total weight alongside the statistic. If the logger can assign zero weight to every otherwise accepted observation, check for that case separately before computing a result.
Carry the mask across interfaces
Keep validity flags aligned with the values through sorting, slicing, and joining. If a table export or another library requires an ordinary array, carry a separate validity column or filter the values and their weights together before the conversion. In the second approach, save enough information to identify what was dropped. A report that retains only the filtered average cannot distinguish a short valid record from a long record that lost most of its observations during a sensor interruption.
When comparing two runs, use the same documented meaning for a weight. Unitless analysis weights answer a different question from a time-weighted average. A time-based statistic needs valid intervals derived from the correct clock and a policy for gaps. Do not extend a reading across an interruption simply because it was the last finite value. The stage that constructs the weights should know whether an excluded record removes a whole interval or whether the surrounding samples can be used under an explicit interpolation rule.
Verify both the result and the evidence behind it
A useful review fixture includes a valid zero, a finite rejected measurement with a large weight, a nonfinite observation, and a fully excluded window. For each case, inspect the retained rows, the weight sum, and the exported report. Also check the value used by any plot or dashboard that receives the aggregate. A pipeline can correctly handle the mask during calculation and then lose the information about missing coverage when it prepares a display.
Keep the accepted weight sum and sample count near the reported torque. A reader can then distinguish a representative value for a well-observed interval from a calculation based on only a small part of the run. The Python fixture is short enough to exercise those rules each time the report's data interface changes.
Sources
NumPy, numpy.ma.masked_invalid
https://numpy.org/doc/stable/reference/generated/numpy.ma.masked_invalid.html
NumPy, numpy.ma.average
https://numpy.org/doc/stable/reference/generated/numpy.ma.average.html
NumPy, numpy.asarray
https://numpy.org/doc/stable/reference/generated/numpy.asarray.html