Environment

class pop2net.Environment(model=None, framework=None, enable_p2n_warnings=True)[source]

Class that organizes actors and locations.

property actors: list

Show a iterable view of all actors in the environment.

Important: While you can make changes to the objects in this list, you cannot modify this attribute itself. Instead you have to use methods like Environment.add_actor() or Environment.remove_actor(), for instance.

Returns:

A non-mutable list of all actors in the environment.

Return type:

list

actors_between_locations(location1, location2, actor_types=None)[source]

Return all actors between two locations.

Parameters:
  • location1 (Location) – Location 1.

  • location2 (Location) – Location 2.

  • actor_types (tuple, optional) – Constrain the actors to the following types. Defaults to None.

Returns:

A list of actors.

Return type:

List

actors_of_location(location)[source]

Return the list of actors associated with a specific location.

Parameters:

location (Location) – The desired location.

Returns:

A list of actors.

add_actor(actor)[source]

Add an actor to the environment.

The added actor will have no connections to other actors or locatons by default. If the actor is already in the current environment, this methods does nothing.

Parameters:

actor (Actor) – Actor to be added to the environment.

Return type:

None

add_actor_to_location(location, actor, weight=None, **kwargs)[source]

Add an actor to a specific location.

Both the actor and the location have to be defined beforehand. All additional keyword arguments will be edge attributes for this connection.

Parameters:
  • location (Location) – Location the actor is to be added to.

  • actor (Actor) – Actor to be added to the location.

  • weight (Optional[float]) – An optional weight for the connection.

  • **kwargs – Additional edge attributes.

Raises:
  • Exception – Raised if the location does not exist in the environment.

  • Exception – Raised if the actor does not exist in the environment.

Return type:

None

add_actors(actors)[source]

Add actors to the environment.

Parameters:

actors (list) – A list of the actors to be added.

Return type:

None

add_location(location)[source]

Add a location to the environment.

The added location will have no connections to other actors or locatons by default. If the location is already in the current environment, this methods does nothing.

Parameters:

location (Location) – Location to be added to the environment.

Return type:

None

add_locations(locations)[source]

Add multiple locations to the environment at once.

Parameters:

locations (list) – An iterable over multiple locations.

Return type:

None

connect_actors(actors, location_cls, weight=None)[source]

Connects multiple actors via an instance of a given location class.

Parameters:
  • actors (list) – A list of actors.

  • location_cls (type) – The location class that is used to create a location instance.

  • weight (float | None) – The edge weight between the actors and the location. Defaults to None.

disconnect_actors(actors, location_labels=None, remove_locations=False)[source]

Disconnects actors by removing them from shared locations.

If a list of location types is given, only shared locations of the given types are considered. Turn on remove_locations in order to not only remove the given actors from the given location instance but also to remove the location instance from the environment. Use this method with care because removing actors from locations also disconnects those actors from all other actors connected to the location. Removing the location instance from the environment could have even more sideeffects to those actors still connected with this location!

Parameters:
  • actors (list) – A list of actors.

  • location_labels (list | None, optional) – A list of location types to specify which

  • None. (shared locations are considered. Defaults to)

  • remove_locations (bool, optional) – A bool that determines whether the shared locations shall be removed from the environment. Defaults to False.

export_actor_network(node_attrs=None, include_0_weights=True)[source]

Creates a projection of the environment’s bipartite network.

Parameters:
  • node_attrs (Optional[list]) – A list of actor attributes

  • include_0_weights (bool) – Should edges with weight 0 be displayed?

Return type:

Graph

Returns:

A weighted graph created from a environment’s actor list. Actors are connected if they are neighbors in the environment. Their connecting edge include the contact_weight as “weight” attribute.

get_weight(actor, location)[source]

Get the weight of an actor at a location.

Parameters:
  • actor (Actor) – The actor.

  • location (Location) – The location.

Returns:

The weight.

Return type:

int

property locations: list

Show a iterable view of all locations in the environment.

Important: While you can make changes to the objects in this list, you cannot modify this attribute itself. Instead you have to use methods like Environment.add_location() or Environment.remove_location(), for instance.

Returns:

a non-mutable LocationList of all locations in the environment.

Return type:

LocationList

locations_between_actors(actor1, actor2, location_labels=None)[source]

Return all locations that connects two actors.

Parameters:
  • actor1 (Actor) – Actor 1.

  • actor2 (Actor) – Actor 2.

  • location_labels (tuple, optional) – Constrain the locations to the following types. Defaults to None.

Returns:

A list of locations.

Return type:

LocationList

locations_of_actor(actor)[source]

Return the list of locations associated with a specific actor.

Parameters:

actor (Actor) – The desired actor.

Return type:

list[Location]

Returns:

A list of locations.

neighbors_of_actor(actor, location_labels=None)[source]

Return a list of neighboring actors for a specific actor.

The locations to be considered can be defined with location_labels.

Parameters:
  • actor (Actor) – Actor of whom the neighbors are to be returned.

  • location_labels (Optional[list]) – A list of location_labels.

Return type:

list[Actor]

Returns:

The list of neighbors for the specified actor.

remove_actor(actor)[source]

Remove an actor from the environment.

If the actor does not exist in the environment, this method does nothing.

Parameters:

actor (Actor) – Actor to be removed.

Return type:

None

remove_actor_from_location(location, actor)[source]

Remove an actor from a location.

Parameters:
  • location (Location) – Location, the actor is to be removed from.

  • actor (Actor) – Actor to be disassociated with the location.

Raises:
  • Exception – Raised if the location does not exist in the environment.

  • Exception – Raised if the actor does not exist in the environment.

Return type:

None

remove_actors(actors)[source]

Remove multiple actors from the environment at once.

Parameters:

actors (list) – An iterable over multiple actors.

Return type:

None

remove_location(location)[source]

Remove a location from the environment.

If the location does not exist in the environment, this method does nothing.

Parameters:

location (Location) – Location to be removed.

Return type:

None

remove_locations(locations)[source]

Remove multiple locations at once.

Parameters:

locations (list) – An iterable over locations.

Return type:

None

set_weight(actor, location, weight=None)[source]

Set the weight of an actor at a location.

If weight is None the method location.weight() will be used to generate a weight.

Parameters:
  • actor (Actor) – The actor.

  • location (Location) – The location.

  • weight (int) – The weight

Return type:

None

update_weights(location_labels=None)[source]

Updates the edge weights between actors and locations.

If you only want to update the weights of specific types of locations specify those types in location_labels.

Parameters:

location_labels (list | None, optional) – A list of location classes that specifiy for which location types the weights should be updated. If location_labels is None all locations are considered. Defaults to None.

Return type:

None