Add deprecation warnings to DataCollector
Hi!
While reviewing the Mesa 4 development tracking issue (#3132), I noticed the task "Add deprecation warnings to DataCollector." It does not appear that there is a dedicated issue for this task yet, so I’m opening this issue to discuss the implementation.
Since DataCollector is planned to be removed in Mesa 4.0, it may be helpful to emit a deprecation warning when it is used, so users are aware that the class will be removed in a future version.
One possible approach would be adding a DeprecationWarning using Python’s warnings module in mesa/datacollection.py, for example when DataCollector is instantiated.
Before starting implementation, I wanted to confirm:
- Whether this task is still planned for Mesa 4.0
- If there are specific locations where the warning should be added
- Whether there is a preferred pattern for deprecation warnings in Mesa
If this approach sounds reasonable, I would be happy to work on the implementation and open a PR.
Thanks!
- yes it is still the plan
- Please read the developer guidelines or do a search. There is an agreed-upon way to handle deprecations
- Adding a warning will break all CI because we don't allow warnings. So, the issue is quite a bit more complicated than simply adding the warning.
Thanks for the clarification.
From the Mesa 4 tracking issue (#3132), it seems that the new data collection system (DataRegistry, DataSet, and DataRecorder) has already been implemented, and DataCollector is planned to be removed in Mesa 4.0.
My understanding is that the next step is to add a deprecation warning when DataCollector is used, guiding users toward the new data collection system.
I'll look through the codebase to see how deprecation warnings are implemented elsewhere and propose an implementation approach that follows Mesa's deprecation policy and does not break CI.
I’ll propose an implementation approach here before opening a PR.
Following our deprecation policy the first step is to add a PendingDeprecationWarning. Our CI is currently configured to show PendingDeprecationWarnings, but not fail the tests on them. So then you can fix them one by one.
Following our deprecation policy the first step is to add a
PendingDeprecationWarning. Our CI is currently configured to showPendingDeprecationWarnings, but not fail the tests on them. So then you can fix them one by one.
Thanks for the clarification!
I’ve updated my PR to follow this approach and added a PendingDeprecationWarning as suggested.
Please let me know if this aligns with the expected deprecation policy or if any further changes are needed.
the DataCollector is heavily locked-in as the sole historical data source for plotting components via model.datacollector.get_model_vars_dataframe().
The breakage will happen exactly here:
https://github.com/mesa/mesa/blob/7ef1afafa40f1ff6eff0c3c994f600140895107b/mesa/visualization/components/altair_components.py#L459-L517
https://github.com/mesa/mesa/blob/7ef1afafa40f1ff6eff0c3c994f600140895107b/mesa/visualization/components/matplotlib_components.py#L133-L177
The lock-in is isolated entirely to the charting components (PlotAltair, PlotMatplotlib), which currently force a massive historical pull via model.datacollector.get_model_vars_dataframe() on every render.
To achieve our vision of streaming data to the UI without locking into the new DataRecorder or DataCollector, we can leverage the signals architecture:
- Give the chart its own transient memory (
history_df = solara.use_reactive(...)). - Since
model.timeis now anObservable, the chart simply subscribes to the model's clock and appropriate Model Signals(such asRUN_ENDEDetc) - On every tick, the chart fetches just the current metric from the model and appends it to its own local buffer.
With this, the frontend will no longer care if the backend is using DataCollector, DataRecorder, or nothing at all. I am curious to get @quaquel perspective on it. I think it aligns with your vision
Just to confirm also in writing, the outline sketched by @codebreaker32 is completely in line with what I would like to see. One minor point is that we will need a configuration object analogous to what is in place for the datarecorder to control how often plots are updated. Moreover, we will, in the longer term, also need to ensure that all other relevant parts of the UI are properly linked to model.time (e.g., visualization of spaces).
Glad we are aligned on the architecture, the next question arises is How should we word the deprecation message to handle this split use-case without confusing users? Should we link directly to a new section in the migration guide that explains the two different paths?
Not entirely sure what you mean by the two paths. For me, this architectural change is MESA 4 only, so we can put it under that in the migration guide. It makes sense to have a section on architectural changes and this UI stuff can be covered there I guess.
I meant that when a user sees the deprecation warning in 3.5.x, they might be doing one of two very different things:
- Running a headless model or parameter sweep. (They just need to swap DataCollector for DataRecorder or use new Scenario stuff for batch_run).
- Trying to build a custom dashboard or chart using
mesa.visualisation. (Warnings to them is redundant because we are anyway going to change it internally which won't affect public APIs).
Thanks for working on this.
As a quick note, I highly prefer FutureWarnings only to be added once a stable alternative is in place. For 3.x, that would be after 4.0 stable is released.