InstituteforDiseaseModeling / covasim

COVID-19 Agent-based Simulator (Covasim): a model for exploring coronavirus dynamics and interventions
https://covasim.org
MIT License
250 stars 223 forks source link

MultiSim to_excel and to_json raises RunTimeError #315

Closed AndrewC19 closed 3 years ago

AndrewC19 commented 3 years ago

Describe the bug The to_excel and to_json methods for MultiSim are always raising RuntimeError: Please run the sim before exporting the results.

To reproduce

Steps to reproduce the behavior: Modifying t01_full_usage_example.py:

import covasim as cv

# Custom intervention -- see Tutorial 5
def protect_elderly(sim):
    if sim.t == sim.day('2020-04-01'):
        elderly = sim.people.age>70
        sim.people.rel_sus[elderly] = 0.0

pars = dict(
    pop_size = 50e3, # Have 50,000 people total in the population
    pop_infected = 100, # Start with 100 infected people
    n_days = 90, # Run the simulation for 90 days
    verbose = 0, # Do not print any output
)

# Running with multisims -- see Tutorial 3
s1 = cv.Sim(pars, label='Default')
s2 = cv.Sim(pars, interventions=protect_elderly, label='Protect the elderly')
msim = cv.MultiSim([s1, s2])
msim.run()
fig = msim.plot(to_plot=['cum_deaths', 'cum_infections'])
msim.to_excel()

Expected behavior

Should export to xlsx but raises a RunTime error despite having run the sims.

cliffckerr commented 3 years ago

Thanks @AndrewC19 ! Here the issue is not that the multisim hasn't been run, but that the individual sims haven't been merged. If after msim.run() you add one of msim.reduce(), msim.median(), msim.mean(), or msim.combine(), it should work. However, I agree this error message is confusing and should be fixed!

AndrewC19 commented 3 years ago

Thanks for the quick reply @cliffckerr. I'm new to Covasim and didn't realise you had to merge the individual sims - a more informative error message would be helpful though!

cliffckerr commented 3 years ago

Closed by https://github.com/amath-idm/covasim/pull/1102