ML Foundations
Check window bounds before taking a NumPy mean
Detect incomplete signal windows before a mean reduction, and choose an explicit boundary policy.
Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
An empty window is not a measurement
A windowed signal calculation may request a slice beyond the available samples. NumPy slicing can return a shorter array than requested, including an empty array, rather than raising an index error. Taking the mean of an empty window can produce a warning and a NaN. Check the specific slice at the point of reduction rather than assuming a nearby debug print describes the same iteration. [1, 2]
Decide how to handle incomplete data before computing a statistic. Skipping a window, averaging the available samples, padding, or raising an error are different contracts. They can yield different results at the boundary. The example below chooses to skip every incomplete window.
Require a complete right-hand window
Run python examples/numpy_empty_window_mean.py. The helper accepts a one-dimensional signal, a nonnegative start and a positive width. It slices the signal and compares the number of returned elements with the requested width. Only a complete window is passed to numpy.mean; an incomplete one returns None. [1, 2]
For the five values 1 through 5 and width three, start zero returns 2.0. Start three has only two samples and start five has none, so both return None. These are intentionally different boundary conditions, handled by the same explicit rule. The assertions cover the three shown cases.
The None result is a missing-window signal, not a numeric zero. Callers should propagate or skip it explicitly before aggregating downstream metrics. This avoids accidentally treating absence as a real observation.
Executed with Python 3.12.14 and NumPy 2.3.5; stdout and checksum are in private evidence.
"""Compute only complete right-hand windows of a one-dimensional signal."""
import numpy as np
def window_mean(values, start, width):
values = np.asarray(values, dtype=float)
if values.ndim != 1 or width <= 0 or start < 0:
raise ValueError('expected 1D values and nonnegative start and positive width')
window = values[start:start + width]
if window.size != width:
return None
return float(np.mean(window))
signal = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
for start in (0, 3, 5):
print(f'start={start}:', window_mean(signal, start, 3))
assert window_mean(signal, 0, 3) == 2.0
assert window_mean(signal, 3, 3) is None
assert window_mean(signal, 5, 3) is None
Recorded output
start=0: 2.0 start=3: None start=5: None
Define the policy for your own data
This example intentionally rejects partial windows even when they contain usable samples. If the application requires partial averages, check for an empty slice separately and report its actual sample count; do not silently combine averages with different support sizes.
The fixture uses finite numeric values and a single dimension. It does not check NaNs or infinities and does not prescribe how to handle a missing sample inside an otherwise complete window. Add a separate validation or missing-data policy when those inputs are possible. The example establishes boundary behavior, not a performance claim.