How to use the leidenalg.ModularityVertexPartition function in leidenalg

To help you get started, we’ve selected a few leidenalg 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 vtraag / leidenalg / tests / test_VertexPartition.py View on Github external
def setUp(self):
    super(ModularityVertexPartitionTest, self).setUp();
    self.partition_type = leidenalg.ModularityVertexPartition;
github iosonofabio / northstar / northstar / cluster_with_annotations.py View on Github external
initial_membership = []
        for j in range(N):
            if j < n_fixed:
                mb = aau.index(aa[j])
            else:
                mb = aaun + (j - n_fixed)
            initial_membership.append(mb)

        if self.metric == 'cpm':
            partition = leidenalg.CPMVertexPartition(
                    g,
                    resolution_parameter=self.resolution_parameter,
                    initial_membership=initial_membership,
                    )
        elif self.metric == 'modularity':
            partition = leidenalg.ModularityVertexPartition(
                    g,
                    resolution_parameter=self.resolution_parameter,
                    initial_membership=initial_membership,
                    )
        else:
            raise ValueError(
                'clustering_metric not understood: {:}'.format(self.metric))

        # Run modified Leiden here
        opt.optimise_partition(partition, fixed_nodes=fixed_nodes)

        # Exctract result
        membership = partition.membership[n_fixed:]

        # Convert the known cell types
        lstring = len(max(aau, key=len))
github iosonofabio / northstar / semiannotate / semiannotate.py View on Github external
while i in tmp:
                i += 1
            initial_membership.append(i)
            tmp.add(i)
            i += 1
        del tmp

        # Compute communities with semi-supervised Leiden
        if clustering_metric == 'cpm':
            partition = leidenalg.CPMVertexPartition(
                    g,
                    resolution_parameter=resolution_parameter,
                    initial_membership=initial_membership,
                    )
        elif clustering_metric == 'modularity':
            partition = leidenalg.ModularityVertexPartition(
                    g,
                    resolution_parameter=resolution_parameter,
                    initial_membership=initial_membership,
                    )
        else:
            raise ValueError(
                'clustering_metric not understood: {:}'.format(clustering_metric))

        fixed_nodes = [int(i < n_fixed) for i in range(N)]
        opt.optimise_partition(partition, fixed_nodes=fixed_nodes)
        membership = partition.membership

        self.membership = membership[n_fixed:]
github iosonofabio / northstar / semiannotate / semiannotate.py View on Github external
initial_membership.append(isi)
            else:
                initial_membership.append(isi)

        if len(initial_membership) != Ne:
            raise ValueError('initial_membership list has wrong length!')

        # Compute communities with semi-supervised Leiden
        if clustering_metric == 'cpm':
            partition = leidenalg.CPMVertexPartition(
                    g,
                    resolution_parameter=resolution_parameter,
                    initial_membership=initial_membership,
                    )
        elif clustering_metric == 'modularity':
            partition = leidenalg.ModularityVertexPartition(
                    g,
                    resolution_parameter=resolution_parameter,
                    initial_membership=initial_membership,
                    )
        else:
            raise ValueError(
                'clustering_metric not understood: {:}'.format(clustering_metric))

        fixed_nodes = [int(i < n_fixede) for i in range(Ne)]
        opt.optimise_partition(partition, fixed_nodes=fixed_nodes)
        membership = partition.membership

        self.membership = membership[n_fixede:]
github oscar-franzen / alona / alona / clustering.py View on Github external
communities Traag V, Waltman L, van Eck NJ
        https://arxiv.org/abs/1810.08473 """
        log_debug('Running leiden clustering...')
        res = self.params['leiden_res']
        seed = self.params['seed']
        # construct the graph object
        nn = set(self.snn_graph[self.snn_graph.columns[0]])
        g = ig.Graph()
        g.add_vertices(len(nn))
        g.vs['name'] = list(range(1, len(nn)+1))
        ll = []
        for i in self.snn_graph.itertuples(index=False):
            ll.append(tuple(i))
        g.add_edges(ll)
        if self.params == 'ModularityVertexPartition':
            part = leidenalg.ModularityVertexPartition
        else:
            part = leidenalg.RBERVertexPartition
        cl = leidenalg.find_partition(g,
                                      part,
                                      n_iterations=10,
                                      resolution_parameter=res,
                                      seed=seed)
        self.leiden_cl = cl.membership
        self.leiden_prep()
        log_debug('Leiden has finished.')
github YosefLab / scVI / scvi / inference / utils.py View on Github external
def louvain_clusters(latent, k=10, rands=0):
    nn_matrix = kneighbors_graph(latent, k)
    rows, cols = np.where(nn_matrix.todense() == 1)
    edges = [(row, col) for row, col in zip(rows, cols)]
    g = ig.Graph()
    g.add_vertices(latent.shape[0])
    g.add_edges(edges)
    res = leidenalg.find_partition(g, leidenalg.ModularityVertexPartition, seed=rands)
    clusters = np.asarray(res.membership)
    return clusters