How to use the networkx.connected_component_subgraphs function in networkx

To help you get started, we’ve selected a few networkx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github patverga / torch-relation-extraction / bin / process / util / LargestConnectedComponent.py View on Github external
g.add_edge(ep_str, rel_str)

    print 'Adding edges to graph'
    graph = nx.Graph()
    [process_line(cur_line, line_num, graph) for line_num, cur_line in enumerate(open(in_file, 'r'))]
    print('\nInitial graph contains ' + str(graph.number_of_nodes()) + ' nodes, '
          + str(graph.number_of_edges()) + ' edges')

    print 'Pruning nodes with degree < ' + str(min_degree_prune)
    remove = [node for node in graph.nodes_iter() if graph.degree(node) < min_degree_prune]

    graph.remove_nodes_from(remove)
    print 'Pruned ' + str(len(remove)) + ' nodes'

    print 'Finding largest connected component'
    largest_component = max(nx.connected_component_subgraphs(graph), key=len)
    print('Largest component contains ' + str(largest_component.number_of_nodes()) + ' nodes, '
          + str(largest_component.number_of_edges()) + ' edges')

    def export_line(line, l_num, f_out):
        if l_num % 100000 == 0:
            sys.stdout.write('\rline : ' + str(line_num / 1000) + 'k')
            sys.stdout.flush()
        if already_int_mapped:
            e1, e2, ep, rel, tokens, label = line.strip().split('\t')
            if largest_component.has_edge(int(ep), int(rel) * -1):
                f_out.write(line)
        else:
            e1_str, e2_str, rel_str, label = line.strip().split('\t')
            ep_str = e1_str + '\t' + e2_str
            if largest_component.has_edge(ep_str, rel_str):
                f_out.write(line)
github vitchyr / Research-in-Math / data_miner.py View on Github external
for j in range(1, 4):
            if j * n_steps / 4 == i:
                print(str(j * 25) + "% done")

        gc_n_nodes_list = []
        gc_diameter_list = []
        n_components_list = []

        for j in range(n_simulations):
            graph = networkx.gnm_random_graph(n_nodes, n_edges)

            for j in range(n_iterations):
                simulation.iterate(graph, I, model_no)

            components = networkx.connected_component_subgraphs(graph)
            gc_n_nodes_list.append(components[0].number_of_nodes())
            gc_diameter_list.append(networkx.diameter(components[0]))
            n_components_list.append(len(components))

        entry = Entry()
        entry.I = I

        entry.gc_n_nodes = float(sum(gc_n_nodes_list))/len(gc_n_nodes_list)
        entry.gc_diameter = float(sum(gc_diameter_list))/len(gc_diameter_list)
        entry.n_components = float(sum(n_components_list))/len(n_components_list)

        output1 += '%s\n' % entry
        I += I_step

    #Comparing iterations
    G = networkx.gnm_random_graph(n_nodes, n_edges)
github BuysDB / SingleCellMultiOmics / singlecellmultiomics / utils / bdbplot.py View on Github external
def layout(self):


        maxRadius = 100
        minDistance = 15 #Distance between the nodes

        components = []
        self.readLen = 0

        toRemove = []
        for nodeA,nodeB,d in self.g.edges_iter(data='hdist'):
            if d!=1:
                toRemove.append( (nodeA, nodeB) )
        self.g.remove_edges_from(toRemove)

        for componentIndex,connectedComponent in enumerate(nx.connected_component_subgraphs(self.g)):
            if len(connectedComponent)>3:
                #Find center(s)
                plot = BDBPlot()

                centerNode = None
                centerAbundance = 0
                for node in connectedComponent:
                    a = connectedComponent.node[node]['abundance']
                    if a > centerAbundance:
                        centerNode = node
                        centerAbundance = a
                    #Todo: compat for more centers
                    self.readLen = max(self.readLen, len(node))
                #Estimate radial size of connected component:
                #longest path from center to member
                radialSize = 1
github davide-belli / generative-graph-transformer / metrics / statistics.py View on Github external
def get_diameters(graph):
    r"""
    Compute histogram of connected components diameters for a graph.
    
    :param graph: graph representation in networkx format: nx.from_numpy_matrix(A)
    :return: list of connected components diameters
    """
    diams = []
    for g in nx.connected_component_subgraphs(graph):
        diams.append(nx.diameter(g))
    diams = list(filter(lambda a: a != 0, diams))
    return np.array(diams)
github SUNCAT-Center / CatKit / catkit / gen / analysis / classifier.py View on Github external
"""
        atoms = self.atoms.copy()

        # Remove the slab atoms
        ads_atoms = self.ads_atoms
        if ads_atoms is None:
            ads_atoms = self.id_adsorbate_atoms()

        if classifier == 'radial':
            con = utils.get_cutoff_neighbors(atoms)
            ads_con = con[ads_atoms][:, ads_atoms]
            G = nx.Graph()
            G.add_nodes_from(ads_atoms)
            edges = utils.connectivity_to_edges(ads_con, indices=ads_atoms)
            G.add_weighted_edges_from(edges, weight='bonds')
            SG = nx.connected_component_subgraphs(G)

            adsorbates = []
            for sg in SG:
                nodes = list(sg.nodes)
                if return_atoms:
                    edges = list(sg.edges)
                    ads = Gratoms(
                        numbers=atoms.numbers[nodes],
                        positions=atoms.positions[nodes],
                        edges=edges)
                    ads.center(vacuum=5)
                else:
                    ads = nodes
                adsorbates += [ads]

        return adsorbates
github StructuralNeurobiologyLab / SyConn / syconn / proc / glia_splitting.py View on Github external
def transform_rag_edgelist2pkl(rag):
    """
    Stores networkx graph as dictionary mapping (1) SSV IDs to lists of SV IDs
     and (2) SSV IDs to subgraphs (networkx)

    Parameters
    ----------
    rag : networkx.Graph
    """
    ccs = nx.connected_component_subgraphs(rag)
    cc_dict_graph = {}
    cc_dict = {}
    for cc in ccs:
        curr_cc = list(cc.nodes())
        min_ix = np.min(curr_cc)
        if min_ix in cc_dict:
            raise ValueError('Multiple SSV IDs')
        cc_dict_graph[min_ix] = cc
        cc_dict[min_ix] = curr_cc
    write_obj2pkl(global_params.config.working_dir + "/glia/cc_dict_rag_graphs.pkl",
                  cc_dict_graph)
    write_obj2pkl(global_params.config.working_dir + "/glia/cc_dict_rag.pkl", cc_dict)
github simonscheider / mapmatching / build / lib / mapmatcher / mapmatcher.py View on Github external
def getNetworkGraph(segments,segmentlengths):
    """
    Builds a networkx graph from the network file, inluding segment length taken from arcpy.
    It selects the largest connected component of the network (to prevent errors from routing between unconnected parts)
    """
    #generate the full network path for GDAL to be able to read the file
    path =str(os.path.join(arcpy.env.workspace,segments))
    print path
    if arcpy.Exists(path):
        g = nx.read_shp(path)
        #This selects the largest connected component of the graph
        sg = list(nx.connected_component_subgraphs(g.to_undirected()))[0]
        print "graph size (excluding unconnected parts): "+str(len(g))
        # Get the length for each road segment and append it as an attribute to the edges in the graph.
        for n0, n1 in sg.edges():
            oid = sg[n0][n1]["OBJECTID"]
            sg[n0][n1]['length'] = segmentlengths[oid]
        return sg
    else:
        print "network file not found on path: "+path
github rasmusbergpalm / recurrent-relational-networks / tasks / ages / rrn.py View on Github external
"""

            :param sources: (bs, n)
            :param targets: (bs, n)
            :param types: (bs, n)
            :param diffs: (bs, n)
            :param question: (bs, )
            :param answers: (bs, )
            :return:
            """
            bs = self.batch_size
            segment_ids = sum([[i] * n_nodes for i in range(bs)], [])

            edges = [(i, j) for i in range(n_nodes) for j in range(n_nodes) if i != j]
            edges = [(i + (b * n_nodes), j + (b * n_nodes)) for b in range(bs) for i, j in edges]
            assert len(list(nx.connected_component_subgraphs(nx.Graph(edges)))) == bs
            edges = tf.constant(edges, tf.int32)  # (bs*8*8, 2)

            sources = tf.reshape(tf.one_hot(sources, n_nodes), (bs * n_nodes, n_nodes))
            targets = tf.reshape(tf.one_hot(targets, n_nodes), (bs * n_nodes, n_nodes))
            types = tf.reshape(tf.one_hot(types, 3), (bs * n_nodes, 3))
            diffs = tf.reshape(tf.one_hot(diffs, 100), (bs * n_nodes, 100))

            question = tf.one_hot(question, n_nodes)
            question = tf.gather(question, segment_ids)  # (bs*n_nodes, n_nodes)

            x = tf.concat([sources, targets, types, diffs, question], axis=1)
            x = mlp(x, 'pre-fn')

            edge_features = tf.zeros_like(edges, tf.float32)

            with tf.variable_scope('steps'):
github maksim2042 / PyCon2012 / code / collection.py View on Github external
def draw_core_retweets():
    """
    Draw the trimmed largest component of retweets.
    """
    comps = nx.connected_component_subgraphs(
            retweets.to_undirected())
github StructuralNeurobiologyLab / SyConn / scripts / multiview_glia / glia_splitting.py View on Github external
def write_recon_EL2pkl(rag):
    ccs = nx.connected_component_subgraphs(rag)
    cc_dict_graph = {}
    cc_dict = {}
    for cc in ccs:
        curr_cc = list(cc.nodes())
        min_ix = np.min(curr_cc)
        if min_ix in cc_dict:
            raise("laksnclkadsnfldskf")
        cc_dict_graph[min_ix] = cc
        cc_dict[min_ix] = curr_cc
    write_obj2pkl(wd + "/glia/cc_dict_rag_graphs.pkl", cc_dict_graph)
    write_obj2pkl(wd + "/glia/cc_dict_rag.pkl", cc_dict)