Skip to content

Commit 9240813

Browse files
authored
update migration guide to describe solaraviz updates (#2297)
1 parent 0cc8515 commit 9240813

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

docs/migration_guide.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,67 @@ You can access it by `Model.steps`, and it's internally in the datacollector, ba
6464

6565

6666
### Visualisation
67-
<!-- TODO -->
67+
68+
Mesa has adopted a new API for our frontend. If you already migrated to the experimental new SolaraViz you can still use
69+
the import from mesa.experimental. Otherwise here is a list of things you need to change.
70+
71+
#### Model Initialization
72+
73+
Previously SolaraViz was initialized by providing a `model_cls` and a `model_params`. This has changed to expect a model instance `model`. You can still provide (user-settable) `model_params`, but only if users should be able to change them. It is now also possible to pass in a "reactive model" by first calling `model = solara.reactive(model)`. This is useful for notebook environments. It allows you to pass the model to the SolaraViz Module, but continue to use the model. For example calling `model.value.step()` (notice the extra .value) will automatically update the plots. This currently only automatically works for the step method, you can force visualization updates by calling `model.value.force_update()`.
74+
75+
#### Default space visualization
76+
77+
Previously we included a default space drawer that you could configure with an `agent_portrayal` function. You now have to explicitly create a space drawer with the `agent_portrayal` function
78+
79+
```python
80+
# old
81+
from mesa.experimental import SolaraViz
82+
83+
SolaraViz(model_cls, model_params, agent_portrayal=agent_portrayal)
84+
85+
# new
86+
from mesa.visualization import SolaraViz, make_space_matplotlib
87+
88+
SolaraViz(model, components=[make_space_matplotlib(agent_portrayal)])
89+
```
90+
91+
#### Plotting "measures"
92+
93+
"Measure" plots also need to be made explicit here. Previously, measure could either be 1) A function that receives a model and returns a solara component or 2) A string or list of string of variables that are collected by the datacollector and are to be plotted as a line plot. 1) still works, but you can pass that function to "components" directly. 2) needs to explicitly call the `make_plot_measure()`function.
94+
95+
```python
96+
# old
97+
from mesa.experimental import SolaraViz
98+
99+
def make_plot(model):
100+
...
101+
102+
SolaraViz(model_cls, model_params, measures=[make_plot, "foo", ["bar", "baz"]])
103+
104+
# new
105+
from mesa.visualization import SolaraViz, make_plot_measure
106+
107+
SolaraViz(model, components=[make_plot, make_plot_measure("foo"), make_plot_measure("bar", "baz")])
108+
```
109+
110+
#### Plotting text
111+
112+
To plot model-dependent text the experimental SolaraViz provided a `make_text` function that wraps another functions that receives the model and turns its string return value into a solara text component. Again, this other function can now be passed directly to the new SolaraViz components array. It is okay if your function just returns a string.
113+
114+
```python
115+
# old
116+
from mesa.experimental import SolaraViz, make_text
117+
118+
def show_steps(model):
119+
return f"Steps: {model.steps}"
120+
121+
SolaraViz(model_cls, model_params, measures=make_text(show_steps))
122+
123+
# new
124+
from mesa.visualisation import SolaraViz
125+
126+
def show_steps(model):
127+
return f"Steps: {model.steps}"
128+
129+
SolaraViz(model, components=[show_steps])
130+
```

0 commit comments

Comments
 (0)