关于EON和scipy模块的求助
求助,按照网站https://epidemicsonnetworks.readthedocs.io/en/latest/GettingStarted.html#installation的代码运行的,但是报错了{:10_266:}...想请问下为什么import networkx as nximport matplotlib.pyplot as plt
import EoN
N=10**5
G=nx.barabasi_albert_graph(N, 5) #create a barabasi-albert graph
tmax = 20
iterations = 5#run 5 simulations
tau = 0.1 #transmission rate
gamma = 1.0 #recovery rate
rho = 0.005 #random fraction initially infected
for counter in range(iterations): #run simulations
t, S, I, R = EoN.fast_SIR(G, tau, gamma, rho=rho, tmax = tmax)
if counter == 0:
plt.plot(t, I, color = 'k', alpha=0.3, label='Simulation')
plt.plot(t, I, color = 'k', alpha=0.3)
#Now compare with ODE predictions.Read in the degree distribution of G
#and use rho to initialize the various model equations.
#There are versions of these functions that allow you to specify the
#initial conditions rather than starting from a graph.
#we expect a homogeneous model to perform poorly because the degree
#distribution is very heterogeneous
t, S, I, R = EoN.SIR_homogeneous_pairwise_from_graph(G, tau, gamma, rho=rho, tmax = tmax)
plt.plot(t, I, '-.', label = 'Homogeneous pairwise', linewidth = 5)
#meanfield models will generally overestimate SIR growth because they
#treat partnerships as constantly changing.
t, S, I, R = EoN.SIR_heterogeneous_meanfield_from_graph(G, tau, gamma, rho=rho, tmax=tmax)
plt.plot(t, I, ':', label = 'Heterogeneous meanfield', linewidth = 5)
#The EBCM model does not account for degree correlations or clustering
t, S, I, R = EoN.EBCM_from_graph(G, tau, gamma, rho=rho, tmax = tmax)
plt.plot(t, I, '--', label = 'EBCM approximation', linewidth = 5)
#the preferential mixing model captures degree correlations.
t, S, I, R = EoN.EBCM_pref_mix_from_graph(G, tau, gamma, rho=rho, tmax=tmax)
plt.plot(t, I, label = 'Pref mix EBCM', linewidth=5, dashes=)
plt.xlabel('$t$')
plt.ylabel('Number infected')
plt.legend()
plt.savefig('SIR_BA_model_vs_sim.png')
import networkx as nx
import matplotlib.pyplot as plt
import EoN
plt.clf()
#Now run for SIS. Simulation is much slower so need smaller network
N=10**4
G=nx.barabasi_albert_graph(N, 5) #create a barabasi-albert graph
for counter in range(iterations):
t, S, I = EoN.fast_SIS(G, tau, gamma, rho=rho, tmax = tmax)
if counter == 0:
plt.plot(t, I, color = 'k', alpha=0.3, label='Simulation')
plt.plot(t, I, color = 'k', alpha=0.3)
#Now compare with ODE predictions.Read in the degree distribution of G
#and use rho to initialize the various model equations.
#There are versions of these functions that allow you to specify the
#initial conditions rather than starting from a graph.
#we expect a homogeneous model to perform poorly because the degree
#distribution is very heterogeneous
t, S, I = EoN.SIS_homogeneous_pairwise_from_graph(G, tau, gamma, rho=rho, tmax = tmax)
plt.plot(t, I, '-.', label = 'Homogeneous pairwise', linewidth = 5)
t, S, I = EoN.SIS_heterogeneous_meanfield_from_graph(G, tau, gamma, rho=rho, tmax=tmax)
plt.plot(t, I, ':', label = 'Heterogeneous meanfield', linewidth = 5)
t, S, I = EoN.SIS_compact_pairwise_from_graph(G, tau, gamma, rho=rho, tmax=tmax)
plt.plot(t, I, '--', label = 'Compact pairwise', linewidth = 5)
plt.xlabel('$t$')
plt.ylabel('Number infected')
plt.legend()
plt.savefig('SIS_BA_model_vs_sim.png')import EoN
import networkx as nx
from matplotlib import rc
import matplotlib.pylab as plt
import scipy
import random
colors = ['#5AB3E6','#FF2000','#009A80','#E69A00', '#CD9AB3', '#0073B3',
'#F0E442']
#commands to make legend be in LaTeX font
#rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
rc('text', usetex=True)
rho = 0.025
target_k = 6
N=10000
tau = 0.5
gamma = 1.
ts = scipy.arange(0,40,0.05)
count = 50 #number of simulations to run for each
def generate_network(Pk, N, ntries = 100):
r'''Generates an N-node random network whose degree distribution is given by Pk'''
counter = 0
while counter< ntries:
counter += 1
ks = []
for ctr in range(N):
ks.append(Pk())
if sum(ks)%2 == 0:
break
if sum(ks)%2 ==1:
raise EoN.EoNError("cannot generate even degree sum")
G = nx.configuration_model(ks)
return G
#An erdos-renyi network has a Poisson degree distribution.
def PkPoisson():
return scipy.random.poisson(target_k)
def PsiPoisson(x):
return scipy.exp(-target_k*(1-x))
def DPsiPoisson(x):
return target_k*scipy.exp(-target_k*(1-x))
#a regular (homogeneous) network has a simple generating function.
def PkHomogeneous():
return target_k
def PsiHomogeneous(x):
return x**target_k
def DPsiHomogeneous(x):
return target_k*x**(target_k-1)
#The following 30 - 40 lines or so are devoted to defining the degree distribution
#and the generating function of the truncated power law network.
#defining the power law degree distribution here:
assert(target_k==6) #if you've changed target_k, then you'll
#want to update the range 1..61 and/or
#the exponent 1.5.
PlPk = {}
exponent = 1.5
kave = 0
for k in range(1,61):
PlPk=k**(-exponent)
kave += k*PlPk
normfactor= sum(PlPk.values())
for k in PlPk:
PlPk /= normfactor
def PkPowLaw():
r = random.random()
for k in PlPk:
r -= PlPk
if r<0:
return k
def PsiPowLaw(x):
#print PlPk
rval = 0
for k in PlPk:
rval += PlPk*x**k
return rval
def DPsiPowLaw(x):
rval = 0
for k in PlPk:
rval += k*PlPk*x**(k-1)
return rval
#End of power law network properties.
def process_degree_distribution(N, Pk, color, Psi, DPsi, symbol, label, count):
report_times = scipy.linspace(0,30,3000)
sums = 0*report_times
for cnt in range(count):
G = generate_network(Pk, N)
t, S, I, R = EoN.fast_SIR(G, tau, gamma, rho=rho)
plt.plot(t, I*1./N, '-', color = color,
alpha = 0.1, linewidth=1)
subsampled_I = EoN.subsample(report_times, t, I)
sums += subsampled_I*1./N
ave = sums/count
plt.plot(report_times, ave, color = 'k')
#Do EBCM
N= G.order()#N is arbitrary, but included because our implementation of EBCM assumes N is given.
t, S, I, R = EoN.EBCM_uniform_introduction(N, Psi, DPsi, tau, gamma, rho, tmin=0, tmax=10, tcount = 41)
plt.plot(t, I/N, symbol, color = color, markeredgecolor='k', label=label)
for cnt in range(3):#do 3 highlighted simulations
G = generate_network(Pk, N)
t, S, I, R = EoN.fast_SIR(G, tau, gamma, rho=rho)
plt.plot(t, I*1./N, '-', color = 'k', linewidth=0.1)
plt.figure(figsize=(8,4))
#Powerlaw
process_degree_distribution(N, PkPowLaw, colors, PsiPowLaw, DPsiPowLaw, 'd', r'Truncated Power Law', count)
#Poisson
process_degree_distribution(N, PkPoisson, colors, PsiPoisson, DPsiPoisson, '^', r'Erd\H{o}s--R\'{e}nyi', count)
#Homogeneous
process_degree_distribution(N, PkHomogeneous, colors, PsiHomogeneous, DPsiHomogeneous, 's', r'Homogeneous', count)
plt.xlabel(r'$t$', fontsize=12)
plt.ylabel(r'Proportion infected', fontsize=12)
plt.legend(loc = 'upper right', numpoints = 1)
plt.axis(xmax=10, xmin=0, ymin=0)
plt.savefig('fig1p2.pdf')
根据报错信息,代码中引用了名为EoN的库,但是在本地环境中没有找到这个库。需要先安装该库才能运行代码。
可以使用pip工具安装EoN库,命令如下:
pip install EoN
如果当前环境有多个Python版本,需要注意确认pip对应的Python版本与当前使用的版本一致。 第一份是EON报错了,第二份是scipy报错了{:10_266:} 两张报错图片,球球大佬救救
页:
[1]