Network model export
A network generated with Pop2net can be exported as a networkx graph object using model.export_bipartite_network()
and model.export_agent_network()
.
First, let us create a example network:
[1]:
import networkx as nx
import pop2net as p2n
N_RINGS = 5
N_AGENTS = 25
model = p2n.Model()
creator = p2n.Creator(model=model)
inspector = p2n.NetworkInspector(model=model)
class MyAgent(p2n.Agent):
def __init__(self, model, *args, **kwargs):
super().__init__(model, *args, **kwargs)
self.age = self.model.random.randint(20, 80)
class Ring(p2n.LocationDesigner):
n_locations = N_RINGS
nxgraph = nx.cycle_graph(int(N_AGENTS / N_RINGS))
def weight(self, agent):
return 10
class Bridge(p2n.LocationDesigner):
def mutate(self):
return {"focal_ring_id": list(range(N_RINGS))}
def filter(self, agent):
return agent.Ring_id in [self.focal_ring_id, self.focal_ring_id + 1]
def split(self, agent):
return agent.Ring_position
creator.create_agents(n=N_AGENTS, agent_class=MyAgent)
creator.create_locations(location_designers=[Ring, Bridge])
[1]:
LocationList (50 objects)
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 = model.export_bipartite_network(agent_attrs=["age"])
bipartite_graph
[3]:
<networkx.classes.graph.Graph at 0x16805efc550>
To transform the network into a agent-level network and save it as a networkx graph object, use model.export_agent_network()
.
[4]:
agent_graph = model.export_agent_network(node_attrs=["age"])
agent_graph
[4]:
<networkx.classes.graph.Graph at 0x1685fdca210>