question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CPMVertexPartition.Bipartite implementation

See original GitHub issue

Is your feature request related to a problem? Please describe. First, thanks for cdlib, it is amazing. I am using it for bipartite network analysis and community detection and then evaluation of results of different algorithms. There is currently only bimlpa implemented for it. I have a suggestion.

Describe the solution you’d like Since you have already implemented CPMVertexPartition, I am guessing implementation of its bipartite (https://leidenalg.readthedocs.io/en/stable/multiplex.html?highlight=bipartite#bipartite) version should not be much complicated (I certainly hope so, but my background is in social sciences, so I cannot say for sure).

Describe alternatives you’ve considered I have provided below a sample of code I have written to use the CPMVertexPartition.Bipartite, but I use it directly in Leidenalg package by https://github.com/vtraag . Do you think there is hope to have CPMVertexPartition.Bipartite in cdlib?

Additional context This is an example of the code I run directly in leidenalg library:


def find_bipartite_partitions(graph, gamma, seed=0):
    """find_bipartite_partitions(graph, seed, gamma)
    
    Takes an igraph graph and returns the bipartite partitions detected with the provided gamma which is the internal density of the communities passed to CPM method in leiden algorithm package by Vincent Traag (https://leidenalg.readthedocs.io/en/latest/multiplex.html#bipartite). Results are returned as a pandas dataframe which includes vertices names, cluster membership and other node attributes in graph. It also return number of clusters detecetd and members in each of them to decide and change gamma if needed.
    
    Parameters
    ----------
    @param graph: igraph graph that needs to be bipartite (e.g., org/paper or author/paper) and named as we use vertex names and attributes in the detaframe returned.
    @param seed: the random seed to be used in CPM method to keep results/partitions replicable.
    @param gamma: CPM method (see leidenalg for more details: https://leidenalg.readthedocs.io/en/latest/multiplex.html#bipartite) takes a gamma which is the internal density of the communities detected and uses it as a resolution parameter.
    
    Examples
    --------
    >>> number_partitions, partitions_freq_members, results_df, p_01 = net_tables.find_bipartite_partitions(graph=gg, seed=0, gamma=1.625e-07)
    
    """

    import igraph as ig
    import leidenalg as la
    import pandas as pd

    optimiser = la.Optimiser()
    # set seed to get the same communities
    la.Optimiser.set_rng_seed(self=optimiser, value=seed)

    p_01, p_0, p_1 = la.CPMVertexPartition.Bipartite(graph, resolution_parameter_01=gamma)

    diff = optimiser.optimise_partition_multiplex([p_01, p_0, p_1], layer_weights=[1, -1, -1])

    # to see number of communities detected
    number_partitions = len(set(p_01.membership))
    # pd series of clusters with number of members in each
    partitions_freq_members = pd.Series(p_01.membership).value_counts()

    # add node attributes and cluster membership and build pandas table of results
    graph.vs['cluster'] = p_01.membership
    # for all other attributes
    all_attributes = dict()
    for node_attr in graph.vs.attributes():
        attr_list = [v[node_attr] for v in graph.vs]
        all_attributes[node_attr] = attr_list
    results_df = pd.DataFrame(all_attributes)

    return (number_partitions, partitions_freq_members, results_df, p_01)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
GiulioRossetticommented, Jan 12, 2021

Thanks to you both for the clarification. As promised, CPM Bipartite is also accessible through CDlib now (only on the GitHub master branch for now). Here’s the doc.

By the way, @vtraag thanks for your excellent leidenalg library!

2reactions
vtraagcommented, Jan 8, 2021

The find_partition_multiplex does something different than detecting communities on a bipartite graph, so I wouldn’t recommend going there. It is indeed the easiest to use the CPMVertexPartition.Bipartite “constructor”, and then use the optimise_partition_multiplex function as @akbaritabar suggested above. The underlying way this works is explained a bit more in-depth in the documentation. If you have any questions about this, feel free to ping me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reference — leidenalg 0.9.2.dev0+gb530332.d20221214 ...
CPMVertexPartition – partition containing the bipartite graph and correct node sizes. ig.CPMVertexPartition – partition for type 0, containing the correct node ...
Read more >
leidenalg Documentation - Read the Docs
In this example we used CPMVertexPartition. but any other VertexPartition would work as ... All of this has been implemented in the constructor...
Read more >
Support for bipartite graphs · Issue #5 · TomKellyGenetics/leiden
The support for bipartite clustering is only implemented for CPMVertexPartition . However, there is a small trick that allows you to mimic ...
Read more >
R Implementation of Leiden Clustering Algorithm
Title R Implementation of Leiden Clustering Algorithm. Version 0.4.3 ... Bipartite, CPMVertexPartition. ... modularity for Bipartite graphs.
Read more >
leiden source: R/find_partition.R - Rdrr.io
Bipartite, CPMVertexPartition.Bipartite (see the Leiden python module documentation for more details) ##' @param initial_membership,weights,node_sizes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found