Coverage for .tox/p313/lib/python3.13/site-packages/scicom/knowledgespread/server.py: 0%
65 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-09 16:53 +1200
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-09 16:53 +1200
1import altair as alt
2import mesa
3import networkx as nx
4import nx_altair as nxa
5import pandas as pd
6import solara
7from matplotlib import colors
8from mesa.visualization.modules import ChartVisualization
10from scicom.knowledgespread.model import KnowledgeSpread
11from scicom.knowledgespread.SimpleContinuousModule import SimpleCanvas
12from scicom.knowledgespread.utils import ageFunction
14model_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}
44def 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 }
56def 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)
72def 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)
89def 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)
121def 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)
149def 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
161chart = ChartVisualization.ChartModule(
162 [
163 {"Label": "Active Agents", "Color": "#000000"},
164 {"Label": "Graph components", "Color": "black"},
165 ],
166 data_collector_name="datacollector",
167)
170epistemic_canvas = SimpleCanvas(agent_draw, 720, 720)
173server = mesa.visualization.ModularServer(
174 KnowledgeSpread,
175 [epistemic_canvas, chart],
176 "Knowledge spread",
177 model_params,
178)