鱼C论坛

 找回密码
 立即注册
查看: 2918|回复: 3

[已解决]关于EON和scipy模块的求助

[复制链接]
发表于 2023-3-27 22:26:39 | 显示全部楼层 |阅读模式
40鱼币
求助,按照网站https://epidemicsonnetworks.read ... d.html#installation的代码运行的,但是报错了...想请问下为什么
  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3. import EoN

  4. N=10**5
  5. G=nx.barabasi_albert_graph(N, 5) #create a barabasi-albert graph

  6. tmax = 20
  7. iterations = 5  #run 5 simulations
  8. tau = 0.1           #transmission rate
  9. gamma = 1.0    #recovery rate
  10. rho = 0.005      #random fraction initially infected

  11. for counter in range(iterations): #run simulations
  12.     t, S, I, R = EoN.fast_SIR(G, tau, gamma, rho=rho, tmax = tmax)
  13.     if counter == 0:
  14.         plt.plot(t, I, color = 'k', alpha=0.3, label='Simulation')
  15.     plt.plot(t, I, color = 'k', alpha=0.3)

  16. #Now compare with ODE predictions.  Read in the degree distribution of G
  17. #and use rho to initialize the various model equations.
  18. #There are versions of these functions that allow you to specify the
  19. #initial conditions rather than starting from a graph.

  20. #we expect a homogeneous model to perform poorly because the degree
  21. #distribution is very heterogeneous
  22. t, S, I, R = EoN.SIR_homogeneous_pairwise_from_graph(G, tau, gamma, rho=rho, tmax = tmax)
  23. plt.plot(t, I, '-.', label = 'Homogeneous pairwise', linewidth = 5)

  24. #meanfield models will generally overestimate SIR growth because they
  25. #treat partnerships as constantly changing.
  26. t, S, I, R = EoN.SIR_heterogeneous_meanfield_from_graph(G, tau, gamma, rho=rho, tmax=tmax)
  27. plt.plot(t, I, ':', label = 'Heterogeneous meanfield', linewidth = 5)

  28. #The EBCM model does not account for degree correlations or clustering
  29. t, S, I, R = EoN.EBCM_from_graph(G, tau, gamma, rho=rho, tmax = tmax)
  30. plt.plot(t, I, '--', label = 'EBCM approximation', linewidth = 5)

  31. #the preferential mixing model captures degree correlations.
  32. t, S, I, R = EoN.EBCM_pref_mix_from_graph(G, tau, gamma, rho=rho, tmax=tmax)
  33. plt.plot(t, I, label = 'Pref mix EBCM', linewidth=5, dashes=[4, 2, 1, 2, 1, 2])

  34. plt.xlabel('$t$')
  35. plt.ylabel('Number infected')

  36. plt.legend()
  37. plt.savefig('SIR_BA_model_vs_sim.png')


  38. import networkx as nx
  39. import matplotlib.pyplot as plt
  40. import EoN

  41. plt.clf()

  42. #Now run for SIS.   Simulation is much slower so need smaller network
  43. N=10**4
  44. G=nx.barabasi_albert_graph(N, 5) #create a barabasi-albert graph
  45. for counter in range(iterations):
  46.     t, S, I = EoN.fast_SIS(G, tau, gamma, rho=rho, tmax = tmax)
  47.     if counter == 0:
  48.         plt.plot(t, I, color = 'k', alpha=0.3, label='Simulation')
  49.     plt.plot(t, I, color = 'k', alpha=0.3)

  50. #Now compare with ODE predictions.  Read in the degree distribution of G
  51. #and use rho to initialize the various model equations.
  52. #There are versions of these functions that allow you to specify the
  53. #initial conditions rather than starting from a graph.

  54. #we expect a homogeneous model to perform poorly because the degree
  55. #distribution is very heterogeneous
  56. t, S, I = EoN.SIS_homogeneous_pairwise_from_graph(G, tau, gamma, rho=rho, tmax = tmax)
  57. plt.plot(t, I, '-.', label = 'Homogeneous pairwise', linewidth = 5)

  58. t, S, I = EoN.SIS_heterogeneous_meanfield_from_graph(G, tau, gamma, rho=rho, tmax=tmax)
  59. plt.plot(t, I, ':', label = 'Heterogeneous meanfield', linewidth = 5)

  60. t, S, I = EoN.SIS_compact_pairwise_from_graph(G, tau, gamma, rho=rho, tmax=tmax)
  61. plt.plot(t, I, '--', label = 'Compact pairwise', linewidth = 5)

  62. plt.xlabel('$t$')
  63. plt.ylabel('Number infected')
  64. plt.legend()
  65. plt.savefig('SIS_BA_model_vs_sim.png')
复制代码
  1. import EoN
  2. import networkx as nx
  3. from matplotlib import rc
  4. import matplotlib.pylab as plt


  5. import scipy
  6. import random



  7. colors = ['#5AB3E6','#FF2000','#009A80','#E69A00', '#CD9AB3', '#0073B3',
  8.         '#F0E442']

  9. #commands to make legend be in LaTeX font
  10. #rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
  11. rc('text', usetex=True)



  12. rho = 0.025
  13. target_k = 6
  14. N=10000
  15. tau = 0.5
  16. gamma = 1.
  17. ts = scipy.arange(0,40,0.05)
  18. count = 50 #number of simulations to run for each



  19. def generate_network(Pk, N, ntries = 100):
  20.     r'''Generates an N-node random network whose degree distribution is given by Pk'''
  21.     counter = 0
  22.     while counter< ntries:
  23.         counter += 1
  24.         ks = []
  25.         for ctr in range(N):
  26.             ks.append(Pk())
  27.         if sum(ks)%2 == 0:
  28.             break
  29.     if sum(ks)%2 ==1:
  30.         raise EoN.EoNError("cannot generate even degree sum")
  31.     G = nx.configuration_model(ks)
  32.     return G



  33. #An erdos-renyi network has a Poisson degree distribution.
  34. def PkPoisson():
  35.     return scipy.random.poisson(target_k)
  36. def PsiPoisson(x):
  37.     return scipy.exp(-target_k*(1-x))
  38. def DPsiPoisson(x):
  39.     return target_k*scipy.exp(-target_k*(1-x))



  40. #a regular (homogeneous) network has a simple generating function.

  41. def PkHomogeneous():
  42.     return target_k
  43. def PsiHomogeneous(x):
  44.     return x**target_k
  45. def DPsiHomogeneous(x):
  46.     return target_k*x**(target_k-1)




  47. #The following 30 - 40 lines or so are devoted to defining the degree distribution
  48. #and the generating function of the truncated power law network.

  49. #defining the power law degree distribution here:
  50. assert(target_k==6) #if you've changed target_k, then you'll
  51.                 #want to update the range 1..61 and/or
  52.                 #the exponent 1.5.

  53. PlPk = {}
  54. exponent = 1.5
  55. kave = 0
  56. for k in range(1,61):
  57.     PlPk[k]=k**(-exponent)
  58.     kave += k*PlPk[k]

  59. normfactor= sum(PlPk.values())
  60. for k in PlPk:
  61.     PlPk[k] /= normfactor

  62. def PkPowLaw():
  63.     r = random.random()
  64.     for k in PlPk:
  65.         r -= PlPk[k]
  66.         if r<0:
  67.             return k

  68. def PsiPowLaw(x):
  69.     #print PlPk
  70.     rval = 0
  71.     for k in PlPk:
  72.         rval += PlPk[k]*x**k
  73.     return rval

  74. def DPsiPowLaw(x):
  75.     rval = 0
  76.     for k in PlPk:
  77.         rval += k*PlPk[k]*x**(k-1)
  78.     return rval
  79. #End of power law network properties.





  80. def process_degree_distribution(N, Pk, color, Psi, DPsi, symbol, label, count):
  81.     report_times = scipy.linspace(0,30,3000)
  82.     sums = 0*report_times
  83.     for cnt in range(count):
  84.         G = generate_network(Pk, N)
  85.         t, S, I, R = EoN.fast_SIR(G, tau, gamma, rho=rho)
  86.         plt.plot(t, I*1./N, '-', color = color,
  87.                                 alpha = 0.1, linewidth=1)
  88.         subsampled_I = EoN.subsample(report_times, t, I)
  89.         sums += subsampled_I*1./N
  90.     ave = sums/count
  91.     plt.plot(report_times, ave, color = 'k')

  92.     #Do EBCM
  93.     N= G.order()#N is arbitrary, but included because our implementation of EBCM assumes N is given.
  94.     t, S, I, R = EoN.EBCM_uniform_introduction(N, Psi, DPsi, tau, gamma, rho, tmin=0, tmax=10, tcount = 41)
  95.     plt.plot(t, I/N, symbol, color = color, markeredgecolor='k', label=label)

  96.     for cnt in range(3):  #do 3 highlighted simulations
  97.         G = generate_network(Pk, N)
  98.         t, S, I, R = EoN.fast_SIR(G, tau, gamma, rho=rho)
  99.         plt.plot(t, I*1./N, '-', color = 'k', linewidth=0.1)




  100. plt.figure(figsize=(8,4))



  101. #Powerlaw
  102. process_degree_distribution(N, PkPowLaw, colors[3], PsiPowLaw, DPsiPowLaw, 'd', r'Truncated Power Law', count)

  103. #Poisson
  104. process_degree_distribution(N, PkPoisson, colors[0], PsiPoisson, DPsiPoisson, '^', r'Erd\H{o}s--R\'{e}nyi', count)

  105. #Homogeneous
  106. process_degree_distribution(N, PkHomogeneous, colors[2], PsiHomogeneous, DPsiHomogeneous, 's', r'Homogeneous', count)

  107. plt.xlabel(r'$t$', fontsize=12)
  108. plt.ylabel(r'Proportion infected', fontsize=12)
  109. plt.legend(loc = 'upper right', numpoints = 1)

  110. plt.axis(xmax=10, xmin=0, ymin=0)
  111. plt.savefig('fig1p2.pdf')
复制代码

最佳答案
2023-3-27 22:26:40
根据报错信息,代码中引用了名为EoN的库,但是在本地环境中没有找到这个库。需要先安装该库才能运行代码。

可以使用pip工具安装EoN库,命令如下:


  1. pip install EoN
复制代码

如果当前环境有多个Python版本,需要注意确认pip对应的Python版本与当前使用的版本一致。

最佳答案

查看完整内容

根据报错信息,代码中引用了名为EoN的库,但是在本地环境中没有找到这个库。需要先安装该库才能运行代码。 可以使用pip工具安装EoN库,命令如下: 如果当前环境有多个Python版本,需要注意确认pip对应的Python版本与当前使用的版本一致。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-27 22:26:40 | 显示全部楼层    本楼为最佳答案   
根据报错信息,代码中引用了名为EoN的库,但是在本地环境中没有找到这个库。需要先安装该库才能运行代码。

可以使用pip工具安装EoN库,命令如下:


  1. pip install EoN
复制代码

如果当前环境有多个Python版本,需要注意确认pip对应的Python版本与当前使用的版本一致。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-27 22:27:19 | 显示全部楼层
第一份是EON报错了,第二份是scipy报错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-27 22:28:08 | 显示全部楼层
两张报错图片,球球大佬救救
微信图片_20230327221937.png
微信图片_20230327221859.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-28 20:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表