Some initial code that must be run first.
Shortcut: To run the code in the currently selected cell, press ctrl+enter.
In [1]:
import networkx as nx
import matplotlib.pyplot as plt
import seaborn as sns
from helpers import *
from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets
plt.ion()
%matplotlib inline
global outdata, infectcount, ilist
plt.rcParams['figure.figsize'] = 14, 6
This section of code chooses which graph you would like to work on.
To choose a different graph, uncomment the line the graph is on. (Remove or add #'s)
In [2]:
# Network 1
G = nx.erdos_renyi_graph(20, 0.12, seed=4)
# Network 2
#G = nx.barabasi_albert_graph(20, 2, seed=4)
# Network 3
#G = nx.watts_strogatz_graph(20, 4, 0.09, seed=6)
# Network 4
#G = nx.star_graph(20)
# Network 5
#G=nx.path_graph(20)
pos = nx.fruchterman_reingold_layout(G)
#pos = nx.circular_layout(G)
nodelist=G.nodes()
nx.draw(G, pos=pos, node_color=sns.xkcd_rgb['light blue'], with_labels=True)
Start an infection in a node with probability p, each person is infectious for _infectivity_time_. Plot the average number of infected over time and the probability that each node gets infected. _Average_over_ counts how many times we re-start the random infection from the initial node.
In [3]:
@interact(p=(0.0,0.4,0.01), initial_node=(min(G.nodes_iter()), max(G.nodes_iter())), infectivity_time=(1,10), average_over=['1','5','100'])
def interactive1(p=0.1, initial_node=4, infectivity_time=4, average_over=1):
global outdata, infectcount
outdata, infectcount = single_p_infection_history(G, p, infectivity_time, initial_node, samplesize=1, iterations=int(average_over))
ax = plt.subplot(121)
outdata.plot(ax = ax)
ax = plt.subplot(122)
nx.draw_networkx(G, pos=pos, nodelist=nodelist, node_color=[infectcount[n] for n in nodelist], vmin=0, vmax=1, cmap=sns.light_palette("red", as_cmap=True))
print 'Final tally:'
print 'Healthy: {}'.format(outdata.Healthy.irow(-1))
print 'Were sick: {}'.format(outdata.Immune.irow(-1))
In [4]:
immunize = set([0,19])
@interact(p=(0.0,0.4,0.01), initial_node=(min(G.nodes_iter()), max(G.nodes_iter())), infectivity_time=(1,10), immunize=fixed(immunize), average_over=['1','5','100'])
def interactive2(p=0.1, infectivity_time=4, immunize=immunize, average_over=1):
global outdata, infectcount
outdata, infectcount = single_p_infection_history(G, p, infectivity_time, samplesize=int(average_over), immunize=immunize, iterations=1)
outdata2, infectcount2 = single_p_infection_history(G, p, infectivity_time, samplesize=int(average_over), iterations=1)
ax = plt.subplot(121)
outdata.plot(ax=ax)
outdata2.plot(ls='--', ax=ax)
plt.legend(loc='upper right', ncol=2)
ax = plt.subplot(122)
nx.draw_networkx(G, pos=pos, nodelist=nodelist, node_color=[infectcount[n]-1*(n in immunize) for n in nodelist], vmin=-1, vmax=1, cmap=sns.blend_palette(sns.color_palette('RdBu_r'), as_cmap=True))
print 'Final tally (No immunization = dashed line):'
print 'Healthy: {0} ({1})'.format(outdata.Healthy.irow(-1), outdata2.Healthy.irow(-1))
print 'Were sick: {0} ({1})'.format(outdata.Immune.irow(-1),outdata2.Immune.irow(-1))
.
.
.
.
.
.
.
.
.
.
.
.
.
.
In [4]:
In [4]: