Ejemplo n.º 1
0
def weighted_edge_betweenness_centrality(graph):
    """weighted_edge_betweenness_centrality"""
    if graph.edges:
        return list(
            centrality.edge_betweenness_centrality(graph,
                                                   weight="weight").values())
    return [np.nan]
Ejemplo n.º 2
0
def top_edges(G, k=3):
    """
        Returns the top k edges for various
        centrality measures: betweenness.
        
        Args:
            G (nx.Graph): graph for which the 
                top nodes must be determined.
            
            k (int): number of top edges to return.
                if set to -ve, all the edges will be
                returned.
            
        Returns:
            res_dict (dict): dictionary of each centrality
                measure with list of top k edges in that 
                measure as values to the dictionary.
    """
    # number of all pair shortest paths that pass through each edge
    edge_btw_dict = centrality.edge_betweenness_centrality(G)

    # sort by nodes by each centrality measure
    top_k_btw_edges = sorted(edge_btw_dict.items(), key=lambda x: -x[1])

    # pick the top k edges
    if k > 0:
        res = top_k_btw_edges[:k]
    else:
        res = top_k_btw_edges

    # format and return the top nodes for each centrality
    res_dict = dict()
    res_dict["edge_betweeness"] = list(zip(*res))[0]
    return res_dict
Ejemplo n.º 3
0
def cut_by_highest_betweenness_centrality(g):
    # order graph node by betweenness_centrality
    highest_centrality_edge = pd.Series(
        edge_betweenness_centrality(g)).sort_values(ascending=False).index[0]
    _g = g.copy()
    _g.remove_edge(*highest_centrality_edge)
    left_tree, right_tree = nx.connected_component_subgraphs(_g)
    return left_tree, right_tree, highest_centrality_edge
Ejemplo n.º 4
0
def calc_edge_based_centrality(edge_index, centrality='betweenness'):
    adj_list = edge_index.numpy().T
    G = nx.Graph()
    G.add_edges_from(adj_list)
    if centrality == 'betweenness':
        edges_centrality = edge_betweenness_centrality(G)
    elif centrality == 'load':
        edges_centrality = edge_load_centrality(G)
    else:
        print(centrality, "is not defined")
        exit(1)
    return edges_centrality
Ejemplo n.º 5
0
def get_edge_betweenness_centrality(G, **kwargs):
    """Returns a dictionary of edge betweenness centrality values.
    """

    # Calculate the edge betweenness centrality values
    centrality_dict = \
        nxc.edge_betweenness_centrality(\
            G = G,
            normalized = kwargs["normalized"],
            weight = kwargs["weight"])

    # Reorder the edge names and return the reordered dictionary
    # of centrality values
    return reorder_edge_names(centrality_dict)
Ejemplo n.º 6
0
def most_between(graph: Graph):
    """
    Returns the tree by selecting edges of increasing betweeness centrality which do not create cycles (Kruskal).
    :param graph: graph on which to build tree
    :return: tree with betweeness nodes
    """

    # def add_neighbors(U):
    #     """
    #     Adds all neighbors of node in graph to q.
    #     :param U: node whose neighbors we add
    #     """
    #     for V in graph[U]:
    #         if (U, V) in betweenness:
    #             if not nx.has_path(new, U, V):
    #                 q.put((-betweenness[U, V], U, V))
    #             continue
    #         if (V, U) in betweenness:
    #             if not nx.has_path(new, U, V):
    #                 q.put((-betweenness[V, U], U, V))
    #             continue

    new = Graph()
    new.add_nodes_from(graph.nodes)

    betweenness = edge_betweenness_centrality(graph)
    q = PriorityQueue()
    for u, v in graph.edges:
        if (u, v) in betweenness:
            q.put((-betweenness[u, v], u, v))
            continue
        if (v, u) in betweenness:
            q.put((-betweenness[v, u], v, u))
            continue

    # for u in nodes:
    #     add_neighbors(u)

    while not nx.is_connected(new):
        bet, u, v = q.get()
        if not nx.has_path(new, u, v):
            new.add_edge(u, v, weight=graph[u][v]['weight'])
            # add_neighbors(u)

    return new
Ejemplo n.º 7
0
def compute_girvan_clusters(clusters, threshold=0.2):
    G = nx.Graph()
    G.add_nodes_from(range(len(clusters)))
    for i in range(len(clusters) - 1):
        for j in range(i + 1, len(clusters)):
            if (compute_jaccard_coef(clusters[i][0][2], clusters[j][0][2]) >=
                    threshold):
                G.add_edge(i, j)
    nx.draw(G, with_labels=True)
    plt.show()
    tot_clusters = len(list(nx.connected_components(G)))
    while (tot_clusters < 9):
        centrality_measures = edge_betweenness_centrality(G)
        rem_edge = max(centrality_measures.items(),
                       key=operator.itemgetter(1))[0]
        G.remove_edge(rem_edge[0], rem_edge[1])
        tot_clusters = len(list(nx.connected_components(G)))
    girvan_clusters = [list(c) for c in nx.connected_component_subgraphs(G)]
    nx.draw(G, with_labels=True)
    plt.show()
    return girvan_clusters
Ejemplo n.º 8
0
    def __init__(self, area, output, centrality=True,
                 useful_tags_path=None):
        """
        The class gets a name of polygon and saves shapefile which includes pedestrian network within
        the polygon extent
        :param area: the place polygon to get the walk network from
        :param output: where to store the network as shapefile
        :param centrality: add centrality indices to edges
        :param useful_tags_path: tags to download from OSM
        """
        # define the tags that should be downloaded with the network
        if useful_tags_path is None:
            useful_tags_path = ['highway', 'handrail', 'footway', 'crossing', 'sidewalk']
            useful_tags_path.extend(list(accessibility_dic.keys()))
        ox.utils.config(useful_tags_way=useful_tags_path)

        #  downloaded the graph based on the specified location and make undirected it project it
        print('download data - {}'.format(datetime.now()))
        if isinstance(area, str):
            graph = ox.graph_from_place(area, network_type='walk')
        else:
            graph = ox.graph_from_polygon(area, network_type='walk')

        # make the graph undirected graph then project  the graph
        self.graph_pr = ox.project_graph(graph)
        self.graph_pr = self.graph_pr.to_undirected()

        if centrality:
            # Calculate betweenness/choice and closeness/integration.
            # to work properly the algorithm work on graph. to run
            # graph_to_gdfs later the code convert it back to MultiGraph.
            print('calculate integration at {}'.format(datetime.now()))
            self.graph_pr = nx.Graph(self.graph_pr)
            # line_graph convert graph to line graph so edges become nodes
            edge_centrality = nx.closeness_centrality(nx.line_graph(self.graph_pr))
            nx.set_edge_attributes(self.graph_pr, edge_centrality, 'integ')

            print("calculate choice- {}".format(datetime.now()))
            dic = edge_betweenness_centrality(self.graph_pr)
            nx.set_edge_attributes(self.graph_pr, dic, 'choice')

            self.graph_pr = nx.MultiGraph(self.graph_pr)

        # from graph to geodataframe , save the point dateframe for later use
        gdf_format = ox.graph_to_gdfs(self.graph_pr)
        gdf_format[0].to_crs(epsg=3857).to_file('pnt_' + output)
        # create dataframe with the necessary columns and convert to shapefile
        edges = gdf_format[1]
        self.columns = [value for value in useful_tags_path if value in list(edges.columns)]
        edges_new = edges.apply(lambda row: self.list_to_str(row), axis=1)

        edges_new.crs = edges.crs
        self.columns.append('geometry')
        if centrality:
            self.columns.append('choice')
            self.columns.append('integ')
        edges_shp = edges_new.loc[:, self.columns]
        # rename long name (accessibility tags)
        edges_shp = get_acc_tags_shp(edges_shp)
        # project the file to compatible coordinate system
        edges_shp.to_crs(epsg=3857).to_file(output)
Ejemplo n.º 9
0
def edge_betweenness_centrality(graph):
    """edge_betweenness_centrality"""
    if graph.edges:
        return list(centrality.edge_betweenness_centrality(graph).values())
    return [np.nan]
Ejemplo n.º 10
0
    def get_metric_from_graph(self,
                              metric=None,
                              nedges=None,
                              keyword=None,
                              graph=None,
                              month=None):

        #'''this func will do most of the work. lets you get a named metric for nodes, optionally restricting this by month, by specified nodes, or by entity type ie lobby/staffer/lobbyist/commissioner. first constructs a cache key and then looks in the cache

        ck = str(metric) + str(month) + str(keyword)
        if ck in self.cache:
            return self.cache[ck]

        g = graph
        if keyword:
            nedges = [
                node for node in g.nodes_iter(data=True)
                if node[1]['type'] == keyword
            ]
        #'''if a keyword search is specified, we list the nodes where that keyword is found in one of its attributes'''

        if metric == u'Degree':
            upshot = self.degree(g, nedges)

        if metric == u'Gatekeepership':
            upshot = self.gatekeeper(g, nedges)

        if metric == u'Closeness Centrality':
            u = centrality.closeness_centrality(g, normalized=True)
            if nedges:
                filter_list = [n[0] for n in nedges]
                upshot = {
                    g.node[k]['name']: v
                    for k, v in u.items() if k in filter_list
                }
            else:
                upshot = {g.node[k]['name']: v for k, v in u.items()}

        if metric == u'Betweenness':
            u = centrality.betweenness_centrality(g,
                                                  weight='weight',
                                                  normalized=True)
            if nedges:
                filter_list = [n[0] for n in nedges]
                upshot = {
                    g.node[k]['name']: v
                    for k, v in u.items() if k in filter_list
                }
            else:
                upshot = {g.node[k]['name']: v for k, v in u.items()}

        if metric == u'Greedy_Fragile':
            upshot = self.greedy_fragile(g, nedges)

        if metric == u'Link Centrality':
            u = centrality.edge_betweenness_centrality(g,
                                                       weight='weight',
                                                       normalized=True)
            upshot = {}
            for k, v in u.items(
            ):  # doing it in a similar way to the other linkwise metric below.
                a, b = k
                c = g.node[a]['name']
                d = g.node[b]['name']
                if nedges:
                    filter_list = [n[0] for n in nedges]
                    if a in filter_list or b in filter_list:
                        upshot[unicode(c + ' - ' + d)] = v
                else:
                    upshot[unicode(a + ' - ' + b)] = v

        if metric == u'Predicted Links':
            gr = self.make_unigraph_from_multigraph(mg=g)
            u = link_prediction.resource_allocation_index(gr)
            upshot = {}
            for k, v, p in u:
                if p > 0:  #RAI examines all nonexistent edges in graph and will return all of them, including ones with a zero index. we therefore filter for positive index values.
                    a = g.node[k]['name']
                    b = g.node[v]['name']
                    if nedges:
                        filter_list = [n[0] for n in nedges]
                        if k in filter_list or v in filter_list:
                            upshot[unicode(a + ' - ' + b)] = p
                    else:
                        upshot[unicode(a + ' - ' + b)] = p
        self.cacheflow(ck, data=upshot)
        return upshot
Ejemplo n.º 11
0
fig, ax = plt.subplots()
nx.draw(
    G,
    pos=coords,
    cmap=plt.get_cmap(
        'Greens'
    ),  # https://matplotlib.org/stable/tutorials/colors/colormaps.html
    node_color=[rank[v] for v in G.nodes()],
    **opt)
ax.set_facecolor('black')
fig.set_facecolor('black')
ax.axis('off')
plt.savefig('rank.png')

centrality = edge_betweenness_centrality(G)
print(list(centrality.items())[:3])

# normalize to [0, 1]
low = min(centrality.values())
high = max(centrality.values())
span = high - low
weight = [(centrality[e] - low) / span for e in G.edges()]

del opt['width']  # discard the constants
width = 10  # set a maximum
del opt['edge_color']
fig, ax = plt.subplots()
nx.draw(G,
        pos=coords,
        cmap=plt.get_cmap('Greens'),
Ejemplo n.º 12
0
	def get_metric_from_graph(self, metric=None, nedges=None, keyword=None, graph=None, month=None):

	#'''this func will do most of the work. lets you get a named metric for nodes, optionally restricting this by month, by specified nodes, or by entity type ie lobby/staffer/lobbyist/commissioner. first constructs a cache key and then looks in the cache

		ck = str(metric) + str(month) + str(keyword)
		if ck in self.cache:
			return self.cache[ck]

		g = graph
		if keyword:
			nedges = [node for node in g.nodes_iter(data=True) if node[1]['type'] == keyword]
		#'''if a keyword search is specified, we list the nodes where that keyword is found in one of its attributes'''
		
		if metric == u'Degree':		
			upshot = self.degree(g, nedges)
		
		if metric == u'Gatekeepership':
			upshot = self.gatekeeper(g, nedges)

		if metric == u'Closeness Centrality':
			u = centrality.closeness_centrality(g, normalized=True)
			if nedges:
				filter_list = [n[0] for n in nedges]
				upshot = {g.node[k]['name']: v for k,v in u.items() if k in filter_list}
			else:
				upshot = {g.node[k]['name']: v for k,v in u.items()}

		if metric == u'Betweenness':
                        u = centrality.betweenness_centrality(g, weight='weight', normalized=True)	
			if nedges:
				filter_list = [n[0] for n in nedges]
				upshot = {g.node[k]['name']: v for k,v in u.items() if k in filter_list}
			else:
				upshot = {g.node[k]['name']: v for k,v in u.items()}

		if metric == u'Greedy_Fragile':
			upshot = self.greedy_fragile(g, nedges)

		if metric == u'Link Centrality':
			u = centrality.edge_betweenness_centrality(g, weight='weight', normalized=True)
			upshot = {}
			for k, v in u.items(): # doing it in a similar way to the other linkwise metric below.
				a, b = k
				c = g.node[a]['name']
				d = g.node[b]['name'] 
				if nedges:
					filter_list = [n[0] for n in nedges]
					if a in filter_list or b in filter_list:
						upshot[unicode(c + ' - ' + d)] = v
				else:
					upshot[unicode(a + ' - ' + b)] = v

		if metric == u'Predicted Links':
			gr = self.make_unigraph_from_multigraph(mg=g)
			u = link_prediction.resource_allocation_index(gr)
			upshot = {}
			for k, v, p in u:
				if p > 0: #RAI examines all nonexistent edges in graph and will return all of them, including ones with a zero index. we therefore filter for positive index values.
					a = g.node[k]['name']
					b = g.node[v]['name']
					if nedges:
						filter_list = [n[0] for n in nedges]
						if k in filter_list or v in filter_list:
							upshot[unicode(a + ' - ' + b)] = p
					else:
						upshot[unicode(a + ' - ' + b)] = p
		self.cacheflow(ck, data=upshot)
		return upshot