Python networkx.find_cliques() Examples

The following are 24 code examples of networkx.find_cliques(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: vrcomplex.py    From MoguTDA with MIT License 6 votes vote down vote up
def __init__(self,
                 points,
                 epsilon,
                 labels=None,
                 distfcn=distance.euclidean):
        self.pts = points
        self.labels = (range(len(self.pts))
                       if labels is None or len(labels) != len(self.pts)
                       else labels)
        self.epsilon = epsilon
        self.distfcn = distfcn
        self.network = self.construct_network(self.pts,
                                              self.labels,
                                              self.epsilon,
                                              self.distfcn)
        self.import_simplices(map(tuple, nx.find_cliques(self.network))) 
Example #2
Source File: clique_oracle.py    From cbc-casper with GNU Affero General Public License v3.0 6 votes vote down vote up
def find_biggest_clique(self):
        """Finds the biggest clique of validators committed to target estimate."""

        # Do not have safety if less than half have candidate_estimate.
        if self.validator_set.weight(self.with_candidate) < self.validator_set.weight() / 2:
            return set(), 0

        edges = self._collect_edges()
        graph = nx.Graph()
        graph.add_edges_from(edges)
        cliques = nx.find_cliques(graph)

        max_clique = []
        max_weight = 0
        for clique in cliques:
            test_weight = utils.get_weight(clique)
            if test_weight > max_weight:
                max_clique = clique
                max_weight = test_weight

        return set(max_clique), max_weight 
Example #3
Source File: PercoMCV.py    From cdlib with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __k_clique_communities(g, cliques=None):
    if cliques is None:
        cliques = nx.find_cliques(g)
    cliques = [frozenset(c) for c in cliques if len(c) >= 4]

    # First index which nodes are in which cliques
    membership_dict = defaultdict(list)
    for clique in cliques:
        for node in clique:
            membership_dict[node].append(clique)

    # For each clique, see which adjacent cliques percolate
    perc_graph = nx.Graph()
    perc_graph.add_nodes_from(cliques)
    for clique in cliques:
        for adj_clique in _get_adjacent_cliques(clique, membership_dict):
            if len(clique.intersection(adj_clique)) >= 3:
                perc_graph.add_edge(clique, adj_clique)

    # Connected components of clique graph with perc edges
    # are the percolated cliques
    for component in nx.connected_components(perc_graph):
        yield frozenset.union(*component) 
Example #4
Source File: main.py    From cd-diagram with GNU General Public License v3.0 6 votes vote down vote up
def form_cliques(p_values, nnames):
    """
    This method forms the cliques
    """
    # first form the numpy matrix data
    m = len(nnames)
    g_data = np.zeros((m, m), dtype=np.int64)
    for p in p_values:
        if p[3] == False:
            i = np.where(nnames == p[0])[0][0]
            j = np.where(nnames == p[1])[0][0]
            min_i = min(i, j)
            max_j = max(i, j)
            g_data[min_i, max_j] = 1

    g = networkx.Graph(g_data)
    return networkx.find_cliques(g) 
Example #5
Source File: test_clique.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_find_cliques2(self):
        hcl = list(nx.find_cliques(self.H))
        assert_equal(sorted(map(sorted, hcl)),
                     [[1, 2], [1, 4, 5, 6], [2, 3], [3, 4, 6]]) 
Example #6
Source File: test_clique.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_directed(self):
        cliques = nx.find_cliques(nx.DiGraph()) 
Example #7
Source File: test_clique.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_find_cliques2(self):
        hcl = list(nx.find_cliques(self.H))
        assert_equal(sorted(map(sorted, hcl)),
                     [[1, 2], [1, 4, 5, 6], [2, 3], [3, 4, 6]]) 
Example #8
Source File: test_clique.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_selfloops(self):
        self.G.add_edge(1, 1)
        cl = list(nx.find_cliques(self.G))
        rcl = nx.find_cliques_recursive(self.G)
        assert_equal(sorted(map(sorted, cl)), sorted(map(sorted, rcl)))
        assert_equal(cl,
                     [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]) 
Example #9
Source File: test_clique.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_find_cliques1(self):
        cl = list(nx.find_cliques(self.G))
        rcl = nx.find_cliques_recursive(self.G)
        expected = [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]
        assert_equal(sorted(map(sorted, cl)), sorted(map(sorted, rcl)))
        assert_equal(sorted(map(sorted, cl)), sorted(map(sorted, expected))) 
Example #10
Source File: test_clique.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        z = [3, 4, 3, 4, 2, 4, 2, 1, 1, 1, 1]
        self.G = cnlti(nx.generators.havel_hakimi_graph(z), first_label=1)
        self.cl = list(nx.find_cliques(self.G))
        H = nx.complete_graph(6)
        H = nx.relabel_nodes(H, dict([(i, i + 1) for i in range(6)]))
        H.remove_edges_from([(2, 6), (2, 5), (2, 4), (1, 3), (5, 3)])
        self.H = H 
Example #11
Source File: test_clique.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_directed(self):
        cliques = nx.find_cliques(nx.DiGraph()) 
Example #12
Source File: test_clique.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_find_cliques2(self):
        hcl = list(nx.find_cliques(self.H))
        assert_equal(sorted(map(sorted, hcl)),
                     [[1, 2], [1, 4, 5, 6], [2, 3], [3, 4, 6]]) 
Example #13
Source File: test_clique.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_selfloops(self):
        self.G.add_edge(1, 1)
        cl = list(nx.find_cliques(self.G))
        rcl = nx.find_cliques_recursive(self.G)
        assert_equal(sorted(map(sorted, cl)), sorted(map(sorted, rcl)))
        assert_equal(cl,
                     [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]) 
Example #14
Source File: test_clique.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_find_cliques1(self):
        cl = list(nx.find_cliques(self.G))
        rcl = nx.find_cliques_recursive(self.G)
        expected = [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]
        assert_equal(sorted(map(sorted, cl)), sorted(map(sorted, rcl)))
        assert_equal(sorted(map(sorted, cl)), sorted(map(sorted, expected))) 
Example #15
Source File: test_clique.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        z = [3, 4, 3, 4, 2, 4, 2, 1, 1, 1, 1]
        self.G = cnlti(nx.generators.havel_hakimi_graph(z), first_label=1)
        self.cl = list(nx.find_cliques(self.G))
        H = nx.complete_graph(6)
        H = nx.relabel_nodes(H, dict([(i, i + 1) for i in range(6)]))
        H.remove_edges_from([(2, 6), (2, 5), (2, 4), (1, 3), (5, 3)])
        self.H = H 
Example #16
Source File: test_clique.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_directed(self):
        cliques = nx.find_cliques(nx.DiGraph()) 
Example #17
Source File: test_clique.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_selfloops(self):
        self.G.add_edge(1, 1)
        cl = list(nx.find_cliques(self.G))
        rcl = nx.find_cliques_recursive(self.G)
        assert_equal(sorted(map(sorted, cl)), sorted(map(sorted, rcl)))
        assert_equal(cl,
                     [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]) 
Example #18
Source File: test_clique.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_find_cliques1(self):
        cl = list(nx.find_cliques(self.G))
        rcl = nx.find_cliques_recursive(self.G)
        expected = [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]
        assert_equal(sorted(map(sorted, cl)), sorted(map(sorted, rcl)))
        assert_equal(sorted(map(sorted, cl)), sorted(map(sorted, expected))) 
Example #19
Source File: test_clique.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        z = [3, 4, 3, 4, 2, 4, 2, 1, 1, 1, 1]
        self.G = cnlti(nx.generators.havel_hakimi_graph(z), first_label=1)
        self.cl = list(nx.find_cliques(self.G))
        H = nx.complete_graph(6)
        H = nx.relabel_nodes(H, dict([(i, i + 1) for i in range(6)]))
        H.remove_edges_from([(2, 6), (2, 5), (2, 4), (1, 3), (5, 3)])
        self.H = H 
Example #20
Source File: structure_data.py    From lammps_interface with MIT License 5 votes vote down vote up
def __or__(self, graph):
        if (len(graph.nodes()) == 1) and len(self.nodes()) == 1:
            return list([0])
        cg = self.correspondence_graph(graph, 0.4)
        cliques = list(nx.find_cliques(cg))
        cliques.sort(key=len)
        return cliques[-1] 
Example #21
Source File: space.py    From qmpy with MIT License 5 votes vote down vote up
def find_cliques(self):
        self._cliques = nx.find_cliques(self.graph)
        return self._cliques 
Example #22
Source File: space.py    From qmpy with MIT License 5 votes vote down vote up
def cliques(self):
        """
        Iterator over maximal cliques in the phase space. To get a list of
        cliques, use list(PhaseSpace.cliques).
        """
        if self._cliques is None:
            self.find_cliques()
        return self._cliques 
Example #23
Source File: rectangle_clustering.py    From aggregation with Apache License 2.0 4 votes vote down vote up
def __cluster__(self,markings,user_ids,tools,reduced_markings,dimensions,subject_id):
        """
        main clustering algorithm - works on a single per-subject basis
        for rectangles, doesn't make use of reduced_markings
        :param markings:
        :param user_ids:
        :param tools:
        :param reduced_markings:
        :param dimensions:
        :param subject_id:
        :return:
        """

        print(markings)
        print(reduced_markings)
        print(len(zip(*markings)))

        print([np.median(axis) for axis in zip(*markings)])
        print([np.median(axis,axis=0) for axis in zip(*markings)])
        print([np.median(axis,axis=1) for axis in zip(*markings)])
        assert False
        # if empty markings, just return nothing
        if markings == []:
            return [],0

        results = []

        overlap_graph = self.__overlap_graph__(markings)

        # each clique is a group of markings which all refer to the same region on the page
        # go through each clique
        for c in networkx.find_cliques(overlap_graph):
            # ignore any clique with less than 3 markings in it
            if len(c) < 0:
                continue

            # get the specific markings in this clique and their corresponding tools
            clique = [markings[i] for i in c]
            tools_in_clique = [tools[i] for i in c]

            # create the new cluster based on this clique
            new_cluster = dict()
            new_cluster["center"] = self.__median_rectangles__(clique)
            new_cluster["cluster members"] = clique
            new_cluster["users"] = [user_ids[i] for i in c]
            # the tools used by each person with a rectangle in this cluster
            new_cluster["tools"] = tools_in_clique
            new_cluster["image area"] = None

            results.append(new_cluster)
        return results,0 
Example #24
Source File: comparisons.py    From eht-imaging with GNU General Public License v3.0 4 votes vote down vote up
def image_agreements(imarr, beamparams, metric_mtx, fracsteps, cutoff=0.95):

    if 'networkx' not in sys.modules:
        raise Exception("networkx not installed: cannot use image_agreements()!")

    (min_psize, max_fov) = get_psize_fov(imarr)

    im_cliques_fraclevels = []
    cliques_fraclevels = []
    for fracidx in range(len(fracsteps)):
        # print(fracidx)

        slice_metric_mtx = metric_mtx[:, :, fracidx]
        cuttoffidx = np.where(slice_metric_mtx >= cutoff)
        consistant = zip(*cuttoffidx)

        # make graph
        G = nx.Graph()
        for i in range(len(consistant)):
            G.add_edge(consistant[i][0], consistant[i][1])

        # find all cliques
        cliques = list(nx.find_cliques(G))
        # print(cliques)

        cliques_fraclevels.append(cliques)

        im_clique = []
        for c in range(len(cliques)):
            clique = cliques[c]
            im_avg = imarr[clique[0]].blur_gauss(beamparams, fracsteps[fracidx])

            for n in range(1, len(clique)):
                imcomp = imarr[clique[n]].blur_gauss(beamparams, fracsteps[fracidx])
                (error, im_avg, im2_shift) = im_avg.compare_images(imcomp, metric=['xcorr'],
                                                                   psize=min_psize,
                                                                   target_fov=max_fov,
                                                                   blur_frac=0.0,
                                                                   beamparams=beamparams)
                im_avg.imvec = (im_avg.imvec + im2_shift.imvec) / 2.0

            im_clique.append(im_avg.copy())

        im_cliques_fraclevels.append(im_clique)

    return(cliques_fraclevels, im_cliques_fraclevels)