Skip to content

Fix data recorders' handling of empty snapshots - #4168

Merged
quaquel merged 2 commits into
mesa:mainfrom
codebreaker32:fix/recorder-empty-snapshots
Nov 22, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
codebreaker32:fix/recorder-empty-snapshots

Conversation

@codebreaker32

Copy link
Copy Markdown
Collaborator

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Fixes three bugs in mesa/experimental/data_collection that involve empty snapshots. One loses recorded data, one adds rows that were never recorded, and one makes the scenario runner reject a valid run. These are items 1, 4 and 7 of #4165.

Bug / Issue

Part of #4165, which has a reproduction script and its output on main.

  1. DataRecorder: when a run ends with an empty agent snapshot, which stores no block, the next run overwrites that time by popping the last block without checking its time. That deletes the previous time's data, and repeated runs can erase the history one block at a time.
  2. JSONDataRecorder.get_table_dataframe: every empty agent snapshot becomes a row with value=[] and NaN ids, which also turns integer ids into floats.
  3. The JSON, Parquet and SQL recorders return a frame without columns when nothing was recorded. RunConfiguration raises OutcomeExtractionException for such a frame, so the run fails. DataRecorder already returns a frame with a time column.

Implementation

  • DataRecorder._store_dataset_snapshot: on an overwrite, only pop the last block if it holds the same time.
  • JSONDataRecorder.get_table_dataframe: skip empty list snapshots, and return pd.DataFrame(columns=["time"]) when there are no records.
  • ParquetDataRecorder.get_table_dataframe and SQLDataRecorder.get_table_dataframe: return pd.DataFrame(columns=["time"]) when nothing has been stored.

Testing

One regression test per bug in tests/experimental/test_datarecorder.py. Each fails on main and passes with the fix. The existing data collection, scenario and example tests pass unchanged.

Additional Notes

🤖 Generated with Claude Code

- DataRecorder: an overwrite only replaces the last block if it holds
  the same time. When a run ended with an empty agent snapshot, which
  stores no block, the next run's overwrite removed the previous time's
  data instead.
- JSONDataRecorder: a snapshot taken without agents no longer adds a row
  with value=[] and NaN ids.
- JSON, Parquet and SQL recorders return an empty frame with a time
  column when nothing has been recorded, as DataRecorder does. The
  scenario runner rejects a frame without columns as a failed outcome.

Part of mesa#4165.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 22, 2026
@codebreaker32 codebreaker32 added the bug Release notes label label Nov 22, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +3.8% [+2.1%, +5.4%] 🟢 -4.1% [-4.7%, -3.6%]
BoltzmannWealth large 🔵 +4.7% [+2.5%, +7.1%] 🔴 +27.3% [+20.2%, +35.1%]
Schelling small 🔴 +9.9% [+8.7%, +11.0%] 🔵 -0.1% [-1.0%, +1.0%]
Schelling large 🔴 +4.4% [+3.3%, +5.4%] 🔴 +14.8% [+11.9%, +18.5%]
WolfSheep small 🔵 +2.8% [+2.2%, +3.3%] 🔵 +1.7% [+0.2%, +3.1%]
WolfSheep large 🔵 +1.8% [-1.0%, +5.4%] 🔵 +2.8% [+0.0%, +5.9%]
SugarscapeG1mt small 🔴 +10.7% [+8.2%, +13.2%] 🔵 +1.5% [-0.7%, +3.7%]
SugarscapeG1mt large 🔵 -0.3% [-3.8%, +3.4%] 🔵 -0.1% [-2.9%, +2.8%]
BoidFlockers small 🟢 -4.0% [-4.7%, -3.2%] 🔵 -3.6% [-4.5%, -2.5%]
BoidFlockers large 🔵 -2.7% [-3.2%, -2.2%] 🔵 -3.4% [-4.6%, -1.8%]
MultiLevelAllianceModel small 🔵 -2.2% [-4.0%, -0.3%] 🔵 -1.6% [-2.6%, -0.7%]
MultiLevelAllianceModel large 🔵 -1.4% [-3.1%, +0.2%] 🟢 -10.5% [-14.0%, -7.3%]

@quaquel
quaquel merged commit be399e6 into mesa:main Nov 22, 2026
3 of 4 checks passed
quaquel added a commit to quaquel/mesa that referenced this pull request Nov 22, 2026
- Add TableDataSet.get_rows_since(index), so BaseDataRecorder only
  tracks the cursor and no longer imports TableDataSet. The recorder
  looks the method up on the dataset's type, so only dataset classes
  that define it qualify.
- Document that window_size keeps the rows of the last N collections
  for a TableDataSet.
- Add a test that adds rows on only some steps and runs twice.
- Drop the dropna in the table test now that mesa#4168 is merged.
- Import TableDataSet at the top of the test module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW
quaquel added a commit that referenced this pull request Nov 25, 2026
* Fix TableDataSet rows being duplicated by data recorders

TableDataSet.data returns its live, append-only rows list. The recorders
stored it by reference on every collection, so all stored blocks aliased
the same list, and even with a copy each collection re-recorded the full
table (1+2+...+n rows with wrong timestamps).

BaseDataRecorder now takes a snapshot via _take_snapshot(): for a
TableDataSet it returns a new list holding only the rows added since the
previous collection (re-including the replaced block's rows on an
overwrite). Row dicts are shared with the dataset rather than copied,
since neither the dataset nor the recorders mutate them; the
TableDataSet.data docstring documents this. Other datasets are unchanged.

Adds a regression test across DataRecorder, JSONDataRecorder,
ParquetDataRecorder and SQLDataRecorder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW

* Address review feedback on TableDataSet recording

- Add TableDataSet.get_rows_since(index), so BaseDataRecorder only
  tracks the cursor and no longer imports TableDataSet. The recorder
  looks the method up on the dataset's type, so only dataset classes
  that define it qualify.
- Document that window_size keeps the rows of the last N collections
  for a TableDataSet.
- Add a test that adds rows on only some steps and runs twice.
- Drop the dropna in the table test now that #4168 is merged.
- Import TableDataSet at the top of the test module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW

* Never overwrite rows of append-only datasets

Collecting again at an already recorded time (for example, the end of a
run followed by collect()) made the next recording of that time an
overwrite. The backends delete what is stored for that time on an
overwrite, but the cursor only re-supplied rows from the latest
collection, so rows were lost (e.g., run_for(2), collect(), run_for(1)
dropped the t=2 row in DataRecorder, ParquetDataRecorder and
SQLDataRecorder).

Rows of an append-only dataset never change once added, so an overwrite
has nothing to correct. The recorder now skips overwrites for such
datasets and only records rows it has not recorded yet, tracked with a
single forward-only counter per dataset. _is_append_only() holds the
class-level get_rows_since check.

Adds a regression test for the case above across all four recorders.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW

* Record datasets through a single method on all collection paths

The time-change, run-end and manual collect() paths each decided
whether to overwrite, took a snapshot and stored it. The rule that
append-only datasets are never overwritten lived only in the first two;
collect() did not need it because it never overwrites.

Move these steps into _record_dataset(), used by all three paths. This
changes no behavior today, but keeps the rule in one place: once
collect() overwrites at an already recorded time (as #4148 proposes),
a table's recorded rows would otherwise be deleted at that time while
the snapshot only holds the new rows.

Adds a test that an overwrite never deletes the recorded rows of an
append-only dataset, across all four recorders.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
EwoutH pushed a commit that referenced this pull request Nov 26, 2026
- DataRecorder: an overwrite only replaces the last block if it holds
  the same time. When a run ended with an empty agent snapshot, which
  stores no block, the next run's overwrite removed the previous time's
  data instead.
- JSONDataRecorder: a snapshot taken without agents no longer adds a row
  with value=[] and NaN ids.
- JSON, Parquet and SQL recorders return an empty frame with a time
  column when nothing has been recorded, as DataRecorder does. The
  scenario runner rejects a frame without columns as a failed outcome.

Part of #4165.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Jan Kwakkel <[email protected]>

Adapted for 3.5.x: 3.5 recorders do not record when a run ends (#4041 is 4.0-only), so an overwrite of the last block never happens and the DataRecorder part is dropped, with its test and the test_run_ended test that main has next to it. The JSONDataRecorder empty-snapshot fix and the empty frame with a time column for the JSON, Parquet and SQL recorders are taken unchanged.

(cherry picked from commit be399e6)

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants