refactor contact counting
@@ -624,21 +624,25 @@ def plot_people(people, bins=None, width=1.0, alpha=0.6, fig_args=None, axis_arg
share_ax = None
for w,w_type in enumerate(['total', 'percapita', 'weighted']): # Plot contacts in different ways
for i,lk in enumerate(lkeys):
+ contacts_lk = people.contacts[lk]
+ members_lk = contacts_lk.members
+ n_contacts = len(contacts_lk)
+ n_members = len(members_lk)
if w_type == 'total':
weight = 1
- total_contacts = 2*len(people.contacts[lk]) # x2 since each contact is undirected
+ total_contacts = 2*n_contacts # x2 since each contact is undirected
ylabel = 'Number of contacts'
- participation = len(people.contacts[lk].members)/len(people.contacts[lk]) if len(people.contacts[lk]) else 0 # Proportion of people that have contacts in this layer
+ participation = n_members/len(people) # Proportion of people that have contacts in this layer
title = f'Total contacts for layer "{lk}": {total_contacts:n}\n({participation*100:.0f}% participation)'
elif w_type == 'percapita':
- age_counts_within_layer = np.histogram(people.age[people.contacts[lk].members], edges)[0]
+ age_counts_within_layer = np.histogram(people.age[members_lk], edges)[0]
weight = np.divide(1.0, age_counts_within_layer, where=age_counts_within_layer>0)
- mean_contacts_within_layer = 2 * len(people.contacts[lk]) / len(people.contacts[lk].members) if len(people.contacts[lk]) else 0 # Factor of 2 since edges are bi-directional
+ mean_contacts_within_layer = 2*n_contacts/n_members if n_members else 0 # Factor of 2 since edges are bi-directional
ylabel = 'Per capita number of contacts'
title = f'Mean contacts for layer "{lk}": {mean_contacts_within_layer:0.2f}'
elif w_type == 'weighted':
weight = people.pars['beta_layer'][lk]*people.pars['beta']
- total_weight = np.round(weight*2*len(people.contacts[lk]))
+ total_weight = np.round(weight*2*n_contacts)
ylabel = 'Weighted number of contacts'
title = f'Total weight for layer "{lk}": {total_weight:n}'
