Coverage for .tox/p311/lib/python3.10/site-packages/scicom/historicalletters/server.py: 100%
22 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 13:41 +0200
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 13:41 +0200
1"""A visualization interface for HistoricalLetters using the older visualization option of mesa."""
2import mesa
3import mesa_geo as mg
4from matplotlib import colors
5from mesa.visualization.modules import ChartVisualization
7from scicom.historicalletters.agents import RegionAgent, SenderAgent
8from scicom.historicalletters.model import HistoricalLetters
10model_params = {
11 "population": mesa.visualization.Slider(
12 "Number of persons",
13 50, 10, 100, 10,
14 description="Choose how many senders to include in the model.",
15 ),
16 "useSocialNetwork": mesa.visualization.Checkbox(
17 "Create initial social network of agents",
18 False,
19 ),
20 "useActivation": mesa.visualization.Checkbox(
21 "Use heterogenous activations",
22 False,
23 ),
24 "similarityThreshold": mesa.visualization.Slider(
25 "Similarity threshold",
26 0.5, 0.0, 1.0, 0.1,
27 description="Choose how similar two agents topics have to be, to send a letter.",
28 ),
29 "moveRange": mesa.visualization.Slider(
30 "Range for moving position</br>(% of mean agent distances)",
31 0.01, 0.00, 0.5, 0.05,
32 description="Choose the visibility range for finding potential locations to move to.",
33 ),
34 "letterRange": mesa.visualization.Slider(
35 "Range for letter sending</br>(% of mean agent distances)",
36 0.2, 0.1, 1.0, 0.1,
37 description="Choose the visibility range for finding potential recipients.",
38 ),
39}
42def topic_draw(agent:mg.GeoAgent) -> dict:
43 """Define visualization strategies for agents.
45 Region agents get the main color as a mean of
46 all region agents topic vectors.
48 Sender agetns have the color of their current
49 topic vector.
50 """
51 portrayal = {}
52 if isinstance(agent, RegionAgent):
53 color = colors.to_hex(agent.has_main_topic())
54 portrayal["color"] = color
55 elif isinstance(agent, SenderAgent):
56 colortuple = set(agent.topicVec)
57 portrayal["radius"] = 5
58 portrayal["shape"] = "circle"
59 portrayal["color"] = colors.to_hex(colortuple)
60 portrayal["description"] = str(agent.unique_id)
61 return portrayal
63map_element = mg.visualization.MapModule(
64 portrayal_method=topic_draw,
65 view=[52, 12],
66 zoom=4,
67 map_width=700,
68 map_height=500,
69)
71chart = ChartVisualization.ChartModule(
72 [
73 {"Label": "Movements", "Color": "red"},
74 {"Label": "Clusters", "Color": "black"},
75 {"Label": "Letters", "Color": "green"},
76 ],
77 data_collector_name="datacollector",
78)
81server = mesa.visualization.ModularServer(
82 HistoricalLetters,
83 [map_element, chart],
84 "Historical Letters",
85 model_params,
86)