Last active
October 26, 2018 21:51
-
-
Save clayadavis/724061da0f989bff6e7f25cbc21e63fa to your computer and use it in GitHub Desktop.
Compute the multiscale backbone edge probabilities of a NetworkX graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compute_p(G, weight='weight', p='p'): | |
# Undirected | |
for node in G: | |
k_n = len(G[node]) | |
if k_n > 1: | |
sum_w = sum( G[node][neighbor][weight] for neighbor in G[node] ) | |
for neighbor in G[node]: | |
edge_weight = G[node][neighbor][weight] | |
p_ij = float(edge_weight)/sum_w | |
G[node][neighbor][p] = (1-p_ij)**(k_n-1) | |
def alpha_cut(G, alpha, p='p'): | |
C = G.copy() | |
C.remove_edges_from((u,v) for u,v,d in G.edges(data=True) if d['p'] < alpha) | |
return C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In networkx 2.2 should be:
C.remove_edges_from((u,v) for u,v,d in G.edges.data() if d['p'] < alpha)