def dists(G, nbunch = None): G = G.copy() if nbunch is None: nbunch = G.nodes() try: out_degree = G.out_degree(nbunch = nbunch) in_degree = G.in_degree(nbunch = nbunch) gross_out_weight = G.out_degree(weighted = True, nbunch = nbunch) gross_in_weight = G.in_degree(weighted = True, nbunch = nbunch) except TypeError: out_degree = G.out_degree(nbunch = nbunch) in_degree = G.in_degree(nbunch = nbunch) gross_out_weight = G.out_degree(weight = 'weight', nbunch = nbunch) gross_in_weight = G.in_degree(weight = 'weight', nbunch = nbunch) A = nx.to_scipy_sparse_matrix(G, nodelist = nbunch) i, j, grosscells = extract.find(A) selfloops = G.selfloop_edges(data = True) G.remove_edges_from(selfloops) try: net_out_weight = G.out_degree(weighted = True, nbunch = nbunch) net_in_weight = G.in_degree(weighted = True, nbunch = nbunch) except TypeError: net_out_weight = G.out_degree(weight = 'weight', nbunch = nbunch) net_in_weight = G.in_degree(weight = 'weight', nbunch = nbunch) A = nx.to_scipy_sparse_matrix(G, nodelist = nbunch) i, j, netcells = extract.find(A) dists = { 'out-degree': np.array([out_degree[i] for i in nbunch],dtype = np.float32), 'in-degree': np.array([in_degree[i] for i in nbunch],dtype = np.float32), 'gross_out-weight': np.array([gross_out_weight[i] for i in nbunch],dtype = np.float32), 'gross_in-weight': np.array([gross_in_weight[i] for i in nbunch],dtype = np.float32), 'net_out-weight': np.array([net_out_weight[i] for i in nbunch],dtype = np.float32), 'net_in-weight': np.array([net_in_weight[i] for i in nbunch],dtype = np.float32), 'gross_cells': grosscells, 'net_cells': netcells } return dists
def __init__(self, Year, pvalue = 0.01): A =Year.Adj v = float(A.sum()) n, m = A.shape self.sets = (n, m) alpha = pvalue / float(n * m) in_degree = A.sum(0) out_degree = A.sum(1) i, j, aij = extract.find(A) nonzero = len(i) pij = np.zeros((nonzero, )) for h in xrange(nonzero): pij[h] = out_degree[i[h]] * in_degree[0,j[h]] / v**2 P = 1-binom.cdf(aij - 1,v,pij) data = 1. * (P<= alpha) zero_entries = np.where(data == 0) data = np.delete(data, zero_entries) i = np.delete(i, zero_entries) j = np.delete(j, zero_entries) aij = np.delete(aij,zero_entries) ij = np.asarray(zip(i,j)).T self.svnet = csc_matrix((data, ij)) self.Adj = csc_matrix((aij,ij)) self.filename = Year.filename self.edgetype = Year.edgetype self.banks = Year.banks self.firms = Year.firms self.descr = 'valid network'
def __init__(self, Year, pvalue = 0.01): A = Year.Adj n, m = A.shape alpha = pvalue / n / m in_degree = A.sum(0) out_degree = A.sum(1) i, j, wij = extract.find(A) indices = np.where(wij > 0) eps = max(wij[indices].min(),0.1) v = A.sum() / eps nonzero = len(i) pij = np.zeros((nonzero, )) for h in xrange(nonzero): pij[h] = out_degree[i[h]] * in_degree[0,j[h]] / v**2 P = 1 - binom.cdf(wij - 1,v,pij) data = P <= alpha zero_entries = np.where(data == 0) data = np.delete(data, zero_entries) i = np.delete(i, zero_entries) j = np.delete(j, zero_entries) wij = np.delete(wij,zero_entries) ij = np.asarray(zip(i,j)).T self.svnet = csc_matrix((data, ij), shape = (n,m) ) self.Adj = csc_matrix((wij, ij), shape = (n,m) ) self.nodes = Year.nodes self.filename = Year.filename self.edgetype = Year.edgetype
def to_pajek(self): os.chdir('Infomap') W = csc_matrix(self.Adj) row,col,data = extract.find(W) n = len(self.nodes) nodes = np.array(range(n)) nodes = np.resize(nodes,(n,1)) row = np.resize(row,(n,1)) col = np.resize(col,(n,1)) data = np.resize(data,(n,1)) row = 1 + row # NB this is for pajek col = 1 + col # idem edges = np.concatenate((row,col,data),axis = 1) nodes = np.concatenate((nodes + 1,nodes),axis=1) filename = self.filename.split('.')[0] filename = '%s.net' % (filename) output = open(filename,'wb') output.write("*vertices %i\n"%(n)) np.savetxt(output,nodes,fmt = ('%1.1i','%1.10s')) output.write("*arcs\n") np.savetxt(output,edges,fmt = ('%1.1i','%1.1i','%1.18e')) os.chdir('..')
def extract_distance_graph(manifold_distance, graph, start): """ Extract a graph with edges filled with the distance from start. This is mainly for plotting purposes in order to plot the graph, and distances along it. """ pt_graph = sparse.csc_matrix(graph.shape) D = manifold_distance start = 6 for i,j in zip(*find(graph)[:2]): pt_graph[i,j] = D[start,j] if j == start: pt_graph[i,j] = D[i,start] return pt_graph
def extract_distance_graph(manifold_distance, graph, start): """ Extract a graph with edges filled with the distance from start. This is mainly for plotting purposes in order to plot the graph, and distances along it. """ pt_graph = sparse.csc_matrix(graph.shape) D = manifold_distance start = 6 for i, j in zip(*find(graph)[:2]): pt_graph[i, j] = D[start, j] if j == start: pt_graph[i, j] = D[i, start] return pt_graph
def edgelist(self, weighted = True): i, j, data = extract.find(self.Adj) n_edges = len(data) firms = dict(zip(range(len(self.firms)), self.firms)) banks = dict(zip(range(len(self.banks)), self.banks)) edgelist = np.zeros((n_edges, ), dtype = self.edgetype) for k in xrange(n_edges): row = i[k] col = j[k] edgelist['source'][k] = firms[row] edgelist['dest'][k] = banks[col] edgelist['weight'][k] = data[k] return edgelist
def edgelist(self , weighted = True): nodes = dict(zip(range(len(self.nodes)), self.nodes)) i, j, data = extract.find(self.Adj) n_edges = len(data) edgelist = np.zeros((n_edges, ), dtype = self.edgetype) for k in xrange(n_edges): row = i[k] col = j[k] edgelist['source'][k] = nodes[row] edgelist['dest'][k] = nodes[col] edgelist['weight'][k] = data[k] filename = self.filename.split('.')[0] np.savetxt(filename + '.svnet', edgelist, fmt = ['%10s','%10s','%10.10f']) return edgelist
def graph(self): """ Return the k-nearest-neighbour graph with self.k neighbours. Optionally the minimum_spanning_tree is added in, according to self.include_mst. """ if getattr(self, '_graph', None) is None: D = self.manifold_corrected_distance_matrix.toarray() idxs = np.argsort(D) r = range(D.shape[0]) idx = idxs[:, :self.k] self._graph = lil_matrix(D.shape) for neighbours in idx.T: self._graph[r, neighbours] = D[r, neighbours] if self.include_mst: mst = self.minimal_spanning_tree for i,j,v in zip(*find(mst)): if self._graph[i,j] == 0: self._graph[i,j] = v return self._graph
def graph(self): """ Return the k-nearest-neighbour graph with self.k neighbours. Optionally the minimum_spanning_tree is added in, according to self.include_mst. """ if getattr(self, '_graph', None) is None: D = self.manifold_corrected_distance_matrix.toarray() idxs = np.argsort(D) r = range(D.shape[0]) idx = idxs[:, :self.k] self._graph = lil_matrix(D.shape) for neighbours in idx.T: self._graph[r, neighbours] = D[r, neighbours] if self.include_mst: mst = self.minimal_spanning_tree for i, j, v in zip(*find(mst)): if self._graph[i, j] == 0: self._graph[i, j] = v return self._graph
def find(self): for A in self.cases: I, J, V = extract.find(A) assert_equal(A.toarray(), csr_matrix(((I, J), V), shape=A.shape))
def find(self): for A in self.cases: I,J,V = extract.find(A) assert_equal(A.toarray(), csr_matrix(((I,J),V), shape=A.shape))
def threshold(Mcorr, sthresh, hthresh): nelems = shape(Mcorr)[0] rows, cols, dat = find(Mcorr >= hthresh) Mthresh = abs(Mcorr) Mthresh[Mthresh < hthresh] = 0 return Mthresh**sthresh, zip(rows, cols)