def test_edge_num_attribute(self): g = nx.karate_club_graph() attr = {(u, v): {"even": int((u+v) % 10)} for (u, v) in g.edges()} nx.set_edge_attributes(g, attr) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c = cpm.EdgeNumericalAttribute("even", value=0, op="==", probability=1) model.add_rule("Susceptible", "Infected", c) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c = cpm.EdgeNumericalAttribute("even", value=[3, 10], op="IN", probability=1) model.add_rule("Susceptible", "Infected", c) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10)
def test_edge_stochastic(self): # Fixed Threshold g = nx.erdos_renyi_graph(1000, 0.1) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c1 = cpm.EdgeStochastic(0.1, triggering_status="Infected") model.add_rule("Susceptible", "Infected", c1) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10) # Ad-hoc Threshold model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c1 = cpm.EdgeStochastic(triggering_status="Infected") model.add_rule("Susceptible", "Infected", c1) config = mc.Configuration() for e in g.edges(): config.add_edge_configuration("threshold", e, np.random.random_sample()) config.add_model_parameter('percentage_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10) # Predefined threshold 1/N model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c1 = cpm.EdgeStochastic(triggering_status="Infected") model.add_rule("Susceptible", "Infected", c1) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10)
def test_compartment_add_node_pa(self): g = nx.karate_club_graph() attr = {n: {"even": int(n % 2)} for n in g.nodes()} nx.set_node_attributes(g, attr) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") a1 = act.AddNode(probability=1, initial_status="Susceptible", copy_attributes=True, number_of_edges=4, model='PA') c1 = cmp.NodeStochastic(1) model.add_rule("Susceptible", "Susceptible", c1) model.add_action(a1) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0) model.set_initial_status(config) iterations = model.iteration_bunch(6) nodes = [sum(n['node_count'].values()) for n in iterations] self.assertEqual(nodes, [35, 36, 37, 38, 39, 40])
def test_compartment_add_node(self): g = nx.karate_club_graph() attr = {n: {"even": int(n % 2)} for n in g.nodes()} nx.set_node_attributes(g, attr) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c2 = act.AddNode(probability=1, initial_status="Susceptible", copy_attributes=True) c1 = cpm.NodeStochastic(1, composed=c2) model.add_rule("Susceptible", "Susceptible", c1) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0) model.set_initial_status(config) iterations = model.iteration_bunch(6) nodes = [sum(n['node_count'].values()) for n in iterations] self.assertEqual(nodes, [34, 67, 133, 265, 529, 1057])
def test_compartment_swap_edge(self): g = nx.karate_club_graph() attr = {(u, v): {"even": int((u + v) % 10)} for (u, v) in g.edges()} nx.set_edge_attributes(g, attr) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") a1 = act.SwapEdges(probability=1, number_of_swaps=1, copy_attributes=True, initial_status="Susceptible") c1 = cmp.NodeStochastic(0.5) model.add_rule("Susceptible", "Susceptible", c1) model.add_action(a1) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0) model.set_initial_status(config) iterations = model.iteration_bunch(6) self.assertEqual(len(iterations), 6)
def full_simulate_net_diffusion(frac_infected=0.01, threshold=0.038, profile=0.0000105, p_removal=0.22, num_exec=20, num_iter=32, nproc=8): # Network generation g = nx.erdos_renyi_graph(1000, 0.1) # Composite Model instantiation sir_th_model = CompMod.CompositeModel(g) # Model statuses sir_th_model.add_status("Susceptible") sir_th_model.add_status("Infected") sir_th_model.add_status("Removed") # Compartment definition c1 = NodeThreshold(threshold=None, triggering_status="Infected") c2 = NodeStochastic(p_removal) # Rule definition sir_th_model.add_rule("Susceptible", "Infected", c1) sir_th_model.add_rule("Infected", "Removed", c2) # Model initial status configuration, assume 1% of population is infected config = MoCon.Configuration() config.add_model_parameter('fraction_infected', frac_infected) # Setting nodes parameters for i in g.nodes(): config.add_node_configuration("threshold", i, threshold) config.add_node_configuration("profile", i, profile) # Simulation execution sir_th_model.set_initial_status(config) trends = multi_runs(sir_th_model, execution_number=num_exec, iteration_number=num_iter, nprocesses=nproc) # Convert into a dataframe that lists each number of infected nodes by iteration number (to make average # calculation) df_infected = pd.DataFrame( [execution['trends']['node_count'][1] for execution in trends]) # Normalize each run, so that they are consistent with Google Trends Data for comparison: df_infected = df_infected.apply(lambda x: x / x.max(), axis=1) df_infected = pd.melt(df_infected, var_name='Execution', value_name='Infected') df_infected['Infected'] *= 100 return df_infected
def simulate_net_diffusion(frac_infected=0.01, threshold=0.18, profile=0.00001, p_removal=0.017, num_exec=10, num_iter=100, nproc=8): g = nx.erdos_renyi_graph(1000, 0.1) # Composite Model instantiation sir_th_model = CompMod.CompositeModel(g) # Model statuses sir_th_model.add_status("Susceptible") sir_th_model.add_status("Infected") sir_th_model.add_status("Removed") # Compartment definition c1 = NodeThreshold(threshold=None, triggering_status="Infected") c2 = NodeStochastic(p_removal) # Rule definition sir_th_model.add_rule("Susceptible", "Infected", c1) sir_th_model.add_rule("Infected", "Removed", c2) # Model initial status configuration config = MoCon.Configuration() config.add_model_parameter('fraction_infected', frac_infected) # Setting nodes parameters for i in g.nodes(): config.add_node_configuration("threshold", i, threshold) config.add_node_configuration("profile", i, profile) # Simulation execution sir_th_model.set_initial_status(config) trends = multi_runs(sir_th_model, execution_number=num_exec, iteration_number=num_iter, nprocesses=nproc) df_infected = pd.DataFrame( [execution['trends']['node_count'][1] for execution in trends]) # Normalize each run: df_infected = df_infected.apply(lambda x: x / x.max(), axis=1) df_infected = pd.melt(df_infected, var_name='Execution', value_name='Infected') # Normalize (mean) values so that they are consistent with Google Trends Data for comparison: y_forecasted = df_infected.groupby('Execution').mean() * 100 return y_forecasted
def test_node_threshold(self): # Fixed Threshold g = nx.erdos_renyi_graph(1000, 0.1) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c1 = cpm.NodeThreshold(0.1, triggering_status="Infected") model.add_rule("Susceptible", "Infected", c1) config = mc.Configuration() config.add_model_parameter('fraction_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10) # Ad-hoc Threshold model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c1 = cpm.NodeThreshold(triggering_status="Infected") model.add_rule("Susceptible", "Infected", c1) config = mc.Configuration() for i in g.nodes(): config.add_node_configuration("threshold", i, np.random.random_sample()) config.add_model_parameter('fraction_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10)
def generate_model(network): # model parameters infect_probability = 0.025 incubation_time = 14 incubation_probability = 0.5 ill_probability = 0.5 disease_time = 21 recover_probability = 0.66 # Composite Model instantiation model = gc.CompositeModel(network) # Model statuses model.add_status("Susceptible") model.add_status("Infected") model.add_status("Contagious") model.add_status("Ill") #model.add_status("Hospitalized") model.add_status("Dead") model.add_status("Recovered") # if another node is contagious or ill, this node can get infected infect_condition_c = EdgeStochastic(infect_probability, "Contagious") model.add_rule("Susceptible", "Infected", infect_condition_c) infect_condition_i = EdgeStochastic(infect_probability, "Ill") model.add_rule("Susceptible", "Infected", infect_condition_i) # if infected, after incubation time there is a chance of becoming contagious incubation_time_condition = CountDown("incubation", iterations=incubation_time) incubation_probability_condition = NodeStochastic( incubation_probability, composed=incubation_time_condition) model.add_rule("Infected", "Contagious", incubation_probability_condition) # if contagious, there is a chance of becoming ill ill_probability_condition = NodeStochastic(ill_probability, triggering_status="Contagious") model.add_rule("Infected", "Ill", ill_probability_condition) # if ill, after disease time there is a chance of becoming Recovered recover_time_condition = CountDown("incubation", iterations=disease_time) recover_probability_condition = NodeStochastic( recover_probability, composed=recover_time_condition) model.add_rule("Ill", "Recovered", recover_probability_condition) # if ill, after disease time there is also chance of becoming Dead die_probability_condition = NodeStochastic((1 - recover_probability), composed=recover_time_condition) model.add_rule("Ill", "Dead", die_probability_condition) return model
def test_countwodn(self): g = nx.karate_club_graph() model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c = cpm.CountDown(name="time", iterations=4) model.add_rule("Susceptible", "Infected", c) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(100) self.assertEqual(len(iterations), 100)
def test_node_attribute(self): g = nx.karate_club_graph() attr = {n: {"even": int(n % 2)} for n in g.nodes()} nx.set_node_attributes(g, attr) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c = cpm.NodeCategoricalAttribute("even", "0", probability=0.6) model.add_rule("Susceptible", "Infected", c) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10)
def test_compartment_remove_node(self): g = nx.karate_club_graph() model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c2 = act.RemoveNode(probability=1) c1 = cpm.NodeStochastic(1, composed=c2) model.add_rule("Susceptible", "Susceptible", c1) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0) model.set_initial_status(config) iterations = model.iteration_bunch(6) nodes = [sum(n['node_count'].values()) for n in iterations] self.assertEqual(nodes, [34, 1, 1, 1, 1, 1])
def test_node_stochastic_composed(self): g = nx.erdos_renyi_graph(1000, 0.1) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") model.add_status("Removed") # cascading composition c3 = cpm.NodeStochastic(0.2) c2 = cpm.NodeStochastic(0.4, composed=c3) c1 = cpm.NodeStochastic(0.5, "Infected", composed=c2) model.add_rule("Susceptible", "Infected", c1) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(100) self.assertEqual(len(iterations), 100)
def test_compartment_remove_node_bottom(self): g = nx.karate_club_graph() model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") a1 = act.RemoveNode(probability=1, model="bottom") c1 = cmp.NodeStochastic(0.5) model.add_rule("Susceptible", "Susceptible", c1) model.add_action(a1) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0) model.set_initial_status(config) iterations = model.iteration_bunch(6) nodes = [sum(n['node_count'].values()) for n in iterations] self.assertEqual(nodes, [33, 32, 31, 30, 29, 28])
def test_node_stochastic(self): g = nx.erdos_renyi_graph(1000, 0.1) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") model.add_status("Removed") c1 = cpm.NodeStochastic(0.02, "Infected") c2 = cpm.NodeStochastic(0.01) c3 = cpm.NodeStochastic(0.5) model.add_rule("Susceptible", "Infected", c1) model.add_rule("Infected", "Removed", c2) model.add_rule("Infected", "Susceptible", c3) config = mc.Configuration() config.add_model_parameter('fraction_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(100) self.assertEqual(len(iterations), 100)
def test_conditional_composition(self): g = nx.erdos_renyi_graph(1000, 0.1) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") model.add_status("Removed") # conditional composition c1 = cpm.NodeStochastic(0.5) c2 = cpm.NodeStochastic(0.2) c3 = cpm.NodeStochastic(0.1) cc = cpm.ConditionalComposition(c1, c2, c3) model.add_rule("Susceptible", "Infected", cc) config = mc.Configuration() config.add_model_parameter('percentage_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(100) self.assertEqual(len(iterations), 100)
import ndlib.models.CompositeModel as gc import ndlib.models.ModelConfig as mc import networkx as nx from ndlib.models.compartments import NodeStochastic import heuristics import random random.seed(a=27609) G = nx.barabasi_albert_graph(2000, 3, seed=27609) # Composite Model instantiation model = gc.CompositeModel(G) # Model statuses model.add_status("Susceptible") model.add_status("Infected") model.add_status("Infected_2") model.add_status("Infected_Both") model.add_status("Removed") # Compartment definition contagion_1 = NodeStochastic(0.1, triggering_status="Infected") contagion_2 = NodeStochastic(0.1, triggering_status="Infected_2") contagion_both = NodeStochastic(0.1, triggering_status="Infected_Both") contagion_interaction_1 = NodeStochastic(.2, triggering_status="Infected") contagion_interaction_2 = NodeStochastic(1, triggering_status="Infected_2") # Should be adjusted based on rate that KKT assumes in original paper. recovered = NodeStochastic(1) # Rule definition
def test_node_num_variable(self): g = nx.karate_club_graph() attr = {n: {"even": int(n % 10)} for n in g.nodes()} nx.set_node_attributes(g, attr) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c = cpm.NodeNumericalVariable('even', var_type=NumericalType.ATTRIBUTE, value=0, op='==') model.add_rule("Susceptible", "Infected", c) config = mc.Configuration() config.add_model_parameter('fraction_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c = cpm.NodeNumericalVariable('even', var_type=NumericalType.ATTRIBUTE, value=[3, 5], op='IN') model.add_rule("Susceptible", "Infected", c) config = mc.Configuration() config.add_model_parameter('fraction_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10) with self.assertRaises(ValueError): cpm.NodeNumericalVariable(5, var_type=NumericalType.ATTRIBUTE, value=0, op='==') with self.assertRaises(ValueError): cpm.NodeNumericalVariable('even', value=0, op='==') with self.assertRaises(ValueError): cpm.NodeNumericalVariable('even', var_type=3, value=0, value_type=3, op='==') with self.assertRaises(ValueError): cpm.NodeNumericalVariable(None, var_type=NumericalType.ATTRIBUTE, value=0, op='==') with self.assertRaises(ValueError): cpm.NodeNumericalVariable('even', var_type=NumericalType.ATTRIBUTE, value=0) with self.assertRaises(ValueError): cpm.NodeNumericalVariable('even', var_type=NumericalType.ATTRIBUTE, value=0, op='IN') with self.assertRaises(ValueError): cpm.NodeNumericalVariable('even', var_type=NumericalType.ATTRIBUTE, value=['a', 3], op='IN') with self.assertRaises(ValueError): cpm.NodeNumericalVariable('even', var_type=NumericalType.ATTRIBUTE, value=[3, 'a'], op='IN') with self.assertRaises(ValueError): cpm.NodeNumericalVariable('even', var_type=NumericalType.ATTRIBUTE, value=[5, 3], op='IN') with self.assertRaises(ValueError): cpm.NodeNumericalVariable('even', var_type=NumericalType.ATTRIBUTE, value=[5, 3], op='IN')