(
    graph,
    radius=5,
    distance=None,
    meshedness=True,
    cds_length=True,
    mode="sum",
    degree="degree",
    length="mm_len",
    mean_node_degree=True,
    proportion={3: True, 4: True, 0: True},
    cyclomatic=True,
    edge_node_ratio=True,
    gamma=True,
    local_closeness=True,
    closeness_weight=None,
    verbose=True,
)
return netx
Calculates all subgraph-based characters.

    Generating subgraph might be a time consuming activity. If we want to use the same
    subgraph for more characters, ``subgraph`` allows this by generating subgraph and
    then analysing it using selected options.


    Parameters
    ----------
    graph : networkx.Graph
        Graph representing street network.
        Ideally generated from GeoDataFrame using :func:`momepy.gdf_to_nx`
    radius: int
        radius defining the extent of subgraph
    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.
    meshedness : bool, default True
        Calculate meshedness (True/False)
    cds_length : bool, default True
        Calculate cul-de-sac length (True/False)
    mode : str (defualt 'sum')
        if ``'sum'``, calculate total cds_length, if ``'mean'`` calculate mean
        cds_length
    degree : str
        name of attribute of node degree (:py:func:`momepy.node_degree`)
    length : str, default `mm_len`
        name of attribute of segment length (geographical)
    mean_node_degree : bool, default True
        Calculate mean node degree (True/False)
    proportion : dict, default {3: True, 4: True, 0: True}
        Calculate proportion {3: True/False, 4: True/False, 0: True/False}
    cyclomatic : bool, default True
        Calculate cyclomatic complexity (True/False)
    edge_node_ratio : bool, default True
        Calculate edge node ratio (True/False)
    gamma : bool, default True
        Calculate gamma index (True/False)
    local_closeness : bool, default True
        Calculate local closeness centrality (True/False)
    closeness_weight : str, optional
      Use the specified edge attribute as the edge distance in shortest
      path calculations in closeness centrality algorithm
    verbose : bool (default True)
        if True, shows progress bars in loops and indication of steps


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

    Examples
    --------
    >>> network_graph = mm.subgraph(network_graph)
Calculates all subgraph-based characters.
Calculates all subgraph - based characters .
def subgraph(
    graph,
    radius=5,
    distance=None,
    meshedness=True,
    cds_length=True,
    mode="sum",
    degree="degree",
    length="mm_len",
    mean_node_degree=True,
    proportion={3: True, 4: True, 0: True},
    cyclomatic=True,
    edge_node_ratio=True,
    gamma=True,
    local_closeness=True,
    closeness_weight=None,
    verbose=True,
):
    """
    Calculates all subgraph-based characters.

    Generating subgraph might be a time consuming activity. If we want to use the same
    subgraph for more characters, ``subgraph`` allows this by generating subgraph and
    then analysing it using selected options.


    Parameters
    ----------
    graph : networkx.Graph
        Graph representing street network.
        Ideally generated from GeoDataFrame using :func:`momepy.gdf_to_nx`
    radius: int
        radius defining the extent of subgraph
    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.
    meshedness : bool, default True
        Calculate meshedness (True/False)
    cds_length : bool, default True
        Calculate cul-de-sac length (True/False)
    mode : str (defualt 'sum')
        if ``'sum'``, calculate total cds_length, if ``'mean'`` calculate mean
        cds_length
    degree : str
        name of attribute of node degree (:py:func:`momepy.node_degree`)
    length : str, default `mm_len`
        name of attribute of segment length (geographical)
    mean_node_degree : bool, default True
        Calculate mean node degree (True/False)
    proportion : dict, default {3: True, 4: True, 0: True}
        Calculate proportion {3: True/False, 4: True/False, 0: True/False}
    cyclomatic : bool, default True
        Calculate cyclomatic complexity (True/False)
    edge_node_ratio : bool, default True
        Calculate edge node ratio (True/False)
    gamma : bool, default True
        Calculate gamma index (True/False)
    local_closeness : bool, default True
        Calculate local closeness centrality (True/False)
    closeness_weight : str, optional
      Use the specified edge attribute as the edge distance in shortest
      path calculations in closeness centrality algorithm
    verbose : bool (default True)
        if True, shows progress bars in loops and indication of steps


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

    Examples
    --------
    >>> network_graph = mm.subgraph(network_graph)
    """

    netx = graph.copy()

    for n in tqdm(netx, total=len(netx), disable=not verbose):
        sub = nx.ego_graph(
            netx, n, radius=radius, distance=distance
        )  # define subgraph of steps=radius

        if meshedness:
            netx.nodes[n]["meshedness"] = _meshedness(sub)

        if cds_length:
            for u, v, k in netx.edges(keys=True):
                if netx.nodes[u][degree] == 1 or netx.nodes[v][degree] == 1:
                    netx[u][v][k]["cdsbool"] = True
                else:
                    netx[u][v][k]["cdsbool"] = False

            netx.nodes[n]["cds_length"] = _cds_length(sub, mode=mode, length=length)

        if mean_node_degree:
            netx.nodes[n]["mean_node_degree"] = _mean_node_degree(sub, degree=degree)

        if proportion:
            counts = _proportion(sub, degree=degree)
            if proportion[3]:
                netx.nodes[n]["proportion_3"] = counts[3] / len(sub)
            if proportion[4]:
                netx.nodes[n]["proportion_4"] = counts[4] / len(sub)
            if proportion[0]:
                netx.nodes[n]["proportion_0"] = counts[1] / len(sub)

        if cyclomatic:
            netx.nodes[n]["cyclomatic"] = _cyclomatic(sub)

        if edge_node_ratio:
            netx.nodes[n]["edge_node_ratio"] = _edge_node_ratio(sub)

        if gamma:
            netx.nodes[n]["gamma"] = _gamma(sub)

        if local_closeness:
            lengraph = len(netx)
            netx.nodes[n]["local_closeness"] = _closeness_centrality(
                sub, n, length=closeness_weight, len_graph=lengraph
            )

    return netx
def subgraph ( graph , radius = 5 , distance = None , meshedness = True , cds_length = True , mode = "sum" , degree = "degree" , length = "mm_len" , mean_node_degree = True , proportion = { 3 : True , 4 : True , 0 : True } , cyclomatic = True , edge_node_ratio = True , gamma = True , local_closeness = True , closeness_weight = None , verbose = True , ) : netx = graph . copy ( ) for n in tqdm ( netx , total = len ( netx ) , disable = not verbose ) : sub = nx . ego_graph ( netx , n , radius = radius , distance = distance ) # define subgraph of steps=radius if meshedness : netx . nodes [ n ] [ "meshedness" ] = _meshedness ( sub ) if cds_length : for u , v , k in netx . edges ( keys = True ) : if netx . nodes [ u ] [ degree ] == 1 or netx . nodes [ v ] [ degree ] == 1 : netx [ u ] [ v ] [ k ] [ "cdsbool" ] = True else : netx [ u ] [ v ] [ k ] [ "cdsbool" ] = False netx . nodes [ n ] [ "cds_length" ] = _cds_length ( sub , mode = mode , length = length ) if mean_node_degree : netx . nodes [ n ] [ "mean_node_degree" ] = _mean_node_degree ( sub , degree = degree ) if proportion : counts = _proportion ( sub , degree = degree ) if proportion [ 3 ] : netx . nodes [ n ] [ "proportion_3" ] = counts [ 3 ] / len ( sub ) if proportion [ 4 ] : netx . nodes [ n ] [ "proportion_4" ] = counts [ 4 ] / len ( sub ) if proportion [ 0 ] : netx . nodes [ n ] [ "proportion_0" ] = counts [ 1 ] / len ( sub ) if cyclomatic : netx . nodes [ n ] [ "cyclomatic" ] = _cyclomatic ( sub ) if edge_node_ratio : netx . nodes [ n ] [ "edge_node_ratio" ] = _edge_node_ratio ( sub ) if gamma : netx . nodes [ n ] [ "gamma" ] = _gamma ( sub ) if local_closeness : lengraph = len ( netx ) netx . nodes [ n ] [ "local_closeness" ] = _closeness_centrality ( sub , n , length = closeness_weight , len_graph = lengraph ) return netx