def get_pop_details(self, pop, dir, title_prefix, location, state_location, country_location, decimal=3): os.makedirs(dir, exist_ok=True) # want default age brackets and to see that they line up with the average contacts by age bracket created age_brackets = spdd.get_census_age_brackets(self.datadir, location, state_location, country_location) age_brackets_labels = [ str(age_brackets[b][0]) + '-' + str(age_brackets[b][-1]) for b in sorted(age_brackets.keys()) ] for setting_code in ['H', 'W', 'S']: average_contacts = utilities.get_average_contact_by_age( pop, self.datadir, setting_code=setting_code, decimal=decimal) fmt = f'%.{str(decimal)}f' # print(f"expected contacts by age for {code}:\n", average_contacts) utilities.plot_array( average_contacts, datadir=self.figDir, testprefix= f"{self.n}_seed_{self.seed}_{setting_code}_average_contacts", expect_label='Expected' if self.generateBaseline else 'Test', names=age_brackets_labels, xlabel_rotation=50) sc.savejson(os.path.join( dir, f"{self.n}_seed_{self.seed}_{setting_code}_average_contact.json" ), dict(enumerate(average_contacts.tolist())), indent=2) for method in ['density', 'frequency']: matrix = sp.calculate_contact_matrix(pop, method, setting_code) brackets = spdd.get_census_age_brackets( self.datadir, location, state_location, country_location) ageindex = spb.get_age_by_brackets_dic(brackets) agg_matrix = spb.get_aggregate_matrix(matrix, ageindex) textfile = os.path.join( dir, f"{self.n}_seed_{self.seed}_{setting_code}_{method}_contact_matrix.csv" ) np.savetxt(textfile, agg_matrix, delimiter=",", fmt=fmt) fig = sp.plot_contacts(pop, setting_code=setting_code, density_or_frequency=method, do_show=False) fig.savefig( os.path.join( self.figDir, f"{self.n}_seed_{self.seed}_{setting_code}_{method}_contact_matrix.png" ))
def get_average_contact_by_age(pop, datadir, location="seattle_metro", state_location="Washington", country_location="usa", setting_code="H", decimal=3): """ Helper method to get average contacts by age brackets Args: pop : population dictionary datadir : data directory to look up reference data state_location : state location country_location : country location setting_code : contact layer code, can be "H", "W", "S" decimal : digits for rounding, default to 3 Returns: numpy.ndarray: A numpy array with average contacts by age brackets. """ brackets = spdd.get_census_age_brackets(datadir, location, state_location, country_location) ageindex = spb.get_age_by_brackets_dic(brackets) total = np.zeros(len(brackets)) contacts = np.zeros(len(brackets)) for p in pop.values(): total[ageindex[p["age"]]] += 1 contacts[ageindex[p["age"]]] += len(p["contacts"][setting_code]) average = np.round(np.divide(contacts, total), decimals=decimal) return average
def test_seattle_age_brackets(self): """ Test for method get_census_age_brackets and get_age_by_brackets. It calls helper method verify_age_bracket_dictionary_correct for verification. Returns: None """ self.is_debugging = False age_brackets = spdd.get_census_age_brackets( datadir=sp.settings.datadir, state_location="Washington", country_location="usa", use_default=False ) age_brackets_json = {} for k in age_brackets: age_brackets_json[k] = age_brackets[k].tolist() if self.is_debugging: with open(f"DEBUG_{self._testMethodName}_age_brackets.json", "w") as outfile: json.dump(age_brackets_json, outfile, indent=4) age_by_brackets = sp.get_age_by_brackets( age_brackets=age_brackets ) self.verify_age_bracket_dictionary_correct(age_by_brackets)
def get_pop_contact_matrix(pop, layer, method): """ Args: pop (pop object) : population, either synthpops.pop.Pop, covasim.people.People, or dict layer (str) : name of the physical contact layer: H for households, S for schools, W for workplaces, C for community or other method: (str) : density or frequency Returns: array: contact matrix """ age_brackets = spdd.get_census_age_brackets(**pop.loc_pars) matrix = sp.calculate_contact_matrix(pop.popdict, method, layer) ageindex = spb.get_age_by_brackets(age_brackets) agg_matrix = spb.get_aggregate_matrix(matrix, ageindex) return agg_matrix
def test_seattle_age_brackets(self): self.is_debugging = False age_brackets = spdd.get_census_age_brackets( datadir=sp.datadir, state_location="Washington", country_location="usa", use_default=False) age_brackets_json = {} for k in age_brackets: age_brackets_json[k] = age_brackets[k].tolist() if self.is_debugging: with open(f"DEBUG_{self._testMethodName}_age_brackets.json", "w") as outfile: json.dump(age_brackets_json, outfile, indent=4) age_by_brackets_dic = sphh.get_age_by_brackets_dic( age_brackets=age_brackets) self.verify_age_bracket_dictionary_correct(age_by_brackets_dic)
def get_pop_average_contacts_by_brackets(pop, layer, decimal=3): """ get population contact counts by age brackets and save the results as json files and plots Args: pop (pop object) : population, either synthpops.pop.Pop, covasim.people.People, or dict layer (str) : name of the physical contact layer: H for households, S for schools, W for workplaces, C for community or other decimal (int) : rounding precision for average contact calculation Returns: dict: a dictionary where index is the age bracket and value is the percentage of contact """ # want default age brackets and to see that they line up with the average contacts by age bracket created age_brackets = spdd.get_census_age_brackets(**pop.loc_pars) average_contacts = [] for k in age_brackets: degree_df = sp.count_layer_degree(pop, layer=layer, ages=age_brackets[k]) people_in_ages = sp.filter_people(pop, ages=age_brackets[k]) average_contacts.append( 0 if len(degree_df) == 0 else np.round(degree_df.sum(axis=0)['degree'] / len(people_in_ages), decimals=decimal)) return dict(enumerate(average_contacts))
def process_long_term_care_facility_rates_by_age(datadir, state_location, country_location): """ Process the National Long Term Care Providers state data tables from 2016 to get the estimated user rates by age. Args: datadir (string) : file path to the data directory state_location (string) : name of the state country_location (string) : name of the country the state is in Returns: dict: A dictionary with the estimated rates of Long Term Care Facility usage by age for the state in 2016. """ year = 2016 # use 2016 data to match the year of data for the LTCF state data table acs_period = 1 # using ACS 1 year estimates part = 2 # which part of the LTCF state data table to use ltcf_df = data_distributions_legacy.get_usa_long_term_care_facility_data( datadir, state_location, country_location, part=part) ltcf_age_bracket_keys = [ 'Under 65', '65–74', '75–84', '85 and over' ] # age brackets as defined by National LTCF Providers survey facility_keys = [ 'Nursing home', 'Residential care community', # 'Hospice', # Hospice users are often cared for in their own homes so it may not be appropriate to include them in facility resident numbers. ] facility_users = {} # 2016 state wide age breakdown of LTCF users facility_user_age_percentage = {} for fk in facility_keys: facility_users[fk] = {} facility_user_age_percentage[fk] = {} facility_users[fk]['Total'] = int(ltcf_df[ ltcf_df.iloc[:, 0] == 'Number of users2, 5'][fk].values[0].replace( ',', '')) for ab in ltcf_age_bracket_keys: facility_user_age_percentage[fk][ab] = float( ltcf_df[ltcf_df.iloc[:, 0] == ab][fk].values[0].replace( ',', '')) / 100. # 2016 state wide total LTCF users by facility type and age brackets for fk in facility_keys: for ab in ltcf_age_bracket_keys: facility_users[fk][ab] = facility_users[fk][ 'Total'] * facility_user_age_percentage[fk][ab] # map the LTCF users age groups to the census age groups age_group_mapping = { 'Under 65': '60-64', '65–74': '65-74', '75–84': '75-84', '85 and over': '85-100' } age_brackets = spdata.get_census_age_brackets( datadir, state_location=state_location, country_location=country_location, nbrackets=18) age_by_brackets_dic = spb.get_age_by_brackets_dic(age_brackets) brackets_in_range_mapping = {} for ab in ltcf_age_bracket_keys: sa, ea = age_group_mapping[ab].split('-') sa, ea = int(sa), int(ea) age_range = np.arange(sa, ea + 1) brackets_in_range = set() for a in age_range: b = age_by_brackets_dic[a] brackets_in_range.add(b) brackets_in_range_mapping[ab] = sorted(brackets_in_range.copy()) # 2016 state population size population_size = process_us_census_population_size( datadir, state_location, state_location, country_location, year, acs_period) # 2016 state age distribution (18 age brackets) age_bracket_distr, age_brackets = process_us_census_age_counts( datadir, state_location, state_location, country_location, year, acs_period) age_bracket_distr = spb.norm_dic(age_bracket_distr) age_bracket_count = {} for b in sorted(age_bracket_distr.keys()): age_bracket_count[b] = population_size * age_bracket_distr[b] # 2016 state age count for the LTCF data defined age brackets state_older_age_counts = {} for ab in ltcf_age_bracket_keys: state_older_age_counts[ab] = 0 for b in brackets_in_range_mapping[ab]: state_older_age_counts[ ab] += age_bracket_distr[b] * population_size # calculate the percent using facilities by the LTCF data age brackets ltcf_rates_by_age = dict.fromkeys(np.arange(101), 0) for ab in ltcf_age_bracket_keys: ltcf_rates_by_age[ab] = np.sum( [facility_users[fk][ab] for fk in facility_keys]) / state_older_age_counts[ab] for b in brackets_in_range_mapping[ab]: for a in age_brackets[b]: ltcf_rates_by_age[a] = ltcf_rates_by_age[ab].copy() for ab in ltcf_age_bracket_keys: ltcf_rates_by_age.pop(ab, None) return ltcf_rates_by_age