def genGraphEDR(dicProperties): # on définit toutes les grandeurs de base rRho2D = dicProperties["Rho"] rLambda = dicProperties["Lambda"] nNodes = 0 nEdges = 0 rDens = 0.0 if "Nodes" in dicProperties.keys(): nNodes = dicProperties["Nodes"] if "Edges" in dicProperties.keys(): nEdges = dicProperties["Edges"] rDens = nEdges / float(nNodes**2) dicProperties["Density"] = rDens else: rDens = dicProperties["Density"] nEdges = int(np.floor(rDens * nNodes**2)) dicProperties["Edges"] = nEdges else: nEdges = dicProperties["Edges"] rDens = dicProperties["Density"] nNodes = int(np.floor(np.sqrt(nEdges / rDens))) dicProperties["Nodes"] = nNodes rSideLength = np.sqrt(nNodes / rRho2D) # generate the positions of the neurons lstPos = np.array([ np.random.uniform(0, rSideLength, nNodes), np.random.uniform(0, rSideLength, nNodes) ]) lstPos = np.transpose(lstPos) nNumDesiredEdges = int(float(rDens * nNodes**2)) graphEDR, pos = geometric_graph(lstPos, 0) graphEDR.set_directed(True) graphEDR.vertex_properties["pos"] = pos # test edges building on random neurons nEdgesTot = graphEDR.num_edges() while nEdgesTot < nNumDesiredEdges: nTests = int(np.ceil(nNumDesiredEdges - nEdgesTot)) lstVertSrc = np.random.randint(0, nNodes, nTests) lstVertDest = np.random.randint(0, nNodes, nTests) lstPosSrc = lstPos[lstVertSrc] lstPosDest = lstPos[lstVertDest] lstDist = np.linalg.norm(lstPosDest - lstPosSrc, axis=1) lstDist = np.exp(np.divide(lstDist, -rLambda)) lstRand = np.random.uniform(size=nTests) lstCreateEdge = np.greater(lstDist, lstRand) nEdges = np.sum(lstCreateEdge) lstEdges = np.array( [lstVertSrc[lstCreateEdge > 0], lstVertDest[lstCreateEdge > 0]]).astype(int) graphEDR.add_edge_list(np.transpose(lstEdges)) nEdgesTot += nEdges # make graph simple and connected remove_self_loops(graphEDR) remove_parallel_edges(graphEDR) graphEDR.reindex_edges() nNodes = graphEDR.num_vertices() nEdges = graphEDR.num_edges() rDens = nEdges / float(nNodes**2) # generate types rFracInhib = dicProperties["FracInhib"] lstTypesGen = np.random.uniform(0, 1, nEdges) lstTypeLimit = np.full(nEdges, rFracInhib) lstIsExcitatory = np.greater(lstTypesGen, lstTypeLimit) nExc = np.count_nonzero(lstIsExcitatory) lstTypes = 2.0 * lstIsExcitatory - 1.0 epropWeights = graphEDR.new_edge_property( "double", lstTypes) # excitatory (True) or inhibitory (False) graphEDR.edge_properties["weight"] = epropWeights # and weights if dicProperties["Weighted"]: lstWeights = dicGenWeights[dicProperties["Distribution"]]( dicProperties, nEdges, nExc) # generate the weights graphEDR.edge_properties["weight"].a = np.multiply( lstWeights, lstTypes) return graphEDR
I don't think we want to do this? The result would only be an approximation! # # Remove all nodes that are not neighbours to patient zero # new_vxs = state.copy(value_type='bool') # infect_vertex_property(g, new_vxs, vals=[True]) # g.set_vertex_filter(new_vxs) """ return results if __name__ == "__main__": n = 10**5 # Population size # g = price_network(n, directed=False, m=2) # RANDOM GEOMETRIC NETWORK points = np.random.random((n, 2)) * 5 g, pos = geometric_graph(points, 0.05, [(0, 4), (0, 4)]) beta = 0.01 gamma = 0.0476 trials = 100 R0_results = calculate_R0(g, beta, gamma, trials) print(np.mean(R0_results)) print(np.std(R0_results)) plt.hist(R0_results, bins=np.linspace(0, 20, round(trials / 10))) plt.title('mean(R0) = ' + str(np.mean(R0_results)) + ', std(R0) =' + str(round(np.std(R0_results) * 100) / 100)) plt.xlabel('R0') plt.ylabel('Frequency') plt.show()
def genGraphEDR(dicProperties): # on définit toutes les grandeurs de base rRho2D = dicProperties["Rho"] rLambda = dicProperties["Lambda"] nNodes = 0 nEdges = 0 rDens = 0.0 if "Nodes" in dicProperties.keys(): nNodes = dicProperties["Nodes"] if "Edges" in dicProperties.keys(): nEdges = dicProperties["Edges"] rDens = nEdges / float(nNodes**2) dicProperties["Density"] = rDens else: rDens = dicProperties["Density"] nEdges = int(np.floor(rDens*nNodes**2)) dicProperties["Edges"] = nEdges else: nEdges = dicProperties["Edges"] rDens = dicProperties["Density"] nNodes = int(np.floor(np.sqrt(nEdges/rDens))) dicProperties["Nodes"] = nNodes rSideLength = np.sqrt(nNodes/rRho2D) # generate the positions of the neurons lstPos = np.array([np.random.uniform(0,rSideLength,nNodes),np.random.uniform(0,rSideLength,nNodes)]) lstPos = np.transpose(lstPos) nNumDesiredEdges = int(float(rDens*nNodes**2)) graphEDR,pos = geometric_graph(lstPos,0) graphEDR.set_directed(True) graphEDR.vertex_properties["pos"] = pos # test edges building on random neurons nEdgesTot = graphEDR.num_edges() while nEdgesTot < nNumDesiredEdges: nTests = int(np.ceil(nNumDesiredEdges-nEdgesTot)) lstVertSrc = np.random.randint(0,nNodes,nTests) lstVertDest = np.random.randint(0,nNodes,nTests) lstPosSrc = lstPos[lstVertSrc] lstPosDest = lstPos[lstVertDest] lstDist = np.linalg.norm(lstPosDest-lstPosSrc,axis=1) lstDist = np.exp(np.divide(lstDist,-rLambda)) lstRand = np.random.uniform(size=nTests) lstCreateEdge = np.greater(lstDist,lstRand) nEdges = np.sum(lstCreateEdge) lstEdges = np.array([lstVertSrc[lstCreateEdge>0],lstVertDest[lstCreateEdge>0]]).astype(int) graphEDR.add_edge_list(np.transpose(lstEdges)) nEdgesTot += nEdges # make graph simple and connected remove_self_loops(graphEDR) remove_parallel_edges(graphEDR) graphEDR.reindex_edges() nNodes = graphEDR.num_vertices() nEdges = graphEDR.num_edges() rDens = nEdges / float(nNodes**2) # generate types rFracInhib = dicProperties["FracInhib"] lstTypesGen = np.random.uniform(0,1,nEdges) lstTypeLimit = np.full(nEdges,rFracInhib) lstIsExcitatory = np.greater(lstTypesGen,lstTypeLimit) nExc = np.count_nonzero(lstIsExcitatory) lstTypes = 2.0*lstIsExcitatory-1.0 epropWeights = graphEDR.new_edge_property("double",lstTypes) # excitatory (True) or inhibitory (False) graphEDR.edge_properties["weight"] = epropWeights # and weights if dicProperties["Weighted"]: lstWeights = dicGenWeights[dicProperties["Distribution"]](dicProperties,nEdges,nExc) # generate the weights graphEDR.edge_properties["weight"].a = np.multiply(lstWeights,lstTypes) return graphEDR
def gen_edr(dicProperties): np.random.seed() # on définit toutes les grandeurs de base rRho2D = dicProperties["Rho"] rLambda = dicProperties["Lambda"] nNodes = 0 nEdges = 0 rDens = 0.0 if "Nodes" in dicProperties.keys(): nNodes = dicProperties["Nodes"] if "Edges" in dicProperties.keys(): nEdges = dicProperties["Edges"] rDens = nEdges / float(nNodes**2) dicProperties["Density"] = rDens else: rDens = dicProperties["Density"] nEdges = int(np.floor(rDens*nNodes**2)) dicProperties["Edges"] = nEdges else: nEdges = dicProperties["Edges"] rDens = dicProperties["Density"] nNodes = int(np.floor(np.sqrt(nEdges/rDens))) dicProperties["Nodes"] = nNodes rSideLength = np.sqrt(nNodes/rRho2D) rAverageDistance = np.sqrt(2)*rSideLength / 3 # generate the positions of the neurons lstPos = np.array([np.random.uniform(0,rSideLength,nNodes),np.random.uniform(0,rSideLength,nNodes)]) lstPos = np.transpose(lstPos) numDesiredEdges = int(float(rDens*nNodes**2)) graphEDR,pos = geometric_graph(lstPos,0) graphEDR.set_directed(True) graphEDR.vertex_properties["pos"] = pos # test edges building on random neurons nEdgesTot = graphEDR.num_edges() numTest = 0 while nEdgesTot < numDesiredEdges and numTest < n_MAXTESTS: nTests = int(np.minimum(1.1*np.ceil(numDesiredEdges-nEdgesTot)*np.exp(np.divide(rAverageDistance,rLambda)),1e7)) lstVertSrc = np.random.randint(0,nNodes,nTests) lstVertDest = np.random.randint(0,nNodes,nTests) lstDist = np.linalg.norm(lstPos[lstVertDest]-lstPos[lstVertSrc],axis=1) lstDist = np.exp(np.divide(lstDist,-rLambda)) lstCreateEdge = np.random.uniform(size=nTests) lstCreateEdge = np.greater(lstDist,lstCreateEdge) nEdges = np.sum(lstCreateEdge) if nEdges+nEdgesTot > numDesiredEdges: nEdges = numDesiredEdges - nEdgesTot lstVertSrc = lstVertSrc[lstCreateEdge][:nEdges] lstVertDest = lstVertDest[lstCreateEdge][:nEdges] lstEdges = np.array([lstVertSrc,lstVertDest]).astype(int) else: lstEdges = np.array([lstVertSrc[lstCreateEdge],lstVertDest[lstCreateEdge]]).astype(int) graphEDR.add_edge_list(np.transpose(lstEdges)) # make graph simple and connected remove_self_loops(graphEDR) remove_parallel_edges(graphEDR) nEdgesTot = graphEDR.num_edges() numTest += 1 lstIsolatedVert = find_vertex(graphEDR, graphEDR.degree_property_map("total"), 0) graphEDR.remove_vertex(lstIsolatedVert) graphEDR.reindex_edges() nNodes = graphEDR.num_vertices() nEdges = graphEDR.num_edges() rDens = nEdges / float(nNodes**2) # generate types rInhibFrac = dicProperties["InhibFrac"] lstTypesGen = np.random.uniform(0,1,nEdges) lstTypeLimit = np.full(nEdges,rInhibFrac) lstIsExcitatory = np.greater(lstTypesGen,lstTypeLimit) nExc = np.count_nonzero(lstIsExcitatory) epropType = graphEDR.new_edge_property("int",np.multiply(2,lstIsExcitatory)-np.repeat(1,nEdges)) # excitatory (True) or inhibitory (False) graphEDR.edge_properties["type"] = epropType # and weights if dicProperties["Weighted"]: lstWeights = dicGenWeights[dicProperties["Distribution"]](graphEDR,dicProperties,nEdges,nExc) # generate the weights epropW = graphEDR.new_edge_property("double",lstWeights) # crée la propriété pour stocker les poids graphEDR.edge_properties["weight"] = epropW return graphEDR