def check_similarity(self, actual, expected): """ Compare two population dictionaries using contact matrix Assuming the canberra distance should be small """ passed = True checked = False for code in ['H', 'W', 'S']: # check average contacts per age, if difference is small enough to tolerate expected_contacts = utilities.get_average_contact_by_age( expected, self.datadir, code=code) print("expected contacts by age:\n", expected_contacts) np.savetxt(os.path.join( self.pdfDir, f"pop{self.n}_seed_{self.seed}_{code}_average_contact.csv"), expected_contacts, delimiter=",") actual_contacts = utilities.get_average_contact_by_age( actual, self.datadir, code=code) print("actual contacts by age:\n", actual_contacts) max_difference = np.abs(expected_contacts - actual_contacts).max() checked = True print( f"max_difference for contacts {code} in each age bracket:{max_difference}\n" ) if max_difference > 1: for option in ['density', 'frequency']: print(f"\ncheck:{code} with {option}") actual_matrix = sp.calculate_contact_matrix( actual, density_or_frequency=option, setting_code=code) # expected_matrix = sp.calculate_contact_matrix(expected, density_or_frequency=option, setting_code=code) expected_matrix = np.loadtxt(os.path.join( self.expectedDir, f"pop{self.n}_seed_{self.seed}_{code}_{option}_contact_matrix.csv" ), unpack=True, delimiter=",") np.savetxt(os.path.join( self.pdfDir, f"pop{self.n}_seed_{self.seed}_{code}_{option}_contact_matrix.csv" ), expected_matrix, delimiter=",") # calculate Canberra distance # assuming they should round to 0 d = np.abs(expected_matrix - actual_matrix).mean() # d = distance.canberra(actual_matrix.flatten(), expected_matrix.flatten()) print( f"mean absolute difference between actual/expected contact matrix for {code}/{option} is {str(round(d, 3))}" ) if d > 1: passed = passed & False return checked & passed
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_pop_details(self, pop, dir, title_prefix, location, state_location, country_location, decimal=3): os.makedirs(dir, exist_ok=True) 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') 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 type in ['density', 'frequency']: matrix = sp.calculate_contact_matrix(pop, type, setting_code) brackets = sp.get_census_age_brackets(self.datadir, state_location, country_location) ageindex = sp.get_age_by_brackets_dic(brackets) agg_matrix = sp.get_aggregate_matrix(matrix, ageindex) np.savetxt(os.path.join( dir, f"{self.n}_seed_{self.seed}_{setting_code}_{type}_contact_matrix.csv" ), agg_matrix, delimiter=",", fmt=fmt) fig = plot_age_mixing_matrices.test_plot_generated_contact_matrix( setting_code=setting_code, population=pop, title_prefix=" Expected " if self.generateBaseline else " Test ", density_or_frequency=type) # fig.show() fig.savefig( os.path.join( self.figDir, f"{self.n}_seed_{self.seed}_{setting_code}_{type}_contact_matrix.png" ))
location=location, state_location=state_location, country_location=country_location, generate=True, rand_seed=seed, with_school_types=with_school_types, school_mixing_type=school_mixing_type) sc.tic() pop = sp.make_population(**params) sc.toc() for setting_code in ['H', 'S', 'W']: average = utilities.get_average_contact_by_age( pop, datadir=datadir, state_location=state_location, country_location=country_location, setting_code=setting_code, decimal=3) utilities.plot_array( average, datadir=figdir, testprefix=f"{setting_code} {test_prefix} contact by age bracket", expect_label="contacts", names=age_brackets_labels, xlabel_rotation=50) utilities_dist.check_age_distribution(pop, n, datadir=datadir, figdir=figdir,