def Update(self, FullyCC=False): filenameVertices = self.filenameVertices filenameEdges = self.filenameEdges P1, P2, P11, P22 = self.readCGAL(filenameEdges, filenameVertices) p, intersections, c = self.getGraph(P1, P2, P11, P22) G = Graph() G.add_nodes_from(range(np.shape(p)[0])) G.add_edges_from(np.ndarray.tolist(c)) for i in range(np.shape(p)[0]): G.node[i]['pos'] = p[i, :] G.to_undirected() if FullyCC == True: # connected components graphs = list(nx.connected_component_subgraphs(G)) s = 0 ind = 0 for idx, i in enumerate(graphs): if len(i) > s: s = len(i) ind = idx G = graphs[ind] G = fixG(G) self.Graph = G
def ReadGraphfromMat(filename): f = sio.loadmat(filename) mat = f['im2'][0, 0] nX = int(mat['nX']) nY = int(mat['nY']) nZ = int(mat['nZ']) scale = mat['Hvox'][0] xx = int(nX * scale[0]) yy = int(nY * scale[1]) zz = int(nZ * scale[2]) # read nodes pos = mat['nodePos'].astype(float) radii = mat['nodeDiam'].T # read edges edg = (mat['nodeEdges']).astype('int') connections = [] for i in range(len(edg)): connections.append((edg[i, 0] - 1, edg[i, 1] - 1)) from VascGraph.GeomGraph import Graph G = Graph() G.add_nodes_from(range(pos.shape[0])) G.add_edges_from(connections) for i, p, r in zip(G.GetNodes(), pos, radii): G.node[i]['pos'] = p G.node[i]['r'] = r return G
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 __CreateGraph(self): # build graph self.Graph = Graph() ind = np.array(range(self.NNodes)) self.Graph.add_nodes_from(ind) for i, p, r in zip(ind, self.GraphNodes, self.GraphRadius): self.Graph.node[i]['pos'] = p self.Graph.node[i]['r'] = r self.Graph.add_edges_from(self.Connections) self.Graph.remove_edges_from(self.Graph.selfloop_edges()) self.Graph = fixG(self.Graph)
def to_undirected(self, reciprocal=False, as_view=False): if as_view is True: return nx.graphviews.GraphView(self) # deepcopy when not a view from VascGraph.GeomGraph import Graph G = Graph() G.graph.update(deepcopy(self.graph)) G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items()) if reciprocal is True: G.add_edges_from((u, v, deepcopy(d)) for u, nbrs in self._adj.items() for v, d in nbrs.items() if v in self._pred[u]) else: G.add_edges_from((u, v, deepcopy(d)) for u, nbrs in self._adj.items() for v, d in nbrs.items()) return G
def __init__(self, path): self.path = path try: with open(self.path, 'r') as f: lines = f.readlines() except: print('Cannot read file!') start = [idx + 1 for idx, i in enumerate(lines) if i[0] == '#'] end = [idx - 1 for idx, i in enumerate(lines) if i[0] == '#'] end = end[1:] for idx, i in enumerate(lines[end[-1]:]): if i[0] == '[': end.append(end[-1] + idx) break pathes = [] for s, e in zip(start, end): a = np.array([[float(j) for j in i.split(' ') if j != ''] for i in lines[s:e]]) pathes.append(a[:, (2, 3, 4)]) g = Graph() for i in pathes: n = g.number_of_nodes() nodes = range(n, n + len(i)) g.add_nodes_from(nodes) for idx, k in enumerate(nodes): g.node[k]['pos'] = np.array(i[idx]) e1 = nodes[0:-1] e2 = nodes[1:] e = [[k1, k2] for k1, k2 in zip(e1, e2)] g.add_edges_from(e) self.graph = g
def __GenerateRandomGraphFromLabel(self): #random sampling x = np.random.uniform(low=0, high=self.Shape[0], size=self.NInitialNodes).tolist() y = np.random.uniform(low=0, high=self.Shape[1], size=self.NInitialNodes).tolist() z = np.random.uniform(low=0, high=self.Shape[2], size=self.NInitialNodes).tolist() NodesIndices = self.Label[(np.floor(x).astype('int'), np.floor(y).astype('int'), np.floor(z).astype('int'))] > 0 Index = np.array([x, y, z]).T NodesPos = Index[NodesIndices] # build graph self.NNodes = len(NodesPos) self.Graph = Graph() self.Graph.add_nodes_from(range(self.NNodes)) # assign positions to nodes for i, p in zip(self.Graph.GetNodes(), NodesPos): self.Graph.node[i]['pos'] = p # build connectivity Tree = sp.spatial.cKDTree(NodesPos) NeigborsIndices = Tree.query(NodesPos, k=self.Connection + 1)[1] Edges = [] for ind, i in enumerate(NeigborsIndices): Neigbours = np.unique(i) c = [[ind, j] for j in Neigbours if j != ind and j != self.NNodes] if c: Edges.append(c) #assign connections Edges = [j for i in Edges for j in i] # unravel self.Graph.add_edges_from(Edges)
def __CreateGraph(self): # build graph self.Graph = Graph() totalnodes = 0 for nodes, c, radii, n in zip(self.SegmentsNodes, self.SegmentsConnections, self.SegmentsNodesRadii, self.NNodes): ind = np.array(range(n)) + totalnodes self.Graph.add_nodes_from(ind) for i, p, r in zip(ind, nodes, radii): self.Graph.node[i]['pos'] = p self.Graph.node[i]['r'] = r self.Graph.add_edges_from(c + totalnodes) totalnodes += n self.Graph.remove_edges_from(self.Graph.selfloop_edges()) self.Graph = fixG(self.Graph)
def __GenerateRandomGridGraphFromLabel(self): IndexTrueVoxels = np.where(self.Label) Index = np.array(IndexTrueVoxels).T #Limit NNodes to # of ture voxels if self.NNodes > len(Index): self.NNodes = len(Index) # probibility of true voxels Probability = (self.Label).astype(float) / np.sum(self.Label) Probability = Probability[IndexTrueVoxels] # obtain nodes NodesIndices = np.random.choice(range(len(Probability)), self.NNodes, p=Probability) NodesPos = Index[NodesIndices] # build graph self.Graph = Graph() self.Graph.add_nodes_from(range(self.NNodes)) # assign positions to nodes for i, p in zip(self.Graph.GetNodes(), NodesPos): self.Graph.node[i]['pos'] = p # build connectivity Tree = sp.spatial.cKDTree(NodesPos) NeigborsIndices = Tree.query(NodesPos, k=self.Connection + 1)[1] Edges = [] for ind, i in enumerate(NeigborsIndices): Neigbours = np.unique(i) c = [[ind, j] for j in Neigbours if j != ind and j != self.NNodes] if c: Edges.append(c) #assign connections Edges = [j for i in Edges for j in i] # unravel self.Graph.add_edges_from(Edges)
def Update(self): try: x1, x2 = self.xls['x1'], self.xls['x2'] y1, y2 = self.xls['y1'], self.xls['y2'] z1, z2 = self.xls['z1'], self.xls['z2'] except: x1, x2 = self.xls['V1 x'], self.xls['V2 x'] y1, y2 = self.xls['V1 y'], self.xls['V2 y'] z1, z2 = self.xls['V1 z'], self.xls['V2 z'] ps = [(i, j, k) for i, j, k in zip(x1, y1, z1)] # start node pe = [(i, j, k) for i, j, k in zip(x2, y2, z2)] # end node # all nodes with their id's and pos's p = list(set(ps).union(set(pe))) pid = dict() pos = dict() for idx, i in enumerate(p): pid[str(i)] = idx pos[idx] = i # graph nodes = range(len(p)) e = [(pid[str(i)], pid[str(j)]) for i, j in zip(ps, pe)] edges = [i for i in e if i[0] != i[1]] g = Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) for i in g.GetNodes(): g.node[i]['pos'] = np.array(pos[i]) self.Graph = g
def __GenerateGridGraphFromLabel(self): def VoxelsPositions(Label, Shape, Length): ''' Shape: shape of array indexing in order: rows by row->depth ''' # positions of each voxel z, x, y = np.meshgrid(range(Shape[0]), range(Shape[1]), range(Shape[2]), indexing='ij') x = x[Label.astype(bool)] y = y[Label.astype(bool)] z = z[Label.astype(bool)] VoxelsPos = np.transpose([z, x, y]) return VoxelsPos def GetConnections(Label, Shape, Length): # connections from pathways on array grid Array = (np.reshape(range(Length), Shape) + 1) * Label # incides of voxels in the Array VoxelsIndices = Array[Label.astype(bool)] #-------- path1 = iter( np.transpose( [Array[:, :, 0:-1].ravel(), Array[:, :, 1:].ravel()])) path1 = (i for i in path1 if all(i)) #-------- path2 = iter( np.transpose([ np.swapaxes(Array[:, 0:-1, :], 1, 2).ravel(), np.swapaxes(Array[:, 1:, :], 1, 2).ravel() ])) path2 = (i for i in path2 if all(i)) #-------- path3 = iter( np.transpose([ np.swapaxes(Array[0:-1, :, :], 0, 2).ravel(), np.swapaxes(Array[1:, :, :], 0, 2).ravel() ])) path3 = (i for i in path3 if all(i)) return VoxelsIndices, path1, path2, path3 if self.Sampling is not None: Scale = (1.0 / self.Sampling, 1.0 / self.Sampling, 1.0 / self.Sampling) Label = image.zoom(self.Label.astype(int), Scale) Shape = np.shape(Label) # size of image Length = Shape[0] * Shape[1] * Shape[2] # number of voxels else: Label = self.Label Shape = self.Shape Length = self.Length # voxel indices and thier positions t1 = time() VoxelsPos = VoxelsPositions(Label, Shape, Length) print('create nodes: ' + str(time() - t1)) t1 = time() VoxelsIndices, Connections1, Connections2, Connections3 = GetConnections( Label, Shape, Length) print('create connections: ' + str(time() - t1)) # build graph t1 = time() self.Graph = Graph() self.Graph.add_nodes_from(VoxelsIndices) for ind, p in zip(VoxelsIndices, VoxelsPos): self.Graph.node[ind]['pos'] = p self.Graph.add_edges_from(Connections1) self.Graph.add_edges_from(Connections2) self.Graph.add_edges_from(Connections3) #exclude nodes with less than 2 neighbors NNodesToExclude = 1 while NNodesToExclude > 0: NodesToExclude = [ i for i in self.Graph.GetNodes() if len(self.Graph.GetNeighbors(i)) <= 2 ] self.Graph.remove_nodes_from(NodesToExclude) NNodesToExclude = len(NodesToExclude) if self.Sampling is not None: for i in self.Graph.GetNodes(): self.Graph.node[i][ 'pos'] = self.Graph.node[i]['pos'] * self.Sampling print('create graph: ' + str(time() - t1))
def __GenerateGridGraphFromLabel(self): def VoxelsPositions(Label, Shape, Length): ''' Shape: shape of array indexing in order: rows by row->depth ''' # positions of each voxel z, x, y = np.meshgrid(range(Shape[0]), range(Shape[1]), range(Shape[2]), indexing='ij') x = x[Label.astype(bool)] y = y[Label.astype(bool)] z = z[Label.astype(bool)] VoxelsPos = np.transpose([z, x, y]) return VoxelsPos def GetConnections(Label, Shape, Length): # connections from pathways on array grid Array = (np.reshape(range(Length), Shape) + 1) * Label # incides of voxels in the Array VoxelsIndices = Array[Label.astype(bool)] #-------- path1 = iter( np.transpose( [Array[:, :, 0:-1].ravel(), Array[:, :, 1:].ravel()])) path1 = (i for i in path1 if all(i)) #-------- path2 = iter( np.transpose([ np.swapaxes(Array[:, 0:-1, :], 1, 2).ravel(), np.swapaxes(Array[:, 1:, :], 1, 2).ravel() ])) path2 = (i for i in path2 if all(i)) #-------- path3 = iter( np.transpose([ np.swapaxes(Array[0:-1, :, :], 0, 2).ravel(), np.swapaxes(Array[1:, :, :], 0, 2).ravel() ])) path3 = (i for i in path3 if all(i)) return VoxelsIndices, path1, path2, path3 if self.Sampling is not None: Scale = (1.0 / self.Sampling, 1.0 / self.Sampling, 1.0 / self.Sampling) Label = image.zoom(self.Label.astype(int), Scale) Shape = np.shape(Label) # size of image Length = Shape[0] * Shape[1] * Shape[2] # number of voxels else: Label = self.Label Shape = self.Shape Length = self.Length # voxel indices and thier positions t1 = time() VoxelsPos = VoxelsPositions(Label, Shape, Length) print('create nodes: ' + str(time() - t1)) t1 = time() VoxelsIndices, Connections1, Connections2, Connections3 = GetConnections( Label, Shape, Length) print('create connections: ' + str(time() - t1)) # build graph t1 = time() self.Graph = Graph() self.Graph.add_nodes_from(VoxelsIndices) for ind, p in zip(VoxelsIndices, VoxelsPos): self.Graph.node[ind]['pos'] = p self.Graph.add_edges_from(Connections1) self.Graph.add_edges_from(Connections2) self.Graph.add_edges_from(Connections3) #exclude nodes with less than 2 neighbors # NNodesToExclude=1 # while NNodesToExclude>0: # NodesToExclude=[i for i in self.Graph.GetNodes() if len(self.Graph.GetNeighbors(i))<=2] # self.Graph.remove_nodes_from(NodesToExclude) # NNodesToExclude=len(NodesToExclude) # reconnect nodes with less that 2 edges (request from Sreekanth ) pos = np.array(self.Graph.GetNodesPos()) nodes = np.array(self.Graph.GetNodes()) NodesToModify = [ i for i in self.Graph.GetNodes() if len(self.Graph.GetNeighbors(i)) <= 2 ] pos_xc_nodes = np.array( [self.Graph.node[k]['pos'] for k in NodesToModify]) new_edges = [] for nn, pp in zip(NodesToModify, pos_xc_nodes): checkp = pos - pp[None, :] checkp = np.sum(checkp**2, axis=1)**0.5 ed_nodes = nodes[checkp <= 2**0.5] new_ed = [[nn, kk] for kk in ed_nodes if kk != nn] #print(len(new_ed)) new_edges.append(new_ed) new_edges = [k2 for k1 in new_edges for k2 in k1] self.Graph.add_edges_from(new_edges) # label extremity nodes at imageborders if self.label_ext: maxx, maxy, maxz = Label.shape maxx -= 1 maxy -= 1 maxz -= 1 pos = self.Graph.GetNodesPos() ext = [] for n, p in zip(self.Graph.GetNodes(), self.Graph.GetNodesPos()): if p[0] == 0 or p[0] == maxx: self.Graph.node[n]['ext'] = 1 if p[1] == 0 or p[1] == maxy: self.Graph.node[n]['ext'] = 1 if p[2] == 0 or p[2] == maxz: self.Graph.node[n]['ext'] = 1 try: dumb = self.Graph.node[n]['ext'] except: self.Graph.node[n]['ext'] = 0 if self.Sampling is not None: for i in self.Graph.GetNodes(): self.Graph.node[i][ 'pos'] = self.Graph.node[i]['pos'] * self.Sampling print('create graph: ' + str(time() - t1))
def __init__(self, filepath, sampling=1): from VascGraph.GeomGraph import Graph try: import h5py except: print('To run this function, \'h5py\' sould be installed.') return # ---- read ----# f=h5py.File(filepath, 'r') refs=[i[0] for i in f.get('GeodesicMSTs/CGPathContinuous')] pathes=[np.array(f[i]).T for i in refs] data=f.get('GeodesicMSTsMatrix/M/data') ir=np.array(f.get('GeodesicMSTsMatrix/M/ir')) jc=np.array(f.get('GeodesicMSTsMatrix/M/jc')) # ----- sampling of pathes nodes ----# ind=[len(i)/sampling for i in pathes] ind=[np.array(range(i))*sampling for i in ind] pathes=[i[indx] for i, indx in zip(pathes, ind)] # ----- build graph from pathes -----# path_ext=[] g=Graph() for path in pathes: n=g.number_of_nodes() nodes=np.array(range(len(path)))+n e1=nodes[1:] e2=nodes[:-1] e=np.array([e1,e2]).T path_ext.append([nodes[0], nodes[-1]]) g.add_nodes_from(nodes) g.add_edges_from(e) for node, pos in zip(nodes, path): g.node[node]['pos']=np.array([pos[1], pos[0], pos[2]]) # ------- connection between pathes ----# path_ext=np.array(path_ext) a = sparse.csc_matrix((data, ir, jc)) ind1, ind2 = np.where(a.todense()>0) e=[] for i,j in zip(ind1,ind2): ee=[[path_ext[i][0], path_ext[j][1]], [path_ext[i][1], path_ext[j][0]], [path_ext[i][0], path_ext[j][0]], [path_ext[i][1], path_ext[j][1]]] poss=np.array([[g.node[k[0]]['pos'], g.node[k[1]]['pos']] for k in ee]) poss= poss[:,0,:]-poss[:,1,:] norm=np.linalg.norm(poss, axis=1) indx=np.where(norm==norm.min())[0][0] e.append(ee[indx]) g.add_edges_from(e) self.graph=g
def ReadFile(self): G_init = nx.read_pajek(self.filename) self.G_init = G_init if self.mode == 'di': G = DiGraph() else: G = Graph() # build geometry for i in list(G_init.nodes()): node = G_init.node[i] # add node #n=int(node['id'].encode()) n = int(i) G.add_node(n) #add position if sys.version_info[0] >= 3: pos = node['pos'] else: pos = node['pos'].encode() pos = pos.split(' ') xyz = [] for j in range(len(pos)): try: value = float(pos[j]) xyz.append(value) except: try: value = pos[j].split('[') try: xyz.append(float(value[1])) except: value = value[1].split(',') xyz.append(float(value[0])) except: try: value = pos[j].split(']') try: xyz.append(float(value[0])) except: value = value[1].split(',') xyz.append(float(value[0])) except: try: value = pos[j].split(',') xyz.append(float(value[0])) except: pass G.node[n]['pos'] = np.array(xyz) # add label try: yORn = node['node'].encode() if yORn == 'False': G.node[n]['node'] = False else: G.node[n]['node'] = True except: pass # add radius try: radius = node['d'].encode() G.node[n]['d'] = float(radius) except: pass # add radius try: radius = node['r'].encode() G.node[n]['r'] = float(radius) except: pass # add radius try: radius = node['r'].encode() G.node[n]['r'] = float(radius.split('[')[1].split(']')[0]) except: pass # add radius try: radius = node['d'].encode() G.node[n]['d'] = float(radius.split('[')[1].split(']')[0]) except: pass # add type try: t = node['type'].encode() G.node[n]['type'] = int(t) except: pass # add branch try: b = node['branch'].encode() G.node[n]['branch'] = int(b) except: pass # add inflow try: b = node['inflow'].encode() G.node[n]['inflow'] = str(int(b)) except: pass # add outflow try: b = node['outflow'].encode() G.node[n]['outflow'] = str(int(b)) except: pass # add sink try: b = node['sink'].encode() G.node[n]['sink'] = str(int(b)) except: pass # add source try: b = node['source'].encode() G.node[n]['source'] = str(int(b)) except: pass # add root try: b = node['root'].encode() G.node[n]['root'] = str(int(b)) except: pass # add flow try: b = node['flow'].encode() G.node[n]['flow'] = float(b) except: pass # add pressure try: b = node['pressure'].encode() G.node[n]['pressure'] = float(b) except: pass # add velocity try: b = node['velocity'].encode() G.node[n]['velocity'] = float(b) except: pass # add velocity try: b = node['so2'].encode() G.node[n]['so2'] = float(b) except: pass # add velocity try: b = node['po2'].encode() G.node[n]['po2'] = float(b) except: pass #build Topology raw_edges = list(G_init.edges()) edges = [(int(i[0]), int(i[1])) for i in raw_edges] G.add_edges_from(edges) for i, j in zip(raw_edges, edges): try: G[j[0]][j[1]]['res'] = G_init[i[0]][i[1]][0]['res'] except: pass try: G[j[0]][j[1]]['flow'] = G_init[i[0]][i[1]][0]['flow'] except: pass try: G[j[0]][j[1]]['pressure'] = G_init[i[0]][i[1]][0]['res'] except: pass try: G[j[0]][j[1]]['inflow'] = G_init[i[0]][i[1]][0]['inflow'] except: pass try: G[j[0]][j[1]]['outflow'] = G_init[i[0]][i[1]][0]['outflow'] except: pass try: G[j[0]][j[1]]['branch'] = G_init[i[0]][i[1]][0]['branch'] except: pass try: G[j[0]][j[1]]['velocity'] = G_init[i[0]][i[1]][0]['velocity'] except: pass try: G[j[0]][j[1]]['pressure'] = G_init[i[0]][i[1]][0]['pressure'] except: pass try: G[j[0]][j[1]]['vol'] = G_init[i[0]][i[1]][0]['vol'] except: pass self.G = G