-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain5.py
More file actions
148 lines (110 loc) · 3.77 KB
/
Copy pathmain5.py
File metadata and controls
148 lines (110 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
"""
Created on Sat May 27 12:59:01 2023
@author: 23783
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import statistics
import random
from random_function import random_mi
from parameter_calculate import Pre_calculation,in_time_calculation1,in_time_calculation2
from Pulse_function import total_pulse
from spread import spread
##################################################################################################
#read in the data
node_data = pd.read_csv('nodes.csv')
edge_data = pd.read_csv('edges.csv')
G = nx.DiGraph()
for i in range(len(node_data)):
G.add_node(node_data.iloc[i,0], label=node_data.iloc[i,1], types = 'S')
n = len(edge_data)
weights = random_mi(2.0,n)
for i in range(n):
G.add_edge(edge_data.iloc[i,0], edge_data.iloc[i,1], weight = weights[i])
###############################################################################################
#信息传播过程计算
starting_point = 26
time_limit=30
prob=0.37
starting = -1
times = np.linspace(0, time_limit,time_limit+1)
time_line = total_pulse(times,starting)
S = list(G.nodes)
S.remove(starting_point)
I = [starting_point]
E = []
R = []
G.add_node(starting_point, types = 'I')
#######################################
total =float(len(G.nodes))
S_Ratio = [1]
E_Ratio = [0]
I_Ratio = [0]
R_Ratio = [0]
Pre_calculation(G)
in_time_calculation1(G,0,time_line,1.5)
S,E,I,R = spread(G, 0, S, E, I, R, prob)
S_Ratio.append(len(S)/total)
E_Ratio.append(len(E)/total)
I_Ratio.append(len(I)/total)
R_Ratio.append(len(R)/total)
for node in list(G.nodes):
G.add_node(node, sit0 = G.nodes[node]['types'])
for t in range(1,time_limit):
in_time_calculation2(G,t,time_line)
S,E,I,R = spread(G, t, S, E, I, R, prob)
for node in list(G.nodes):
name = 'sit'+str(t)
G.add_node(node, **{name: G.nodes[node]['types']})
S_Ratio.append(len(S)/total)
E_Ratio.append(len(E)/total)
I_Ratio.append(len(I)/total)
R_Ratio.append(len(R)/total)
# #########################################################绘图
# # 设置ggplot风格
# plt.style.use('seaborn-whitegrid')
# plt.rcParams['font.family'] = ['sans-serif']
# plt.rcParams['font.sans-serif'] = ['SimHei']
# # 绘制图像
# # plt.plot(times, S_Ratio, linestyle='-', label='S')
# # plt.plot(times, E_Ratio, linestyle=':', label='E')
# # plt.plot(times, I_Ratio, linestyle='-.',label='I')
# # plt.plot(times, R_Ratio, linestyle='–',label='R',color='gold')
# plt.plot(times, S_Ratio, marker='s',markersize=6, label='S')
# plt.plot(times, E_Ratio, marker='o',markersize=6, label='E')
# plt.plot(times, I_Ratio, marker='^',markersize=6, label='I')
# plt.plot(times, R_Ratio, marker='D',markersize=6, label='R',color='gold')
# # 添加图例
# plt.legend()
# # 添加标签和标题
# plt.xlabel('时间')
# plt.ylabel('比例')
# plt.title('各类人群占比随时间变化图')
# # 显示图像
# plt.show()
# ################################################差分图
# # 设置ggplot风格
# plt.style.use('seaborn-whitegrid')
# plt.rcParams['font.family'] = ['sans-serif']
# plt.rcParams['font.sans-serif'] = ['SimHei']
# # 绘制图像
# S_diff= np.abs(np.diff(S_Ratio))
# E_diff= np.diff(E_Ratio)
# I_diff= np.diff(I_Ratio)
# R_diff= np.diff(R_Ratio)
# totoal_diff = E_diff+I_diff+R_diff
# # a = S_diff[0]
# # S_diff[0] = S_diff[1]
# # S_diff[1] = a
# plt.plot(times[1:], S_diff,color='gold')
# # 添加图例
# plt.legend()
# # 添加标签和标题
# plt.xlabel('时间')
# plt.ylabel('比例')
# plt.title('各时间消息传播人群占总人数比例')
# # 显示图像
# plt.show()