def computegtmod(filename, weighted): fnetwork = 0 #with open(filename+'_ml_network.pickle') as handle: # fnetwork = pickle.load(handle) ml_network, layer, node_l, node_c, top, bot, couple, edge_l, edge_c, mu, commu = read_raw_network( filename, weighted) status = Status() status.layer = layer status.node_l = node_l status.node_c = node_c status.top = top status.bot = bot status.edge_l = edge_l status.edge_c = edge_c status.couple = couple status.mu = mu mod = __modularity(commu, status, ml_network) return mod, commu
def getSeries(filename, weighted): '''network_name = "networks_low_couple/" + filename.split('/')[-1] # commented code block fp=open(network_name,'r') line=fp.readline() line=line.rstrip() n_layer=int(line) layer={} node_l={} l_ID=1 edge_l={} edge_c={} # f_el = open(filename+'_edges_list_commod'+str(g), 'w') for i in range(0,n_layer): line=fp.readline() line=line.rstrip() line=line.split() layer[l_ID]=set() ##print line for n in line: layer[l_ID].add(int(n)) line=fp.readline() line=int(line.rstrip()) n_edge=line ##print n_edge edge_l[l_ID]=n_edge for j in range(0,n_edge): line=fp.readline() line=line.rstrip() line=line.split() n1=int(line[0]) n2=int(line[1]) if n1 not in node_l: node_l[n1]=set() node_l[n1].add(n2) if n2 not in node_l: node_l[n2]=set() node_l[n2].add(n1) # f_el.write(str(n1-1)+' '+str(n2-1)+'\n') l_ID+=1 line=fp.readline() line=line.rstrip() n_couple=int(line) ##print n_couple node_c={} top={} bot={} c_ID=1 couple={} for i in range(0,n_couple): line=fp.readline() ##print line line=line.rstrip() line=line.split() top[c_ID]=int(line[0]) bot[c_ID]=int(line[1]) couple[c_ID]=layer[top[c_ID]].union(layer[bot[c_ID]]) line=fp.readline() line=int(line.rstrip()) n_edge=line ##print n_edge edge_c[c_ID]=n_edge count_edge = 0 for j in range(0,n_edge): line=fp.readline() line=line.rstrip() line=line.split() n1=int(line[0]) n2=int(line[1]) if n1 not in node_c: node_c[n1]=set() node_c[n1].add(n2) if n2 not in node_c: node_c[n2]=set() node_c[n2].add(n1) count_edge += 1 # f_el.write(str(n1-1)+' '+str(n2-1)+'\n') edge_c[c_ID] = count_edge c_ID=c_ID+1 line=fp.readline() line=line.rstrip() ##print line n_comm=int(line) commu={} com_ID=1 for i in range(0,n_comm): line=fp.readline() line=line.rstrip() line=line.split() commu[com_ID]=set() for n in line: commu[com_ID].add(int(n)) com_ID+=1 mu=0''' #with open(filename+'_ml_network.pickle') as handle: # fnetwork = pickle.load(handle) ml_network, layer, node_l, node_c, top, bot, couple, edge_l, edge_c, mu, commu = read_raw_network( filename, weighted) #ml_network =build_network(layer, node_l, node_c, top, bot, couple, edge_l, edge_c) #with open(filename+'_ml_network.pickle', 'w') as handle: # pickle.dump([ml_network, layer, node_l, node_c, top, bot, couple, edge_l, edge_c, mu, commu], handle) dendogram, mod = louvain(ml_network, layer, node_l, node_c, top, bot, couple, edge_l, edge_c, mu) status = Status() status.layer = layer status.node_l = node_l status.node_c = node_c status.top = top status.bot = bot status.edge_l = edge_l status.edge_c = edge_c status.couple = couple status.mu = mu mod_old = mod commu = _get_com_wise_nodes( partition_at_level(dendogram, len(dendogram) - 1)) #commu =_get_commu_dict(partition_at_level(dendogram, len(dendogram)-1)) mod = __modularity(commu, status, ml_network) #print "--------- BEF RET ---------" #print mod_old, mod #print "----------------" return mod, dendogram
def louvain(graph, layer, node_l, node_c, top, bot, couple, edge_l, edge_c, mu): current_graph = graph.copy() status = Status() status.layer = layer status.node_l = node_l status.node_c = node_c status.top = top status.bot = bot status.edge_l = edge_l status.edge_c = edge_c status.couple = couple status.mu = mu status.init(current_graph) old_status = status.copy() status_list = list() level_count = 0 mod = __modularity(_get_commu_dict(status.node2com), status, graph) #print "Modularity before First Iteration ",mod __one_level(graph, old_status, current_graph, status, status_list, level_count) partition = __renumber(status.node2com) status_list.append(partition) current_graph, part, status = induced_graph_multilayer( partition, current_graph, status) mod1 = __modularity(_get_com_wise_nodes(part), status, current_graph) p = _get_com_wise_nodes( partition_at_level(status_list, len(status_list) - 1)) new_mod = __modularity(p, old_status, graph) #print "-> merge mod after level 0 : ", mod1 #print "-> Modularity after level 0: ",new_mod, "\n" ##print("Louvain, partition: ",partition) #print("Louvain partition: ",part) A = nx.adjacency_matrix(current_graph) ##print(A.todense()) status.init(current_graph) while True: level_count += 1 ##print level_count modif = __one_level(graph, old_status, current_graph, status, status_list, level_count, 1) partition = __renumber(status.node2com) status_list.append(partition) new_mod = __modularity( _get_commu_dict(partition_at_level(status_list, level_count)), old_status, graph) #print "-> Modularity after level ",level_count,": ",new_mod, "\n" #new_mod = __modularity(_get_commu_dict(partition), status, current_graph) #new_mod = __modularity(_get_com_wise_nodes(partition), status, current_graph) if modif == False: #if new_mod - mod < __MIN : break mod = new_mod #current_graph = induced_graph(partition, current_graph) current_graph, part, status = induced_graph_multilayer( partition, current_graph, status) #status.init(current_graph) status.init(current_graph, part) ##print("Louvain, partition: ",partition) # #print("Louvain, part after: ",part) # A = nx.adjacency_matrix(current_graph) ##print(A.todense()) return status_list, mod