covasim报错:BrokenProcessPool解决方法

文章描述了一段在运行Covasim疫苗模拟代码时遇到的BrokenProcessPool错误。问题出在未向定义的函数`vaccinate_by_age`传入正确参数。解决方案是将`subtarget`参数更改为`vaccinate_by_age(sim)`,确保函数在调用时接收到`Sim`对象。修复后的代码能够成功运行并进行疫苗策略的模拟比较。
摘要由CSDN通过智能技术生成

        在运行covasim-main\docs\tutorials\tut_interventions.ipynb下的“Age targeting and other subtargeting”的示例代码:

import covasim as cv
import numpy as np

# Define the vaccine subtargeting
def vaccinate_by_age(sim):
    young  = cv.true(sim.people.age < 50) # cv.true() returns indices of people matching this condition, i.e. people under 50
    middle = cv.true((sim.people.age >= 50) * (sim.people.age < 75)) # Multiplication means "and" here
    old    = cv.true(sim.people.age >= 75)
    inds = sim.people.uid # Everyone in the population -- equivalent to np.arange(len(sim.people))
    vals = np.ones(len(sim.people)) # Create the array
    vals[young] = 0.1 # 10% probability for people <50
    vals[middle] = 0.5 # 50% probability for people 50-75
    vals[old] = 0.9 # 90% probability for people >75
    output = dict(inds=inds, vals=vals)
    return output


# Define the vaccine
vaccine = cv.simple_vaccine(days=20, rel_sus=0.0, rel_symp=0.06, subtarget=vaccinate_by_age)

# Create, run, and plot the simulations
sim1 = cv.Sim(label='Baseline')
sim2 = cv.Sim(interventions=vaccine, label='With age-targeted vaccine')
msim = cv.parallel(sim1, sim2)
msim.plot()

报错:

BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

除了这部分代码,其他tutorials中使用到代码:

msim = cv.parallel(sim1, sim2)

也报了相同的错误代码。

        去GitHub上找了下,没有相关issue,搜索也搜索不到,自己看了下,发现主函数在使用定义的函数,即:

vaccine = cv.simple_vaccine(days=20, rel_sus=0.0, rel_symp=0.06, subtarget=vaccinate_by_age)

时,没有给vaccinate_by_age函数传递变量,修改为以下即可正常运行:

import covasim as cv
import numpy as np

# Define the vaccine subtargeting
def vaccinate_by_age(sim):
    young  = cv.true(sim.people.age < 50) # cv.true() returns indices of people matching this condition, i.e. people under 50
    middle = cv.true((sim.people.age >= 50) * (sim.people.age < 75)) # Multiplication means "and" here
    old    = cv.true(sim.people.age >= 75)
    inds = sim.people.uid # Everyone in the population -- equivalent to np.arange(len(sim.people))
    vals = np.ones(len(sim.people)) # Create the array
    vals[young] = 0.1 # 10% probability for people <50
    vals[middle] = 0.5 # 50% probability for people 50-75
    vals[old] = 0.9 # 90% probability for people >75
    output = dict(inds=inds, vals=vals)
    return output

sim = cv.Sim(verbose = 0).run()

# Define the vaccine
vaccine = cv.simple_vaccine(days=20, rel_sus=0.0, rel_symp=0.06, subtarget=vaccinate_by_age(sim))

# Create, run, and plot the simulations
sim1 = cv.Sim(label='Baseline')
sim2 = cv.Sim(interventions=vaccine, label='With age-targeted vaccine')
msim = cv.parallel(sim1, sim2)
msim.plot()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值