springer-math / Mathematics-of-Epidemics-on-Networks

Source code accompanying 'Mathematics of Epidemics on Networks' by Kiss, Miller, and Simon http://www.springer.com/us/book/9783319508047 . Documentation for the software package is at https://epidemicsonnetworks.readthedocs.io/en/latest/
MIT License
151 stars 61 forks source link

Visualizing SIRV model #38

Closed debmalya191 closed 5 years ago

debmalya191 commented 5 years ago

Hi @joelmiller I have created a modified version of your SIR model and I wanted to do visualization on it.

My code-

def test_transmission(u, v, p):

    return random.random()<p

def discrete_SIR(G,
                initial_infecteds,beta,
                w,Vl):

    if G.has_node(initial_infecteds):
        initial_infecteds=[initial_infecteds]           

    node_history = defaultdict(lambda : ([tmin], ['S']))
    #    transmissions = []
    for node in initial_infecteds:
        node_history[node] = ([tmin], ['I'])
        #transmissions.append((tmin-1, None, node))

    N=G.order()
    t = [tmin]
    S = [N-len(initial_infecteds)]
    I = [len(initial_infecteds)]
    R = [0]
    V = [0]

    susceptible = defaultdict(lambda: True)  
    #above line is equivalent to u.susceptible=True for all nodes.

    for u in initial_infecteds:
        susceptible[u] = False

    infecteds = set(initial_infecteds)

    while infecteds and t[-1]<tmax :
        new_infecteds = set()
        vaccinated= set()
        #infector = {}  #used for returning full data.  a waste of time otherwise
        for u in infecteds:
         #   print('u-->' +str(u))
            for v in G.neighbors(u):
         #       print('v --> '+ str(v))
            ##vaccination
                if len(vaccinated)+V[-1]< (Vl*N)  : #check if vaccination over or not
                    #print(len(vaccinated),Vl*N)
                    #print("HI")

                    if susceptible[v] and test_transmission(u, v, w): 
                        vaccinated.add(v)
                        susceptible[v] = False
         #               print('transmitting vaccination')

                    elif susceptible[v] and test_transmission(u,v,beta):
                        new_infecteds.add(v)
                        susceptible[v]=False
         #               print('transmitting infection')
                else:

        #            print("BYE")
                    if susceptible[v] and test_transmission(u, v,beta): 
                        new_infecteds.add(v)
                        susceptible[v] = False

               #infector[v] = [u]

        next_time = t[-1]+1
        if next_time <= tmax:
            for u in infecteds:
                node_history[u][0].append(next_time)
                node_history[u][1].append('R')
            for v in new_infecteds:
                node_history[v][0].append(next_time)
                node_history[v][1].append('I')     

        infecteds = new_infecteds

        R.append(R[-1]+I[-1])
        V.append(len(vaccinated)+V[-1])
        I.append(len(infecteds))
        S.append(N-V[-1]-I[-1]-R[-1])
        t.append(t[-1]+1)

    return t[-1], scipy.array(S),scipy.array(V), scipy.array(I),scipy.array(R),node_history

i want to run this code from EON -

m=5

G=nx.grid_2d_graph(m,m,periodic=True)
initial_infections = [(u,v) for (u,v) in G if u==int(m/2) and v==int(m/2)]
sim = EoN.basic_discrete_SIR(G,0.5,initial_infecteds = initial_infections,
               return_full_data=True, tmax = 25)

pos = {node:node for node in G}
sim.set_pos(pos)
sim.display(0, node_size = 40) #display time 6
plt.show()
plt.savefig('SIR_2dgrid.png')

Thanks in advance!

joelmiller commented 5 years ago

I have spent the last day focusing mostly on this problem. I'm very close to getting it to all work. I should have something tomorrow, possibly tonight after kids go to bed [I'm in Melbourne, 4pm here].

joelmiller commented 5 years ago

I believe this is now addressed.

See https://stackoverflow.com/questions/57010037/visualizing-modified-sir-model/57212880#57212880 for more detailed code implementing this in a discrete time SIRV model.

I've also introduced an example in the documentation:

https://epidemicsonnetworks.readthedocs.io/en/latest/examples/Simulation_Investigation.html

which does a continuous-time version SIRV model, with an additional difference being that the vaccine is randomly distributed, rather than just going to partners of infected individuals (as happens in the model you have).