Populations#

policyengine_core.populations contains classes defining Population (a set of people) and GroupPopulation (a set of groups of people).

Population#

class policyengine_core.populations.population.Population(entity: Entity)[source]#

Bases: object

check_array_compatible_with_entity(array: ndarray) None[source]#
check_period_validity(variable_name: str, period: Period) None[source]#
clone(simulation: Simulation) Population[source]#
empty_array() ndarray[source]#
filled_array(value: Any, dtype: Any = None) ndarray[source]#
get_holder(variable_name: str) Holder[source]#
get_index(id: str) int[source]#
get_memory_usage(variables: List[str] = None)[source]#
get_rank(entity: Entity, criteria: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], condition: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]] = True) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Get the rank of a person within an entity according to a criteria. The person with rank 0 has the minimum value of criteria. If condition is specified, then the persons who don’t respect it are not taken into account and their rank is -1.

Example:

>>> age = person('age', period)  # e.g [32, 34, 2, 8, 1]
>>> person.get_rank(household, age)
>>> [3, 4, 0, 2, 1]
>>> is_child = person.has_role(Household.CHILD)  # [False, False, True, True, True]
>>> person.get_rank(household, - age, condition = is_child)  # Sort in reverse order so that the eldest child gets the rank 0.
>>> [-1, -1, 1, 0, 2]
has_any_input(variable_name: str) bool[source]#
has_role(role: Role) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Check if a person has a given role within its GroupEntity

Example:

>>> person.has_role(Household.CHILD)
>>> array([False])
value_from_partner(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], entity: Entity, role: Role) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

GroupPopulation#

class policyengine_core.populations.group_population.GroupPopulation(entity: Entity, members: Population)[source]#

Bases: Population

all(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], role: Role = None) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Return True if array is True for all members of the entity.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example:

>>> salaries = household.members('salary', '2018-01')  # e.g. [2000, 1500, 0, 0, 0]
>>> household.all(salaries >= 1800)
>>> array([False])
any(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], role: Role = None) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Return True if array is True for any members of the entity.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example:

>>> salaries = household.members('salary', '2018-01')  # e.g. [2000, 1500, 0, 0, 0]
>>> household.any(salaries >= 1800)
>>> array([True])
clone(simulation: Simulation, members: Population) GroupPopulation[source]#
get_role(role_name: str) Role[source]#
max(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], role: Role = None) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Return the maximum value of array for the entity members.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example:

>>> salaries = household.members('salary', '2018-01')  # e.g. [2000, 1500, 0, 0, 0]
>>> household.max(salaries)
>>> array([2000])
property members_entity_id: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]#
property members_position: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]#
property members_role: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]#
min(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], role: Role = None) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Return the minimum value of array for the entity members.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example:

>>> salaries = household.members('salary', '2018-01')  # e.g. [2000, 1500, 0, 0, 0]
>>> household.min(salaries)
>>> array([0])
>>> household.min(salaries, role = Household.PARENT)  # Assuming the 1st two persons are parents
>>> array([1500])
nb_persons(role: Role = None) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Returns the number of persons contained in the entity.

If role is provided, only the entity member with the given role are taken into account.

property ordered_members_map: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]#

Mask to group the persons by entity This function only caches the map value, to see what the map is used for, see value_nth_person method.

project(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], role: Role = None) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#
reduce(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], reducer: Callable, neutral_element: Any, role: Role = None) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#
sum(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], role: Role = None) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Return the sum of array for the members of the entity.

array must have the dimension of the number of persons in the simulation

If role is provided, only the entity member with the given role are taken into account.

Example:

>>> salaries = household.members('salary', '2018-01')  # e.g. [2000, 1500, 0, 0, 0]
>>> household.sum(salaries)
>>> array([3500])
value_from_first_person(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]])[source]#
value_from_person(array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], role: Role, default: Any = 0) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Get the value of array for the person with the unique role role.

array must have the dimension of the number of persons in the simulation

If such a person does not exist, return default instead

The result is a vector which dimension is the number of entities

value_nth_person(n: int, array: Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], default: Any = 0) Union[Buffer, _SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]][source]#

Get the value of array for the person whose position in the entity is n.

Note that this position is arbitrary, and that members are not sorted.

If the nth person does not exist, return default instead.

The result is a vector which dimension is the number of entities.