mesa icon indicating copy to clipboard operation
mesa copied to clipboard

`DataRecorder` duplicates `TableDataSet` rows and gives them the wrong time

Open 0xnithinmys opened this issue 1 month ago • 6 comments

TableDataSet.data returns its live rows list (mesa/experimental/data_collection/dataset.py:275). DataRecorder (datarecorders.py:106-108) and JSONDataRecorder store that list by reference at every snapshot, so all stored blocks are the same growing list. The output repeats every row once per snapshot and stamps rows with times before they were added.

Expected behavior

Each table row appears once in get_table_dataframe(), with the time at which it was recorded.

To Reproduce

from mesa import Model
from mesa.experimental.data_collection import DataRecorder
from mesa.experimental.data_collection.dataset import TableDataSet

class MyModel(Model):
    def __init__(self):
        super().__init__(rng=42)
        self.events = self.data_registry.create_dataset(
            TableDataSet, "events", fields=["what"]
        )
        self.recorder = DataRecorder(self, config={"events": {}})

    def step(self):
        self.events.add_row({"what": f"added at t={self.time}"})

model = MyModel()
model.run_for(3)
print(len(model.events.data))
print(model.recorder.get_table_dataframe("events"))

Output:

3
             what  time
0  added at t=1.0   1.0
1  added at t=2.0   1.0
2  added at t=3.0   1.0
3  added at t=1.0   2.0
4  added at t=2.0   2.0
5  added at t=3.0   2.0
6  added at t=1.0   3.0
7  added at t=2.0   3.0
8  added at t=3.0   3.0

Three rows were added, but nine come back. The row added at t=3.0 also appears at times 1.0 and 2.0. JSONDataRecorder returns 12 rows, because it also stores the empty list at time 0, and that list fills up later.

Additional context

  • Proposed fix: at each snapshot, record only the rows added since the previous one, and copy them. Copying the whole list would still repeat every row in every snapshot.

0xnithinmys avatar Oct 22 '26 16:10 0xnithinmys

Hello maintainers I would like to take this- reproduced it on main, 3 rows become 9. Reproduced on main (4.0.0a0): the issue repro gives 3 live rows but 9 recorded rows, with rows stamped at times before they were added.

Root cause: TableDataSet.data (dataset.py:270) returns the live rows list, and DataRecorder._store_dataset_snapshot stores it by reference in the list() case (datarecorders.py:106-109) — the numpy case just above it does .copy(). JSONDataRecorder has the same problem.

Plan: copy the list at snapshot time in both recorders, and add a test in tests/experimental/test_datarecorder.py asserting exactly 3 rows with times 1.0, 2.0, 3.0.

Shall I open the PR with this approach?

Nishant-neural avatar Nov 21 '26 19:11 Nishant-neural

Hi, following up on my comment above — I read the two earlier PRs before starting work, so I wanted to check direction first rather than repeat them. What I found: #4059 (copy mutable values) was closed Nov 10 in favour of discussion #3997, with the note that a direction needs to be agreed first. #3997 still looks undecided between copying values and restricting recorders to numbers/strings. Before I write a fix: should this follow the delta-rows approach from #3969, the copy approach from #4059, or wait for a decision in #3997? Happy to implement whatever method you prefer

Nishant-neural avatar Nov 21 '26 19:11 Nishant-neural

I first want to get a clear explanation of what is happening. I understand and agree there is a bug here. I don't understand how it happens and without that, we can't agree on a fix. In my view this bug is independent from #3997.

quaquel avatar Nov 21 '26 20:11 quaquel

Ok, figured out what is going on. There are two problems

  1. TableDataSet returns its mutable list of rows. The recorder takes a snapshot of it (rather than copying it). So, at every step a new row is added and the already taken snapshot is silently also expanded.
  2. At every snapshot, all rows are included, rather than the rows that are new compared to the previous snapshot.

The solution is to keep track of which rows have been seen previously and only add the new rows. There is no need press to copy the entire rows over. So there is no link is #3997.

quaquel avatar Nov 21 '26 20:11 quaquel

Thank you , I understand now — I'll implement the cursor approach only: keep track of which rows have been seen and add only the new rows at each snapshot, without copying the entire rows list. I'll build on #3969's _table_row_offsets approach, including the is_overwrite handling you asked about and open a PR against #3967.

Nishant-neural avatar Nov 21 '26 20:11 Nishant-neural

I am about to open a PR already. Now that I figured it out, the fix is trivial.

quaquel avatar Nov 21 '26 20:11 quaquel