def generate_network(n_nodes, mean_inters, std_dev, n_sick): # Create a graph with selected attributes n_ties = pd.Series(map(int, np.round(np.random.normal(mean_inters, std_dev, n_nodes)))) # Who distances themselves? Make them safer distancers = random.sample(list(n_ties.index), round(prop_social_distancing * n_nodes)) n_ties[distancers] = n_ties[distancers] * effectiveness_social_distancing # If you isolate, you dont get sick and dont get others sick isolaters = random.sample(list(n_ties.index), round(prop_self_isolating * n_nodes)) n_ties[isolaters] = 0 G = expected_degree_graph(n_ties, selfloops=False) # Decide who is sick nx.set_node_attributes(G, 0, 'COVID-19') nx.set_node_attributes(G, 0, 'Time Sick') nx.set_node_attributes(G, 0, 'Symptomatic') nx.set_node_attributes(G, 0, 'Time Showing') sick_nodes = random.sample(list(G.nodes), n_sick) for node in sick_nodes: G.nodes[node]['COVID-19'] = 1 return(G, sick_nodes)
""" ======================== Expected Degree Sequence ======================== Random graph from given degree sequence. """ import networkx as nx from networkx.generators.degree_seq import expected_degree_graph # make a random graph of 500 nodes with expected degrees of 50 n = 500 # n nodes p = 0.1 w = [p * n for i in range(n)] # w = p*n for all nodes G = expected_degree_graph(w) # configuration model print("Degree histogram") print("degree (#nodes) ****") dh = nx.degree_histogram(G) for i, d in enumerate(dh): print(f"{i:2} ({d:2}) {'*'*d}")
======================== Expected Degree Sequence ======================== Random graph from given degree sequence. """ # Author: Aric Hagberg ([email protected]) # Copyright (C) 2006-2018 by # Aric Hagberg <*****@*****.**> # Dan Schult <*****@*****.**> # Pieter Swart <*****@*****.**> # All rights reserved. # BSD license. import networkx as nx from networkx.generators.degree_seq import expected_degree_graph # make a random graph of 500 nodes with expected degrees of 50 n = 500 # n nodes p = 0.1 w = [p * n for i in range(n)] # w = p*n for all nodes G = expected_degree_graph(w) # configuration model print("Degree histogram") print("degree (#nodes) ****") dh = nx.degree_histogram(G) low = min(nx.degree(G)) for i in range(low, len(dh)): bar = ''.join(dh[i] * ['*']) print("%2s (%2s) %s" % (i, dh[i], bar))
def network_creation (path,t): #t number of nodes lista_attributi = ['gender', 'age', 'state'] w = [] p_df= covid_statistics(path+"covid_gender_age.csv") p_df1 = p_df[p_df.columns[-2:]] p_df1 = p_df1.replace(p_df1, 0) diz = p_df1.to_dict('index') df_con = pd.read_csv(path+"contacts_agegroups.csv", index_col=0, names=['contacts'], header=None) df_con = round(df_con.sort_values(by=['contacts'])) df_con.rename(index={'70-100': '70-105'}, inplace=True) print('The number of daily contat for each age group is:\n',df_con) table= gen_age(path+"Italy-2019.csv") #print(diz) nodi = {} # final dictionary will contain: node number: [sex,age,State] ind= -1 for age in (list(table.index.values)): e = re.findall(r'\d+', age) età = randrange(int(e[0]), int(e[1])) for sex in (list(table.columns)): perc = table.loc[age][sex] # this is the fraction (percentage) of population that will have a specific sex and age # rows= age , col= sex n = int(round(perc * t / 100)) for i in range(len(df_con)): N=df_con.index[i].split('-') if int(N[0]) <= età <= int(N[1]): w1 = [df_con.iloc[i][0] for k in range(n)] for el in w1: ind=ind+1 nodi[ind]=[sex,età, 'S'] for ages in diz: ages1=ages.split('-') if int(ages1[0]) <=età<= int(ages1[1]): if diz[ages][sex]==0: diz[ages][sex]=[ind] else: diz[ages][sex].append(ind) w.extend(w1) #HERE WE CREATED W THAT IS A LIST CONTAINING THE NUMBER OF CONTACTS FOR EACH NODE G = expected_degree_graph(w) # configuration model print("Degree histogram") # dh = nx.degree_histogram(G) #print(dh) #print("degree (#nodes) ****") #for i, d in enumerate(dh): # print(f"{i:2} ({d:2}) {'*'*d}") degree_sequence = sorted([d for n, d in G.degree()], reverse=True) # degree sequence degreeCount = collections.Counter(degree_sequence) deg, cnt = zip(*degreeCount.items()) #fig, ax = plt.subplots() #plt.bar(deg, cnt, width=0.80, color="b") #plt.title("Degree Histogram") #plt.ylabel("Count") #plt.xlabel("Degree") #ax.set_xticks([d + 0.4 for d in deg]) #ax.set_xticklabels(deg) #plt.show() #HERE WE ASSIGN ALL THE ATTRIBUTES (AGE, GENDER, AND STATE) FOR EACH NODE OF THE NETWORK for key in nodi: for j in range(len(lista_attributi)): G.nodes[int(key)][lista_attributi[j]] = nodi[key][j] # per ogni attributo della lista, prende il valore dal dizionario return(G,diz,p_df)