Skip to content

SpaceRenderer question #2874

@quaquel

Description

@quaquel

#2803 introduced the SpaceRenderer class. I am now using this for the first time, and I am a bit confused about how to use it. Following e.g., the Boltzmann example, I should do

def agent_portrayal(agent)
	return AgentPortrayalStyle(color=agent.wealth)

renderer = SpaceRenderer(model)
renderer.draw_agents(agent_portrayal=agent_portrayal, cmap="viridis", vmin=0, vmax=10)

page = SolaraViz(
    model,
    renderer,
    components=[GiniPlot],
    model_params=model_params,
    name="Boltzmann Wealth Model",
)

In draw_agents two attributes are then set for the first time: agent_portrayal and agent_kwargs. This, in my view, is poor design. Methods should update state, not add new state variables. Moreover, if I forget to call draw_agents before instantiating SolaraViz with renderer, I guess no agents will ever be drawn, nor can I pass the various arguments (i.e., agent_portrayal, cmap, vmin, and vmax).

Moreover, because draw_agents in turn calls the backend and if your backend is matplotlib, you are creating a new figure but not showing it (i.e., plt.show()) is not called. If you are using SolaraViz inside a jupyter notebook, this leaves an unplotted figure hanging. This will be rendered wherever you next call plt.show(), even though this might be far away from where you made the solara vizualization.

@Sahil-Chhoker, is my interpretation of how the render works correct and if so, what are your thoughts on moving agent_portrayal and agent_kwargs into the __init__ of the SpaceRenderer. As a minor point, we might want to make agent_kwargs a bit more descriptive (e.g., draw_agent_kwargs).

So to be clear, my envisioned API would become

def agent_portrayal(agent)
	return AgentPortrayalStyle(color=agent.wealth)

renderer = SpaceRenderer(model, agent_portrayal=agent_portrayal,
						 draw_agent_kwargs={"cmap"="viridis", "vmin"=0, "vmax"=10})

page = SolaraViz(
    model,
    renderer,
    components=[GiniPlot],
    model_params=model_params,
    name="Boltzmann Wealth Model",
)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions