Coverage for .tox/p313/lib/python3.13/site-packages/scicom/knowledgespread/server.py: 0%

1 import altair as alt

2 import mesa

3 import networkx as nx

4 import nx_altair as nxa

5 import pandas as pd

6 import solara

7 from matplotlib import colors

8 from mesa.visualization.modules import ChartVisualization

9

10 from scicom.knowledgespread.model import KnowledgeSpread

11 from scicom.knowledgespread.SimpleContinuousModule import SimpleCanvas

12 from scicom.knowledgespread.utils import ageFunction

13

14 model_params = {

15 "num_scientists": mesa.visualization.Slider(

16 "Initial number of scientists",

17 100, 10, 200, 10,

18 ),

19 "num_timesteps": mesa.visualization.Slider(

20 "How long is the number of agents growing?",

21 20, 5, 100, 5,

22 ),

23 "oppositionPercent": mesa.visualization.Slider(

24 "Percentage of opposing agents",

25 0.05, 0, 0.5, 0.05,

26 ),

27 "epiInit": mesa.visualization.Choice(

28 "Choose initial conditions for epistemic space",

29 value=("complex"),

30 choices=["complex", "central", "polarized"],

31 ),

32 "timeInit": mesa.visualization.Choice(

33 "Choose initial conditions for population growth.",

34 value=("saturate"),

35 choices=["saturate", "linear", "exponential"],

36 ),

37 "epiRange": mesa.visualization.Slider(

38 "Basic range of visibility in epistemic space",

39 0.005, 0.001, 0.03, 0.001,

40 ),

41}

42

43

44 def agent_draw_altair(agent) -> dict:

45 """Represent agents."""

46 colortuple = set(agent.topicledger[-1])

47 color = colors.to_hex(colortuple)

48 size = ageFunction(agent, agent.a, agent.b, agent.c, 10) if agent.age > 0 else 0.001

49 return {

50 "opposition": agent.opposition,

51 "size": size,

52 "Color": color,

53 "color": color,

54 }

55

56 def chart_draw_altair_agents(model):

57 data = model.datacollector.get_model_vars_dataframe().reset_index()

58 chart = (

59 alt.Chart(data)

60 .mark_bar(color="orange")

61 .encode(

62 x=alt.X("index", title="Step"),

63 y=alt.Y(

64 "Active Agents",

65 title="Active agents",

66 ),

67 )

68 )

69 return solara.FigureAltair(chart)

70

71

72 def chart_draw_altair_communities(model):

73 data = model.datacollector.get_model_vars_dataframe().reset_index()

74 data.insert(0, "compLen", data["Graph structure"].apply(lambda x: len(x)))

75 chart = (

76 alt.Chart(data)

77 .mark_bar(color="orange")

78 .encode(

79 x=alt.X("index", title="Step"),

80 y=alt.Y(

81 "compLen",

82 title="Communities in social network",

83 ),

84 )

85 )

86 return solara.FigureAltair(chart)

87

88

89 def epiSpace_draw_altair(model, agent_portrayal):

90 isOpposed = (False, True)

91 shape_range = ("circle", "triangle-up")

92 all_agent_data = []

93 if not model.schedule.agents:

94 return solara.Markdown("## Finished run")

95 else:

96 for agent in model.schedule.agents:

97 cur_agent = agent_draw_altair(agent)

98 cur_agent["x"] = agent.pos[0]

99 cur_agent["y"] = agent.pos[1]

100 all_agent_data.append(cur_agent)

101 df = pd.DataFrame(all_agent_data)

102 colors = list(set(a["color"] for a in all_agent_data))

103 chart_color = alt.Color("color").legend(None).scale(domain=colors, range=colors)

104 chart = (

105 alt.Chart(df)

106 .mark_point(filled=True)

107 .encode(

108 x=alt.X("x", axis=None), # no x-axis label

109 y=alt.Y("y", axis=None), # no y-axis label

110 size=alt.Size("size", title="current activity"), # relabel size for legend

111 color=chart_color,

112 shape=alt.Shape( # use shape to indicate choice

113 "opposition", scale=alt.Scale(domain=isOpposed, range=shape_range),

114 ),

115 )

116 .configure_view(strokeOpacity=0) # hide grid/chart lines

117 )

118 return solara.FigureAltair(chart)

119

120

121 def socialNetwork_draw_altair(model):

122 currentTime = model.schedule.time

123 H = model.socialNetwork

124 Graph = nx.Graph(((u, v, e) for u, v, e in H.edges(data=True) if e["time"] <= currentTime))

125 pos = nx.kamada_kawai_layout(Graph)

126 between = nx.betweenness_centrality(Graph)

127 nx.set_node_attributes(Graph, between, "between")

128 communities = list(nx.community.label_propagation_communities(Graph))

129 for f in Graph.nodes():

130 for i, c in enumerate(communities):

131 if f in c:

132 Graph.nodes()[f].update(

133 {

134 "community": str(i),

135 "name": f,

136 },

137 )

138 chart = nxa.draw_networkx(

139 G=Graph,

140 pos=pos,

141 node_color="between",

142 edge_color="time",

143 cmap="viridis",

144 )

145 chart.configure_view(strokeOpacity=0) # hide grid/chart lines

146 return solara.FigureAltair(chart)

147

148

149 def agent_draw(agent):

150 colortuple = set(agent.topicledger[-1])

151 color = "#" + "".join(format(int(round(val * 255)), "02x") for val in colortuple)

152 probDict = {

153 "Shape": "circle",

154 "r": ageFunction(agent),

155 "Filled": "true",

156 "Color": color,

157 }

158 return probDict

159

160

161 chart = ChartVisualization.ChartModule(

162 [

163 {"Label": "Active Agents", "Color": "#000000"},

164 {"Label": "Graph components", "Color": "black"},

165 ],

166 data_collector_name="datacollector",

167)

168

169

170 epistemic_canvas = SimpleCanvas(agent_draw, 720, 720)

171

172

173 server = mesa.visualization.ModularServer(

174 KnowledgeSpread,

175 [epistemic_canvas, chart],

176 "Knowledge spread",

177 model_params,

178)