网络科学导论课程关于SIR模型的python代码,没看懂

问题遇到的现象和发生背景

在上网络科学导论的实验中要求我们利用该代码画出传播范围和有效传播概率的关系,之前没学过python,去网上看了不少有关python和SIR模型视频后仍看不懂这个代码,希望能有学python老哥讲解一下

问题相关代码,请勿粘贴截图

import math
import time
import networkx as nx
import numpy as np
import random
import matplotlib.pyplot as plt

from numba import njit

from scipy import optimize

from collections import namedtuple

def read_graph(filename=None, separator=None):
fname = filename
graph = nx.read_edgelist(fname, delimiter=separator, nodetype=int)
graph = graph.to_undirected()
print('is_connected:', nx.is_connected(graph))
return graph, fname.split('/')[-1]#通过指定分隔符对字符串进行切片

def sir_simulation_model(G=None, trans_rate=None, recov_rate=None, initial_p=None):
nodelist = []
for node in G.nodes:
nodelist.append(node)
node_num = len(nodelist)#返回字符串、列表、字典、元组等长度
#print(node_num)
I_dic = {}#Python 字典 dict() 函数用于创建一个新的字典。
R_dic = {}
S_dic = {}
# initial seeds
seed_list = []
seed_num = int(node_num * initial_p)
while len(seed_list) <= seed_num:
seed_index = random.randint(0, node_num - 1) # the range is from 0 to node_num-1 included 函数返回参数1和参数2之间的任意整数
seed = nodelist[seed_index]
if seed not in seed_list:#右边的内容是否不包含左边的内容,如果不包含返回true,包含返回false
seed_list = seed_list + [seed]
# initial the dicts
for node in nodelist:
if node in seed_list:#右侧的内日那个里面是否包含了左侧的内容
I_dic[node] = [n for n in G.neighbors(node)]
else:
S_dic[node] = [n for n in G.neighbors(node)]
# spread processes
t = 0
while len(I_dic) > 0:
t = t + 1
# tansmit the rumor
newI_dic = {}
for spreader in I_dic:
#print(S_dic[spreader])
neighbor_list = I_dic[spreader]
for node in neighbor_list:
temp = random.random()#用于生成一个0到1的随机符点数: 0 <= n < 1.0
#print(temp,trans_rate,node)
if (temp < trans_rate) and (node in S_dic):
newI_dic[node] = [n for n in G.neighbors(node)]

    # !!!old !!! spreader recover
    newR_dic = {}
    for spreader in I_dic:
        temp = random.random()
        # print(temp,trans_rate,node)
        if temp < recov_rate:
            newR_dic[spreader] = t
    # print('seed',seed_list)
    # print('spreader',S_dic.keys())
    # print('newR',newR_dic.keys())

    # update the nodes in all the dicts
    # remove the recovered nodes   add the new spreader nodes to I_dic
    for node in newI_dic:
        S_dic.pop(node)
    for node in newR_dic:
        I_dic.pop(node)
    I_dic.update(newI_dic)
    R_dic.update(newR_dic)
    test_num = len(S_dic) + len(I_dic) + len(R_dic)
    # print('test num:', test_num)
return len(R_dic) / node_num

def sir_average(G=None, trans_rate=None, recov_rate=None, initial_p=None, repeat_time=None):
# 运行sir_simulation_model,然后取平均, repeat_time为传入参数
temp = 0
for i in range(repeat_time):#只有一个参数,表示从0到这个参数内的所有整数,不包括参数
temp += sir_simulation_model(G=G, trans_rate=trans_rate, recov_rate=recov_rate, initial_p=initial_p)
return temp / repeat_time

if name == 'main':#文件作为脚本直接执行 才会被执行

start = time.time()#用time来计算一下程序执行的时间
graph, title = read_graph(filename='graph_alpha=3.0node200.txt', separator='\t')
title = 'SF30'
u = 0.5
p0 = 0.05
point = 50
lambda_list = []
for i in range(point):
    # lambda_list += [u * (10 ** (i * (-3) / point))]
    lambda_list += [(i+1)*u/point]
lambda_list.sort(reverse=False)
print(lambda_list)
sim_list = []
f = open(title + 'test' + str(u) + '.txt', 'w')
f.write('beta' + '\t' + 'numeircal' + '\n')
beta_list = []
for lam in lambda_list:
    sim = sir_average(G=graph, trans_rate=lam, recov_rate=u, initial_p=p0, repeat_time=2000)
    sim_list += [sim]
    print(lam, sim)
    beta_list += [lam / u]
    f.write(str(lam / u) + '\t' + str(sim) + '\n')
f.close()
plt.plot(beta_list, sim_list, 'ro')
plt.semilogx()#此功能用于以x轴转换为对数格式的方式显示数据,显示更多数量级较小的数据
plt.savefig(title + 'test' + str(u) + '.png')
plt.close()

end = time.time()
print("Elapsed (after compilation) = %s" % (end - start))
运行结果及报错内容

SF30test0.5.txt:
beta numeircal
0.02 0.060690000000001174
0.04 0.06731750000000006
0.06 0.07568249999999963
0.08 0.08555249999999957
0.1 0.09847749999999983
0.12 0.1153549999999999
0.14 0.1354725
0.16 0.1606199999999999
0.18 0.19159999999999977
0.2 0.22607249999999973
0.22 0.27414249999999984
0.24 0.3235499999999995
0.26 0.3754075000000001
0.28 0.4364399999999999
0.3 0.4889000000000004
0.32 0.5422899999999993
0.34 0.5864699999999999
0.36 0.6255674999999996
0.38 0.6659274999999995
0.4 0.6966275000000002
0.42 0.7275349999999993
0.44 0.7541100000000002
0.46 0.7791250000000016
0.48 0.7955925000000011
0.5 0.8166924999999983
0.52 0.83462
0.54 0.8502375000000025
0.56 0.8639850000000004
0.58 0.8767000000000017
0.6 0.8877075000000016
0.62 0.8981650000000011
0.64 0.9075475000000016
0.66 0.9161325000000021
0.68 0.9231525000000008
0.7 0.9287175000000004
0.72 0.936470000000001
0.74 0.9424725000000006
0.76 0.9467799999999988
0.78 0.9505199999999995
0.8 0.9552599999999976
0.82 0.9597524999999968
0.84 0.9631949999999964
0.86 0.9666699999999935
0.88 0.9698324999999932
0.9 0.9726424999999921
0.92 0.9743974999999914
0.94 0.9771924999999897
0.96 0.9790049999999878
0.98 0.9809199999999854
1.0 0.982792499999985

graph_alpha=3.0node200.txt
153 68
153 94
153 69
153 92
153 75
153 119
153 93
153 112
153 170
153 171
153 22
68 156
68 95
68 46
68 133
141 22
141 130
141 178
141 62
141 181
141 3
141 68
22 163
22 186
22 180
22 91
22 130
139 168
139 0
139 114
139 89
168 69
168 129
168 33
168 126
42 68
42 30
42 22
42 163
42 103
42 124
42 162
42 165
42 27
30 114
30 43
30 125
30 13
30 74
163 182
163 135
163 131
163 186
163 118
163 34
163 181
163 15
163 195
163 110
163 103
15 92
15 25
15 61
15 110
92 120
92 82
49 109
49 190
49 185
109 151
109 107
109 67
109 20
8 33
8 11
8 148
8 87
33 52
33 127
33 89
94 9
94 136
94 18
94 59
94 188
55 1
55 41
55 104
55 143
55 155
55 86
1 149
1 178
155 113
155 36
155 137
155 190
155 105
155 150
155 165
155 182
155 195
155 157
155 99
155 16
155 98
113 122
113 126
113 38
113 20
113 65
125 184
125 146
125 190
184 151
184 183
186 98
186 82
186 25
186 71
186 158
186 65
186 161
186 52
186 157
186 53
98 29
98 104
98 78
152 191
152 3
152 180
152 73
152 107
152 166
191 131
191 48
191 40
191 147
191 64
191 69
114 176
114 182
114 38
114 178
114 71
114 54
114 136
114 67
114 86
28 26
28 187
28 66
28 198
28 150
28 20
28 9
28 97
26 154
26 71
26 93
64 130
64 0
64 115
130 48
130 69
130 128
130 180
130 13
130 173
130 172
130 20
161 181
161 188
161 144
161 56
181 6
181 53
181 138
181 197
181 59
181 81
181 124
97 36
97 41
97 6
97 173
97 61
97 170
97 105
36 58
36 159
36 189
36 85
36 118
178 37
178 11
178 108
37 54
37 189
80 32
80 93
80 129
32 101
32 135
32 171
166 136
166 21
166 159
166 111
136 69
136 24
136 177
136 65
136 197
136 2
136 147
41 89
140 135
140 20
140 187
135 187
135 18
135 120
135 124
189 143
189 96
189 129
189 179
189 110
189 105
143 100
143 60
143 75
20 93
20 108
20 58
20 174
20 47
20 95
20 29
20 27
20 169
20 126
93 193
93 118
116 81
116 119
116 199
116 27
116 182
81 21
81 57
25 101
25 89
25 31
69 177
69 35
69 172
69 10
6 160
82 39
82 12
82 148
82 14
82 101
134 173
134 177
134 170
134 182
134 2
134 19
134 59
173 195
173 111
196 197
196 72
196 95
196 77
196 174
196 70
197 195
39 56
39 61
39 128
195 177
18 61
18 190
18 121
18 38
61 13
145 85
145 127
145 50
145 158
85 160
85 17
85 111
119 175
119 44
176 46
176 63
167 83
167 185
167 117
83 194
83 107
17 133
17 99
17 103
133 75
133 31
40 43
40 183
40 190
43 4
43 154
149 123
149 177
21 151
21 112
122 100
122 96
122 146
48 138
48 126
48 102
110 162
110 23
110 84
162 73
162 51
162 175
27 57
27 62
27 90
57 13
57 131
188 129
188 108
24 60
24 164
24 185
60 156
198 14
198 59
198 138
198 194
198 95
14 183
14 31
194 169
194 132
120 96
120 95
120 108
151 107
151 118
151 16
151 63
151 31
182 148
182 138
182 53
182 117
182 165
182 157
182 99
182 31
182 71
182 102
190 185
190 19
185 183
67 73
67 199
67 44
67 9
67 187
87 45
87 34
87 52
45 52
45 31
45 127
144 79
144 12
144 52
187 102
187 19
187 138
187 199
187 62
34 101
156 192
177 183
183 0
183 13
9 77
9 100
9 23
9 78
9 76
77 164
58 31
58 51
58 132
131 86
131 29
131 175
105 174
105 158
105 7
105 35
174 72
174 74
96 99
159 193
44 192
192 142
66 95
66 79
84 179
84 150
179 123
31 127
31 5
31 90
31 106
31 137
31 47
127 180
3 74
3 11
3 70
74 170
86 76
86 50
86 4
86 126
23 88
146 164
95 106
95 117
95 88
106 157
106 165
164 104
164 147
164 11
164 63
164 158
164 47
104 7
104 137
104 123
7 89
7 132
89 154
13 160
138 70
193 199
53 150
199 16
126 46
117 35
117 169
137 63
72 62
100 160
100 171
180 170
180 71
12 2
160 121
160 115
118 172
118 90
118 91
76 88
124 169
147 54
147 2
5 10
5 112
0 115
79 121
46 47
16 128
71 10
71 56
169 78
154 51
10 88
91 29
62 142
50 142
50 115
4 2
65 78

img

我的解答思路和尝试过的方法

尝试过把不太懂的语句都注释一下,但仍难以联系起来

我想要达到的结果