Skip to content

Record TableDataSet rows once in data recorders - #3969

Closed
0xnithinmys wants to merge 3 commits into
mesa:mainfrom
0xnithinmys:fix/table-dataset-recorder-duplicates
Closed

0xnithinmys wants to merge 3 commits into
mesa:mainfrom
0xnithinmys:fix/table-dataset-recorder-duplicates

Conversation

@0xnithinmys

@0xnithinmys 0xnithinmys commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

All four data recorders repeated every TableDataSet row once per snapshot and stamped rows with times before they were added. Three rows over run_for(3) came back as 9 rows from DataRecorder and 12 from JSONDataRecorder.

Bug / Issue

Fixes #3967.

TableDataSet.data returns its live rows list. DataRecorder, JSONDataRecorder, ParquetDataRecorder and SQLDataRecorder stored that whole list at every snapshot, so a table's output contained every row once per snapshot, including snapshots taken before the row existed.

Implementation

  • BaseDataRecorder._snapshot_data() builds the data for every snapshot: time changes, RUN_ENDED and manual collect(). For a TableDataSet it returns only the rows added since the previous snapshot. For an overwrite it returns the rows since the start of the snapshot being replaced. Other datasets are passed through unchanged. This fixes all four recorders in one place. TableDataSet is imported inside the method because dataset.py imports this module (the same pattern as mesa_signals/core.py).
  • DataRecorder: an overwrite now pops the last block only if it is from the same time. An empty snapshot stores no block, so replacing one used to pop the previous time's block and lose its rows. With incremental tables, empty snapshots are common.
  • JSONDataRecorder.get_table_dataframe() no longer turns an empty snapshot into a placeholder row (value=[] with NaN fields). The other three recorders already produce no rows for an empty snapshot.

Testing

  • New test_table_dataset_rows_recorded_once, run for all four recorders. A model adds one row per step except at time 3, and runs run_for(3) then run_for(2), so the empty RUN_ENDED snapshot at time 3 is overwritten. It checks that each row appears once, with the time it was recorded.
  • It fails for all four recorders on main and passes with the fix. Removing only the DataRecorder time check makes the DataRecorder case fail again.
  • tests/experimental/test_datarecorder.py and tests/experimental/test_dataset.py pass.
  • Full suite: the only failure is test_suppressed_wake_is_logged_at_debug, which also fails on unmodified main (Importing the Boltzmann example app disables MESA log propagation, breaking caplog in later tests #3935).
  • ruff check and ruff format --check are clean.

Additional Notes

  • Rows added before a table's first snapshot, for example before a dataset's start_time, are included in that first snapshot.
  • After clear(), rows that were already recorded are not recorded again; recording continues with new rows.

TableDataSet.data returns its live list of rows, and every recorder
stored that whole list at each snapshot. Each row was repeated once per
snapshot and stamped with times before it was added.

BaseDataRecorder now passes only the rows added since the previous
snapshot of a TableDataSet; an overwrite passes the rows since the start
of the snapshot it replaces. This fixes all four recorders.

Two related fixes keep empty snapshots correct. DataRecorder only pops
the last block on an overwrite if that block is from the same time, so
replacing an empty snapshot no longer drops an earlier one.
JSONDataRecorder no longer turns an empty snapshot into a placeholder
row, matching the other recorders.

Fixes mesa#3967
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Oct 22, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -3.4% [-4.2%, -2.5%] 🔵 +0.4% [+0.1%, +0.6%]
BoltzmannWealth large 🔵 -2.2% [-3.5%, -0.8%] 🟢 -9.1% [-14.8%, -3.5%]
Schelling small 🔵 -0.4% [-0.9%, +0.1%] 🔵 -0.2% [-0.8%, +0.4%]
Schelling large 🔵 +1.3% [+0.4%, +2.0%] 🔵 +1.5% [+0.6%, +2.4%]
WolfSheep small 🔵 +0.2% [-0.3%, +0.8%] 🔵 +2.3% [-1.9%, +6.7%]
WolfSheep large 🔵 +1.8% [+1.3%, +2.4%] 🔴 +5.9% [+4.6%, +7.0%]
SugarscapeG1mt small 🔵 -0.1% [-0.7%, +0.6%] 🔵 +1.8% [+0.1%, +3.3%]
SugarscapeG1mt large 🔵 -1.6% [-3.6%, +0.4%] 🔵 +0.3% [-1.5%, +1.9%]
BoidFlockers small 🔵 -0.8% [-2.0%, +0.4%] 🔵 -0.2% [-0.5%, +0.1%]
BoidFlockers large 🔵 -1.3% [-2.2%, -0.4%] 🔵 -0.3% [-0.6%, -0.0%]
MultiLevelAllianceModel small 🟢 -6.0% [-8.3%, -3.9%] 🔵 +2.4% [+2.0%, +2.8%]
MultiLevelAllianceModel large 🔵 -3.6% [-6.3%, -0.9%] 🔴 +6.8% [+4.3%, +9.0%]

@quaquel quaquel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the code, but I fail to understand the bug. Should is_overwrite not simply remove all old data?

Also, I noticed, that is_overwrite is missing from the signature in base data recorder, so while you are at it, could you include that in this PR.

@quaquel

quaquel commented Nov 21, 2026

Copy link
Copy Markdown
Member

Closing this as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataRecorder duplicates TableDataSet rows and gives them the wrong time

2 participants