def test_multiple_si_model(self):
g = nx.erdos_renyi_graph(1000, 0.1)
model = si.SIModel(g)
config = mc.Configuration()
config.add_model_parameter('beta', 0.01)
config.add_model_parameter("percentage_infected", 0.1)
model.set_initial_status(config)
executions = ut.multi_runs(model, execution_number=10, iteration_number=50)
self.assertEqual(len(executions), 10)
iterations = model.iteration_bunch(10, node_status=False)
self.assertEqual(len(iterations), 10)
python类erdos_renyi_graph()的实例源码
def test_threshold_model(self):
g = nx.erdos_renyi_graph(1000, 0.1)
model = th.ThresholdModel(g)
config = mc.Configuration()
config.add_model_parameter('percentage_infected', 0.1)
threshold = 0.2
for i in g.nodes():
config.add_node_configuration("threshold", i, threshold)
model.set_initial_status(config)
iterations = model.iteration_bunch(10)
self.assertEqual(len(iterations), 10)
iterations = model.iteration_bunch(10, node_status=False)
self.assertEqual(len(iterations), 10)
def test_profile_threshold_model(self):
g = nx.erdos_renyi_graph(1000, 0.1)
model = pt.ProfileThresholdModel(g)
config = mc.Configuration()
config.add_model_parameter('percentage_infected', 0.1)
threshold = 0.2
profile = 0.1
for i in g.nodes():
config.add_node_configuration("threshold", i, threshold)
config.add_node_configuration("profile", i, profile)
model.set_initial_status(config)
iterations = model.iteration_bunch(10)
self.assertEqual(len(iterations), 10)
model = pt.ProfileThresholdModel(g)
config = mc.Configuration()
config.add_model_parameter('percentage_infected', 0.1)
config.add_model_parameter("blocked", 0.1)
config.add_model_parameter("adopter_rate", 0.001)
threshold = 0.2
profile = 0.1
for i in g.nodes():
config.add_node_configuration("threshold", i, threshold)
config.add_node_configuration("profile", i, profile)
model.set_initial_status(config)
iterations = model.iteration_bunch(10)
self.assertEqual(len(iterations), 10)
iterations = model.iteration_bunch(10, node_status=False)
self.assertEqual(len(iterations), 10)
def test_profile_model(self):
g = nx.erdos_renyi_graph(1000, 0.1)
model = pr.ProfileModel(g)
config = mc.Configuration()
config.add_model_parameter('percentage_infected', 0.1)
profile = 0.1
for i in g.nodes():
config.add_node_configuration("profile", i, profile)
model.set_initial_status(config)
iterations = model.iteration_bunch(10)
self.assertEqual(len(iterations), 10)
model = pr.ProfileModel(g)
config = mc.Configuration()
config.add_model_parameter('percentage_infected', 0.1)
config.add_model_parameter("blocked", 0.1)
config.add_model_parameter("adopter_rate", 0.001)
profile = 0.1
for i in g.nodes():
config.add_node_configuration("profile", i, profile)
model.set_initial_status(config)
iterations = model.iteration_bunch(10, node_status=False)
self.assertEqual(len(iterations), 10)
def test_independent_cascade_model(self):
g = nx.erdos_renyi_graph(1000, 0.1)
model = ids.IndependentCascadesModel(g)
config = mc.Configuration()
config.add_model_parameter('percentage_infected', 0.1)
threshold = 0.1
for e in g.edges():
config.add_edge_configuration("threshold", e, threshold)
model.set_initial_status(config)
iterations = model.iteration_bunch(10)
self.assertEqual(len(iterations), 10)
iterations = model.iteration_bunch(10, node_status=False)
self.assertEqual(len(iterations), 10)
def test_initial_infected(self):
g = nx.erdos_renyi_graph(1000, 0.1)
model = sis.SISModel(g)
config = mc.Configuration()
config.add_model_parameter('beta', 0.5)
config.add_model_parameter('lambda', 0.2)
predefined_infected = [0, 1, 2, 3, 4, 5]
config.add_model_initial_configuration("Infected", predefined_infected)
model.set_initial_status(config)
inft = [k for k, v in future.utils.iteritems(model.status) if v == 1]
self.assertAlmostEqual(inft, predefined_infected)
iterations = model.iteration_bunch(10)
self.assertEqual(len(iterations), 10)
def setUp( self ):
'''Set up the experimental parameters and experiment.'''
# single experiment
self._params = dict(pInfect = 0.1,
pInfected = 0.01,
tInfected = 1)
self._network = networkx.erdos_renyi_graph(1000, 0.005)
# lab run
self._lab = epyc.Lab()
self._lab['pInfect'] = [ 0.1, 0.2, 0.3 ]
self._lab['pInfected'] = [ 0.01 ]
self._lab['tInfected'] = [ 0.5, 1, 2 ]
def setUp( self ):
'''Set up the experimental parameters and experiment.'''
self._er = networkx.erdos_renyi_graph(1000, 0.005)
self._params = dict(pInfect = 0.1,
pInfected = 0.01,
pRemove = 0.05)
def partition_at_level(dendrogram, level):
"""Return the partition of the nodes at the given level
A dendrogram is a tree and each level is a partition of the graph nodes.
Level 0 is the first partition, which contains the smallest communities,
and the best is len(dendrogram) - 1.
The higher the level is, the bigger are the communities
Parameters
----------
dendrogram : list of dict
a list of partitions, ie dictionnaries where keys of the i+1 are the
values of the i.
level : int
the level which belongs to [0..len(dendrogram)-1]
Returns
-------
partition : dictionnary
A dictionary where keys are the nodes and the values are the set it
belongs to
Raises
------
KeyError
If the dendrogram is not well formed or the level is too high
See Also
--------
best_partition which directly combines partition_at_level and
generate_dendrogram to obtain the partition of highest modularity
Examples
--------
>>> G=nx.erdos_renyi_graph(100, 0.01)
>>> dendrogram = generate_dendrogram(G)
>>> for level in range(len(dendrogram) - 1) :
>>> print("partition at level", level, "is", partition_at_level(dendrogram, level)) # NOQA
"""
partition = dendrogram[0].copy()
for index in range(1, level + 1):
for node, community in partition.items():
partition[node] = dendrogram[index][community]
return partition
def modularity(partition, graph) :
"""Compute the modularity of a partition of a graph
Parameters
----------
partition : dict
the partition of the nodes, i.e a dictionary where keys are their nodes and values the communities
graph : networkx.Graph
the networkx graph which is decomposed
Returns
-------
modularity : float
The modularity
Raises
------
KeyError
If the partition is not a partition of all graph nodes
ValueError
If the graph has no link
TypeError
If graph is not a networkx.Graph
References
----------
.. 1. Newman, M.E.J. & Girvan, M. Finding and evaluating community structure in networks. Physical Review E 69, 26113(2004).
Examples
--------
>>> G=nx.erdos_renyi_graph(100, 0.01)
>>> part = best_partition(G)
>>> modularity(part, G)
"""
if type(graph) != nx.Graph :
raise TypeError("Bad graph type, use only non directed graph")
inc = dict([])
deg = dict([])
links = graph.size(weight='weight')
if links == 0 :
raise ValueError("A graph without link has an undefined modularity")
for node in graph :
# community label
com = partition[node]
#sum of the node's degree of the node with label 'com'
deg[com] = deg.get(com, 0.) + graph.degree(node, weight = 'weight')
for neighbor, datas in graph[node].items() :
weight = datas.get("weight", 1)
if partition[neighbor] == com :
if neighbor == node :
inc[com] = inc.get(com, 0.) + float(weight)
else :
inc[com] = inc.get(com, 0.) + float(weight) / 2.
res = 0.
for com in set(partition.values()) :
res += (inc.get(com, 0.) / links) - (deg.get(com, 0.) / (2.*links))**2
return res
def modularity(partition, graph) :
"""Compute the modularity of a partition of a graph
Parameters
----------
partition : dict
the partition of the nodes, i.e a dictionary where keys are their nodes and values the communities
graph : networkx.Graph
the networkx graph which is decomposed
Returns
-------
modularity : float
The modularity
Raises
------
KeyError
If the partition is not a partition of all graph nodes
ValueError
If the graph has no link
TypeError
If graph is not a networkx.Graph
References
----------
.. 1. Newman, M.E.J. & Girvan, M. Finding and evaluating community structure in networks. Physical Review E 69, 26113(2004).
Examples
--------
>>> G=nx.erdos_renyi_graph(100, 0.01)
>>> part = best_partition(G)
>>> modularity(part, G)
"""
if type(graph) != nx.Graph :
raise TypeError("Bad graph type, use only non directed graph")
inc = dict([])
deg = dict([])
links = graph.size(weight='weight')
if links == 0 :
raise ValueError("A graph without link has an undefined modularity")
for node in graph :
# community label
com = partition[node]
#sum of the node's degree of the node with label 'com'
deg[com] = deg.get(com, 0.) + graph.degree(node, weight = 'weight')
for neighbor, datas in graph[node].items() :
weight = datas.get("weight", 1)
if partition[neighbor] == com :
if neighbor == node :
inc[com] = inc.get(com, 0.) + float(weight)
else :
inc[com] = inc.get(com, 0.) + float(weight) / 2.
res = 0.
for com in set(partition.values()) :
res += (inc.get(com, 0.) / links) - (deg.get(com, 0.) / (2.*links))**2
return res
def partition_at_level(dendrogram, level):
"""Return the partition of the nodes at the given level
A dendrogram is a tree and each level is a partition of the graph nodes.
Level 0 is the first partition, which contains the smallest communities,
and the best is len(dendrogram) - 1.
The higher the level is, the bigger are the communities
Parameters
----------
dendrogram : list of dict
a list of partitions, ie dictionnaries where keys of the i+1 are the
values of the i.
level : int
the level which belongs to [0..len(dendrogram)-1]
Returns
-------
partition : dictionnary
A dictionary where keys are the nodes and the values are the set it
belongs to
Raises
------
KeyError
If the dendrogram is not well formed or the level is too high
See Also
--------
best_partition which directly combines partition_at_level and
generate_dendrogram to obtain the partition of highest modularity
Examples
--------
>>> G=nx.erdos_renyi_graph(100, 0.01)
>>> dendrogram = generate_dendrogram(G)
>>> for level in range(len(dendrogram) - 1) :
>>> print("partition at level", level, "is", partition_at_level(dendrogram, level)) # NOQA
"""
partition = dendrogram[0].copy()
for index in range(1, level + 1):
for node, community in partition.items():
partition[node] = dendrogram[index][community]
return partition
def partition_at_level(dendrogram, level):
"""Return the partition of the nodes at the given level
A dendrogram is a tree and each level is a partition of the graph nodes.
Level 0 is the first partition, which contains the smallest communities,
and the best is len(dendrogram) - 1.
The higher the level is, the bigger are the communities
Parameters
----------
dendrogram : list of dict
a list of partitions, ie dictionnaries where keys of the i+1 are the
values of the i.
level : int
the level which belongs to [0..len(dendrogram)-1]
Returns
-------
partition : dictionnary
A dictionary where keys are the nodes and the values are the set it
belongs to
Raises
------
KeyError
If the dendrogram is not well formed or the level is too high
See Also
--------
best_partition which directly combines partition_at_level and
generate_dendrogram to obtain the partition of highest modularity
Examples
--------
>>> G=nx.erdos_renyi_graph(100, 0.01)
>>> dendrogram = generate_dendrogram(G)
>>> for level in range(len(dendrogram) - 1) :
>>> print("partition at level", level, "is", partition_at_level(dendrogram, level)) # NOQA
"""
partition = dendrogram[0].copy()
for index in range(1, level + 1):
for node, community in partition.items():
partition[node] = dendrogram[index][community]
return partition
def test_optional_parameters(self):
g = nx.erdos_renyi_graph(1000, 0.1)
model = th.ThresholdModel(g)
config = mc.Configuration()
config.add_model_parameter('percentage_infected', 0.1)
model.set_initial_status(config)
iterations = model.iteration_bunch(10)
config.add_node_set_configuration("test", {n: 1 for n in g.nodes()})
config.add_edge_set_configuration("etest", {e: 1 for e in g.edges()})
self.assertEqual(len(iterations), 10)
model = ks.KerteszThresholdModel(g)
config = mc.Configuration()
config.add_model_parameter('adopter_rate', 0.4)
predefined_blocked = [0, 1, 2, 3, 4, 5]
config.add_model_initial_configuration("Blocked", predefined_blocked)
config.add_model_parameter('percentage_infected', 0.1)
model.set_initial_status(config)
iteration = model.iteration()
blocked = [x for x, v in future.utils.iteritems(iteration["status"]) if v == -1]
self.assertEqual(blocked, predefined_blocked)
model = ids.IndependentCascadesModel(g)
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 = pr.ProfileModel(g)
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 = pt.ProfileThresholdModel(g)
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 = th.ThresholdModel(g)
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 = ks.KerteszThresholdModel(g)
config = mc.Configuration()
config.add_model_parameter('adopter_rate', 0.4)
config.add_model_parameter('percentage_blocked', 0.1)
config.add_model_parameter('percentage_infected', 0.1)
model.set_initial_status(config)
iterations = model.iteration_bunch(10)
self.assertEqual(len(iterations), 10)