Implementation:Facebookresearch Habitat lab WindowedRunningMean
| Knowledge Sources | |
|---|---|
| Domains | Embodied_AI, Statistics |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
WindowedRunningMean is an efficient implementation of a windowed running mean that supports both finite window sizes and an infinite (cumulative) window using a circular buffer.
Description
WindowedRunningMean uses the attrs library for a compact, slot-based dataclass definition. It maintains a running sum and count. When the window size is finite, a NumPy circular buffer of the specified size stores recent values; once the buffer is full, the oldest value is subtracted from the running sum when a new value is added. When the window is infinite (signaled by math.isinf(window_size) or a non-positive value), it simply accumulates all values. The class provides properties for mean, sum, count, and infinite_window, and supports += via __iadd__ and float() conversion via __float__.
Usage
Use WindowedRunningMean when you need to track a running average over a sliding window of recent values, such as for smoothing training metrics or timing measurements. Pass float('inf') as the window size for cumulative averaging.
Code Reference
Source Location
- Repository: Facebookresearch_Habitat_lab
- File: habitat-baselines/habitat_baselines/common/windowed_running_mean.py
- Lines: 16-74
Signature
@attr.s(auto_attribs=True, slots=True, repr=False)
class WindowedRunningMean:
window_size: Union[int, float]
Import
from habitat_baselines.common.windowed_running_mean import WindowedRunningMean
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| window_size | Union[int, float] | Yes | Size of the sliding window. Use float('inf') or a non-positive value for cumulative (infinite) averaging. |
Outputs
| Name | Type | Description |
|---|---|---|
| mean | float | The current running mean of values in the window |
| sum | float | The current running sum of values in the window |
| count | int | Number of values currently tracked (up to window_size) |
Key Methods
add
def add(self, v_i: Union[numbers.Real, float, int]) -> None
Adds a single value to the running mean.
add_many
def add_many(self, vs: Sequence[Union[numbers.Real, float, int]])
Adds multiple values sequentially.
Usage Examples
Basic Usage
from habitat_baselines.common.windowed_running_mean import WindowedRunningMean
# Finite window of 100 values
tracker = WindowedRunningMean(window_size=100)
for value in range(200):
tracker.add(value)
print(tracker.mean) # Mean of the last 100 values (150.0 - 199.0)
print(tracker.count) # 100
# Infinite window (cumulative mean)
cumulative = WindowedRunningMean(window_size=float("inf"))
cumulative += 10.0
cumulative += 20.0
print(float(cumulative)) # 15.0