def test_HLC(self): g = get_string_graph() coms = algorithms.hierarchical_link_community(g) self.assertEqual(type(coms.communities), list) if len(coms.communities) > 0: self.assertEqual(type(coms.communities[0]), list) self.assertEqual(type(coms.communities[0][0]), tuple)
def test_read_write_json(self): g = nx.karate_club_graph() communities = algorithms.louvain(g) readwrite.write_community_json(communities, "coms.json") communities_r = readwrite.read_community_json("coms.json") self.assertListEqual(communities.communities, communities_r.communities) os.remove("coms.json") communities = algorithms.louvain(g) readwrite.write_community_json(communities, "coms.gzip", zip=True) communities_r = readwrite.read_community_json("coms.gzip", zip=True) self.assertListEqual(communities.communities, communities_r.communities) os.remove("coms.gzip") communities = algorithms.frc_fgsn(g, 1, 0.5, 3) readwrite.write_community_json(communities, "coms.json") communities_r = readwrite.read_community_json("coms.json") self.assertListEqual(communities.communities, communities_r.communities) os.remove("coms.json") communities = algorithms.hierarchical_link_community(g) readwrite.write_community_json(communities, "coms.json") communities_r = readwrite.read_community_json("coms.json") self.assertListEqual(communities.communities, communities_r.communities) with open("coms.json") as f: cr = f.read() readwrite.read_community_from_json_string(cr) os.remove("coms.json")
def test_node_map(self): g = nx.karate_club_graph() coms = algorithms.hierarchical_link_community(g) edge_com_map = coms.to_edge_community_map() self.assertIsInstance(edge_com_map, dict)
def test_to_json(self): g = nx.karate_club_graph() coms = algorithms.hierarchical_link_community(g) self.assertIsInstance(coms, EdgeClustering) js = coms.to_json() self.assertIsInstance(js, str)
def make_communities(g, method): ''' Function to run community detection Inputs: g : networkx object Networkx network object representing raw music data method : string String identifying clustering method to be used. Options are (case-sensitive): 1) infomap 2) LPM 3) louvain 4) HLC Returns: graph : networkx object Networkx network object with added community data ''' print("*******Inside main comm function *******") if method == "infomap": edge_tuples = [edge.tuple for edge in g.es] im = infomap.Infomap() im.add_links(edge_tuples) im.run("-d -N 10") modules = im.get_multilevel_modules() # igraph non-hierarchical version #infomap_partition = g.community_infomap(edge_weights='weight') infomap_partition_assignment = { g.vs[i]['name']: modules[i] for i in range(g.vcount()) } return infomap_partition_assignment elif method == "LPM": lpm_partition = g.community_label_propagation(weights='weight') lpm_partition_assignment = { g.vs[i]['name']: [lpm_partition.membership[i]] for i in range(g.vcount()) } return lpm_partition_assignment elif method == 'louvain': louvain_partition = g.community_multilevel( weights=[e['weight'] for e in g.es], return_levels=True) louvain_partition_assignment = { g.vs[i]['name']: [level.membership[i] for level in louvain_partition] for i in range(len(g.vs)) } return louvain_partition_assignment elif method == 'HLC': coms = algorithms.hierarchical_link_community(g) return coms.communities