0

Currently, i have a NetworkX graph that changes color and size of nodes based on degree centrality, but I am looking to instead of changing color based on degree centrality, I am looking to change the color of the nodes based on modularity, preferably using label propagation in the calculation of the modularity.

I've tried changing the color the same way I did on the code that changes according to degree centrality, but only getting errors as the degree centrality had multiple values and the modularity was one single value.

The expected result is to have the color of the nodes change depending on modularity instead of degree centrality, while keeping the size of the nodes based on degree centrality.

the CSV file used in this project is available here: https://www.mediafire.com/file/q0kziy9h251fcjf/nutrients.csv/file

Here is the code for the project

import networkx as nx
import matplotlib.pyplot as plt
import networkx.algorithms.community as nx_com
import numpy as np

# create graph from data
with open("nutrients.csv", "r") as f:
    G = nx.parse_edgelist(f.readlines(), delimiter=",")

# centrality
deg_centrality = nx.degree_centrality(G)
centrality = np.fromiter(deg_centrality.values(), float)

# modularity
mod = nx_com.modularity(G, nx_com.label_propagation_communities(G))

# plot
pos = nx.spring_layout(G)
nx.draw(G, pos, node_color=centrality, node_size=centrality*2e3)
nx.draw_networkx_labels(G, pos)
plt.show()

Roald Andre Kvarv
  • 187
  • 1
  • 3
  • 21
  • 1
    What is the "node-wise modularity" value? Modularity is a quality measure (or objective) of community/clustering algorithms. Hence, it returns only one value for the whole graph (and given one node partition). – Sparky05 Apr 28 '21 at 09:04
  • @Sparky05 i am unsure on how to find node-wise modularity. but I do believe that is the values I need to make it work. but unsure on how to find it – Roald Andre Kvarv Apr 29 '21 at 07:16

1 Answers1

0

I fixed the problem. here is the answer

import networkx as nx
import matplotlib.pyplot as plt
import networkx.algorithms.community as nx_com
import numpy as np
import community as community_louvain

# create graph from data
with open("nutrients.csv", "r") as f:
    G = nx.parse_edgelist(f.readlines(), delimiter=",")

# centrality
deg_centrality = nx.degree_centrality(G)
centrality = np.fromiter(deg_centrality.values(), float)

# modularity
label = community_louvain.best_partition(G)
mod = community_louvain.modularity(label, G)
values = [label.get(node) for node in G.nodes()]


# plot
pos = nx.spring_layout(G)
nx.draw(G, pos, node_color=values, node_size=centrality*2e3)
nx.draw_networkx_labels(G, pos)
plt.show()

Roald Andre Kvarv
  • 187
  • 1
  • 3
  • 21
  • I'm not that familiar with the `community` package, but as far as I see, the node's color represents now the community and not some modularity - or am I wrong? – Sparky05 Apr 29 '21 at 10:28
  • @Sparky05 the package is called python-louvain, but you import it as community. it has a modularity function that does the calculation, and a best partition function that finds the best communities, and then then the color changes depending on he different communities, because each community has a specific modularity – Roald Andre Kvarv Apr 29 '21 at 12:57
  • You still have one modularity value for the pair of (graph, partition). What you do is assigning colors based on the partition or in other words the color corresponds to a cluster/community/group/block (however you call it). But it does not show different modularity values. You would not need the variable `mod=....`. The modularity is only optimised for finding the `best_partition`. – Sparky05 Apr 29 '21 at 13:36