pythoncluster-analysisnetworkxmarkov

Getting node membership in each cluster


I am using Markov clustering to cluster a graph of 878 nodes. The implementation is based on the work mentioned here https://github.com/guyallard/markov_clustering

adj_matrix = nx.to_numpy_matrix(G)
res = mcl.run_mcl(adj_matrix)
clusters = mcl.get_clusters(res)

clusters:

[(0,73, 88,173,223,235,390,405,409,435,442,456,481,501,573,615), 
(5, 38, 193, 403, 657, 679, 760, 791, 835, 854),
...
...
(7, 201, 640)]

It looks that the procedure is giving me the node order NOT the original label that i used to build the graph which was like this 780873982, 928735728, 293482978, etc. Is there a way to map the above result to the original node labels?

The expected the result would be like this

[(780873982, 928735728, 293482978), (293482932, 883482978), ...] 

Thank you in advance !


Solution

  • you can use a dictionary:

    # dummy nodes and graph
    nodes = [np.random.randint(0,10031) for a in range(100)]
    G = nx.Graph()
    G.add_nodes_from(nodes)
    
    # dummy clusters
    clusters = [list(np.random.choice(range(len(G.nodes)), np.random.randint(1,5))) for a in range(8)]
    
    
    # make mapping dictionary:
    mapping = {a:b for a, b in zip(range(len(G.nodes)), G.nodes())}
    
    # apply mapping_dictionary:
    
    clusters_with_node_names = []
    
    for c in clusters:
        clusters_with_node_names.append([mapping[a] for a in c])