def make_popdict_with_supplied_ages(datadir, n=2e3, location='seattle_metro', state_location='Washington', country_location='usa', use_demography=True): """ Make a popdict of n people, with ages supplied. """ if use_demography: # get ages from demographic data and supply them to popdict ages = sp.get_age_n(datadir, n=n, location=location, state_location=state_location, country_location=country_location) popdict = sp.make_popdict(n=n, ages=ages, location=location, state_location=state_location, country_location=country_location) else: # supply any ages you want between and this will populate them in the popdict object min_age, max_age = 0, 100 ages = np.random.randint(min_age, max_age + 1, size=n) # supply any distribution you like popdict = sp.make_popdict(n=n, ages=ages) return popdict
def test_make_popdict_generic(n=default_n): sc.heading(f'Making popdict for {n} people') n = int(n) popdict = sp.make_popdict(n=n, use_demography=False) # Non-USA not implemented return popdict
def make_popdict_of_random_people(n=2e3): """ Make a popdict of n people, age and sex assigned randomly, not informed by data. Age range from 0 to 100. """ popdict = sp.make_popdict(n=n) return popdict
def make_random_contacts(n=2e3, location='seattle_metro', state_location='Washington', country_location='usa', average_degree=30, verbose=False): """ Make a popdict of n people, age and sex sampled from Seattle Metro demographics but random contacts. Network created is an Erdos-Renyi network with average degree of 30. """ popdict = sp.make_popdict(n=n, location=location, state_location=state_location, country_location=country_location, use_demography=True) network_distr_args = {'average_degree': average_degree} contacts = sp.make_contacts(popdict, network_distr_args=network_distr_args) if verbose: # print uid and uids of contacts uids = contacts.keys() uids = [uid for uid in uids] for uid in uids: print(uid, contacts[uid]['contacts']['M']) return contacts
def test_webapp_make_contacts_and_show_some_layers(n=default_n, n_contacts_dic=None, state_location='Washington', location='seattle_metro', country_location='usa', sheet_name='United States of America'): sc.heading(f'Making popdict for {n} people') n = int(n) popdict = sp.make_popdict(n=n, state_location=state_location, location=location, country_location=country_location) options_args = dict.fromkeys(['use_age', 'use_social_layers', 'use_age_mixing'], True) contacts = sp.make_contacts(popdict, n_contacts_dic=n_contacts_dic, state_location=state_location, location=location, country_location=country_location, sheet_name=sheet_name, options_args=options_args) uids = contacts.keys() uids = [uid for uid in uids] for n, uid in enumerate(uids): if n > 20: break layers = contacts[uid]['contacts'] print(n, 'uid', uid, 'age', contacts[uid]['age'], 'total contacts', np.sum([len(contacts[uid]['contacts'][k]) for k in layers])) for k in layers: contact_ages = [contacts[c]['age'] for c in contacts[uid]['contacts'][k]] print(k, len(contact_ages), 'contact ages', contact_ages) print() return popdict
def test_webapp_contacts_calls(n=default_n, location='seattle_metro', state_location='Washington', country_location='usa'): n = int(n) popdict = sp.make_popdict(n=n, state_location=state_location, location=location, country_location=country_location) print(popdict) return
def test_make_popdict_generic(n=default_n): sc.heading(f'Making popdict for {n} people') n = int(n) popdict = None # Now this works # with pytest.raises(NotImplementedError): popdict = sp.make_popdict(n=n,use_demography=False) # Non-USA not implemented return popdict
def test_make_contacts(n=default_n): sc.heading(f'Making contacts for {n} people') popdict = popdict = sp.make_popdict(n=n) options_args = dict.fromkeys(['use_age', 'use_sex', 'use_loc', 'use_social_layers'], True) contacts = sp.make_contacts(popdict, options_args=options_args) return contacts
def test_make_popdict_supplied_ages(n=default_n): sc.heading(f'Making "supplied" popdict for {n} people') n = int(n) fixed_age = 40 uids = [str(i) for i in np.arange(n)] ages = fixed_age * np.ones(n) ages[-10:] = fixed_age * 2 # generate sex popdict = sp.make_popdict(uids=uids, ages=ages) return popdict
def make_popdict_of_people(n=2e3, location='seattle_metro', state_location='Washington', country_location='usa'): """ Make a popdict of n people, age and sex sampled from Seattle Metro demographics. """ popdict = sp.make_popdict(n=n, location=location, state_location=state_location, country_location=country_location, use_demography=True) return popdict
def test_make_popdict_supplied_sexes(n=default_n): sc.heading(f'Making "supplied" popdict for {n} people -- skipping for now') n = int(n) fixed_p_sex = 0.4 uids = [str(i) for i in np.arange(n)] sexes = np.random.binomial(1, p=fixed_p_sex, size=n) sexes = None # Skip for now since not working # generate ages country_location = 'usa' popdict = sp.make_popdict(uids=uids, sexes=sexes, country_location=country_location) return popdict
def make_contacts(): # Copied from test_contacts.py weights_dic = {'H': 4.11, 'S': 11.41, 'W': 8.07, 'R': 2.79} weights_dic[ 'R'] = 7 # increase the general community weight because the calibrate weight 2.79 doesn't include contacts from the general community that you don't know but are near! n = 10000 kwargs = dict(weights_dic=weights_dic, use_social_layers=True, directed=False, use_student_weights=True) # Crashes if False popdict = sp.make_popdict(n=n) contacts = sp.make_contacts(popdict, **kwargs) return contacts
def test_make_popdict_supplied(n=default_n): sc.heading(f'Making "supplied" popdict for {n} people') n = int(n) fixed_age = 40 fixed_sex = 1 uids = [i for i in np.arange(n)] ages = fixed_age * np.ones(n) sexes = fixed_sex * np.ones(n) # Simply compile these into a dict popdict = sp.make_popdict(uids=uids, ages=ages, sexes=sexes) assert popdict[uids[0]]['age'] == fixed_age assert popdict[uids[0]]['sex'] == fixed_sex return popdict
def test_make_contacts_generic_from_network_distr_args(Npop=5000): popdict = sp.make_popdict(n=Npop) network_distr_args = {'average_degree': 30, 'directed': False, 'network_type': 'poisson_degree'} contacts = sp.make_contacts_generic(popdict=popdict, network_distr_args=network_distr_args) uids = contacts.keys() uids = [uid for uid in uids] for n, uid in enumerate(uids): if n > 20: break layers = contacts[uid]['contacts'] print('uid', uid, 'age', contacts[uid]['age'], 'total contacts', np.sum([len(contacts[uid]['contacts'][k]) for k in layers])) for k in layers: contact_ages = [contacts[c]['age'] for c in contacts[uid]['contacts'][k]] print(k, len(contact_ages), 'contact ages', contact_ages) print() return contacts
def test_make_contacts_generic(n=default_n): sc.heading(f'Making popdict for {n} people') n = int(n) popdict = sp.make_popdict(n=n,use_demography=False) contacts = sp.make_contacts(popdict) uids = contacts.keys() uids = [uid for uid in uids] for n,uid in enumerate(uids): if n > 20: break layers = contacts[uid]['contacts'] print('uid',uid,'age',contacts[uid]['age'],'total contacts', np.sum([len(contacts[uid]['contacts'][k]) for k in layers])) for k in layers: contact_ages = [contacts[c]['age'] for c in contacts[uid]['contacts'][k]] print(k,len(contact_ages),'contact ages',contact_ages) print() return contacts
def test_make_contacts_and_show_some_layers(n=default_n,n_contacts_dic=None,state_location='Washington',location='seattle_metro',country_location='usa'): sc.heading(f'Make contacts for {int(n)} people and showing some layers') popdict = sp.make_popdict(n=1e3,state_location=state_location,location=location) options_args = dict.fromkeys(['use_age','use_sex','use_loc','use_age_mixing','use_social_layers'], True) contacts = sp.make_contacts(popdict,n_contacts_dic=n_contacts_dic,state_location=state_location,location=location,country_location=country_location,options_args=options_args) uids = contacts.keys() uids = [uid for uid in uids] for n,uid in enumerate(uids): if n > 20: break layers = contacts[uid]['contacts'] print('uid',uid,'age',contacts[uid]['age'],'total contacts', np.sum([len(contacts[uid]['contacts'][k]) for k in layers])) for k in layers: contact_ages = [contacts[c]['age'] for c in contacts[uid]['contacts'][k]] print(k,len(contact_ages),'contact ages',contact_ages) print() return contacts
state_location=state_location, country_location=country_location, options_args=options_args, network_distr_args=network_distr_args) # save to file # file_path = os.path.join(datadir,'demographics','contact_matrices_152_countries',country_location,state_location,'contact_networks') # sp.save_synthpop(file_path,contacts,location) pop = contacts keys = pop.keys() keys = [k for k in keys] sc.tic() pop = sp.make_popdict(n, uids=keys, location=location, state_location=state_location, country_location=country_location, use_demography=True) sc.toc() # print(pop) # print(keys) file_path = os.path.join(datadir, 'demographics', 'contact_matrices_152_countries', country_location, state_location, 'contact_networks') # households_by_uid_path = os.path.join(file_path, location + '_' + str(n) + '_synthetic_households_with_uids.dat') age_by_uid_path = os.path.join(file_path, location + '_' + str(n) + '_age_by_uid.dat') df = pd.read_csv(age_by_uid_path, delimiter=' ', header=None)
def make_contacts_by_social_layers_and_age_mixing( n=2e3, location='seattle_metro', state_location='Washington', country_location='usa', sheet_name='United States of America', verbose=False): """ Make a popdict of n people, age and sex sampled from Seattle Metro demographics. Contacts are created and stored by layer. Layers are : 'H' - households 'S' - schools 'W' - workplaces 'C' - general community Use sheet_name to decide which set of age mixing patterns to sample contact ages from. Age mixing patterns in the population will match this approximately, but contact networks will still be random in the sense that clustering or triangles will not be enforced. For example, an individual's contacts in the household layer ('H') will match what's expected given their age, but their contacts won't have the same contacts as them. This means the model is not directly creating households, schools, or worksplaces, but contacts for each individual similar to those expected in terms of age. Caveat: students/teachers interact at school and workers interact at work, but they won't interact in both. What's the purpose of this without clustering you ask? Compartmental models routinely use age mixing matrices to model the effects of age mixing patterns on infectious disease spreading at the aggregate level. Agent based models require information at the individual level and this allows us to bring some of that age mixing from compartmental models into an agent based modeling framework. """ popdict = sp.make_popdict(n=n, location=location, state_location=state_location, country_location=country_location, use_demography=True) n_contacts_dic = { 'H': 7, 'S': 20, 'W': 20, 'C': 10 } # dict of the average number of contacts per layer options_args = {'use_age_mixing': True, 'use_social_layers': True} contacts = sp.make_contacts(popdict, n_contacts_dic=n_contacts_dic, location=location, state_location=state_location, country_location=country_location, sheet_name=sheet_name, options_args=options_args) if verbose: layers = ['H', 'S', 'W', 'C'] uids = contacts.keys() uids = [uid for uid in uids] for uid in uids: print(uid) for k in layers: print(k, contacts[uid]['contacts'][k]) return contacts
def test_make_popdict(n=default_n): sc.heading(f'Making popdict for {n} people') popdict = sp.make_popdict(n=n) return popdict