def BuildGraph(self): ''' 1 Build graph based on info from 'path_net' 2 Set attributes to graph nodes based on info from 'path_attr' ''' def setatt(g, e, v, name): for i, j in zip(e, v): try: if j > G.node[i[0]][name]: G.node[i[0]][name] = j except: G.node[i[0]][name] = j try: if j > G.node[i[1]][name]: G.node[i[1]][name] = j except: G.node[i[1]][name] = j return g p, e = self.ReadNet() if self.path_attr is not None: self.attr = self.ReadAttr() if self.mode == 'di': G = DiGraph() else: G = Graph() G.add_nodes_from(range(len(p))) for i, j in zip(G.GetNodes(), p): G.node[i]['pos'] = j e = np.array(e) - 1 G.add_edges_from(e.tolist()) # set diameter/radius try: d = np.array(self.attr['Dia']).ravel().astype(float) G = setatt(G, e, d, 'd') G = setatt(G, e, d / 2, 'r') except: print('--Cannot set diam!') # set flow try: flow = np.array(self.attr['flow']).ravel().astype(float) G = setatt(G, e, flow, 'flow') except: print('--Cannot set flow!') # set po2 try: po2 = np.array(self.attr['ppO2']).ravel().astype(float) G = setatt(G, e, po2, 'po2') except: print('--Cannot set po2!') self.G = fixG(G)
def BuildGraph(self): ''' read nodes with their pos and diameter, read edges ''' d, p, e, f = self.ReadLines() G=DiGraph() G.add_nodes_from(range(len(p))) for i, j in zip(G.GetNodes(), p): G.node[i]['pos']=j e=np.array(e)-1 G.add_edges_from(e.tolist()) for i, j in zip(e, d): try: if j>G.node[i[0]]['d']: G.node[i[0]]['d']=j except: G.node[i[0]]['d']=j try: if j>G.node[i[1]]['d']: G.node[i[1]]['d']=j except: G.node[i[1]]['d']=j self.G=fixG(G)
def fix_indexing(g_old, root): bfs=list(nx.bfs_predecessors(treegraph, root)) old_indices=[root] old_indices.extend([i[0] for i in bfs]) new_indices=range(len(old_indices)) mapping={old_indices[i]:new_indices[i] for i in new_indices} g=DiGraph() g.add_nodes_from(new_indices) for i in old_indices: g.node[mapping[i]]['pos']=g_old.node[i]['pos'] g.node[mapping[i]]['r']=g_old.node[i]['r'] edges_old=g_old.GetEdges() edges_new=[[mapping[e[0]], mapping[e[1]]] for e in edges_old] g.add_edges_from(edges_new) return g
except: pass for i in DiG.GetNodes(): try: DiG.node[i]['inflow'] = G.node[i]['inflow'] except: pass try: DiG.node[i]['outflow'] = G.node[i]['outflow'] except: pass print('Build directed graph ...') di_g = DiGraph() di_g.add_nodes_from(g.GetNodes()) di_g.add_edges_from(di_edges) TransferAttributes(di_g, g) # ---------------------------------------------# # ------ build A from cycles ---------# print('Build A matrix ...') # assigne currents contibuting to each edge currents_in_edges = dict() for ed in di_g.GetEdges(): cyc = [] for idx, c in enumerate(cycles): if ed in c: cyc.append(idx)