(
    graph,
    name="betweenness",
    mode="nodes",
    weight="mm_len",
    endpoints=True,
    radius=None,
    distance=None,
    normalized=False,
    verbose=True,
    **kwargs
)
return netx
Calculates the shortest-path betweenness centrality for nodes.

    Wrapper around ``networkx.betweenness_centrality`` or
    ``networkx.edge_betweenness_centrality``.

    Betweenness centrality of a node `v` is the sum of the
    fraction of all-pairs shortest paths that pass through `v`

    .. math::

       c_B(v) =\\sum_{s,t \\in V} \\frac{\\sigma(s, t|v)}{\\sigma(s, t)}

    where `V` is the set of nodes, :math:`\\sigma(s, t)` is the number of
    shortest :math:`(s, t)`-paths,  and :math:`\\sigma(s, t|v)` is the number of
    those paths  passing through some  node `v` other than `s, t`.
    If `s = t`, :math:`\\sigma(s, t) = 1`, and if `v` in `{s, t}``,
    :math:`\\sigma(s, t|v) = 0`.

    Betweenness centrality of an edge `e` is the sum of the
    fraction of all-pairs shortest paths that pass through `e`

    .. math::

       c_B(e) =\\sum_{s,t \\in V} \\frac{\\sigma(s, t|e)}{\\sigma(s, t)}

    where `V` is the set of nodes, :math:`\\sigma(s, t)` is the number of
    shortest :math:`(s, t)`-paths, and :math:`\\sigma(s, t|e)` is the number of
    those paths passing through edge `e`.

    Adapted from :cite:`porta2006`.

    Parameters
    ----------
    graph : networkx.Graph
        Graph representing street network.
        Ideally generated from GeoDataFrame using :func:`momepy.gdf_to_nx`
    name : str, optional
        calculated attribute name
    mode : str, default 'nodes'
        mode of betweenness calculation. 'node' for node-based, 'edges' for edge-based
    weight : str (default 'mm_len')
        attribute holding the weight of edge (e.g. length, angle)
    radius: int
        Include all neighbors of distance <= radius from n
    distance : str, optional
        Use specified edge data key as distance.
        For example, setting ``distance=’weight’`` will use the edge ``weight`` to
        measure the distance from the node n during ego_graph generation.
    normalized : bool, optional
        If True the betweenness values are normalized by `2/((n-1)(n-2))`,
        where n is the number of nodes in subgraph.
    verbose : bool (default True)
        if True, shows progress bars in loops and indication of steps
    **kwargs
        kwargs for ``networkx.betweenness_centrality`` or
        ``networkx.edge_betweenness_centrality``

    Returns
    -------
    Graph
        networkx.Graph

    Examples
    --------
    >>> network_graph = mm.betweenness_centrality(network_graph)

    Notes
    -----
    In case of angular betweenness, implementation is based on "Tasos Implementation".
Calculates the shortest-path betweenness centrality for nodes.
Calculates the shortest - path betweenness centrality for nodes .
def betweenness_centrality(
    graph,
    name="betweenness",
    mode="nodes",
    weight="mm_len",
    endpoints=True,
    radius=None,
    distance=None,
    normalized=False,
    verbose=True,
    **kwargs
):
    """
    Calculates the shortest-path betweenness centrality for nodes.

    Wrapper around ``networkx.betweenness_centrality`` or
    ``networkx.edge_betweenness_centrality``.

    Betweenness centrality of a node `v` is the sum of the
    fraction of all-pairs shortest paths that pass through `v`

    .. math::

       c_B(v) =\\sum_{s,t \\in V} \\frac{\\sigma(s, t|v)}{\\sigma(s, t)}

    where `V` is the set of nodes, :math:`\\sigma(s, t)` is the number of
    shortest :math:`(s, t)`-paths,  and :math:`\\sigma(s, t|v)` is the number of
    those paths  passing through some  node `v` other than `s, t`.
    If `s = t`, :math:`\\sigma(s, t) = 1`, and if `v` in `{s, t}``,
    :math:`\\sigma(s, t|v) = 0`.

    Betweenness centrality of an edge `e` is the sum of the
    fraction of all-pairs shortest paths that pass through `e`

    .. math::

       c_B(e) =\\sum_{s,t \\in V} \\frac{\\sigma(s, t|e)}{\\sigma(s, t)}

    where `V` is the set of nodes, :math:`\\sigma(s, t)` is the number of
    shortest :math:`(s, t)`-paths, and :math:`\\sigma(s, t|e)` is the number of
    those paths passing through edge `e`.

    Adapted from :cite:`porta2006`.

    Parameters
    ----------
    graph : networkx.Graph
        Graph representing street network.
        Ideally generated from GeoDataFrame using :func:`momepy.gdf_to_nx`
    name : str, optional
        calculated attribute name
    mode : str, default 'nodes'
        mode of betweenness calculation. 'node' for node-based, 'edges' for edge-based
    weight : str (default 'mm_len')
        attribute holding the weight of edge (e.g. length, angle)
    radius: int
        Include all neighbors of distance <= radius from n
    distance : str, optional
        Use specified edge data key as distance.
        For example, setting ``distance=’weight’`` will use the edge ``weight`` to
        measure the distance from the node n during ego_graph generation.
    normalized : bool, optional
        If True the betweenness values are normalized by `2/((n-1)(n-2))`,
        where n is the number of nodes in subgraph.
    verbose : bool (default True)
        if True, shows progress bars in loops and indication of steps
    **kwargs
        kwargs for ``networkx.betweenness_centrality`` or
        ``networkx.edge_betweenness_centrality``

    Returns
    -------
    Graph
        networkx.Graph

    Examples
    --------
    >>> network_graph = mm.betweenness_centrality(network_graph)

    Notes
    -----
    In case of angular betweenness, implementation is based on "Tasos Implementation".
    """
    netx = graph.copy()

    # has to be Graph not MultiGraph as MG is not supported by networkx2.4
    G = nx.Graph()
    for u, v, k, data in netx.edges(data=True, keys=True):
        if G.has_edge(u, v):
            if G[u][v][weight] > netx[u][v][k][weight]:
                nx.set_edge_attributes(G, {(u, v): data})
        else:
            G.add_edge(u, v, **data)

    if radius:
        for n in tqdm(G, total=len(G), disable=not verbose):
            sub = nx.ego_graph(
                G, n, radius=radius, distance=distance
            )  # define subgraph of steps=radius
            netx.nodes[n][name] = nx.betweenness_centrality(
                sub, weight=weight, normalized=normalized, **kwargs
            )[n]

    elif mode == "nodes":
        vals = nx.betweenness_centrality(
            G, weight=weight, endpoints=endpoints, **kwargs
        )
        nx.set_node_attributes(netx, vals, name)
    elif mode == "edges":
        vals = nx.edge_betweenness_centrality(G, weight=weight, **kwargs)
        for u, v, k in netx.edges(keys=True):
            try:
                val = vals[u, v]
            except KeyError:
                val = vals[v, u]
            netx[u][v][k][name] = val
    else:
        raise ValueError(
            "Mode {} is not supported. Use 'nodes' or 'edges'.".format(mode)
        )

    return netx
def betweenness_centrality ( graph , name = "betweenness" , mode = "nodes" , weight = "mm_len" , endpoints = True , radius = None , distance = None , normalized = False , verbose = True , * * kwargs ) : netx = graph . copy ( ) # has to be Graph not MultiGraph as MG is not supported by networkx2.4 G = nx . Graph ( ) for u , v , k , data in netx . edges ( data = True , keys = True ) : if G . has_edge ( u , v ) : if G [ u ] [ v ] [ weight ] > netx [ u ] [ v ] [ k ] [ weight ] : nx . set_edge_attributes ( G , { ( u , v ) : data } ) else : G . add_edge ( u , v , * * data ) if radius : for n in tqdm ( G , total = len ( G ) , disable = not verbose ) : sub = nx . ego_graph ( G , n , radius = radius , distance = distance ) # define subgraph of steps=radius netx . nodes [ n ] [ name ] = nx . betweenness_centrality ( sub , weight = weight , normalized = normalized , * * kwargs ) [ n ] elif mode == "nodes" : vals = nx . betweenness_centrality ( G , weight = weight , endpoints = endpoints , * * kwargs ) nx . set_node_attributes ( netx , vals , name ) elif mode == "edges" : vals = nx . edge_betweenness_centrality ( G , weight = weight , * * kwargs ) for u , v , k in netx . edges ( keys = True ) : try : val = vals [ u , v ] except KeyError : val = vals [ v , u ] netx [ u ] [ v ] [ k ] [ name ] = val else : raise ValueError ( "Mode {} is not supported. Use 'nodes' or 'edges'." . format ( mode ) ) return netx