Network analysis

The NetworkInspector provides tools for quickly analyzing the created network.

First, let us create an example network:

[15]:
import pop2net as p2n

model = p2n.Model()
creator = p2n.Creator(model=model)

for _ in range(20):
    agent = p2n.Agent(model=model)
    agent.status = "pupil"

for _ in range(4):
    agent = p2n.Agent(model=model)
    agent.status = "teacher"


class School(p2n.LocationDesigner):
    n_locations = 2
    overcrowding = False

    def weight(self, agent):
        return 0.1

    def stick_together(self, agent):
        return agent.Classroom


class PupilsInClassroom(p2n.MeltLocationDesigner):
    n_agents = 5

    def filter(self, agent):
        return agent.status == "pupil"


class TeacherInClassroom(p2n.MeltLocationDesigner):
    n_agents = 1

    def filter(self, agent):
        return agent.status == "teacher"


class Classroom(p2n.LocationDesigner):
    def melt(self):
        return PupilsInClassroom, TeacherInClassroom


creator.create_locations(
    location_designers=[
        Classroom,
        School,
    ]
)
[15]:
LocationList (6 objects)

In the following, we initialize an instance of the NetworkInspector and bind it to our model:

[16]:
inspector = p2n.NetworkInspector(model=model)

Network visualization

The method plot_bipartite_network() plots the biparte network. Agents are displayed as circles and locations are displayed as squares. Using the arguments agent_color and location_color, agent and location attributes can be displayed as colors in the plot. With the arguments agent_attrs and location_attrs you can add attributes that should be displayed in the tooltips for each node when you move the mouse pointer over it.

In the following example I display the agent attribute status as color and add id to the list of shown attribute values per agent node.

[17]:
inspector.plot_bipartite_network(agent_color="status", agent_attrs=["id"])

You may have noticed that the weight between nodes is automatically reflected in the alpha (transparency) of the edge connecting them.

Using plot_agent_network(), the agent-level network is shown:

[18]:
inspector.plot_agent_network(agent_color="status", agent_attrs=["id"])

Using plot_networks(), both network types are plotted:

[19]:
inspector.plot_networks(agent_color="status", agent_attrs=["id"])

Evaluate affiliations

Using eval_affiliations(), the number of locations per label, the number of agents per location and the number of locations per agent can be displayed:

[20]:
inspector.eval_affiliations()


______________________________________
Number of locations
______________________________________

                count
location_label
Classroom           4
School              2


______________________________________
Number of agents per location
______________________________________

                mean  std   min   25%   50%   75%   max
location_label
Classroom        6.0  0.0   6.0   6.0   6.0   6.0   6.0
School          12.0  0.0  12.0  12.0  12.0  12.0  12.0


______________________________________
Number of affiliated locations per agent
______________________________________

      n_affiliated_locations
mean                     2.0
std                      0.0
min                      2.0
25%                      2.0
50%                      2.0
75%                      2.0
max                      2.0

Network measures

Using inspector.network_measures(), common network measures are calculated for the agent-level network. If there are unconnected components within the network, the network measures are calculated for each component.

[21]:
inspector.network_measures()
[21]:
[{'n_nodes': 12,
  'diameter': 0.2,
  'density': 1.0,
  'transitivity': 1.0,
  'avg_clustering': 0.3472381885547426,
  'avg_path_length': 0.14545454545454548},
 {'n_nodes': 12,
  'diameter': 0.2,
  'density': 1.0,
  'transitivity': 1.0,
  'avg_clustering': 0.3472381885547426,
  'avg_path_length': 0.14545454545454548}]

Coming soon …

The inspector offers several additional methods for analyzing the created network. Detailed documentation will be added soon.