def test_modularity(self): g = nx.karate_club_graph() communities = louvain(g) mod = evaluation.newman_girvan_modularity(g, communities) self.assertLessEqual(mod.score, 1) self.assertGreaterEqual(mod.score, -0.5) mod = evaluation.erdos_renyi_modularity(g, communities) self.assertLessEqual(mod.score, 1) self.assertGreaterEqual(mod.score, -0.5) mod = evaluation.modularity_density(g, communities) self.assertIsInstance(mod.score, float) mod = evaluation.z_modularity(g, communities) self.assertLessEqual(mod.score, np.sqrt(g.number_of_nodes())) self.assertGreaterEqual(mod.score, -0.5)
def z_modularity(self): """ Z-modularity is another variant of the standard modularity proposed to avoid the resolution limit. The concept of this version is based on an observation that the difference between the fraction of edges inside communities and the expected number of such edges in a null model should not be considered as the only contribution to the final quality of algorithms structure. :return: the z-modularity score :Example: >>> from cdlib.algorithms import louvain >>> g = nx.karate_club_graph() >>> communities = louvain(g) >>> mod = communities.z_modularity() :References: Miyauchi, Atsushi, and Yasushi Kawase. **Z-score-based modularity for algorithms detection in networks.** PloS one 11.1 (2016): e0147805. """ if self.__check_graph(): return evaluation.z_modularity(self.graph, self) else: raise ValueError("Graph instance not specified")