def compareGraphs(g1, g2):
    
    """#Compares the quantitative properties of two graph. So I can check the coarse graining. """

    
    #Nodes and edges
    print 'Graph1: #(Nodes, Edges) = (' + str(len(g1.nodes())) + ', ' + str(len(g1.edges())) + ')'
    print 'Graph2: #(Nodes, Edges) = (' + str(len(g2.nodes())) + ', ' + str(len(g2.edges())) + ')'

    #Connected Components
    #print '\n#CCs for graph 1: ' + str(len(nx.connected_components(g1)))
    #print '#CCs for graph 2: ' + str(len(nx.connected_components(g2)))
    
    plt.hist([len(i) for i in nx.connected_components(g1)])
    plt.hist([len(i) for i in nx.connected_components(g2)])
    plt.title('Cluster Size')
    plt.xlabel('Cluster Size')
    plt.ylabel('#Cluster')
    show()
    
    #Degree Distribution
    plt.hist(nx.degree_histogram(g1))
    plt.hist(nx.degree_histogram(g2))
    plt.title('Degree Distribution' )
    plt.xlabel('Degree')
    plt.ylabel('#Nodes')
    show()
    
    #Betweeness --- this is by far the most compuationally demanding.
    plt.hist(nx.betweenness_centrality(g1, normalized = False).values())
    plt.hist(nx.betweenness_centrality(g2, normalized = False).values())
    plt.title('Distribution of Betweenness' )
    plt.xlabel('Betweenness')
    plt.ylabel('#Nodes')
    show()        
Exemplo n.º 2
0
def degree_stats(graph_ref_list, graph_pred_list, is_parallel=False):
    ''' Compute the distance between the degree distributions of two unordered sets of graphs.
    Args:
      graph_ref_list, graph_target_list: two lists of networkx graphs to be evaluated
    '''
    sample_ref = []
    sample_pred = []
    # in case an empty graph is generated
    graph_pred_list_remove_empty = [G for G in graph_pred_list if not G.number_of_nodes() == 0]

    prev = datetime.now()
    if is_parallel:
        with concurrent.futures.ProcessPoolExecutor() as executor:
            for deg_hist in executor.map(degree_worker, graph_ref_list):
                sample_ref.append(deg_hist)
        with concurrent.futures.ProcessPoolExecutor() as executor:
            for deg_hist in executor.map(degree_worker, graph_pred_list_remove_empty):
                sample_pred.append(deg_hist)

    else:
        for i in range(len(graph_ref_list)):
            degree_temp = np.array(nx.degree_histogram(graph_ref_list[i]))
            sample_ref.append(degree_temp)
        for i in range(len(graph_pred_list_remove_empty)):
            degree_temp = np.array(nx.degree_histogram(graph_pred_list_remove_empty[i]))
            sample_pred.append(degree_temp)
    print(len(sample_ref),len(sample_pred))
    mmd_dist = mmd.compute_mmd(sample_ref, sample_pred, kernel=mmd.gaussian_emd)
    elapsed = datetime.now() - prev
    if PRINT_TIME:
        print('Time computing degree mmd: ', elapsed)
    return mmd_dist
Exemplo n.º 3
0
def main():
	global g
	global expanded
	set_g()
	degree_dict = get_degree_dict()
	print len(g.nodes())
	label_ordered_list = get_ordered_label_list(degree_dict)
	for item in label_ordered_list:
		print item
		print len(item[1])
	print nx.degree_histogram(g)
	draw(g)
Exemplo n.º 4
0
    def plot_degree_histogram(graph):
        plt.rcParams['text.usetex'] = False
        plt.loglog(nx.degree_histogram(graph),'b-', marker='o')

        n = graph.number_of_nodes()
        er = nx.erdos_renyi_graph(n, 0.05)
        ba = nx.barabasi_albert_graph(n, 5)

        plt.loglog(nx.degree_histogram(er), 'r-', marker='o')
        plt.loglog(nx.degree_histogram(ba), 'k-', marker='o')

        plt.xlabel("degree")
        plt.ylabel("rank")
        plt.savefig('degree_histogram.png', dpi=75, transparent=False)
Exemplo n.º 5
0
def degree_distribution_comparison(g, g1, title, model_name):
    # get the degree histogram
    hist = nx.degree_histogram(g)
    hist1 = nx.degree_histogram(g1)
    plt.plot(range(0, len(hist)), hist, ".", markersize=10, label="Actor Network")
    plt.plot(range(0, len(hist1)), hist1, "r.", markersize=10, label=model_name)
    plt.title(title, fontsize=15)
    plt.xlabel("Degree", fontsize=10, labelpad=-2)
    plt.ylabel("#Nodes", fontsize=10, labelpad=-2)
    plt.tick_params(axis='x', labelsize=9)
    plt.tick_params(axis='y', labelsize=9)
    plt.loglog()
    plt.legend(numpoints=1, loc=0, fontsize="x-small")
    plt.show()
Exemplo n.º 6
0
  def hist_Graph(self): # sauvegarde les données de l'individu ayant la plus basse fitness
    individu_min=self.pop[pop1.f[0][1]]
    distri = nx.degree_histogram(individu_min.graphe)
    liste = []
    #print "distri"+ str(distri)
    for i in range(0,len(distri)) :
      for j in range(0,distri[i]) :
        liste.append(i)
    plt.hist(liste)
    plt.xlabel("degree")
    plt.ylabel("nombre de noeud")
    plt.show()
    plt.savefig("data/histogram.png")
    plt.close()
    
    # Affichage des distributions de clustering
    distrib=individu_min.loi_clustering_best()
    plt.plot(distrib[0],distrib[1],'bo')
    plt.plot(distrib[0],distrib[2],'r')
    plt.xlabel("k")
    plt.ylabel("C(k)")
    plt.show()
    plt.savefig("data/distrib_clique.png")
    plt.close()

     # Affichage des distributions de clustering
    distribp=individu_min.loi_puissance_best()
    plt.loglog()
    plt.plot(distribp[0],distribp[1],'bo')
    plt.plot(distribp[0],distribp[2],'r')
    plt.xlabel("k")
    plt.ylabel("P(k)")
    plt.show()
    plt.savefig("data/distrib_puissance.png")
    plt.close()
Exemplo n.º 7
0
def getGraphStats(threshold=0.5, sector=None, lib="nx"):
	th = re.sub(r'([0-9]*)\.([0-9]*)',r'\1\2',str(threshold))
	if sector != None:
		filename = PROCESSED_FILE_LOC + PREFIX + CRITERIA + "stock_graph_"+lib+"_"+sector+"_th"+th+".xml"
		outFilename = PROCESSED_FILE_LOC + PREFIX + CRITERIA + "stock_graph_stats_"+lib+"_"+sector+"_th"+th
	else:
		filename = PROCESSED_FILE_LOC + PREFIX + CRITERIA + "stock_graph_"+lib+"_th"+th+".xml"
		outFilename = PROCESSED_FILE_LOC + PREFIX + CRITERIA + "stock_graph_stats_"+lib+"_th"+th

	stats = dict()
	if lib == "nx":
		stats = dict()
		g = nx.read_graphml(filename)
		stats['num_nodes'] = g.number_of_nodes()
		stats['num_edges'] = g.number_of_edges()
		#stats['diameter'] = nx.diameter(g)
		stats['clustering_coeff'] = nx.average_clustering(g)
		stats['avg_degree'] = np.average(nx.degree(g).values())
		stats['degree_hist'] = nx.degree_histogram(g)

		y = stats['degree_hist']
		x = range(len(y))
		
	
	f = open(outFilename + ".txt", "wb")
	f.write(str(stats))
	f.close()

	plt.plot(x, y)
	plt.savefig(outFilename + ".png")
	plt.show()

	return stats
def drawNetwork(graph,k):
    pl.figure(k)
    pl.subplot(211)
    #pos = nx.shell_layout(graph) # positions for all nodes
    pos = nx.spring_layout(graph) # positions for all nodes

    # nodes
    nx.draw_networkx_nodes(graph,pos,node_size=200)

    # edges
    nx.draw_networkx_edges(graph,pos,
                        width=3)

    # labels
    # nx.draw_networkx_labels(graph,pos,font_size=20,font_family='sans-serif')

    plt.axis('off')

    # plot degree distribution
    dd = nx.degree_histogram(graph)
    fig = pl.figure(k)
    ax = pl.subplot(212)
    plt.bar(np.arange(len(dd)), dd, width = 0.1)
    plt.axis([0,len(dd),0,max(dd)])
    plt.title("Degree distribution")
    plt.xlabel("degree")
    plt.ylabel("number of nodes")
    #plt.figtext(2, 6, stats, fontsize=15)
    
    plt.draw() # display
    
    return
def calGraph(infile, mode = 1):
	#init Parameter
	inputpath = 'edge_list/'
	outputpath = 'network_output/'
	n = mode
	Data_G = inputpath+infile+'_'+str(n)+'.edgelist'
	
	#init Graph
	G = nx.read_edgelist(Data_G, create_using=nx.DiGraph())
	GU = nx.read_edgelist(Data_G)
	#basci info
	print nx.info(G),'\n', nx.info(GU) 
	average_degree = float(sum(nx.degree(G).values()))/len(G.nodes())
	print 'average degree :', average_degree 
	degree_histogram = nx.degree_histogram(G)
	print 'degree histogram max :', degree_histogram[1]
	desity = nx.density(G)
	print 'desity :', desity

	#Approximation
	#Centrality
	degree_centrality = nx.degree_centrality(G)
	print 'degree centrality top 10 !', sorted_dict(degree_centrality)[:2]
	out_degree_centrality = nx.out_degree_centrality(G)
	print 'out degree centrality top 10 !', sorted_dict(out_degree_centrality)[:2]
Exemplo n.º 10
0
 def test_approx_coloring(self):
     
     KR = kapoor_rizzi()
     
     degree = 40
     partition_n_nodes = 15
     
     # generate random graph
     g = graph_util().generate_random_regular_bipartite(partition_n_nodes, degree)
     
     # generate arbitrary partitions for approximation algo
     arbitrary = [graph_util().generate_random_regular_bipartite(partition_n_nodes, 1) for i in xrange((degree % 2) + 1)]
     
     # algorithm is destructive so save these for later comparisons
     original_nodes = g.nodes()
     arbitrary_edges = reduce(lambda x,y: x+y, (m.edges() for m in arbitrary))
     original_edges = g.edges() + arbitrary_edges
     
     solution = KR.solve(degree, g, arbitrary)
     
     # check the amount of matchings
     self.assertEqual(len(solution), degree + len(arbitrary), "Didn't get enough matchings")
     # check each matching:
     for matching in solution:
         # matching preserves nodes
         self.assertEquals(matching.nodes(), original_nodes)
         # every node has degree 1
         self.assertEquals(nx.degree_histogram(matching), [0, 2*partition_n_nodes])
     # matchings preserve edges
     matching_edges = reduce(lambda x,y: x+y, (m.edges() for m in solution))
     self.assertEquals(sorted(matching_edges), sorted(original_edges))#, "Mismatch between input and output edges")
Exemplo n.º 11
0
def pic(c,n):
    G = nx.Graph()
    if c=='gsm':
        with open('E:/data/degree-distribution/'+c+'/305-7-level'+str(n)+'.txt', 'r') as f:
            for position, line in enumerate(f):
                if line.strip().split(' ')[0]!=line.strip().split(' ')[1]:
                    u= line.strip().split(' ')[0]
                    n=line.strip().split(' ')[1]
                    G.add_edge(u, n)
    else:
        with open('E:/data/degree-distribution/'+c+'/49-24-level'+str(n)+'.txt', 'r') as f:
            for position, line in enumerate(f):
                if line.strip().split(' ')[0]!=line.strip().split(' ')[1]:
                    u= line.strip().split(' ')[0]
                    n=line.strip().split(' ')[1]
                    G.add_edge(u, n)
    degree_hist = nx.degree_histogram(G)
    x = range(len(degree_hist))[1:]
    # print(x)
    y = [float(i+1) / float(sum(degree_hist)) for i in degree_hist[1:]]
    bin_x, bin_y = bining_data(x, y, 8)

    d = []
    pd =[]
    for i in range(0, len(bin_x)):
        d.append(np.median(bin_x[i]))
        pd.append(np.median(bin_y[i]))
    return d, pd
Exemplo n.º 12
0
    def __init__(self, graph, slow_stuff = False):
        graph.info()

        # paolo - 20070919 - computing also the strongly connected
        # components directly on the directed graph. Changing a
        # directed graph into an undirected usually destroys a lot of
        # its structure and meaning. Let see.  while in the published
        # API there is a method
        # strongly_connected_component_subgraphs(graph), I don't have it
        # on my machine (probably I have an older networkx version),
        # so for now I commented the following code.  the method
        # strongly_connected_component_subgraphs(graph) was added on
        # 07/21/07. See https://networkx.lanl.gov/changeset/640 . On
        # my machine I have "python-networkx/feisty uptodate 0.32-2"
        # while on networkx svn there is already version 0.35.1

        if False:
            self.strongconcom_subgraphs = component.strongly_connected_component_subgraphs(graph)
            strongconcom_subgraph_size = map(len, self.strongconcom_subgraphs)     

            print "size of largest strongly connected components:",
            print ", ".join(map(str, strongconcom_subgraph_size[:10])), "..."
            print "%nodes in largest strongly connected component:",
            print 1.0 * strongconcom_subgraph_size[0] / len(graph)
        
        undir_graph = graph.to_undirected()
        self.concom_subgraphs = component.connected_component_subgraphs(undir_graph)
        concom_subgraph_size = map(len, self.concom_subgraphs)
        print "size of largest connected components:",
        print ", ".join(map(str, concom_subgraph_size[:10])), "..."
        
        print "%nodes in largest connected component:",
        print 1.0 * concom_subgraph_size[0] / len(graph)

        #only work on connected graphs, maybe we could run it on the
        #largest strongly connected component.

        #print "diameter:", distance.diameter(G)
        #print "radius:", distance.radius(graph)

        print "density:", networkx.density(graph)

        print "degree histogram:", networkx.degree_histogram(graph)[:15]

        print "average_clustering:", cluster.average_clustering(graph)

        print "transitivity:", cluster.transitivity(graph)

        if slow_stuff:
            #not yet in my networkx revision  -- try try except
            print "number_of_cliques", cliques.number_of_cliques(graph)

            """this returns a dict with the betweenness centrality of
            every node, maybe we want to compute the average
            betweenness centrality but before it is important to
            understand which measures usually are usually reported in
            papers as peculiar for capturing the characteristics and
            structure of a directed graph."""
            print "betweenness_centrality:",
            print centrality.betweenness_centrality(graph)
def P_k(DiG,T,WalkerNum):
	#WalkerNum = 10
	#T = 100000
	final = final_state(DiG,T,WalkerNum)
	degree_Num = {}
	kin = DiG.in_degree().values()
	k = DiG.degree().values()
	degree_Num_k = {}
	degree_histogram,_ = np.histogram(kin,np.arange(0,max(kin)+2,1))
	degree_histogram2 = nx.degree_histogram(DiG)
	#degree_histogram = nx.degree_histogram(DiG)
	for node in final:
		k = DiG.in_degree(node)
		if k in degree_Num:
			degree_Num[k]+=1
		else:
			degree_Num[k] = 1
	for node in final:
		k = DiG.degree(node)
		if k in degree_Num_k:
			degree_Num_k[k]+=1
		else:
			degree_Num_k[k] = 1

	degree_Probability = {}
	for degree in degree_Num:
		degree_Probability[degree] = 1.0/degree_histogram[degree]*degree_Num[degree]/WalkerNum
	degree_Probability_k = {}
	for degree in degree_Num_k:
		degree_Probability_k[degree] = 1.0/degree_histogram2[degree]*degree_Num_k[degree]/WalkerNum
	return degree_Probability, degree_Probability_k
Exemplo n.º 14
0
def degreeAnalysis(G):
    '''
    Analyze the degree distribution
    '''
    print('--------Node Degree Analysis:--------')
    tempY=nx.degree_histogram(G)
    tempX=list(range(len(tempY))) # isolated node's degree = 0
    X=[];Y=[]
    print('Number of Isolated Node: ',tempY[0])
    for i in range(1,len(tempY)):# here I discard all isolated nodes.
        if tempY[i]!=0:
            Y.append(np.log(tempY[i]))
            X.append(np.log(tempX[i]))
    A=np.array([X,np.ones(len(X))])
    k=np.linalg.lstsq(A.T,Y)[0]
    X=np.array(X)
    regressionLine=k[0]*X+k[1]
    print('Slope of regression line: ',k[0])
    #plt.plot(X,Y,'o',X,regressionLine,label='$Y = %dx$'% k[0])
    plt.plot(X,Y,'o')
    plt.plot(X,regressionLine,'r',label='$Y = %fx+%f$'% (k[0], k[1]))
    plt.legend()
    plt.xlabel('log(k)')
    plt.ylabel('logN(k)')
    plt.title('Node degree analysis')
    plt.show()       
Exemplo n.º 15
0
def _compute_rc(G):
    """Returns the rich-club coefficient for each degree in the graph
    `G`.

    `G` is an undirected graph without multiedges.

    Returns a dictionary mapping degree to rich-club coefficient for
    that degree.

    """
    deghist = nx.degree_histogram(G)
    total = sum(deghist)
    # Compute the number of nodes with degree greater than `k`, for each
    # degree `k` (omitting the last entry, which is zero).
    nks = (total - cs for cs in accumulate(deghist) if total - cs > 1)
    # Create a sorted list of pairs of edge endpoint degrees.
    #
    # The list is sorted in reverse order so that we can pop from the
    # right side of the list later, instead of popping from the left
    # side of the list, which would have a linear time cost.
    edge_degrees = sorted((sorted(map(G.degree, e)) for e in G.edges()),
                          reverse=True)
    ek = G.number_of_edges()
    k1, k2 = edge_degrees.pop()
    rc = {}
    for d, nk in enumerate(nks):
        while k1 <= d:
            if len(edge_degrees) == 0:
                ek = 0
                break
            k1, k2 = edge_degrees.pop()
            ek -= 1
        rc[d] = 2 * ek / (nk * (nk - 1))
    return rc
Exemplo n.º 16
0
def pic(c,n):
    G = nx.Graph()
    if c=='gsm':
        with open('E:/data/degree-distribution/'+c+'/305-7-level'+str(n)+'.txt', 'r') as f:
            for position, line in enumerate(f):
                if line.strip().split(' ')[0]!=line.strip().split(' ')[1]:
                    u= line.strip().split(' ')[0]
                    n=line.strip().split(' ')[1]
                    G.add_edge(u, n)
    else:
        with open('E:/data/degree-distribution/'+c+'/49-24-level'+str(n)+'.txt', 'r') as f:
            for position, line in enumerate(f):
                if line.strip().split(' ')[0]!=line.strip().split(' ')[1]:
                    u= line.strip().split(' ')[0]
                    n=line.strip().split(' ')[1]
                    G.add_edge(u, n)
    degree_hist = nx.degree_histogram(G)
    x = range(len(degree_hist))[1:]
    print(degree_hist)

    y = [float(i+1) / float(sum(degree_hist)) for i in degree_hist[1:]]
    plt.loglog(x, y, 'bo', linewidth = 2)
    plt.title('Degree Distribution')
    plt.ylabel('Probability')
    plt.xlabel('Degree')
    return x, y
Exemplo n.º 17
0
def analyse_data(data, threshold=0.66):
    """ perform graph theory analysis on data

    Parameters
    ----------
    data:   dict
        the keys are the names of the datasets
        and the values are dicts that include 'corr' which represents
        the corr matrix from which to derive the graph

    Returns
    -------
    result: dict of graph theory results
        the keys are the names of the datasets
        the values are another dict containing
        'L' - the average shortest path length
        'CC' - the average clustering coefficient
        'DD' - the degree histogram
        'Nodes' - the number of nodes in the graph
        'Edges' - the number of edges in the graph
    """
    result = dict()
    for label, dataset in data.items():
        summary = dict()
        corr = dataset['corr']
        graph, _ = corr_matrix_to_graph(corr, threshold=threshold)
        summary['L'] = nx.average_shortest_path_length(graph)
        summary['CC'] = nx.average_clustering(graph)
        summary['DD'] = nx.degree_histogram(graph)
        summary['Nodes'] = graph.number_of_nodes()
        summary['Edges'] = graph.number_of_edges()
        result[label] = summary

    return result
def analyze_graph(G):
	print nx.info(G)
	degree_freq = nx.degree_histogram(G)
	diameter = nx.diameter(G)
	print "Diameter: {0}".format(diameter)
	triangles = nx.triangles(G)
	triangles_values = sorted(triangles.values(), reverse=True)
	print "Triangles: {0}".format(triangles_values)
Exemplo n.º 19
0
def cum_deg_histogram(G):
    """Cumulative degree histogram."""
    from numpy import cumsum
    from networkx import degree_histogram
    deg_hist = degree_histogram(G)
    deg_hist.reverse()
    cum_deg_hist = list(cumsum(deg_hist))
    cum_deg_hist.reverse()
    return cum_deg_hist
 def degree_distribution(g):
     hist = nx.degree_histogram(g)  # get the degree histogram
     plt.plot(range(0, len(hist)), hist, ".")
     plt.title("Degree Distribution")
     plt.xlabel("Degree")
     plt.ylabel("#Nodes")
     plt.loglog()  #Draw LogLog distribution
     plt.savefig("network_distribution.png")
     plt.show()#show result
Exemplo n.º 21
0
def make_histogram(aGraph):     
    fig = pylab.figure()
    pylab.title(aGraph.name)
    hist = nx.degree_histogram(aGraph)
    pylab.bar(range(len(hist)), hist, align = 'center')
    pylab.xlim((0, len(hist)))
    pylab.xlabel("Degree of node")
    pylab.ylabel("Number of nodes")
    return fig
Exemplo n.º 22
0
def get_graphinfo(graph_fn):
    graph = nx.read_gml(graph_fn)

    cells_inhib = np.array(nx.get_node_attributes(graph, 'inh').values(), 
                         dtype=np.int)
    graph_edges = nx.edges(graph)
    number_of_nodes = nx.number_of_nodes(graph)
    degree_histogram = nx.degree_histogram(graph)
    return cells_inhib, graph_edges,number_of_nodes,degree_histogram
Exemplo n.º 23
0
def degreedist():
	if created == 0:
		print 'No graph created!'
	elif created == 1:
		try:
			dist = [histogram/G.number_of_nodes() for histogram in nx.degree_histogram(G)]
			print dist
			print 'The degree values are the index in the list.'
		except nx.NetworkXError, e:
			print e
Exemplo n.º 24
0
def degree_distrib(graph, n):
    g = nx.read_adjlist(graph)
    dc = nx.degree_histogram(g)
    fileName = open(BASE_PATH+"histogram_"+str(n)+".txt", "w")
    nx.set_node_attributes(g, 'degree_cent', dc)
    #degcent_sorted = sorted(dc.items(), key = operator.itemgetter(1), reverse = True)
    for key, value in enumerate(dc):
        fileName.write("\nThe degree " + str(key) + " frequency is "+ str(value))
        fileName.close()
        return dc
Exemplo n.º 25
0
def isCycle(graph):
	"""	This function takes a networkx graph object as input and returns True if the graph is a cycle
		Note: A graph is a cycle iff degree of every node is two and the graph is connected.
	"""
	if graph:
		degree_hist = nx.degree_histogram(graph) 			# get frequency of degree of all nodes
		if degree_hist[2] == len(graph.nodes()) and nx.is_connected(graph):	# if all nodes have degree = 2 => cycle
			Debuglogger.debug("Is a cycle")
			return True
	Debuglogger.debug("Is not a cycle")
	return False	
Exemplo n.º 26
0
Arquivo: main.py Projeto: zidarsk8/bio
def plotDegreeDistribution(G, log=False):
    deg = nx.degree_histogram(G)
    if log:
        deg = [i+1 for i in deg]
        plt.yscale('log')
        plt.xscale('log')
    l = plt.plot(range(len(deg)), deg)
    plt.xlabel('Degree')
    plt.ylabel('Number of nodes')
    plt.title('Degree distribution of the network')
    plt.show()
Exemplo n.º 27
0
def degree_distribution(g, title):
    # get the degree histogram
    hist = nx.degree_histogram(g)

    plt.plot(range(0, len(hist)), hist, ".", markersize=10)
    plt.title(title, fontsize=14)
    plt.xlabel("Degree", fontsize=10, labelpad=-2)
    plt.ylabel("#Nodes", fontsize=10, labelpad=-2)
    plt.tick_params(axis='x', labelsize=9)
    plt.tick_params(axis='y', labelsize=9)
    plt.loglog()
    plt.show()
def degree_nodes():
    """
    Degree of nodes histogram.
    
    d[0] : list of pathes for the graphs (here v34 experiment)

    """
    for i in d[0][::5]:
        g = nx.read_gpickle(i)
        hist = nx.degree_histogram(g)
        n = nx.number_of_nodes(g)
        np.save('/home/yuliya/codes/lengths/v34/degree' + i[30:-10] + '.npy', np.asarray(hist)/float(n))
Exemplo n.º 29
0
 def make_histogram(self):
     fig = pylab.figure()
     # ax = fig.add_subplot(1,1,1)
     pylab.title(self.G.name)
     hist = nx.degree_histogram(self.G)
     pylab.bar(range(len(hist)), hist, align = 'center')
     pylab.xlim((0, len(hist)))
     # ax.set_xscale('log')
     # ax.set_yscale('log')
     pylab.xlabel("Degree of node")
     pylab.ylabel("Number of nodes")
     return fig
Exemplo n.º 30
0
def process_network(G, namespace):
    print 'Nodes:', len(G)
    print 'Edges:', G.number_of_edges()
    if namespace.clustering_coefficient:
        print 'Clustering Coefficient:', nx.average_clustering(G)
    if namespace.components:
        components = nx.connected_component_subgraphs(G)
        print 'Number of Components:', len(components)
        isles = [c for c in components if len(c) == 1]
        print 'Isles:', len(isles)
        print 'Largest Component Size:', len(components[0])
    else: components = None
    if namespace.cpl:
        if namespace.approximate_cpl:
            average_shortest_path_length = approximate_cpl
        else:
            print 'Using full slow CPL'
            average_shortest_path_length = nx.average_shortest_path_length
        if components is None:
            components = nx.connected_component_subgraphs(G)
        for i, g in enumerate(g for g in components if
                float(len(g))/float(len(G)) >
                namespace.component_size):
            print 'CPL %d: (%f)' % (i, float(len(g))/float(len(G)))
            print average_shortest_path_length(g)
    if namespace.assortativity:
        print 'Assortativity: NOT IMPLEMENTED.'
    if namespace.degree_distribution:
        hst = nx.degree_histogram(G)

        plt.subplot(121)
        plt.xscale('log')
        plt.yscale('log')
        plt.title("Degree Distribution")
        plt.ylabel("Occurrencies")
        plt.xlabel("Degree")
        plt.plot(range(len(hst)), hst, marker='+')

        plt.subplot(122)
        ccdf = pynetsym.mathutil.ccdf(hst)
        plt.xscale('log')
        plt.yscale('log')
        plt.title("CCDF Degree Distribution")
        plt.ylabel("$P(X>x)$")
        plt.xlabel("Degree")
        plt.plot(range(len(ccdf)), ccdf, color='red')

        if namespace.degree_distribution_out is None:
            plt.show()
        else:
            plt.save_fig(namespace.degree_distribution_out)
Exemplo n.º 31
0
# -*- coding: UTF-8 -*-
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

actor_edge = pd.read_csv('network_actor_rankavgbox.csv') #导入演员网络边的csv文件,导入后为Dataframe格式
print(actor_edge.head(3)) #显示表的前3行

weight_edge = []
for _,row in actor_edge.iterrows(): #把边及边的权重加入列表,数据格式为(节点,节点,权重)
    weight_edge.append((row['ActorID_1'],row['ActorID_2'],row['rankaveragebox']))

AW = nx.Graph() #初始化无向图
AW.add_weighted_edges_from(weight_edge) #把带权重边的信息加入无向图中

degree_hist = nx.degree_histogram(AW) #返回图中所有节点的度分布序列

x = range(len(degree_hist)) #生成x轴序列
y = [z / float(sum(degree_hist)) for z in degree_hist] #生产y轴序列,将频次转换为频率
plt.loglog(x,y,color="blue",linewidth=2) #在双对数坐标轴上绘制度分布曲线
plt.title('Degree Distribution Actor ') #图表标题
plt.xlabel('Degree') #x轴标题
plt.ylabel('Probability') #y轴标题
plt.savefig('Degree Distribution Actor.png') #保存图片
plt.show() #显示图表
Exemplo n.º 32
0
for files in ls:
	if files.endswith(".txt"):
		name = files
		edges = gid.loadGraphEdges(name)
		rewire = gid.loadGraphRewire(name)
		n = gid.loadGraphN(name)
		pos = gid.loadGraphPositions(n)

		g = nx.Graph(edges)
		gRew = nx.Graph(rewire)
		l0 = 23.336
		c0 = 0.442651428571

		print "Links Totales: {0}".format(nx.number_of_edges(g))
		print "Links de rewiring: {0}".format(nx.number_of_edges(gRew))
		t = time.clock()
		#print "Camino minimo medio: {0}".format(nx.average_shortest_path_length(g))
		print time.clock() - t
		#print "Clustering medio: {0}".format(nx.average_clustering(g))

		plt.plot(nx.degree_histogram(g),'ro-')
		#nx.draw(g,pos)
		plt.show()

		#nx.draw(g,pos,edge_color = 'r',node_size=50)
		#plt.show()
		#print nx.number_of_edges(gRew)
		#nx.draw(gRew,pos,edge_color = 'g',node_size=50)
		#plt.show()

Exemplo n.º 33
0
    # crawler_list = ['mod']

    for crawler_type in crawler_list:
        for i in range(0, MAX_GEN):
            for ad_p_remove in os.listdir(directory):
                # for ad_p_remove in ["0", "0.01", "0.05", "0.1", "0.2", "0.3", "0.4","0.5"]:
                fname = directory + str(ad_p_remove) + '/' + \
                        dataset + '/' + crawler_type + '/' + \
                        dataset + '_' + str(i+1) + '_1'
                G = _mylib.read_file(fname)
                node_count = G.number_of_nodes()

                if ad_p_remove == "0":
                    com_G = get_community(G, fname)
                    node_G = G.nodes()
                    deg_hist_G = nx.degree_histogram(G)
                    continue
                else:
                    com_adv = get_community(G, fname)
                    node_adv = G.nodes()
                    deg_hist_adv = nx.degree_histogram(G)
                    d, p = stats.ks_2samp(deg_hist_G, deg_hist_adv)

                int_ = len(set(node_G).intersection(set(node_adv)))
                union_ = len(set(node_G).union(set(node_adv)))

                jaccard = 1. * int_ / union_
                # node_cov_size = 1.*len(node_adv) / len(node_G)
                coverage_dist = 1. * (abs(len(node_adv) - len(node_G))) / (
                    abs(len(node_adv)) + abs(len(node_G)))
                node_cov_sim = 1. - coverage_dist
def main():
    # parse the command line
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-m",
        "--markPos",
        help=
        "specifies the seed marker position of the haplotypes used to draw the graph",
        action="store",
        required=True)
    args = parser.parse_args()

    #g = nx.read_weighted_edgelist(os.getcwd() + "lrrkPairsAtSite_" + args.markPos + ".out", delimiter=",", nodetype=int)
    #nx.write_pajek(g, os.getcwd()+args.markPos+".net")
    g = nx.read_weighted_edgelist(
        "/projects/sequence_analysis/vol4/CHAT_simGWAS/Chat1.2Lrrk2Graph/lrrkPairsAtSite_3999.out",
        comments='h',
        delimiter=",",
        nodetype=str)
    nx.write_pajek(
        g,
        "/projects/sequence_analysis/vol4/CHAT_simGWAS/Chat1.2Lrrk2Graph/lrrk_ego/lrrkPairsAtSite_3999.net"
    )
    gc.collect()  #garbage collection
    del g
    g = nx.read_pajek(
        "/projects/sequence_analysis/vol4/CHAT_simGWAS/Chat1.2Lrrk2Graph/lrrkPairsAtSite_8003.net"
    )

    #assign graph attributes
    #g.graph['marker'] = args.markPos
    g.graph['marker'] = '3999'

    #use nx.info() to see whether the network has been loaded correctly
    #display basic information of the network (num of nodes, edges, average degree)
    nx.info(g)

    #calculate degree distribution
    dh = nx.degree_histogram(g)
    #Plot using same method as http://networkx.lanl.gov/examples/drawing/degree_histogram.html
    #pos=nx.spring_layout(g)
    P.figure(figsize=(8, 8))
    P.loglog(dh, 'b-', marker='o')
    P.title("Degree distribution (log-log)")
    P.ylabel("Degree")
    P.xlabel("Frequency")
    #Draw graph in inset
    #P.axes([0.45,0.45,0.45,0.45])
    #P.axis('off')
    #nx.draw_networkx_nodes(g,pos,node_size=10)
    #nx.draw_networkx_edges(g,pos,alpha=0.4)
    P.savefig("8003_degDist.png")
    P.clf()
    #igraph version

    #Generate a sorted list of connected components, largest first.
    [len(c) for c in sorted(nx.connected_components(G), key=len, reverse=True)]

    #Get the largest connected component
    largest_cc = max(nx.connected_components(G), key=len)

    from itertools import combinations
    top_overlap = [list(combinations(c, 2)) for c in components if len(c) > 1]
    top_overlap = [item for sublist in top_overlap for item in sublist]
    #top_overlap
    #[(1, 3), (1, 2), (3, 2)]

    #Caution: the following step is memory intensive
    c = list(nx.k_clique_communities(g, 3))
Exemplo n.º 35
0
    def __init__(self, graph, Time):
        #------------------------ MDP problem Parameters ----------------
        self.isQuad = False
        self.R = None
        # rewards matrix
        self.C = None
        # constant part of the reward
        self.P = None
        self.Time = Time
        # number of time steps
        self.States = None
        # number of states
        self.Actions = None
        # number of actions
        self.constrainedState = None
        self.constrainedUpperBound = None
        #------------ Underlying Network parameters -------------------
        self.G = None
        self.graphPos = None
        # for drawing
        #--------------- LP CVX Parameters ---------------------
        self.yijt = None
        self.lpObj = None
        self.exactPenalty = None
        #----------- LP Constraints--------------------
        self.positivity = None
        self.initialCondition = None
        self.massConservation = None

        #-------exact penalty parameters--------------
        self.epsilon = 0.1
        # for exact penalty
        self.optimalDual = None

        # ---------- choose type of underlying graph---------------------------
        if graph.type is "grid":
            self.G = nx.grid_graph([graph.rowSize, graph.colSize])
            self.G = nx.convert_node_labels_to_integers(self.G)
            self.graphPos = nx.spring_layout(self.G)
            self.States = graph.rowSize * graph.colSize
            self.Actions = 5
            # up, down left, right, stay
            self.P, c = mdp.generateGridMDP(self.States,
                                            self.Actions,
                                            self.G,
                                            test=True)
            self.R = np.zeros((self.States, self.Actions, Time))
            self.constrainedUpperBound = 0.2
            for t in range(Time):
                self.R[:, :, t] = 1.0 * c
        # seattle graph
        elif graph.type is "seattle":
            self.graphPos, self.G = fG.NeighbourGen(False)
            self.States = self.G.number_of_nodes()
            self.Actions = len(nx.degree_histogram(self.G))
            self.P, c = mdp.generateMDP(self.States, self.Actions, self.G)
            self.R = np.zeros((self.States, self.Actions, Time))
            for t in range(Time):
                self.R[:, :, t] = 1.0 * c
        # seattle graph
        elif graph.type is "seattleQuad":
            self.graphPos, self.G, distances = fG.NeighbourGen(False)
            self.States = self.G.number_of_nodes()
            self.Actions = len(nx.degree_histogram(self.G))
            self.P, c, d = mdp.generateQuadMDP(self.States, self.Actions,
                                               self.G, distances)
            self.R = np.zeros((self.States, self.Actions, Time))
            self.C = np.zeros((self.States, self.Actions, Time))
            for t in range(Time):
                self.R[:, :, t] = 1.0 * d
                self.C[:, :, t] = 1.0 * c
Exemplo n.º 36
0
def get_subgraph(V,E,label_filepath,dataset_name,level=1,subgraph_count=5,ignore_deg=None,root_node=None):
    """
    # total_points: total number of data points
    # feature_dm: number of features per datapoint
    # number_of_labels: total number of labels
    # X: feature matrix of dimension total_points * feature_dm
    # Y: list of size total_points. Each element of the list containing labels corresponding to one datapoint
    # V: list of all labels (nodes)
    # E: dict of edge tuple -> weight, eg. {(1, 4): 1, (2, 7): 3}
    """

    # get a dict of label -> textual_label
    label_dict = get_label_dict(label_filepath)

    # an utility function to relabel nodes of upcoming graph with textual label names
    def mapping(v):
        """
        An utility function to relabel nodes of upcoming graph with textual label names
        :param v: label id (int)
        :return: returns the texual label of the node id [v]
        """
        v = int(v)
        if v in label_dict:
            return label_dict[v]
        return str(v)

    # build a unweighted graph of all edges
    g = nx.Graph()
    g.add_edges_from(E.keys())

    # Below section will try to build a smaller subgraph from the actual graph for visualization
    subgraph_lists = []
    for sg in range(subgraph_count):
        if root_node is None:
            # select a random vertex to be the root
            np.random.shuffle(V)
            v = V[0]
        else:
            v = root_node

        # two files to write the graph and label information
        # Remove characters like \, /, <, >, :, *, |, ", ? from file names,
        # windows can not have file name with these characters
        label_info_filepath = 'samples/'+str(dataset_name)+'_Info[{}].txt'.format(str(int(v)) + '-' + remove_special_chars(mapping(v)))
        label_graph_filepath = 'samples/'+str(dataset_name)+'_G[{}].graphml'.format(str(int(v)) + '-' + remove_special_chars(mapping(v)))
        # label_graph_el = 'samples/'+str(dataset_name)+'_E[{}].el'.format(str(int(v)) + '-' + mapping(v)).replace(' ','_')

        print('Label:[' + mapping(v) + ']')
        label_info_file = open(label_info_filepath,'w')
        label_info_file.write('Label:[' + mapping(v) + ']' + "\n")

        # build the subgraph using bfs
        bfs_q = Queue()
        bfs_q.put(v)
        bfs_q.put(0)
        node_check = {}
        ignored = []

        sub_g = nx.Graph()
        lvl = 0
        while not bfs_q.empty() and lvl <= level:
            v = bfs_q.get()
            if v == 0:
                lvl += 1
                bfs_q.put(0)
                continue
            elif node_check.get(v,True):
                node_check[v] = False
                edges = list(g.edges(v))
                # label_info_file.write('\nNumber of edges: ' + str(len(edges)) + ' for node: ' + mapping(v) + '[' +
                # str(v) + ']' + '\n')
                if ignore_deg is not None and len(edges) > ignore_deg:
                    # label_info_file.write('Ignoring: [' + mapping(v) + '] \t\t\t degree: [' + str(len(edges)) + ']\n')
                    ignored.append("Ignoring: deg [" + mapping(v) + "] = [" + str(len(edges)) + "]\n")
                    continue
                for uv_tuple in edges:
                    edge = tuple(sorted(uv_tuple))
                    sub_g.add_edge(edge[0],edge[1],weight=E[edge])
                    bfs_q.put(uv_tuple[1])
            else:
                continue

        # relabel the nodes to reflect textual label
        nx.relabel_nodes(sub_g,mapping,copy=False)
        print('sub_g:',sub_g)

        label_info_file.write(str('\n'))
        # Writing some statistics about the subgraph
        label_info_file.write(str(nx.info(sub_g)) + '\n')
        label_info_file.write('density: ' + str(nx.density(sub_g)) + '\n')
        label_info_file.write('list of the frequency of each degree value [degree_histogram]: ' +
                              str(nx.degree_histogram(sub_g)) + '\n')
        for nodes in ignored:
            label_info_file.write(str(nodes) + '\n')
        # TODO: Add other statistics for better understanding of the subgraph.
        # subg_edgelist = nx.generate_edgelist(sub_g,label_graph_el)
        label_info_file.close()
        nx.write_graphml(sub_g,label_graph_filepath)

        subgraph_lists.append(sub_g)

        print('Graph generated at: ' + label_graph_filepath)

        if root_node:
            print("Root node provided, will generate only one graph file.")
            break

    return subgraph_lists
Exemplo n.º 37
0
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from util import *
import pandas as pd
import scipy.stats as stats

from networkx.utils import py_random_state

G = nx.read_gml('power.gml', None)
degreeFrequencies = np.array(nx.degree_histogram(G))
degrees = [i[1] for i in G.degree]

# print(nx.info(G))
'''
Number of nodes: 4941
Number of edges: 6594
Average degree:   2.6691
'''
'''
density = nx.density(G)                             # 0.0005403026973346214
globalclusteringCoefficient = nx.transitivity(G)    # 0.10315322452860086
localClusteringCoefficients = nx.clustering(G)
aspl = nx.average_shortest_path_length(G)             # 18.989185424445708
'''

# Normal Scale
# plotDegreeDistribution(degreeFrequencies, logScale=False)

# Logoritmic scale
# plotDegreeDistribution(degreeFrequencies, logScale=True)
Exemplo n.º 38
0
#Confirm that the produces networks which follow the Power law by plotting the degree distribution on a log-log scale.
#Producing the divergence duplication model:
G_dupli = duplication_div(1, 0.2, 0.3, 0.5, 1000)
plt.figure()
nx.draw(G_dupli, node_size=10)
plt.savefig("Graph_dupli.png", format="PNG", node_color='blue')

#Producing the divergence duplication model:
G_part = partial_dupli(1, 0.2, 0.3, 1000)
plt.figure()
nx.draw(G_part, node_size=10)
plt.savefig("Graph_part.png", format="PNG", node_color='blue')

#Plotting the Degree Distribution of the 2 graphs
degree_sequence = nx.degree_histogram(G_dupli)
plt.figure()
plt.loglog(degree_sequence, 'b-', marker='o')
plt.title("Degree rank plot of duplication divergence model")
plt.ylabel("degree")
plt.xlabel("rank")
plt.savefig('degree_rank_duplication')

degree_sequence = nx.degree_histogram(G_part)
plt.figure()
plt.loglog(degree_sequence, 'b-', marker='o')
plt.title("Degree rank plot of partial model")
plt.ylabel("degree")
plt.xlabel("rank")
plt.savefig('degree_rank_partial_model')
Exemplo n.º 39
0
        newib = -maxVector(-IB,-maxVector(np.dot(ib,pi) + E,np.zeros(N)))
        delta = abs(ib - newib)
    #     sum = sum + 1
    # print(sum)
    contagion = set(np.where(ib - newib > 0)[0])
    contagion = contagion.difference(fundamental) # 传染违约银行集合
    return newib,fundamental,contagion

global N # 银行个数
N = 80

G = nx.barabasi_albert_graph(N,1)                               # 创建无标度网络
average_clustering = nx.average_clustering(G)                   # 计算平均聚集度
average_degree_connectivity = nx.average_degree_connectivity(G) # 计算平均连接度
degree = G.degree()                                             # 得到每个节点的度
degree_histogram = nx.degree_histogram(G)                       # 网络度分布
g = nx.to_numpy_array(G)                                        # 得到信用关系矩阵(不含借贷方向)
r,R = relation(g,0,0)                                       # 得到信用关系矩阵(含借贷方向)
L = balanceSheet(r,100,2)                                       # 得到借贷规模矩阵
IL = sum(L)                                                     # 银行间贷款
L = L.T
IB = sum(L)                                                     # 银行间借款
p = 0.4                                                         # 杠杆
data = generateData(IL,IB,p)                                    # 得到资产负债表数据
temp = np.array(data < 0)
if True in temp:
    print('error')
shock = abs(np.random.normal(0,0.3,(N)))                        # 给出冲击
ib,fundamental,contagion = clearVector(data,L,shock)            # 得到清算向量与违约银行集合
LGD = 1 - ib / data['银行间借款']                                # 计算违约损失率
LGD_avg = 1 - ib.sum() / data['银行间借款'].sum()                # 计算平均违约损失率
Exemplo n.º 40
0
 def test_degree_histogram(self):
     assert_equal(nx.degree_histogram(self.G), [1, 1, 1, 1, 1])
Exemplo n.º 41
0
 def test_degree_distribution(self):
     for n in range(1, 10):
         G = nx.hypercube_graph(n)
         expected_histogram = [0] * n + [2**n]
         assert nx.degree_histogram(G) == expected_histogram
#!/usr/bin/env python
"""
========================
Expected Degree Sequence
========================

Random graph from given degree sequence.
"""
# Author: Aric Hagberg ([email protected])

#    Copyright (C) 2006-2019 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)
for i, d in enumerate(dh):
    print("%2s (%2s) %s" % (i, d, '*' * d))
Exemplo n.º 43
0
    def degree(self, brain, outfilebase="brain", append=True):
        # node degrees
        outfile = outfilebase + '_degrees_nodes'
        boolVal = self.fileCheck(outfile, append)

        # open file to write to
        if append and boolVal:
            # case where file is appended
            f = open(outfile, "ab")
            nodewriter = DictWriter(f, fieldnames=brain.G.nodes())

        else:
            # case where a new file is created
            f = open(outfile, "wb")
            nodewriter = DictWriter(f, fieldnames=brain.G.nodes())
            # write header based on node names
            headers = dict((n, n) for n in brain.G.nodes())
            nodewriter.writerow(headers)

        # get degrees of each node
        degs = brain.G.degree()
        degstowrite = dict((n, None) for n in brain.G.nodes())
        for node in degs.keys():
            degstowrite[node] = degs[node]
        # write out
        nodewriter.writerow(degstowrite)
        f.close()

        ## ===========================================================================

        ## hub degrees
        if brain.hubs != []:
            outfile = outfilebase + '_degrees_hubs'
            hubidfile = outfilebase + '_degrees_hubs_ids'

            # move old file if necessary
            boolOF = self.fileCheck(outfile, append)
            self.fileCheck(hubidfile, append)

            # open files to write to
            # bit dodgy, should check both files before "ab" opening...
            if append and boolOF:
                f = open(outfile, "ab")
                g = open(hubidfile, "ab")

            else:
                f = open(outfile, "wb")
                g = open(hubidfile, "wb")

            # hubs within largest connected graph component
            deghubs = [hub for hub in brain.hubs if hub in brain.G]

            # write hub identifies to file
            idwriter = DictWriter(f, fieldnames=brain.hubs)
            hubwriter = DictWriter(g, fieldnames=brain.hubs)

            headers = dict((n, n) for n in brain.hubs)
            hubwriter.writerow(headers)

            # empty dictionary to populate with degree data
            degstowrite = dict((n, None) for n in brain.hubs)

            # get degrees of hubs
            try:
                degs = brain.G.degree(deghubs)
                for node in degs.keys():
                    degstowrite[node] = degs[node]
            except:
                # why not check for this specifically beforehand than using try...except?
                print "no hubs in largest connected component"

            # write to file
            idwriter.writerow(degstowrite)
            f.close()
            g.close()

        ## =======================================================================

        ## write degree histogram
        outfileHist = outfilebase + '_degreehistogram'
        histBool = self.fileCheck(outfileHist, append)

        # open file
        if append and histBool:
            f = open(outfileHist, "ab")
        else:
            f = open(outfileHist, "wb")

        # get histogram
        degreeHist = degree_histogram(brain.G)

        # write histogram to file
        histwriter = writer(f)
        histwriter.writerow(degreeHist)
        f.close()
Exemplo n.º 44
0
 def test_degree_histogram(self):
     assert nx.degree_histogram(self.G) == [1, 1, 1, 1, 1]
Exemplo n.º 45
0
      top16_clo_cc, top17_clo_cc, top18_clo_cc, top19_clo_cc, top20_clo_cc)

#%%
# =============================================================================
#  Degree Distribution2
# =============================================================================

# def plot_degree_distribution(G):
#     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)
#     plt.ylabel("Количество")
#     plt.xlabel("Степень или валентность вершины")

# plot_degree_distribution(Gx2)
# plt.title("Распределение степеней (узлов, вершин)(2016.1.1-2019.2.1)",
#           fontdict=font_dict)
# plt.show()

#%%
degree_freq = np.array(nx.degree_histogram(Gx2))
plt.figure(figsize=(12, 8))
plt.stem(degree_freq)
plt.ylabel("Частота (количество узлов)")
plt.xlabel("Степень одного узла")
plt.title("Распределение степеней узла (01.06.2016-31.05.2017)",
          fontdict=font_dict)
plt.show()
Exemplo n.º 46
0
        for title in list(largest_of_G_paper_fine)
    ]

    #绘图
    nx.draw(g,
            pos=nx.random_layout(g),
            node_color='b',
            edge_color='r',
            node_size=10,
            style='solid',
            node_shape='o',
            font_size=20)
    plt.savefig("paper_group.png", dpi=400, bbox_inches='tight')
    plt.show()

    #描述
    len(G_paper_title)
    G_paper_title.number_of_edges()
    nx.degree_histogram(G_paper_fine)

    #转矩阵
    nx.adjacency_matrix(G_paper_title).todense()
    dense = nx.adjacency_matrix(G_paper_title).todense()
    sparse.coo_matrix(dense)

    ##储存中间结果
    path_pkl = 'D:/bigdatahw/dissertation/pre_defence/nod/paper_group1_metrix.pkl'
    paper_dict_file = open(path_pkl, 'wb')
    pkl.dump(dense, paper_dict_file)
    paper_dict_file.close()
Exemplo n.º 47
0
from pylab import *
import networkx as nx
from scipy import stats as st

n = 10000
ba = nx.barabasi_albert_graph(n, 5)
Pk = [float(x) / n for x in nx.degree_histogram(ba)]
domain = range(len(Pk))
ccdf = [sum(Pk[k:]) for k in domain]

logkdata = []
logFdata = []
prevF = ccdf[0]
for k in domain:
    F = ccdf[k]
    if F != prevF:
        logkdata.append(log(k))
        logFdata.append(log(F))
        prevF = F

a, b, r, p, err = st.linregress(logkdata, logFdata)
print 'Estimated CCDF: F(k) =', exp(b), '* k^', a
print 'r =', r
print 'p-value =', p

plot(logkdata, logFdata, 'o')
kmin, kmax = xlim()
plot([kmin, kmax],[a * kmin + b, a * kmax + b]) 
xlabel('log k')
ylabel('log F(k)')
show()
Exemplo n.º 48
0
    nodetype=int)
#G = nx.read_edgelist('/home/larry/programs/Python/networkx/loc-brightkite_edges.txt', create_using=nx.Graph(), nodetype=int)
# nx.write_gml(g, "facebook_combined.gml")

print(nx.info(G))

import matplotlib.pyplot as plt  #导入科学绘图的matplotlib包
import matplotlib as mpl
plt.switch_backend('agg')
mpl.rcParams['agg.path.chunksize'] = 100000

print('*' * 20, '度分布曲线', '*' * 20)

plt.figure()
plt.clf()
degree = nx.degree_histogram(G)  # 返回每个度, 对应的节点个数

degree_num = [i * j for i, j in enumerate(degree)]
print("平均度数", sum(degree_num) / sum(degree))

degree_x = range(len(degree))  #生成x轴序列,从0到最大度
degree_y = [z / sum(degree) for z in degree]  # 每个度的节点个数, 对总节点的占比

plt.scatter(degree_x, degree_y, marker='.')

plt.title("degree distribute")
plt.xlabel("degree")
plt.ylabel("frequency")
plt.savefig("degree distribute")  #显示图表\

print('*' * 20, '点度中心性', '*' * 20)
 def print_basic_graph_info(self):
     print("Info:")
     print(nx.info(self.graph))
     print("\nDegree histogram: " + str(nx.degree_histogram(self.graph)))
     print("Density: " + str(nx.density(self.graph)))
Exemplo n.º 50
0
                partners = tie[2:6]
                for partner in partners: #could have multiple partners
                    if partner != -2 and partner !=-9: #could it have another tie to same partner?
                        form = aggform = consolform = 1
                        if partner < 1000:   #only DBFs                     
#                            if partner > 1000:
#                                cur.execute("SELECT form, aggform, consolform FROM Partners WHERE pid="+str(partner))
#                                parts = cur.fetchall()
#                                form, aggform, consolform = parts[0]
                            if fid != partner: #no self loops
                                g.add_edge(fid, partner, nih=int(nih), form=int(form), aggform=int(aggform), consolform=int(consolform))
                                    
    
            n = nx.number_of_nodes(g)
            e = nx.number_of_edges(g)
            d = nx.degree_histogram(g)
            t = nx.transitivity(g)
            kc = nx.core_number(g)
            nx.set_node_attributes(g,'k_core',kc)
            dc=nx.degree_centrality(g)
            nx.set_node_attributes(g,'dc',dc)
            
#            nihs=nx.get_edge_attributes(g,'nih')
            print "Graph has %d nodes and %d edges and %f transitivity" %(n, e, t)
            
            #grab initial pairs
            pairs = list(nx.preferential_attachment(g))
            pairs += list(nx.preferential_attachment(g,g.edges()))            
            
            yr =  int(str(year)[0:4])
            for pair in pairs:
Exemplo n.º 51
0
 def net_degrees(self):
     #n = self._net.original_graph.to_undirected().degree_histogram() # SAGE compatible
     n = degree_histogram(self._net.original_graph) # networkx compatible
     return sorted([i for i, j in enumerate(n) if j])
Exemplo n.º 52
0
def degree_worker(G):
    return np.array(nx.degree_histogram(G))
Exemplo n.º 53
0
counts = update_cooffenders(crime_dict, offenders)
num_lone = len(list(filter(lambda x: len(counts[x]) == 0, counts)))
#if load_from_file:
#    print(len(offenders))
#    overall = nx.read_gpickle("graph.p")
#    print(len(offenders))
#else:
#    print(len(offenders))
overall = create_cooffending_network(offenders)["overall"]
print(len(offenders))
#nx.write_gpickle(overall,"graph.p")

print("number of nodes:", nx.number_of_nodes(overall))
print("number of lone_wolves:", num_lone)
print("number of edges:", nx.number_of_edges(overall))
deg_freq = nx.degree_histogram(overall)
degrees = np.arange(len(deg_freq))
connected_components = [
    c for c in sorted(nx.connected_components(overall), key=len, reverse=True)
]
largest_component = connected_components[0]
print("number of connected components:", len(connected_components))
print("num of nodes in largest cc:", len(largest_component))
plotDist(degrees, deg_freq, "Number of Degrees", "Frequency of Degrees",
         "Frequency of nodes with degrees")
"""
Problem 5.3
"""


def sort_dict_by_value(data_dict, reverse=False):
Exemplo n.º 54
0
def adaptive_SEIR_simulation(Graph, beta, eta, gamma, a, w, init_exp, init_inf,
                             t_max):

    G = Graph.copy()

    N = nx.number_of_nodes(G)

    # Initialize activation/deletion rates
    alpha = a(0)
    omega = w(0)

    # Get edges/possible edges
    all_possible_edges = [(i, j) for i in nx.nodes(G) for j in nx.nodes(G)
                          if i < j]
    edges = list(nx.edges(G))
    non_edges = list(set(all_possible_edges) - set(edges))

    # Initialize epidemic variables
    times, S, E, I, R = [[0], [N - len(init_exp + init_inf)], [len(init_exp)],
                         [len(init_inf)], [0]]
    infected_nodes = init_inf.copy()
    exposed_nodes = init_exp.copy()
    recovered_nodes = []
    neighbors = []

    #Initialize network variables
    #Get degree distributions
    degrees = nx.degree_histogram(G)

    k = [sum([k * degrees[k] / N for k in range(len(degrees))])]
    k2_k = [sum([k * (k - 1) * degrees[k] / N for k in range(len(degrees))])]
    phi = [nx.transitivity(G)]

    # Get all the neighbors of infected nodes
    for i in infected_nodes:
        temp = list(nx.neighbors(G, i))
        neigh_temp = [x for x in temp if x not in neighbors]
        neighbors = neighbors + neigh_temp
    at_risk_nodes = [
        node for node in neighbors
        if node not in infected_nodes + exposed_nodes + recovered_nodes
    ]

    # Get force of infection
    infectious_neighbors = [0 for i in range(N)]
    for i in at_risk_nodes:
        infectious_neighbors[i] = len(
            list(
                set(infected_nodes).intersection(set(list(nx.neighbors(G,
                                                                       i))))))
    infection_rate = [beta * i for i in infectious_neighbors]

    # Total Rates
    total_activation_rate = alpha * len(non_edges)
    total_deletion_rate = omega * len(edges)
    total_infection_rate = sum(infection_rate)
    total_latency_rate = eta * len(exposed_nodes)
    total_recovery_rate = gamma * len(infected_nodes)
    #
    total_rate = total_infection_rate + total_latency_rate + total_recovery_rate + total_activation_rate + total_deletion_rate
    time = np.random.exponential(1 / total_rate)

    while (time < t_max) and (total_rate > 0):
        r = np.random.uniform(0, total_rate)
        #print("At Risk == Nonzero inf neigh?",len(at_risk_nodes)==len([x for x in infectious_neighbors if x != 0]))
        #Recovery
        if r < total_recovery_rate:
            u = rand.choice(infected_nodes)
            infected_nodes = [inf for inf in infected_nodes if inf != u]
            recovered_nodes.append(u)
            for i in list(
                    set(at_risk_nodes).intersection(set(nx.neighbors(G, u)))):
                if infectious_neighbors[i] == 1:
                    infectious_neighbors[i] = 0
                    at_risk_nodes.remove(i)
                elif infectious_neighbors[i] > 1:
                    infectious_neighbors[i] -= 1
            d_k, d_k2_k, d_T = [0, 0, 0]
        #Become Infectious
        elif total_recovery_rate <= r < total_recovery_rate + total_latency_rate:
            u = rand.choice(exposed_nodes)
            exposed_nodes = [exp for exp in exposed_nodes if exp != u]
            infected_nodes.append(u)
            for i in list(nx.neighbors(G, u)):
                if i not in at_risk_nodes + infected_nodes + exposed_nodes + recovered_nodes:
                    infectious_neighbors[i] = 1
                    at_risk_nodes.append(i)
                elif i in at_risk_nodes:
                    infectious_neighbors[i] += 1
            d_k, d_k2_k, d_T = [0, 0, 0]
        #Exposure
        elif total_recovery_rate + total_latency_rate <= r < total_recovery_rate + total_latency_rate + total_infection_rate:
            #probs = [beta*inf/total_infection_rate for inf in infectious_neighbors if inf != 0]
            #u = np.random.choice(sorted(at_risk_nodes),p=probs)
            probs = [
                beta * infectious_neighbors[at_risk_nodes[i]] /
                total_infection_rate for i in range(len(at_risk_nodes))
            ]
            u = np.random.choice(at_risk_nodes, p=probs)
            at_risk_nodes = [node for node in at_risk_nodes if node != u]
            exposed_nodes.append(u)
            infectious_neighbors[u] = 0
            d_k, d_k2_k, d_T = [0, 0, 0]
        #Link Activation
        elif total_recovery_rate + total_latency_rate + total_infection_rate <= r < total_recovery_rate + total_latency_rate + total_infection_rate + total_activation_rate:
            if len(edges) == N * (N - 1) / 2:
                pass
            else:
                #Get a random non-edge
                node1, node2 = rand.choice(non_edges)
                n1 = nx.degree(G, node1)
                n2 = nx.degree(G, node2)

                #Add the edge
                non_edges.remove((node1, node2))
                edges.append((node1, node2))
                G.add_edge(node1, node2)

                if node1 in infected_nodes:
                    if node2 not in at_risk_nodes + infected_nodes + exposed_nodes + recovered_nodes:
                        at_risk_nodes.append(node2)
                        infectious_neighbors[node2] = 1
                    elif node2 in at_risk_nodes:
                        infectious_neighbors[node2] += 1
                elif node2 in infected_nodes:
                    if node1 not in at_risk_nodes + infected_nodes + exposed_nodes + recovered_nodes:
                        at_risk_nodes.append(node1)
                        infectious_neighbors[node1] = 1
                    elif node1 in at_risk_nodes:
                        infectious_neighbors[node1] += 1
                d_k = 2 / N
                d_k2_k = 2 * (n1 + n2) / N
                d_T = len(
                    set(nx.neighbors(G, node1)) & set(nx.neighbors(G, node2)))
        #Link Removal
        else:
            if len(edges) == 0:
                pass
            else:
                #Random Edge
                node1, node2 = rand.choice(edges)
                n1 = nx.degree(G, node1)
                n2 = nx.degree(G, node2)

                #Remove the edge
                non_edges.append((node1, node2))
                edges.remove((node1, node2))
                G.remove_edge(node1, node2)

                if node1 in at_risk_nodes and node2 in infected_nodes:
                    if infectious_neighbors[node1] == 1:
                        at_risk_nodes.remove(node1)
                        infectious_neighbors[node1] = 0
                    else:
                        infectious_neighbors[node1] -= 1
                elif node2 in at_risk_nodes and node1 in infected_nodes:
                    if infectious_neighbors[node2] == 1:
                        at_risk_nodes.remove(node2)
                        infectious_neighbors[node2] = 0
                    else:
                        infectious_neighbors[node2] -= 1
                d_k = -2 / N
                d_k2_k = (4 - 2 * (n1 + n2)) / N
                d_T = -len(
                    set(nx.neighbors(G, node1)) & set(nx.neighbors(G, node2)))

        #Update Variable Lists
        times.append(time)
        S.append(N - len(infected_nodes + exposed_nodes + recovered_nodes))
        E.append(len(exposed_nodes))
        I.append(len(infected_nodes))
        R.append(len(recovered_nodes))

        k.append(k[-1] + d_k)
        k2_k.append(k2_k[-1] + d_k2_k)
        phi.append(6 * (phi[-1] * N * k2_k[-2] / 6 + d_T) / (N * k2_k[-1]))

        #Update Rates
        alpha = a(time)
        omega = w(time)
        total_activation_rate = alpha * len(non_edges)
        total_deletion_rate = omega * len(edges)
        total_latency_rate = eta * len(exposed_nodes)
        total_recovery_rate = gamma * len(infected_nodes)
        total_infection_rate = beta * sum(infectious_neighbors)
        total_rate = total_infection_rate + total_latency_rate + total_recovery_rate + total_activation_rate + total_deletion_rate

        if total_rate != 0:
            time = time + np.random.exponential(1 / total_rate)
    return [times, S, E, I, R, k, k2_k, phi]
Exemplo n.º 55
0
def create_network(n, m):
    m0 = 20
    G = nx.Graph()

    for ni in range(1, m0 + 1):
        G.add_node(ni)
        for nj in range(1, ni):
            if nj != ni:
                G.add_edge(ni, nj)

    nconnected = m0 + 1
    gsize = G.size(weight = 'weight')
    for ni in range(m0 + 1, n + 1):
        for mi in range(1, m + 1):
            attached = False
            while not attached:
                for nj in range(1, nconnected):
                    if ni != nj and not G.has_edge(ni, nj):
                        p = G.degree(nj, weight = 'weight') / gsize
                        if random.uniform(0, 1) < p:
                            G.add_edge(ni, nj)
                            attached = True
                            gsize += 1
                            break
        nconnected += 1

    nx.draw_networkx(G, node_size = 4, with_labels = False)
    plt.title('n = ' + str(n) + ', m = ' + str(m))
    filename = 'ba_n' + str(n) + '_m' + str(m) + '_net.png'
    plt.savefig(filename)
    # plt.show()
    plt.clf()

    histo = nx.degree_histogram(G)
    total = sum(histo)
    norm_histo = np.divide(histo, total)
    length = len(norm_histo)
    kn = np.arange(length)
    knm = np.add(kn, m)
    plt.plot(kn, norm_histo, 'r-', label = 'empirical')

    exponential = np.empty(length)
    for k in range(0, length):
        exponential[k] = (k + 1) ** (-3)
        
    total = sum(exponential)
    norm_exponential = np.divide(exponential, total)
    
    plt.plot(knm, norm_exponential, 'b-', label = 'exponential(-3)')
    plt.title('n = ' + str(n) + ', m = ' + str(m))
    plt.xlabel('Grado k')
    plt.ylabel('Fracción de nodos')
    plt.legend(loc = 1)
    filename = 'ba_n' + str(n) + '_m' + str(m) + '_dg.png'
    plt.savefig(filename)
    # plt.show()
    plt.clf()

    if n >= 1000:
        plt.plot(kn, norm_histo, 'r-', label = 'empirical')
        plt.plot(knm, norm_exponential, 'b-', label = 'exponential(-3)')
        plt.title('n = ' + str(n) + ', m = ' + str(m) + ' (log-log)')
        plt.xlabel('Grado k')
        plt.ylabel('Fracción de nodos')
        plt.xscale('log')
        plt.yscale('log')
        plt.legend(loc = 3)
        filename = 'ba_n' + str(n) + '_m' + str(m) + '_dg_log_log.png'
        plt.savefig(filename)
        # plt.show()
        plt.clf()

        plt.bar(kn, histo, align='center', label = 'empirical')
        plt.title('n = ' + str(n) + ', m = ' + str(m) + ' (bar-log-log)')
        plt.xlabel('Grado k')
        plt.ylabel('Fracción de nodos')
        plt.xscale('log')
        plt.yscale('log')
        plt.legend(loc = 1)
        filename = 'ba_n' + str(n) + '_m' + str(m) + '_dg_bar_log_log.png'
        plt.savefig(filename)
        # plt.show()
        plt.clf()
        
        filename = 'ba_n' + str(n) + '_m' + str(m) + '_dg.txt'
        file = open(filename, 'w')
        for i in range(length):
            file.write(str(i) + ' ' + str(histo[i]) + '\n')
        file.close()        
    return
Exemplo n.º 56
0
import sys

# connected_watts_strogatz_graph(n, k, p, tries=100, seed=None)
# Returns a connected Watts–Strogatz small-world graph.
# Attempts to generate a connected graph by repeated generation of Watts–Strogatz small-world graphs. An
# exception is raised if the maximum number of tries is exceeded.
# Parameters
# • n (int) – The number of nodes
# • k (int) – Each node is joined with its k nearest neighbors in a ring topology.
# • p (float) – The probability of rewiring each edge
# • tries (int) – Number of attempts to generate a connected graph.
# • seed (integer, random_state, or None (default)) – Indicator of random number generation state.
# Notes
# First create a ring over 𝑛 nodes1. Then each node in the ring is joined to its 𝑘 nearest neighbors (or 𝑘 − 1
# neighbors if 𝑘 is odd). Then shortcuts are created by replacing some edges as follows: for each edge (𝑢, 𝑣)
# in the underlying “𝑛-ring with 𝑘 nearest neighbors” with probability 𝑝 replace it with a new edge (𝑢,𝑤) with
# uniformly random choice of existing node 𝑤. The entire process is repeated until a connected graph results.

# Define number of nodes in the employee graph
numberOfNodes = 2000
G = nx.connected_watts_strogatz_graph(numberOfNodes, 4, 0.250)

#Write an GRAPHML file -- THIS WORKS WITH NETLOGO!!
nx.write_graphml(G, "SmallWorld-2000Nodes-Ver1.graphml")

print("\n")
print(nx.info(G))
print(nx.degree_histogram(G))
print("Clustering Coefficient: " + str(nx.average_clustering(G)))
print("Average Shortest Path length: " +
      str((nx.average_shortest_path_length(G))))
Exemplo n.º 57
0
def entropy1(g):
    if nx.number_of_nodes(g) == 0:
        return 0
    diff_degrees = sum(x > 0 for x in nx.degree_histogram(g)) - 1
    return diff_degrees / nx.number_of_nodes(g)
Exemplo n.º 58
0
import networkx as nx
import matplotlib.pyplot as plt

from functions import bipartite_poisson_generator


# %% Generate bipartite network with two Poisson degree distributions

N = 200 #Number of Individuals
M = 50 #Number of Mixing Locations
lamb1 = 2 #Average Individual Degree (Bipartite Network)

G, G_bipartite = bipartite_poisson_generator(N,M,lamb1)

#Degree Distribution
degree_dist = list(nx.degree_histogram(G)).copy()
degrees = [d for d in range(len(degree_dist)) for i in range(degree_dist[d])]

#####################
# %% Plots


# Figure 3a - Mixing Network

plt.figure(figsize=(12,4))
color = ['#47aee6' for i in range(N)] + ['#123AA1' for i in range(M)]
size = [20 for i in range(N)] + [50 for i in range(M)]
nx.draw(G_bipartite,node_color=color, node_size = size, width=0.2,pos=nx.bipartite_layout(G_bipartite,nx.bipartite.sets(G_bipartite)[1],align='horizontal'))

#plt.savefig("Figure3a.png")
Exemplo n.º 59
0

# chamar a funcao
g = graph_func()
# info of graph
print(nx.info(g))

# mostrar graficamente o grafo
plt.figure(figsize=(10, 10))
plt.title("Graph Network")
nx.draw(g, node_size=250, node_color='red', with_labels=True)
nx.draw_networkx_labels(g)
plt.show()

# degree distribution
degree_distribution = nx.degree_histogram(g)
d_h = pd.DataFrame(degree_distribution)

ax = d_h.plot(kind='bar', legend=False)
plt.title("Degree Distribution")
ax.set_xlabel("Degree")
ax.set_ylabel("Frequency")
plt.show()

# betweenness centrality
bc = nx.betweenness_centrality(g)

print(
    sorted(nx.betweenness_centrality(g).items(),
           key=operator.itemgetter(1),
           reverse=True))
Exemplo n.º 60
0
 def test_degree_distribution(self):
     m, n = 5, 6
     G = nx.grid_2d_graph(m, n)
     expected_histogram = [0, 0, 4, 2 * (m + n) - 8, (m - 2) * (n - 2)]
     assert nx.degree_histogram(G) == expected_histogram