def test1(): network = nx.karate_club_graph() communities = qs.louvain(network) sg, pvals, qhat, shat = qs.qstest(network, communities, qs.qmod, qs.vol, qs.louvain) assert len(sg) == len(communities) assert len(pvals) == len(communities) assert max(pvals) <= 1.0 assert min(pvals) >= 0.0
import networkx as nx import qstest as qs from networkx.algorithms import community as nxcdalgorithm # Wrapper function for async_fluidc implemented in Networkx 2.0 def my_cdalgorithm(network): communities = [] subnets = nx.connected_component_subgraphs(network) for subnet in subnets: coms_iter = nxcdalgorithm.asyn_fluidc(subnet, min([C, subnet.order()]), maxiter) for nodes in iter(coms_iter): communities.append(list(nodes)) return communities # Pareameters of async_fluidc C = 3 maxiter = 10 network = nx.karate_club_graph() communities = my_cdalgorithm(network) sg, p_values = qs.qstest(network, communities, qs.qmod, qs.vol, my_cdalgorithm)
print(" Subgraph has no edges.") # Identify communities print("** Finding communities (disease modules)...") communities = louvain(g) coms = community_louvain.best_partition(g) C = max(coms.values()) + 1 print(" Done.") # Check statistical significance of the communities over randomly generated communities print("\n** Testing significance of the communities....") stat_test = qs.qstest(g, communities, find_modularity, find_size, louvain, num_of_rand_net=int(args.random), num_of_thread=int(args.threads)) values = [list(x) for x in stat_test] sg = values[0] pval = values[1] # Write significant communities to file out = write_sig_comm_tofile(sg, pval, communities, args.out) # Create logfile with miscellaneous details ofile = os.path.join(args.out, 'find_cluster_logfile.txt') outfile = open(ofile, 'w') outfile.write(" ** Network Details:\n") outfile.write(" Network contains %s nodes and %s edges\n" %
import networkx as nx import qstest as qs # Number of intra-community edges def my_qfunc(network, nodes): return network.subgraph(nodes).size() network = nx.karate_club_graph() communities = qs.louvain(network) sg, p_values = qs.qstest(network, communities, my_qfunc, qs.vol, qs.louvain)