Socio-Technical helper functions

Various helpers for subgroups.

More information can be found about the possible values for subgroup.

class demod.utils.subgroup_handling.Subgroup

Represent a subgroup of the population.

Addition to a dict is that it can be sorted.

Added in v_0.2. Previously dict where used, which should be fine for compatibility in general.

copy()

Returns a new Subgroup instead of dict.

Return type

demod.utils.subgroup_handling.Subgroup

demod.utils.subgroup_handling.sort_subgroup(subgroup)

Sort the key values of a subgroup.

Parameters

subgroup – subgroup to sort.

Returns

sorted_subgroup

demod.utils.subgroup_handling.subgroup_string(subgroup)

Transform a subgroup to a string.

The subgroup is sorted before transformation using sort_subgroup(). If any subgroup value is a list, it is extracted in the string.

Parameters

subgroup – the subgroup to transform

Returns

The string matching the subgroup

Return type

subgroup_string

demod.utils.subgroup_handling.remove_time_attributues(subgroup)

Return a new subgroup withtout the time attributes.

Removes ‘quarter’, ‘weekday’, … TODO: add all the ones that need removed

Parameters

subgroup (demod.utils.subgroup_handling.Subgroup) – The original subgroup

Returns

The subgroup without time attributes.

Return type

demod.utils.subgroup_handling.Subgroup

demod.utils.subgroup_handling.check_weekend_day_format(subgroup)

Check the subgroup is weekday or weekend.

subgroup[‘weekday’] must be [1, 2, 3, 4, 5] or [6, 7] which means either weekdays or weekends.

Parameters

subgroup (demod.utils.subgroup_handling.Subgroup) – the subgroup to be checked

Raises

ValueError – If the subgroup is not weekend or weekday

demod.utils.subgroup_handling.is_weekend(subgroup)

Check that the subgroup correspond to the weekend definition.

Parameters

subgroup (demod.utils.subgroup_handling.Subgroup) – the subgroup to be checked

Returns

whether subgroup is weekend

Return type

bool

demod.utils.subgroup_handling.is_weekday(subgroup)

Check that the subgroup correspond to the weekday definition.

Parameters

subgroup (demod.utils.subgroup_handling.Subgroup) – the subgroup to be checked

Returns

whether subgroup is weekday

Return type

bool

demod.utils.subgroup_handling.subgroup_households_to_persons(hh_subgroups)

Convert households subgroups to person subgroups.

For each household subgroup given as input, return a list of all the persons in this household.

More information can be found about subgroup.

Added in v_0.2.

Warning

This function migth not be suited to any kind of subgroups. It is designed to work for subgroups based on ‘n_residents’ or ‘household_type’. Also this was designed for the GTOU dataset.

Parameters
  • hh_subgroups (Tuple[List[Subgroup], demod.utils.subgroup_handling.Subgroup]) – The households subgroups that need to be converted. If a single subgroup is given, a single list with the person subgroups is returned.

  • data – The dataset to use for the conversion.

Returns

The converted subgroups, as a list of person

subgroups.

Return type

person_subgroups

demod.utils.subgroup_handling.add_time(subgroup, desired_datetime, use_week_ends_days=True, use_week_sat_sun=False, use_7days=False, use_quarters=False)

Add a time component to the subgroup depending on the parameters.

Added in v_0.2.

Parameters
  • use_week_ends_days (bool) – Distinguish the subgroups between weekdays and weekends. Defaults to True.

  • use_week_sat_sun (bool) – Distinguish the subgroups between weekdays, saturday and sunday. Defaults to False.

  • use_7days (bool) – Distinguish the subgroup between the 7 days of the weeks. Defaults to False.

  • use_quarters (bool) – Distinguish the subgroup depending on the quarters of a year. Defaults to False.

  • subgroup (demod.utils.subgroup_handling.Subgroup) –

  • desired_datetime (datetime.datetime) –

Return type

demod.utils.subgroup_handling.Subgroup