mesa icon indicating copy to clipboard operation
mesa copied to clipboard

DataRecorder ignores empty agent snapshots, so they don’t count toward window_size

Open spyke7 opened this issue 2 weeks ago • 3 comments

Describe the bug DatasetConfig.window_size is documented as the maximum number of snapshots to keep. However, DataRecorder skips appending a snapshot when the agent list is empty. An empty snapshot therefore never advances the sliding window.

With window_size=2, take snapshots that are non-empty at t1, empty at t2, and non-empty at t3. Storage can end up retaining t1 and t3, because t2 never occupied a slot in the window. The result is stale data (t1) that should already have been evicted, and the window no longer spans the last N time steps.

The existing window tests in test_datarecorder.py only cover non-empty data, so this case isn't caught.

Expected behavior Every recording step counts toward the window, including steps where no agents exist. With window_size=2, after t3 only the t2 (empty) and t3 snapshots should be retained, and t1 should be evicted.

To Reproduce

from mesa import Agent, Model
from mesa.experimental.data_collection import DataRecorder, DatasetConfig


class Person(Agent):
    def __init__(self, model, value):
        super().__init__(model)
        self.value = value


model = Model()
first = Person(model, 10)
model.data_registry.track_agents(model.agents, "people", fields="value")

recorder = DataRecorder(
    model,
    config={"people": DatasetConfig(window_size=2)},
)

model.step()       # Snapshot 1: agent 1 is present
first.remove()
model.step()       # Snapshot 2: no agents
Person(model, 20)
model.step()       # Snapshot 3: agent 2 is present

df = recorder.get_table_dataframe("people")
print(df[["time", "unique_id", "value"]].to_string(index=False))

Additional context Python 3.13.9 OS - Windows

  • Affected area: mesa.experimental.data_collection (DataRecorder, DatasetConfig.window_size)

spyke7 avatar Nov 13 '26 15:11 spyke7

Thanks for the report. One clarification: window_size counts snapshots, not time steps:

https://github.com/mesa/mesa/blob/2d9b25177627f38fb43c4273ac2bdd03b95f01cb/mesa/experimental/data_collection/basedatarecorder.py#L47

So with interval=2 a window of 3 covers 6 time units, and it only equals the last N steps when interval=1.

So the question here is whether a collection with no agents counts as a snapshot. Every stored row keeps its correct timestamp, so no values are wrong; the window just keeps older rows in this case. Given how rare it is, I'm inclined atmax to document the behaviour (window_size keeps the last N stored snapshots; a collection with no agents stores nothing) rather than change it. Thoughts @quaquel ?

codebreaker32 avatar Nov 13 '26 17:11 codebreaker32

@codebreaker32 Thanks for clarifying 😅 I agree documenting that window_size retains the last N stored snapshots, and that empty agent collections aren’t stored, may be the clearest approach

spyke7 avatar Nov 13 '26 18:11 spyke7

@EwoutH @quaquel @codebreaker32 I will generate a PR regarding this, please update me with any suggestions..

spyke7 avatar Nov 15 '26 22:11 spyke7