The Pop2net basics

After the installation of Pop2net, the first thing we have to do is to import the package in order to use it:

[1]:
import pop2net as p2n

Model

In Pop2net, Model is the most important object class because almost everything happens inside - or at least in regard to - a Model. You can imagine a Model as the world where all the entities we create live. Therefore, everthing you can do in Pop2net starts with creating an instance of p2n.Model.

[3]:
model = p2n.Model()

A Model has three very important attributes:

  1. model.g is a network-graph which stores all agents, locations and their relations to each other as a bipartite network:

[4]:
model.g
[4]:
<networkx.classes.graph.Graph at 0x1d387d8f410>

You never need to access model.g yourself unless you are a very experienced Pop2net user. In most cases, anything you want to do with the network can be done using the convenient methods provided by Pop2net.

  1. model.agents returns a list of all agents stored in model.g:

[5]:
model.agents
[5]:
AgentList (0 objects)
  1. Model.locations returns a list of all locations stored in Model.g:

[6]:
model.locations
[6]:
LocationList (0 objects)

Agents and locations

Next, we create our first agent:

[7]:
agent = p2n.Agent(model)

Whenever a new instance of an Agent is created, it gets automatically added to the given instance of Model. This means that the created Agent instance is represented as a node in model.g

[8]:
model.g.nodes
[8]:
NodeView((1,))

… and gets returned in model.agents:

[9]:
model.agents
[9]:
AgentList (1 object)

The same applies to locations:

[10]:
location = p2n.Location(model)
[11]:
model.g.nodes
[11]:
NodeView((1, 2))
[12]:
model.locations
[12]:
LocationList (1 object)

A quick look: Plotting a network graph

Often the best thing you can do to understand things is to look at them. In Pop2net, the NetworkInspector helps you to understand the network you are building. For instance, using the NetworkInsepctor, you can visualize the bipartite network of agents and locations:

[13]:
inspector = p2n.NetworkInspector(model)
inspector.plot_bipartite_network()

To be honest, the above is not really a network because there are only two nodes without any edge. However, the graph gives an intuitive overview of the entities in your model.

There is also a method to plot the graph at the agent level:

[14]:
inspector.plot_agent_network()

And there is also a method to plot both graphs!

[15]:
inspector.plot_networks()

Creating edges between agents and locations

Our model has one agent and one location. Let’s create an edge between those two entities by adding the agent to the location:

[16]:
location.add_agent(agent)

By the way: we could also let the agent do the same action by running agent.add_location(location). And we could also let the model do the work using model.add_agent_to_location(location=location, agent=agent).

Anyway, let’s have a look at the network graph:

[17]:
inspector.plot_bipartite_network()

The attribute location.agents shows all agents that are connected to this location:

[18]:
location.agents
[18]:
AgentList (1 object)

The attribute agent.locations shows all locations that are connected to the agent:

[19]:
agent.locations
[19]:
LocationList (1 object)

Let’s add 3 more agents to the model using a For-Loop:

[20]:
for _ in range(3):
    p2n.Agent(model)

As you can see, we do not even have to assign the created instances of Agent to a variable because their are added to the model automatically during the initialization. Now we have 4 agents in our model:

[21]:
model.agents
[21]:
AgentList (4 objects)

Let’s add an additional location. But now we create our own location class School which inherits from p2n.Location. You will understand the purpose of this later.

[22]:
class School(p2n.Location):
    pass

Again, we only have to initialize the instance of School to add it to the model:

[23]:
School(model)
[23]:
School (Obj 6)

Now, the model has two locations:

[24]:
model.locations
[24]:
LocationList (2 objects)

Let’s have a look at the bipartite network graph:

[25]:
inspector.plot_bipartite_network()

Because there is still only one edge, in the next step, we assign each agent to one location and make sure that each location has not more than 2 agents:

[26]:
for location in model.locations:
    for agent in model.agents:
        if len(location.agents) < 2 and len(agent.locations) == 0:
            location.add_agent(agent)

Let’s look at the graph again:

[27]:
inspector.plot_bipartite_network()

Just because we can, let’s create some more edges by adding one agent to all locations:

[28]:
agent = model.agents[0]
for location in model.locations:
    location.add_agent(agent)
[29]:
inspector.plot_bipartite_network()

Here is the corresponding agent-level network graph:

[30]:
inspector.plot_agent_network()

Removing entities from the graph

So far, we only added agents to the model, locations to the model, agents to locations and locations to agents. Of course we can also remove things.

To remove the edge between an agent and a location, we can use mode.remove_agent_from_location(), agent.remove_location() and location.remove_agent(). In the following example, we remove the edge between agent and location using location.remove_agent():

[31]:
location.remove_agent(agent)
[32]:
inspector.plot_networks()

To remove agents or locations from the graph, we can use model.remove_agent() and model.remove_location(). In the following example, we remove agent from the graph:

[33]:
model.remove_agent(agent)
[34]:
inspector.plot_networks()

Let’s put agent back in the graph:

[35]:
model.add_agent(agent)
[36]:
inspector.plot_networks()

Connecting agents quickly

In Pop2net, agents are considered connected if they share a location. Thus, in order to connect agents, we first have to create a location first and then assign the agents to this location.

The methods agent.connect() and model.connect_agents() simplify that process by creating an instance from a given location class and directly assigning the given agents to this location instance. In the following, we connect agent with the second agent of the model model.agents[1] using agent.connect():

[37]:
agent.connect(agent=model.agents[1], location_cls=School)
[38]:
inspector.plot_networks()

Using model.connect_agents() we can also connect multiple agents at the same time. In addition, this time we use a custom location class to connect the agents:

[39]:
class World(p2n.Location):
    pass
[40]:
model.connect_agents(agents=model.agents, location_cls=World)
[41]:
inspector.plot_networks()

Finding neighbors and shared locations

From the perspective of an agent, it is very important to know with whom the agent is connected in the network. As Pop2net works with bipartite networks, technically agents are only connected with locations. As you might already know, agents can access all connected locations via agent.locations:

[42]:
agent.locations
[42]:
LocationList (2 objects)

As locations are mainly a helping tool to connect agents, we often want to know which other agents are connected to the same locations. Every agent can access those other agents who are at the same location via agent.neighbors():

[43]:
agent.neighbors()
[43]:
AgentList (3 objects)

If we want to find only those neighbors who share a specific type of location with the agent, we can use the argument location_labels to filter by types of locations. In the following we only want neighbors who share a location of the type School with the agent:

[44]:
agent.neighbors(location_labels=["School"])
[44]:
AgentList (1 object)

By using different types of locations we can organize the relations of an agent to other agents and then access neighbors with a certain relation to the agent as we just did it above.

Sometimes we need to access all the locations an agent shares with another agent. This can be done using agent.shared_locations():

[45]:
agent2 = model.agents[1]

agent.shared_locations(agent2)
[45]:
LocationList (2 objects)

Agent.shared_locations() also has the argument location_labels to filter by the type of the location:

[46]:
agent.shared_locations(agent2, location_labels=["School"])
[46]:
[School (Obj 7)]
[47]:
agent.shared_locations(agent2, location_labels=["World"])
[47]:
[World (Obj 8)]