def drawGraphFromBatch(mybatch, index): G=cnv.to_networkx(getSparseData(mybatch.x[index],mybatch.adj[index],mybatch.mask[index])) G=G.to_undirected() pos= graphviz_layout(G) nx.draw(G,pos,alpha=0.75) return G, pos
def draw(data, node_size=1000, font_size=12, save_img_file=None): """ input: (torch_geometric.data.data.Data, path or string) effect: show and save graph data, with graphviz layout visualization """ G = to_networkx(data) pos = nx.nx_pydot.graphviz_layout(G) if (data.edge_attr != None): edge_labels = {(u, v): lab for u, v, lab in data.edge_attr} if (data.node_attr != None): node_labels = dict(zip(G.nodes, data.node_attr)) nx.draw(G, pos=pos, nodecolor='r', edge_color='b', node_size=node_size, with_labels=False) nx.draw_networkx_labels(G, pos=pos, labels=node_labels, font_size=font_size) nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_labels, font_size=font_size) print(G.nodes) print(G.edges) if save_img_file != None: plt.savefig(save_img_file) plt.show() return
def remove_Connected(dataset): custom_dataset = [] for data in dataset: g = cnv.to_networkx(data).to_undirected() no_of_components = nx.connected_components(g) maxset = [] maxsize = 0 count = 0 for comp in no_of_components: count += 1 comp_size = len(comp) if(comp_size > maxsize): maxsize = comp_size maxset = comp maxset = list(maxset) adj = nx.adjacency_matrix(g).todense() adj = adj[maxset,:] adj = adj[:,maxset] g2 = nx.from_numpy_matrix(adj).to_undirected() custom_dataset += [cnv.from_networkx(g2)] return custom_dataset
def visualize_graph(instance): graph = to_networkx(instance) fig = plt.figure(1, figsize=(14, 12)) nx.draw(graph, node_size=75, linewidths=6, node_color='red', edge_color='#00b4d9') plt.savefig('./first_graph.png')
def _adjust_t(self, data): r"""adjust data type for current transform.""" if self._data_t == "tensor": data_np2tensor(data) elif self._data_t == "np": data_tensor2np(data) elif self._data_t == "nx": if not hasattr(data, "G") or data.G is None: data.G = to_networkx(data, to_undirected=True)
def node_insertion(data): #torch_geometric.data.Data type ''' input a graph, randomly select a strongly-connected sub-graph S, remove all edges in S, add a node n, and add an edge between n and each node in S, return the processed graph torch tensor: :param data.x: [num_nodes,num_node_features] :param data.edge_index: [2,num_edges] :param data.edge_attr: [num_edges, num_edge_features] for now we do not consider edge features :param data.y: [graph_label_dimension] :return: torch_geometric.data.Data ''' num_nodes = data.x.shape[0] G = to_networkx(data) con = nx.strongly_connected_components(G) component_list = [] for component in list(con): if len(component) > 1: component_list.append(list(component)) if len(component_list) < 1: return data random_number = random.randint(0, len(component_list) - 1) G.add_node(num_nodes) discrete = is_onehot(data.x[0]) if discrete == False: new_node = torch.mean(data.x[component_list[random_number], :], dim=0) new_node = new_node.unsqueeze(dim=0) else: component_features = data.x[component_list[random_number], :] new_node = get_discrete_attr_for_inserted_node(component_features) new_node_features = torch.cat((data.x, new_node), dim=0) edge_index_list = data.edge_index.t().long().detach().cpu().numpy().tolist( ) all_edges_in_component = [] for u in component_list[random_number]: for v in component_list[random_number]: if [u, v] in edge_index_list: all_edges_in_component.append((u, v)) G.remove_edges_from(all_edges_in_component) for node in component_list[random_number]: G.add_edge(node, num_nodes) G.add_edge(num_nodes, node) new_edge_index = torch.Tensor(list(G.edges)).t().contiguous().long() new_graph = Data(x=new_node_features, y=data.y, edge_index=new_edge_index, edge_attr=None) return new_graph
def plot_graph(graph_data): import networkx as nx from torch_geometric.utils.convert import to_networkx from matplotlib import pyplot as plt graph = to_networkx(graph_data) plt.figure(1, figsize=(7, 6)) nx.draw(graph, cmap=plt.get_cmap('Set1'), node_size=75, linewidths=6) plt.show()
def render(self, root=None): # graph = self.data task_list = [t.barcode for t in self.task_list] graph = to_networkx(Data(self.x, self.edge_index.contiguous())) pos = graphviz_layout(graph, prog='dot', root=root) # pos = graphviz_layout(G, prog='twopi') node_color = [color_normalized[task[0]] for task in task_list] # plt.figure(figsize=(8, 8)) nx.draw_networkx_nodes(graph, pos, node_color=node_color) nx.draw_networkx_edges(graph, pos)
def test_visualize_cora(self): """ export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64:/usr/local/cudnn-10.0-v7.6.5.32 proxychains python -c "from template_lib.examples.DGL.geometric.test_pytorch_geometric import TestingGeometric;\ TestingGeometric().test_learning_methods_on_graphs()" """ if 'CUDA_VISIBLE_DEVICES' not in os.environ: os.environ['CUDA_VISIBLE_DEVICES'] = '0' if 'PORT' not in os.environ: os.environ['PORT'] = '6006' if 'TIME_STR' not in os.environ: os.environ['TIME_STR'] = '0' if utils.is_debugging() else '1' # func name assert sys._getframe().f_code.co_name.startswith('test_') command = sys._getframe().f_code.co_name[5:] class_name = self.__class__.__name__[7:] \ if self.__class__.__name__.startswith('Testing') \ else self.__class__.__name__ outdir = f'results/{class_name}/{command}' from datetime import datetime TIME_STR = bool(int(os.getenv('TIME_STR', 0))) time_str = datetime.now().strftime("%Y%m%d-%H_%M_%S_%f")[:-3] outdir = outdir if not TIME_STR else (outdir + '_' + time_str) print(outdir) import collections, shutil shutil.rmtree(outdir, ignore_errors=True) os.makedirs(outdir, exist_ok=True) import networkx as nx import torch import numpy as np import pandas as pd from torch_geometric.datasets import Planetoid from torch_geometric.utils.convert import to_networkx dataset1 = Planetoid(root='datasets/cora', name='Cora') cora = dataset1[0] coragraph = to_networkx(cora) node_labels = cora.y[list(coragraph.nodes)].numpy() import matplotlib.pyplot as plt plt.figure(1, figsize=(14, 12)) nx.draw(coragraph, cmap=plt.get_cmap('Set1'), node_color=node_labels, node_size=75, linewidths=6) plt.show() pass
def get_diameter(graph): networkx_graph = to_networkx(graph).to_undirected() sub_graph_list = [ networkx_graph.subgraph(c) for c in connected_components(networkx_graph) ] sub_graph_diam = [] for sub_g in sub_graph_list: sub_graph_diam.append(diameter(sub_g)) return max(sub_graph_diam)
def plot_mapper_graph(args): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print("Plotting the {} graph".format(args.dataset)) data, num_classes, legend_dict = load_dataset(args.dataset) data = data.to(device) graph = to_networkx(data).to_undirected() print("Graph nodes", graph.number_of_nodes()) print("Graph edges", graph.number_of_edges()) if args.lens == 'PR': pr = nx.pagerank(graph) embed = np.empty(len(pr)) for node, score in pr.items(): embed[node] = score embed = embed[:, None] elif args.lens == 'density': d_path = "./data/{}_distance_matrix.npy".format(args.dataset) if os.path.isfile(d_path): print('Using existing distance matrix') d = np.load(d_path) else: d = get_distance_matrix(graph, args.cutoff) np.save(d_path, d) embed = np.sum(np.exp(-d / args.scale), axis=-1)[:, None] else: raise ValueError('Unsupported lens {}'.format(args.lens)) embed = reduce_embedding(embed, reduce_dim=args.reduce_dim, method=args.reduce_method) print('Creating visualisation...') colorbar = embed.shape[1] == 1 mapper_graph, res = DGM(num_intervals=args.intervals, overlap=args.overlap, eps=args.eps, min_component_size=args.min_component_size, sdgm=args.sdgm).fit_transform(graph, embed) plot_graph(mapper_graph, node_color=res['mnode_to_color'], node_size=res['node_sizes'], edge_weight=res['edge_weight'], node_list=res['node_list'], name=gtl_name_from_args(args, False), save_dir=args.dir, colorbar=colorbar) print("Filtered Mapper Graph nodes", mapper_graph.number_of_nodes()) print("Filtered Mapper Graph edges", mapper_graph.number_of_edges()) print("Nodes from the original graph", np.sum(res['node_sizes'])) binary_labels = (args.reduce_method == 'binary_prob') labeled_colors = color_mnodes_with_labels(res['mnode_to_nodes'], data.y.cpu().numpy(), binary=binary_labels) plot_graph(mapper_graph, node_color=labeled_colors, node_size=res['node_sizes'], edge_weight=res['edge_weight'], node_list=res['node_list'], name=gtl_name_from_args(args, True), save_dir=args.dir, colorbar=binary_labels, legend_dict=legend_dict)
def visualize_(predictions_save_path, use_predicted_graph, pos_threshold, layout, color_clusters, axes_off, output_fname, size): assert layout in ['spring', 'latent'] pred = torch.load(predictions_save_path) G = to_networkx( pred['G']) if not use_predicted_graph else nx.from_scipy_sparse_matrix( sps.csr_matrix(pred['A'] >= pos_threshold)) t_data = pd.DataFrame( (spring_layout(G, dim=3).values() if layout == 'spring' else PCA( n_components=3, random_state=SEED).fit_transform(pred['z'])), columns=['x', 'y', 'z']) t_data['color'] = pred['cl'] if color_clusters else 1 pp = PlotlyPlot() pp.add_plot(t_data, G, size=size) pp.plot(output_fname=output_fname, axes_off=axes_off)
def get_graph_diameter(data): networkx_graph = to_networkx(data).to_undirected() sub_graph_list = [ networkx_graph.subgraph(c) for c in connected_components(networkx_graph) ] sub_graph_diam = [] for sub_g in sub_graph_list: sub_graph_diam.append(diameter(sub_g)) data.diameter = max(sub_graph_diam) if data.x is None: data.x = torch.ones(data.num_nodes, 1) return data
def edge_insertion(data): #torch_geometric.data.Data type ''' input a graph, randomly select two nodes, if they are not directly connected but there is a path between them,add an edge between these two nodes, return the processed graph torch tensor: :param data.x: [num_nodes,num_node_features] :param data.edge_index: [2,num_edges] :param data.edge_attr: [num_edges, num_edge_features] for now we do not consider edge features :param data.y: [graph_label_dimension] :return: torch_geometric.data.Data ''' num_nodes = data.x.shape[0] G = to_networkx(data) succeed = False random_nodes = random.sample(range(0, num_nodes), 2) edge_index_list = data.edge_index.t().detach().cpu().numpy().tolist() if nx.has_path(G, source=random_nodes[0], target=random_nodes[1]) and nx.has_path( G, source=random_nodes[1], target=random_nodes[0]): edge_between_nodes = [random_nodes[0], random_nodes[1]] in edge_index_list if not edge_between_nodes: edge1 = torch.Tensor([random_nodes[0], random_nodes[1]]).unsqueeze(dim=1).long() edge2 = torch.Tensor([random_nodes[1], random_nodes[0]]).unsqueeze(dim=1).long() new_edge_index = torch.cat((data.edge_index, edge1, edge2), dim=1) succeed = True if succeed == False: return edge_deletion(data) else: data.edge_index = new_edge_index.contiguous() data.edge_attr = None return data
def visualize_graph(data, labels, positions=None): data_graph = to_networkx(data) pos = nx.kamada_kawai_layout(data_graph) # labels = data.y[list(data_graph)].cpu().detach().numpy() plt.figure(1, figsize=(14, 12)) nx.draw_networkx_nodes(data_graph, pos=pos, nodelist=data_graph.nodes, node_color='r', alpha=0.8) # nx.draw(data_graph, cmap=plt.get_cmap('Set1'), node_size=75, linewidths=6) nx.draw_networkx_edges(data_graph, pos=pos, edgelist=data_graph.edges, width=1.0, alpha=0.5) nx.draw_networkx_labels(data_graph, pos=pos, labels=labels, font_size=16) plt.show()
def get_graph_diameter(data): ''' compute the graph diameter and add the attribute to data object :param data: the graph :return: the graph representation augmented with diameter attribute ''' networkx_graph = to_networkx(data).to_undirected() sub_graph_list = [ networkx_graph.subgraph(c) for c in connected_components(networkx_graph) ] sub_graph_diam = [] for sub_g in sub_graph_list: sub_graph_diam.append(diameter(sub_g)) data.diameter = max(sub_graph_diam) if data.x is None: data.x = torch.ones(data.num_nodes, 1) return data
def display_graph(state, q_values=None, node_size=3000): """display a graph state using networkx.""" pos_map = {i: pos.numpy() for i, pos in enumerate(state.pos.cpu())} # Swap x, y, invert y pos_map = {i: np.array([x, y]) for i, (y, x) in pos_map.items()} max_y = max([y for x, y in pos_map.values()]) pos_map = {i: np.array([x, max_y - y]) for i, (x, y) in pos_map.items()} # node color features = state.x.cpu()[:, :4] colors = np.empty((features.size(0), 3)) types = np.empty(features.size(0), dtype=str) for i, m in enumerate(MEANING_TO_COLOR.keys()): temp = torch.all(features == MEANING_TO_STATE[m], -1).numpy() colors[temp] = MEANING_TO_COLOR[m] types[temp] = m # display q values on each node if q_values is not None: labels = { i: f"{types[i]} [{i}] \n{value.item():.5f}" for i, value in enumerate(q_values) } else: labels = {i: "{} [{}] ".format(types[i], i) for i in range(len(state.x))} nx.draw( to_networkx(state), node_color=colors, labels=labels, node_size=node_size, linewidths=1, font_color="w", font_size=10, pos=pos_map, )
def visualize_graph( graph, pos, im, suffix, idx, vis_dir, mode="full", edge_colors=None, node_colors=None, true_graph=None ): im = np.rollaxis(im, axis=0, start=3) network = to_networkx(graph) plt.figure() plt.imshow(im) if mode == "only_edges": true_network = to_networkx(true_graph) nx.draw_networkx_edges( true_network, pos=pos, arrowstyle="-", style="dashed", alpha=0.8, node_size=15, edge_color="white", arrowsize=1, connectionstyle="arc3,rad=0.2", ) nx.draw_networkx( network, pos=pos, cmap=plt.get_cmap("inferno"), node_color="white", node_size=15, linewidths=1, arrowstyle="-", edge_color=edge_colors, arrowsize=1, with_labels=False, connectionstyle="arc3,rad=0.2", vmin=0.0, vmax=1.0, width=2.0, ) suffix = suffix + "_" + str(int(time.time() * 100)) elif mode == "triang": node_colors = np.linspace(0, 1, len(network.nodes)) nx.draw_networkx( network, pos=pos, cmap=plt.get_cmap("Set1"), node_color=node_colors, node_size=15, linewidths=5, arrowsize=1, with_labels=False, vmin=0.0, vmax=1.0, arrowstyle="-", ) elif mode == "full": node_colors = np.linspace(0, 1, len(network.nodes)) edge_labels = {graph.edge_index[:, i]: f"{i}" for i in range(graph.edge_index.shape[1])} nx.draw_networkx( network, pos=pos, cmap=plt.get_cmap("Set1"), node_color=node_colors, node_size=100, linewidths=10 ) nx.draw_networkx_edge_labels(network, pos=pos, edge_labels=edge_labels, label_pos=0.3) elif mode == "only_nodes": node_colors = np.linspace(0, 1, len(network.nodes)) nx.draw_networkx_nodes( network, pos=pos, cmap=plt.get_cmap("Set1"), node_color=node_colors, node_size=15, linewidths=1 ) elif mode == "nograph": pass else: raise NotImplementedError filename = os.path.join(vis_dir, f"{idx}_{suffix}.png") plt.savefig(filename) plt.close() abs_filename = os.path.abspath(filename) return abs_filename
def drawGraphFromData(myx,myadj,mask,seed=None,nodecolor=False,edgecolor=False,seedhops=False,hoplabels=False,binarycut=False): if myx.unsqueeze(-1).shape[1]>1: myx=myx[:,0] #pad x values to fit standardized form newx = padToData(myx,Data(x=myx,adj = myadj)) #convert to nx graph G=cnv.to_networkx(getSparseData(myx,myadj,mask)) G=G.to_undirected() pos= graphviz_layout(G) nofnodes= G.number_of_nodes() if nodecolor: #initialize color matrices for the plots nodecolors=torch.zeros(nofnodes,3) colv=myx[:nofnodes] colv=torch.log(colv+1e-6) colv=(colv-colv.min())/(colv.max()-colv.min()) #assign the values to the red channel nodecolors[:,0]= colv #assign some constant value to other channels, black nodes can be confusing nodecolors[:nofnodes,1]= 0.0*torch.ones_like(colv) nodecolors[:nofnodes,2]= 0.0*torch.ones_like(colv) if seedhops == True: positions={} withoutseed={} theseed=seed shortestpaths=nx.shortest_path_length(G,theseed) maxpath= list(shortestpaths.values())[-1] orderpaths = {} orderpathswoseed={} for index in range(nofnodes): if nx.has_path(G,seed,index): orderpaths[index] = str(shortestpaths[index]) if index != theseed: positions[index] = pos[index] orderpathswoseed[index]=shortestpaths[index] else: orderpaths[index] = -1 positions[index]=pos[index] orderpathswoseed[index]=-1 if binarycut: cutnodes = list(myx.nonzero().reshape(-1).numpy()) cutedg = nx.edge_boundary(G,cutnodes) cutedges = [] for k in cutedg: cutedges += [k] cutedges = set(cutedges) cutpaths = [] cutpos = {} for i in cutnodes: if nx.has_path(G,seed,i): cutpaths += [shortestpaths[i]] cutpos[i] = pos[i] else: cutpaths += [-1] cutpos[i] = pos[i] nx.draw_networkx_nodes(G,cutpos,nodelist=cutnodes,alpha=0.5,node_color=[[1, 0, 0]],node_shape='o',node_size=1000) for key in shortestpaths: if key != theseed: scale = 1- shortestpaths[key]/maxpath nodecolors[key,2] = scale*0.7 + 0.3 nodecolors[key,1] = scale*0.7 + 0.3 withoutseed[key] = key #print(position) else: scale = 1- shortestpaths[key]/maxpath position = {key: pos[key]} nx.draw_networkx_nodes(G,position,alpha=1.0,nodelist=[key],node_color='r',node_size=1200*scale) nx.draw_networkx_nodes(G,positions,alpha=0.65,nodelist=list(positions.keys()),node_color=list(orderpathswoseed.values()),vmin=0,vmax=maxpath,cmap=plt.cm.hsv,node_size=450) if hoplabels: nx.draw_networkx_labels(G,pos,labels=orderpaths,font_color='k',alpha=0.75) else: nodecolors = 'r' if seedhops == False: nx.draw_networkx_nodes(G,pos,alpha=0.75,node_color=nodecolors,node_size=200) if edgecolor: edgecolors= torch.zeros(G.number_of_edges(),3) count=0 edgecolvec= torch.zeros(G.number_of_edges()) for i in G.edges(): edgecolvec[count] = data.adj[i] count+=1; print(edgecolvec) edgecolors[:,1]= edgecolvec edgecolors[:,0]= 0.2*torch.ones_like(edgecolvec) edgecolors[:,2]= 0.2*torch.ones_like(edgecolvec) nx.draw_networkx_edges(G,pos,alpha=1, width=edgecolvec.numpy()) else: edgecolor= None if binarycut == False: nx.draw_networkx_edges(G,pos,alpha=0.5) else: nx.draw_networkx_edges(G,pos,G.edges()-cutedges,alpha=0.5) nx.draw_networkx_edges(G,pos,cutedges,alpha=0.5,width=5,edge_color='r') return G, pos
import torch.nn.functional as F from torch.nn import ModuleList from torch_geometric.datasets import KarateClub from torch_geometric.datasets import Planetoid import torch_geometric.transforms as T from torch_geometric.nn import GCNConv, ChebConv # noq from torch_geometric.utils import convert import matplotlib.pyplot as plt from sklearn.manifold import TSNE import networkx as nx dataset = 'Cora' path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', dataset) dataset = Planetoid(path, dataset, transform=T.TargetIndegree()) data = dataset[0] G = convert.to_networkx(data) colors = [ '#ffc0cb', '#bada55', '#008080', '#420420', '#7fe5f0', '#065535', '#ffd700' ] co = [] b = ['c3', 'c2', 'c2', 'c2', 'c2', 'c0', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c3', 'c2', 'c2', 'c2', 'c2', 'c1', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c0', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c4', 'c1', 'c1', 'c1', 'c1', 'c1', 'c3', 'c6', 'c4', 'c4', 'c4', 'c4', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c0', 'c2', 'c1', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c6', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c5', 'c5', 'c5', 'c5', 'c5', 'c5', 'c2', 'c2', 'c2', 'c2', 'c2', 'c6', 'c6', 'c3', 'c0', 'c0', 'c6', 'c0', 'c5', 'c0', 'c0', 'c3', 'c3', 'c0', 'c0', 'c6', 'c6', 'c6', 'c3', 'c3', 'c3', 'c3', 'c1', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c4', 'c3', 'c1', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c5', 'c5', 'c5', 'c5', 'c0', 'c5', 'c5', 'c5', 'c2', 'c2', 'c2', 'c3', 'c4', 'c3', 'c4', 'c3', 'c3', 'c2', 'c5', 'c5', 'c0', 'c6', 'c6', 'c5', 'c5', 'c5', 'c5', 'c6', 'c3', 'c3', 'c0', 'c3', 'c1', 'c1', 'c3', 'c1', 'c6', 'c5', 'c6', 'c6', 'c6', 'c6', 'c6', 'c0', 'c0', 'c3', 'c0', 'c0', 'c0', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c4', 'c4', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c4', 'c4', 'c5', 'c5', 'c5', 'c5', 'c3', 'c5', 'c5', 'c5', 'c5', 'c5', 'c5', 'c4', 'c4', 'c4', 'c0', 'c3', 'c4', 'c4', 'c4', 'c6', 'c6', 'c6', 'c6', 'c6', 'c0', 'c5', 'c5', 'c5', 'c0', 'c5', 'c3', 'c0', 'c0', 'c3', 'c3', 'c3', 'c1', 'c3', 'c1', 'c3', 'c3', 'c3', 'c6', 'c3', 'c3', 'c1', 'c6', 'c1', 'c4', 'c3', 'c4', 'c3', 'c3', 'c3', 'c3', 'c3', 'c6', 'c0', 'c3', 'c0', 'c0', 'c0', 'c0', 'c0', 'c0', 'c0', 'c6', 'c0', 'c0', 'c6', 'c0', 'c0', 'c6', 'c1', 'c3', 'c6', 'c5', 'c6', 'c6', 'c4', 'c4', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c4', 'c3', 'c3', 'c3', 'c4', 'c1', 'c1', 'c5', 'c1', 'c0', 'c6', 'c0', 'c0', 'c0', 'c0', 'c0', 'c0', 'c0', 'c2', 'c3', 'c5', 'c5', 'c5', 'c3', 'c3', 'c3', 'c3', 'c3', 'c0', 'c6', 'c6', 'c0', 'c0', 'c0', 'c0', 'c3', 'c3', 'c3', 'c1', 'c1', 'c1', 'c1', 'c1', 'c2', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c3', 'c1', 'c1', 'c1', 'c1', 'c1', 'c5', 'c5', 'c0', 'c6', 'c6', 'c3', 'c3', 'c5', 'c1', 'c1', 'c1', 'c4', 'c6', 'c6', 'c6', 'c6', 'c2', 'c3', 'c3', 'c0', 'c3', 'c3', 'c4', 'c4', 'c4', 'c4', 'c4', 'c4', 'c4', 'c4', 'c4', 'c4', 'c4', 'c4', 'c0', 'c6', 'c0', 'c6', 'c6', 'c3', 'c1', 'c1', 'c3', 'c3', 'c3', 'c3', 'c1', 'c1', 'c1', 'c6', 'c3', 'c6', 'c6', 'c2', 'c6', 'c3', 'c6', 'c6', 'c6', 'c0', 'c6', 'c6', 'c6', 'c6', 'c6', 'c3', 'c3', 'c6', 'c6', 'c6', 'c2', 'c2', 'c3', 'c5', 'c0', 'c0', 'c6', 'c6', 'c3', 'c3', 'c3', 'c0', 'c0', 'c0', 'c0', 'c0', 'c6', 'c5', 'c5', 'c0', 'c4', 'c6', 'c0', 'c6', 'c4', 'c6', 'c2', 'c2', 'c5', 'c0', 'c0', 'c0', 'c0', 'c0', 'c0', 'c0', 'c0', 'c4', 'c4', 'c4', 'c3', 'c5', 'c6', 'c1', 'c1', 'c3', 'c3', 'c3', 'c3', 'c3', 'c6', 'c1', 'c0', 'c2', 'c2', 'c4', 'c4', 'c4', 'c4', 'c4', 'c5', 'c0', 'c3', 'c3', 'c3', 'c0', 'c1', 'c2', 'c5', 'c4', 'c4', 'c4', 'c4', 'c4', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c4', 'c4', 'c4', 'c1', 'c1', 'c4', 'c1', 'c6', 'c5', 'c3', 'c3', 'c3', 'c4', 'c0', 'c4', 'c4', 'c4', 'c5', 'c0', 'c0', 'c3', 'c5', 'c5', 'c5', 'c5', 'c5', 'c0', 'c5', 'c0', 'c0', 'c6', 'c2', 'c6', 'c5', 'c6', 'c3', 'c5', 'c5', 'c5', 'c5', 'c5', 'c4', 'c4', 'c3', 'c3', 'c6', 'c4', 'c6', 'c3', 'c5', 'c5', 'c5', 'c1', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c2', 'c3', 'c4', 'c3', 'c0', 'c0', 'c3', 'c1', 'c3', 'c6', 'c3', 'c1', 'c1', 'c1', 'c0', 'c1', 'c5', 'c1', 'c1', 'c1', 'c1', 'c1', 'c1', 'c0', 'c1', 'c4', 'c0', 'c2', 'c4', 'c4', 'c3', 'c1', 'c1', 'c1', 'c6', 'c6', 'c3', 'c3', 'c4', 'c4', 'c0', 'c4', 'c4', 'c0', 'c4', 'c4', 'c4', 'c4', 'c0', 'c0', 'c0', 'c4', 'c2', 'c3', 'c3', 'c4', 'c5', 'c0', 'c2', 'c2', 'c3', 'c3', 'c3', 'c3', 'c3', 'c2', 'c0', 'c5', 'c5', 'c4', 'c2', 'c4', 'c3', 'c4', 'c3', 'c4', 'c4', 'c3', 'c3', 'c4', 'c4', 'c6', 'c2', 'c2', 'c2', 'c2', 'c4', 'c0', 'c0', 'c6', 'c0', 'c3', 'c4', 'c4', 'c4', 'c3', 'c3', 'c0', 'c5', 'c3', 'c5', 'c0', 'c3', 'c3', 'c3', 'c3', 'c2', 'c3', 'c2', 'c3', 'c3', 'c0', 'c0', 'c3', 'c2', 'c6', 'c6', 'c2', 'c0', 'c0', 'c1', 'c1', 'c1', 'c6', 'c4', 'c4', 'c5', 'c4', 'c4', 'c6', 'c1', 'c1', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c2', 'c5', 'c2', 'c2', 'c2', 'c0', 'c6', 'c6', 'c2', 'c6', 'c6', 'c2', 'c2', 'c6', 'c5', 'c4', 'c4', 'c4', 'c2', 'c0', 'c0', 'c2', 'c2', 'c3', 'c4', 'c4', 'c4', 'c3', 'c2', 'c3', 'c1', 'c6', 'c6', 'c5', 'c1', 'c0', 'c0', 'c6', 'c3', 'c1', 'c1', 'c4', 'c4', 'c5', 'c2', 'c3', 'c1', 'c3', 'c4', 'c5', 'c4', 'c0', 'c3', 'c3', 'c1', 'c2', 'c1', 'c1', 'c5', 'c2', 'c3', 'c3', 'c6', 'c0', 'c2', 'c3', 'c2', 'c2', 'c5', 'c3', 'c4', 'c3', 'c4', 'c4', 'c2', 'c2', 'c4', 'c2', 'c4', 'c5', 'c5', 'c3', 'c2', 'c3', 'c1', 'c0', 'c3', 'c3', 'c4', 'c5', 'c4', 'c3', 'c3', 'c3', 'c1', 'c3', 'c0', 'c5', 'c2', 'c4', 'c4', 'c4', 'c3', 'c3', 'c3', 'c1', 'c2', 'c3', 'c2', 'c2', 'c2', 'c3', 'c2', 'c2', 'c3', 'c4', 'c4', 'c2', 'c2', 'c2', 'c3', 'c2', 'c2', 'c2', 'c2', 'c3', 'c3', 'c0', 'c0', 'c4', 'c3', 'c3', 'c3', 'c2', 'c3', 'c4', 'c2', 'c2', 'c2', 'c4', 'c3', 'c4', 'c4', 'c1', 'c5', 'c3', 'c6', 'c3', 'c2', 'c2', 'c1', 'c3', 'c2', 'c2', 'c0', 'c0', 'c6', 'c3', 'c2', 'c2', 'c6', 'c2', 'c3', 'c5', 'c2', 'c3', 'c2', 'c4', 'c2', 'c5', 'c4', 'c4', 'c0', 'c5', 'c6', 'c6', 'c3', 'c3', 'c3', 'c2', 'c5', 'c3', 'c3', 'c4', 'c3', 'c3', 'c3', 'c3', 'c3', 'c4', 'c6', 'c6', 'c4', 'c2', 'c2', 'c2', 'c5', 'c4', 'c4', 'c4', 'c4', 'c5', 'c3', 'c2', 'c2', 'c5', 'c2', 'c2', 'c2', 'c2', 'c2', 'c3', 'c4', 'c4', 'c4', 'c3', 'c1', 'c4', 'c4', 'c3', 'c3', 'c6', 'c4', 'c4', 'c4', 'c4', 'c4', 'c4', 'c1', 'c4', 'c4', 'c4', 'c6', 'c4', 'c4', 'c4', 'c4', 'c2', 'c3', 'c3', 'c3', 'c2', 'c3', 'c2', 'c6', 'c3', 'c4', 'c4', 'c4', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c3', 'c2', 'c1', 'c3', 'c3'] print(b[0:3]) for a in data.y.cpu(): co += [colors[a]] fig = plt.figure(figsize=(8, 8)) pos = nx.spring_layout(G) im = nx.draw_networkx_nodes(G, pos, node_color=co, node_size=10) nx.draw_networkx_edges(G, pos)
def visualize_on_map(data, labels): ''' Credits: Code adapted from https://github.com/tuangauss/DataScienceProjects/blob/master/Python/flights_networkx.py Args: data (pytorch geometric graph): labels (dictionary): Returns: Displays/saves a plot ''' df = pd.read_csv(SOURCE_PATH / '../dataset/lat_lon_usa.csv') plt.figure(figsize=(15, 20)) m = Basemap(projection='merc', llcrnrlon=-180, llcrnrlat=10, urcrnrlon=-50, urcrnrlat=70, lat_ts=0, resolution='l', suppress_ticks=True) data_graph = to_networkx(data) pos = {} temp_x, temp_y = [], [] edge_weights = data.edge_attr.numpy() degrees = dict(data_graph.degree) edges = data_graph.edges strong_nodes = [] weak_nodes = [] node_strengths = {} weightage = 0.5 for index, edge in enumerate(edges): n1, n2 = edge if n1 in node_strengths: node_strengths[n1] += edge_weights[index] else: node_strengths[n1] = edge_weights[index] + weightage * degrees[n1] if n2 in node_strengths: node_strengths[n2] += edge_weights[index] else: node_strengths[n2] = edge_weights[index] + weightage * degrees[n2] print(degrees) print(labels) for k, v in node_strengths.items(): print("State: ", labels[k], " Strength: ", v) # The top x nodes get assigned the strong nodes category x = 10 counter = 0 for k, v in sorted(node_strengths.items(), key=lambda item: item[1], reverse=True): if counter < x: strong_nodes.append(k) else: weak_nodes.append(k) counter += 1 # Assign cor-ordinates to plot the graph for index, node in enumerate(data_graph.nodes): state = labels[node] lon = df.loc[df['State'] == state, 'lon'].item() lat = df.loc[df['State'] == state, 'lat'].item() temp_x.append(lon) temp_y.append(lat) mx, my = m(temp_x, temp_y) for index, val in enumerate(data_graph.nodes): pos[val] = (mx[index], my[index]) nx.draw_networkx_nodes(data_graph, pos=pos, nodelist=strong_nodes, node_size=20, node_color='r', alpha=0.8) nx.draw_networkx_nodes(data_graph, pos=pos, nodelist=weak_nodes, node_size=15, node_color='b', alpha=0.6) nx.draw_networkx_edges(data_graph, pos=pos, edgelist=data_graph.edges, width=1.0, alpha=0.1, arrows=False, edge_color='g') nx.draw_networkx_labels(data_graph, pos=pos, labels=labels, font_size=8) m.drawcountries(linewidth=3) m.drawstates(linewidth=0.2) m.drawcoastlines(linewidth=1) m.fillcontinents(alpha=0.3) line1 = mlines.Line2D(range(1), range(1), color="white", marker='o', markerfacecolor="red") line2 = mlines.Line2D(range(1), range(1), color="white", marker='o', markerfacecolor="blue") line3 = mlines.Line2D(range(1), range(1), color="green", marker='', markerfacecolor="green") plt.legend((line1, line2, line3), ('Strong nodes', 'Weak nodes', 'Edges'), loc=4, fontsize='xx-large') plt.savefig(SOURCE_PATH / '../results/usmap.png') plt.show()
y=y_data, edge_attr=edge_features) data = self.transform(data=data) return data if __name__ == '__main__': m_x = np.random.randn(4, 5).astype(np.int) m_y = torch.as_tensor(np.random.binomial(size=(4, 1), n=1, p=0.5).ravel()) m_x_dist = distance.pdist(m_x, metric='euclidean') m_dist_graph = distance.squareform(m_x_dist) m_final_graph = np.where(m_dist_graph > 1, m_dist_graph, 0) print(m_final_graph) # data = create_pytorch_dataset(final_graph=final_graph, x_data=x, y_data=y) # print(data) m_dataset = ABIDEDataset(final_graph=m_final_graph, x_data=m_x, y_data=m_y) m_data = m_dataset[0] # inv_transform = T.ToDense() # print(data) # data = inv_transform(data) print(m_data) m_graph = to_networkx(data=m_data, edge_attrs=['edge_attr'], to_undirected=True) # nx.draw(graph, with_labels=False, font_weight='bold', node_color=data.y, cmap="Set2") m_labels = nx.get_edge_attributes(m_graph, 'edge_attr') m_pos = nx.spring_layout(m_graph) nx.draw(m_graph, pos=m_pos, with_labels=True) nx.draw_networkx_edge_labels(m_graph, m_pos, edge_labels=m_labels) plt.savefig('./sample.png')
def plot_dgm_graph(args): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print("Plotting the {} graph".format(args.dataset)) data, num_classes, legend_dict = load_dataset(args.dataset) data = data.to(device) graph = to_networkx(data).to_undirected() print("Graph nodes", graph.number_of_nodes()) print("Graph edges", graph.number_of_edges()) if args.true_labels: # The visualisation is driven by the labels. embed = data.y.cpu().numpy().astype(np.float32)[:, None] else: embed_path = "./data/{}_{}.npy".format(args.dataset, args.train_mode) if os.path.isfile(embed_path): print('Using existing embedding') embed = np.load(embed_path) else: print('No embedding found. Training a new model...') embed = train_model(data, args.train_mode, num_classes, device) np.save(embed_path, embed) embed = reduce_embedding(embed, reduce_dim=args.reduce_dim, method=args.reduce_method) print('Creating visualisation...') out_graph, res = DGM(num_intervals=args.intervals, overlap=args.overlap, eps=args.eps, min_component_size=args.min_component_size, sdgm=args.sdgm).fit_transform(graph, embed) binary = args.reduce_method == 'binary_prob' if not args.true_labels: plot_graph(out_graph, node_color=res['mnode_to_color'], node_size=res['node_sizes'], edge_weight=res['edge_weight'], node_list=res['node_list'], name=dgm_name_from_args(args, False), save_dir=args.dir, colorbar=binary) print("Filtered Mapper Graph nodes", out_graph.number_of_nodes()) print("Filtered Mapper Graph edges", out_graph.number_of_edges()) labeled_colors = color_mnodes_with_labels(res['mnode_to_nodes'], data.y.cpu().numpy(), binary=binary) plot_graph(out_graph, node_color=labeled_colors, node_size=res['node_sizes'], edge_weight=res['edge_weight'], node_list=res['node_list'], name=dgm_name_from_args(args, True), save_dir=args.dir, colorbar=binary, legend_dict=legend_dict)
x.append([8, AST_NODES.index(type(curr))]) if isinstance(curr, ast.FunctionDef): node_names[node_counter.index(curr)] += f': {curr.name}' elif isinstance(curr, ast.arg): node_names[node_counter.index(curr)] += f': {curr.arg}' elif isinstance(curr, AST_ASYNC_AWAIT): x.append([9, AST_NODES.index(type(curr))]) elif isinstance(curr, AST_TOP_LEVEL): x.append([10, AST_NODES.index(type(curr))]) else: x.append([11], -1) for child in ast.iter_child_nodes(curr): next_nodes.append(child) node_counter.append(child) child_idx = len(node_counter) - 1 node_names.append(clean_class(str(type(child)))) edge_index.append([node_counter.index(curr), child_idx]) data = Data(x=torch.tensor(x, dtype=torch.float), edge_index=torch.tensor(edge_index, dtype=torch.long).t().contiguous()) graph = to_networkx(data) png_graph = nx.drawing.nx_pydot.to_pydot(graph) for i, n in enumerate(node_counter): png_graph.get_node(str(i))[0].set_label(node_names[i] + f'\n {i}: {str(x[i])}') # png_graph.get_node('0')[0].set_label(type(node_counter[0])) png_graph.write_png('img/' + args.filename.split('/')[-1].replace('.py', '') + '_AST.png')
if elem!=s: c=c+1 s=elem l1.append(c) for i,elem2 in enumerate(new_edge_index[1]): for j,elem in enumerate(new_edge_index[0]): if elem2==elem: l2[i]=l1[j] batch['edge_index'] = torch.stack((torch.tensor(l1),torch.tensor(l2))) # to label the nodes by their class color = cand_ids_one_hot.argmax(axis=1)[batch['top_50_index']] print('color', color) G_type_50 = to_networkx(batch, node_attrs=['top_50_values'], edge_attrs=None, to_undirected=True, remove_self_loops=False) nx.draw(G_type_50, font_weight='bold', node_color=color) plt.savefig("eta_505.png") print() # G_type = to_networkx(batch, node_attrs=['type'], edge_attrs=['edge_weight'], to_undirected=True, remove_self_loops=False) # # nx.draw(G_type) # # plt.savefig("type.png") # pos=nx.spring_layout(G_type) #G is my graph # nx.draw(G_type,pos,node_color='#A0CBE2',edge_color='#BB0000',width=2,edge_cmap=plt.cm.Blues,with_labels=True) # plt.savefig("type.png", dpi=500, facecolor='w', edgecolor='w',orientation='portrait', papertype=None, format=None,transparent=False, bbox_inches=None, pad_inches=0.1) # print('finished 1st graph..') # batch['pt']=res[:,1] # G_pt = to_networkx(batch, node_attrs=['pt'], edge_attrs=['edge_weight'], to_undirected=True, remove_self_loops=False)
''' 每一个表是一个图数据 ''' # print(os.path.splitext(os.path.basename("xxxxx.text"))[0]) # with open(r'F:\imgs\SciTSR\train\chunk\0704.2596v1.2.chunk',"r") as f: # #print(json.load(f)["cells"]) # st = json.load(f)["chunks"] # x_min = min(st, key=lambda p: p["pos"][0])["pos"][0] # x_max = max(st, key=lambda p: p["pos"][1])["pos"][1] # y_min = min(st, key=lambda p: p["pos"][2])["pos"][2] # y_max = max(st, key=lambda p: p["pos"][3])["pos"][3] # print(x_min,x_max,y_min,y_max) rootPath = r"F:\imgs\SciTSR\train" test = "Y" dataset = GFTE_POS_DATASET(rootPath, None, None) print(dir(dataset)) print(dataset[0]) print(dataset[0].keys) print(dataset[0]['edge_index']) print(dataset[0]['y']) print(sum(dataset[0]['y'])) edge_index = dataset[0]['edge_index'] edge_index = edge_index.t() print(edge_index) g = to_networkx(dataset[0]) nx.draw(g) plt.show()
import networkx as nx import torch import numpy as np import pandas as pd from torch_geometric.datasets import Planetoid from torch_geometric.utils.convert import to_networkx dataset1 = Planetoid(root = '/content/cora',name='Cora') cora = dataset1 [0] coragraph = to_networkx(cora) node_labels = cora.y[list(coragraph.nodes)].numpy() import matplotlib.pyplot as plt plt.figure(1,figsize=(14,12)) nx.draw(coragraph, cmap=plt.get_cmap('Set1'),node_color = node_labels,node_size=75,linewidths=6) plt.show()
def render(self): def color_task(task): colors = [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]] if task in self.running: time_proportion =1 - (self.ready_proc[self.running_task2proc[task]] - self.time)/\ self.task_data.task_list[task].duration_cpu color_time = [1., time_proportion, time_proportion] return color_time elif task in self.ready_tasks: return colors[1] return colors[2] def color_processor(processor): if self.running[processor] == -1: return [0, 1, 0] if self.current_proc == processor else [0.7, 0.7, 0.7] else: time_proportion = (self.ready_proc[processor] - self.time) / \ self.task_data.task_list[self.running[processor]].duration_cpu return [time_proportion, 0, 0] visible_graph, node_num = compute_sub_graph(self.task_data, torch.tensor(np.concatenate((self.running[self.running > -1], self.ready_tasks)), dtype=torch.long), self.window) plt.figure(figsize=(8 , 8)) plt.suptitle('time: {}'.format(self.time)) plt.subplot(121) plt.box(on=None) visible_graph.render(root=list(self.running[self.running > -1])) # plt.title('time: {}'.format(self.time)) # plt.show() plt.subplot(122) plt.box(on=None) graph = to_networkx(Data(visible_graph.x, visible_graph.edge_index.contiguous())) pos = graphviz_layout(graph, prog='dot', root=None) # pos = graphviz_layout(G, prog='tree') node_color = [color_task(task[0].item()) for task in node_num] # plt.figure(figsize=(8, 8)) nx.draw_networkx_nodes(graph, pos, node_color=node_color) nx.draw_networkx_edges(graph, pos) labels = {} for i, task in enumerate(node_num): if task[0].item() in self.ready_tasks: labels[i] = task[0].item() nx.draw_networkx_labels(graph, pos, labels, font_size=16) # plt.title('time: {}'.format(self.time)) plt.show() # Cluster edges_list = [(u, v, {"cost": self.cluster.communication_cost[u, v]}) for u in range(self.p) for v in range(self.p) if u != v] colors = [color_processor(p) for p in range(self.p)] G = nx.Graph() G.add_nodes_from(list(range(len(self.cluster.node_types)))) G.add_edges_from(edges_list) pos = graphviz_layout(G) node_labels = {} for i, node_type in enumerate(self.cluster.node_types): node_labels[i] = ["CPU", "GPU"][node_type] plt.figure(figsize=(8, 8)) nx.draw_networkx_nodes(G, pos=pos, node_color=colors, node_size=1000) nx.draw_networkx_edges(G, pos=pos) nx.draw_networkx_edge_labels(G, pos=pos) nx.draw_networkx_labels(G, pos, node_labels, font_size=16) plt.show()