在运行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()