def write_distance_info(G, report_file):
    report_file.write("===DISTANCE_INFO_STRONGLY_CONNECTED===\n")
    if nx.is_strongly_connected(G):
        report_file.write("Center: {}\n".format(nx.center(G)))
        report_file.write("Diameter: {}\n".format(nx.diameter(G)))
        report_file.write("Periphery: {}\n".format(nx.periphery(G)))
        report_file.write("Radius: {}\n".format(nx.radius(G)))
    else:
        report_file.write("Center: +INF\n")
        report_file.write("Diameter: +INF\n")
        report_file.write("Periphery: +INF\n")
        report_file.write("Radius: +INF\n")

    report_file.write("===DISTANCE_INFO_WEAKLY_CONNECTED===\n")
    if nx.is_weakly_connected(G):
        undirected_G = nx.to_undirected(G)
        report_file.write("Center: {}\n".format(nx.center(undirected_G)))
        report_file.write("Diameter: {}\n".format(nx.diameter(undirected_G)))
        report_file.write("Periphery: {}\n".format(nx.periphery(undirected_G)))
        report_file.write("Radius: {}\n".format(nx.radius(undirected_G)))
    else:
        report_file.write("Center: +INF\n")
        report_file.write("Diameter: +INF\n")
        report_file.write("Periphery: +INF\n")
        report_file.write("Radius: +INF\n")
    def print_graph_features(self, graph):
        res = {}
        try:
            print('diameter: ', nx.diameter(graph))
            print('eccentricity: ', nx.eccentricity(graph))
            print('center: ', nx.center(graph))
            print('periphery: ', nx.periphery(graph))
            res['connected'] = True
        except Exception as e:
            print('Graph not connected')
            res['connected'] = False

        res['density'] = '{:.6f}'.format(nx.density(graph))
        res['Avg_degree'] = '{:.6f}'.format(sum([i[1] for i in nx.degree(graph)]) / len(nx.degree(graph)))
        res['Avg_weight'] = '{:.6f}'.format(sum([graph[edge[0]][edge[1]]['weight'] for edge in graph.edges]) / len(
            [graph[edge[0]][edge[1]]['weight'] for edge in graph.edges]))
        res['edges'] = len(graph.edges)
        res['nodes'] = len(graph.nodes)
        res['self_loops'] = len(list(nx.nodes_with_selfloops(graph)))
        res['edge_to_node_ratio'] = '{:.6f}'.format(len(graph.nodes) / len(graph.edges))
        res['negative_edges'] = nx.is_negatively_weighted(graph)
        print(algo.max_clique(graph))
        # print('density: ', res['density'])
        # print('Average degree: ', res['Avg_degree'])
        # print('Average weight: ', res['Avg_weight'])
        # print('edges: ', len(graph.edges))
        # print('Nodes: ', len(graph.nodes))
        # print('self loops: ', res['self_loops'])
        # print('edges to nodes ratio: ', res['edge_to_node_ratio'])
        # print('negative edges: ', res['negative_edges'])
        # nodes = [node for node in graph.nodes]
        # print(nodes)

        return res
Exemple #3
0
def alp_planar(g, subgraphs):
    if not g:
        raise ValueError("Cannot specify graph as null.")
    elif not subgraphs:
        raise ValueError("Cannot specify subgraphs as null.")

    ls = []
    import operator
    for g_p in subgraphs.values():
        if g.is_directed():
            subg = list(nx.strongly_connected_component_subgraphs(g.subgraph(g_p)))[0]
        else:
            subg = list(nx.connected_component_subgraphs(g.subgraph(g_p), False))[0]

        ls.append(nx.periphery(subg)[0])
        '''e = nx.eccentricity(subg)
        #get node with highest eccentricity
        if e:
            ls.append(max(e.iteritems(), key=operator.itemgetter(1))[0])
        else:
            print "Could not find eccentricity"'''
        '''max_e = 0
        max_node = 0
        #identify node with max eccentricity
        for node in g_p:
             if e[node] > max_e:
                max_e = e[node]
                max_node = node

        #add to set
        ls.append(max_node)'''
    return ls
Exemple #4
0
    def basic_stats(self):
        #not decided on what level to deal with this yet:
        #either return error un not dealing with unconnected files,
        #or making it deal with unconnected files: the latter.
        #How about with dealing with each independently.
        #    if not nx.is_connected(g):
        #        conl= nx.connected_components(g)
        #        for n in conl:
        #            turn n into graph if it isnt
        #            calculate ec, per, cnt
        #            how and when to visualise the subgraphs?
        #            iterate to next n

        if nx.is_connected(self.nx_graph):
            ec = nx.eccentricity(self.nx_graph) 
        else:
            ec = 'NA - graph is not connected'

        per = nx.periphery(self.nx_graph)
        cnt = nx.center(self.nx_graph)
        result = { #"""fast betweenness algorithm"""  
            'bbc': nx.brandes_betweenness_centrality(self.nx_graph),
            'tn': nx.triangles(self.nx_graph), # number of triangles
            'ec': ec,
            'per': per,
            'cnt': cnt,
            'Per': self.nx_graph.subgraph(per),
            'Cnt': self.nx_graph.subgraph(cnt)
            }
        return result
Exemple #5
0
    def extract_simple_features(self, graph):
        res = {}
        try:
            print('diameter: ', nx.diameter(graph))
            print('eccentricity: ', nx.eccentricity(graph))
            print('center: ', nx.center(graph))
            print('periphery: ', nx.periphery(graph))
            res['connected'] = True
        except Exception as e:
            print('Graph not connected')
            res['connected'] = False

        res['density'] = '{:.6f}'.format(nx.density(graph))
        res['Avg_degree'] = '{:.6f}'.format(
            sum([i[1] for i in nx.degree(graph)]) / len(nx.degree(graph)))
        res['Avg_weight'] = '{:.6f}'.format(
            sum([graph[edge[0]][edge[1]]['weight'] for edge in graph.edges]) /
            len([graph[edge[0]][edge[1]]['weight'] for edge in graph.edges]))
        res['edges'] = len(graph.edges)
        res['nodes'] = len(graph.nodes)
        res['self_loops'] = len(list(nx.nodes_with_selfloops(graph)))
        res['edge_to_node_ratio'] = '{:.6f}'.format(
            len(graph.nodes) / len(graph.edges))
        res['negative_edges'] = nx.is_negatively_weighted(graph)
        return res
def answer_eleven():
        
    # Your Code Here
    # We need to use only the nodes in the periphery
    # The eccentricity is the maximum distance from one node to all other nodes in G (returns an dict)
    # ecc = nx.eccentricity(G_sc)
    # The periphery is the set of nodes with eccentricity equal to the diameter.
    peri = nx.periphery(G_sc)
    # The diameter is the maximum eccentricity
    diam = nx.diameter(G_sc)

    numPathsDiam = {}

    # AQUI FICOU FODA!
    for node in peri:
        sp = nx.shortest_path(G=G_sc, source=node)
        pathsLenghtDiam = [path for path in sp.values() if (len(path) -1 == diam)]
        numPathsDiam[node] = len(pathsLenghtDiam)
    # FIM AQUI FICOU FODA!
    
    keys = list(numPathsDiam.keys())
    values = list(numPathsDiam.values())
    resultKey = keys[values.index(max(values))]
    
    return resultKey, numPathsDiam[resultKey] # == ('97', 63) # Your Answer Here
Exemple #7
0
def Distance_measures(P, tipo, ruta):
    RUTA =  ruta + '/NetWX/files/'
    path = Path(RUTA)
    path.mkdir(parents = True,exist_ok = True)
    
    P_center            = []
    P_diameter          = []
    P_extrema_bounding  = []
    P_periphery         = []
    P_radius            = []
    for i in range(len(P)):
        P_center.append(center(P[i]))
        P_diameter.append(diameter(P[i]))
        P_extrema_bounding.append(extrema_bounding(P[i]))
        P_periphery.append(periphery(P[i]))
        P_radius.append(radius(P[i]))
    P_center           = DataFrame(P_center)
    P_diameter         = DataFrame(P_diameter)
    P_extrema_bounding = DataFrame(P_extrema_bounding)
    P_periphery        = DataFrame(P_periphery)
    P_radius           = DataFrame(P_radius)
    P_center.to_csv(RUTA + tipo + " - center.txt", sep = '\t',header = None,index = False)
    P_diameter.to_csv(RUTA + tipo +" - diameter.txt", sep = '\t',header = None,index = False)
    P_extrema_bounding.to_csv(RUTA + tipo +" - extrema_bounding.txt", sep = '\t',header = None,index = False)
    P_periphery.to_csv(RUTA + tipo + " - periphery.txt", sep = '\t',header = None,index = False)
    P_radius.to_csv(RUTA + tipo + " - radius.txt", sep = '\t',header = None,index = False)
                
            
Exemple #8
0
    def find_longest_paths(self):
        """ finds the longest path in each chain. """

        self.longest_paths = []

        # loop through the chains
        for chain in self.chains:
            # The longest of the shortest paths between
            # the set of nodes in the periphery of the network is identified.

            # The eccentricity of a node is the maximum distance from a node to the other nodes.
            # the periphery is the set of nodes with the maximum eccentricity.
            # this is known as the diameter is network
            periphery = nx.periphery(chain)

            # find all the atoms belonging to the shortest paths between the elements of periphery 
            candidate_longest_paths = [
                [nx.shortest_path(chain, source, target) for target in periphery[source_index + 1:]] for
                source_index, source in enumerate(periphery)]

            # flatten each longest path
            candidate_longest_paths = [item for sub_list in candidate_longest_paths for item in sub_list]

            # get the length of each longest path
            longest_path_lengths = [len(long_path) for long_path in candidate_longest_paths]

            # find the max length (many will be the same, doesn't matter. get the first one).
            longest_path_index = np.argmax(longest_path_lengths)

            # extract the longest path from the current list of longest paths.
            # get one longest path per chain.
            self.longest_paths.append(candidate_longest_paths[longest_path_index])
def graph_stats(G: nx.Graph) -> Dict[str, Any]:
    stats = dict()

    n, m = G.number_of_nodes(), G.number_of_edges()

    stats['number_of_vertices'] = n
    stats['number_of_edges'] = m
    stats['complexity'] = n * m
    stats['density'] = 2 * m / (n * (n - 1))

    stats['connected_components'] = []
    for G_hat in (G.subgraph(c) for c in nx.connected_components(G)):
        component_stats = dict()

        component_stats['number_of_vertices'] = G_hat.number_of_nodes()
        component_stats['number_of_edges'] = G_hat.number_of_edges()
        component_stats['diameter'] = nx.diameter(G_hat, usebounds=True)
        component_stats['radius'] = nx.radius(G_hat, usebounds=True)
        component_stats['center_size'] = len(nx.center(G_hat, usebounds=True))
        component_stats['periphery_size'] = len(
            nx.periphery(G_hat, usebounds=True))

        stats['connected_components'] += [component_stats]

    stats['number_of_connected_components'] = len(
        stats['connected_components'])

    stats['average_clustering_coefficient'] = nx.average_clustering(G)

    return stats
def info(graph):
    """Function that take the graph whose information is to be displayed
       Information includes - 
       number of nodes - Total number of nodes
       number of edges - Total number of edges
       Average degree - Degree of nodes (Indegree+ Outdegree)
       radius - Minimum eccentricity
       diameter - Maximum eccentricity
       eccentricity - Eccentricity of a node v is the maximum distance from v to
        all other nodes in graph.
       center - Set of nodes with eccentricity equal to radius.
       periphery - Set of nodes with eccentricity equal to the diameter.
       Density
    """
    try:
        print("-" * 10, "Node Attributes", "-" * 10)
        print(n.info(graph))
        print("-" * 10, "Distance Measures", "-" * 10)
        print("radius: %d" % n.radius(graph))
        print("diameter: %d" % n.diameter(graph))
        print("eccentricity: %s" % n.eccentricity(graph))
        print("center: %s" % n.center(graph))
        print("periphery: %s" % n.periphery(graph))
        print("density: %s" % n.density(graph))
    except Exception as ex:
        print(ex)
    return graph
Exemple #11
0
def mkMatch(network, method):
    g_ = network
    if 'kclick' in method:  # k-click communities
        # k_ = int(method.replace('kclick', ''))
        svs = [i for i in x.algorithms.community.k_clique_communities(g_, 10)]
        gg = x.Graph()
        for i, sv in enumerate(svs):
            gg.add_node(i, weight=len(sv), children=sv)
    elif method == 'lab':  # label propagation
        svs = [
            i for i in x.algorithms.community.label_propagation_communities(g_)
        ]
        gg = x.Graph()
        for i, sv in enumerate(svs):
            gg.add_node(i, weight=len(sv), children=sv)
    elif method == 'cp':
        sub = [i for i in x.connected_component_subgraphs(g_)]
        g = sub[0]
        per = set(x.periphery(g))
        if len(sub) > 1:
            for per_ in sub[1:]:
                per.update(per_.nodes())
        cen = set(x.center(g))
        pc = per.union(cen)
        nodes = set(g.nodes())
        inter = nodes.difference(pc)
        gg = x.Graph()
        gg.add_node(0, label='center', weight=len(cen), children=cen)
        gg.add_node(1, label='intermediary', weight=len(inter), children=inter)
        gg.add_node(2, label='periphery', weight=len(per), children=per)
    return gg  # meta-network
Exemple #12
0
def multiplePartion(mutiplelist, infectionG, rumorSourceList):

    # 构建关于这个社区的传播子图
    tempGraph1 = nx.Graph()
    for edge in infectionG.edges:
        # if infectG.adj[edge[0]][edge[1]]['Infection']==2:      #作为保留项。
        if edge[0] in mutiplelist[0] and edge[1] in mutiplelist[0]:
            tempGraph1.add_edges_from([edge], weight=1)
    print('这个感染区域的传播子图边个数')
    print(tempGraph1.number_of_edges())
    print(tempGraph1.number_of_nodes())
    nodelist=list(tempGraph1.nodes)
    resultSource = []
    #
    tree = Tree()
    #初始化这些值。
    for  nodetemp in (tempGraph1.nodes):
        tempGraph1.add_node(nodetemp, T=0)
        tempGraph1.add_node(nodetemp, P=0)

    peripheryList=nx.periphery(tempGraph1)
    print ('边界节点'+str(peripheryList))

    randomnode= random.choice(nodelist)
    print ('node是多少'+str(randomnode))
    dict_Temp=dict(nx.bfs_successors(tempGraph1, source=randomnode))
    print (dict_Temp)
    for nodeTemps  in nodelist:
        if nodeTemps in peripheryList:
            tempGraph1.add_node(nodeTemps,T=1)
            tempGraph1.add_node(nodeTemps,P=1)
        else:
            if nodeTemps ==randomnode:
def parseCSVGraphAnalysis(G):
    global headers
    row = ''
    data = nx.info(G).split('\n')
    nameG = data[0].split(':')[1].strip()
    if saveGEXF:
        nx.write_gexf(G, nameG + '.gexf')
    if headers:
        row = 'Name' + separator + separator.join(
            val.split(':')[0] for val in data[2:])
        row += (separator + 'Density') if selected[0] else ''
        row += (separator + 'Diameter') if selected[1] else ''
        row += (separator + 'Degrees') if selected[2] else ''
        row += (separator + 'Eccentricity') if selected[3] else ''
        row += (separator + 'Center') if selected[4] else ''
        row += (separator + 'Periphery') if selected[5] else ''
        row += '\n'
        headers = False
    row += nameG + separator + separator.join(
        val.split(':')[1].strip() for val in data[2:])
    row += (separator + str(nx.density(G))) if selected[0] else ''
    row += (separator + str(nx.diameter(G))) if selected[1] else ''
    row += (separator + str(nx.degree(G))) if selected[2] else ''
    row += (separator + str(nx.eccentricity(G))) if selected[3] else ''
    row += (separator + str(nx.center(G))) if selected[4] else ''
    row += (separator + str(nx.periphery(G))) if selected[5] else ''
    return row
def create_graph():
    print('create graph')
    api = API()
    papers = api.get_all_paper()
    g = nx.Graph()
    g.clear()

    for paper in papers:
        references = [
            x.get_paper_id() for x in paper.references if x.get_paper_id()
        ]

        for ref_id in references:
            g.add_edge(str(paper.id), str(ref_id))

    degrees = [len(g.edges(node)) for node in g.nodes]

    for degree in degrees:
        if degree == 0:
            print("nope!")

    print("# nodes: ", g.number_of_nodes())
    print("# edges: ", g.number_of_edges())
    print("# components: ", len(list(nx.connected_components(g))))
    print("max degree: ", max(degrees))
    print("mean degree: ", round(mean(degrees), 4))
    print("median degree: ", statistics.median(degrees))
    print("diameter: ", nx.diameter(g), " (maximum eccentricity - max path)")
    print("periphery: ", len(nx.periphery(g)),
          " (# nodes eccentricity equal to the diameter)")
    create_degree_distribution(degrees, 'Degree Distribution', '#00365A', 13,
                               100, 3.5)
Exemple #15
0
def print_graph_info(graph):
  e = nx.eccentricity(graph)
  print 'graph with %u nodes, %u edges' % (len(graph.nodes()), len(graph.edges()))
  print 'radius: %s' %  nx.radius(graph, e) # min e
  print 'diameter: %s' % nx.diameter(graph, e) # max e
  print 'len(center): %s' % len(nx.center(graph, e)) # e == radius
  print 'len(periphery): %s' % len(nx.periphery(graph, e)) # e == diameter
def graph_properties(G):
    print("No. of triangles:", nx.triangles(G))
    print("Clustering: ", nx.clustering(G))
    structural_balance(G)
    print("Radius:", nx.radius(G))
    print("Diameter:", nx.diameter(G))
    print("Density:", nx.density(G))
    print("Periphery:", nx.periphery(G))
Exemple #17
0
def print_graph_info(graph):
    e = nx.eccentricity(graph)
    print 'graph with %u nodes, %u edges' % (len(
        graph.nodes()), len(graph.edges()))
    print 'radius: %s' % nx.radius(graph, e)  # min e
    print 'diameter: %s' % nx.diameter(graph, e)  # max e
    print 'len(center): %s' % len(nx.center(graph, e))  # e == radius
    print 'len(periphery): %s' % len(nx.periphery(graph, e))  # e == diameter
def answer_nine():
       
    # Your Code Here
#     d = nx.diameter(G_sc)
#     e = nx.eccentricity(G_sc)
#     n = [node for node in e.items() if node[1] == d]
#     set([node[0] for node in n])
    return set(nx.periphery(G_sc)) # Your Answer Here
def find_periphery(
        G):  #set of nodes that have the eccentricity equal to the diameter
    try:
        per = nx.periphery(G)
    except nx.exception.NetworkXError:
        print('Graph is not connected')
        per = {}
    print('Periphery: ', per)
    return per
Exemple #20
0
def answer_nine():
    G = answer_six()

    # Manual calculate:
    # ec = nx.eccentricity(G)
    # diameter = nx.diameter(G)
    # return set([node for node, value in ec.items() if value==diameter])

    return set(nx.periphery(G))
def answer_eleven():
    scores = {}
    G_sc = answer_six()
    diameter = nx.diameter(G_sc)
    periphery = nx.periphery(G_sc)
    for peripheral in periphery:
        shortest = nx.shortest_path_length(G_sc, peripheral)
        current = len([k for k,v in shortest.items() if v == diameter])
        scores[peripheral] = current
    return (str(max(scores)), scores[max(scores)])
def answer_nine():

    # Your Code Here

    # initialise the graph
    G = answer_six()

    # get the nodes whos eccentricity is equal to the graph diameter
    nodes = set(nx.periphery(G))

    return nodes  # Your Answer Here
Exemple #23
0
def answer_eleven():
    # Your Code Here
    graph = answer_six()
    diameter, periphery = nx.diameter(graph), nx.periphery(graph)
    result, max_count = None, -1
    for node in periphery:
        sp = nx.shortest_path_length(graph, node)
        count = list(sp.values()).count(diameter)
        if count > max_count:
            result = node
            max_count = count
    return result, max_count  # Your Answer Here
def test_periphery(testgraph):
    """
    Testing periphery function for graphs.
    """

    a, b = testgraph
    nx_per = nx.periphery(a)
    sg_per = sg.digraph_distance_measures.periphery(b, b.nodes())
    nx_per = array(nx_per, dtype = uint32)
    nx_per.sort()
    sg_per.sort()
    assert_equal(nx_per, sg_per)
Exemple #25
0
def test_periphery(testgraph):
    """
    Testing periphery function for graphs.
    """

    a, b = testgraph
    nx_per = nx.periphery(a)
    sg_per = sg.graph_distance_measures.periphery(b, b.order())
    nx_per = array(nx_per, dtype=uint32)
    nx_per.sort()
    sg_per.sort()
    assert_equal(nx_per, sg_per)
Exemple #26
0
def answer_eleven():
    
    G_sc =  answer_six()   
    dia = nx.diameter(G_sc)
    periph_list = list(set(nx.periphery(G_sc)))
    result = []
    for node in periph_list:
        p = nx.shortest_path_length(G_sc, node).values()
        
        result.append((node,len([val for val in p if val == dia])))
    
    return max(result, key = lambda x : x[1])
def distanceMeasures(G):
    #print("average",nx.average_shortest_path_length(G)) #Average distance between every pair of nodes.
    print("diameter",
          nx.diameter(G))  #maximum distance between any pair of nodes.
    print(
        "eccen", nx.eccentricity(G)
    )  #of a node n is the largest distance between n and all other nodes.
    print("radius", nx.radius(G))  #Minimum eccentricity.
    print("periphery", nx.periphery(
        G))  #Set of nodes that have eccentricity equal to the diameter.
    print("center", nx.center(
        G))  #set of nodes that have eccentricity equal to the radius.
Exemple #28
0
def important_characteristics_of_graph(G):
    print("Eccentricity: ", nx.eccentricity(G))
    print("Diameter: ", nx.diameter(G))
    print("Radius: ", nx.radius(G))
    print("Preiphery: ", list(nx.periphery(G)))
    print("Center: ", list(nx.center(G)))

    weakly_component = [G.subgraph(c).copy() for c in sorted(nx.weakly_connected_components(G))]

    largest_wcc = max(weakly_component)

    print(weakly_component)
Exemple #29
0
def answer_eleven():
    G = answer_six()
    d = nx.diameter(G)
    peripheries = nx.periphery(G)
    max_count = -1
    node = None
    for item in peripheries:
        sp = nx.shortest_path_length(G, item)
        count = list(sp.values()).count(d)
        if count > max_count:
            node = item
            max_count = count
    return node, max_count
Exemple #30
0
def print_main_params(G):
    # Eccentricity of a node n is the largest distance between n and all other nodes.
    print ('eccentricity=',nx.eccentricity(G))                                 #{'Зябликово': 7, 'Шипиловская': 6, 'Борисово': 5, 'Марьино': 4, 'Чкаловская': 4, 'Сретенский бульвар': 5, 'Достоевская': 6, 'Курская': 5, 'Таганская': 6, 'Киевская': 7, 'Комсомольская': 6, 'Новослободская': 7}
    # Average distance between every pair of nodes.
    print ('average_shortest_path_length=',nx.average_shortest_path_length(G)) # 3.03
    # Diameter: maximum distance between any pair of nodes.
    print ('diameter=',nx.diameter(G))                                         # 7
    # Radius: the minimum eccentricity in the graph.
    print ('radius=',nx.radius(G))                                             # 4
    # The Periphery is the set of nodes with eccentricity = diameter.
    print ('periphery=',nx.periphery(G))                                       # ['Зябликово', 'Киевская', 'Новослободская']
    # The center is the set of nodes with eccentricity = radius. 
    print ('center=',nx.center(G))                                             # ['Марьино', 'Чкаловская']
Exemple #31
0
    def _get_list_pos(self, graph):

        temp_pos = dict()

        center = nx.center(graph)
        for node in center:
            temp_pos[node] = 1

        periphery = nx.periphery(graph)
        for node in periphery:
            temp_pos[node] = 2

        return temp_pos
Exemple #32
0
def answer_eleven():
    G = answer_six()
    d = nx.diameter(G)
    peripheries = nx.periphery(G)
    max_count = -1
    result_node = None
    for node in peripheries:
        sp = nx.shortest_path_length(G, node)
        count = list(sp.values()).count(d)
        if count > max_count:
            result_node = node
            max_count = count
            return result_node, max_count
Exemple #33
0
    def distance(self):
        rslt = {}
        if self.directed == 'undirected':
            rslt['center'] = nx.center(self.graph)
            rslt['diameter'] = nx.diameter(self.graph)
            rslt['eccentricity'] = nx.eccentricity(self.graph)
            rslt['periphery'] = nx.periphery(self.graph)
            rslt['radius'] = nx.radius(self.graph)

        fname_distance = self.DIR + '/distance.json'
        with open(fname_distance, "w") as f:
            json.dump(rslt, f, cls=SetEncoder, indent=2)
        print(fname_distance)
def make_dag(g):
    if nx.is_directed_acyclic_graph(g):
        return
    p = nx.periphery(g)
    for c in nx.weakly_connected_component_subgraphs(g):
        if nx.is_directed_acyclic_graph(g):
            continue
        cycles = nx.simple_cycles(c)
        for c in cycles:
            edges = zip(c[:-1], c[1:])
            edges.append((c[-1], c[0]))
            for e in edges:
                data = g.edges(e[0], e[1])[0][2]
                c.remove_edge(e[0], e[1])
def UndirectedGraphFeature(df, dfOrigin, part):
    indexs, files = df.index, dfOrigin.file_id.unique()
    for col in apiSet:
        df[col+'_center_degree'] = 0

    for index, file in zip(indexs, files):
        X = pd.read_csv('./' + part + '/' + str(index) + '.csv')
        api = X.groupby(by='tid').apply(lambda x: ' '.join(x.api))
        api = pd.DataFrame(api)
        api.rename(columns={0: 'api_call'}, inplace=True)

        G = nx.Graph()
        for row in api.index:
            apiCall = (api.loc[row, 'api_call']).split(' ')
            for i in range(len(apiCall) - 1):
                G.add_edge(apiCall[i], apiCall[i + 1])
        if (len(G) <= 1):
            continue
        isConnnected = (nx.is_connected(G) == False)
        if isConnnected:
            df.loc[index, 'avg_length'] = -1
            df.loc[index, 'minimum_edge_cut'] = -1
            df.loc[index, 'degree_assortativity_coefficient'] = -1
            df.loc[index, 'radius'] = -1
            df.loc[index, 'diameter'] = -1
            df.loc[index, 'periphery'] = -1
            df.loc[index, 'is_eulerian'] = -1
            df.loc[index, 'center'] = -1
            df.loc[index, 'order'] = -1
            df.loc[index, 'size'] = -1
            df.loc[index, 'density'] = -1

        else:
            df.loc[index, 'avg_length'] = nx.average_shortest_path_length(G)
            df.loc[index, 'minimum_edge_cut'] =  len(set(nx.minimum_edge_cut(G)))
            df.loc[index, 'degree_assortativity_coefficient'] = nx.degree_assortativity_coefficient(G)
            df.loc[index, 'radius'] = nx.radius(G)
            df.loc[index, 'diameter'] = nx.diameter(G)
            df.loc[index, 'periphery'] = len(set(nx.periphery(G)))
            df.loc[index, 'is_eulerian'] = int(nx.is_eulerian(G))
            df.loc[index, 'center'] =  len(set(nx.center(G)))
            df.loc[index, 'density'] = nx.density(G)
            df.loc[index, 'order'] = G.order()
            df.loc[index, 'size'] = G.size()

        if not isConnnected:
            for x in set(nx.center(G)):
                df.loc[index,x+'_center_degree'] = 1
        print(index)
    return df
    def f8(self):
        return "ND"
        start = 0
        try:
            per_list = nx.periphery(self.G)
        except nx.exception.NetworkXError:
            per_list = {}

        if per_list != {}:
            res = len(per_list)/self.nodes
        else:
            res = "ND"
        stop = 0
        # self.feature_time.append(stop - start)
        return res
Exemple #37
0
def append_periphery_nodes(graph, table_data):
    """Append periphery nodes to table_data."""

    # first, setup all values to false
    for node in graph.nodes(data=True):
        node_name = gen_username(node[1]['first_name'],
                                 node[1]['last_name'],
                                 node[0])
        if node_name in table_data:
            table_data[node_name].append('')
        else:
            table_data[node_name] = ['']

    # second, update these values to True for periphery
    for uid in nx.periphery(graph):
        node_name = gen_username(graph.node[uid]['first_name'],
                                 graph.node[uid]['last_name'],
                                 uid)
        table_data[node_name][-1] = 'True'
def report_components(g):
	components = nx.connected_component_subgraphs(g)
	print "Components: %d" % len(components)
	c_data = {}
	for i in range(len(components)):
		c = components[i]
		if len(c.nodes()) > 5: # Avoid reporting on many small components
			c_data["nodes"] = len(c.nodes())
			c_data["edges"] = len(c.edges())
			c_data["avg_clustering"] = nx.average_clustering(c)
			c_data["diameter"] = nx.diameter(c)
			c_data["radius"] = nx.radius(c)
			c_data["center"] = len(nx.center(c))
			c_data["periphery"] = len(nx.periphery(c))

			print "* Component %d:" % i
			for k in c_data:
				print "--- %s: %s" % (k, c_data[k])

	return c_data
 def test_bound_periphery(self):
     assert_equal(set(networkx.periphery(self.G, usebounds=True)), set([1, 4, 13, 16]))
Exemple #40
0
#plus courts chemins
pathlengths=[]

print("source vertex {target:length, }")
for v in G.nodes():
    spl=nx.single_source_shortest_path_length(G,v)
    print('%s %s' % (v,spl))
    for p in spl.values():
        pathlengths.append(p)

print('')
print("plus court chemin moyen %s" % (sum(pathlengths)/len(pathlengths)))

#distribution des plus courts
dist={}
for p in pathlengths:
    if p in dist:
        dist[p]+=1
    else:
        dist[p]=1

print('')
print("length #paths")

print("densité : %s" % nx.density(G))
print("radius : %d" % nx.radius(G))
print("diamètre : %d" % nx.diameter(G))
print("eccentricity: %s" % nx.eccentricity(G))
print("center: %s" % nx.center(G))
print("periphery: %s" % nx.periphery(G))
 def periphery(self): #f-28
     return len(nx.periphery(self.ng))/float(self.N)
Exemple #42
0
    def cleanGraph(self):
        #Need fully connected graphs to get the diameter
        subG = nx.connected_component_subgraphs(self.inputGml)
        #Just in case subG isn't of list type (networkx 1.7)
        try:
            logging.info("PreFilter: %d subGraphs" % (len(subG)))
        except TypeError:
            pass
        
        self.subGraphs = []
        
        for myGraph in subG:
            #Break any edge that didn't make a correct assembly
            # or doesn't span (excluding scaffold/contig evidence)
            for a,b in myGraph.edges():
                name = makeFilMetName(a,b)
                if "Contig" in myGraph.edge[a][b]['evidence']:
                    if name in self.allMetrics.keys():
                        #circles...
                        del(self.allMetrics[name])
                elif "Scaffold" in myGraph.edge[a][b]['evidence']:
                    if name in self.allMetrics.keys():
                        myGraph.node[a]['trim'] = self.allMetrics[name].getTrim(a)
                        myGraph.node[b]['trim'] = self.allMetrics[name].getTrim(b)
                elif name not in self.allMetrics.keys():
                    logging.debug("Breaking %s due to assembly failure" %  name)
                    myGraph.remove_edge(a, b)
                elif not self.allMetrics[name].span:
                    logging.debug("Breaking %s due to non-span" %  name)
                    myGraph.remove_edge(a, b)
                elif self.allMetrics[name].isSelfCircle:
                    logging.debug("Breaking %s due to self-circularity" % name)
                    myGraph.remove_edge(a, b)
    
            #Resolving "forked" nodes
            # Usually caused by some repeat. Right now, it's all about the fill quality
            for node in myGraph.nodes_iter():
                if len(myGraph.edge[node]) > 2:
                    best = None
                    bestScoreSpan= 0
                    bestScoreSeqs = 0
                    bestSpanScore = 0
                    for edge in myGraph.edge[node]:
                        name = makeFilMetName(node,edge)
                        if name in self.allMetrics.keys():
                            data = self.allMetrics[name]
                            seq = data.getSequence()
                            if seq is None:
                                #I fixed this
                                logging.debug("About to Fail on %s (node: %s)" % (name, node))
                            
                            #if len(seq.qual) == 0 or seq is None:
                            if len(seq.qual) == 0:
                                logging.info("NoFilling %s " % name)
                                myScoreSpan = 0
                                myScoreSeqs = 0
                            else:
                                #myScore = data.contribBases / float(data.contribSeqs)
                                myScoreSpan = data.spanCount
                                myScoreSeqs = data.contribBases
                                #sum([ord(y)-33 for y in seq.qual])/float(len(seq.qual))
                                
                            if myScoreSpan > bestScoreSpan:
                                bestScoreSpan = myScoreSpan
                                bestScoreSeqs = myScoreSeqs
                                best = name
                            elif myScoreSeqs == bestScoreSeqs:
                                if myScoreSpan > bestScoreSpan:
                                    bestScoreSpan = myScoreSpan
                                    bestScoreSeqs = myScoreSeqs
                                    best = name
                            
                    logging.debug("Resolved fork to be %s" % best)
                    if best is None:
                        #Again, think I fixed
                        logging.debug("I don't know how this doesn't get set")
                        logging.debug("Node %s" % (node))
                        logging.debug(json.dumps(myGraph.edge[node], indent=4))
                        
                    for edge in list(myGraph.edge[node]):
                        name = makeFilMetName(node, edge)
                        if "Contig" in myGraph.edge[node][edge]['evidence'] \
                            or "Scaffold" in myGraph.edge[node][edge]['evidence']:
                            pass
                        elif name != best:
                            logging.debug("Removed edge %s" % name)
                            myGraph.remove_edge(node,edge)
                
                #add trim everywhere--everything left is either with or without metrics
                for edge in myGraph.edge[node]:
                    if "Contig" in myGraph.edge[node][edge]['evidence']:
                        continue
                    name = makeFilMetName(node,edge)
                    if name not in self.allMetrics.keys():
                        myGraph.node[node]['trim'] = 0
                        myGraph.node[edge]['trim'] = 0
                        continue
                    myGraph.node[node]['trim'] = self.allMetrics[name].getTrim(node)
                    myGraph.node[edge]['trim'] = self.allMetrics[name].getTrim(edge)
                if node in self.allMetrics.keys() and 'trim' not in myGraph.node[node].keys():
                    myGraph.node[node]['trim'] = self.allMetrics[node].getTrim(node)
                    
            #Getting the contig paths
            for i,s in enumerate(nx.connected_component_subgraphs(myGraph)):
                #print "prefilter diameter of testSub piece %d == %d" % (i, nx.diameter(s))
                #I may get an error here if my above cleaning work isn't good enough 
                #for every case
                try:
                    #I don't understand the error I'm getting here...
                    #Doesn't happen anymore
                    ends = nx.periphery(s)#Singletons...
                except AttributeError:
                    logging.debug("Weird error!")
                    logging.debug("Nodes " + json.dumps(myGraph.node))
                    logging.debug("edges " + json.dumps(myGraph.edge))
                    logging.debug("types " + str(type(s)) + " " + str(i) + " " + str(type(i)))
                    logging.debug("Trying again??")
                    ends = nx.periphery(s)
                    
                if len(ends) > 2 and len(ends) == s.number_of_nodes():
                    logging.warning("Circular graph detected. Breaking weakest edge")
                    worstScoreSpan = 0
                    worstScoreSeqs = 0
                    worstEdge = None
                    for a,b in s.edges():
                        if "Contig" in myGraph.edge[a][b]['evidence'] \
                           or "Scaffold" in myGraph.edge[a][b]['evidence']:
                            continue
                        
                        logging.debug( "HERE" )
                        logging.debug(str(a)+" "+str(b))
                        name = makeFilMetName(a,b)
                        data = self.allMetrics[name]
                        myScoreSpan = data.spanCount
                        logging.debug(myScoreSpan)
                        myScoreSeqs = data.contribBases
                        logging.debug(myScoreSeqs)
                            
                        if myScoreSpan > worstScoreSpan:
                            worstScoreSpan = myScoreSpan
                            worstScoreSeqs = myScoreSeqs
                            worstEdge = (a,b)
                        if myScoreSeqs == worstScoreSeqs:
                            if myScoreSpan > worstScoreSpan:
                                worstScoreSpan = myScoreSpan
                                worstScoreSeqs = myScoreSeqs
                                worstEdge = (a,b)
                        
                        
                    logging.info("breaking at %s" % (str(worstEdge)))
                    s.remove_edge(*worstEdge)
                
                #if the above didn't if didn't fix periphery, we'll get
                #a value error and a problem parsing the graph...
                try:
                    a,b = nx.periphery(s)
                except ValueError:
                    logging.error("Graph doesn't have ends. Check it's repeats in collectionErr.gml")
                    logging.error(nx.periphery(s))
                    nx.write_gml(s, "collectionError.gml")
                    exit(1)
                #What I should default to here is just breaking all non captured links

                #nx.shortest_path(s,a,b)
                self.subGraphs.append(s)
    
        logging.info("PostFilter: %d subGraphs" % (len(self.subGraphs)))
def peri(net):
    return setSize(nx.periphery(net,e=eccentricity_list(net)), net.number_of_nodes(),'periphery')
Exemple #44
0
 def outputContigs(self):
     """
     output all the contigs, use the span and stuff get
     """
     fout = open(os.path.join(self.protocol.outDir, "jelly.out.fasta"), 'w')
     qout = open(os.path.join(self.protocol.outDir, "jelly.out.qual" ), 'w')
     liftOverTable = {}#ContigName: [(piece, strand, size), ]
     for part,graph in enumerate(self.subGraphs):
         logging.debug("Contig %d" % part)
         liftTracker = []
         end, start = nx.periphery(graph)
         path = nx.shortest_path(graph, start, end)
         #Change 1 For Filps -- Try moving down normal
         #if not start.endswith("e5"):
             #start, end = start, end
         
         curFasta = []
         curQual = []
         name = makeFilMetName(start, "")
         #First guy's extender
         if name in self.allMetrics.keys():
             logging.debug("First Extender %s" % name)
             data = self.allMetrics[name]
             seq = data.getExtendSequence(name)
             strand = '+'
             if data.seed1Strand == '-':
                 seq.reverseCompliment()
                 strand = '-'
             liftTracker.append((name, strand, len(seq.seq)))
             curFasta.append(seq.seq)
             curQual.append(seq.qual)
         
         #Did we filp the previous sequence
         pFlip = 1  #No = 1
         #Are we putting this together backwards from the start
         FirstFlip = name.endswith('e3')
         
         for i, nodeA in enumerate(path[:-1]):
             nodeB = path[i+1]
             logging.debug("Moving from %s to %s (p=%d)" % (nodeA, nodeB, pFlip))
             name = makeFilMetName(nodeA, nodeB)
             #Existing sequence -- put in A
             if "Contig" in graph.edge[nodeA][nodeB]['evidence']:
                 logging.debug("contig")
                 #need to output the contig seq
                 #Trim Note 1
                 seq = self.grabContig(nodeA, nodeB, graph)
                 strand = '+'
                 if pFlip == -1:
                     strand = '-'
                     seq.reverseCompliment()
                 curFasta.append(seq.seq)
                 curQual.append(seq.qual)
                 liftTracker.append((name, strand, len(seq.seq)))
             #We have to, at the very least, keep a gap in the sequence
             elif "Scaffold" in graph.edge[nodeA][nodeB]['evidence'] and \
                             name not in self.allMetrics.keys():
                 logging.debug("unimproved gap")
                 #keep mat orientation the same
                 a = nodeA[nodeA.rindex('.')+1:-2]
                 b = nodeB[nodeB.rindex('.')+1:-2]
                 j = [int(a),int(b)]; j.sort(); a,b = j
                 gapName = "%s_%d_%d" % (nodeA[:10], a, b)
                 gapSize = self.gapInfo[gapName].length
                 curFasta.append("N"*gapSize)
                 curQual.append("!"*gapSize)
                 liftTracker.append((name, '?', gapSize))
             elif "Scaffold" in graph.edge[nodeA][nodeB]['evidence'] and \
                             name in self.allMetrics.keys():
                 logging.debug("improved gap")
                 data = self.allMetrics[name]
                 seq = data.getSequence()
                 
                 if not data.sameStrand:
                     logging.error(("Gap %s has opposite strand "
                                      "fillers even though they're "
                                      "within scaffold gaps") % name)
                     exit(10)#never happens
                 strand = '+'
                 if (pFlip == -1 and data.getSeedStrand(nodeA) == '+') or \
                    (pFlip == 1 and data.getSeedStrand(nodeA) == '-'):
                     strand = '+'
                     seq.reverseCompliment()
                 liftTracker.append((name, strand, len(seq.seq)))
                 curFasta.append(seq.seq)
                 curQual.append(seq.qual)
             else:
                 #Else we have new sequence joining Scaffolds
                 logging.debug("new sequence")
                 data = self.allMetrics[name]
                 
                 seq = data.getSequence()
                 
                 a = 1 if data.getSeedStrand(nodeA) == '+' else -1
                 b = 1 if data.getSeedStrand(nodeB) == '+' else -1
                 
                 #I've put the first contig in backwards
                 #if FirstFlip is None:
                     #FirstFlip = nodeA.endswith('e3') 
                     #logging.debug("FirstFlip internal %s - %s" % (str(FirstFlip), nodeA))
                 
                 if pFlip == a:
                     m = 1
                 else:
                     m = -1
                 
                 strand = '+'
                 if m == -1:
                     strand = '-'
                     seq.reverseCompliment()
                 liftTracker.append((name, strand, len(seq.seq)))
                 
                 curFasta.append(seq.seq)
                 curQual.append(seq.qual)
                 
                 pFlip = b * m
                 
         name = makeFilMetName(end, "")
         #Final guy's extender
         if name in self.allMetrics.keys():
             data = self.allMetrics[name]
             seq = data.getExtendSequence(name)
             strand = '+'
             #Here -- if we're in the filp and this is on opposite strand
             if pFlip == -1 and data.seed1Strand == '+':
                 strand = '-'
                 seq.reverseCompliment()
             liftTracker.append((name, strand, len(seq.seq)))
             curFasta.append(seq.seq)
             curQual.append(seq.qual)
         
         #We may have been assembling - strand this whole time and we need
         # revcomp it
         if FirstFlip:
             tF = []
             tQ = []
             tL = []
             logging.debug("FirstFlipping %d" % (part))
             """
             for i in curFasta[::-1]:
                 #tF.append(i.translate(revComp)[::-1])
                 tF.append(revComp)
             for i in curQual[::-1]:
                 #tQ.append(i[::-1])
                 tQ.append(i)
             for i in liftTracker[::-1]:
                 name, strand, size = i
                 #if strand == '+':
                     #strand = '-'
                 #elif strand == '-':
                     #strand = '+'
                 tL.append((name, strand, size))
             """
             #""Change 2 -- Should I be iterating this backwards?
             for i in curFasta:
                 tF.append(i.translate(revComp)[::-1])
             for i in curQual:
                 tQ.append(i[::-1])
             for i in liftTracker:
                 name, strand, size = i
                 if strand == '+':
                     strand = '-'
                 elif strand == '-':
                     strand = '+'
                 tL.append((name, strand, size))
             #"
             curFasta = tF
             curQual = tQ
             liftTracker = tL
             
         fout.write(">Contig%d\n%s\n" %  (part, "".join(curFasta)))
         qout.write(">Contig%d\n%s\n" % (part, "".join(curQual)))
         liftOverTable["Contig%d" % (part)] = liftTracker
     
     fout.close()
     qout.close()
     lout = open(os.path.join(self.protocol.outDir, 'liftOverTable.json'),'w')
     json.dump(liftOverTable, lout)
     lout.close()
Exemple #45
0
nx.condensation(G, scc)

# attracting components
nx.is_attracting_component(G)
nx.number_attracting_components(G)
nx.attracting_components(G)

# directed acyclic graphs
nx.is_directed_acyclic_graph(G)
nx.is_aperiodic(G)

# distance measure  (all for connected graph)
nx.center(Gcc)
nx.diameter(Gcc)
nx.eccentricity(Gcc) 
nx.periphery(Gcc)
nx.radius(Gcc)

# flows (seg fault currently)
#nx.max_flow(Gcc, 1, 2)
#nx.min_cut(G, 1, 2)

# isolates
nx.is_isolate(G, 1)     # False
nx.is_isolate(G, 5)     # True

# HITS
nx.hits(G,max_iter=1000)  # cannot converge?

# maximal independent set
nx.maximal_independent_set(G)
Exemple #46
0
import sys
import networkx as nx
from time import time
from copy import deepcopy
from itertools import permutations, combinations
stop = False
explored = 0
while not stop:
  explored += 1
  nodes = 8
  degree = 3
  G  = nx.random_regular_graph(degree, nodes)
  nx.set_edge_attributes(G, 'capacity', {k : 1.0 for k in G.edges_iter()})
  umoded_graph = nx.Graph.copy(G)
  try:
    source = nx.periphery(G)[0]
  except:
    continue
  diameter = nx.diameter(G)
  distances = nx.single_source_dijkstra(G, source)
  target = max(distances[1], key=lambda k:distances[0][k])
  nbrs = G.neighbors(source)
  G.graph['source'] = source
  G.graph['dest'] = target
  G.node[source]['source'] = True
  G.node[target]['target'] = True
  paths = []
  mincut = min(map(lambda x: nx.min_cut(G, x[0], x[1]), combinations(xrange(0, nodes), 2)))
  for ordering in permutations(nbrs):
    G = nx.Graph.copy(umoded_graph)
    del paths[:]
 def test_bound_periphery(self):
     result = set([1, 4, 13, 16])
     assert_equal(set(nx.periphery(self.G, usebounds=True)), result)
Exemple #48
0
def parametres(G,nG):
    print "\nParametres disponibles:\n"
    
    ###################################
    print "\tParametres Globals: \n"
    print "\t[1] Betweenness Centralization\n"
    print "\t[2] Average path length\n"
    print "\t[3] Assortativity degree\n"
    print "\t[4] Diameter\n"
    print "\t[5] Density\n"
    print "\t[6] Cohesion\n"
    print "\t[7] Radius\n"
    
    print "\tParametres per cada node de la xarxa: \n"
    print "\t[11] Betweenness\n"
    print "\t[12] Pagerank\n"
    print "\t[13] EigenVectorCentrality\n"
    print "\t[14] Average degree connectivity\n"
    print "\t[15] Periphery\n"
    print "\t[16] Eccentricity\n"
    print "\t[17] Center nodes\n"
    
    
    se = raw_input('Escriu els numeros dels parametres que vulguis amb una coma entre mig:\n')
    secom = se.split(',')
    
    for i in range(len(secom)):
        if secom[i] == "1":
                print "\nBetweenness Centralization:"
                print betweenness_centralization(G)
        elif secom[i] == "2":
                print "\nAverage path length:"
                print G.average_path_length()            
        elif secom[i] == "3":
                print "\nAssortativity degree:"
                print G.assortativity_degree()            
        elif secom[i] == "4":
                print "\nDiameter:"
                print G.diameter()          
        elif secom[i] == "5":
                print "\nDensity:"
                print G.density()            
        elif secom[i] == "6":
                print "\nCohesion:"
                print G.cohesion()            
        elif secom[i] == "7":
                print("\nRadius:")
                print nx.radius(nG)
    ###################################
        elif secom[i] == "11":
                print "\nBetweenness:"
                print G.betweenness(directed=False, cutoff=16)            
        elif secom[i] == "12":
                print "\nPagerank:"
                print G.pagerank()           
        elif secom[i] == "13":
                print "\nEigenVectorCentrality:"
                print G.eigenvector_centrality()            
        elif secom[i] == "14":
                print "\nAverage degree connectivity:"
                print nx.average_degree_connectivity(nG)            
        elif secom[i] == "15":
                print "\nPeriphery:"
                print nx.periphery(nG)            
        elif secom[i] == "16":
                print("\nEccentricity:")
                print nx.eccentricity(nG)            
        elif secom[i] == "17":
                print("\nCenter:")
                print nx.center(nG)            
    
    #####Parametres exclosos#####
    #print "Assortativity meu:" # es el mateix que el degree?
    #print assortativitymeu(G)  
    #print("diameter: %d" % nx.diameter(nG))
    #print("density: %s" % nx.density(nG))
    #print("richclub coefficient: %s" % nx.rich_club_coefficient(nG.to_undirected()))
    #print("richclub coefficient: %s" % nx.rich_club_coefficient(nG))
    
    return 2
Exemple #49
0
import networkx as nx
import plot_multigraph
import matplotlib.pylab as plt
from matplotlib import pylab as plt

n = 80
k = int(.2 * n)
p = 10. / n
G = nx.fast_gnp_random_graph(n, p, seed=42)

def set_to_list(set_, G):
  return [1. * (k in set_) for k in G.nodes()]

graph_colors = [
  ("center", set_to_list(nx.center(G), G)),
  ("periphery", set_to_list(nx.periphery(G), G)),
  ("k_core", set_to_list(nx.k_core(G), G)),
  ("k_shell", set_to_list(nx.k_shell(G), G)),
  ("k_crust", set_to_list(nx.k_crust(G), G)),
  ("k_corona", set_to_list(nx.k_corona(G, k), G)),
]

fig = plot_multigraph.plot_color_multigraph(G, graph_colors, 2, 3, node_size=50)
plt.savefig('graphs/sets.png', facecolor=fig.get_facecolor())
 def test_periphery(self):
     assert_equal(set(networkx.periphery(self.G)),set([1, 4, 13, 16]))
Exemple #51
0
def parametres(G,nG):
    print "\nParametres disponibles:\n"
    
    ###################################
    print "\tParametres Globals: \n"
    print "\t[1] Betweenness Centralization\n" #Aguanta xarxes inconectes
    # Es la mesura de betweenes centrality feta en tots els nodes de la xarxa.
    #The network betweenness centralization score is calculated based on the betweenness centrality for each individual in the network.
    #Betweenness centrality examines the number of times one person lies on the shortest path between two others
    #A highly centralized network has a great degree of inequality between individual centrality scores, while an uncentralized network has no inequality between individual centrality scores.
    print "\t[2] Average path length\n" 
    #La longitud de cami mitjana entre dos punts
    print "\t[3] Assortativity degree\n"
    #positive values of r indicate a correlation between nodes of similar degree,
    #while negative values indicate relationships between nodes of different degree.
    #This coefficient is basically the correlation between the actual connectivity patterns of the vertices and the pattern expected from the
    #disribution of the vertex types.
    print "\t[4] Diameter\n"
    #Diametre de la xarxa
    print "\t[5] Density\n"
    #El numero de enllacos respecte al total que hi pot existir a la xarxa
    #Si el numero s'apropa a 1 sera una xarxa densa, si es un numero molt petit sera una xarxa escasa o sparse graph.
    print "\t[6] Cohesion\n"
    #Calcula el numero necesari de vertex per tal de divir una xarxa en dos components.
    #The vertex connectivity between two given vertices is the number of vertices
    #that have to be removed in order to disconnect the two vertices into two
    #separate components. This is also the number of vertex disjoint directed
    #paths between the vertices (apart from the source and target vertices of
    #course). The vertex connectivity of the graph is the minimal vertex
    #connectivity over all vertex pairs.
    #
    #This method calculates the vertex connectivity of a given vertex pair if both
    #the source and target vertices are given. If none of them is given (or they
    #are both negative), the overall vertex connectivity is returned.
    
    print "\t[7] Radius\n" #No funciona amb xarxes inconectes
    #The radius of a graph is defined as the minimum eccentricity of its vertices
    #The eccentricity of a vertex is calculated by measuring the shortest distance from (or to) the vertex, to (or from) all other
    #vertices in the graph, and taking the maximum.
    
    
    print "\tParametres per cada node de la xarxa: \n"
    print "\t[11] Betweenness\n"
    #Mostra per cada node el seu valor de betweenness, gran = centric.
    print "\t[12] Pagerank\n"
    #Calcula algorisme de google, es una variant del eigenvector centratily, canviar posicio
    print "\t[13] EigenVectorCentrality\n"
    #It assigns relative scores to all nodes in the network based on the concept that connections to high-scoring nodes
    #contribute more to the score of the node in question than equal connections to low-scoring nodes.
    print "\t[14] Average degree connectivity\n"
    #The average degree connectivity is the average nearest neighbor degree ofnodes with degree k. 
    print "\t[15] Periphery\n" #No funciona amb xarxes inconectes
    #The periphery is the set of nodes with eccentricity equal to the diameter. 
    print "\t[16] Eccentricity\n"#No funciona amb xarxes inconectes
    #The eccentricity of a node v is the maximum distance from v to all other nodes in G.
    
    print "\t[17] Center nodes\n"#No funciona amb xarxes inconectes
    #The center is the set of nodes with eccentricity equal to radius. 

    print "\t[18] Degree Distribution\n"
    #Grau de distribucio del graf, distribucio de probabilitat de connexions en tota la xarxa
    #the degree of a node in a network is the number of connections it has to other nodes and the degree distribution is
    #the probability distribution of these degrees over the whole network.
    print "\t[19] Count the number of Motif\n"
    #Troba el numero de motifs, que son agrupacions de nodes, de mida tres o quatre (per defecte 3).
    #It is argued that the motif profile (ie. the number of different motifs in the graph)
    #is characteristic for different types of networks and network function is related to the motifs in the graph.
    #
    #S. Wernicke and F. Rasche: FANMOD: a tool for fast network motif detection, Bioinformatics 22(9), 1152--1153, 2006.
    #
    #Counts the total number of motifs in the graph
    #Motifs are small subgraphs of a given structure in a graph.
    #This function counts the total number of motifs in a graph without
    #assigning isomorphism classes to them.
    print "\t[20] Similarity Jaccard\n"
    #The Jaccard similarity coefficient of two vertices is the number of their
    #common neighbors divided by the number of vertices that are adjacent to
    #at least one of them.
    
    se = raw_input('Escriu els numeros dels parametres que vulguis amb una coma entre mig:\n')
    secom = se.split(',')
    
    for i in range(len(secom)):
        if secom[i] == "1":
                print "\nBetweenness Centralization:"
                p1 = betweenness_centralization(G)
                print p1
                param.append("Betweenness Centralization")
                param.append(p1)
        elif secom[i] == "2":
                print "\nAverage path length:"
                p2 = G.average_path_length()            
                print p2
                param.append("Average path length")
                param.append(p2)
        elif secom[i] == "3":
                print "\nAssortativity degree:"
                p3 = G.assortativity_degree()            
                print p3
                param.append("Assortativity degree")
                param.append(p3)
        elif secom[i] == "4":
                print "\nDiameter:"
                p4 = G.diameter()          
                print p4
                param.append("Diameter")
                param.append(p4)
        elif secom[i] == "5":
                print "\nDensity:"
                p5 = G.density()            
                print p5
                param.append("Density")
                param.append(p5)
        elif secom[i] == "6":
                print "\nCohesion:"
                p6 = G.cohesion()            
                print p6
                param.append("Cohesion")
                param.append(p6)
        elif secom[i] == "7":
                print("\nRadius:")
                p7 = nx.radius(nG)
                print p7
                param.append("Radius")
                param.append(p7)
    ###################################
        elif secom[i] == "11":
                print "\nBetweenness:"
                p11 = G.betweenness(directed=False, cutoff=16)            
                print p11
                param.append("Betweenness")
                param.append(p11)
        elif secom[i] == "12":
                print "\nPagerank:"
                p12 = G.pagerank()           
                print p12
                param.append("Pagerank")
                param.append(p12)
        elif secom[i] == "13":
                print "\nEigenVectorCentrality:"
                p13 = G.eigenvector_centrality()            
                print p13
                param.append("EigenVectorCentrality")
                param.append(p13)
        elif secom[i] == "14":
                print "\nAverage degree connectivity:"
                p14 = nx.average_degree_connectivity(nG)            
                print p14
                param.append("Average degree connectivity")
                param.append(p14)
        elif secom[i] == "15":
                print "\nPeriphery:"
                p15 = nx.periphery(nG)            
                print p15
                param.append("Periphery")
                param.append(p15)
        elif secom[i] == "16":
                print("\nEccentricity:")
                p16 = nx.eccentricity(nG)            
                print p16
                param.append("Eccentricity")
                param.append(p16)
        elif secom[i] == "17":
                print("\nCenter:")
                p17 = nx.center(nG)            
                print p17
                param.append("Center")
                param.append(p17)
        elif secom[i] == "18":
                print("\nDegree Distribution:")
                p18 = G.degree_distribution()    
                print p18
                param.append("Degree Distribution")
                param.append(p18)
        elif secom[i] == "19":
                print("\nTotal number of Motif:")
                p19 = G.motifs_randesu_no()
                print p19
                param.append("Total number of Motif")
                param.append(p19)
        elif secom[i] == "20":
                print("\nSimilarity Jaccard:")
                p20 = G.similarity_jaccard()
                print p20
                param.append("Similarity Jaccard")
                param.append(p20)
        else:
            print "Paramatre incorrecte"
            return
    #####Parametres exclosos#####
    #print "Assortativity meu:" # es el mateix que el degree?
    #print assortativitymeu(G)  
    #print("diameter: %d" % nx.diameter(nG))
    #print("density: %s" % nx.density(nG))
    #print("richclub coefficient: %s" % nx.rich_club_coefficient(nG.to_undirected()))
    #print("richclub coefficient: %s" % nx.rich_club_coefficient(nG))
    
    return param
Exemple #52
0
    usair97 = True

    if uk12 is True:
        # Simulacao do grafo UK12, com k = 2
        data['uk12']['a'] = {}
        data['uk12']['b'] = {}
        data['uk12']['lbl'] = []

        data['uk12']['grafo'] = load_graph(data['uk12']['dist'],data['uk12']['name'])

        # Lista com os nomes dos vertices
        for i in data['uk12']['grafo'].nodes(data=True):
            data['uk12']['lbl'].append(i[1]['name'])

        # Usaremos a periferia para obter duas sementes espalhadas
        data['uk12']['a']['seeds'] = nx.periphery(data['uk12']['grafo'])

        # Usaremos um vertice aleatorio e um de seus vizinhos como sementes proximas uma da outra
        random_v = np.random.randint(0, len(data['uk12']['grafo'].nodes()))

        data['uk12']['b']['seeds'] = ([random_v,
                     data['uk12']['grafo'].neighbors(random_v)
                     [np.random.randint(0, len(data['uk12']['grafo'].neighbors(random_v)))]])

        # Executando o Dijkstra nas sementes
        data['uk12']['a']['lambda'], \
        data['uk12']['a']['pi'],\
        data['uk12']['a']['h'] = dijkstra(data['uk12']['grafo'], data['uk12']['a']['seeds'])

        data['uk12']['b']['lambda'], \
        data['uk12']['b']['pi'], \
Exemple #53
0
def extended_stats(G, connectivity=False, anc=False, ecc=False, bc=False, cc=False):
    """
    Calculate extended topological stats and metrics for a graph.

    Many of these algorithms have an inherently high time complexity. Global
    topological analysis of large complex networks is extremely time consuming
    and may exhaust computer memory. Consider using function arguments to not
    run metrics that require computation of a full matrix of paths if they
    will not be needed.

    Parameters
    ----------
    G : networkx multidigraph
    connectivity : bool
        if True, calculate node and edge connectivity
    anc : bool
        if True, calculate average node connectivity
    ecc : bool
        if True, calculate shortest paths, eccentricity, and topological metrics
        that use eccentricity
    bc : bool
        if True, calculate node betweenness centrality
    cc : bool
        if True, calculate node closeness centrality

    Returns
    -------
    stats : dict
        dictionary of network measures containing the following elements (some
        only calculated/returned optionally, based on passed parameters):

          - avg_neighbor_degree
          - avg_neighbor_degree_avg
          - avg_weighted_neighbor_degree
          - avg_weighted_neighbor_degree_avg
          - degree_centrality
          - degree_centrality_avg
          - clustering_coefficient
          - clustering_coefficient_avg
          - clustering_coefficient_weighted
          - clustering_coefficient_weighted_avg
          - pagerank
          - pagerank_max_node
          - pagerank_max
          - pagerank_min_node
          - pagerank_min
          - node_connectivity
          - node_connectivity_avg
          - edge_connectivity
          - eccentricity
          - diameter
          - radius
          - center
          - periphery
          - closeness_centrality
          - closeness_centrality_avg
          - betweenness_centrality
          - betweenness_centrality_avg

    """

    stats = {}
    full_start_time = time.time()

    # create a DiGraph from the MultiDiGraph, for those metrics that require it
    G_dir = nx.DiGraph(G)

    # create an undirected Graph from the MultiDiGraph, for those metrics that
    # require it
    G_undir = nx.Graph(G)

    # get the largest strongly connected component, for those metrics that
    # require strongly connected graphs
    G_strong = get_largest_component(G, strongly=True)

    # average degree of the neighborhood of each node, and average for the graph
    avg_neighbor_degree = nx.average_neighbor_degree(G)
    stats['avg_neighbor_degree'] = avg_neighbor_degree
    stats['avg_neighbor_degree_avg'] = sum(avg_neighbor_degree.values())/len(avg_neighbor_degree)

    # average weighted degree of the neighborhood of each node, and average for
    # the graph
    avg_weighted_neighbor_degree = nx.average_neighbor_degree(G, weight='length')
    stats['avg_weighted_neighbor_degree'] = avg_weighted_neighbor_degree
    stats['avg_weighted_neighbor_degree_avg'] = sum(avg_weighted_neighbor_degree.values())/len(avg_weighted_neighbor_degree)

    # degree centrality for a node is the fraction of nodes it is connected to
    degree_centrality = nx.degree_centrality(G)
    stats['degree_centrality'] = degree_centrality
    stats['degree_centrality_avg'] = sum(degree_centrality.values())/len(degree_centrality)

    # calculate clustering coefficient for the nodes
    stats['clustering_coefficient'] = nx.clustering(G_undir)

    # average clustering coefficient for the graph
    stats['clustering_coefficient_avg'] = nx.average_clustering(G_undir)

    # calculate weighted clustering coefficient for the nodes
    stats['clustering_coefficient_weighted'] = nx.clustering(G_undir, weight='length')

    # average clustering coefficient (weighted) for the graph
    stats['clustering_coefficient_weighted_avg'] = nx.average_clustering(G_undir, weight='length')

    # pagerank: a ranking of the nodes in the graph based on the structure of
    # the incoming links
    pagerank = nx.pagerank(G_dir, weight='length')
    stats['pagerank'] = pagerank

    # node with the highest page rank, and its value
    pagerank_max_node = max(pagerank, key=lambda x: pagerank[x])
    stats['pagerank_max_node'] = pagerank_max_node
    stats['pagerank_max'] = pagerank[pagerank_max_node]

    # node with the lowest page rank, and its value
    pagerank_min_node = min(pagerank, key=lambda x: pagerank[x])
    stats['pagerank_min_node'] = pagerank_min_node
    stats['pagerank_min'] = pagerank[pagerank_min_node]

    # if True, calculate node and edge connectivity
    if connectivity:
        start_time = time.time()

        # node connectivity is the minimum number of nodes that must be removed
        # to disconnect G or render it trivial
        stats['node_connectivity'] = nx.node_connectivity(G_strong)

        # edge connectivity is equal to the minimum number of edges that must be
        # removed to disconnect G or render it trivial
        stats['edge_connectivity'] = nx.edge_connectivity(G_strong)
        log('Calculated node and edge connectivity in {:,.2f} seconds'.format(time.time() - start_time))

    # if True, calculate average node connectivity
    if anc:
        # mean number of internally node-disjoint paths between each pair of
        # nodes in G, i.e., the expected number of nodes that must be removed to
        # disconnect a randomly selected pair of non-adjacent nodes
        start_time = time.time()
        stats['node_connectivity_avg'] = nx.average_node_connectivity(G)
        log('Calculated average node connectivity in {:,.2f} seconds'.format(time.time() - start_time))

    # if True, calculate shortest paths, eccentricity, and topological metrics
    # that use eccentricity
    if ecc:
        # precompute shortest paths between all nodes for eccentricity-based
        # stats
        start_time = time.time()
        sp = {source:dict(nx.single_source_dijkstra_path_length(G_strong, source, weight='length')) for source in G_strong.nodes()}

        log('Calculated shortest path lengths in {:,.2f} seconds'.format(time.time() - start_time))

        # eccentricity of a node v is the maximum distance from v to all other
        # nodes in G
        eccentricity = nx.eccentricity(G_strong, sp=sp)
        stats['eccentricity'] = eccentricity

        # diameter is the maximum eccentricity
        diameter = nx.diameter(G_strong, e=eccentricity)
        stats['diameter'] = diameter

        # radius is the minimum eccentricity
        radius = nx.radius(G_strong, e=eccentricity)
        stats['radius'] = radius

        # center is the set of nodes with eccentricity equal to radius
        center = nx.center(G_strong, e=eccentricity)
        stats['center'] = center

        # periphery is the set of nodes with eccentricity equal to the diameter
        periphery = nx.periphery(G_strong, e=eccentricity)
        stats['periphery'] = periphery

    # if True, calculate node closeness centrality
    if cc:
        # closeness centrality of a node is the reciprocal of the sum of the
        # shortest path distances from u to all other nodes
        start_time = time.time()
        closeness_centrality = nx.closeness_centrality(G, distance='length')
        stats['closeness_centrality'] = closeness_centrality
        stats['closeness_centrality_avg'] = sum(closeness_centrality.values())/len(closeness_centrality)
        log('Calculated closeness centrality in {:,.2f} seconds'.format(time.time() - start_time))

    # if True, calculate node betweenness centrality
    if bc:
        # betweenness centrality of a node is the sum of the fraction of
        # all-pairs shortest paths that pass through node
        start_time = time.time()
        betweenness_centrality = nx.betweenness_centrality(G, weight='length')
        stats['betweenness_centrality'] = betweenness_centrality
        stats['betweenness_centrality_avg'] = sum(betweenness_centrality.values())/len(betweenness_centrality)
        log('Calculated betweenness centrality in {:,.2f} seconds'.format(time.time() - start_time))

    log('Calculated extended stats in {:,.2f} seconds'.format(time.time()-full_start_time))
    return stats
 def periphery(self): #f-28
     try:
         return len(nx.periphery(self.ng))/float(self.N)
     except:
         return 0