Skip to content

Commit

Permalink
Compute modularity
Browse files Browse the repository at this point in the history
  • Loading branch information
huangwenren authored and hurthwell committed May 27, 2019
1 parent acc8078 commit 9508bb0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ aenum = "*"
pytest-cov = "*"
gitpython = "*"
sphinx = "*"
python-louvain = "*"

[dev-packages]

Expand Down
10 changes: 10 additions & 0 deletions persper/analytics/analyzer2.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ def compute_project_complexity(self, r_n: int, r_e: int):
"""
return self.graph.eval_project_complexity(r_n, r_e)

def compute_modularity(self):
"""Compute modularity score based on function graph.
Returns
-------
modularity : float
The modularity score of this graph.
"""
return self.graph.compute_modularity()

async def analyze(self, maxAnalyzedCommits=None, suppressStdOutLogs=False):
commitSpec = self._terminalCommit
if self._originCommit:
Expand Down
24 changes: 24 additions & 0 deletions persper/analytics/call_commit_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from persper.analytics.score import normalize
from typing import Union, Set, List, Dict, Optional
from persper.analytics.complexity import eval_project_complexity
import community

class CommitIdGenerators:
@staticmethod
Expand Down Expand Up @@ -241,3 +242,26 @@ def developer_devranks(self, alpha, black_set=None):
else:
developer_devranks[email] = commit_devranks[sha]
return developer_devranks

def compute_modularity(self):
"""Compute modularity score based on function graph.
Returns
-------
modularity : float
The modularity score of this graph.
"""
# Construct non directed graph
graph = nx.Graph()
for node in self.nodes():
graph.add_node(node)
for (source, target) in self.edges():
graph.add_edge(source, target)
# Compute the partition of the graph nodes
partition = community.best_partition(graph)
# Compute modularity
modularity = community.modularity(partition, graph)
# Normalize [0, 1] to [0, 100]
modularity = modularity * 100

return modularity

0 comments on commit 9508bb0

Please sign in to comment.