synthpops.base module¶
The module contains frequently-used functions that do not neatly fit into other areas of the code base.
-
synthpops.base.
norm_dic
(dic)¶ Normalize the dictionary
dic
.- Parameters
dic (dict) – A dictionary with numerical values.
- Returns
A normalized dictionary.
-
synthpops.base.
norm_age_group
(age_dic, age_min, age_max)¶ Create a normalized dictionary for the range
age_min
toage_max
, inclusive.- Parameters
age_dic (dict) – A dictionary with numerical values.
age_min (int) – The minimum value of the range for the dictionary.
age_max (int) – The maximum value of the range for the dictionary.
- Returns
A normalized dictionary for keys in the range
age_min
toage_max
, inclusive.
-
synthpops.base.
get_index_by_brackets_dic
(brackets)¶ Create a dictionary mapping each item in the value arrays to the key. For example, if brackets are age brackets, then this function will map each age to the age bracket or bin that it belongs to, so that the resulting dictionary will give by_brackets_dic[age_index] = age bracket of age_index.
- Parameters
brackets (dict) – A dictionary mapping bracket or bin keys to the array of values that belong to each bracket.
- Returns
A dictionary mapping indices to the brackets or bins each index belongs to.
- Return type
dict
-
synthpops.base.
get_age_by_brackets_dic
(age_brackets)¶ Create a dictionary mapping age to the age bracket it falls in.
- Parameters
age_brackets (dict) – A dictionary mapping age bracket keys to age bracket range.
- Returns
A dictionary of age bracket by age.
Example
age_brackets = sp.get_census_age_brackets(sp.datadir,state_location='Washington',country_location='usa') age_by_brackets_dic = sp.get_age_by_brackets_dic(age_brackets)
-
synthpops.base.
get_ids_by_age_dic
(age_by_id_dic)¶ Get lists of IDs that map to each age.
- Parameters
age_by_id_dic (dict) – A dictionary with the age of each individual by their ID.
- Returns
A dictionary listing IDs for each age from a dictionary that maps ID to age.
-
synthpops.base.
count_ages
(popdict)¶ Create an age count from a population dictionary.
- Parameters
popdict (dict) – dictionary defining population
- Returns
Dictionary of the age count of the population.
- Return type
dict
-
synthpops.base.
get_aggregate_ages
(ages, age_by_brackets_dic)¶ Create a dictionary of the count of ages by age brackets.
- Parameters
ages (dict) – A dictionary of age count by single year.
age_by_brackets_dic (dict) – A dictionary mapping age to the age bracket range it falls within.
- Returns
A dictionary of aggregated age count for specified age brackets.
Example
aggregate_age_count = sp.get_aggregate_ages(age_count, age_by_brackets_dic) aggregate_matrix = symmetric_matrix.copy() aggregate_matrix = sp.get_aggregate_matrix(aggregate_matrix, age_by_brackets_dic)
-
synthpops.base.
get_aggregate_matrix
(matrix, age_by_brackets_dic)¶ Aggregate a symmetric matrix to fewer age brackets. Do not use for homogeneous mixing matrix.
- Parameters
matrix (np.ndarray) – A symmetric age contact matrix.
age_by_brackets_dic (dict) – A dictionary mapping age to the age bracket range it falls within.
- Returns
A symmetric contact matrix (
np.ndarray
) aggregated to age brackets.
Example
age_brackets = sp.get_census_age_brackets(sp.datadir,state_location='Washington',country_location='usa') age_by_brackets_dic = sp.get_age_by_brackets_dic(age_brackets) aggregate_age_count = sp.get_aggregate_ages(age_count, age_by_brackets_dic) aggregate_matrix = symmetric_matrix.copy() aggregate_matrix = sp.get_aggregate_matrix(aggregate_matrix, age_by_brackets_dic) asymmetric_matrix = sp.get_asymmetric_matrix(aggregate_matrix, aggregate_age_count)
-
synthpops.base.
get_asymmetric_matrix
(symmetric_matrix, aggregate_ages)¶ Get the contact matrix for the average individual in each age bracket.
- Parameters
symmetric_matrix (np.ndarray) – A symmetric age contact matrix.
aggregate_ages (dict) – A dictionary mapping single year ages to age brackets.
- Returns
A contact matrix (
np.ndarray
) whose elementsM_ij
describe the contact frequency for the average individual in age bracketi
with all possible contacts in age bracketj
.
Example
age_brackets = sp.get_census_age_brackets(sp.datadir,state_location='Washington',country_location='usa') age_by_brackets_dic = sp.get_age_by_brackets_dic(age_brackets) aggregate_age_count = sp.get_aggregate_ages(age_count, age_by_brackets_dic) aggregate_matrix = symmetric_matrix.copy() aggregate_matrix = sp.get_aggregate_matrix(aggregate_matrix, age_by_brackets_dic) asymmetric_matrix = sp.get_asymmetric_matrix(aggregate_matrix, aggregate_age_count)