You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, agent_portrayal should return a dict. Valid fields are color, marker, size, and zorder. In #2430 and in a comment on #2389, it has been suggested to shift to an AgentPortrayaStyle class instead. The benefit of this is that is much easier to write your own agent_portrayal function. It also ensures that only valid fields are used. It might make it easier to write agent_portrayal functions that are indifferent about the plotting backend (i.e., altair or matplotlib). Last, it becomes trivial for all draw_x functions in mesa.visualization.compoments.matplotlib to be modifies so agent_portrayal becomes an optional keyword argument.
A while back @wang-boyu also made a remark about this in #1441.
The text was updated successfully, but these errors were encountered:
quaquel
changed the title
Shift from dict for agent_potrayal to an AgentPortrayaStyle class
Shift from dict for agent_potrayal to an AgentPortrayalStyle class
Oct 30, 2024
@quaquel, I personally believe that using a dedicated class for agent portrayal is a great idea. I've implemented a simple version based on my vision.
@dataclassclassAgentPortrayalStyle:
color: str|tuple="tab:blue"marker: str="o"size: int=50zorder: int=1valid_marker: list ["o", "s", "^", ...]
def__post_init__(self):
"""Validate the attributes after initialization."""ifself.colorisnotNone:
# Validate colortry:
mcolors.to_rgb(self.color)
exceptValueError:
raiseValueError(f"Invalid color specification: {self.color}")
ifself.markerisnotNone:
# Validate Markersifself.markernotinVALID_MARKERS:
raiseValueError(f"Invalid marker '{self.marker}'.)
ifself.sizeisnotNoneandnotisinstance(self.size, Number):
raiseValueError(f"Size must be a number, got {type(self.size)}")
ifself.zorderisnotNoneandnotisinstance(self.zorder, int):
raiseValueError(f"Zorder must be an integer, got {type(self.zorder)}")
defto_dict(self) ->dict:
"""Convert the style to a dictionary."""return {k: vfork, vinself.__dict__.items() ifvisnotNone}
This approach can also remain consistent with the current API. I would love your feedback on this, as well as any suggestions for additional features or improvements that could be implemented.
Currently,
agent_portrayal
should return a dict. Valid fields arecolor
,marker
,size
, andzorder
. In #2430 and in a comment on #2389, it has been suggested to shift to anAgentPortrayaStyle
class instead. The benefit of this is that is much easier to write your own agent_portrayal function. It also ensures that only valid fields are used. It might make it easier to write agent_portrayal functions that are indifferent about the plotting backend (i.e., altair or matplotlib). Last, it becomes trivial for alldraw_x
functions inmesa.visualization.compoments.matplotlib
to be modifies so agent_portrayal becomes an optional keyword argument.A while back @wang-boyu also made a remark about this in #1441.
The text was updated successfully, but these errors were encountered: