def drawActivityResourceGraph_advanced(self, ActivityRscMatrix, weight_threshold, directed, splitted): rows, cols = np.where(ActivityRscMatrix > weight_threshold) weights = list(); for x in range(len(rows)): weights.append(ActivityRscMatrix[rows[x]][cols[x]]) got_net = Network(height="750px", width="100%", bgcolor="black", font_color="#f57b2b", directed= directed) # set the physics layout of the network got_net.barnes_hut() edge_data = zip(rows, cols, weights) counter = 1 for e in edge_data: src = self.activityList[e[0]] # convert ids to labels dst = self.resourceList[e[1]] w = e[2] # I have to add some options here, there is no parameter highlight = {'border': "#3de975", 'background': "#41e9df"} if(splitted): got_net.add_node(src, src, title=src, labelHighlightBold=True, color={'highlight': highlight},shape='square') got_net.add_node(dst+ "__" +str(counter) , dst , title=dst , labelHighlightBold=True, color={'border': "#dd4b39", 'background': "#dd4b39", 'highlight': highlight}) got_net.add_edge(src, dst+ "__" +str(counter), value=w, title=w) counter +=1 else: got_net.add_node(src, src, title=src, labelHighlightBold=True, color={'highlight': highlight},shape='square') got_net.add_node(dst , dst, title=dst , labelHighlightBold=True, color={'border': "#dd4b39", 'background': "#dd4b39", 'highlight': highlight}) got_net.add_edge(src, dst , value=w, title=w) neighbor_map = got_net.get_adj_list() dict = got_net.get_edges() self.getResourceList() # add neighbor data to node hover data for node in got_net.nodes: counter = 0 if(directed): node["title"] = "<h3>" + node["title"] + " Output Links: </h3>" else: node["title"] = "<h3>" + node["title"] + " Links: </h3>" for neighbor in neighbor_map[node["id"]]: if(counter % 10 == 0): node["title"] += "<br>::: " + neighbor else: node["title"] += " ::: " + neighbor node["value"] = len(neighbor_map[node["id"]]) counter += 1 got_net.show_buttons(filter_=['nodes', 'edges', 'physics']) got_net.show_buttons(filter_=['physics']) got_net.show("PMSocial.html")
def drawRscRscGraph_advanced(self, RscRscMatrix, weight_threshold, directed, encryption): rows, cols = np.where(RscRscMatrix > weight_threshold) weights = list(); for x in range(len(rows)): weights.append(RscRscMatrix[rows[x]][cols[x]]) #got_net = Network(height="750px", width="100%", bgcolor="white", font_color="#3de975", directed=directed) got_net = Network(height="750px", width="100%", bgcolor="white", font_color="black", directed=directed) ##f57b2b # set the physics layout of the network got_net.barnes_hut() edge_data = zip(rows, cols, weights) for e in edge_data: src = self.resourceList[e[0]] # convert ids to labels dst = self.resourceList[e[1]] w = e[2] if(encryption): src = Utilities.AES_ECB_Encrypt(self,src.encode('utf-8')) dst = Utilities.AES_ECB_Encrypt(self,dst.encode('utf-8')) # I have to add some options here, there is no parameter highlight = {'border': "#3de975", 'background': "#41e9df"} #color = {'border': "#000000", 'background': "#123456"} got_net.add_node(src, src, title=src, labelHighlightBold=True, color={'highlight': highlight}) got_net.add_node(dst, dst, title=dst , labelHighlightBold=True, color={'highlight': highlight}) got_net.add_edge(src, dst, value=w, title=w) neighbor_map = got_net.get_adj_list() dict = got_net.get_edges() self.getResourceList() # add neighbor data to node hover data for node in got_net.nodes: counter = 0 if(directed): node["title"] = "<h3>" + node["title"] + " Output Links: </h3>" else: node["title"] = "<h3>" + node["title"] + " Links: </h3>" for neighbor in neighbor_map[node["id"]]: if(counter % 10 == 0): node["title"] += "<br>::: " + neighbor else: node["title"] += " ::: " + neighbor node["value"] = len(neighbor_map[node["id"]]) counter += 1 got_net.show_buttons(filter_=['nodes', 'edges', 'physics']) got_net.show_buttons(filter_=['physics']) got_net.show("PMSocial.html")
def apply(metric_values, parameters=None): """ Perform SNA visualization starting from the Matrix Container object and the Resource-Resource matrix Parameters ------------- metric_values Value of the metrics parameters Possible parameters of the algorithm, including: - Parameters.WEIGHT_THRESHOLD -> the weight threshold to use in displaying the graph Returns ------------- temp_file_name Name of a temporary file where the visualization is placed """ from pyvis.network import Network if parameters is None: parameters = {} weight_threshold = exec_utils.get_param_value(Parameters.WEIGHT_THRESHOLD, parameters, 0) directed = metric_values[2] temp_file_name = get_temp_file_name("html") rows, cols = np.where(metric_values[0] > weight_threshold) weights = list() for x in range(len(rows)): weights.append(metric_values[0][rows[x]][cols[x]]) got_net = Network(height="750px", width="100%", bgcolor="black", font_color="#3de975", directed=directed) # set the physics layout of the network got_net.barnes_hut() edge_data = zip(rows, cols, weights) for e in edge_data: src = metric_values[1][e[0]] # convert ids to labels dst = metric_values[1][e[1]] w = e[2] # I have to add some options here, there is no parameter highlight = {'border': "#3de975", 'background': "#41e9df"} # color = {'border': "#000000", 'background': "#123456"} got_net.add_node(src, src, title=src, labelHighlightBold=True, color={'highlight': highlight}) got_net.add_node(dst, dst, title=dst, labelHighlightBold=True, color={'highlight': highlight}) got_net.add_edge(src, dst, value=w, title=w) neighbor_map = got_net.get_adj_list() dict = got_net.get_edges() # add neighbor data to node hover data for node in got_net.nodes: counter = 0 if directed: node["title"] = "<h3>" + node["title"] + " Output Links: </h3>" else: node["title"] = "<h3>" + node["title"] + " Links: </h3>" for neighbor in neighbor_map[node["id"]]: if (counter % 10 == 0): node["title"] += "<br>::: " + neighbor else: node["title"] += " ::: " + neighbor node["value"] = len(neighbor_map[node["id"]]) counter += 1 got_net.show_buttons(filter_=['nodes', 'edges', 'physics']) got_net.write_html(temp_file_name) return temp_file_name
tot_export = sum(list(map(isemptycell, [y3, y4, y5, y6, y7, y8, y9]))) #Creation of Edges a.add_edge(i, j, physics=False, width=tot_export // 10**10, weight=tot_export, title=product) # G.add_node(i) # G.add_node(j) G.add_edge(i, j, weight=tot_export) #Complete dictionary of edges neighbor_map = a.get_edges() #Parsing the Graph for e_node in neighbor_map: if outd.get(e_node['from']) == None: outd[e_node['from']] = {e_node['to']} else: outd[e_node['from']].add(e_node['to']) if ind.get(e_node['to']) == None: ind[e_node['to']] = {e_node['from']} else: ind[e_node['to']].add(e_node['from']) if outproduct.get(e_node['title']) == None: outproduct[e_node['title']] = {}
random_weight = random.randint(1, 5) G.add_edge(random_node_from, random_node_to, weight=random_weight) number_of_nodes = st.slider("Number of nodes?", 0, 200, 10) number_of_edges = st.slider("Number of edges?", 1, 400, 20) G = nx.DiGraph() # initilize the graph bidirectional for x in range(number_of_nodes): generate_random_node(G) for x in range(number_of_edges): generate_random_edge(G) render_demo = st.button("Render Sample Node Network") if (render_demo): nt = Network('500px', '500px') nt.from_nx(G) nt.inherit_edge_colors(False) for ix in nt.get_edges(): ix["width"] = ix["weight"] nt.write_html('demo.html') print(nt) # display(HTML('demo.html')) HtmlFile = open("demo.html", 'r', encoding='utf-8') source_code = HtmlFile.read() components.html(source_code, height=700)
def configure_graph(self, graph): net = Network("70%", "70%") net.from_nx(graph) net.show_buttons() nx_edges = list(graph.edges(data="label")) nx_edges.sort(key=lambda edge: edge[0]) py_vis_edges = net.get_edges() py_vis_edges.sort(key=lambda edge: edge["from"]) for i, edge in enumerate(py_vis_edges): edge["label"] = nx_edges[i][2] if (edge["from"].startswith("Step:") and edge["from"].startswith("Step:")): edge["dashes"] = True out_degrees = list(graph.out_degree()) out_degrees.sort(key=lambda tup: tup[0]) nodes = net.nodes nodes.sort(key=lambda node: node["id"]) for i, node in enumerate(nodes): node["shape"] = "ellipse" if "Scenario:" in node['id']: node["color"] = { "background": "red", "border": "red", "highlight": { "background": "rgba(255,186,96,1)" } } node["level"] = 1 node["label"] = node["label"].replace("Scenario:", "") elif out_degrees[i][1] > 0: node["color"] = { "background": "green", "border": "green", "highlight": { "background": "rgba(255,186,96,1)" } } node["level"] = 2 node["label"] = node["label"].replace("Step:", "") elif "Step:" in node['id']: node["color"] = { "background": "blue", "border": "blue", "highlight": { "background": "rgba(255,186,96,1)" } } node["level"] = 3 node["label"] = node["label"].replace("Step:", "") label = node["label"].split(" ") for i, word in enumerate(label): if i != 0 and i % 5 == 0: label[i] = word + '\n' node["label"] = " ".join(label) net.show(f"{self.config.OUTPUT_DIR}/graph.html")
#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 ]) nodes_numerical = {nodes_all[n]: n for n in range(len(nodes_all))} nodes_names = dict(zip(list(nodes_numerical.values()), nodes_all)) edges_numerical = [(nodes_numerical[selected_edges[n][0]], nodes_numerical[selected_edges[n][1]], abs(selected_edges[n][2]))