Hello, I am trying to spread an infection over a random graph where the beta is an edge property. I want to implement something that could simulate the lockdown, this is what I wrote:
import matplotlib.pyplot as plt from graph_tool.all import * import numpy as np import random
# graph parameters N = 6000 # number of vertices max_count = 100 # time_stamp beta = 0.01 time_cut = 10
def deg_sample(k): return np.random.poisson(k)
def evolution(G, beta, counts, perc): eprop = G.new_edge_property("double") eprop.a = beta
state = SIState(G, beta=eprop, constant_beta=False)
infected = [state.get_state().fa.sum()] time = range(counts) for i in time: state.iterate_sync() infected.append(state.get_state().fa.sum()) if (i == time_cut) & (perc != 0): n = np.array(random.sample(range(len(list(G.edges()))), int(G.num_edges() * perc/100))) eprop.a[n] = 0 else: pass
return infected
G = random_graph(N, lambda: deg_sample(5), directed=False) G = extract_largest_component(G) graph_draw(G, output='network_layout.pdf')
x = evolution(G, beta, max_count, 100)
plt.plot(x) plt.xlabel(r"Time") plt.ylabel(r"Infectious nodes") plt.title('infected vs time with all edges cutted at time=%d' % time_cut) plt.tight_layout() plt.show()
In evolution I change the beta of all the edges of the graph at a given timestamp, and I would expect that the infection will stop to spread after I change the edge property map. But it doesn't happen and the infenction continue to spread in the network. I want to understand better what's happening in the SIState fuction. Regards, BH.
-- Sent from: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/
Am 10.05.20 um 21:02 schrieb BleakHeart:
In evolution I change the beta of all the edges of the graph at a given timestamp, and I would expect that the infection will stop to spread after I change the edge property map. But it doesn't happen and the infenction continue to spread in the network.
There was a bug in the implementation of 'constant_beta' that prevents this for working. This has been fixed already in the git version. In the meantime, you will need to re-create the SISState object after you modify the transmission probabilities.
(Note also that your code is terribly inefficient, and you generate a full list of edges at each iteration, only to sample a single one.)
Hi, I solved my issue re-creating a new SIState using s parameter to keep tracking of the previous graph vertices states. This is how I did it:
def evolution(G, beta, count, perc):
eprop = G.new_edge_property("double") vprop = G.new_vertex_property("int") eprop.a = beta
state = SIState(G, beta=eprop, constant_beta=False) infected = [state.get_state().fa.sum()]
for i in range(count): state.iterate_sync() infected.append(state.get_state().fa.sum())
if (i == time_cut) & (perc != 0.): n = np.array(random.sample(range(G.num_edges()), int(G.num_edges() * perc / 100))) eprop.a[n] = 0.
vprop.a = state.get_state().fa
state = SIState(G, beta=eprop, s=vprop, constant_beta=False) else: pass
return infected
n is an array containing only the edges' indexes edges of which I want to modify. And using eprop.a[n], I change the properties of the selected edges. When I reapply the SIState, the s parameter keeps the previous vertices states stored in vprop.
I don't understand where the code is inefficient, could you tell me where I am wrong? Regards, BH
-- Sent from: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/