Network model export
A network generated with Pop2net can be exported as a networkx graph object using model.export_bipartite_network()
and model.export_network()
.
First, let us create a example network:
[1]:
import random
import networkx as nx
import pop2net as p2n
N_RINGS = 5
N_ACTORS = 25
env = p2n.Environment()
creator = p2n.Creator(env)
inspector = p2n.NetworkInspector(env)
class MyActor(p2n.Actor):
def __init__(self):
super().__init__()
self.age = random.randint(20, 80)
class Ring(p2n.LocationDesigner):
n_locations = N_RINGS
nxgraph = nx.cycle_graph(int(N_ACTORS / N_RINGS))
def weight(self, actor):
return 10
class Bridge(p2n.LocationDesigner):
def mutate(self):
return {"focal_ring_id": list(range(N_RINGS))}
def filter(self, actor):
return actor.Ring_id in [self.focal_ring_id, self.focal_ring_id + 1]
def split(self, actor):
return actor.Ring_position
_ = creator.create_actors(n=N_ACTORS, actor_class=MyActor)
_ = creator.create_locations(location_designers=[Ring, Bridge])
Let’s have look on the network before we export it:
[2]:
inspector.plot_networks()
In order to export the bipartite network as a networkx graph object, use model.export_bipartite_network()
. Using the arguments location_attrs
and agent_attrs
, location attributes and agent attributes will be saved as node attributes.
[3]:
bipartite_graph = env.export_bipartite_network(actor_attrs=["age"])
bipartite_graph
[3]:
<networkx.classes.graph.Graph at 0x7347d2a33500>
To transform the network into a agent-level network and save it as a networkx graph object, use model.export_actor_network()
.
[4]:
actor_graph = env.export_actor_network(node_attrs=["age"])
actor_graph
[4]:
<networkx.classes.graph.Graph at 0x7347d22407a0>