Skip to content

connected_components

Get the connected components of a spatial weights object.

Parameters:

Name Type Description Default
w W

The spatial weights object.

required
ids ndarray

The ids of the nodes in the weights object.

required

Returns:

Name Type Description
sub_graphs List[W]

The connected components of the graph.

Source code in src/histolytics/spatial_graph/utils.py
def get_connected_components(w: W, ids: np.ndarray) -> List[W]:
    """Get the connected components of a spatial weights object.

    Parameters:
        w (W):
            The spatial weights object.
        ids (np.ndarray):
            The ids of the nodes in the weights object.

    Returns:
        sub_graphs (List[W]):
            The connected components of the graph.
    """
    w_sub = w_subset(w, ids, silence_warnings=True)

    G = w_sub.to_networkx()
    sub_graphs = [
        W(nx.to_dict_of_lists(G.subgraph(c).copy()), silence_warnings=True)
        for c in nx.connected_components(G)
    ]

    return sub_graphs