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:

[6]:
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])
[6]:
[<pop2net.creator.Location object at 0x7cf82757e450>,
 <pop2net.creator.Location object at 0x7cf8275811f0>,
 <pop2net.creator.Location object at 0x7cf8276458b0>,
 <pop2net.creator.Location object at 0x7cf8275806e0>,
 <pop2net.creator.Location object at 0x7cf82ad1f950>,
 <pop2net.creator.Location object at 0x7cf82757ef30>,
 <pop2net.creator.Location object at 0x7cf827580fe0>,
 <pop2net.creator.Location object at 0x7cf8275808c0>,
 <pop2net.creator.Location object at 0x7cf827580920>,
 <pop2net.creator.Location object at 0x7cf827580980>,
 <pop2net.creator.Location object at 0x7cf82766f8c0>,
 <pop2net.creator.Location object at 0x7cf82766f5f0>,
 <pop2net.creator.Location object at 0x7cf8275814f0>,
 <pop2net.creator.Location object at 0x7cf827581610>,
 <pop2net.creator.Location object at 0x7cf8275815b0>,
 <pop2net.creator.Location object at 0x7cf827581760>,
 <pop2net.creator.Location object at 0x7cf827581460>,
 <pop2net.creator.Location object at 0x7cf827581b80>,
 <pop2net.creator.Location object at 0x7cf827714140>,
 <pop2net.creator.Location object at 0x7cf8275800b0>,
 <pop2net.creator.Location object at 0x7cf827581fa0>,
 <pop2net.creator.Location object at 0x7cf8275807d0>,
 <pop2net.creator.Location object at 0x7cf827582000>,
 <pop2net.creator.Location object at 0x7cf827582060>,
 <pop2net.creator.Location object at 0x7cf8275820c0>,
 <pop2net.creator.Location object at 0x7cf82757e570>,
 <pop2net.creator.Location object at 0x7cf827580c20>,
 <pop2net.creator.Location object at 0x7cf8275820f0>,
 <pop2net.creator.Location object at 0x7cf827582270>,
 <pop2net.creator.Location object at 0x7cf8275822d0>,
 <pop2net.creator.Location object at 0x7cf82757dc10>,
 <pop2net.creator.Location object at 0x7cf827668920>,
 <pop2net.creator.Location object at 0x7cf827582480>,
 <pop2net.creator.Location object at 0x7cf82757c680>,
 <pop2net.creator.Location object at 0x7cf8275824b0>,
 <pop2net.creator.Location object at 0x7cf82757eed0>,
 <pop2net.creator.Location object at 0x7cf82757f200>,
 <pop2net.creator.Location object at 0x7cf8275826c0>,
 <pop2net.creator.Location object at 0x7cf827582420>,
 <pop2net.creator.Location object at 0x7cf827582750>,
 <pop2net.creator.Location object at 0x7cf827644e00>,
 <pop2net.creator.Location object at 0x7cf827582690>,
 <pop2net.creator.Location object at 0x7cf82757e0c0>,
 <pop2net.creator.Location object at 0x7cf827582900>,
 <pop2net.creator.Location object at 0x7cf827582990>,
 <pop2net.creator.Location object at 0x7cf8f827dfd0>,
 <pop2net.creator.Location object at 0x7cf8275828a0>,
 <pop2net.creator.Location object at 0x7cf8275828d0>,
 <pop2net.creator.Location object at 0x7cf82757c800>,
 <pop2net.creator.Location object at 0x7cf8276470e0>]

Let’s have look on the network before we export it:

[7]:
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.

[11]:
bipartite_graph = env.export_bipartite_network(actor_attrs=["age"])
bipartite_graph
[11]:
<networkx.classes.graph.Graph at 0x7cf827584bc0>

To transform the network into a agent-level network and save it as a networkx graph object, use model.export_agent_network().

[15]:
actor_graph = env.export_actor_network(node_attrs=["age"])
actor_graph
[15]:
<networkx.classes.graph.Graph at 0x7cf827588e60>