diff --git a/Pipfile b/Pipfile index 19b18641463..dbdd78f493b 100644 --- a/Pipfile +++ b/Pipfile @@ -25,6 +25,7 @@ aenum = "*" pytest-cov = "*" gitpython = "*" sphinx = "*" +python-louvain = "*" [dev-packages] diff --git a/persper/analytics/analyzer2.py b/persper/analytics/analyzer2.py index ecfc398e850..fc1e08bbf8b 100644 --- a/persper/analytics/analyzer2.py +++ b/persper/analytics/analyzer2.py @@ -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: diff --git a/persper/analytics/call_commit_graph.py b/persper/analytics/call_commit_graph.py index b6a0b21926c..31e98e2c6df 100644 --- a/persper/analytics/call_commit_graph.py +++ b/persper/analytics/call_commit_graph.py @@ -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 @@ -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