Coverage for src/scicom/randomletters/server.py: 0%

1 import mesa

2

3 from .model import LetterSpace

4 from .SimpleContinuousModule import SimpleCanvas

5

6

7 def letter_draw(agent):

8 colortuple = set(agent.topicVec)

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

10 probDict = {

11 "Shape": "circle",

12 "r": agent.numLettersReceived,

13 "Filled": "true",

14 "Color": color

15 }

16 return probDict

17

18

19 def node_draw(G):

20 nodes = []

21 node_label_to_node_id = {}

22 node_id = 0

23 # edge_id = 0

24 for node_label, agents in G.nodes.data("agent"):

25 node = G.nodes()[node_label]

26 agent = agents[0]

27 colortuple = set(agent.topicVec)

28 nodes.append({

29 "size": node["numLettersReceived"],

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

31 "tooltip": f"id: {agent.unique_id}<br>Topics: {agent.topicVec}",

32 })

33 node_label_to_node_id[node_label] = node_id

34 node_id += 1

35

36 portrayal = dict()

37 portrayal["nodes"] = nodes

38 edges = []

39 for (source, target) in G.edges():

40 sourceAgent = [agents for x, agents in G.nodes.data("agent") if x == source]

41 agent = sourceAgent[0][0]

42 edges.append({

43 "source": node_label_to_node_id[source],

44 "target": node_label_to_node_id[target],

45 "color": '#D3D3D3',

46 "width": G.nodes()[source]["numLettersSend"],

47 }

48 )

49 portrayal["edges"] = edges

50

51 return portrayal

52

53

54 letter_canvas = SimpleCanvas(letter_draw, 360, 720)

55

56 network_canvas = mesa.visualization.NetworkModule(node_draw, 360, 720)

57

58

59 model_params = {

60 "population": mesa.visualization.Slider(

61 "Number of persons",

62 50,

63 10,

64 200,

65 10,

66 description="Choose how many persons to include in the model.",

67 ),

68 "updateTopic": mesa.visualization.Slider(

69 "Strength of adoption",

70 0.05,

71 0.01,

72 0.3,

73 0.05,

74 description="Choose how strongly letter sending changes ones topics.",

75 ),

76 "updateHelp": mesa.visualization.StaticText(

77 "Higher value:<br/> A letter sender faster adopts to the topic of a receiver."

78 ),

79 "threshold": mesa.visualization.Slider(

80 "Similarity threshold",

81 0.5,

82 0.0,

83 1.0,

84 0.1,

85 description="Choose how similar two persons topics have to be, to send a letter.",

86 ),

87 "thresholdHelp": mesa.visualization.StaticText(

88 "Higher value:<br/> Sending a letter is less likely."

89 ),

90 "width": 360,

91 "height": 180,

92 "moveRange": mesa.visualization.Slider(

93 "Range for moving position",

94 20,

95 0,

96 100,

97 10,

98 description="Choose the visibility range for finding potential locations to move to.",

99 ),

100 "letterRange": mesa.visualization.Slider(

101 "Range for letter sending",

102 50,

103 0,

104 150,

105 10,

106 description="Choose the visibility range for finding potential receipients.",

107 ),

108 "minSep": 3

109}

110

111 server = mesa.visualization.ModularServer(

112 LetterSpace, [letter_canvas, network_canvas], "Random Letters", model_params

113)