def corr_graph(Source=list(windows_df_filtered_tb.Table.unique()), Destination=list(windows_df_filtered_tb.Table.unique())): sp_graph, sp_list, sp_colors, sp_sizes = shortest_path(windows_df_filtered_tb,Source,Destination) # Check if the sp_list returned is not NULL which means there is no path and if sp_list == 1 this means that the Table name in Source and Destination is the same if sp_list is not None and len(sp_list) > 1: sp_gr=Network(notebook=True, bgcolor="#222222", font_color="white") sp_gr.add_nodes(sp_list, value=sp_sizes, title=sp_list, color=sp_colors) sp_gr.barnes_hut() sp_gr.from_nx(sp_graph) return(sp_gr.show("graphs/shortest_path_graph.html"))
def create_example_viz_2(filename): g = Network() g.add_nodes( [1,2,3], value=[10, 100, 400], title=["I am node 1", "node 2 here", "and im node 3"], x=[21.4, 54.2, 11.2], y=[100.2, 23.54, 32.1], label=["NODE 1", "NODE 2", "NODE 3"], color=["#00ff1e", "#162347", "#dd4b39"] ) g.show(filename)
def show(graph, output_filename): """ Saves an HTML file locally containing a visualization of the graph, and returns a pyvis Network instance of the graph. """ g = Network(directed=graph.is_directed) g.add_nodes(graph.nodes) g.add_edges(graph.edges) g.show(output_filename) return g
def draw_net(package): sample, data = package net = Network() nodes = targeted_isotpye colors = list(map(lambda x: isotype_color[x], nodes)) # nodes = # sizes = data.groupby("isotype")["cluster"].c ount()[targeted_isotpye][nodes].to_list() # net.add_nodes(nodes, color = colors, size = sizes) net.add_nodes(nodes, color=colors) tmp = deepcopy(nodes) edges = [] sample_num = len(data) max = 0 while len(tmp) > 0: element_from = tmp.pop(0) for element_to in tmp: if set([element_from, element_to]) == set(["IGHA1", "IGHA2"]): continue set_x = set(data[data["isotype"] == element_from]["cluster"]) set_y = set(data[data["isotype"] == element_to]["cluster"]) x_and_y = len(set_x.intersection(set_y)) x_and_Ny = len(set_x - set_y) Nx_and_y = len(set_y - set_x) Ny_and_Nx = sample_num - len(set_x.union(set_y)) odd, p = fisher_exact([[x_and_y, Nx_and_y], [x_and_Ny, Ny_and_Nx]]) # p = -log10(p) # p = 2 if p < -log10(0.01) else p #卡阈值为0.01 width = x_and_y # width = 1 if width > 0 and width < 1 else width edges.append( [element_from, len(set_x), element_to, len(set_y), width, p]) edges = pd.DataFrame( edges, columns=["from", "from_size", "to", "to_size", "weight", "p"]) edges["qValue"] = qvalue(edges["p"])[1] edges["Sig"] = ["*" if y < 0.05 else "" for y in edges["qValue"]] edges["sample"] = sample edges = edges.set_index("sample", append=True) # edges.to_csv("./data/{}.csv".format(sample)) # edges = edges.set_index(["index",sample]) return edges
def create_network_kmeans(text, kmeans=[], tfidf=None, key_terms=None, freq_df=[], k_list=[2], n=20): ''' create html files with network graphs, where for each cluster it is stored the most frequent terms ''' if len(kmeans) == 0: if tfidf is None or key_terms is None: #determine tfidf, key_terms tfidf, key_terms = determine_tfidf(text) #determine kmeans for k in k_list: kmeans.append(k_means(tfidf, k)) if len(freq_df) == 0: #determine frequency for each key term freq_df = find_frequent_terms(key_terms, text) for c in range(len(k_list)): #number of clusters k = k_list[c] #clusters lables labels = pd.Series(kmeans[c].labels_.tolist()) #start network G = Network(height="750px", width="100%", font_color="black") for i in labels.unique(): #filter text items within the cluster filter_text = text.loc[labels == i].reset_index(drop=True) #add node with the cluster name, have the size as the number of items G.add_nodes(['Cluster ' + str(i)], value=[len(filter_text)]) #determine frequency of the key within cluster only new_freq_df = find_frequent_terms(key_terms, filter_text) #for the most frequent terms within the cluster for j in range(n): #get term term = new_freq_df['terms'][j] #add node with the term with the respective frequency overall as size G.add_node(str(term), value=freq_df.loc[freq_df['terms'] == term, ] ['count'].tolist()[0]) #add edge between cluster label and term G.add_edge('Cluster ' + str(i), str(term)) #create and html file with network graph G.show('_' + str(k) + "clusters.html")
def print_structure(structure, var2optimize, evidences): adjacency_matrix = bnlearnPac.amat(structure) # adjacency matrix vector = np.array(adjacency_matrix) nodes = bnlearnPac.nodes(structure) # nodes names positions = [] mod = 600 p = 0 lista = var2optimize + evidences + list( set(nodes) - set(var2optimize + evidences)) for i in lista: positions.append([ i, int(p / mod) * 200 + (random.randint(-20, 20)), p % mod + (random.randint(-20, 20)) ]) p = p + 150 color_map = [] for i in list(nodes): if i in var2optimize: color_map.append('red') if i in evidences: color_map.append('blue') if i == 'COSTE': color_map.append('green') Gr = Network("800px", "800px", directed=True) Gr.add_nodes(list(range(len(nodes))), title=nodes, x=[row[1] for row in positions], y=[row[2] for row in positions], color=color_map, label=nodes) for i in range(len(vector)): for j in range(len(vector[i])): if vector[i][j] == 1: Gr.add_edge( list(range(len(nodes)))[i], list(range(len(nodes)))[j]) for n in Gr.nodes: n.update({'physics': False}) # Gr.show_buttons(filter=['physics']) Gr.show_buttons() Gr.show("mygraph.html")
class Vis: def __init__(self, artists, adjacency): self.artists = artists self.adjacency = adjacency self.network = Network(height='1500px', width='1500px') def init_network(self): id = list(self.artists.keys()) label = [a['name'] for _, a in self.artists.items()] size = [a['popularity'] / 10 for _, a in self.artists.items()] self.network.add_nodes(id, label=label, size=size) [ self.network.add_edges([(key, v) for v in value]) for key, value in self.adjacency.items() ] def show_network(self): self.network.show('net.html')
def pyvis_guanxi(): from pyvis.network import Network net = Network() # 添加单个节点 net.add_node(1, label='node_1') net.add_node(2, label='node_2') # 添加属性,注意这⾥里里是添加多个节点,使⽤用add_nodes # 属性值传⼊入的是列列表,要保证列列表⻓长度与节点列列表⻓长度⼀一致 net.add_nodes([3, 4, 5], value=[10, 20, 30], title=["节点3", "节点4", "节点5"], x=[21.4, 54.2, 11.2], y=[100.2, 23.54, 32.1], label=["node_3", "node_4", "node_5"], color=["#00ff1e", "#162347", "#dd4b39"]) # ⼀一次添加⼀一条边 net.add_edge(1, 2) net.add_edge(1, 3) net.add_edge(1, 4) net.add_edge(2, 5) # 有权重边 net.add_edge(2, 1, wright=0.87) net.add_edge(2, 3, wright=0.5) ''' #结合networkx使⽤用 import networkx as nx # ⽣生成5个节点,每个点与其他所有点相互联系 nxg = nx.complete_graph(5) G = Network() G.from_nx(nxg) # 输出并⽣生成demo2.html G.show('demo2.html') ''' # 交互式显示,# 可对打开的html进⾏行行调整 net.show_buttons(filter_=['physics']) net.save_graph( 'app/templates/render/pyvis_guanxi.html') #输出并⽣生成pyvis_guanxi.html return render_template('render/pyvis_guanxi.html')
def make_graph(subjects: list): # read data df = pd.DataFrame() for subject in subjects: courses = pd.read_json(f"courses/{subject}.json") df = pd.concat([df, courses]) edge_list = make_edge_list(df) node_list, title_list = make_node_list(df) # make graph g = Network(directed=True, height="650px", width="100%", bgcolor="#222222", font_color="white") g.barnes_hut() # spring physics on the edges g.inherit_edge_colors(False) g.add_nodes(node_list, title=title_list) for edge in edge_list: g.add_edge(edge[0], edge[1], color="#94c4fc") # add neighbor data to node hover data for node in g.nodes: prereq = df[df["label"] == node["label"]]["prereq"] prereq = [] if prereq.empty else prereq.item() next_courses = g.neighbors(node["label"]) node["title"] += "<br>Prerequisites:<br>" \ + "<br>".join(prereq) \ + "<br>Next courses:<br>" \ + "<br>".join(next_courses) node["value"] = len(next_courses) + len(prereq) # highlight the node if it serves as a prerequisites for more than 5 course node["font"]["size"] = node["value"] * 5 if node["value"] >= 8: node["color"] = "red" return g
def vpcpeering(ctx, **kwargs): """Plot VPC Peering connections graph""" kwargs['service'] = f"AWS::EC2::VPCPeeringConnection%" kwargs['select'] = "configuration.requesterVpcInfo.ownerId" \ ", configuration.requesterVpcInfo.vpcId" \ ", configuration.accepterVpcInfo.vpcId" \ ", configuration.accepterVpcInfo.ownerId" \ ", configuration.vpcPeeringConnectionId" \ ", tags.tag" kwargs['pager'] = False ret = query(ctx, kwargs) net = Network(height="750px", width="100%") net.barnes_hut() net.show_buttons(filter_=['physics']) for peer in ret: src_id = peer['configuration']['requesterVpcInfo']['ownerId'] dst_id = peer['configuration']['accepterVpcInfo']['ownerId'] src_vpc = peer['configuration']['requesterVpcInfo']['vpcId'] dst_vpc = peer['configuration']['accepterVpcInfo']['vpcId'] tags = peer['tags'] tag = [] for t in tags: tag.append(t['tag']) net.add_nodes([str(src_vpc), str(dst_vpc)], title=[str(src_id), str(dst_id)], label=[str(src_vpc), str(dst_vpc)]) net.add_edge(str(src_vpc), str(dst_vpc), title=",".join(tag)) net.show("mygraph.html") ctx.log("Graph saved to './mygraph.html'")
def main(): filename = "" args = sys.argv if len(args) > 1: filename = args[1] # else: # filename = "../cypher/delillo_white_noise.cypher" lines = get_lines(filename) print(f"loaded {len(lines)} lines") header, project = get_metadata(lines) net = Network("95%", "100%", heading=header) net.show_buttons() nodes, edges, labels = get_nodes_and_edges(lines) node_weight = get_nodes_occurences(edges) print( f"collected {len(nodes)} nodes, {len(edges)} edges, {len(labels)} labels" ) # shapes = { # "Person": "triangle", # "Institution": "star", # "Everything else": "dot", # } # net.add_node("LEGEND", shape="circle") # for shape_key in shapes: # net.add_node(shape_key, shape=shapes[shape_key]) # net.add_edge("LEGEND", shape_key) label_set = set() for node in nodes: first_label = labels[node][0] label_set.add(first_label) # shape = shapes.get(first_label, "dot") group = first_label if first_label else "" net.add_node( node, label=nodes[node], group=group, size=(sqrt(node_weight[node]) * 5), ) for vars, title in edges: var1, var2 = vars net.add_nodes([var1, var2]) net.add_edge(var1, var2, title=title) # net.add_node("LEGEND", shape="circle", physics=False) # for label in label_set: # net.add_node(label, group=label) # net.add_edge("LEGEND", label, hidden=True) load_dir = os.path.split(filename)[0] save_file = "index.html" if project: save_dir = project else: base = os.path.basename(filename) pure = os.path.splitext(base)[0] save_file = pure + ".html" save_dir = "no_project_metadata" save_dir = os.path.join(load_dir, save_dir) if not os.path.exists(save_dir): os.mkdir(save_dir) net.barnes_hut( gravity=-4000, central_gravity=0.3, spring_strength=0.001, damping=0.09, overlap=1, ) net.show(os.path.join(save_dir, save_file)) print(f"saved into {save_file} in {save_dir}") if project: bash_line = f"surge . --domain {project}.surge.sh" # \nrm index.html" bash_file = os.path.join(save_dir, "s.sh") with open(bash_file, "w") as f: f.write(bash_line) os.chmod(bash_file, 509) print(f"bash file created") curr_dir = os.getcwd() chdir(save_dir) os.popen("s.sh") input("ENTER to remove reduntand directory") chdir("..") shutil.rmtree(save_dir) chdir(curr_dir) print("done")
except: print ("Creation of the directory %s failed, location probably already exists" % path_graphs) else: print ("Successfully created the directory %s " % path_graphs) ## All OS Graph By using `pyvis.network` module I could create beautiful interactive graphs. The graph is created from the graph I created in the `create_OS_graph` function the colors and sizes are also taken from that function. `barnes_hut` is the type of visualization used. # Get the needed data from the function OS_graph, colors, sizes, nodelist = create_OS_graph(extract_df_os) gr=Network(height=800, width=1200, notebook=True, bgcolor="#222222", font_color="white") # First add the nodes with its properties to the graph gr.add_nodes(nodelist, value=sizes, title=nodelist, color=colors) gr.barnes_hut() # Now connect the nodes based on the graph returned from the function gr.from_nx(OS_graph) gr.show("graphs/osquery_tables_OS_ALL_graph.html") ## Windows Graph Filtering used is OS dependent. I noticed that filtering needs some more fine-tuning this will be in the next release of this Notebook. ignore_list_w = ['name','path','type','version','size','version','description','status','state','label','class','source','device','mode','value','result','hardware_model','manufacturer','query','model','device_id','action','script_text','time','enabled', 'date','caption','publisher','active','autoupdate','flags','comment','data','registry','author','directory','license','summary','permissions'] #maybe filter out key not the same meaning in all tables #path can be used for some good joins but too noisy for now, example http://www.osdfcon.org/presentations/2016/Facebook-osquery.pdf Below we create the Windows-only graph, we first filter down the OS DataFrame we created earlier with the `ignore_list_w`.
import pandas as pd from pairwise_corr import read_nmf_data #ref: https://pyvis.readthedocs.io/en/latest/tutorial.html # #dummy data # users=["u1","u2","u3","u4"] # coor_matrix=[[1,1,0,0], # [1,1,0,0], # [0,0,1,1], # [0,0,1,1]] # real data filename = "/home/smollfish/Desktop/CSE573_Twitter_bot_detection/DataSet_filtered/nmf/nmf-for-week-1.pkl" users, values = read_nmf_data(filename) df = pd.DataFrame(values).transpose() coor_matrix = df.corr(method='pearson') coor_matrix[coor_matrix < .995] = 0 # filter net = Network() net.add_nodes(users, label=users) for i in range(0, len(coor_matrix)): for j in range(0, len(coor_matrix)): if (j <= i): continue else: if(coor_matrix[i][j]!=0): net.add_edge(users[i], users[j], label=coor_matrix[i][j]) net.toggle_physics(True) net.show("hello_graph.html")
#Analysis of each of the negative edges networks in the dataset #Repeat with positive edges networks for n in len(negative_edges): selected_edges = negative_edges[n] #Get list of nodes #Keep only the kinases names (i.e. remove the z-scores) from the list of edges nodes_bf = [(a, b) for a, b, c in selected_edges] #Flatten list of edges tuples to list of nodes nodes = list(itertools.chain(*nodes_bf)) #Remove duplicates from the list nodes_list = list(dict.fromkeys(nodes)) #Add nodes & edges to graph networkgraph.add_nodes(nodes_list, value=[10] * len(nodes_list), title=nodes_list, label=nodes_list) networkgraph.add_edges(selected_edges) #Show to which kinases each kinase is connected #Show to which kinases each kinase is connected kinases_map = networkgraph.get_adj_list() network_map = networkgraph.get_edges() for key, value in kinases_map.items(): kinases_map[key] = ([ functools.reduce(operator.add, ((x, str([(y["width"]) for y in network_map if x in y["from"] and key in y["to"] or x in y["to"] and key in y["from"]])))) for x in value
path = os.getcwd() + "/data/cora/" dataset = "cora" idx_features_labels = np.genfromtxt(f"{path}{dataset}.content", dtype=np.dtype(str)) edges_unordered = np.genfromtxt(f"{path}{dataset}.cites", dtype=int) idx = np.array(idx_features_labels[:, 0], dtype=int) idx_map = {j: i for i, j in enumerate(idx)} labels = list(idx_features_labels[:, -1]) edges = np.array(list(map(idx_map.get, edges_unordered.flatten())), dtype=int).reshape(edges_unordered.shape) color = [ '#f09494', '#eebcbc', '#72bbd0', '#91f0a1', '#629fff', '#bcc2f2', '#eebcbc' ] color_map = {label: color[i] for i, label in enumerate(set(labels))} colors = np.array([color_map[label] for label in labels]) num_node = 300 net = Network(heading="cora") net.add_nodes(list(range(len(labels[:num_node]))), label=labels[:num_node], color=colors[:num_node]) for edge in edges: if edge[0] < num_node and edge[1] < num_node: net.add_edge(int(edge[0]), int(edge[1])) net.show_buttons(filter_=['edges']) net.show("visualize/cora.html")
def plotNetwork( self, source: str, target: str, filename: str = "net.html", size: Tuple[int, int] = (500, 500), ): try: paths = nx.all_shortest_paths(self, source, target) except nx.NetworkXNoPath as e: print("No path") finally: paths = list(paths) nodes = list(set(chain.from_iterable(paths))) edges = [(p[i], p[i + 1]) for p in paths for i in range(0, len(p) - 1)] g = Network(height=size[0], width=size[1], directed=True) g.add_nodes( nodes, color=[ "red" if n == source else "green" if n == target else "blue" for n in nodes ], ) g.add_edges(edges) g.set_options(""" { "nodes": { "font": { "size": 9 }, "scaling": { "max": 36 }, "shadow": { "enabled": true } }, "edges": { "arrows": { "to": { "enabled": true, "scaleFactor": 1.4 } }, "smooth": false }, "layout": { "hierarchical": { "enabled": true, "sortMethod": "directed" } }, "interaction": { "keyboard": { "enabled": true } }, "physics": { "hierarchicalRepulsion": { "centralGravity": 0, "nodeDistance": 225 }, "minVelocity": 0.75, "solver": "hierarchicalRepulsion" } }""") return g
class MyNetwork: def __init__(self): self.lock = threading.RLock() self.g: ig.Graph = None # 网络对象 self.vg: pyvis.network.Network = None # 动态可视化网络对象 self.vertex_df: pd.DataFrame = None # 节点文件表 # 度、度分布、聚集系数、介数、中心性、度相关性 self.vertex_count = None # 节点数 self.edge_count = None # 边数 self.avg_degree = None # 平均度 self.max_degree = None # 最大度 self.degree_list = None # 节点度列表 self.diameter = None # 网络直径 self.avg_path_len = None # 平均路经长 self.clustering_coefficient = None # 聚集系数 self.density = None # 网络密度 self.betweenness = None # 介数中心性列表 self.closeness = None # 紧密中心性列表 self.clique_number = None # 团数量 self.page_rank = None # pagerank值 self.colors = ['#70f3ff', '#44cef6', '#3eede7', '#1685a9', '#177cb0', '#065279', '#003472', '#4b5cc4', '#a1afc9', '#2e4e7e', '#3b2e7e', '#4a4266', '#426666', '#425066', '#574266', '#8d4bbb', '#815463', '#815476', '#4c221b', '#003371', '#56004f', '#801dae', '#4c8dae', '#b0a4e3', '#cca4e3', '#edd1d8', '#e4c6d0', '#ff461f', '#ff2d51', '#f36838', '#ed5736', '#ff4777', '#f00056', '#ffb3a7', '#f47983', '#db5a6b', '#c93756', '#f9906f', '#f05654', '#ff2121', '#f20c00', '#8c4356', '#c83c23', '#9d2933', '#ff4c00', '#ff4e20', '#f35336', '#dc3023', '#ff3300', '#cb3a56', '#a98175', '#b36d61', '#ef7a82', '#ff0097', '#c32136', '#be002f', '#c91f37', '#bf242a', '#c3272b', '#9d2933', '#60281e', '#622a1d', '#bce672', '#c9dd22', '#bddd22', '#afdd22', '#a3d900', '#9ed900', '#9ed048', '#96ce54', '#00bc12', '#0eb83a', '#0eb83a', '#0aa344', '#16a951', '#21a675', '#057748', '#0c8918', '#00e500', '#40de5a', '#00e079', '#00e09e', '#3de1ad', '#2add9c', '#2edfa3', '#7fecad', '#a4e2c6', '#7bcfa6', '#1bd1a5', '#48c0a3', '#549688', '#789262', '#758a99', '#50616d', '#424c50', '#41555d', '#eaff56', '#fff143', '#faff72', '#ffa631', '#ffa400', '#fa8c35', '#ff8c31', '#ff8936', '#ff7500', '#ffb61e', '#ffc773', '#ffc64b', '#f2be45', '#f0c239', '#e9bb1d', '#d9b611', '#eacd76', '#eedeb0', '#d3b17d', '#e29c45', '#a78e44', '#c89b40', '#ae7000', '#ca6924', '#b25d25', '#b35c44', '#9b4400', '#9c5333', '#a88462', '#896c39', '#827100', '#6e511e', '#7c4b00', '#955539', '#845a33', '#ffffff', '#e9e7ef', '#f0f0f4', '#e9f1f6', '#f0fcff', '#e3f9fd', '#d6ecf0', '#fffbf0', '#f2ecde', '#fcefe8', '#fff2df', '#f3f9f1', '#e0eee8', '#e0f0e9', '#c0ebd7', '#bbcdc5', '#c2ccd0', '#bacac6', '#808080', '#75878a', '#88ada6', '#6b6882', '#725e82', '#3d3b4f', '#392f41', '#75664d', '#5d513c', '#665757', '#493131', '#312520', '#161823'] self.community_detect_algorithm_dict = None def get_network_properties_json(self) -> str: ''' 网络属性组成的json格式数据 :return: ''' directed = "有向图" if self.g.is_directed() else "无向图" data = f'{{' \ f'"IsDirected":\"{directed}\",' \ f'"VertexCount":{self.vertex_count},' \ f'"EdgeCount":{self.edge_count},' \ f'"AvgDegree":{self.avg_degree},' \ f'"MaxDegree":{self.max_degree},' \ f'"Diameter":{self.diameter},' \ f'"AvgPathLen":{self.avg_path_len},' \ f'"ClusteringCoefficient":{self.clustering_coefficient},' \ f'"Density":{self.density},' \ f'"CliqueCount":{self.clique_number},' \ f'}}' # f'"DegreeList":{self.degree_list},' \ # f'"BetweennessList":{self.betweenness},' \ # f'"ClosenessList":{self.closeness}' \ return data def generate_graph_from_dataframe(self, v: pd.DataFrame, e: pd.DataFrame, directed=False) -> None: ''' 从dataframe中初始化网络,并获取网络各类属性值 :param v: 节点表 :param e: 边表 :param directed: 是否是有向图 ''' # 加锁 self.lock.acquire() try: # self.vertex_df = v # 生成网络图对象 self.g = ig.Graph.DataFrame(edges=e, vertices=v, directed=directed) # igraph图对象 self.vg = Network(height='100%', width='100%', directed=directed) # 可视化图对象 self.vg.add_nodes([i for i in range(self.g.vcount())]) self.vg.add_edges(self.g.get_edgelist()) self.vg.show_buttons(filter_=['physics']) # 获取网络属性 self.vertex_count = self.g.vcount() # 节点数 self.edge_count = self.g.ecount() # 边数 self.avg_degree = np.average(self.g.degree()) # 平均度 self.max_degree = self.g.maxdegree() # 最大度 self.degree_list = self.g.degree() # 节点度列表 self.diameter = self.g.diameter() # 网络直径 self.avg_path_len = self.g.average_path_length() # 平均路经长 self.clustering_coefficient = self.g.transitivity_undirected() # 聚集系数 self.density = self.g.density() # 网络密度 self.clique_number = self.g.clique_number() # 团数量 self.g.vs.set_attribute_values(attrname='_度_', values=self.degree_list) # self.betweenness = self.g.betweenness() # 介数列表 self.betweenness = [-1 if np.isnan(v) else v for v in self.g.betweenness()] self.g.vs.set_attribute_values(attrname='_介数_', values=self.betweenness) # self.closeness = self.g.closeness() # 紧密中心性列表 self.closeness = [-1 if np.isnan(v) else v for v in self.g.closeness()] self.g.vs.set_attribute_values(attrname='_紧密中心性_', values=self.closeness) self.page_rank = [-1 if np.isnan(v) else v for v in self.g.pagerank()] self.g.vs.set_attribute_values(attrname='_PageRank_', values=self.page_rank) self.community_detect_algorithm_dict = { 'EdgeBetweenness': self.g.community_edge_betweenness, # 速度慢 'FastGreedy': self.g.community_fastgreedy, # 不能有重复边 'InfoMap': self.g.community_infomap, # 返回VertexClustering 'LabelPropagation': self.g.community_label_propagation, 'LeadingEigenvector': self.g.community_leading_eigenvector, # 速度较慢 # 'LeadingEigenvectorNaive': self.g.community_leading_eigenvector_naive, 'Leiden': self.g.community_leiden, 'MultiLevel': self.g.community_multilevel, 'SpinGlass': self.g.community_spinglass, # 速度慢 'WalkTrap': self.g.community_walktrap } finally: self.lock.release() def export_network(self, path: str, isHtml: int = 1, vertex_size: str = '', vertex_color: str = '', vertex_label: str = '', edge_weight: str = '', layout: str = 'kk' ): ''' 网络可视化 :param path: 保存路径 :param isHtml: 是否生成HTML文件或是svg图片 :param vertex_size: 节点大小 _默认_、_度_、_介数_、_紧密中心性_、其他数值属性 :param vertex_color: 节点颜色 _默认_#789262、_随机_、_度_、_介数_、_紧密中心性_、其他属性 :param vertex_label: 节点标签,默认为节点ID :param edge_weight: 边宽度 _默认_、其他数值属性 :param layout: 静态图布局 :param size: 静态图图像大小 ''' self.lock.acquire() try: if vertex_size != '': if vertex_size == '_默认_': vertex_size = None elif vertex_size == "_度_": vertex_size = [int(30 + d * 40) for d in self.value_map(self.degree_list, 1, 10)] elif vertex_size == "_介数_": vertex_size = [int(30 + d * 40) for d in self.value_map(self.betweenness, 1, 10)] elif vertex_size == "_紧密中心性_": vertex_size = [int(30 + d * 40) for d in self.value_map(self.closeness, 1, 10)] else: if vertex_size in self.g.vs.attribute_names(): try: x = np.array(self.g.vs.get_attribute_values(vertex_size)) vertex_size = [int(30 + d * 40) for d in self.value_map(x, 1, 10)] except Exception as e: vertex_size = None else: vertex_size = None if vertex_color != '': if vertex_color == '_默认_': # vertex_color = [f"#{vertex_color.split('#')[-1]}"] * self.vertex_count vertex_color = None elif vertex_color == "_随机_": colors = random.sample(self.colors, 20) vertex_color = [random.choice(colors) for _ in range(self.vertex_count)] elif vertex_color == "_度_": vertex_color = self.value2color(self.degree_list, 10) elif vertex_color == "_介数_": vertex_color = self.value2color(self.betweenness, 10) elif vertex_color == "_紧密中心性_": vertex_color = self.value2color(self.closeness, 10) else: if vertex_color in self.g.vs.attribute_names(): attr = self.g.vs.get_attribute_values(vertex_color) try: x = np.array(attr) vertex_color = self.value2color(x, 10) except Exception as e: vertex_color = self.category2color(attr) else: vertex_color = None if vertex_label == "_默认_": if 'name' in self.g.vs.attribute_names(): vertex_label = [str(i) for i in self.g.vs.get_attribute_values('name')] else: vertex_label = [str(i) for i in range(self.vertex_count)] else: if vertex_label in self.g.vs.attribute_names(): vertex_label = [str(i) for i in self.g.vs.get_attribute_values(vertex_label)] else: vertex_label = [str(i) for i in range(self.vertex_count)] if edge_weight in self.g.es.attribute_names(): if edge_weight == 'dist': edge_weight = [int(10 + i * 20) for i in 1.0 / np.array(self.value_map(self.g.es.get_attribute_values('dist'), 0.2, 1))] else: edge_weight = None else: edge_weight = None if isHtml == 1: nodes = self.vg.nodes ids = [i for i in range(self.vertex_count)] if vertex_size is not None: for i in ids: nodes[i]['value'] = vertex_size[i] if vertex_color is not None: for i in ids: nodes[i]['color'] = vertex_color[i] if vertex_label is not None: for i in ids: nodes[i]['label'] = vertex_label[i] for i in ids: nodes[i]['title'] = f"<br>ID:{i} 标签:{nodes[i]['label']} 度:{self.degree_list[i]}<br>" \ f"<br>介数:{self.betweenness[i]} 紧密中心度:{self.closeness[i]}<br>" if edge_weight is not None: i = 0 for edge in self.vg.edges: edge['value'] = edge_weight[i] i += 1 # self.vg.force_atlas_2based() self.vg.show_buttons() self.vg.write_html(path) # self.vg.show(path) else: if vertex_size is None: vertex_size = 20 if layout == '': layout = 'circle' ig.plot(self.g, target=path, vertex_size=vertex_size, vertex_color=vertex_color, vertex_label=vertex_label, edge_width=edge_weight, layout=layout) finally: self.lock.release() def community_detect(self, path: str, algorithm: str) -> int: ''' 执行社团发现算法,并生成网络图 :param path: :param algorithm: :return: ''' self.lock.acquire() try: community: [ig.VertexDendrogram, ig.VertexClustering] = self.community_detect_algorithm_dict[algorithm]() # count = community.optimal_count # 社团数量 if type(community) is ig.VertexClustering: vertex_community = community.membership else: vertex_community = community.as_clustering().membership # 节点所在社团 vertex_colors = self.category2color(vertex_community) # print(f'社团数:{max(vertex_community) + 1}') for i in range(self.vertex_count): self.vg.nodes[i]['color'] = vertex_colors[i] self.vg.nodes[i]['title'] = f"<br>ID:{i} 标签:{self.vg.nodes[i]['label']} 度:{self.degree_list[i]}<br>" \ f"<br>介数:{self.betweenness[i]} 紧密中心度:{self.closeness[i]}<br> <br>社团:{vertex_community[i]}<br>" self.vg.write_html(path) self.g.vs.set_attribute_values(attrname='_社团_', values=vertex_community) return max(vertex_community) + 1 finally: self.lock.release() def value_map(self, nums, target_min, target_max): ''' 将数值映射到另一个区间 :param nums: :param target_min: :param target_max: :return: ''' x = nums if type(nums) is not np.ndarray: x = np.array(nums) s_min = np.min(x) s_max = np.max(x) return target_min + (target_max - target_min) / (s_max - s_min) * (x - s_min) def value2color(self, nums, color_num): ''' 连续值映射为颜色 :param nums: 值 :param color_num: 映射的颜色数 :return: ''' x = self.value_map(nums, 1, color_num) colors = random.sample(self.colors, color_num + 1) return [colors[math.ceil(c)] for c in x] def category2color(self, category): ''' 离散值转颜色 :param category: 离散数据 :return: ''' u_c = list(set(category)) random.shuffle(self.colors) color_dict = {} i = 0 color_len = len(self.colors) for c in u_c: color_dict[c] = self.colors[i] i += 1 if i >= color_len: i = color_len - 1 return [color_dict[c] for c in category] def shortest_path_1_to_n(self, start_node_id) -> list: self.g.vs.find() return self.g.get_shortest_paths(v=start_node_id)
def visualize_pyvis(log_file: str, quad_systems_file: str, output_html="quad.html", width=int(screen_width * 0.8), height=int(screen_height * 0.8)): df = get_df(log_file) quad_systems = pickle.load(open(quad_systems_file, 'rb')) quad_systems = set(map(lambda s: clear_system_str(str(s)), quad_systems)) print(quad_systems) g = nx.from_pandas_edgelist(df, "from", "to", edge_attr="edge", create_using=nx.DiGraph) quad_edges = list(filter(lambda e: e[1] in quad_systems, g.edges)) quad_edges_labels = list( map(lambda e: g.get_edge_data(*e)['edge'], quad_edges)) g = nx.subgraph(g, get_processed_nodes(df)) g = nx.DiGraph(g) for e, l in zip(quad_edges, quad_edges_labels): g.add_edge(*e, edge=l) for node, attributes in g.nodes.items(): attributes['title'] = node g = nx.relabel_nodes(g, get_nodes_enumeration_in_process_order(df)) nodes_in_edges_count = nodes_with_multiple_in_edges_and_count(g) print( f"Count of nodes with multiple parents: {len(nodes_in_edges_count)} / {len(g.nodes)} " f"= {np.round(len(nodes_in_edges_count) / len(g.nodes) * 100, 1)}%") print(nodes_in_edges_count) for (node, attributes), n_parents in zip(g.nodes.items(), in_edges_count(g)): attributes['n_parents'] = n_parents if n_parents > 1: attributes['color'] = '#ffff66' # light yellow elif node == 0: attributes['color'] = '#32cd32' # lime green else: attributes['color'] = '#87cefa' # light sky bly if attributes['title'] in quad_systems: attributes['color'] = '#ff0000' # red nt = Network(directed=True, height=f"{height}px", width=f"{width}px", heading="Quadratization algorithm visualization") nt.add_nodes(list(g.nodes.keys()), title=[v['title'] for v in g.nodes.values()], color=[v['color'] for v in g.nodes.values()]) for f, t in g.edges: nt.add_edge(f, t, label=g.get_edge_data(f, t)['edge'], arrowStrikethrough=True) nt.show_buttons(filter_=['physics', 'layout']) nt.set_options(r""" var options = { "configure": { "enabled": true }, "edges": { "color": { "inherit": true }, "smooth": { "enabled": false, "type": "continuous" } }, "layout": { "hierarchical": { "enabled": true, "levelSeparation": 315, "nodeSpacing": 245, "treeSpacing": 325, "sortMethod": "directed" } }, "interaction": { "dragNodes": true, "hideEdgesOnDrag": false, "hideNodesOnDrag": false, "keyboard": { "enabled": true }, "navigationButtons": true, "tooltipDelay": 100 }, "physics": { "enabled": true, "stabilization": { "enabled": true, "fit": true, "iterations": 1000, "onlyDynamicEdges": false, "updateInterval": 50 }, "hierarchicalRepulsion": { "centralGravity": 0, "springLength": 180, "springConstant": 0.15, "nodeDistance": 380 }, "minVelocity": 0.75, "solver": "hierarchicalRepulsion" } } """) nt.show(output_html)
if __name__ == "__main__": net = Network() course_list_1 = ['Matematikai-alapok-GY', 'Programozas-EA-GY', 'Imperativ-programozas-EA-GY', 'Szamitogepes-rendszerek-EA-GY'] course_list_2 = ['Web-fejlesztes-Ea-Gy', 'Algoritmusok-es-adatszerkezetek-I-Ea', 'Objektumelvu-programozas-Ea-Gy', 'Algoritmusok-es-adatszerkezetek-I-Gy', 'Diszkret-matematika-I-Ea', 'Diszkret-matematika-I-Gy', 'Analizis-I-Gy', 'Analizis-I-Ea'] course_list_3 = ['Programozasi-nyelvek-Ea-Gy', 'Algoritmusok-es-adatszerkezetek-II-Gy', 'Algoritmusok-es-adatszerkezetek-II-Ea', 'Programozasi-nyelvek-I-Ea-Gy-C++', 'Kozgazdasagi-es-jogi-ismeretek-Ea', 'Funkcionalis-programozas-Ea-Gy', 'Programozasi-nyelvek-II-Ea-Gy-Java', 'Analizis-II-Ea', 'Analizis-II-Gy'] course_list_4 = ['Konkurrens-programozas-Ea-Gy', 'Operacios-rendszerek-Ea-Gy', 'Webprogramozas-Ea-Gy', 'A-szamitaselmelet-alapjai-I-Ea', 'A-szamitaselmelet-alapjai-I-Ea', 'Programozasi-technologia-Ea-Gy', 'Numerikus-modszerek-Ea', 'Numerikus-modszerek-Gy'] course_list_5 = ['Mesterseges-intelligencia-Ea', 'Adatbazisok-I-Ea', 'Adatbazisok-I-Gy', 'A-szamitaselmelet-alapjai-II-Gy', 'A-szamitaselmelet-alapjai-II-Ea', 'Diszkret-modellek-alkalmazasai-Gy', 'Valoszinusegszamitas-es-statisztika-Gy'] course_list_6 = ['Adatbazisok-II-Ea', 'Telekommunikacios-halozatok-Ea', 'Telekommunikacios-halozatok-Gy'] net.add_nodes(course_list_1, x = range(len(course_list_1)), y = [0] * len(course_list_1), color = ['#00ff1e'] * len(course_list_1)) net.add_nodes(course_list_2) #, x = range(len(course_list_1)), y = [0] * len(course_list_1)) #net.add_nodes(course_list_3) #net.add_nodes(course_list_4) #net.add_nodes(course_list_5) #net.add_nodes(course_list_6) """ net.add_node(1, label="Node 1") net.add_node(2) nodes = ["a", "b", "c", "d"] net.add_nodes(nodes) net.add_nodes("hello") g = Network() g.add_nodes([1,2,3], value=[10, 100, 400], title=["I am node 1", "node 2 here", "and im node 3"], x=[21.4, 54.2, 11.2],
def network(request, n, f): fileType = f inputFile = n htmlFile = "./NetworkViewer/networks/" + n.split(".")[0] + ".html" # The network parameters to be displayed are initiated. All parameters can not be calculated for all types of networks diameter = 0 clusteringCoeff = 0 cliques = 0 degree = 0 connectedComponents = 0 stronglyConnectedomponents = 0 # Different file types needs to be handeled in different ways ngx = None if fileType == 'geneSpider': # Gene spider matrix is read as a matrix that is then transposed, in order to get the directions right networkDf = pd.read_csv(os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'media/' + inputFile), sep=',', header=None) networkDfTransposed = networkDf.T nxg = nx.from_numpy_matrix(np.array(networkDfTransposed), create_using=nx.MultiDiGraph()) elif fileType == 'adjacencyList': # Adjacency lists/Edge lists are red as undirected pandas edgelists networkDf = pd.read_csv(os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'media/' + inputFile), sep='\s+', header=None) nxg = nx.from_pandas_edgelist(networkDf, source=0, target=1) elif fileType == 'directedAdjacencyList': # Directed edge lists need to be read as directed pandas edgelists networkDf = pd.read_csv(os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'media/' + inputFile), sep='\s+', header=None) nxg = nx.from_pandas_edgelist(networkDf, source=0, target=1, create_using=nx.MultiDiGraph()) elif fileType == 'adjacencyMatrix': # Adjacency matrix need to be read as an undirected matrix networkDf = pd.read_csv(os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'media/' + inputFile), sep=',', header=None) nxg = nx.from_numpy_matrix(np.array(networkDf)) elif fileType == 'directedAdjacencyMatrix': # Directed Adjacency matrix need to be read as a directed matrix networkDf = pd.read_csv(os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'media/' + inputFile), sep=',', header=None) nxg = nx.from_numpy_matrix(np.array(networkDf), create_using=nx.MultiDiGraph()) elif fileType == 'funCoup': # FunCoup-network-files contains several columns defining the evidence types and scores. Here, they are red as edge lists, only taking the genes and their edges into account networkDf = pd.read_csv(os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'media/' + inputFile), sep='\t') networkDfGenes = networkDf[['2:Gene1', '3:Gene2']] nxg = nx.from_pandas_edgelist(networkDfGenes, source='2:Gene1', target='3:Gene2') # The networkx-networks are drawn as png-images, since the pyvis can not handle other formats than html. pos = nx.spring_layout(nxg) nx.draw(nxg, pos, with_labels=True) # nx.draw_networkx_edge_labels(nxg, pos, with_labels = True) plt.savefig("./NetworkViewer/templates/NetworkViewer/networks/" + inputFile.split(".")[0] + ".png", format="PNG") plt.clf() # Initiating pyvis interactive network with customized graphics g = Network() g.barnes_hut(gravity=-2000, central_gravity=0.02, spring_length=1, spring_strength=0.000001, damping=0.09, overlap=0) g.toggle_physics(True) # Calculating network prooperties from the networkx graphs if nx.is_directed(nxg): g.directed = True stronglyConnectedomponents = nx.number_strongly_connected_components( nxg) else: connectedComponents = nx.number_connected_components(nxg) if connectedComponents == 1: diameter = nx.diameter(nxg, e=None) clusteringCoeffs = nx.clustering(nxg) clusteringCoeff = np.mean(list(clusteringCoeffs.values())) allCliques = nx.find_cliques(nxg) cliques = len(list(allCliques)) degrees = nxg.degree() degreesOnly = [] for node, degree in degrees: degreesOnly.append(degree) degree = np.mean(degreesOnly) maxDegree = np.max(degreesOnly) # Filling the pyvis graph with nodes and edges from the networkx graph. allNodes = list(nxg.nodes) allSizes = [] # The nodes gets sizes according to their degree for d in degreesOnly: allSizes.append(40 * (d / maxDegree)) g.add_nodes(allNodes, size=allSizes) # The edges gets width according to their weights allEdges = nxg.edges(data=True) edges = [] for a, b, w in allEdges: edges.append((a, b, w.get('weight'))) g.add_edges(edges) g.height = "100%" g.width = "100%" # The pyvis graph is saved as an html file, that is embedded in the network viewer-vindow g.save_graph("./NetworkViewer/templates/NetworkViewer/networks/" + inputFile.split(".")[0] + ".html") # Exporting network properties to the html-view context = { "diameter": ("%.2f" % diameter), "clustering": ("%.2f" % clusteringCoeff), "cliques": ("%.2f" % cliques), "degree": ("%.2f" % degree), "connectedComponents": connectedComponents, "stronglyConnectedomponents": stronglyConnectedomponents, "inputFile": inputFile, "htmlFile": htmlFile } return render(request, 'NetworkViewer/viewNetwork.html', context)
def show3(graph, output_filename): g = Network(directed=False) g.add_nodes(graph.nodes) g.add_edges(graph.edges) g.show(output_filename)
def create_and_return_flow_diagram(request_dict, path_to_reactions=path_to_reactions, path_to_template=path_to_template, csv_results_path=csv_results_path_default): global userSelectedMinMax global minAndMaxOfSelectedTimeFrame global previous_vals global csv_results_path_default logging.info("using csv_results_path: " + csv_results_path) csv_results_path_default = csv_results_path if (('maxMolval' in request_dict and 'minMolval' in request_dict) and (request_dict['maxMolval'] != '' and request_dict['minMolval'] != '') and (request_dict['maxMolval'] != 'NULL' and request_dict['minMolval'] != 'NULL')): userSelectedMinMax = [ float(request_dict["minMolval"]), float(request_dict["maxMolval"])] logging.info("new user selected min and max: " + str(userSelectedMinMax)) if 'startStep' not in request_dict: request_dict.update({'startStep': 1}) if 'maxArrowWidth' not in request_dict: request_dict.update({'maxArrowWidth': 10}) minAndMaxOfSelectedTimeFrame = [999999999999, -1] # load csv file csv = pd.read_csv(csv_results_path) start_step = int(request_dict['startStep']) end_step = int(request_dict['endStep']) # scale with correct scaling function scale_type = request_dict['arrowScalingType'] max_width = request_dict['maxArrowWidth'] previousMin = float(request_dict["currentMinValOfGraph"]) previousMax = float(request_dict["currentMaxValOfGraph"]) previous_vals = [previousMin, previousMax] isPhysicsEnabled = request_dict['isPhysicsEnabled'] if isPhysicsEnabled == 'true': max_width = 2 # change for max width with optimized performance # load species json and reactions json with open(path_to_reactions, 'r') as f: reactions_data = json.load(f) # completely new method of creating nodes and filtering elements selected_species = request_dict['includedSpecies'].split(',') blockedSpecies = request_dict['blockedSpecies'].split(',') (network_content, raw_yields, edgeColors, quantities, minVal, maxVal, raw_yield_values, species_colors, species_sizes, total_quantites, reaction_names_on_hover) = new_find_reactions_and_species( selected_species, reactions_data, blockedSpecies, csv, start_step, end_step, max_width, scale_type, csv_results_path) # add edges and nodes # force network to be 100% width and height before it's sent to page net = Network(height='100%', width='100%', directed=True) # make it so we can manually change arrow colors net.inherit_edge_colors(False) shouldMakeSmallNode = False if isPhysicsEnabled == "true": net.toggle_physics(False) net.force_atlas_2based() else: net.force_atlas_2based(gravity=-200, overlap=1) reac_nodes = network_content['reaction_nodes'] hover_names = reaction_names_on_hover names = [getReactName(hover_names, x) for x in reac_nodes] colors = [species_colors[x] for x in network_content['species_nodes']] if shouldMakeSmallNode: net.add_nodes(names, color=["#FF7F7F" for x in reac_nodes], size=[ 10 for x in list(reac_nodes)], title=names) net.add_nodes(network_content['species_nodes'], color=colors, size=[ 10 for x in list(network_content['species_nodes'])]) else: net.add_nodes(names, color=[ "#FF7F7F" for x in reac_nodes], title=names) net.add_nodes(network_content['species_nodes'], color=colors, size=[species_sizes[x] for x in list( network_content['species_nodes'])]) net.set_edge_smooth('dynamic') # add edges individually so we can modify contents i = 0 values = edgeColors for edge in network_content['edges']: unbeu1 = unbeautifyReaction(edge[0]) unbeu2 = unbeautifyReaction(edge[1]) val = unbeu1+"__TO__"+unbeu2 flux = str(raw_yields[unbeu1+"__TO__"+unbeu2]) colorVal = "" try: colorVal = values[val] except KeyError: colorVal = values[val.replace('->', '-')] if colorVal == "#e0e0e0": # don't allow blocked edge to show value on hover if "→" in edge[0]: be1 = beautifyReaction(reaction_names_on_hover[unbeu1]) net.add_edge(be1, edge[1], color=colorVal, width=edge[2]) elif "→" in edge[1]: try: be2 = beautifyReaction(reaction_names_on_hover[unbeu2]) net.add_edge(edge[0], be2, color=colorVal, width=edge[2]) except KeyError: be2 = beautifyReaction(unbeu2) net.add_edge(edge[0], be2, color=colorVal, width=edge[2]) else: net.add_edge(edge[0], edge[1], color=colorVal, width=edge[2]) else: # hover over arrow to show value for arrows within range # check if value is reaction by looking for arrow if "→" in edge[0]: be1 = beautifyReaction(reaction_names_on_hover[unbeu1]) net.add_edge(be1, edge[1], color=colorVal, width=float( edge[2]), title="flux: "+flux) elif "→" in edge[1]: try: be2 = beautifyReaction(reaction_names_on_hover[unbeu2]) net.add_edge(edge[0], be2, color=colorVal, width=float( edge[2]), title="flux: "+flux) except KeyError: be2 = beautifyReaction(unbeu2) net.add_edge(edge[0], be2, color=colorVal, width=float( edge[2]), title="flux: "+flux) else: net.add_edge(edge[0], edge[1], color=colorVal, width=float(edge[2]), title="flux: "+flux) i = i+1 net.show(path_to_template) if minAndMaxOfSelectedTimeFrame[0] == minAndMaxOfSelectedTimeFrame[1]: minAndMaxOfSelectedTimeFrame = [0, maxVal] with open(path_to_template, 'r+') as f: ############################################# # here we are going to replace the contents # ############################################# a = """<script> network.on("stabilizationIterationsDone", function () { network.setOptions( { physics: false } ); }); </script>""" logging.debug("((DEBUG)) [min,max] of selected time frame: " + str(minAndMaxOfSelectedTimeFrame)) logging.debug("((DEBUG)) [min,max] given by user: "******"""<script> parent.document.getElementById("flow-start-range2").value = "NULL"; parent.document.getElementById("flow-end-range2").value = "NULL"; console.log("inputting NULL");""" else: a = '<script>\ parent.document.getElementById("flow-start-range2").value = \ "'+formattedMinOfSelected+'";' a += 'parent.document.getElementById("flow-end-range2").value = \ "'+str( formattedMaxOfSelected)+'";' a += """ currentMinValOfGraph = """+formattedPrevMin+"""; currentMaxValOfGraph = """+formattedPrevMax+"""; """ if (str(formattedPrevMin) != str(formattedMinOfSelected) or str(formattedPrevMax) != str(formattedMaxOfSelected) or previousMax == 1): logging.debug("previousMin:" + str(formattedPrevMin) + "does not equal " + str(formattedMinOfSelected)) logging.debug("previousMax: " + str(formattedPrevMax) + " does not equal " + str(formattedMaxOfSelected)) logging.debug("previousMin: " + str(previousMin) + " equals 0") logging.debug("previousMax: " + str(previousMax) + " equals 1") a += 'parent.document.getElementById("flow-start-range2").value =\ "'+str(formattedMinOfSelected)+'";\ parent.document.getElementById("flow-end-range2").value =\ "'+str(formattedMaxOfSelected)+'";' a += ('parent.reloadSlider("'+str(formattedMinOfSelected)+'","' + str(formattedMaxOfSelected)+'", "'+str( formattedMinOfSelected)+'", "' + str(formattedMaxOfSelected)+'", '+str(get_step_length(csv_results_path))+');</script>') else: logging.debug("looks like min and max are the same") isNotDefaultMin = int(userSelectedMinMax[0]) != 999999999999 isNotDefaultmax = int(userSelectedMinMax[1]) != -1 block1 = 'parent.document.getElementById("flow-start-range2")' rangeId = block1+'.value = ' if isNotDefaultmax or isNotDefaultMin: a += (rangeId+str( formattedUserMin)+'"; \ '+rangeId+'"'+formattedUserMax+'";') block1 = 'parent.reloadSlider("' + formattedUserMin + '", "' fmos = str(formattedMinOfSelected) block2 = formattedUserMax + '", "' + fmos block3 = '", "' + formattedMaxOfSelected + '", '+str(get_step_length(csv_results_path))+');\ </script>' a += block1 + block2 + block3 else: fmos = formattedMinOfSelected block1 = 'parent.reloadSlider("' + fmos + '", "' block2 = formattedMaxOfSelected + '", "' + str(fmos) a += block1 + '", "' + formattedMaxOfSelected + '", '+str(get_step_length(csv_results_path))+');</script>' if isPhysicsEnabled == 'true': # add options to reduce text size a += \ """<script> var container = document.getElementById("mynetwork"); var options = {physics: false, nodes: { shape: "dot", size: 10, font: {size: 5} } }; var network = new vis.Network(container, data, options); </script>""" lines = f.readlines() for i, line in enumerate(lines): # find a pattern so that we can add next to that line if line.startswith('</script>'): lines[i] = lines[i]+a f.truncate() f.seek(0) # rewrite into the file for line in lines: f.write(line) f.close() # read from file and return the contents with open(path_to_template, 'r') as f: return f.read()
[table['foreign_keys'] + [table['name']] for table in db_info if len(table['foreign_keys']) > 0])) db_info = [info for info in db_info if info['name'] in connected_nodes] documents = [document_from_database_info(info) for info in db_info] k = 10 labels = tfidf_kmeans(documents, k) # labels = get_wordnet_labels(documents) vis_network = Network(height="100%", width="70%") colors = get_different_colors(max(labels)+1) # labels from k means color_labels = [colors[l] for l in labels] # services use this tables # color_labels = ["#FF0000" if len(get_services_from_table(x['name'])) > 0 else "#DDDDDD" for x in db_info] vis_network.add_nodes([x['name'] for x in db_info], color=color_labels) for table in db_info: for key in table['foreign_keys']: foreign_key_graph.add_edge(table['name'], key) if key in vis_network.get_nodes(): vis_network.add_edge(table['name'], key) # git_network = Network(height="750px", width="100%") # git_network.from_nx(foreign_key_graph) # git_network.show_buttons(filter_=['physics']) # git_network.show("network_outputs/foreign_key.html") vis_network.show_buttons(filter_=['physics']) vis_network.show("network_outputs/foreign_key.html")
'#d3b17d', '#e29c45', '#a78e44', '#c89b40', '#ae7000', '#ca6924', '#b25d25', '#b35c44', '#9b4400', '#9c5333', '#a88462', '#896c39', '#827100', '#6e511e', '#7c4b00', '#955539', '#845a33', '#ffffff', '#e9e7ef', '#f0f0f4', '#e9f1f6', '#f0fcff', '#e3f9fd', '#d6ecf0', '#fffbf0', '#f2ecde', '#fcefe8', '#fff2df', '#f3f9f1', '#e0eee8', '#e0f0e9', '#c0ebd7', '#bbcdc5', '#c2ccd0', '#bacac6', '#808080', '#75878a', '#88ada6', '#6b6882', '#725e82', '#3d3b4f', '#392f41', '#75664d', '#5d513c', '#665757', '#493131', '#312520', '#161823' ] random.shuffle(colors) print(colors[:5]) vg = Network(height='100%', width='100%', heading='交通网络图', directed=True) vg.add_nodes( [i for i in range(g.vcount())], value=[20 + d * 15 for d in g.degree()], # 节点大小 label=g.vs.get_attribute_values('Label'), # 节点标签,显示在点下方,缩小自动隐藏 color=[random.choice(colors[:20]) for i in range(g.vcount())]) vg.add_edges(g.get_edgelist()) # for edge in vg.edges: # # edge['value'] = random.randint(1,20) # edge['color'] = 'red' # # vg.set_edge_smooth(smooth_type='dynamic') for node in vg.nodes: # node['color'] = 'red' # node["title"] = "Neighbors:<br>" + "<br>".join(neighbor_map[node["id"]]) # 节点标题,鼠标移动到点上面时显示 node["title"] = f"<br>ID:{node['id']} Label:{node['label']}<br>" # vg.set_template() # vg.force_atlas_2based()
def show(graph, output_filename): g = Network(directed=True) g.add_nodes(graph.nodes) g.add_edges(graph.edges) g.show(output_filename) return g
from pyvis.network import Network from colour import Color if __name__ == '__main__': # reading the data with open('profile_list.json') as json_file: profile_list = json.load(json_file) tics = list(profile_list.keys()) n_tics = len(tics) red = Color("red") colors = list(red.range_to(Color("green"), n_tics)) G = Network() G.add_nodes(tics, color =[_.hex_l for _ in colors]) tics_comb = combinations(tics, 2) # pandas dataframe for the similarity matrix M = pd.DataFrame(index=tics, columns=tics) W = pd.DataFrame(index=tics, columns=tics) for _ in tics_comb: d = textdistance.jaro.distance(profile_list[_[0]], profile_list[_[1]]) M[_[1]][_[0]] = M[_[0]][_[1]] = d if d > 0.25: s = 0.0 else: s = np.log(d) / np.log(1 - d) G.add_edge(_[0],_[1], value=s)