Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate Pattern 1 #48

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions seirsplus/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import scipy.integrate


# My first commit July 13 2021
########################################################
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
#@ @#
Expand Down
234 changes: 186 additions & 48 deletions seirsplus/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,220 @@
import matplotlib.pyplot as pyplot


#Aggregate Pattern
class CommunitySetting():

#The class enables to ensure there is a root aggregate to maintain consistency within the dofferent types of network.

def generate_workplace_contact_network(num_cohorts=1, num_nodes_per_cohort=100, num_teams_per_cohort=10,
mean_intracohort_degree=6, pct_contacts_intercohort=0.2,
farz_params={'alpha':5.0, 'gamma':5.0, 'beta':0.5, 'r':1, 'q':0.0, 'phi':10,

def generate_educationalInstitution_contact_network(num_cohorts=1, num_nodes_per_cohort=100, num_teams_per_cohort=10, mean_intracohort_degree=6, pct_contacts_intercohort=0.2, farz_params={'alpha':5.0, 'gamma':5.0, 'beta':0.5, 'r':1, 'q':0.0, 'phi':10,
'b':0, 'epsilon':1e-6, 'directed': False, 'weighted': False},
distancing_scales=[]):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Generate FARZ networks of intra-cohort contacts:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cohortNetworks = []

teams_indices = {}

for i in range(num_cohorts):

numNodes = num_nodes_per_cohort[i] if isinstance(num_nodes_per_cohort, list) else num_nodes_per_cohort
numTeams = num_teams_per_cohort[i] if isinstance(num_teams_per_cohort, list) else num_teams_per_cohort
cohortMeanDegree = mean_intracohort_degree[i] if isinstance(mean_intracohort_degree, list) else mean_intracohort_degree

farz_params.update({'n':numNodes, 'k':numTeams, 'm':cohortMeanDegree})

cohortNetwork, cohortTeamLabels = FARZ.generate(farz_params=farz_params)

cohortNetworks.append(cohortNetwork)

for node, teams in cohortTeamLabels.items():
for team in teams:
try:
teams_indices['c'+str(i)+'-t'+str(team)].append(node)
except KeyError:
teams_indices['c'+str(i)+'-t'+str(team)] = [node]

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Establish inter-cohort contacts:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cohortsAdjMatrices = [networkx.adj_matrix(cohortNetwork) for cohortNetwork in cohortNetworks]

eduAdjMatrix = scipy.sparse.block_diag(cohortsAdjMatrices)
eduInsNetwork = networkx.from_scipy_sparse_matrix(eduAdjMatrix)

N = eduInsNetwork.number_of_nodes()

cohorts_indices = {}
cohortStartIdx = -1
cohortFinalIdx = -1
for c, cohortNetwork in enumerate(cohortNetworks):

cohortStartIdx = cohortFinalIdx + 1
cohortFinalIdx = cohortStartIdx + cohortNetwork.number_of_nodes() - 1
cohorts_indices['c'+str(c)] = list(range(cohortStartIdx, cohortFinalIdx))

for team, indices in teams_indices.items():
if('c'+str(c) in team):
teams_indices[team] = [idx+cohortStartIdx for idx in indices]

for i in list(range(cohortNetwork.number_of_nodes())):
i_intraCohortDegree = cohortNetwork.degree[i]
i_interCohortDegree = int( ((1/(1-pct_contacts_intercohort))*i_intraCohortDegree)-i_intraCohortDegree )
# Add intercohort edges:
if(len(cohortNetworks) > 1):
for d in list(range(i_interCohortDegree)):
j = numpy.random.choice(list(range(0, cohortStartIdx))+list(range(cohortFinalIdx+1, N)))
eduInsNetwork.add_edge(i, j)

return eduInsNetwork, cohorts_indices, teams_indices

# Transportation network added to make sure the information is transferred through the system
def generate_publicTransportation_contact_network(num_buses=1, num_nodes_per_bus=100, num_teams_per_bus=10,
mean_intracohort_degree=6, pct_contacts_intercohort=0.2,
farz_params={'alpha':5.0, 'gamma':5.0, 'beta':0.5, 'r':1, 'q':0.0, 'phi':10,
'b':0, 'epsilon':1e-6, 'directed': False, 'weighted': False},
distancing_scales=[]):

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Generate FARZ networks of intra-cohort contacts:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cohortNetworks = []

teams_indices = {}

for i in range(num_buses):

numNodes = num_nodes_per_bus[i] if isinstance(num_nodes_per_bus, list) else num_nodes_per_bus
numTeams = num_teams_per_bus[i] if isinstance(num_teams_per_bus, list) else num_teams_per_bus
cohortMeanDegree = mean_intracohort_degree[i] if isinstance(mean_intracohort_degree, list) else mean_intracohort_degree

farz_params.update({'n':numNodes, 'k':numTeams, 'm':cohortMeanDegree})

cohortNetwork, cohortTeamLabels = FARZ.generate(farz_params=farz_params)

cohortNetworks.append(cohortNetwork)

for node, teams in cohortTeamLabels.items():
for team in teams:
try:
teams_indices['c'+str(i)+'-t'+str(team)].append(node)
except KeyError:
teams_indices['c'+str(i)+'-t'+str(team)] = [node]

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Establish inter-cohort contacts:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

busesAdjMatrices = [networkx.adj_matrix(cohortNetwork) for cohortNetwork in cohortNetworks]

transportAdjMatrix = scipy.sparse.block_diag(busesAdjMatrices)
transportNetwork = networkx.from_scipy_sparse_matrix(transportAdjMatrix)

N = transportNetwork.number_of_nodes()

cohorts_indices = {}
cohortStartIdx = -1
cohortFinalIdx = -1
for c, cohortNetwork in enumerate(cohortNetworks):

cohortStartIdx = cohortFinalIdx + 1
cohortFinalIdx = cohortStartIdx + cohortNetwork.number_of_nodes() - 1
cohorts_indices['c'+str(c)] = list(range(cohortStartIdx, cohortFinalIdx))

for team, indices in teams_indices.items():
if('c'+str(c) in team):
teams_indices[team] = [idx+cohortStartIdx for idx in indices]

for i in list(range(cohortNetwork.number_of_nodes())):
i_intraCohortDegree = cohortNetwork.degree[i]
i_interCohortDegree = int( ((1/(1-pct_contacts_intercohort))*i_intraCohortDegree)-i_intraCohortDegree )
# Add intercohort edges:
if(len(cohortNetworks) > 1):
for d in list(range(i_interCohortDegree)):
j = numpy.random.choice(list(range(0, cohortStartIdx))+list(range(cohortFinalIdx+1, N)))
workplaceNetwork.add_edge(i, j)

return transportNetwork, cohorts_indices, teams_indices

def generate_workplace_contact_network(num_cohorts=1, num_nodes_per_cohort=100, num_teams_per_cohort=10,
mean_intracohort_degree=6, pct_contacts_intercohort=0.2,
farz_params={'alpha':5.0, 'gamma':5.0, 'beta':0.5, 'r':1, 'q':0.0, 'phi':10,
'b':0, 'epsilon':1e-6, 'directed': False, 'weighted': False},
distancing_scales=[]):

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Generate FARZ networks of intra-cohort contacts:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Generate FARZ networks of intra-cohort contacts:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cohortNetworks = []
cohortNetworks = []

teams_indices = {}
teams_indices = {}

for i in range(num_cohorts):
for i in range(num_cohorts):

numNodes = num_nodes_per_cohort[i] if isinstance(num_nodes_per_cohort, list) else num_nodes_per_cohort
numTeams = num_teams_per_cohort[i] if isinstance(num_teams_per_cohort, list) else num_teams_per_cohort
cohortMeanDegree = mean_intracohort_degree[i] if isinstance(mean_intracohort_degree, list) else mean_intracohort_degree
numNodes = num_nodes_per_cohort[i] if isinstance(num_nodes_per_cohort, list) else num_nodes_per_cohort
numTeams = num_teams_per_cohort[i] if isinstance(num_teams_per_cohort, list) else num_teams_per_cohort
cohortMeanDegree = mean_intracohort_degree[i] if isinstance(mean_intracohort_degree, list) else mean_intracohort_degree

farz_params.update({'n':numNodes, 'k':numTeams, 'm':cohortMeanDegree})
farz_params.update({'n':numNodes, 'k':numTeams, 'm':cohortMeanDegree})

cohortNetwork, cohortTeamLabels = FARZ.generate(farz_params=farz_params)
cohortNetwork, cohortTeamLabels = FARZ.generate(farz_params=farz_params)

cohortNetworks.append(cohortNetwork)
cohortNetworks.append(cohortNetwork)

for node, teams in cohortTeamLabels.items():
for team in teams:
try:
teams_indices['c'+str(i)+'-t'+str(team)].append(node)
except KeyError:
teams_indices['c'+str(i)+'-t'+str(team)] = [node]
for node, teams in cohortTeamLabels.items():
for team in teams:
try:
teams_indices['c'+str(i)+'-t'+str(team)].append(node)
except KeyError:
teams_indices['c'+str(i)+'-t'+str(team)] = [node]

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Establish inter-cohort contacts:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Establish inter-cohort contacts:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cohortsAdjMatrices = [networkx.adj_matrix(cohortNetwork) for cohortNetwork in cohortNetworks]
cohortsAdjMatrices = [networkx.adj_matrix(cohortNetwork) for cohortNetwork in cohortNetworks]

workplaceAdjMatrix = scipy.sparse.block_diag(cohortsAdjMatrices)
workplaceNetwork = networkx.from_scipy_sparse_matrix(workplaceAdjMatrix)
workplaceAdjMatrix = scipy.sparse.block_diag(cohortsAdjMatrices)
workplaceNetwork = networkx.from_scipy_sparse_matrix(workplaceAdjMatrix)

N = workplaceNetwork.number_of_nodes()
N = workplaceNetwork.number_of_nodes()

cohorts_indices = {}
cohortStartIdx = -1
cohortFinalIdx = -1
for c, cohortNetwork in enumerate(cohortNetworks):
cohorts_indices = {}
cohortStartIdx = -1
cohortFinalIdx = -1
for c, cohortNetwork in enumerate(cohortNetworks):

cohortStartIdx = cohortFinalIdx + 1
cohortFinalIdx = cohortStartIdx + cohortNetwork.number_of_nodes() - 1
cohorts_indices['c'+str(c)] = list(range(cohortStartIdx, cohortFinalIdx))
cohortStartIdx = cohortFinalIdx + 1
cohortFinalIdx = cohortStartIdx + cohortNetwork.number_of_nodes() - 1
cohorts_indices['c'+str(c)] = list(range(cohortStartIdx, cohortFinalIdx))

for team, indices in teams_indices.items():
if('c'+str(c) in team):
teams_indices[team] = [idx+cohortStartIdx for idx in indices]
for team, indices in teams_indices.items():
if('c'+str(c) in team):
teams_indices[team] = [idx+cohortStartIdx for idx in indices]

for i in list(range(cohortNetwork.number_of_nodes())):
i_intraCohortDegree = cohortNetwork.degree[i]
i_interCohortDegree = int( ((1/(1-pct_contacts_intercohort))*i_intraCohortDegree)-i_intraCohortDegree )
# Add intercohort edges:
if(len(cohortNetworks) > 1):
for d in list(range(i_interCohortDegree)):
j = numpy.random.choice(list(range(0, cohortStartIdx))+list(range(cohortFinalIdx+1, N)))
workplaceNetwork.add_edge(i, j)
for i in list(range(cohortNetwork.number_of_nodes())):
i_intraCohortDegree = cohortNetwork.degree[i]
i_interCohortDegree = int( ((1/(1-pct_contacts_intercohort))*i_intraCohortDegree)-i_intraCohortDegree )
# Add intercohort edges:
if(len(cohortNetworks) > 1):
for d in list(range(i_interCohortDegree)):
j = numpy.random.choice(list(range(0, cohortStartIdx))+list(range(cohortFinalIdx+1, N)))
workplaceNetwork.add_edge(i, j)

return workplaceNetwork, cohorts_indices, teams_indices
return workplaceNetwork, cohorts_indices, teams_indices


# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


def generate_demographic_contact_network(N, demographic_data, layer_generator='FARZ', layer_info=None, distancing_scales=[], isolation_groups=[], verbose=False):
def generate_demographic_contact_network(N, demographic_data, layer_generator='FARZ', layer_info=None, distancing_scales=[], isolation_groups=[], verbose=False):

graphs = {}

Expand Down