def node2node_flow_from_rank(adj, rank): # Get flow matrix of the network from the PageRank # and degree of its nodes N = adj.shape[0] T = atMatrix.toRightStochastic(adj) flow = np.zeros((N, N)) for ii in range(N): for jj in range(N): flow[ii, jj] = T[ii, jj] * rank[ii] return flow
def node2com_trans_from_trans(T, member, dist=None, direction='to'): T = np.matrix(atMatrix.toRightStochastic(T)) N = T.shape[0] if (dist is None) and (direction == 'from'): dist = pagerank(T) dist = np.matrix(dist).reshape(N, 1) coms = np.unique(member) ncom = coms.shape[0] if direction == 'to': n2c_trans = np.matrix(np.empty((N, ncom))) elif direction == 'from': n2c_trans = np.matrix(np.empty((ncom, N))) for k in range(ncom): inodes = member == coms[k] if direction == 'to': n2c_trans[:, k] = T[:, inodes].sum(1) elif direction == 'from': n2c_trans[k, :] = np.multiply(T[inodes, :], np.tile(dist[inodes], (1, N))).sum(0) / dist[inodes].sum() else: sys.exit('Direction must be "to" or "from".') return n2c_trans
def pagerank(T, tol=0, maxiter=0): N = T.shape[0] # Get transition matrix T = atMatrix.toRightStochastic(T) # Get First Eigen-Vector x = np.matrix(np.ones((N, )) * 1. / N) xi = x * T k = 1 eps = np.abs(xi - x).sum() iteration = 0 while eps > tol: x = xi.copy() xi = x * T k += 1 eps = np.abs(xi - x).sum() iteration += 1 if (maxiter > 0) and (iteration >= maxiter): break return xi
def node2com_flow_from_trans(T, member, dist=None, direction='to'): T = np.matrix(atMatrix.toRightStochastic(T)) N = T.shape[0] if dist is None: dist = pagerank(T) dist = np.matrix(dist).reshape(N, 1) coms = np.unique(member) ncom = coms.shape[0] if direction == 'to': n2c_flow = np.matrix(np.empty((N, ncom))) elif direction == 'from': n2c_flow = np.matrix(np.empty((ncom, N))) for k in range(ncom): inodes = member == coms[k] if direction == 'to': n2c_flow[:, k] = np.multiply(T[:, inodes].sum(1), dist) elif direction == 'from': n2c_flow[k, :] = np.multiply(T[inodes, :], np.tile(dist[inodes], (1, N))).sum(0) else: sys.exit('Direction must be "to" or "from".') return n2c_flow
def pagerank(T, tol=0, maxiter=0): N = T.shape[0] # Get transition matrix T = atMatrix.toRightStochastic(T) # Get First Eigen-Vector x = np.matrix(np.ones((N,)) * 1. / N) xi = x * T k = 1 eps = np.abs(xi - x).sum() iteration = 0 while eps > tol: x = xi.copy() xi = x * T k += 1 eps = np.abs(xi - x).sum() iteration += 1 if (maxiter > 0) and (iteration >= maxiter): break return xi