Infection count edge weighting
This commit is contained in:
parent
622daed52b
commit
9c2f92b91c
BIN
Analysis/SEIRDS.pdf
Normal file
BIN
Analysis/SEIRDS.pdf
Normal file
Binary file not shown.
Binary file not shown.
@ -1,6 +1,12 @@
|
|||||||
## Set Working Directory to file directory - RStudio approach
|
## Set Working Directory to file directory - RStudio approach
|
||||||
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
|
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
|
||||||
|
|
||||||
|
weighted <- 'False'
|
||||||
|
#conda_install(envname = "r-reticulate", packages="networkx")
|
||||||
|
#conda_install(envname = "r-reticulate", packages="matplotlib")
|
||||||
|
#conda_install(envname = "r-reticulate", packages="pydot")
|
||||||
|
#conda_install(envname = "r-reticulate", packages="pygraphviz")
|
||||||
|
|
||||||
seirds.f <- function(t, y, k) {
|
seirds.f <- function(t, y, k) {
|
||||||
S <- y[1]
|
S <- y[1]
|
||||||
E <- y[2]
|
E <- y[2]
|
||||||
@ -18,7 +24,7 @@ seirds.f <- function(t, y, k) {
|
|||||||
|
|
||||||
# Saying infec rate of S in contact with E same as contact with I
|
# Saying infec rate of S in contact with E same as contact with I
|
||||||
dS <- epsilon - (beta*E + beta*I)*S + waning*R - gamma_d*S
|
dS <- epsilon - (beta*E + beta*I)*S + waning*R - gamma_d*S
|
||||||
dE <- beta*S*E + beta*S*I - (delta+gamma_d)*E
|
dE <- (beta*E + beta*I)*S - (delta+gamma_d)*E
|
||||||
dI <- delta*E - (1+gamma_d)*I
|
dI <- delta*E - (1+gamma_d)*I
|
||||||
dR <- (1-mu)*I - (waning+gamma_d)*R
|
dR <- (1-mu)*I - (waning+gamma_d)*R
|
||||||
dD <- mu*I
|
dD <- mu*I
|
||||||
@ -27,11 +33,22 @@ seirds.f <- function(t, y, k) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
library(reticulate)
|
library(reticulate)
|
||||||
#conda_install(envname = "r-reticulate", packages="networkx")
|
|
||||||
#conda_install(envname = "r-reticulate", packages="matplotlib")
|
|
||||||
#conda_install(envname = "r-reticulate", packages="pydot")
|
|
||||||
#conda_install(envname = "r-reticulate", packages="pygraphviz")
|
|
||||||
source_python('prep_model.py')
|
source_python('prep_model.py')
|
||||||
|
model_data <- prep_seirds(weighted)
|
||||||
|
|
||||||
|
S <- unlist(model_data)[1]
|
||||||
|
E <- unlist(model_data)[2]
|
||||||
|
I <- unlist(model_data)[3]
|
||||||
|
R <- unlist(model_data)[4]
|
||||||
|
D <- unlist(model_data)[5]
|
||||||
|
|
||||||
|
beta <- unlist(model_data)[6]
|
||||||
|
delta <- unlist(model_data)[7]
|
||||||
|
gamma_r <- unlist(model_data)[8]
|
||||||
|
gamma_d <- unlist(model_data)[9]
|
||||||
|
mu <- unlist(model_data)[10]
|
||||||
|
epsilon <- unlist(model_data)[11]
|
||||||
|
omega <- unlist(model_data)[12]
|
||||||
|
|
||||||
# Obtained from prep_model.py
|
# Obtained from prep_model.py
|
||||||
seirds.params <- c(beta, # beta
|
seirds.params <- c(beta, # beta
|
||||||
@ -79,6 +96,6 @@ plot.seirds <- function(sol, method){
|
|||||||
}
|
}
|
||||||
|
|
||||||
plot.seirds(seirds.ode.sol, "ODE45")
|
plot.seirds(seirds.ode.sol, "ODE45")
|
||||||
ggsave("SERIDS.pdf")
|
ggsave("SEIRDS.pdf")
|
||||||
# Sanity check: Make sure sums to ~1.0
|
# Sanity check: Make sure sums to ~1.0
|
||||||
sum(tail(seirds.ode.sol$y,1))
|
sum(tail(seirds.ode.sol$y,1))
|
||||||
@ -11,104 +11,136 @@ print(os.getcwd())
|
|||||||
#os.chdir(os.path.dirname(sys.argv[0]))
|
#os.chdir(os.path.dirname(sys.argv[0]))
|
||||||
#print(os.getcwd())
|
#print(os.getcwd())
|
||||||
|
|
||||||
# AGraph preserves attributes, networkx Graph does not.
|
def prep_seirds(weighted):
|
||||||
# Many of the desired functions are in networkx.
|
print("Prepping the SEIRDS model, using trivial weighting=", weighted)
|
||||||
# So import AGraph to keep attributes, then convert to Networkx.
|
# AGraph preserves attributes, networkx Graph does not.
|
||||||
A = nx.drawing.nx_agraph.to_agraph(nx.drawing.nx_pydot.read_dot("./1_mo_color_DOTFILE.dot"))
|
# Many of the desired functions are in networkx.
|
||||||
A.layout('dot')
|
# So import AGraph to keep attributes, then convert to Networkx.
|
||||||
#A.draw('tree.png')
|
A = nx.drawing.nx_agraph.to_agraph(nx.drawing.nx_pydot.read_dot("./1_mo_color_DOTFILE.dot"))
|
||||||
A.remove_node('\\n') # Remove "newline" node from newline end of dot file
|
A.layout('dot')
|
||||||
G=nx.DiGraph(A)
|
#A.draw('tree.png')
|
||||||
|
A.remove_node('\\n') # Remove "newline" node from newline end of dot file
|
||||||
|
G=nx.DiGraph(A)
|
||||||
|
|
||||||
color_map = []
|
color_map = []
|
||||||
color_d = {}
|
color_d = {}
|
||||||
node_pos = {} # used for drawing/graphing
|
node_pos = {} # used for drawing/graphing
|
||||||
|
|
||||||
# Compartments
|
# Compartments
|
||||||
S = 0
|
S = 0
|
||||||
I_R = 0
|
I_R = 0
|
||||||
I_D = 0
|
I_D = 0
|
||||||
E = 0
|
E = 0
|
||||||
R = 0
|
R = 0
|
||||||
D = 0
|
D = 0
|
||||||
|
|
||||||
ep_tmp = 0 # counter for epsilon
|
ep_tmp = 0 # counter for epsilon
|
||||||
|
inf_ct = 0 # infection rate counter
|
||||||
|
|
||||||
for node in A:
|
for node in A:
|
||||||
color = A.get_node(node).attr.to_dict()['fillcolor']
|
color = A.get_node(node).attr.to_dict()['fillcolor']
|
||||||
str_pos = A.get_node(node).attr.to_dict()['pos']
|
str_pos = A.get_node(node).attr.to_dict()['pos']
|
||||||
coords = str_pos.split(',')
|
coords = str_pos.split(',')
|
||||||
x = coords[0] # layout for draw function
|
x = coords[0] # layout for draw function
|
||||||
y = coords[1]
|
y = coords[1]
|
||||||
node_pos[node] = float(x), float(y)
|
node_pos[node] = float(x), float(y)
|
||||||
if color is None or color == '':
|
if color is None or color == '':
|
||||||
color_map.append("white")
|
color_map.append("white")
|
||||||
color_d[node] = color
|
color_d[node] = color
|
||||||
in_edges = list(G.in_edges(node))
|
in_edges = list(G.in_edges(node))
|
||||||
tmp_S = 1
|
out_edges = list(G.out_edges(node))
|
||||||
for source in in_edges:
|
|
||||||
tmp_S = 1
|
|
||||||
# If previous node was infected, then we are recovered
|
tmp_S = 1
|
||||||
|
tmp_inf = 0
|
||||||
|
for source in in_edges:
|
||||||
|
tmp_S = 1
|
||||||
|
# If previous node was infected, then we are recovered
|
||||||
|
if (color_d[source[0]] == 'red'):
|
||||||
|
R = R + 1
|
||||||
|
tmp_S = 0
|
||||||
|
break # No need to check the other nodes
|
||||||
|
for source in out_edges:
|
||||||
if (color_d[source[0]] == 'red'):
|
if (color_d[source[0]] == 'red'):
|
||||||
R = R + 1
|
if(weighted == 'False' or not out_edges):
|
||||||
tmp_S = 0
|
tmp_inf = 1
|
||||||
break # No need to check the other nodes
|
else:
|
||||||
S = S + tmp_S
|
inf_ct = inf_ct + 1/len(out_edges) # trivial weighting
|
||||||
#G[source[0]][node]['weight'] = 3
|
inf_ct = inf_ct + tmp_inf
|
||||||
elif color == 'yellow':
|
S = S + tmp_S
|
||||||
color_map.append(color)
|
#G[source[0]][node]['weight'] = 3
|
||||||
color_d[node] = color
|
elif color == 'yellow':
|
||||||
in_edges = list(G.in_edges(node))
|
color_map.append(color)
|
||||||
tmp_E = 1
|
color_d[node] = color
|
||||||
for source in in_edges:
|
in_edges = list(G.in_edges(node))
|
||||||
tmp_E = 1
|
tmp_E = 1
|
||||||
# If previous node was infected, then we are recovered
|
tmp_R = 0
|
||||||
if (color_d[source[0]] == 'red'):
|
tmp_inf = 0
|
||||||
R = R + 1
|
for source in in_edges:
|
||||||
tmp_E = 0
|
# If previous node was infected, then we are recovered
|
||||||
break # No need to check the other nodes
|
if (color_d[source[0]] == 'red'):
|
||||||
E = E + tmp_E
|
tmp_R = 1
|
||||||
else:
|
tmp_E = 0
|
||||||
color_map.append(color)
|
if (color_d[source[0]] == '' or color_d[source[0]] == 'white'):
|
||||||
color_d[node] = color
|
if(weighted == 'False' or not in_edges):
|
||||||
# Check if node dies
|
tmp_inf = 1 # add 1 for the inf counter
|
||||||
out_edges = list(G.out_edges(node))
|
else:
|
||||||
if not out_edges:
|
inf_ct = inf_ct + 1/len(in_edges) # trivial weighting
|
||||||
D = D + 1
|
E = E + tmp_E
|
||||||
I_D = I_D + 1
|
R = R + tmp_R
|
||||||
else:
|
inf_ct = inf_ct + tmp_inf
|
||||||
I_R = I_R + 1
|
else:
|
||||||
# Check if imported
|
color_map.append(color)
|
||||||
in_edges = list(G.in_edges(node))
|
color_d[node] = color
|
||||||
if not in_edges:
|
# Check if node dies
|
||||||
ep_tmp = ep_tmp + 1
|
out_edges = list(G.out_edges(node))
|
||||||
|
if not out_edges:
|
||||||
|
D = D + 1
|
||||||
|
I_D = I_D + 1
|
||||||
|
else:
|
||||||
|
I_R = I_R + 1
|
||||||
|
# Check if imported
|
||||||
|
in_edges = list(G.in_edges(node))
|
||||||
|
if not in_edges:
|
||||||
|
ep_tmp = ep_tmp + 1
|
||||||
|
else:
|
||||||
|
tmp_inf = 0
|
||||||
|
for source in in_edges:
|
||||||
|
if (color_d[source[0]] == '' or color_d[source[0]] == 'white'):
|
||||||
|
if(weighted == 'False' or not in_edges):
|
||||||
|
tmp_inf = 1
|
||||||
|
else:
|
||||||
|
inf_ct = inf_ct + 1/len(in_edges) # trivial weightin
|
||||||
|
inf_ct = inf_ct + tmp_inf
|
||||||
|
|
||||||
# Params
|
# Params
|
||||||
beta = (I_R+I_D)/len(A) # rate of infec (I/total?)
|
beta = (inf_ct)/len(A) # rate of infec
|
||||||
#delta = E/len(A) # symptom appearance rate (E/total?)
|
delta = 1 # incubation period
|
||||||
delta = 1 # incubation period
|
gamma_r = R/len(A) # recov rate
|
||||||
gamma_r = R/len(A) # recov rate (R/total?)
|
gamma_d = D/len(A) # death rate
|
||||||
gamma_d = D/len(A) # death rate (D/total?)
|
mu = D/(I_R+I_D) # fatality ratio
|
||||||
mu = D/(I_R+I_D) # fatality ratio (D/I)
|
epsilon = ep_tmp/len(A) # infected import rate
|
||||||
epsilon = ep_tmp/len(A) # infected import rate
|
omega = 1 # waning immunity rate
|
||||||
omega = 1 # waning immunity rate
|
|
||||||
|
|
||||||
|
|
||||||
print("Model Compartments:")
|
print("Model Compartments:")
|
||||||
print("S:", str(S))
|
print("S:", str(S))
|
||||||
print("E:", str(E))
|
print("E:", str(E))
|
||||||
print("I_R:", str(I_R))
|
print("I_R:", str(I_R))
|
||||||
print("I_D:", str(I_D))
|
print("I_D:", str(I_D))
|
||||||
print("R:", str(R))
|
print("R:", str(R))
|
||||||
print("D:", str(D))
|
print("D:", str(D))
|
||||||
print("\n")
|
print("\n")
|
||||||
|
|
||||||
print("Model Parameters:")
|
print("Model Parameters:")
|
||||||
print("beta:", str(beta))
|
print("infect counter:", str(inf_ct))
|
||||||
print("delta:", str(delta))
|
print("beta:", str(beta))
|
||||||
print("gamma_r:", str(gamma_r))
|
print("delta:", str(delta))
|
||||||
print("gamma_d:", str(gamma_d))
|
print("gamma_r:", str(gamma_r))
|
||||||
print("mu:", str(mu))
|
print("gamma_d:", str(gamma_d))
|
||||||
print("epsilon:", str(epsilon))
|
print("mu:", str(mu))
|
||||||
print("omega:", str(omega))
|
print("epsilon:", str(epsilon))
|
||||||
|
print("omega:", str(omega))
|
||||||
|
|
||||||
|
return (S, E, I_R+I_D, R, D, beta, delta, gamma_r, gamma_d, mu, epsilon, omega)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user