Network Inspector

class pop2net.inspector.NetworkInspector(model)[source]

Helper class that contains functions to inspect the network of a pop2net model.

create_contact_matrix(agents=None, attr='id', weighted=False, plot=True, annot=False, return_df=False)[source]

Create a contact matrix as a DataFrame from a given model’s agent list.

Parameters:
  • agents (Optional[list]) – A list of agents.

  • attr (str) – The agent attribute which is shown in the matrix.

  • weighted (bool) – Should the contacts be weighted? Defaults to False.

  • plot (bool) – Should the matrix be plotted? Defaults to False.

  • annot (bool) – Should the plottet matrix be annotated? Defaults to False.

  • return_df (bool) – Should the data be returned as pandas.DataFrame?

Return type:

DataFrame

Returns:

A DataFrame containing a contact matrix based on attr.

eval_affiliations(return_data=False)[source]

Prints information on the distribution of agents per location and locations per agent.

Raises:
  • Pop2netException – _description_

  • Pop2netException – _description_

Return type:

Optional[tuple[DataFrame, DataFrame]]

location_crosstab(select_locations, agent_attributes, output_format='table')[source]

Crosstable for specified location classes and agent attribute.

location_information(select_locations=None, agent_attributes=None, output_format='table')[source]

Provides information on the agents assigned to location instances.

Displayed information can be filtered by specifying certain location classes and agent_attributes

Parameters:
  • select_locations (p2n.Location | list[p2n.Location] | None, optional) – A list of location classes. Defaults to None.

  • agent_attributes (str | None | list[str] | None, optional) – A list of agent attributes. Defaults to None.

  • output_format (str, optional) – A str determining what is returned. Defaults to “table”.

Returns:

A pandas.DataFrame or nothing.

Return type:

None | pd.DataFrame

network_measures(node_attrs=None)[source]

Creates nx networkgraph and calculates common network measures.

If the created network consist of independent groups of nodes subgraphs are created and measures are calculated for each subgraph

Parameters:

node_attrs – A list of agent attributes

Return type:

dict | list[dict]

Returns:

dictionary/or list of dictionaries of the common network measure results

plot_agent_network(agent_color=None, agent_attrs=None, edge_alpha='weight', edge_color='black', include_0_weights=True)[source]

Plots the agent network.

Parameters:
  • agent_color (str, optional) – The node attribute that determines the color of the nodes. If None, the node color represents whether it is a location or an agent instance.

  • agent_attrs (list | None, optional) – A list of agent attributes that should be shown as node attributes in the network graph. Defaults to None.

  • edge_alpha (str, optional) – The node attribute that determines the edges’ transparency. Defaults to “weight”.

  • edge_color (str, optional) – The node attribute that determines the edges’ color. Defaults to “black”.

  • include_0_weights (bool, optional) – Should edges with a weight of zero be included in the plot? Defaults to True.

Return type:

None

plot_bipartite_network(agent_attrs=None, location_attrs=None, agent_color=None, location_color=None, edge_alpha='weight', edge_color='black')[source]

Plots the two-mode network of agents and locations.

Parameters:
  • agent_color (str, optional) – The agent attribute that determines the color of the agent nodes.

  • agent_attrs (list | None, optional) – A list of agent attributes that should be shown as node attributes in the network graph. Defaults to None.

  • location_color (str, optional) – The location attribute that determines the color of the location nodes.

  • location_attrs (list | None, optional) – A list of location attributes that should be shown as node attributes in the network graph. Defaults to None.

  • edge_alpha (str, optional) – The node attribute that determines the edges’ transparency. Defaults to “weight”.

  • edge_color (str, optional) – The node attribute that determines the edges’ color. Defaults to “black”.

  • include_0_weights (bool, optional) – Should edges with a weight of zero be included in the plot? Defaults to True.

Return type:

None

plot_networks(agent_attrs=None, location_attrs=None, agent_color=None, location_color=None, edge_alpha='weight', edge_color='black', include_0_weights=True)[source]

Plots the two-mode network and the agent network.

Parameters:
  • agent_color (str, optional) – The agent attribute that determines the color of the agent nodes.

  • agent_attrs (list | None, optional) – A list of agent attributes that should be shown as node attributes in the network graph. Defaults to None.

  • location_color (str, optional) – The location attribute that determines the color of the location nodes.

  • location_attrs (list | None, optional) – A list of location attributes that should be shown as node attributes in the network graph. Defaults to None.

  • edge_alpha (str, optional) – The node attribute that determines the edges’ transparency. Defaults to “weight”.

  • edge_color (str, optional) – The node attribute that determines the edges’ color. Defaults to “black”.

  • include_0_weights (bool) – Should edges with a weight of zero be included in the plot? Defaults to True.

Return type:

None