Network Inspector

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

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

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

Create a contact matrix as a DataFrame from a given env’s actor list.

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

  • attr (str) – The actor 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 actors per location and locations per actor.

Raises:
  • Pop2netException – _description_

  • Pop2netException – _description_

Return type:

Optional[tuple[DataFrame, DataFrame]]

location_crosstab(location_labels, actor_attributes, output_format='table')[source]

Generates a crosstabulation of actor attributes for specified location labels.

Parameters:
  • location_labels (str | list[str]) – Location label(s) to filter locations for the crosstab.

  • actor_attributes (str | list[str]) – Actor attribute(s) to use for the crosstab.

  • output_format (str, optional) – Output format, either “table” for printed tables or “df” for DataFrame output. Defaults to “table”.

Returns:

List of DataFrames with crosstab results if output_format is “df”, otherwise None.

Return type:

list[pd.DataFrame] | None

location_information(location_labels, actor_attributes=None, output_format='table')[source]

Provides detailed information about actors assigned to specific location instances.

This method allows filtering by location labels and actor attributes, and can output the information either as a formatted table (printed to stdout) or as a pandas DataFrame.

Parameters:
  • location_labels (str | list[str]) – One or more location labels to filter the locations.

  • actor_attributes (str | None | list[str], optional) – One or more actor attributes to include in the output. If None, a default subset of attributes is used. Defaults to None.

  • output_format (str, optional) – Determines the output format. Use “table” to print a formatted table, or “df” to return a pandas DataFrame. Defaults to “table”.

Returns:

Returns None if output_format is “table” (prints to stdout).

Returns a pandas DataFrame if output_format is “df”.

Return type:

None | pd.DataFrame

network_measures(actor_attrs=None)[source]

Calculates common network measures for the actor-level network graph.

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

Parameters:

actor_attrs – A list of actor attributes

Return type:

list[dict]

Returns:

list of dictionaries of the common network measure results

plot_actor_network(actor_color=None, actor_attrs=None, edge_alpha='weight', edge_color='black', include_0_weights=True, edge_size=1, node_size=10, node_alpha=0.5)[source]

Plots the actor network.

Parameters:
  • actor_color (str | None, optional) – The actor attribute that determines the

  • None. (color of the actor nodes. Defaults to)

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

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

  • edge_color (str, optional) – The color of the edges. Defaults to “black”.

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

  • edge_size (int, optional) – The size of the edges. Defaults to 1.

  • node_size (int, optional) – The size of the nodes. Defaults to 10.

  • node_alpha (float, optional) – The transparency of the nodes. Defaults to 0.5.

Return type:

None

plot_bipartite_network(actor_attrs=None, location_attrs=None, actor_color=None, location_color=None, edge_alpha='weight', edge_color='black', edge_size=1, node_size=10, node_alpha=0.5)[source]

Plots the bipartite network of actors and locations.

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

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

  • actor_color (str | None, optional) – The actor attribute that determines the

  • None. (color of the location nodes. Defaults to)

  • location_color (str | None, optional) – The location attribute that determines the

  • None.

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

  • edge_color (str, optional) – The color of the edges. Defaults to “black”.

  • edge_size (int, optional) – The size of the edges. Defaults to 1.

  • node_size (int, optional) – The size of the nodes. Defaults to 10.

  • node_alpha (float, optional) – The transparency of the nodes. Defaults to 0.5.

Return type:

None

plot_networks(actor_attrs=None, location_attrs=None, actor_color=None, location_color=None, edge_alpha='weight', edge_color='black', include_0_weights=True, edge_size=1, node_size=10, node_alpha=0.5)[source]

Plots the bipartite network and the actor network.

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

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

  • actor_color (str | None, optional) – The actor attribute that determines the color of the actor nodes. Defaults to None.

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

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

  • edge_color (str, optional) – The color of the edges. Defaults to “black”.

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

  • edge_size (int, optional) – The size of the edges. Defaults to 1.

  • node_size (int, optional) – The size of the nodes. Defaults to 10.

  • node_alpha (float, optional) – The transparency of the nodes. Defaults to 0.5.

Return type:

None