Support Scenarios in SolaraViz visualization#3178
Conversation
Mesa's Scenario support was previously not integrated with SolaraViz, causing model reset failures when parameters belonging to a Scenario were passed directly to the Model's __init__. This change adds first-class support for Scenarios in the visualization pipeline. SolaraViz now accepts a [scenario_params](cci:1://file://wsl.localhost/Ubuntu/root/mesa/tests/visualization/test_solara_viz_scenarios.py:31:4-37:119) argument and can also auto-detect which parameters belong to a Scenario by inspecting the model instance and class signatures. When the model is reset, SolaraViz correctly reconstructs the Scenario object from the updated parameters before instantiating the Model. This ensures that Scenario-owned variables are correctly routed and do not cause initialization errors. The UI is also updated to show Scenario parameters in their own section within the sidebar.
|
Performance benchmarks:
|
Mesa's Scenario support was previously not integrated with SolaraViz, causing model reset failures when parameters belonging to a Scenario were passed directly to the Model's __init__. This change adds first-class support for Scenarios in the visualization pipeline. SolaraViz now accepts a [scenario_params](cci:1://file://wsl.localhost/Ubuntu/root/mesa/tests/visualization/test_solara_viz_scenarios.py:31:4-37:119) argument and can also auto-detect which parameters belong to a Scenario by inspecting the model instance and class signatures. When the model is reset, SolaraViz correctly reconstructs the Scenario object from the updated parameters before instantiating the Model. This ensures that Scenario-owned variables are correctly routed and do not cause initialization errors. The UI is also updated to show Scenario parameters in their own section within the sidebar.
Mesa's Scenario support was previously not integrated with SolaraViz, causing model reset failures when parameters belonging to a Scenario were passed directly to the Model's __init__. This change adds first-class support for Scenarios in the visualization pipeline. SolaraViz now accepts a [scenario_params](cci:1://file://wsl.localhost/Ubuntu/root/mesa/tests/visualization/test_solara_viz_scenarios.py:31:4-37:119) argument and can also auto-detect which parameters belong to a Scenario by inspecting the model instance and class signatures. When the model is reset, SolaraViz correctly reconstructs the Scenario object from the updated parameters before instantiating the Model. This ensures that Scenario-owned variables are correctly routed and do not cause initialization errors. The UI is also updated to show Scenario parameters in their own section within the sidebar.
Mesa's Scenario support was previously not integrated with SolaraViz, causing model reset failures when parameters belonging to a Scenario were passed directly to the Model's __init__. This change adds first-class support for Scenarios in the visualization pipeline. SolaraViz now accepts a [scenario_params](cci:1://file://wsl.localhost/Ubuntu/root/mesa/tests/visualization/test_solara_viz_scenarios.py:31:4-37:119) argument and can also auto-detect which parameters belong to a Scenario by inspecting the model instance and class signatures. When the model is reset, SolaraViz correctly reconstructs the Scenario object from the updated parameters before instantiating the Model. This ensures that Scenario-owned variables are correctly routed and do not cause initialization errors. The UI is also updated to show Scenario parameters in their own section within the sidebar.
6896932 to
cbf1127
Compare
|
Thanks for the PR. However, I feel we didn’t completere finish the discussion in #3176 on how we would integrate scenario variables exactly. Let’s get full consensus on the design first. API design is important, since once we commit to it, we have to support it for a long time. |
|
I suggest simplifying this by only solving the current scenario problem. That is, keep the reflection stuff to figure out what is in the scenario object and what is on the model init, and use that for the ModelController. Leave the slider stuff untouched. |
Mesa's Scenario support was previously not integrated with SolaraViz, causing model reset failures when parameters belonging to a Scenario were passed directly to the Model's __init__. This change adds first-class support for Scenarios in the visualization pipeline. SolaraViz now accepts a [scenario_params](cci:1://file://wsl.localhost/Ubuntu/root/mesa/tests/visualization/test_solara_viz_scenarios.py:31:4-37:119) argument and can also auto-detect which parameters belong to a Scenario by inspecting the model instance and class signatures. When the model is reset, SolaraViz correctly reconstructs the Scenario object from the updated parameters before instantiating the Model. This ensures that Scenario-owned variables are correctly routed and do not cause initialization errors. The UI is also updated to show Scenario parameters in their own section within the sidebar.
Removes the explicit 'scenario_params' argument from SolaraViz and related components. Scenario parameters are now automatically detected and seperated from 'model_params'during model reset, simplifying the public API.
Removes the explicit 'scenario_params' argument from SolaraViz and related components. Scenario parameters are now automatically detected and seperated from 'model_params'during model reset, simplifying the public API.
4f1b10f to
6555574
Compare
|
@quaquel Any thoughts here? |
|
Please include test code in the existing test files. @Sahil-Chhoker, I would appreciate your input here as well. Basically, for now, I just want to ensure that solara-viz is compatible with scenarios. |
Integrate Mesa's Scenario system with SolaraViz to enable proper parameter routing between Model and Scenario during visualization and model resets. Previously, SolaraViz would fail when models used Scenarios because scenario parameters were incorrectly passed to Model.__init__, causing TypeError exceptions. This change adds intelligent parameter splitting that automatically detects which parameters belong to the Scenario vs the Model by inspecting their respective signatures. When a model is reset, the Scenario is correctly reconstructed with the updated parameters. The implementation updates ModelController, SimulatorController, and ModelCreator components to handle scenario-aware parameter routing while maintaining full backward compatibility. Tests are added to both test_solara_viz.py (core functionality) and test_solara_viz_scenarios.py (scenario-specific edge cases).
c940f34 to
3776e14
Compare
|
Performance benchmarks:
|
|
Performance benchmarks:
|
I am currently testing the visualization using |
|
I had a closer look at this and I know what is going on. First, revert your last commit. Errors should never be past over in silence. Also, it is not the root cause of the problem My problem now happens when I hit reset. This trigger indirectly The solution is to change how we recreate the scenario instance in for example for key, value in model_parameters.items():
if key == 'rng':
scenario_kwargs['key'] = value
elif key in scenario_defaults and key not in model_init_params:
scenario_kwargs[key] = value
else:
kwargs[key] = valuefixes the problem. This is clearly hacky, but it shows that my diagnosis is correct. A slightly more elabore solution is to change def _get_scenario_defaults(scenario: Scenario) -> dict[str, Any]:
"""Return defaults for a scenario instance, validating scenario type."""
scenario_class = type(scenario)
if not issubclass(scenario_class, Scenario):
raise ValueError(
"Expected model.scenario to be a subclass instance of mesa.experimental.scenarios.Scenario."
)
defaults = getattr(scenario, "_scenario_defaults", {})
defaults['rng'] = None
return defaults |
tests/examples/test_examples_viz.py
Outdated
| ) | ||
|
|
||
|
|
||
| def _screenshot_first_image(page, attempts=3, wait_ms=100): |
There was a problem hiding this comment.
What is going on with these new functions? How is this relevant to the scenario issue? If it is not relevant, please consider submitting it as a separate pr
There was a problem hiding this comment.
sorry these were not meant to be pushed (Might have messed up while checking out with uncommitted changes)
8736603 to
79172ff
Compare
|
It works now on my end. I will give the code another quick check, but it seems we are almost there. |
| @pytest.mark.skip( | ||
| reason="Requires interactive Solara state handling; covered indirectly." | ||
| ) | ||
| def test_reset_with_scenario(): |
There was a problem hiding this comment.
I understand that you skip this, but you might want to test _build_model_init_kwargs. It would have helped in finding the bug.
There was a problem hiding this comment.
Okay I'll replace the skipped test with a concrete _build_model_init_kwargs test
Thank you for the help! |
|
Great to see the progress! I would like to also have @Sahil-Chhoker’s take on it. |
|
I have tested this locally and can confirm it works. I am merging this PR, and we'll see if #3167 also works. @Sahil-Chhoker, I would still appreciate your thoughts even post-merge. |
|
Thanks for this effort, glad to see it merged! |
Summary
This PR adds first-class support for
ScenariosinSolaraViz. It allows users to visualize and interact with models that utilize the new MesaScenariosystem, ensuring that parameters are correctly routed to either the Model or the Scenario during instantiation and resets.Motive
With the introduction of
Scenariosin Mesa (#3103, #3168), there was a disconnect in the visualization layer.SolaraVizpreviously tried to pass all user-defined sliders and parameters directly intoModel.__init__. This caused aTypeErrorwhen a model used a Scenario, because parameters intended for the Scenario were rejected by the Model's initialization function. This PR solves the routing problem and provides a clear UI separation between Model and Scenario parameters.Implementation
The implementation follows the "Option 1" (Explicit API) and "Option 2" (Auto-detection) suggestions from the issue:
SolaraVizParameter Routing: EnhancedSolaraVizto automatically introspect the model'sscenarioattribute and intelligently split parameters between Model and Scenario based on their respective signatures.ModelControllerandSimulatorController. When the "Reset" button is clicked, it now captures the current values of scenario sliders, reconstructs the specificScenariosubclass instance, and passes it into theModelconstructor via thescenariokeyword argument.ModelCreatorto render Scenario-bound sliders in a dedicated "Scenario Parameters" section beneath the main Model parameters, providing visual clarity on parameter ownership.tests/visualization/test_solara_viz.py(testing core functionality like parameter splitting logic,ModelCreatorwith scenarios, andSolaraVizintegration) andtests/visualization/test_solara_viz_scenarios.py(testing scenario-specific edge cases and type hints).Usage Examples
Auto Detection:
If your sliders match field names in your
Scenarioclass,SolaraVizhandles the routing automatically:Additional Notes
This change is fully backward compatible. Models not using Scenarios will function exactly as before.
Fixes: #3176