Package: qstest
Python codes for the (q, s)-test, a significance test for individual communities in networks.Please citeKojaku, S. and Masuda, N. "A generalised significance test for individual communities in networks". Preprint arXiv: 1712.00298 (2017)———————————————————————————————————————————————————————————————————————————ContentsLICENSE - License of qstestREADME.md - README file for GithubREADME.txt - This README filesetup.py - Script for installing qstestrequirements.txt - List of libraries installed by setup.pytest.py - Test code for Travis CI.gitignore - Configuration file for GitHub.travis.yml - Configuration file for Travis CIqstest/ - Python codes for the (q, s)-test:qstest/__init__.py - Header fileqstest/cdalgorithm_wrapper.py - Codes for community-detection algorithmsqstest/qstest.py contains - Codes for the (q, s)-testqstest/quality_functions.py - Codes for calculating quality functions of a communityqstest/size_functions.py - Codes for calculating the size of a communityexamples/ - example codes:examples/example1.py - Usage of qstest with a built-in quality function, community-size function and community detection algorithmexamples/example2.py - Usage of qstest with a user-defined quality functionexamples/example3.py - Usage of qstest with a user-defined community-size functionexamples/example4.py - Usage of qstest with a user-defined community-detection algorithm———————————————————————————————————————————————————————————————————————————InstallationYou can install qstest with pip, a package management system for Python.To install, runpip install qstestIf this does not work, trypython setup.py install———————————————————————————————————————————————————————————————————————————Usagesg, p_values = qstest(network, communities, qfunc, sfunc, cdalgorithm, num_of_rand_net = 500, alpha = 0.05, num_of_thread = 4)Inputnetwork - Networkx Graph class instancecommunities - C-dimensional list of lists. communities[c] is a list containing the IDs of nodes belonging to community c. Node and community indices start from 0.qfunc - Quality of a community. The following quality functions are available:qmod - Contribution of a community to the modularityqint - Internal average degreeqexp - Expansionqcnd - ConductanceTo pass your quality function to qstest, see "How to pass your quality function to qstest" below.sfunc - Community-size function (i.e., size of a community). The following community-size functions are available:n - Number of nodes in a communityvol - Sum of the degrees of nodes in a communityTo pass your community-size function to qstest, see "How to pass your community-size function to qstest" below.cdalgorithm - Community-detection algorithm. The following algorithms are available:louvain - Louvain algorithm (http://perso.crans.org/aynaud/communities/index.html)label_propagation - Label propagation algorithm (https://networkx.github.io/documentation/stable/reference/algorithms/community.html)To pass your community-detection algorithm to qstest, see "How to pass your community-detection algorithm to qstest" below.num_of_rand_net (optional) - Number of randomised networks (Default: 500)alpha (optional) - Statistical significance level before the Šidák correction (Default: 0.05)num_of_thread (optional) - Maximum number of CPU threads (Default: 4)Outputsg - Results of the significance test (C-dimensional list). sg[c] = True or False indicates that community c is significant or insignificant, respectively.p_values - P-values for the communities (C-dimensional list). p_values[c] is the p-value for community c.Example (examples/example1.py)import networkx as nximport qstest as qsnetwork = nx.karate_club_graph()communities = qs.louvain(network)sg, p_values = qs.qstest(network, communities, qs.qmod, qs.vol, qs.louvain)———————————————————————————————————————————————————————————————————————————How to pass your quality function to qstestWrite a quality function of a community as follows:q = my_qfunc(network, community)Inputnetwork - Networkx Graph class instancecommunity - List of nodes belonging to a communityOutputq - Quality of the communityThen, pass my_qfunc to qstest:sg, p_values = qstest(network, communities, my_qfunc, sfunc, cdalgorithm)Example (examples/example2.py)import networkx as nximport qstest as qs# Number of intra-community edgesdef 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)———————————————————————————————————————————————————————————————————————————How to pass your community-size function to qstestWrite a community-size function of a community as follows:s = my_sfunc(network, community)Inputnetwork - Networkx Graph class instancecommunity - List of the IDs of nodes belonging to a communityOutputs - Size of the communityThen, pass my_sfunc to qstest:sg, p_values = qstest(network, communities, qfunc, my_sfunc, cdalgorithm)Example (examples/example3.py)import networkx as nximport qstest as qs# Square of the number of nodes in a communitydef my_sfunc(network, nodes):return len(nodes) * len(nodes)network = nx.karate_club_graph()communities = qs.louvain(network)sg, p_values = qs.qstest(network, communities, qs.qmod, my_sfunc, qs.louvain)———————————————————————————————————————————————————————————————————————————How to pass your community-detection algorithm to qstestTo pass your community-detection algorithm to qstest, write a wrapper function of the following form:communities = my_cdalgorithm(network)Inputnetwork - Networkx Graph class instanceOutputcommunities - C-dimensional list of lists. communities[c] is a list containing the IDs of nodes belonging to community c.Then, pass my_cdalgorithm to qstest:sg, p_values = qstest(network, communities, qfunc, sfunc, my_cdalgorithm)If the community-detection algorithm requires parameters such as the number of communities, then pass the parameters as global variables, e.g., define a global variable X, then use X within the cdalgorithm.Example (examples/example4.py)import networkx as nximport qstest as qsfrom networkx.algorithms import community as nxcdalgorithm# Wrapper function for async_fluidc implemented in Networkx 2.0def 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_fluidcC = 3maxiter = 10network = nx.karate_club_graph()communities = my_cdalgorithm(network)sg, p_values = qs.qstest(network, communities, qs.qmod, qs.vol, my_cdalgorithm)———————————————————————————————————————————————————————————————————————————RequirementsPython 2.7, 3.4 or laterSciPy 1.0 or laterNetworkx 2.0 or laterpython-louvain 0.9———————————————————————————————————————————————————————————————————————————Last updated: 29 November 2017