Coverage for .tox/p313/lib/python3.13/site-packages/scicom/historicalletters/interface.py: 100%

1"""Define the solara interface to run and control the HistoricalLetters model."""

2 from __future__ import annotations

3

4 import matplotlib.pyplot as plt

5 import mesa_geo as mg

6 import mesa_geo.visualization as mgv

7 import solara

8

9 from mesa.visualization import SolaraViz, make_plot_component

10 from mesa_geo.visualization import make_geospace_component

11

12 import xyzservices.providers as xyz

13 from matplotlib import colors

14 from matplotlib.figure import Figure

15

16 from scicom.historicalletters.agents import (

17 RegionAgent,

18 SenderAgent,

19)

20 from scicom.historicalletters.model import HistoricalLetters

21

22

23 def agent_draw(agent: mg.GeoAgent) -> dict:

24 """Define visualization strategies for agents.

25

26 Region agents get the main color as a mean of

27 all region agents topic vectors.

28

29 Sender agents have the color of their current

30 topic vector.

31 """

32 portrayal = {}

33 if isinstance(agent, RegionAgent):

34 color = colors.to_hex(agent.has_main_topic())

35 portrayal["color"] = color

36 elif isinstance(agent, SenderAgent):

37 colortuple = set(agent.topicVec)

38 portrayal["marker_type"] = "AwesomeIcon"

39 portrayal["name"] = "fas fa-male"

40 portrayal["icon_properties"] = {

41 "marker_color": "white",

42 "icon_color": colors.to_hex(colortuple),

43 }

44 portrayal["description"] = str(agent.unique_id)

45 return portrayal

46

47 model_params = {

48 "population": {

49 "type": "SliderInt",

50 "value": 50,

51 "label": "Number of agents",

52 "min": 10,

53 "max": 200,

54 "step": 10,

55 },

56 "useSocialNetwork": {

57 "type": "Select",

58 "value": False,

59 "label": "Choose if an initial social network exists.",

60 "values": [True, False],

61 },

62 "useActivation": {

63 "type": "Select",

64 "value": False,

65 "label": "Choose if agents have heterogeneous acitvations.",

66 "values": [True, False],

67 },

68 "similarityThreshold": {

69 "type": "SliderFloat",

70 "value": 0.5,

71 "label": "Threshold for similarity of topics.",

72 "min": 0.0,

73 "max": 1.0,

74 "step": 0.1,

75 },

76 "moveRange": {

77 "type": "SliderFloat",

78 "value": 0.05,

79 "label": "Range defining targets for movements.",

80 "min": 0.0,

81 "max": 0.5,

82 "step": 0.1,

83 },

84 "letterRange": {

85 "type": "SliderFloat",

86 "value": 0.2,

87 "label": "Range for sending letters.",

88 "min": 0.0,

89 "max": 0.5,

90 "step": 0.1,

91 },

92}

93

94 model = HistoricalLetters()

95 page = SolaraViz(

96 model,

97 name="Historical Letters ABM",

98 model_params=model_params,

99 components=[

100 make_geospace_component(

101 agent_draw,

102 zoom=4,

103 view=[47,12],

104 tiles=xyz.OpenTopoMap,

105 scroll_wheel_zoom=True

106 ),

107 make_plot_component(["Movements", "Letters", "Clusters"])

108 ],

109)

110 page