Package: xswap
XSwap: Fast degree-preserving network permutationXSwap is an algorithm for degree-preserving network randomization (permutation) [1].
Permuted networks can be used for a number of purposes in network analysis, including for generating counterfactual distributions of features when only the network's degree sequence is maintained or for computing a prior probability of an edge given only the network's degree sequence.
Overall, permuted networks allow one to quantify the effects of degree on analysis and prediction methods.
Understanding this effect is useful when a network's degree sequence is subject to biases.
This implementation is a modified version of the algorithm due to Hanhijärvi et al. with two additional parameters (allow_self_loopsandallow_antiparallel), which enable greater generalizability to bipartite, directed, and undirected networks.Randomization Techniques for GraphsSami Hanhijärvi, Gemma C. Garriga, Kai PuolamäkiProceedings of the 2009 SIAM International Conference on Data Mining(2009-04-30)https://doi.org/f3mn58DOI:10.1137/1.9781611972795.67Usage examplesPermuting an edge list>>>edges=[(0,1),(1,0)]>>>permuted_edges,permutation_statistics=xswap.permute_edge_list(edges,allow_self_loops=True,allow_antiparallel=True,multiplier=10)>>>permuted_edges[(0,0),(1,1)]>>>permutation_statistics{'swap_attempts':20,'same_edge':10,'self_loop':0,'duplicate':1,'undir_duplicate':0,'excluded':0}Computing degree-sequence based prior probabilities of edges existing>>>edges=[(0,1),(1,0)]>>>prior_prob_df=xswap.prior.compute_xswap_priors(edges,n_permutations=10000,shape=(2,2),allow_self_loops=True,allow_antiparallel=True)>>>prior_prob_dfsource_idtarget_idedgesource_degreetarget_degreexswap_prior000False110.5101True110.5210True110.5311False110.5Choice of parametersBipartite networksBipartite networks should be indexed using the bi-adjacency matrix, meaning that the edge(0, 0)is from source node 0 to target node 0, and is not a self-loop.
Moreover, bipartite networks should be permuted usingallow_self_loops=Falseandallow_antiparallel=True.Directed and undirected networksFor non-bipartite networks, the decisions ofallow_self_loopsandallow_antiparallelare not always the same.
For undirected networks, setallow_antiparallel=False, as otherwise the edges (1, 0) and (0, 1), which represent the same edge, will be treated as separate.
Antiparallel edges may or may not be allowed for directed networks, depending on context.
Similarly, self-loops may or may not be allowed for directed or undirected networks, depending on the specific network being permuted.LibrariesThe XSwap library includes Roaring Bitmaps (https://github.com/RoaringBitmap/CRoaring), available under the Apache 2.0 license (https://github.com/RoaringBitmap/CRoaring/blob/LICENSE).AcknowledgmentsDevelopment of this project has largely taken place in theGreene Labat the University of Pennsylvania. However, as an open source project under thehetioorganization, this repository is grateful for its community of maintainers, contributors, and users.This work is funded in part by the Gordon and Betty Moore Foundation’s Data-Driven Discovery Initiative through Grants GBMF4552 to Casey Greene, GBMF4560 to Blair Sullivan, and the National Institutes of Health’s National Human Genome Research Institute R01 HG010067.