tapios / risk-networks

Code for risk networks: a blend of compartmental models, graphs, data assimilation and semi-supervised learning
Other
2 stars 2 forks source link

Removes placeholders from kinetic model and speeds up contact simulator #55

Closed glwagner closed 4 years ago

glwagner commented 4 years ago

This PR removes placeholders from KineticModel. This is a first step toward resolving #53. We can now run a simple simulation of an epidemic with no accounting for hospitalization and the corresponding changes to the contact network.

It also uses numba to speed up the Gillespie simulation of the contact durations. By isolating and @jit-ing a few expensive functions, we get 5x speed up or so. There is probably more to be had, but we'll see if its needed.

EDIT: not everyone is able to install numba, so the numba-ized contract simulator is isolated in fast_contact_simulator.py.

It adds an example simulate_simple_epidemic.py that simulates an epidemic. The example produces this plot:

image
lubo93 commented 4 years ago

Very nice. Thanks for adding the plotting function!

lubo93 commented 4 years ago

Ok, the llvmlite and numba causes some trouble on my computer. After installing the most recent llvmlite and numba versions, I get the following error:

image

This seems to be a general problem: https://github.com/numba/numba/issues/2428

Can you provide a version without numba/llvmlite? This would be helpful to run some test simulations.

glwagner commented 4 years ago

damn. I didn't anticipate that.

glwagner commented 4 years ago

Ok, the numba stuff is now isolated in a module called fast_contact_simulator. We may have to refactor / revert a few more changes in the non-numba version to make it more performant (since it added some python functions that are inevitably slow).

On my Mac, the difference is:

Slow

(risknet) $ python3 contact_simulation_example.py 
 Simulated 5000 contacts in 8.810 seconds
Simulated 10000 contacts in 23.712 seconds
Simulated 20000 contacts in 63.544 seconds

I guess the slow version sped up a bit too during this PR...

Fast (numba)

(risknet) $ python3 fast_contact_simulation_example.py 
Simulated 5000 contacts in 3.817 seconds
Simulated 10000 contacts in 7.176 seconds
Simulated 20000 contacts in 26.726 seconds

The output of the above scripts is

image